felf.c 44 KB

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