simple-object-elf.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612
  1. /* simple-object-elf.c -- routines to manipulate ELF object files.
  2. Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Google.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #include "config.h"
  17. #include "libiberty.h"
  18. #include "simple-object.h"
  19. #include <errno.h>
  20. /* mingw.org's MinGW doesn't have ENOTSUP. */
  21. #ifndef ENOTSUP
  22. # define ENOTSUP ENOSYS
  23. #endif
  24. #include <stddef.h>
  25. #ifdef HAVE_STDLIB_H
  26. #include <stdlib.h>
  27. #endif
  28. #ifdef HAVE_STDINT_H
  29. #include <stdint.h>
  30. #endif
  31. #ifdef HAVE_STRING_H
  32. #include <string.h>
  33. #endif
  34. #ifdef HAVE_INTTYPES_H
  35. #include <inttypes.h>
  36. #endif
  37. #include "simple-object-common.h"
  38. /* ELF structures and constants. */
  39. /* 32-bit ELF file header. */
  40. typedef struct {
  41. unsigned char e_ident[16]; /* ELF "magic number" */
  42. unsigned char e_type[2]; /* Identifies object file type */
  43. unsigned char e_machine[2]; /* Specifies required architecture */
  44. unsigned char e_version[4]; /* Identifies object file version */
  45. unsigned char e_entry[4]; /* Entry point virtual address */
  46. unsigned char e_phoff[4]; /* Program header table file offset */
  47. unsigned char e_shoff[4]; /* Section header table file offset */
  48. unsigned char e_flags[4]; /* Processor-specific flags */
  49. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  50. unsigned char e_phentsize[2]; /* Program header table entry size */
  51. unsigned char e_phnum[2]; /* Program header table entry count */
  52. unsigned char e_shentsize[2]; /* Section header table entry size */
  53. unsigned char e_shnum[2]; /* Section header table entry count */
  54. unsigned char e_shstrndx[2]; /* Section header string table index */
  55. } Elf32_External_Ehdr;
  56. /* 64-bit ELF file header. */
  57. typedef struct {
  58. unsigned char e_ident[16]; /* ELF "magic number" */
  59. unsigned char e_type[2]; /* Identifies object file type */
  60. unsigned char e_machine[2]; /* Specifies required architecture */
  61. unsigned char e_version[4]; /* Identifies object file version */
  62. unsigned char e_entry[8]; /* Entry point virtual address */
  63. unsigned char e_phoff[8]; /* Program header table file offset */
  64. unsigned char e_shoff[8]; /* Section header table file offset */
  65. unsigned char e_flags[4]; /* Processor-specific flags */
  66. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  67. unsigned char e_phentsize[2]; /* Program header table entry size */
  68. unsigned char e_phnum[2]; /* Program header table entry count */
  69. unsigned char e_shentsize[2]; /* Section header table entry size */
  70. unsigned char e_shnum[2]; /* Section header table entry count */
  71. unsigned char e_shstrndx[2]; /* Section header string table index */
  72. } Elf64_External_Ehdr;
  73. /* Indexes and values in e_ident field of Ehdr. */
  74. #define EI_MAG0 0 /* File identification byte 0 index */
  75. #define ELFMAG0 0x7F /* Magic number byte 0 */
  76. #define EI_MAG1 1 /* File identification byte 1 index */
  77. #define ELFMAG1 'E' /* Magic number byte 1 */
  78. #define EI_MAG2 2 /* File identification byte 2 index */
  79. #define ELFMAG2 'L' /* Magic number byte 2 */
  80. #define EI_MAG3 3 /* File identification byte 3 index */
  81. #define ELFMAG3 'F' /* Magic number byte 3 */
  82. #define EI_CLASS 4 /* File class */
  83. #define ELFCLASSNONE 0 /* Invalid class */
  84. #define ELFCLASS32 1 /* 32-bit objects */
  85. #define ELFCLASS64 2 /* 64-bit objects */
  86. #define EI_DATA 5 /* Data encoding */
  87. #define ELFDATANONE 0 /* Invalid data encoding */
  88. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  89. #define ELFDATA2MSB 2 /* 2's complement, big endian */
  90. #define EI_VERSION 6 /* File version */
  91. #define EV_CURRENT 1 /* Current version */
  92. #define EI_OSABI 7 /* Operating System/ABI indication */
  93. /* Values for e_type field of Ehdr. */
  94. #define ET_REL 1 /* Relocatable file */
  95. /* Values for e_machine field of Ehdr. */
  96. #define EM_SPARC 2 /* SUN SPARC */
  97. #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
  98. /* Special section index values. */
  99. #define SHN_UNDEF 0 /* Undefined section */
  100. #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
  101. #define SHN_COMMON 0xFFF2 /* Associated symbol is in common */
  102. #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
  103. #define SHN_HIRESERVE 0xffff /* End of reserved indices */
  104. /* 32-bit ELF program header. */
  105. typedef struct {
  106. unsigned char p_type[4]; /* Identifies program segment type */
  107. unsigned char p_offset[4]; /* Segment file offset */
  108. unsigned char p_vaddr[4]; /* Segment virtual address */
  109. unsigned char p_paddr[4]; /* Segment physical address */
  110. unsigned char p_filesz[4]; /* Segment size in file */
  111. unsigned char p_memsz[4]; /* Segment size in memory */
  112. unsigned char p_flags[4]; /* Segment flags */
  113. unsigned char p_align[4]; /* Segment alignment, file & memory */
  114. } Elf32_External_Phdr;
  115. /* 64-bit ELF program header. */
  116. typedef struct {
  117. unsigned char p_type[4]; /* Identifies program segment type */
  118. unsigned char p_flags[4]; /* Segment flags */
  119. unsigned char p_offset[8]; /* Segment file offset */
  120. unsigned char p_vaddr[8]; /* Segment virtual address */
  121. unsigned char p_paddr[8]; /* Segment physical address */
  122. unsigned char p_filesz[8]; /* Segment size in file */
  123. unsigned char p_memsz[8]; /* Segment size in memory */
  124. unsigned char p_align[8]; /* Segment alignment, file & memory */
  125. } Elf64_External_Phdr;
  126. /* 32-bit ELF section header */
  127. typedef struct {
  128. unsigned char sh_name[4]; /* Section name, index in string tbl */
  129. unsigned char sh_type[4]; /* Type of section */
  130. unsigned char sh_flags[4]; /* Miscellaneous section attributes */
  131. unsigned char sh_addr[4]; /* Section virtual addr at execution */
  132. unsigned char sh_offset[4]; /* Section file offset */
  133. unsigned char sh_size[4]; /* Size of section in bytes */
  134. unsigned char sh_link[4]; /* Index of another section */
  135. unsigned char sh_info[4]; /* Additional section information */
  136. unsigned char sh_addralign[4]; /* Section alignment */
  137. unsigned char sh_entsize[4]; /* Entry size if section holds table */
  138. } Elf32_External_Shdr;
  139. /* 64-bit ELF section header. */
  140. typedef struct {
  141. unsigned char sh_name[4]; /* Section name, index in string tbl */
  142. unsigned char sh_type[4]; /* Type of section */
  143. unsigned char sh_flags[8]; /* Miscellaneous section attributes */
  144. unsigned char sh_addr[8]; /* Section virtual addr at execution */
  145. unsigned char sh_offset[8]; /* Section file offset */
  146. unsigned char sh_size[8]; /* Size of section in bytes */
  147. unsigned char sh_link[4]; /* Index of another section */
  148. unsigned char sh_info[4]; /* Additional section information */
  149. unsigned char sh_addralign[8]; /* Section alignment */
  150. unsigned char sh_entsize[8]; /* Entry size if section holds table */
  151. } Elf64_External_Shdr;
  152. /* Values for sh_type field. */
  153. #define SHT_NULL 0 /* Section header table entry unused */
  154. #define SHT_PROGBITS 1 /* Program data */
  155. #define SHT_SYMTAB 2 /* Link editing symbol table */
  156. #define SHT_STRTAB 3 /* A string table */
  157. #define SHT_RELA 4 /* Relocation entries with addends */
  158. #define SHT_REL 9 /* Relocation entries, no addends */
  159. #define SHT_GROUP 17 /* Section contains a section group */
  160. #define SHT_SYMTAB_SHNDX 18 /* Extended section indeces */
  161. /* Values for sh_flags field. */
  162. #define SHF_INFO_LINK 0x00000040 /* `sh_info' contains SHT index */
  163. #define SHF_EXECINSTR 0x00000004 /* Executable section. */
  164. #define SHF_EXCLUDE 0x80000000 /* Link editor is to exclude this
  165. section from executable and
  166. shared library that it builds
  167. when those objects are not to be
  168. further relocated. */
  169. /* Symbol table entry. */
  170. typedef struct
  171. {
  172. unsigned char st_name[4]; /* Symbol name (string tbl index) */
  173. unsigned char st_value[4]; /* Symbol value */
  174. unsigned char st_size[4]; /* Symbol size */
  175. unsigned char st_info; /* Symbol type and binding */
  176. unsigned char st_other; /* Symbol visibility */
  177. unsigned char st_shndx[2]; /* Section index */
  178. } Elf32_External_Sym;
  179. typedef struct
  180. {
  181. unsigned char st_name[4]; /* Symbol name (string tbl index) */
  182. unsigned char st_info; /* Symbol type and binding */
  183. unsigned char st_other; /* Symbol visibility */
  184. unsigned char st_shndx[2]; /* Section index */
  185. unsigned char st_value[8]; /* Symbol value */
  186. unsigned char st_size[8]; /* Symbol size */
  187. } Elf64_External_Sym;
  188. #define ELF_ST_BIND(val) (((unsigned char) (val)) >> 4)
  189. #define ELF_ST_TYPE(val) ((val) & 0xf)
  190. #define ELF_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
  191. #define STT_NOTYPE 0 /* Symbol type is unspecified */
  192. #define STT_OBJECT 1 /* Symbol is a data object */
  193. #define STT_FUNC 2 /* Symbol is a code object */
  194. #define STT_TLS 6 /* Thread local data object */
  195. #define STT_GNU_IFUNC 10 /* Symbol is an indirect code object */
  196. #define STB_LOCAL 0 /* Local symbol */
  197. #define STB_GLOBAL 1 /* Global symbol */
  198. #define STB_WEAK 2 /* Weak global */
  199. #define STV_DEFAULT 0 /* Visibility is specified by binding type */
  200. #define STV_HIDDEN 2 /* Can only be seen inside currect component */
  201. /* Functions to fetch and store different ELF types, depending on the
  202. endianness and size. */
  203. struct elf_type_functions
  204. {
  205. unsigned short (*fetch_Elf_Half) (const unsigned char *);
  206. unsigned int (*fetch_Elf_Word) (const unsigned char *);
  207. ulong_type (*fetch_Elf_Addr) (const unsigned char *);
  208. void (*set_Elf_Half) (unsigned char *, unsigned short);
  209. void (*set_Elf_Word) (unsigned char *, unsigned int);
  210. void (*set_Elf_Addr) (unsigned char *, ulong_type);
  211. };
  212. static const struct elf_type_functions elf_big_32_functions =
  213. {
  214. simple_object_fetch_big_16,
  215. simple_object_fetch_big_32,
  216. simple_object_fetch_big_32_ulong,
  217. simple_object_set_big_16,
  218. simple_object_set_big_32,
  219. simple_object_set_big_32_ulong
  220. };
  221. static const struct elf_type_functions elf_little_32_functions =
  222. {
  223. simple_object_fetch_little_16,
  224. simple_object_fetch_little_32,
  225. simple_object_fetch_little_32_ulong,
  226. simple_object_set_little_16,
  227. simple_object_set_little_32,
  228. simple_object_set_little_32_ulong
  229. };
  230. #ifdef UNSIGNED_64BIT_TYPE
  231. static const struct elf_type_functions elf_big_64_functions =
  232. {
  233. simple_object_fetch_big_16,
  234. simple_object_fetch_big_32,
  235. simple_object_fetch_big_64,
  236. simple_object_set_big_16,
  237. simple_object_set_big_32,
  238. simple_object_set_big_64
  239. };
  240. static const struct elf_type_functions elf_little_64_functions =
  241. {
  242. simple_object_fetch_little_16,
  243. simple_object_fetch_little_32,
  244. simple_object_fetch_little_64,
  245. simple_object_set_little_16,
  246. simple_object_set_little_32,
  247. simple_object_set_little_64
  248. };
  249. #endif
  250. /* Hideous macro to fetch the value of a field from an external ELF
  251. struct of some sort. TYPEFUNCS is the set of type functions.
  252. BUFFER points to the external data. STRUCTTYPE is the appropriate
  253. struct type. FIELD is a field within the struct. TYPE is the type
  254. of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr. */
  255. #define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \
  256. ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD)))
  257. /* Even more hideous macro to fetch the value of FIELD from BUFFER.
  258. SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
  259. elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
  260. the struct. TYPE is the type of the field in the struct: Elf_Half,
  261. Elf_Word, or Elf_Addr. */
  262. #define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, \
  263. FIELD, TYPE) \
  264. ELF_FETCH_STRUCT_FIELD (TYPEFUNCS, \
  265. Elf ## SIZE ## _External_ ## STRUCTTYPE, \
  266. FIELD, BUFFER, TYPE)
  267. /* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value. */
  268. #define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, \
  269. FIELD, TYPE) \
  270. ((CLASS) == ELFCLASS32 \
  271. ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
  272. TYPE) \
  273. : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
  274. TYPE))
  275. /* Hideous macro to set the value of a field in an external ELF
  276. structure to VAL. TYPEFUNCS is the set of type functions. BUFFER
  277. points to the external data. STRUCTTYPE is the appropriate
  278. structure type. FIELD is a field within the struct. TYPE is the
  279. type of the field in the struct: Elf_Half, Elf_Word, or
  280. Elf_Addr. */
  281. #define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \
  282. (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL))
  283. /* Even more hideous macro to set the value of FIELD in BUFFER to VAL.
  284. SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
  285. elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
  286. the struct. TYPE is the type of the field in the struct: Elf_Half,
  287. Elf_Word, or Elf_Addr. */
  288. #define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \
  289. TYPE, VAL) \
  290. ELF_SET_STRUCT_FIELD (TYPEFUNCS, \
  291. Elf ## SIZE ## _External_ ## STRUCTTYPE, \
  292. FIELD, BUFFER, TYPE, VAL)
  293. /* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value. */
  294. #define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD, \
  295. TYPE, VAL) \
  296. ((CLASS) == ELFCLASS32 \
  297. ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
  298. TYPE, VAL) \
  299. : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
  300. TYPE, VAL))
  301. /* Private data for an simple_object_read. */
  302. struct simple_object_elf_read
  303. {
  304. /* Type functions. */
  305. const struct elf_type_functions* type_functions;
  306. /* Elf data. */
  307. unsigned char ei_data;
  308. /* Elf class. */
  309. unsigned char ei_class;
  310. /* ELF OS ABI. */
  311. unsigned char ei_osabi;
  312. /* Elf machine number. */
  313. unsigned short machine;
  314. /* Processor specific flags. */
  315. unsigned int flags;
  316. /* File offset of section headers. */
  317. ulong_type shoff;
  318. /* Number of sections. */
  319. unsigned int shnum;
  320. /* Index of string table section header. */
  321. unsigned int shstrndx;
  322. };
  323. /* Private data for an simple_object_attributes. */
  324. struct simple_object_elf_attributes
  325. {
  326. /* Type functions. */
  327. const struct elf_type_functions* type_functions;
  328. /* Elf data. */
  329. unsigned char ei_data;
  330. /* Elf class. */
  331. unsigned char ei_class;
  332. /* ELF OS ABI. */
  333. unsigned char ei_osabi;
  334. /* Elf machine number. */
  335. unsigned short machine;
  336. /* Processor specific flags. */
  337. unsigned int flags;
  338. };
  339. /* Private data for an simple_object_write. */
  340. struct simple_object_elf_write
  341. {
  342. struct simple_object_elf_attributes attrs;
  343. unsigned char *shdrs;
  344. };
  345. /* See if we have an ELF file. */
  346. static void *
  347. simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
  348. int descriptor, off_t offset,
  349. const char *segment_name ATTRIBUTE_UNUSED,
  350. const char **errmsg, int *err)
  351. {
  352. unsigned char ei_data;
  353. unsigned char ei_class;
  354. const struct elf_type_functions *type_functions;
  355. unsigned char ehdr[sizeof (Elf64_External_Ehdr)];
  356. struct simple_object_elf_read *eor;
  357. if (header[EI_MAG0] != ELFMAG0
  358. || header[EI_MAG1] != ELFMAG1
  359. || header[EI_MAG2] != ELFMAG2
  360. || header[EI_MAG3] != ELFMAG3
  361. || header[EI_VERSION] != EV_CURRENT)
  362. {
  363. *errmsg = NULL;
  364. *err = 0;
  365. return NULL;
  366. }
  367. ei_data = header[EI_DATA];
  368. if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB)
  369. {
  370. *errmsg = "unknown ELF endianness";
  371. *err = 0;
  372. return NULL;
  373. }
  374. ei_class = header[EI_CLASS];
  375. switch (ei_class)
  376. {
  377. case ELFCLASS32:
  378. type_functions = (ei_data == ELFDATA2LSB
  379. ? &elf_little_32_functions
  380. : &elf_big_32_functions);
  381. break;
  382. case ELFCLASS64:
  383. #ifndef UNSIGNED_64BIT_TYPE
  384. *errmsg = "64-bit ELF objects not supported";
  385. *err = 0;
  386. return NULL;
  387. #else
  388. type_functions = (ei_data == ELFDATA2LSB
  389. ? &elf_little_64_functions
  390. : &elf_big_64_functions);
  391. break;
  392. #endif
  393. default:
  394. *errmsg = "unrecognized ELF size";
  395. *err = 0;
  396. return NULL;
  397. }
  398. if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr,
  399. errmsg, err))
  400. return NULL;
  401. eor = XNEW (struct simple_object_elf_read);
  402. eor->type_functions = type_functions;
  403. eor->ei_data = ei_data;
  404. eor->ei_class = ei_class;
  405. eor->ei_osabi = header[EI_OSABI];
  406. eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  407. e_machine, Elf_Half);
  408. eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  409. e_flags, Elf_Word);
  410. eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  411. e_shoff, Elf_Addr);
  412. eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  413. e_shnum, Elf_Half);
  414. eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  415. e_shstrndx, Elf_Half);
  416. if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX)
  417. && eor->shoff != 0)
  418. {
  419. unsigned char shdr[sizeof (Elf64_External_Shdr)];
  420. /* Object file has more than 0xffff sections. */
  421. if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr,
  422. (ei_class == ELFCLASS32
  423. ? sizeof (Elf32_External_Shdr)
  424. : sizeof (Elf64_External_Shdr)),
  425. errmsg, err))
  426. {
  427. XDELETE (eor);
  428. return NULL;
  429. }
  430. if (eor->shnum == 0)
  431. eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  432. shdr, sh_size, Elf_Addr);
  433. if (eor->shstrndx == SHN_XINDEX)
  434. {
  435. eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  436. shdr, sh_link, Elf_Word);
  437. /* Versions of the GNU binutils between 2.12 and 2.18 did
  438. not handle objects with more than SHN_LORESERVE sections
  439. correctly. All large section indexes were offset by
  440. 0x100. There is more information at
  441. http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
  442. Fortunately these object files are easy to detect, as the
  443. GNU binutils always put the section header string table
  444. near the end of the list of sections. Thus if the
  445. section header string table index is larger than the
  446. number of sections, then we know we have to subtract
  447. 0x100 to get the real section index. */
  448. if (eor->shstrndx >= eor->shnum
  449. && eor->shstrndx >= SHN_LORESERVE + 0x100)
  450. eor->shstrndx -= 0x100;
  451. }
  452. }
  453. if (eor->shstrndx >= eor->shnum)
  454. {
  455. *errmsg = "invalid ELF shstrndx >= shnum";
  456. *err = 0;
  457. XDELETE (eor);
  458. return NULL;
  459. }
  460. if (eor->shstrndx == 0)
  461. {
  462. *errmsg = "invalid ELF shstrndx == 0";
  463. *err = 0;
  464. XDELETE (eor);
  465. return NULL;
  466. }
  467. return (void *) eor;
  468. }
  469. /* Find all sections in an ELF file. */
  470. static const char *
  471. simple_object_elf_find_sections (simple_object_read *sobj,
  472. int (*pfn) (void *, const char *,
  473. off_t offset, off_t length),
  474. void *data,
  475. int *err)
  476. {
  477. struct simple_object_elf_read *eor =
  478. (struct simple_object_elf_read *) sobj->data;
  479. const struct elf_type_functions *type_functions = eor->type_functions;
  480. unsigned char ei_class = eor->ei_class;
  481. size_t shdr_size;
  482. unsigned int shnum;
  483. unsigned char *shdrs;
  484. const char *errmsg;
  485. unsigned char *shstrhdr;
  486. size_t name_size;
  487. off_t shstroff;
  488. unsigned char *names;
  489. unsigned int i;
  490. shdr_size = (ei_class == ELFCLASS32
  491. ? sizeof (Elf32_External_Shdr)
  492. : sizeof (Elf64_External_Shdr));
  493. /* Read the section headers. We skip section 0, which is not a
  494. useful section. */
  495. shnum = eor->shnum;
  496. shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
  497. if (!simple_object_internal_read (sobj->descriptor,
  498. sobj->offset + eor->shoff + shdr_size,
  499. shdrs,
  500. shdr_size * (shnum - 1),
  501. &errmsg, err))
  502. {
  503. XDELETEVEC (shdrs);
  504. return errmsg;
  505. }
  506. /* Read the section names. */
  507. shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
  508. name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  509. shstrhdr, sh_size, Elf_Addr);
  510. shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  511. shstrhdr, sh_offset, Elf_Addr);
  512. names = XNEWVEC (unsigned char, name_size);
  513. if (!simple_object_internal_read (sobj->descriptor,
  514. sobj->offset + shstroff,
  515. names, name_size, &errmsg, err))
  516. {
  517. XDELETEVEC (names);
  518. XDELETEVEC (shdrs);
  519. return errmsg;
  520. }
  521. for (i = 1; i < shnum; ++i)
  522. {
  523. unsigned char *shdr;
  524. unsigned int sh_name;
  525. const char *name;
  526. off_t offset;
  527. off_t length;
  528. shdr = shdrs + (i - 1) * shdr_size;
  529. sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  530. shdr, sh_name, Elf_Word);
  531. if (sh_name >= name_size)
  532. {
  533. *err = 0;
  534. XDELETEVEC (names);
  535. XDELETEVEC (shdrs);
  536. return "ELF section name out of range";
  537. }
  538. name = (const char *) names + sh_name;
  539. offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  540. shdr, sh_offset, Elf_Addr);
  541. length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  542. shdr, sh_size, Elf_Addr);
  543. if (!(*pfn) (data, name, offset, length))
  544. break;
  545. }
  546. XDELETEVEC (names);
  547. XDELETEVEC (shdrs);
  548. return NULL;
  549. }
  550. /* Fetch the attributes for an simple_object_read. */
  551. static void *
  552. simple_object_elf_fetch_attributes (simple_object_read *sobj,
  553. const char **errmsg ATTRIBUTE_UNUSED,
  554. int *err ATTRIBUTE_UNUSED)
  555. {
  556. struct simple_object_elf_read *eor =
  557. (struct simple_object_elf_read *) sobj->data;
  558. struct simple_object_elf_attributes *ret;
  559. ret = XNEW (struct simple_object_elf_attributes);
  560. ret->type_functions = eor->type_functions;
  561. ret->ei_data = eor->ei_data;
  562. ret->ei_class = eor->ei_class;
  563. ret->ei_osabi = eor->ei_osabi;
  564. ret->machine = eor->machine;
  565. ret->flags = eor->flags;
  566. return ret;
  567. }
  568. /* Release the privata data for an simple_object_read. */
  569. static void
  570. simple_object_elf_release_read (void *data)
  571. {
  572. XDELETE (data);
  573. }
  574. /* Compare two attributes structures. */
  575. static const char *
  576. simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err)
  577. {
  578. struct simple_object_elf_attributes *to =
  579. (struct simple_object_elf_attributes *) todata;
  580. struct simple_object_elf_attributes *from =
  581. (struct simple_object_elf_attributes *) fromdata;
  582. if (to->ei_data != from->ei_data || to->ei_class != from->ei_class)
  583. {
  584. *err = 0;
  585. return "ELF object format mismatch";
  586. }
  587. if (to->machine != from->machine)
  588. {
  589. int ok;
  590. /* EM_SPARC and EM_SPARC32PLUS are compatible and force an
  591. output of EM_SPARC32PLUS. */
  592. ok = 0;
  593. switch (to->machine)
  594. {
  595. case EM_SPARC:
  596. if (from->machine == EM_SPARC32PLUS)
  597. {
  598. to->machine = from->machine;
  599. ok = 1;
  600. }
  601. break;
  602. case EM_SPARC32PLUS:
  603. if (from->machine == EM_SPARC)
  604. ok = 1;
  605. break;
  606. default:
  607. break;
  608. }
  609. if (!ok)
  610. {
  611. *err = 0;
  612. return "ELF machine number mismatch";
  613. }
  614. }
  615. return NULL;
  616. }
  617. /* Release the private data for an attributes structure. */
  618. static void
  619. simple_object_elf_release_attributes (void *data)
  620. {
  621. XDELETE (data);
  622. }
  623. /* Prepare to write out a file. */
  624. static void *
  625. simple_object_elf_start_write (void *attributes_data,
  626. const char **errmsg ATTRIBUTE_UNUSED,
  627. int *err ATTRIBUTE_UNUSED)
  628. {
  629. struct simple_object_elf_attributes *attrs =
  630. (struct simple_object_elf_attributes *) attributes_data;
  631. struct simple_object_elf_write *ret;
  632. /* We're just going to record the attributes, but we need to make a
  633. copy because the user may delete them. */
  634. ret = XNEW (struct simple_object_elf_write);
  635. ret->attrs = *attrs;
  636. ret->shdrs = NULL;
  637. return ret;
  638. }
  639. /* Write out an ELF ehdr. */
  640. static int
  641. simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor,
  642. const char **errmsg, int *err)
  643. {
  644. struct simple_object_elf_attributes *attrs =
  645. (struct simple_object_elf_attributes *) sobj->data;
  646. const struct elf_type_functions* fns;
  647. unsigned char cl;
  648. size_t ehdr_size;
  649. unsigned char buf[sizeof (Elf64_External_Ehdr)];
  650. simple_object_write_section *section;
  651. unsigned int shnum;
  652. unsigned int shstrndx;
  653. fns = attrs->type_functions;
  654. cl = attrs->ei_class;
  655. shnum = 0;
  656. for (section = sobj->sections; section != NULL; section = section->next)
  657. ++shnum;
  658. if (shnum > 0)
  659. {
  660. /* Add a section header for the dummy section and one for
  661. .shstrtab. */
  662. shnum += 2;
  663. }
  664. ehdr_size = (cl == ELFCLASS32
  665. ? sizeof (Elf32_External_Ehdr)
  666. : sizeof (Elf64_External_Ehdr));
  667. memset (buf, 0, sizeof (Elf64_External_Ehdr));
  668. buf[EI_MAG0] = ELFMAG0;
  669. buf[EI_MAG1] = ELFMAG1;
  670. buf[EI_MAG2] = ELFMAG2;
  671. buf[EI_MAG3] = ELFMAG3;
  672. buf[EI_CLASS] = cl;
  673. buf[EI_DATA] = attrs->ei_data;
  674. buf[EI_VERSION] = EV_CURRENT;
  675. buf[EI_OSABI] = attrs->ei_osabi;
  676. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL);
  677. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine);
  678. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT);
  679. /* e_entry left as zero. */
  680. /* e_phoff left as zero. */
  681. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size);
  682. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags);
  683. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size);
  684. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half,
  685. (cl == ELFCLASS32
  686. ? sizeof (Elf32_External_Phdr)
  687. : sizeof (Elf64_External_Phdr)));
  688. /* e_phnum left as zero. */
  689. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half,
  690. (cl == ELFCLASS32
  691. ? sizeof (Elf32_External_Shdr)
  692. : sizeof (Elf64_External_Shdr)));
  693. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half,
  694. shnum >= SHN_LORESERVE ? 0 : shnum);
  695. if (shnum == 0)
  696. shstrndx = 0;
  697. else
  698. {
  699. shstrndx = shnum - 1;
  700. if (shstrndx >= SHN_LORESERVE)
  701. shstrndx = SHN_XINDEX;
  702. }
  703. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx);
  704. return simple_object_internal_write (descriptor, 0, buf, ehdr_size,
  705. errmsg, err);
  706. }
  707. /* Write out an ELF shdr. */
  708. static int
  709. simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor,
  710. off_t offset, unsigned int sh_name,
  711. unsigned int sh_type, unsigned int sh_flags,
  712. off_t sh_addr,
  713. unsigned int sh_offset, unsigned int sh_size,
  714. unsigned int sh_link, unsigned int sh_info,
  715. size_t sh_addralign,
  716. size_t sh_entsize,
  717. const char **errmsg, int *err)
  718. {
  719. struct simple_object_elf_attributes *attrs =
  720. (struct simple_object_elf_attributes *) sobj->data;
  721. const struct elf_type_functions* fns;
  722. unsigned char cl;
  723. size_t shdr_size;
  724. unsigned char buf[sizeof (Elf64_External_Shdr)];
  725. fns = attrs->type_functions;
  726. cl = attrs->ei_class;
  727. shdr_size = (cl == ELFCLASS32
  728. ? sizeof (Elf32_External_Shdr)
  729. : sizeof (Elf64_External_Shdr));
  730. memset (buf, 0, sizeof (Elf64_External_Shdr));
  731. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name);
  732. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type);
  733. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags);
  734. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addr, Elf_Addr, sh_addr);
  735. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset);
  736. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size);
  737. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link);
  738. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_info, Elf_Word, sh_info);
  739. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign);
  740. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_entsize, Elf_Addr, sh_entsize);
  741. return simple_object_internal_write (descriptor, offset, buf, shdr_size,
  742. errmsg, err);
  743. }
  744. /* Write out a complete ELF file.
  745. Ehdr
  746. initial dummy Shdr
  747. user-created Shdrs
  748. .shstrtab Shdr
  749. user-created section data
  750. .shstrtab data */
  751. static const char *
  752. simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor,
  753. int *err)
  754. {
  755. struct simple_object_elf_write *eow =
  756. (struct simple_object_elf_write *) sobj->data;
  757. struct simple_object_elf_attributes *attrs = &eow->attrs;
  758. unsigned char cl;
  759. size_t ehdr_size;
  760. size_t shdr_size;
  761. const char *errmsg;
  762. simple_object_write_section *section;
  763. unsigned int shnum;
  764. size_t shdr_offset;
  765. size_t sh_offset;
  766. unsigned int first_sh_size;
  767. unsigned int first_sh_link;
  768. size_t sh_name;
  769. unsigned char zero;
  770. unsigned secnum;
  771. if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err))
  772. return errmsg;
  773. cl = attrs->ei_class;
  774. if (cl == ELFCLASS32)
  775. {
  776. ehdr_size = sizeof (Elf32_External_Ehdr);
  777. shdr_size = sizeof (Elf32_External_Shdr);
  778. }
  779. else
  780. {
  781. ehdr_size = sizeof (Elf64_External_Ehdr);
  782. shdr_size = sizeof (Elf64_External_Shdr);
  783. }
  784. shnum = 0;
  785. for (section = sobj->sections; section != NULL; section = section->next)
  786. ++shnum;
  787. if (shnum == 0)
  788. return NULL;
  789. /* Add initial dummy Shdr and .shstrtab. */
  790. shnum += 2;
  791. shdr_offset = ehdr_size;
  792. sh_offset = shdr_offset + shnum * shdr_size;
  793. if (shnum < SHN_LORESERVE)
  794. first_sh_size = 0;
  795. else
  796. first_sh_size = shnum;
  797. if (shnum - 1 < SHN_LORESERVE)
  798. first_sh_link = 0;
  799. else
  800. first_sh_link = shnum - 1;
  801. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  802. 0, 0, 0, 0, 0, first_sh_size, first_sh_link,
  803. 0, 0, 0, &errmsg, err))
  804. return errmsg;
  805. shdr_offset += shdr_size;
  806. sh_name = 1;
  807. secnum = 0;
  808. for (section = sobj->sections; section != NULL; section = section->next)
  809. {
  810. size_t mask;
  811. size_t new_sh_offset;
  812. size_t sh_size;
  813. struct simple_object_write_section_buffer *buffer;
  814. unsigned int sh_type = SHT_PROGBITS;
  815. unsigned int sh_flags = 0;
  816. off_t sh_addr = 0;
  817. unsigned int sh_link = 0;
  818. unsigned int sh_info = 0;
  819. size_t sh_addralign = 1U << section->align;
  820. size_t sh_entsize = 0;
  821. if (eow->shdrs)
  822. {
  823. sh_type = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  824. eow->shdrs + secnum * shdr_size,
  825. sh_type, Elf_Word);
  826. sh_flags = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  827. eow->shdrs + secnum * shdr_size,
  828. sh_flags, Elf_Addr);
  829. sh_addr = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  830. eow->shdrs + secnum * shdr_size,
  831. sh_addr, Elf_Addr);
  832. sh_link = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  833. eow->shdrs + secnum * shdr_size,
  834. sh_link, Elf_Word);
  835. sh_info = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  836. eow->shdrs + secnum * shdr_size,
  837. sh_info, Elf_Word);
  838. sh_addralign = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  839. eow->shdrs + secnum * shdr_size,
  840. sh_addralign, Elf_Addr);
  841. sh_entsize = ELF_FETCH_FIELD (attrs->type_functions, attrs->ei_class, Shdr,
  842. eow->shdrs + secnum * shdr_size,
  843. sh_entsize, Elf_Addr);
  844. secnum++;
  845. }
  846. mask = sh_addralign - 1;
  847. new_sh_offset = sh_offset + mask;
  848. new_sh_offset &= ~ mask;
  849. while (new_sh_offset > sh_offset)
  850. {
  851. unsigned char zeroes[16];
  852. size_t write;
  853. memset (zeroes, 0, sizeof zeroes);
  854. write = new_sh_offset - sh_offset;
  855. if (write > sizeof zeroes)
  856. write = sizeof zeroes;
  857. if (!simple_object_internal_write (descriptor, sh_offset, zeroes,
  858. write, &errmsg, err))
  859. return errmsg;
  860. sh_offset += write;
  861. }
  862. sh_size = 0;
  863. for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
  864. {
  865. if (!simple_object_internal_write (descriptor, sh_offset + sh_size,
  866. ((const unsigned char *)
  867. buffer->buffer),
  868. buffer->size, &errmsg, err))
  869. return errmsg;
  870. sh_size += buffer->size;
  871. }
  872. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  873. sh_name, sh_type, sh_flags,
  874. sh_addr, sh_offset,
  875. sh_size, sh_link, sh_info,
  876. sh_addralign, sh_entsize,
  877. &errmsg, err))
  878. return errmsg;
  879. shdr_offset += shdr_size;
  880. sh_name += strlen (section->name) + 1;
  881. sh_offset += sh_size;
  882. }
  883. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  884. sh_name, SHT_STRTAB, 0, 0, sh_offset,
  885. sh_name + strlen (".shstrtab") + 1, 0, 0,
  886. 1, 0, &errmsg, err))
  887. return errmsg;
  888. /* .shstrtab has a leading zero byte. */
  889. zero = 0;
  890. if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1,
  891. &errmsg, err))
  892. return errmsg;
  893. ++sh_offset;
  894. for (section = sobj->sections; section != NULL; section = section->next)
  895. {
  896. size_t len;
  897. len = strlen (section->name) + 1;
  898. if (!simple_object_internal_write (descriptor, sh_offset,
  899. (const unsigned char *) section->name,
  900. len, &errmsg, err))
  901. return errmsg;
  902. sh_offset += len;
  903. }
  904. if (!simple_object_internal_write (descriptor, sh_offset,
  905. (const unsigned char *) ".shstrtab",
  906. strlen (".shstrtab") + 1, &errmsg, err))
  907. return errmsg;
  908. return NULL;
  909. }
  910. /* Release the private data for an simple_object_write structure. */
  911. static void
  912. simple_object_elf_release_write (void *data)
  913. {
  914. struct simple_object_elf_write *eow = (struct simple_object_elf_write *) data;
  915. if (eow->shdrs)
  916. XDELETE (eow->shdrs);
  917. XDELETE (data);
  918. }
  919. /* Copy all sections in an ELF file. */
  920. static const char *
  921. simple_object_elf_copy_lto_debug_sections (simple_object_read *sobj,
  922. simple_object_write *dobj,
  923. char *(*pfn) (const char *),
  924. int *err)
  925. {
  926. struct simple_object_elf_read *eor =
  927. (struct simple_object_elf_read *) sobj->data;
  928. const struct elf_type_functions *type_functions = eor->type_functions;
  929. struct simple_object_elf_write *eow =
  930. (struct simple_object_elf_write *) dobj->data;
  931. unsigned char ei_class = eor->ei_class;
  932. size_t shdr_size;
  933. unsigned int shnum;
  934. unsigned char *shdrs;
  935. const char *errmsg;
  936. unsigned char *shstrhdr;
  937. size_t name_size;
  938. off_t shstroff;
  939. unsigned char *names;
  940. unsigned int i;
  941. int changed;
  942. int *pfnret;
  943. const char **pfnname;
  944. unsigned new_i;
  945. unsigned *sh_map;
  946. unsigned first_shndx = 0;
  947. unsigned int *symtab_indices_shndx;
  948. shdr_size = (ei_class == ELFCLASS32
  949. ? sizeof (Elf32_External_Shdr)
  950. : sizeof (Elf64_External_Shdr));
  951. /* Read the section headers. We skip section 0, which is not a
  952. useful section. */
  953. shnum = eor->shnum;
  954. shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
  955. if (!simple_object_internal_read (sobj->descriptor,
  956. sobj->offset + eor->shoff + shdr_size,
  957. shdrs,
  958. shdr_size * (shnum - 1),
  959. &errmsg, err))
  960. {
  961. XDELETEVEC (shdrs);
  962. return errmsg;
  963. }
  964. /* Read the section names. */
  965. shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
  966. name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  967. shstrhdr, sh_size, Elf_Addr);
  968. shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  969. shstrhdr, sh_offset, Elf_Addr);
  970. names = XNEWVEC (unsigned char, name_size);
  971. if (!simple_object_internal_read (sobj->descriptor,
  972. sobj->offset + shstroff,
  973. names, name_size, &errmsg, err))
  974. {
  975. XDELETEVEC (names);
  976. XDELETEVEC (shdrs);
  977. return errmsg;
  978. }
  979. pfnret = XNEWVEC (int, shnum);
  980. pfnname = XNEWVEC (const char *, shnum);
  981. /* Map of symtab to index section. */
  982. symtab_indices_shndx = XCNEWVEC (unsigned int, shnum - 1);
  983. /* First perform the callbacks to know which sections to preserve and
  984. what name to use for those. */
  985. for (i = 1; i < shnum; ++i)
  986. {
  987. unsigned char *shdr;
  988. unsigned int sh_name, sh_type;
  989. const char *name;
  990. char *ret;
  991. shdr = shdrs + (i - 1) * shdr_size;
  992. sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  993. shdr, sh_name, Elf_Word);
  994. if (sh_name >= name_size)
  995. {
  996. *err = 0;
  997. XDELETEVEC (names);
  998. XDELETEVEC (shdrs);
  999. return "ELF section name out of range";
  1000. }
  1001. name = (const char *) names + sh_name;
  1002. ret = (*pfn) (name);
  1003. pfnret[i - 1] = ret == NULL ? -1 : 0;
  1004. pfnname[i - 1] = ret == NULL ? name : ret;
  1005. if (first_shndx == 0
  1006. && pfnret[i - 1] == 0)
  1007. first_shndx = i;
  1008. /* Remember the indexes of existing SHT_SYMTAB_SHNDX sections. */
  1009. sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1010. shdr, sh_type, Elf_Word);
  1011. if (sh_type == SHT_SYMTAB_SHNDX)
  1012. {
  1013. unsigned int sh_link;
  1014. sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1015. shdr, sh_link, Elf_Word);
  1016. symtab_indices_shndx[sh_link - 1] = i - 1;
  1017. /* Always discard the extended index sections, after
  1018. copying it will not be needed. This way we don't need to
  1019. update it and deal with the ordering constraints of
  1020. processing the existing symtab and changing the index. */
  1021. pfnret[i - 1] = -1;
  1022. }
  1023. }
  1024. /* Mark sections as preserved that are required by to be preserved
  1025. sections. */
  1026. do
  1027. {
  1028. changed = 0;
  1029. for (i = 1; i < shnum; ++i)
  1030. {
  1031. unsigned char *shdr;
  1032. unsigned int sh_type, sh_info, sh_link;
  1033. off_t offset;
  1034. off_t length;
  1035. shdr = shdrs + (i - 1) * shdr_size;
  1036. sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1037. shdr, sh_type, Elf_Word);
  1038. sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1039. shdr, sh_info, Elf_Word);
  1040. sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1041. shdr, sh_link, Elf_Word);
  1042. if (sh_type == SHT_GROUP)
  1043. {
  1044. /* Mark groups containing copied sections. */
  1045. unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class,
  1046. Shdr, shdr, sh_entsize,
  1047. Elf_Addr);
  1048. unsigned char *ent, *buf;
  1049. int keep = 0;
  1050. offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1051. shdr, sh_offset, Elf_Addr);
  1052. length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1053. shdr, sh_size, Elf_Addr);
  1054. buf = XNEWVEC (unsigned char, length);
  1055. if (!simple_object_internal_read (sobj->descriptor,
  1056. sobj->offset + offset, buf,
  1057. (size_t) length, &errmsg, err))
  1058. {
  1059. XDELETEVEC (buf);
  1060. XDELETEVEC (names);
  1061. XDELETEVEC (shdrs);
  1062. return errmsg;
  1063. }
  1064. for (ent = buf + entsize; ent < buf + length; ent += entsize)
  1065. {
  1066. unsigned sec = type_functions->fetch_Elf_Word (ent);
  1067. if (pfnret[sec - 1] == 0)
  1068. keep = 1;
  1069. }
  1070. if (keep)
  1071. {
  1072. changed |= (pfnret[sh_link - 1] == -1
  1073. || pfnret[i - 1] == -1);
  1074. pfnret[sh_link - 1] = 0;
  1075. pfnret[i - 1] = 0;
  1076. }
  1077. }
  1078. if (sh_type == SHT_RELA
  1079. || sh_type == SHT_REL)
  1080. {
  1081. /* Mark relocation sections and symtab of copied sections. */
  1082. if (pfnret[sh_info - 1] == 0)
  1083. {
  1084. changed |= (pfnret[sh_link - 1] == -1
  1085. || pfnret[i - 1] == -1);
  1086. pfnret[sh_link - 1] = 0;
  1087. pfnret[i - 1] = 0;
  1088. }
  1089. }
  1090. if (sh_type == SHT_SYMTAB)
  1091. {
  1092. /* Mark strings sections of copied symtabs. */
  1093. if (pfnret[i - 1] == 0)
  1094. {
  1095. changed |= pfnret[sh_link - 1] == -1;
  1096. pfnret[sh_link - 1] = 0;
  1097. }
  1098. }
  1099. }
  1100. }
  1101. while (changed);
  1102. /* Compute a mapping of old -> new section numbers. */
  1103. sh_map = XNEWVEC (unsigned, shnum);
  1104. sh_map[0] = 0;
  1105. new_i = 1;
  1106. for (i = 1; i < shnum; ++i)
  1107. {
  1108. if (pfnret[i - 1] == -1)
  1109. sh_map[i] = 0;
  1110. else
  1111. sh_map[i] = new_i++;
  1112. }
  1113. if (new_i - 1 >= SHN_LORESERVE)
  1114. {
  1115. *err = ENOTSUP;
  1116. return "Too many copied sections";
  1117. }
  1118. eow->shdrs = XNEWVEC (unsigned char, shdr_size * (new_i - 1));
  1119. /* Then perform the actual copying. */
  1120. new_i = 0;
  1121. for (i = 1; i < shnum; ++i)
  1122. {
  1123. unsigned char *shdr;
  1124. unsigned int sh_name, sh_type;
  1125. const char *name;
  1126. off_t offset;
  1127. off_t length;
  1128. simple_object_write_section *dest;
  1129. off_t flags;
  1130. unsigned char *buf;
  1131. if (pfnret[i - 1])
  1132. continue;
  1133. new_i++;
  1134. shdr = shdrs + (i - 1) * shdr_size;
  1135. sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1136. shdr, sh_name, Elf_Word);
  1137. if (sh_name >= name_size)
  1138. {
  1139. *err = 0;
  1140. XDELETEVEC (names);
  1141. XDELETEVEC (shdrs);
  1142. XDELETEVEC (symtab_indices_shndx);
  1143. return "ELF section name out of range";
  1144. }
  1145. name = pfnname[i - 1];
  1146. offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1147. shdr, sh_offset, Elf_Addr);
  1148. length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1149. shdr, sh_size, Elf_Addr);
  1150. sh_type = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1151. shdr, sh_type, Elf_Word);
  1152. dest = simple_object_write_create_section (dobj, pfnname[i - 1],
  1153. 0, &errmsg, err);
  1154. if (dest == NULL)
  1155. {
  1156. XDELETEVEC (names);
  1157. XDELETEVEC (shdrs);
  1158. XDELETEVEC (symtab_indices_shndx);
  1159. return errmsg;
  1160. }
  1161. /* Record the SHDR of the source. */
  1162. memcpy (eow->shdrs + (new_i - 1) * shdr_size, shdr, shdr_size);
  1163. shdr = eow->shdrs + (new_i - 1) * shdr_size;
  1164. /* Copy the data.
  1165. ??? This is quite wasteful and ideally would be delayed until
  1166. write_to_file (). Thus it questions the interfacing
  1167. which eventually should contain destination creation plus
  1168. writing. */
  1169. buf = XNEWVEC (unsigned char, length);
  1170. if (!simple_object_internal_read (sobj->descriptor,
  1171. sobj->offset + offset, buf,
  1172. (size_t) length, &errmsg, err))
  1173. {
  1174. XDELETEVEC (buf);
  1175. XDELETEVEC (names);
  1176. XDELETEVEC (shdrs);
  1177. XDELETEVEC (symtab_indices_shndx);
  1178. return errmsg;
  1179. }
  1180. /* If we are processing .symtab purge any symbols
  1181. in discarded sections. */
  1182. if (sh_type == SHT_SYMTAB)
  1183. {
  1184. unsigned entsize = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1185. shdr, sh_entsize, Elf_Addr);
  1186. size_t prevailing_name_idx = 0;
  1187. unsigned char *ent;
  1188. unsigned *shndx_table = NULL;
  1189. /* Read the section index table if present. */
  1190. if (symtab_indices_shndx[i - 1] != 0)
  1191. {
  1192. unsigned char *sidxhdr = shdrs + symtab_indices_shndx[i - 1] * shdr_size;
  1193. off_t sidxoff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1194. sidxhdr, sh_offset, Elf_Addr);
  1195. size_t sidxsz = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1196. sidxhdr, sh_size, Elf_Addr);
  1197. unsigned int shndx_type
  1198. = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1199. sidxhdr, sh_type, Elf_Word);
  1200. if (shndx_type != SHT_SYMTAB_SHNDX)
  1201. return "Wrong section type of a SYMTAB SECTION INDICES section";
  1202. shndx_table = (unsigned *)XNEWVEC (char, sidxsz);
  1203. simple_object_internal_read (sobj->descriptor,
  1204. sobj->offset + sidxoff,
  1205. (unsigned char *)shndx_table,
  1206. sidxsz, &errmsg, err);
  1207. }
  1208. /* Find a WEAK HIDDEN symbol which name we will use for removed
  1209. symbols. We know there's a prevailing weak hidden symbol
  1210. at the start of the .debug_info section. */
  1211. for (ent = buf; ent < buf + length; ent += entsize)
  1212. {
  1213. unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class,
  1214. Sym, ent,
  1215. st_shndx, Elf_Half);
  1216. unsigned char *st_info;
  1217. unsigned char *st_other;
  1218. if (ei_class == ELFCLASS32)
  1219. {
  1220. st_info = &((Elf32_External_Sym *)ent)->st_info;
  1221. st_other = &((Elf32_External_Sym *)ent)->st_other;
  1222. }
  1223. else
  1224. {
  1225. st_info = &((Elf64_External_Sym *)ent)->st_info;
  1226. st_other = &((Elf64_External_Sym *)ent)->st_other;
  1227. }
  1228. if (st_shndx == SHN_XINDEX)
  1229. st_shndx = type_functions->fetch_Elf_Word
  1230. ((unsigned char *)(shndx_table + (ent - buf) / entsize));
  1231. if (st_shndx != SHN_COMMON
  1232. && !(st_shndx != SHN_UNDEF
  1233. && st_shndx < shnum
  1234. && pfnret[st_shndx - 1] == -1)
  1235. && ELF_ST_BIND (*st_info) == STB_WEAK
  1236. && *st_other == STV_HIDDEN)
  1237. {
  1238. prevailing_name_idx = ELF_FETCH_FIELD (type_functions,
  1239. ei_class, Sym, ent,
  1240. st_name, Elf_Word);
  1241. break;
  1242. }
  1243. }
  1244. for (ent = buf; ent < buf + length; ent += entsize)
  1245. {
  1246. unsigned st_shndx = ELF_FETCH_FIELD (type_functions, ei_class,
  1247. Sym, ent,
  1248. st_shndx, Elf_Half);
  1249. unsigned raw_st_shndx = st_shndx;
  1250. unsigned char *st_info;
  1251. unsigned char *st_other;
  1252. int discard = 0;
  1253. if (ei_class == ELFCLASS32)
  1254. {
  1255. st_info = &((Elf32_External_Sym *)ent)->st_info;
  1256. st_other = &((Elf32_External_Sym *)ent)->st_other;
  1257. }
  1258. else
  1259. {
  1260. st_info = &((Elf64_External_Sym *)ent)->st_info;
  1261. st_other = &((Elf64_External_Sym *)ent)->st_other;
  1262. }
  1263. if (st_shndx == SHN_XINDEX)
  1264. st_shndx = type_functions->fetch_Elf_Word
  1265. ((unsigned char *)(shndx_table + (ent - buf) / entsize));
  1266. /* Eliminate all COMMONs - this includes __gnu_lto_slim
  1267. which otherwise cause endless LTO plugin invocation.
  1268. FIXME: remove the condition once we remove emission
  1269. of __gnu_lto_slim symbol. */
  1270. if (st_shndx == SHN_COMMON)
  1271. discard = 1;
  1272. /* We also need to remove symbols refering to sections
  1273. we'll eventually remove as with fat LTO objects
  1274. we otherwise get duplicate symbols at final link
  1275. (with GNU ld, gold is fine and ignores symbols in
  1276. sections marked as EXCLUDE). ld/20513 */
  1277. else if (st_shndx != SHN_UNDEF
  1278. && st_shndx < shnum
  1279. && pfnret[st_shndx - 1] == -1)
  1280. discard = 1;
  1281. /* We also need to remove global UNDEFs which can
  1282. cause link fails later. */
  1283. else if (st_shndx == SHN_UNDEF
  1284. && ELF_ST_BIND (*st_info) == STB_GLOBAL)
  1285. discard = 1;
  1286. if (discard)
  1287. {
  1288. /* Make discarded symbols undefined and unnamed
  1289. in case it is local. */
  1290. int bind = ELF_ST_BIND (*st_info);
  1291. int other = STV_DEFAULT;
  1292. if (bind == STB_LOCAL)
  1293. {
  1294. /* Make discarded local symbols unnamed and
  1295. defined in the first prevailing section. */
  1296. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1297. ent, st_name, Elf_Word, 0);
  1298. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1299. ent, st_shndx, Elf_Half,
  1300. sh_map[first_shndx]);
  1301. }
  1302. else
  1303. {
  1304. /* Make discarded global symbols hidden weak
  1305. undefined and sharing a name of a prevailing
  1306. symbol. */
  1307. bind = STB_WEAK;
  1308. other = STV_HIDDEN;
  1309. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1310. ent, st_name, Elf_Word,
  1311. prevailing_name_idx);
  1312. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1313. ent, st_shndx, Elf_Half, SHN_UNDEF);
  1314. }
  1315. *st_other = other;
  1316. *st_info = ELF_ST_INFO (bind, STT_NOTYPE);
  1317. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1318. ent, st_value, Elf_Addr, 0);
  1319. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1320. ent, st_size, Elf_Word, 0);
  1321. }
  1322. else if (raw_st_shndx < SHN_LORESERVE
  1323. || raw_st_shndx == SHN_XINDEX)
  1324. /* Remap the section reference. */
  1325. ELF_SET_FIELD (type_functions, ei_class, Sym,
  1326. ent, st_shndx, Elf_Half, sh_map[st_shndx]);
  1327. }
  1328. XDELETEVEC (shndx_table);
  1329. }
  1330. else if (sh_type == SHT_GROUP)
  1331. {
  1332. /* Remap section indices in groups and remove removed members. */
  1333. unsigned char *ent, *dst;
  1334. for (dst = ent = buf + 4; ent < buf + length; ent += 4)
  1335. {
  1336. unsigned shndx = type_functions->fetch_Elf_Word (ent);
  1337. if (pfnret[shndx - 1] == -1)
  1338. ;
  1339. else
  1340. {
  1341. type_functions->set_Elf_Word (dst, sh_map[shndx]);
  1342. dst += 4;
  1343. }
  1344. }
  1345. /* Adjust the length. */
  1346. length = dst - buf;
  1347. }
  1348. errmsg = simple_object_write_add_data (dobj, dest,
  1349. buf, length, 1, err);
  1350. XDELETEVEC (buf);
  1351. if (errmsg)
  1352. {
  1353. XDELETEVEC (names);
  1354. XDELETEVEC (shdrs);
  1355. XDELETEVEC (symtab_indices_shndx);
  1356. return errmsg;
  1357. }
  1358. flags = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1359. shdr, sh_flags, Elf_Addr);
  1360. /* Remap the section references. */
  1361. {
  1362. unsigned int sh_info, sh_link;
  1363. if (flags & SHF_INFO_LINK || sh_type == SHT_REL || sh_type == SHT_RELA)
  1364. {
  1365. sh_info = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1366. shdr, sh_info, Elf_Word);
  1367. if (sh_info < SHN_LORESERVE
  1368. || sh_info > SHN_HIRESERVE)
  1369. sh_info = sh_map[sh_info];
  1370. ELF_SET_FIELD (type_functions, ei_class, Shdr,
  1371. shdr, sh_info, Elf_Word, sh_info);
  1372. }
  1373. sh_link = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  1374. shdr, sh_link, Elf_Word);
  1375. if (sh_link < SHN_LORESERVE
  1376. || sh_link > SHN_HIRESERVE)
  1377. sh_link = sh_map[sh_link];
  1378. ELF_SET_FIELD (type_functions, ei_class, Shdr,
  1379. shdr, sh_link, Elf_Word, sh_link);
  1380. }
  1381. /* The debugobj doesn't contain any code, thus no trampolines.
  1382. Even when the original object needs trampolines, debugobj
  1383. doesn't. */
  1384. if (strcmp (name, ".note.GNU-stack") == 0)
  1385. flags &= ~SHF_EXECINSTR;
  1386. /* Clear SHF_EXCLUDE on to be preserved sections. */
  1387. flags &= ~SHF_EXCLUDE;
  1388. ELF_SET_FIELD (type_functions, ei_class, Shdr,
  1389. shdr, sh_flags, Elf_Addr, flags);
  1390. }
  1391. XDELETEVEC (names);
  1392. XDELETEVEC (shdrs);
  1393. XDELETEVEC (pfnret);
  1394. XDELETEVEC (pfnname);
  1395. XDELETEVEC (symtab_indices_shndx);
  1396. XDELETEVEC (sh_map);
  1397. return NULL;
  1398. }
  1399. /* The ELF functions. */
  1400. const struct simple_object_functions simple_object_elf_functions =
  1401. {
  1402. simple_object_elf_match,
  1403. simple_object_elf_find_sections,
  1404. simple_object_elf_fetch_attributes,
  1405. simple_object_elf_release_read,
  1406. simple_object_elf_attributes_merge,
  1407. simple_object_elf_release_attributes,
  1408. simple_object_elf_start_write,
  1409. simple_object_elf_write_to_file,
  1410. simple_object_elf_release_write,
  1411. simple_object_elf_copy_lto_debug_sections
  1412. };