felf.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987
  1. /*
  2. * Copyright : (C) 2022 Phytium Information Technology, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is OPEN SOURCE software: you can redistribute it and/or modify it
  6. * under the terms of the Phytium Public License as published by the Phytium Technology Co.,Ltd,
  7. * either version 1.0 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY;
  10. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  11. * See the Phytium Public License for more details.
  12. *
  13. *
  14. * FilePath: felf.c
  15. * Date: 2021-08-31 11:16:59
  16. * LastEditTime: 2022-02-17 18:05:16
  17. * Description:  This file is for providing elf functions.
  18. *
  19. * Modify History:
  20. * Ver   Who        Date         Changes
  21. * ----- ------     --------    --------------------------------------
  22. * 1.0 zhugengyu 2022/10/27 rename file name
  23. */
  24. #include <string.h>
  25. #include "fkernel.h"
  26. #include "felf.h"
  27. #include "fcache.h"
  28. #include "fdebug.h"
  29. #include "fprintk.h"
  30. /* This version doesn't work for 64-bit ABIs - Erik */
  31. /* These typedefs need to be handled better */
  32. typedef u32 Elf32_Addr; /* Unsigned program address */
  33. typedef u32 Elf32_Off; /* Unsigned file offset */
  34. typedef s32 Elf32_Sword; /* Signed large integer */
  35. typedef u32 Elf32_Word; /* Unsigned large integer */
  36. typedef u16 Elf32_Half; /* Unsigned medium integer */
  37. /* 64-bit ELF base types */
  38. typedef u64 Elf64_Addr;
  39. typedef u16 Elf64_Half;
  40. typedef s16 Elf64_SHalf;
  41. typedef u64 Elf64_Off;
  42. typedef s32 Elf64_Sword;
  43. typedef u32 Elf64_Word;
  44. typedef u64 Elf64_Xword;
  45. typedef s64 Elf64_Sxword;
  46. /* e_ident[] identification indexes */
  47. #define EI_MAG0 0 /* file ID */
  48. #define EI_MAG1 1 /* file ID */
  49. #define EI_MAG2 2 /* file ID */
  50. #define EI_MAG3 3 /* file ID */
  51. #define EI_CLASS 4 /* file class */
  52. #define EI_DATA 5 /* data encoding */
  53. #define EI_VERSION 6 /* ELF header version */
  54. #define EI_OSABI 7 /* OS/ABI specific ELF extensions */
  55. #define EI_ABIVERSION 8 /* ABI target version */
  56. #define EI_PAD 9 /* start of pad bytes */
  57. #define EI_NIDENT 16 /* Size of e_ident[] */
  58. /* ELF Header */
  59. typedef struct
  60. {
  61. unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
  62. Elf32_Half e_type; /* object file type */
  63. Elf32_Half e_machine; /* machine */
  64. Elf32_Word e_version; /* object file version */
  65. Elf32_Addr e_entry; /* virtual entry point */
  66. Elf32_Off e_phoff; /* program header table offset */
  67. Elf32_Off e_shoff; /* section header table offset */
  68. Elf32_Word e_flags; /* processor-specific flags */
  69. Elf32_Half e_ehsize; /* ELF header size */
  70. Elf32_Half e_phentsize; /* program header entry size */
  71. Elf32_Half e_phnum; /* number of program header entries */
  72. Elf32_Half e_shentsize; /* section header entry size */
  73. Elf32_Half e_shnum; /* number of section header entries */
  74. Elf32_Half e_shstrndx; /* section header table's "section
  75. header string table" entry offset */
  76. } Elf32_Ehdr;
  77. typedef struct
  78. {
  79. unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
  80. Elf64_Half e_type; /* object file type */
  81. Elf64_Half e_machine; /* machine */
  82. Elf64_Word e_version; /* object file version */
  83. Elf64_Addr e_entry; /* virtual entry point */
  84. Elf64_Off e_phoff; /* program header table offset */
  85. Elf64_Off e_shoff; /* section header table offset */
  86. Elf64_Word e_flags; /* processor-specific flags */
  87. Elf64_Half e_ehsize; /* ELF header size */
  88. Elf64_Half e_phentsize; /* program header entry size */
  89. Elf64_Half e_phnum; /* number of program header entries */
  90. Elf64_Half e_shentsize; /* section header entry size */
  91. Elf64_Half e_shnum; /* number of section header entries */
  92. Elf64_Half e_shstrndx; /* section header table's "section
  93. header string table" entry offset */
  94. } Elf64_Ehdr;
  95. /* Section Header */
  96. typedef struct
  97. {
  98. Elf32_Word sh_name; /* name - index into section header
  99. string table section */
  100. Elf32_Word sh_type; /* type */
  101. Elf32_Word sh_flags; /* flags */
  102. Elf32_Addr sh_addr; /* address */
  103. Elf32_Off sh_offset; /* file offset */
  104. Elf32_Word sh_size; /* section size */
  105. Elf32_Word sh_link; /* section header table index link */
  106. Elf32_Word sh_info; /* extra information */
  107. Elf32_Word sh_addralign; /* address alignment */
  108. Elf32_Word sh_entsize; /* section entry size */
  109. } Elf32_Shdr;
  110. typedef struct
  111. {
  112. Elf64_Word sh_name; /* name - index into section header
  113. string table section */
  114. Elf64_Word sh_type; /* type */
  115. Elf64_Xword sh_flags; /* flags */
  116. Elf64_Addr sh_addr; /* address */
  117. Elf64_Off sh_offset; /* file offset */
  118. Elf64_Xword sh_size; /* section size */
  119. Elf64_Word sh_link; /* section header table index link */
  120. Elf64_Word sh_info; /* extra information */
  121. Elf64_Xword sh_addralign; /* address alignment */
  122. Elf64_Xword sh_entsize; /* section entry size */
  123. } Elf64_Shdr;
  124. /* Symbol Table Entry */
  125. typedef struct
  126. {
  127. Elf32_Word st_name; /* name - index into string table */
  128. Elf32_Addr st_value; /* symbol value */
  129. Elf32_Word st_size; /* symbol size */
  130. unsigned char st_info; /* type and binding */
  131. unsigned char st_other; /* 0 - no defined meaning */
  132. Elf32_Half st_shndx; /* section header index */
  133. } Elf32_Sym;
  134. /* Relocation entry with implicit addend */
  135. typedef struct
  136. {
  137. Elf32_Addr r_offset; /* offset of relocation */
  138. Elf32_Word r_info; /* symbol table index and type */
  139. } Elf32_Rel;
  140. /* Relocation entry with explicit addend */
  141. typedef struct
  142. {
  143. Elf32_Addr r_offset; /* offset of relocation */
  144. Elf32_Word r_info; /* symbol table index and type */
  145. Elf32_Sword r_addend;
  146. } Elf32_Rela;
  147. typedef struct
  148. {
  149. Elf64_Addr r_offset; /* Location at which to apply the action */
  150. Elf64_Xword r_info; /* index and type of relocation */
  151. } Elf64_Rel;
  152. typedef struct
  153. {
  154. Elf64_Addr r_offset; /* Location at which to apply the action */
  155. Elf64_Xword r_info; /* index and type of relocation */
  156. Elf64_Sxword r_addend; /* Constant addend used to compute value */
  157. } Elf64_Rela;
  158. /* Program Header */
  159. typedef struct
  160. {
  161. Elf32_Word p_type; /* segment type */
  162. Elf32_Off p_offset; /* segment offset */
  163. Elf32_Addr p_vaddr; /* virtual address of segment */
  164. Elf32_Addr p_paddr; /* physical address of segment */
  165. Elf32_Word p_filesz; /* number of bytes in file for seg */
  166. Elf32_Word p_memsz; /* number of bytes in mem. for seg */
  167. Elf32_Word p_flags; /* flags */
  168. Elf32_Word p_align; /* memory alignment */
  169. } Elf32_Phdr;
  170. typedef struct
  171. {
  172. Elf64_Word p_type; /* segment type */
  173. Elf64_Word p_flags; /* flags */
  174. Elf64_Off p_offset; /* segment offset */
  175. Elf64_Addr p_vaddr; /* virtual address of segment */
  176. Elf64_Addr p_paddr; /* physical address of segment */
  177. Elf64_Xword p_filesz; /* number of bytes in file for seg */
  178. Elf64_Xword p_memsz; /* number of bytes in mem. for seg */
  179. Elf64_Xword p_align; /* memory alignment */
  180. } Elf64_Phdr;
  181. /* Dynamic structure */
  182. typedef struct
  183. {
  184. Elf32_Sword d_tag; /* controls meaning of d_val */
  185. union
  186. {
  187. Elf32_Word d_val; /* Multiple meanings - see d_tag */
  188. Elf32_Addr d_ptr; /* program virtual address */
  189. } d_un;
  190. } Elf32_Dyn;
  191. extern Elf32_Dyn _DYNAMIC[];
  192. typedef struct
  193. {
  194. Elf64_Sxword d_tag; /* entry tag value */
  195. union
  196. {
  197. Elf64_Xword d_val;
  198. Elf64_Addr d_ptr;
  199. } d_un;
  200. } Elf64_Dyn;
  201. /* e_ident[] magic number */
  202. #define ELFMAG0 0x7f /* e_ident[EI_MAG0] */
  203. #define ELFMAG1 'E' /* e_ident[EI_MAG1] */
  204. #define ELFMAG2 'L' /* e_ident[EI_MAG2] */
  205. #define ELFMAG3 'F' /* e_ident[EI_MAG3] */
  206. #define ELFMAG "\177ELF" /* magic */
  207. #define SELFMAG 4 /* size of magic */
  208. /* e_ident[] file class */
  209. #define ELFCLASSNONE 0 /* invalid */
  210. #define ELFCLASS32 1 /* 32-bit objs */
  211. #define ELFCLASS64 2 /* 64-bit objs */
  212. #define ELFCLASSNUM 3 /* number of classes */
  213. /* e_ident[] data encoding */
  214. #define ELFDATANONE 0 /* invalid */
  215. #define ELFDATA2LSB 1 /* Little-Endian */
  216. #define ELFDATA2MSB 2 /* Big-Endian */
  217. #define ELFDATANUM 3 /* number of data encode defines */
  218. /* e_ident[] OS/ABI specific ELF extensions */
  219. #define ELFOSABI_NONE 0 /* No extension specified */
  220. #define ELFOSABI_HPUX 1 /* Hewlett-Packard HP-UX */
  221. #define ELFOSABI_NETBSD 2 /* NetBSD */
  222. #define ELFOSABI_LINUX 3 /* Linux */
  223. #define ELFOSABI_SOLARIS 6 /* Sun Solaris */
  224. #define ELFOSABI_AIX 7 /* AIX */
  225. #define ELFOSABI_IRIX 8 /* IRIX */
  226. #define ELFOSABI_FREEBSD 9 /* FreeBSD */
  227. #define ELFOSABI_TRU64 10 /* Compaq TRU64 UNIX */
  228. #define ELFOSABI_MODESTO 11 /* Novell Modesto */
  229. #define ELFOSABI_OPENBSD 12 /* OpenBSD */
  230. /* 64-255 Architecture-specific value range */
  231. /* e_ident[] ABI Version */
  232. #define ELFABIVERSION 0
  233. /* e_ident */
  234. #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \
  235. (ehdr).e_ident[EI_MAG1] == ELFMAG1 && \
  236. (ehdr).e_ident[EI_MAG2] == ELFMAG2 && \
  237. (ehdr).e_ident[EI_MAG3] == ELFMAG3)
  238. /* e_type */
  239. #define ET_NONE 0 /* No file type */
  240. #define ET_REL 1 /* relocatable file */
  241. #define ET_EXEC 2 /* executable file */
  242. #define ET_DYN 3 /* shared object file */
  243. #define ET_CORE 4 /* core file */
  244. #define ET_NUM 5 /* number of types */
  245. #define ET_LOOS 0xfe00 /* reserved range for operating */
  246. #define ET_HIOS 0xfeff /* system specific e_type */
  247. #define ET_LOPROC 0xff00 /* reserved range for processor */
  248. #define ET_HIPROC 0xffff /* specific e_type */
  249. /* e_machine */
  250. #define EM_NONE 0 /* No Machine */
  251. #define EM_M32 1 /* AT&T WE 32100 */
  252. #define EM_SPARC 2 /* SPARC */
  253. #define EM_386 3 /* Intel 80386 */
  254. #define EM_68K 4 /* Motorola 68000 */
  255. #define EM_88K 5 /* Motorola 88000 */
  256. #if 0
  257. #define EM_486 6 /* RESERVED - was Intel 80486 */
  258. #endif
  259. #define EM_860 7 /* Intel 80860 */
  260. #define EM_MIPS 8 /* MIPS R3000 Big-Endian only */
  261. #define EM_S370 9 /* IBM System/370 Processor */
  262. #define EM_MIPS_RS4_BE 10 /* MIPS R4000 Big-Endian */
  263. #if 0
  264. #define EM_SPARC64 11 /* RESERVED - was SPARC v9 \
  265. 64-bit unoffical */
  266. #endif
  267. /* RESERVED 11-14 for future use */
  268. #define EM_PARISC 15 /* HPPA */
  269. /* RESERVED 16 for future use */
  270. #define EM_VPP500 17 /* Fujitsu VPP500 */
  271. #define EM_SPARC32PLUS 18 /* Enhanced instruction set SPARC */
  272. #define EM_960 19 /* Intel 80960 */
  273. #define EM_PPC 20 /* PowerPC */
  274. #define EM_PPC64 21 /* 64-bit PowerPC */
  275. #define EM_S390 22 /* IBM System/390 Processor */
  276. /* RESERVED 23-35 for future use */
  277. #define EM_V800 36 /* NEC V800 */
  278. #define EM_FR20 37 /* Fujitsu FR20 */
  279. #define EM_RH32 38 /* TRW RH-32 */
  280. #define EM_RCE 39 /* Motorola RCE */
  281. #define EM_ARM 40 /* Advanced Risc Machines ARM */
  282. #define EM_ALPHA 41 /* Digital Alpha */
  283. #define EM_SH 42 /* Hitachi SH */
  284. #define EM_SPARCV9 43 /* SPARC Version 9 */
  285. #define EM_TRICORE 44 /* Siemens TriCore embedded processor */
  286. #define EM_ARC 45 /* Argonaut RISC Core */
  287. #define EM_H8_300 46 /* Hitachi H8/300 */
  288. #define EM_H8_300H 47 /* Hitachi H8/300H */
  289. #define EM_H8S 48 /* Hitachi H8S */
  290. #define EM_H8_500 49 /* Hitachi H8/500 */
  291. #define EM_IA_64 50 /* Intel Merced */
  292. #define EM_MIPS_X 51 /* Stanford MIPS-X */
  293. #define EM_COLDFIRE 52 /* Motorola Coldfire */
  294. #define EM_68HC12 53 /* Motorola M68HC12 */
  295. #define EM_MMA 54 /* Fujitsu MMA Multimedia Accelerator*/
  296. #define EM_PCP 55 /* Siemens PCP */
  297. #define EM_NCPU 56 /* Sony nCPU embeeded RISC */
  298. #define EM_NDR1 57 /* Denso NDR1 microprocessor */
  299. #define EM_STARCORE 58 /* Motorola Start*Core processor */
  300. #define EM_ME16 59 /* Toyota ME16 processor */
  301. #define EM_ST100 60 /* STMicroelectronic ST100 processor */
  302. #define EM_TINYJ 61 /* Advanced Logic Corp. Tinyj emb.fam*/
  303. #define EM_X86_64 62 /* AMD x86-64 */
  304. #define EM_PDSP 63 /* Sony DSP Processor */
  305. /* RESERVED 64,65 for future use */
  306. #define EM_FX66 66 /* Siemens FX66 microcontroller */
  307. #define EM_ST9PLUS 67 /* STMicroelectronics ST9+ 8/16 mc */
  308. #define EM_ST7 68 /* STmicroelectronics ST7 8 bit mc */
  309. #define EM_68HC16 69 /* Motorola MC68HC16 microcontroller */
  310. #define EM_68HC11 70 /* Motorola MC68HC11 microcontroller */
  311. #define EM_68HC08 71 /* Motorola MC68HC08 microcontroller */
  312. #define EM_68HC05 72 /* Motorola MC68HC05 microcontroller */
  313. #define EM_SVX 73 /* Silicon Graphics SVx */
  314. #define EM_ST19 74 /* STMicroelectronics ST19 8 bit mc */
  315. #define EM_VAX 75 /* Digital VAX */
  316. #define EM_CHRIS 76 /* Axis Communications embedded proc. */
  317. #define EM_JAVELIN 77 /* Infineon Technologies emb. proc. */
  318. #define EM_FIREPATH 78 /* Element 14 64-bit DSP Processor */
  319. #define EM_ZSP 79 /* LSI Logic 16-bit DSP Processor */
  320. #define EM_MMIX 80 /* Donald Knuth's edu 64-bit proc. */
  321. #define EM_HUANY 81 /* Harvard University mach-indep objs */
  322. #define EM_PRISM 82 /* SiTera Prism */
  323. #define EM_AVR 83 /* Atmel AVR 8-bit microcontroller */
  324. #define EM_FR30 84 /* Fujitsu FR30 */
  325. #define EM_D10V 85 /* Mitsubishi DV10V */
  326. #define EM_D30V 86 /* Mitsubishi DV30V */
  327. #define EM_V850 87 /* NEC v850 */
  328. #define EM_M32R 88 /* Mitsubishi M32R */
  329. #define EM_MN10300 89 /* Matsushita MN10200 */
  330. #define EM_MN10200 90 /* Matsushita MN10200 */
  331. #define EM_PJ 91 /* picoJava */
  332. #define EM_NUM 92 /* number of machine types */
  333. /* Version */
  334. #define EV_NONE 0 /* Invalid */
  335. #define EV_CURRENT 1 /* Current */
  336. #define EV_NUM 2 /* number of versions */
  337. /* Special Section Indexes */
  338. #define SHN_UNDEF 0 /* undefined */
  339. #define SHN_LORESERVE 0xff00 /* lower bounds of reserved indexes */
  340. #define SHN_LOPROC 0xff00 /* reserved range for processor */
  341. #define SHN_HIPROC 0xff1f /* specific section indexes */
  342. #define SHN_LOOS 0xff20 /* reserved range for operating */
  343. #define SHN_HIOS 0xff3f /* specific semantics */
  344. #define SHN_ABS 0xfff1 /* absolute value */
  345. #define SHN_COMMON 0xfff2 /* common symbol */
  346. #define SHN_XINDEX 0xffff /* Index is an extra table */
  347. #define SHN_HIRESERVE 0xffff /* upper bounds of reserved indexes */
  348. /* sh_type */
  349. #define SHT_NULL 0 /* inactive */
  350. #define SHT_PROGBITS 1 /* program defined information */
  351. #define SHT_SYMTAB 2 /* symbol table section */
  352. #define SHT_STRTAB 3 /* string table section */
  353. #define SHT_RELA 4 /* relocation section with addends*/
  354. #define SHT_HASH 5 /* symbol hash table section */
  355. #define SHT_DYNAMIC 6 /* dynamic section */
  356. #define SHT_NOTE 7 /* note section */
  357. #define SHT_NOBITS 8 /* no space section */
  358. #define SHT_REL 9 /* relation section without addends */
  359. #define SHT_SHLIB 10 /* reserved - purpose unknown */
  360. #define SHT_DYNSYM 11 /* dynamic symbol table section */
  361. #define SHT_INIT_ARRAY 14 /* Array of constructors */
  362. #define SHT_FINI_ARRAY 15 /* Array of destructors */
  363. #define SHT_PREINIT_ARRAY 16 /* Array of pre-constructors */
  364. #define SHT_GROUP 17 /* Section group */
  365. #define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
  366. #define SHT_NUM 19 /* number of section types */
  367. #define SHT_LOOS 0x60000000 /* Start OS-specific */
  368. #define SHT_HIOS 0x6fffffff /* End OS-specific */
  369. #define SHT_LOPROC 0x70000000 /* reserved range for processor */
  370. #define SHT_HIPROC 0x7fffffff /* specific section header types */
  371. #define SHT_LOUSER 0x80000000 /* reserved range for application */
  372. #define SHT_HIUSER 0xffffffff /* specific indexes */
  373. /* Section names */
  374. #define ELF_BSS ".bss" /* uninitialized data */
  375. #define ELF_COMMENT ".comment" /* version control information */
  376. #define ELF_DATA ".data" /* initialized data */
  377. #define ELF_DATA1 ".data1" /* initialized data */
  378. #define ELF_DEBUG ".debug" /* debug */
  379. #define ELF_DYNAMIC ".dynamic" /* dynamic linking information */
  380. #define ELF_DYNSTR ".dynstr" /* dynamic string table */
  381. #define ELF_DYNSYM ".dynsym" /* dynamic symbol table */
  382. #define ELF_FINI ".fini" /* termination code */
  383. #define ELF_FINI_ARRAY ".fini_array" /* Array of destructors */
  384. #define ELF_GOT ".got" /* global offset table */
  385. #define ELF_HASH ".hash" /* symbol hash table */
  386. #define ELF_INIT ".init" /* initialization code */
  387. #define ELF_INIT_ARRAY ".init_array" /* Array of constuctors */
  388. #define ELF_INTERP ".interp" /* Pathname of program interpreter */
  389. #define ELF_LINE ".line" /* Symbolic line numnber information */
  390. #define ELF_NOTE ".note" /* Contains note section */
  391. #define ELF_PLT ".plt" /* Procedure linkage table */
  392. #define ELF_PREINIT_ARRAY ".preinit_array" /* Array of pre-constructors */
  393. #define ELF_REL_DATA ".rel.data" /* relocation data */
  394. #define ELF_REL_FINI ".rel.fini" /* relocation termination code */
  395. #define ELF_REL_INIT ".rel.init" /* relocation initialization code */
  396. #define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */
  397. #define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */
  398. #define ELF_REL_TEXT ".rel.text" /* relocation code */
  399. #define ELF_RODATA ".rodata" /* read-only data */
  400. #define ELF_RODATA1 ".rodata1" /* read-only data */
  401. #define ELF_SHSTRTAB ".shstrtab" /* section header string table */
  402. #define ELF_STRTAB ".strtab" /* string table */
  403. #define ELF_SYMTAB ".symtab" /* symbol table */
  404. #define ELF_SYMTAB_SHNDX ".symtab_shndx" /* symbol table section index */
  405. #define ELF_TBSS ".tbss" /* thread local uninit data */
  406. #define ELF_TDATA ".tdata" /* thread local init data */
  407. #define ELF_TDATA1 ".tdata1" /* thread local init data */
  408. #define ELF_TEXT ".text" /* code */
  409. /* Section Attribute Flags - sh_flags */
  410. #define SHF_WRITE 0x1 /* Writable */
  411. #define SHF_ALLOC 0x2 /* occupies memory */
  412. #define SHF_EXECINSTR 0x4 /* executable */
  413. #define SHF_MERGE 0x10 /* Might be merged */
  414. #define SHF_STRINGS 0x20 /* Contains NULL terminated strings */
  415. #define SHF_INFO_LINK 0x40 /* sh_info contains SHT index */
  416. #define SHF_LINK_ORDER 0x80 /* Preserve order after combining*/
  417. #define SHF_OS_NONCONFORMING 0x100 /* Non-standard OS specific handling */
  418. #define SHF_GROUP 0x200 /* Member of section group */
  419. #define SHF_TLS 0x400 /* Thread local storage */
  420. #define SHF_MASKOS 0x0ff00000 /* OS specific */
  421. #define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
  422. /* specific section attributes */
  423. /* Section Group Flags */
  424. #define GRP_COMDAT 0x1 /* COMDAT group */
  425. #define GRP_MASKOS 0x0ff00000 /* Mask OS specific flags */
  426. #define GRP_MASKPROC 0xf0000000 /* Mask processor specific flags */
  427. /* Symbol table index */
  428. #define STN_UNDEF 0 /* undefined */
  429. /* Extract symbol info - st_info */
  430. #define ELF32_ST_BIND(x) ((x) >> 4)
  431. #define ELF32_ST_TYPE(x) (((unsigned int)x) & 0xf)
  432. #define ELF32_ST_INFO(b, t) (((b) << 4) + ((t)&0xf))
  433. #define ELF32_ST_VISIBILITY(x) ((x)&0x3)
  434. /* Symbol Binding - ELF32_ST_BIND - st_info */
  435. #define STB_LOCAL 0 /* Local symbol */
  436. #define STB_GLOBAL 1 /* Global symbol */
  437. #define STB_WEAK 2 /* like global - lower precedence */
  438. #define STB_NUM 3 /* number of symbol bindings */
  439. #define STB_LOOS 10 /* reserved range for operating */
  440. #define STB_HIOS 12 /* system specific symbol bindings */
  441. #define STB_LOPROC 13 /* reserved range for processor */
  442. #define STB_HIPROC 15 /* specific symbol bindings */
  443. /* Symbol type - ELF32_ST_TYPE - st_info */
  444. #define STT_NOTYPE 0 /* not specified */
  445. #define STT_OBJECT 1 /* data object */
  446. #define STT_FUNC 2 /* function */
  447. #define STT_SECTION 3 /* section */
  448. #define STT_FILE 4 /* file */
  449. #define STT_NUM 5 /* number of symbol types */
  450. #define STT_TLS 6 /* Thread local storage symbol */
  451. #define STT_LOOS 10 /* reserved range for operating */
  452. #define STT_HIOS 12 /* system specific symbol types */
  453. #define STT_LOPROC 13 /* reserved range for processor */
  454. #define STT_HIPROC 15 /* specific symbol types */
  455. /* Symbol visibility - ELF32_ST_VISIBILITY - st_other */
  456. #define STV_DEFAULT 0 /* Normal visibility rules */
  457. #define STV_INTERNAL 1 /* Processor specific hidden class */
  458. #define STV_HIDDEN 2 /* Symbol unavailable in other mods */
  459. #define STV_PROTECTED 3 /* Not preemptible, not exported */
  460. /* Extract relocation info - r_info */
  461. #define ELF32_R_SYM(i) ((i) >> 8)
  462. #define ELF32_R_TYPE(i) ((unsigned char)(i))
  463. #define ELF32_R_INFO(s, t) (((s) << 8) + (unsigned char)(t))
  464. /* Segment types - p_type */
  465. #define PT_NULL 0 /* unused */
  466. #define PT_LOAD 1 /* loadable segment */
  467. #define PT_DYNAMIC 2 /* dynamic linking section */
  468. #define PT_INTERP 3 /* the RTLD */
  469. #define PT_NOTE 4 /* auxiliary information */
  470. #define PT_SHLIB 5 /* reserved - purpose undefined */
  471. #define PT_PHDR 6 /* program header */
  472. #define PT_TLS 7 /* Thread local storage template */
  473. #define PT_NUM 8 /* Number of segment types */
  474. #define PT_LOOS 0x60000000 /* reserved range for operating */
  475. #define PT_HIOS 0x6fffffff /* system specific segment types */
  476. #define PT_LOPROC 0x70000000 /* reserved range for processor */
  477. #define PT_HIPROC 0x7fffffff /* specific segment types */
  478. /* Segment flags - p_flags */
  479. #define PF_X 0x1 /* Executable */
  480. #define PF_W 0x2 /* Writable */
  481. #define PF_R 0x4 /* Readable */
  482. #define PF_MASKOS 0x0ff00000 /* OS specific segment flags */
  483. #define PF_MASKPROC 0xf0000000 /* reserved bits for processor */
  484. /* specific segment flags */
  485. #define ELF64_R_SYM(i) ((i) >> 32)
  486. #define ELF64_R_TYPE(i) ((i)&0xffffffff)
  487. /* Dynamic Array Tags - d_tag */
  488. #define DT_NULL 0 /* marks end of _DYNAMIC array */
  489. #define DT_NEEDED 1 /* string table offset of needed lib */
  490. #define DT_PLTRELSZ 2 /* size of relocation entries in PLT */
  491. #define DT_PLTGOT 3 /* address PLT/GOT */
  492. #define DT_HASH 4 /* address of symbol hash table */
  493. #define DT_STRTAB 5 /* address of string table */
  494. #define DT_SYMTAB 6 /* address of symbol table */
  495. #define DT_RELA 7 /* address of relocation table */
  496. #define DT_RELASZ 8 /* size of relocation table */
  497. #define DT_RELAENT 9 /* size of relocation entry */
  498. #define DT_STRSZ 10 /* size of string table */
  499. #define DT_SYMENT 11 /* size of symbol table entry */
  500. #define DT_INIT 12 /* address of initialization func */
  501. #define DT_FINI 13 /* address of termination function */
  502. #define DT_SONAME 14 /* string table offset of shared obj */
  503. #define DT_RPATH 15 /* string table offset of library \
  504. search path */
  505. #define DT_SYMBOLIC 16 /* start sym search in shared obj */
  506. #define DT_REL 17 /* address of rel. tbl. w addends */
  507. #define DT_RELSZ 18 /* size of DT_REL relocation table */
  508. #define DT_RELENT 19 /* size of DT_REL relocation entry */
  509. #define DT_PLTREL 20 /* PLT referenced relocation entry */
  510. #define DT_DEBUG 21 /* bugger */
  511. #define DT_TEXTREL 22 /* Allow rel. mod. to unwritable seg */
  512. #define DT_JMPREL 23 /* add. of PLT's relocation entries */
  513. #define DT_BIND_NOW 24 /* Process relocations of object */
  514. #define DT_INIT_ARRAY 25 /* Array with addresses of init fct */
  515. #define DT_FINI_ARRAY 26 /* Array with addresses of fini fct */
  516. #define DT_INIT_ARRAYSZ 27 /* Size in bytes of DT_INIT_ARRAY */
  517. #define DT_FINI_ARRAYSZ 28 /* Size in bytes of DT_FINI_ARRAY */
  518. #define DT_RUNPATH 29 /* Library search path */
  519. #define DT_FLAGS 30 /* Flags for the object being loaded */
  520. #define DT_ENCODING 32 /* Start of encoded range */
  521. #define DT_PREINIT_ARRAY 32 /* Array with addresses of preinit fct*/
  522. #define DT_PREINIT_ARRAYSZ 33 /* size in bytes of DT_PREINIT_ARRAY */
  523. #define DT_NUM 34 /* Number used */
  524. #define DT_LOOS 0x60000000 /* reserved range for OS */
  525. #define DT_HIOS 0x6fffffff /* specific dynamic array tags */
  526. #define DT_LOPROC 0x70000000 /* reserved range for processor */
  527. #define DT_HIPROC 0x7fffffff /* specific dynamic array tags */
  528. /* Dynamic Tag Flags - d_un.d_val */
  529. #define DF_ORIGIN 0x01 /* Object may use DF_ORIGIN */
  530. #define DF_SYMBOLIC 0x02 /* Symbol resolutions starts here */
  531. #define DF_TEXTREL 0x04 /* Object contains text relocations */
  532. #define DF_BIND_NOW 0x08 /* No lazy binding for this object */
  533. #define DF_STATIC_TLS 0x10 /* Static thread local storage */
  534. /* Standard ELF hashing function */
  535. unsigned long elf_hash(const unsigned char *name);
  536. #define ELF_TARG_VER 1 /* The ver for which this code is intended */
  537. /* ELF register definitions */
  538. #define R_386_NONE 0
  539. #define R_386_32 1
  540. #define R_386_PC32 2
  541. #define R_386_GOT32 3
  542. #define R_386_PLT32 4
  543. #define R_386_COPY 5
  544. #define R_386_GLOB_DAT 6
  545. #define R_386_JMP_SLOT 7
  546. #define R_386_RELATIVE 8
  547. #define R_386_GOTOFF 9
  548. #define R_386_GOTPC 10
  549. #define R_386_NUM 11
  550. /* x86-64 relocation types */
  551. #define R_X86_64_NONE 0 /* No reloc */
  552. #define R_X86_64_64 1 /* Direct 64 bit */
  553. #define R_X86_64_PC32 2 /* PC relative 32 bit signed */
  554. #define R_X86_64_GOT32 3 /* 32 bit GOT entry */
  555. #define R_X86_64_PLT32 4 /* 32 bit PLT address */
  556. #define R_X86_64_COPY 5 /* Copy symbol at runtime */
  557. #define R_X86_64_GLOB_DAT 6 /* Create GOT entry */
  558. #define R_X86_64_JUMP_SLOT 7 /* Create PLT entry */
  559. #define R_X86_64_RELATIVE 8 /* Adjust by program base */
  560. /* 32 bit signed pc relative offset to GOT */
  561. #define R_X86_64_GOTPCREL 9
  562. #define R_X86_64_32 10 /* Direct 32 bit zero extended */
  563. #define R_X86_64_32S 11 /* Direct 32 bit sign extended */
  564. #define R_X86_64_16 12 /* Direct 16 bit zero extended */
  565. #define R_X86_64_PC16 13 /* 16 bit sign extended pc relative */
  566. #define R_X86_64_8 14 /* Direct 8 bit sign extended */
  567. #define R_X86_64_PC8 15 /* 8 bit sign extended pc relative */
  568. #define R_X86_64_NUM 16
  569. /*
  570. * XXX - PowerPC defines really don't belong in here,
  571. * but we'll put them in for simplicity.
  572. */
  573. /* Values for Elf32/64_Ehdr.e_flags */
  574. #define EF_PPC_EMB 0x80000000 /* PowerPC embedded flag */
  575. #define EF_PPC64_ELFV1_ABI 0x00000001
  576. #define EF_PPC64_ELFV2_ABI 0x00000002
  577. /* Cygnus local bits below */
  578. #define EF_PPC_RELOCATABLE 0x00010000 /* PowerPC -mrelocatable flag*/
  579. #define EF_PPC_RELOCATABLE_LIB 0x00008000 /* PowerPC -mrelocatable-lib \
  580. flag */
  581. /* PowerPC relocations defined by the ABIs */
  582. #define R_PPC_NONE 0
  583. #define R_PPC_ADDR32 1 /* 32bit absolute address */
  584. #define R_PPC_ADDR24 2 /* 26bit address, 2 bits ignored */
  585. #define R_PPC_ADDR16 3 /* 16bit absolute address */
  586. #define R_PPC_ADDR16_LO 4 /* lower 16bit of absolute address */
  587. #define R_PPC_ADDR16_HI 5 /* high 16bit of absolute address */
  588. #define R_PPC_ADDR16_HA 6 /* adjusted high 16bit */
  589. #define R_PPC_ADDR14 7 /* 16bit address, 2 bits ignored */
  590. #define R_PPC_ADDR14_BRTAKEN 8
  591. #define R_PPC_ADDR14_BRNTAKEN 9
  592. #define R_PPC_REL24 10 /* PC relative 26 bit */
  593. #define R_PPC_REL14 11 /* PC relative 16 bit */
  594. #define R_PPC_REL14_BRTAKEN 12
  595. #define R_PPC_REL14_BRNTAKEN 13
  596. #define R_PPC_GOT16 14
  597. #define R_PPC_GOT16_LO 15
  598. #define R_PPC_GOT16_HI 16
  599. #define R_PPC_GOT16_HA 17
  600. #define R_PPC_PLTREL24 18
  601. #define R_PPC_COPY 19
  602. #define R_PPC_GLOB_DAT 20
  603. #define R_PPC_JMP_SLOT 21
  604. #define R_PPC_RELATIVE 22
  605. #define R_PPC_LOCAL24PC 23
  606. #define R_PPC_UADDR32 24
  607. #define R_PPC_UADDR16 25
  608. #define R_PPC_REL32 26
  609. #define R_PPC_PLT32 27
  610. #define R_PPC_PLTREL32 28
  611. #define R_PPC_PLT16_LO 29
  612. #define R_PPC_PLT16_HI 30
  613. #define R_PPC_PLT16_HA 31
  614. #define R_PPC_SDAREL16 32
  615. #define R_PPC_SECTOFF 33
  616. #define R_PPC_SECTOFF_LO 34
  617. #define R_PPC_SECTOFF_HI 35
  618. #define R_PPC_SECTOFF_HA 36
  619. /* Keep this the last entry */
  620. #define R_PPC_NUM 37
  621. /*
  622. * The remaining relocs are from the Embedded ELF ABI, and are not
  623. * in the SVR4 ELF ABI.
  624. */
  625. #define R_PPC_EMB_NADDR32 101
  626. #define R_PPC_EMB_NADDR16 102
  627. #define R_PPC_EMB_NADDR16_LO 103
  628. #define R_PPC_EMB_NADDR16_HI 104
  629. #define R_PPC_EMB_NADDR16_HA 105
  630. #define R_PPC_EMB_SDAI16 106
  631. #define R_PPC_EMB_SDA2I16 107
  632. #define R_PPC_EMB_SDA2REL 108
  633. #define R_PPC_EMB_SDA21 109 /* 16 bit offset in SDA */
  634. #define R_PPC_EMB_MRKREF 110
  635. #define R_PPC_EMB_RELSEC16 111
  636. #define R_PPC_EMB_RELST_LO 112
  637. #define R_PPC_EMB_RELST_HI 113
  638. #define R_PPC_EMB_RELST_HA 114
  639. #define R_PPC_EMB_BIT_FLD 115
  640. #define R_PPC_EMB_RELSDA 116 /* 16 bit relative offset in SDA */
  641. /* Diab tool relocations */
  642. #define R_PPC_DIAB_SDA21_LO 180 /* like EMB_SDA21, but lower 16 bit */
  643. #define R_PPC_DIAB_SDA21_HI 181 /* like EMB_SDA21, but high 16 bit */
  644. #define R_PPC_DIAB_SDA21_HA 182 /* like EMB_SDA21, adjusted high 16 */
  645. #define R_PPC_DIAB_RELSDA_LO 183 /* like EMB_RELSDA, but lower 16 bit */
  646. #define R_PPC_DIAB_RELSDA_HI 184 /* like EMB_RELSDA, but high 16 bit */
  647. #define R_PPC_DIAB_RELSDA_HA 185 /* like EMB_RELSDA, adjusted high 16 */
  648. /*
  649. * This is a phony reloc to handle any old fashioned TOC16 references
  650. * that may still be in object files.
  651. */
  652. #define R_PPC_TOC16 255
  653. /* ARM relocs */
  654. #define R_ARM_NONE 0 /* No reloc */
  655. #define R_ARM_RELATIVE 23 /* Adjust by program base */
  656. /* AArch64 relocs */
  657. #define R_AARCH64_NONE 0 /* No relocation */
  658. #define R_AARCH64_RELATIVE 1027 /* Adjust by program base */
  659. /* RISC-V relocations */
  660. #define R_RISCV_32 1
  661. #define R_RISCV_64 2
  662. #define R_RISCV_RELATIVE 3
  663. /*
  664. * A very simple ELF64 loader, assumes the image is valid, returns the
  665. * entry point address.
  666. *
  667. * Note if U-Boot is 32-bit, the loader assumes the to segment's
  668. * physical address and size is within the lower 32-bit address space.
  669. */
  670. static unsigned long ElfLoadElf64ImagePhdr(unsigned long addr)
  671. {
  672. Elf64_Ehdr *ehdr; /* Elf header structure pointer */
  673. Elf64_Phdr *phdr; /* Program header structure pointer */
  674. int i;
  675. ehdr = (Elf64_Ehdr *)addr;
  676. phdr = (Elf64_Phdr *)(addr + (unsigned long)ehdr->e_phoff);
  677. /* Load each program header */
  678. for (i = 0; i < ehdr->e_phnum; ++i)
  679. {
  680. void *dst = (void *)(unsigned long)phdr->p_paddr;
  681. void *src = (void *)addr + phdr->p_offset;
  682. f_printk("Loading phdr %i to 0x%p (%lu bytes)",
  683. i, dst, (unsigned long)phdr->p_filesz);
  684. if (phdr->p_filesz)
  685. {
  686. memcpy(dst, src, phdr->p_filesz);
  687. }
  688. if (phdr->p_filesz != phdr->p_memsz)
  689. {
  690. memset(dst + phdr->p_filesz, 0x00,
  691. phdr->p_memsz - phdr->p_filesz);
  692. }
  693. FCacheDCacheFlushRange((uintptr)dst, phdr->p_memsz);
  694. ++phdr;
  695. }
  696. if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags & EF_PPC64_ELFV1_ABI))
  697. {
  698. /*
  699. * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
  700. * descriptor pointer with the first double word being the
  701. * address of the entry point of the function.
  702. */
  703. uintptr_t addr = ehdr->e_entry;
  704. return *(Elf64_Addr *)addr;
  705. }
  706. return ehdr->e_entry;
  707. }
  708. static unsigned long ElfLoadElf64ImageShdr(unsigned long addr)
  709. {
  710. Elf64_Ehdr *ehdr; /* Elf header structure pointer */
  711. Elf64_Shdr *shdr; /* Section header structure pointer */
  712. unsigned char *strtab = 0; /* String table pointer */
  713. unsigned char *image; /* Binary image pointer */
  714. int i; /* Loop counter */
  715. ehdr = (Elf64_Ehdr *)addr;
  716. /* Find the section header string table for output info */
  717. shdr = (Elf64_Shdr *)(addr + (unsigned long)ehdr->e_shoff +
  718. (ehdr->e_shstrndx * sizeof(Elf64_Shdr)));
  719. if (shdr->sh_type == SHT_STRTAB)
  720. {
  721. strtab = (unsigned char *)(addr + (unsigned long)shdr->sh_offset);
  722. }
  723. /* Load each appropriate section */
  724. for (i = 0; i < ehdr->e_shnum; ++i)
  725. {
  726. shdr = (Elf64_Shdr *)(addr + (unsigned long)ehdr->e_shoff +
  727. (i * sizeof(Elf64_Shdr)));
  728. if (!(shdr->sh_flags & SHF_ALLOC) ||
  729. shdr->sh_addr == 0 || shdr->sh_size == 0)
  730. {
  731. continue;
  732. }
  733. if (strtab)
  734. {
  735. f_printk("%sing %s @ 0x%08lx (%ld bytes)",
  736. (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
  737. &strtab[shdr->sh_name],
  738. (unsigned long)shdr->sh_addr,
  739. (long)shdr->sh_size);
  740. }
  741. if (shdr->sh_type == SHT_NOBITS)
  742. {
  743. memset((void *)(uintptr)shdr->sh_addr, 0,
  744. shdr->sh_size);
  745. }
  746. else
  747. {
  748. image = (unsigned char *)addr + (unsigned long)shdr->sh_offset;
  749. memcpy((void *)(uintptr)shdr->sh_addr,
  750. (const void *)image, shdr->sh_size);
  751. }
  752. FCacheDCacheFlushRange((uintptr)shdr->sh_addr, shdr->sh_size);
  753. }
  754. if (ehdr->e_machine == EM_PPC64 && (ehdr->e_flags &
  755. EF_PPC64_ELFV1_ABI))
  756. {
  757. /*
  758. * For the 64-bit PowerPC ELF V1 ABI, e_entry is a function
  759. * descriptor pointer with the first double word being the
  760. * address of the entry point of the function.
  761. */
  762. uintptr addr = ehdr->e_entry;
  763. return *(Elf64_Addr *)addr;
  764. }
  765. return ehdr->e_entry;
  766. }
  767. unsigned long ElfLoadElfImagePhdr(unsigned long addr)
  768. {
  769. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  770. Elf32_Phdr *phdr; /* Program header structure pointer */
  771. int i;
  772. ehdr = (Elf32_Ehdr *)addr;
  773. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  774. {
  775. return ElfLoadElf64ImagePhdr(addr);
  776. }
  777. phdr = (Elf32_Phdr *)(addr + ehdr->e_phoff);
  778. /* Load each program header */
  779. for (i = 0; i < ehdr->e_phnum; ++i)
  780. {
  781. void *dst = (void *)(uintptr)phdr->p_paddr;
  782. void *src = (void *)addr + phdr->p_offset;
  783. f_printk("Loading phdr %i to 0x%p (%i bytes)",
  784. i, dst, phdr->p_filesz);
  785. if (phdr->p_filesz)
  786. {
  787. memcpy(dst, src, phdr->p_filesz);
  788. }
  789. if (phdr->p_filesz != phdr->p_memsz)
  790. {
  791. memset(dst + phdr->p_filesz, 0x00,
  792. phdr->p_memsz - phdr->p_filesz);
  793. }
  794. FCacheDCacheFlushRange((uintptr)dst, phdr->p_memsz);
  795. ++phdr;
  796. }
  797. return ehdr->e_entry;
  798. }
  799. unsigned long ElfLoadElfImageShdr(unsigned long addr)
  800. {
  801. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  802. Elf32_Shdr *shdr; /* Section header structure pointer */
  803. unsigned char *strtab = 0; /* String table pointer */
  804. unsigned char *image; /* Binary image pointer */
  805. int i; /* Loop counter */
  806. ehdr = (Elf32_Ehdr *)addr;
  807. if (ehdr->e_ident[EI_CLASS] == ELFCLASS64)
  808. {
  809. return ElfLoadElf64ImageShdr(addr);
  810. }
  811. /* Find the section header string table for output info */
  812. shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
  813. (ehdr->e_shstrndx * sizeof(Elf32_Shdr)));
  814. if (shdr->sh_type == SHT_STRTAB)
  815. {
  816. strtab = (unsigned char *)(addr + shdr->sh_offset);
  817. }
  818. /* Load each appropriate section */
  819. for (i = 0; i < ehdr->e_shnum; ++i)
  820. {
  821. shdr = (Elf32_Shdr *)(addr + ehdr->e_shoff +
  822. (i * sizeof(Elf32_Shdr)));
  823. if (!(shdr->sh_flags & SHF_ALLOC) ||
  824. shdr->sh_addr == 0 || shdr->sh_size == 0)
  825. {
  826. continue;
  827. }
  828. if (strtab)
  829. {
  830. f_printk("%sing %s @ 0x%08lx (%ld bytes)",
  831. (shdr->sh_type == SHT_NOBITS) ? "Clear" : "Load",
  832. &strtab[shdr->sh_name],
  833. (unsigned long)shdr->sh_addr,
  834. (long)shdr->sh_size);
  835. }
  836. if (shdr->sh_type == SHT_NOBITS)
  837. {
  838. memset((void *)(uintptr)shdr->sh_addr, 0,
  839. shdr->sh_size);
  840. }
  841. else
  842. {
  843. image = (unsigned char *)addr + shdr->sh_offset;
  844. memcpy((void *)(uintptr)shdr->sh_addr,
  845. (const void *)image, shdr->sh_size);
  846. }
  847. FCacheDCacheFlushRange((uintptr)shdr->sh_addr, shdr->sh_size);
  848. }
  849. return ehdr->e_entry;
  850. }
  851. /*
  852. * Determine if a valid ELF image exists at the given memory location.
  853. * First look at the ELF header magic field, then make sure that it is
  854. * executable.
  855. */
  856. int ElfIsImageValid(unsigned long addr)
  857. {
  858. Elf32_Ehdr *ehdr; /* Elf header structure pointer */
  859. ehdr = (Elf32_Ehdr *)addr;
  860. if (!IS_ELF(*ehdr))
  861. {
  862. f_printk("## No elf image at address 0x%08lx.", addr);
  863. return 0;
  864. }
  865. if (ehdr->e_type != ET_EXEC)
  866. {
  867. f_printk("## Not a 32-bit elf image at address 0x%08lx.", addr);
  868. return 0;
  869. }
  870. return 1;
  871. }
  872. /* Allow ports to override the default behavior */
  873. unsigned long ElfExecBootElf(unsigned long (*entry)(int, char *const[]),
  874. int argc, char *const argv[])
  875. {
  876. unsigned long ret;
  877. /*
  878. * pass address parameter as argv[0] (aka command name),
  879. * and all remaining args
  880. */
  881. ret = entry(argc, argv);
  882. return ret;
  883. }