simple-object-elf.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  1. /* simple-object-elf.c -- routines to manipulate ELF object files.
  2. Copyright 2010 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. #include <stddef.h>
  21. #ifdef HAVE_STDLIB_H
  22. #include <stdlib.h>
  23. #endif
  24. #ifdef HAVE_STDINT_H
  25. #include <stdint.h>
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. #include <string.h>
  29. #endif
  30. #ifdef HAVE_INTTYPES_H
  31. #include <inttypes.h>
  32. #endif
  33. #include "simple-object-common.h"
  34. /* ELF structures and constants. */
  35. /* 32-bit ELF file header. */
  36. typedef struct {
  37. unsigned char e_ident[16]; /* ELF "magic number" */
  38. unsigned char e_type[2]; /* Identifies object file type */
  39. unsigned char e_machine[2]; /* Specifies required architecture */
  40. unsigned char e_version[4]; /* Identifies object file version */
  41. unsigned char e_entry[4]; /* Entry point virtual address */
  42. unsigned char e_phoff[4]; /* Program header table file offset */
  43. unsigned char e_shoff[4]; /* Section header table file offset */
  44. unsigned char e_flags[4]; /* Processor-specific flags */
  45. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  46. unsigned char e_phentsize[2]; /* Program header table entry size */
  47. unsigned char e_phnum[2]; /* Program header table entry count */
  48. unsigned char e_shentsize[2]; /* Section header table entry size */
  49. unsigned char e_shnum[2]; /* Section header table entry count */
  50. unsigned char e_shstrndx[2]; /* Section header string table index */
  51. } Elf32_External_Ehdr;
  52. /* 64-bit ELF file header. */
  53. typedef struct {
  54. unsigned char e_ident[16]; /* ELF "magic number" */
  55. unsigned char e_type[2]; /* Identifies object file type */
  56. unsigned char e_machine[2]; /* Specifies required architecture */
  57. unsigned char e_version[4]; /* Identifies object file version */
  58. unsigned char e_entry[8]; /* Entry point virtual address */
  59. unsigned char e_phoff[8]; /* Program header table file offset */
  60. unsigned char e_shoff[8]; /* Section header table file offset */
  61. unsigned char e_flags[4]; /* Processor-specific flags */
  62. unsigned char e_ehsize[2]; /* ELF header size in bytes */
  63. unsigned char e_phentsize[2]; /* Program header table entry size */
  64. unsigned char e_phnum[2]; /* Program header table entry count */
  65. unsigned char e_shentsize[2]; /* Section header table entry size */
  66. unsigned char e_shnum[2]; /* Section header table entry count */
  67. unsigned char e_shstrndx[2]; /* Section header string table index */
  68. } Elf64_External_Ehdr;
  69. /* Indexes and values in e_ident field of Ehdr. */
  70. #define EI_MAG0 0 /* File identification byte 0 index */
  71. #define ELFMAG0 0x7F /* Magic number byte 0 */
  72. #define EI_MAG1 1 /* File identification byte 1 index */
  73. #define ELFMAG1 'E' /* Magic number byte 1 */
  74. #define EI_MAG2 2 /* File identification byte 2 index */
  75. #define ELFMAG2 'L' /* Magic number byte 2 */
  76. #define EI_MAG3 3 /* File identification byte 3 index */
  77. #define ELFMAG3 'F' /* Magic number byte 3 */
  78. #define EI_CLASS 4 /* File class */
  79. #define ELFCLASSNONE 0 /* Invalid class */
  80. #define ELFCLASS32 1 /* 32-bit objects */
  81. #define ELFCLASS64 2 /* 64-bit objects */
  82. #define EI_DATA 5 /* Data encoding */
  83. #define ELFDATANONE 0 /* Invalid data encoding */
  84. #define ELFDATA2LSB 1 /* 2's complement, little endian */
  85. #define ELFDATA2MSB 2 /* 2's complement, big endian */
  86. #define EI_VERSION 6 /* File version */
  87. #define EV_CURRENT 1 /* Current version */
  88. #define EI_OSABI 7 /* Operating System/ABI indication */
  89. /* Values for e_type field of Ehdr. */
  90. #define ET_REL 1 /* Relocatable file */
  91. /* Values for e_machine field of Ehdr. */
  92. #define EM_SPARC 2 /* SUN SPARC */
  93. #define EM_SPARC32PLUS 18 /* Sun's "v8plus" */
  94. /* Special section index values. */
  95. #define SHN_LORESERVE 0xFF00 /* Begin range of reserved indices */
  96. #define SHN_XINDEX 0xFFFF /* Section index is held elsewhere */
  97. /* 32-bit ELF program header. */
  98. typedef struct {
  99. unsigned char p_type[4]; /* Identifies program segment type */
  100. unsigned char p_offset[4]; /* Segment file offset */
  101. unsigned char p_vaddr[4]; /* Segment virtual address */
  102. unsigned char p_paddr[4]; /* Segment physical address */
  103. unsigned char p_filesz[4]; /* Segment size in file */
  104. unsigned char p_memsz[4]; /* Segment size in memory */
  105. unsigned char p_flags[4]; /* Segment flags */
  106. unsigned char p_align[4]; /* Segment alignment, file & memory */
  107. } Elf32_External_Phdr;
  108. /* 64-bit ELF program header. */
  109. typedef struct {
  110. unsigned char p_type[4]; /* Identifies program segment type */
  111. unsigned char p_flags[4]; /* Segment flags */
  112. unsigned char p_offset[8]; /* Segment file offset */
  113. unsigned char p_vaddr[8]; /* Segment virtual address */
  114. unsigned char p_paddr[8]; /* Segment physical address */
  115. unsigned char p_filesz[8]; /* Segment size in file */
  116. unsigned char p_memsz[8]; /* Segment size in memory */
  117. unsigned char p_align[8]; /* Segment alignment, file & memory */
  118. } Elf64_External_Phdr;
  119. /* 32-bit ELF section header */
  120. typedef struct {
  121. unsigned char sh_name[4]; /* Section name, index in string tbl */
  122. unsigned char sh_type[4]; /* Type of section */
  123. unsigned char sh_flags[4]; /* Miscellaneous section attributes */
  124. unsigned char sh_addr[4]; /* Section virtual addr at execution */
  125. unsigned char sh_offset[4]; /* Section file offset */
  126. unsigned char sh_size[4]; /* Size of section in bytes */
  127. unsigned char sh_link[4]; /* Index of another section */
  128. unsigned char sh_info[4]; /* Additional section information */
  129. unsigned char sh_addralign[4]; /* Section alignment */
  130. unsigned char sh_entsize[4]; /* Entry size if section holds table */
  131. } Elf32_External_Shdr;
  132. /* 64-bit ELF section header. */
  133. typedef struct {
  134. unsigned char sh_name[4]; /* Section name, index in string tbl */
  135. unsigned char sh_type[4]; /* Type of section */
  136. unsigned char sh_flags[8]; /* Miscellaneous section attributes */
  137. unsigned char sh_addr[8]; /* Section virtual addr at execution */
  138. unsigned char sh_offset[8]; /* Section file offset */
  139. unsigned char sh_size[8]; /* Size of section in bytes */
  140. unsigned char sh_link[4]; /* Index of another section */
  141. unsigned char sh_info[4]; /* Additional section information */
  142. unsigned char sh_addralign[8]; /* Section alignment */
  143. unsigned char sh_entsize[8]; /* Entry size if section holds table */
  144. } Elf64_External_Shdr;
  145. /* Values for sh_type field. */
  146. #define SHT_PROGBITS 1 /* Program data */
  147. #define SHT_STRTAB 3 /* A string table */
  148. /* Functions to fetch and store different ELF types, depending on the
  149. endianness and size. */
  150. struct elf_type_functions
  151. {
  152. unsigned short (*fetch_Elf_Half) (const unsigned char *);
  153. unsigned int (*fetch_Elf_Word) (const unsigned char *);
  154. ulong_type (*fetch_Elf_Addr) (const unsigned char *);
  155. void (*set_Elf_Half) (unsigned char *, unsigned short);
  156. void (*set_Elf_Word) (unsigned char *, unsigned int);
  157. void (*set_Elf_Addr) (unsigned char *, ulong_type);
  158. };
  159. static const struct elf_type_functions elf_big_32_functions =
  160. {
  161. simple_object_fetch_big_16,
  162. simple_object_fetch_big_32,
  163. simple_object_fetch_big_32_ulong,
  164. simple_object_set_big_16,
  165. simple_object_set_big_32,
  166. simple_object_set_big_32_ulong
  167. };
  168. static const struct elf_type_functions elf_little_32_functions =
  169. {
  170. simple_object_fetch_little_16,
  171. simple_object_fetch_little_32,
  172. simple_object_fetch_little_32_ulong,
  173. simple_object_set_little_16,
  174. simple_object_set_little_32,
  175. simple_object_set_little_32_ulong
  176. };
  177. #ifdef UNSIGNED_64BIT_TYPE
  178. static const struct elf_type_functions elf_big_64_functions =
  179. {
  180. simple_object_fetch_big_16,
  181. simple_object_fetch_big_32,
  182. simple_object_fetch_big_64,
  183. simple_object_set_big_16,
  184. simple_object_set_big_32,
  185. simple_object_set_big_64
  186. };
  187. static const struct elf_type_functions elf_little_64_functions =
  188. {
  189. simple_object_fetch_little_16,
  190. simple_object_fetch_little_32,
  191. simple_object_fetch_little_64,
  192. simple_object_set_little_16,
  193. simple_object_set_little_32,
  194. simple_object_set_little_64
  195. };
  196. #endif
  197. /* Hideous macro to fetch the value of a field from an external ELF
  198. struct of some sort. TYPEFUNCS is the set of type functions.
  199. BUFFER points to the external data. STRUCTTYPE is the appropriate
  200. struct type. FIELD is a field within the struct. TYPE is the type
  201. of the field in the struct: Elf_Half, Elf_Word, or Elf_Addr. */
  202. #define ELF_FETCH_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE) \
  203. ((TYPEFUNCS)->fetch_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD)))
  204. /* Even more hideous macro to fetch the value of FIELD from BUFFER.
  205. SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
  206. elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
  207. the struct. TYPE is the type of the field in the struct: Elf_Half,
  208. Elf_Word, or Elf_Addr. */
  209. #define ELF_FETCH_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, \
  210. FIELD, TYPE) \
  211. ELF_FETCH_STRUCT_FIELD (TYPEFUNCS, \
  212. Elf ## SIZE ## _External_ ## STRUCTTYPE, \
  213. FIELD, BUFFER, TYPE)
  214. /* Like ELF_FETCH_SIZED_FIELD but taking an ELFCLASS value. */
  215. #define ELF_FETCH_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, \
  216. FIELD, TYPE) \
  217. ((CLASS) == ELFCLASS32 \
  218. ? ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
  219. TYPE) \
  220. : ELF_FETCH_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
  221. TYPE))
  222. /* Hideous macro to set the value of a field in an external ELF
  223. structure to VAL. TYPEFUNCS is the set of type functions. BUFFER
  224. points to the external data. STRUCTTYPE is the appropriate
  225. structure type. FIELD is a field within the struct. TYPE is the
  226. type of the field in the struct: Elf_Half, Elf_Word, or
  227. Elf_Addr. */
  228. #define ELF_SET_STRUCT_FIELD(TYPEFUNCS, STRUCTTYPE, FIELD, BUFFER, TYPE, VAL) \
  229. (TYPEFUNCS)->set_ ## TYPE ((BUFFER) + offsetof (STRUCTTYPE, FIELD), (VAL))
  230. /* Even more hideous macro to set the value of FIELD in BUFFER to VAL.
  231. SIZE is 32 or 64. STRUCTTYPE is the name of the struct from
  232. elf/external.h: Ehdr, Shdr, etc. FIELD is the name of a field in
  233. the struct. TYPE is the type of the field in the struct: Elf_Half,
  234. Elf_Word, or Elf_Addr. */
  235. #define ELF_SET_SIZED_FIELD(TYPEFUNCS, SIZE, STRUCTTYPE, BUFFER, FIELD, \
  236. TYPE, VAL) \
  237. ELF_SET_STRUCT_FIELD (TYPEFUNCS, \
  238. Elf ## SIZE ## _External_ ## STRUCTTYPE, \
  239. FIELD, BUFFER, TYPE, VAL)
  240. /* Like ELF_SET_SIZED_FIELD but taking an ELFCLASS value. */
  241. #define ELF_SET_FIELD(TYPEFUNCS, CLASS, STRUCTTYPE, BUFFER, FIELD, \
  242. TYPE, VAL) \
  243. ((CLASS) == ELFCLASS32 \
  244. ? ELF_SET_SIZED_FIELD (TYPEFUNCS, 32, STRUCTTYPE, BUFFER, FIELD, \
  245. TYPE, VAL) \
  246. : ELF_SET_SIZED_FIELD (TYPEFUNCS, 64, STRUCTTYPE, BUFFER, FIELD, \
  247. TYPE, VAL))
  248. /* Private data for an simple_object_read. */
  249. struct simple_object_elf_read
  250. {
  251. /* Type functions. */
  252. const struct elf_type_functions* type_functions;
  253. /* Elf data. */
  254. unsigned char ei_data;
  255. /* Elf class. */
  256. unsigned char ei_class;
  257. /* ELF OS ABI. */
  258. unsigned char ei_osabi;
  259. /* Elf machine number. */
  260. unsigned short machine;
  261. /* Processor specific flags. */
  262. unsigned int flags;
  263. /* File offset of section headers. */
  264. ulong_type shoff;
  265. /* Number of sections. */
  266. unsigned int shnum;
  267. /* Index of string table section header. */
  268. unsigned int shstrndx;
  269. };
  270. /* Private data for an simple_object_attributes. */
  271. struct simple_object_elf_attributes
  272. {
  273. /* Type functions. */
  274. const struct elf_type_functions* type_functions;
  275. /* Elf data. */
  276. unsigned char ei_data;
  277. /* Elf class. */
  278. unsigned char ei_class;
  279. /* ELF OS ABI. */
  280. unsigned char ei_osabi;
  281. /* Elf machine number. */
  282. unsigned short machine;
  283. /* Processor specific flags. */
  284. unsigned int flags;
  285. };
  286. /* See if we have an ELF file. */
  287. static void *
  288. simple_object_elf_match (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
  289. int descriptor, off_t offset,
  290. const char *segment_name ATTRIBUTE_UNUSED,
  291. const char **errmsg, int *err)
  292. {
  293. unsigned char ei_data;
  294. unsigned char ei_class;
  295. const struct elf_type_functions *type_functions;
  296. unsigned char ehdr[sizeof (Elf64_External_Ehdr)];
  297. struct simple_object_elf_read *eor;
  298. if (header[EI_MAG0] != ELFMAG0
  299. || header[EI_MAG1] != ELFMAG1
  300. || header[EI_MAG2] != ELFMAG2
  301. || header[EI_MAG3] != ELFMAG3
  302. || header[EI_VERSION] != EV_CURRENT)
  303. {
  304. *errmsg = NULL;
  305. *err = 0;
  306. return NULL;
  307. }
  308. ei_data = header[EI_DATA];
  309. if (ei_data != ELFDATA2LSB && ei_data != ELFDATA2MSB)
  310. {
  311. *errmsg = "unknown ELF endianness";
  312. *err = 0;
  313. return NULL;
  314. }
  315. ei_class = header[EI_CLASS];
  316. switch (ei_class)
  317. {
  318. case ELFCLASS32:
  319. type_functions = (ei_data == ELFDATA2LSB
  320. ? &elf_little_32_functions
  321. : &elf_big_32_functions);
  322. break;
  323. case ELFCLASS64:
  324. #ifndef UNSIGNED_64BIT_TYPE
  325. *errmsg = "64-bit ELF objects not supported";
  326. *err = 0;
  327. return NULL;
  328. #else
  329. type_functions = (ei_data == ELFDATA2LSB
  330. ? &elf_little_64_functions
  331. : &elf_big_64_functions);
  332. break;
  333. #endif
  334. default:
  335. *errmsg = "unrecognized ELF size";
  336. *err = 0;
  337. return NULL;
  338. }
  339. if (!simple_object_internal_read (descriptor, offset, ehdr, sizeof ehdr,
  340. errmsg, err))
  341. return NULL;
  342. eor = XNEW (struct simple_object_elf_read);
  343. eor->type_functions = type_functions;
  344. eor->ei_data = ei_data;
  345. eor->ei_class = ei_class;
  346. eor->ei_osabi = header[EI_OSABI];
  347. eor->machine = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  348. e_machine, Elf_Half);
  349. eor->flags = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  350. e_flags, Elf_Word);
  351. eor->shoff = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  352. e_shoff, Elf_Addr);
  353. eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  354. e_shnum, Elf_Half);
  355. eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Ehdr, ehdr,
  356. e_shstrndx, Elf_Half);
  357. if ((eor->shnum == 0 || eor->shstrndx == SHN_XINDEX)
  358. && eor->shoff != 0)
  359. {
  360. unsigned char shdr[sizeof (Elf64_External_Shdr)];
  361. /* Object file has more than 0xffff sections. */
  362. if (!simple_object_internal_read (descriptor, offset + eor->shoff, shdr,
  363. (ei_class == ELFCLASS32
  364. ? sizeof (Elf32_External_Shdr)
  365. : sizeof (Elf64_External_Shdr)),
  366. errmsg, err))
  367. {
  368. XDELETE (eor);
  369. return NULL;
  370. }
  371. if (eor->shnum == 0)
  372. eor->shnum = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  373. shdr, sh_size, Elf_Addr);
  374. if (eor->shstrndx == SHN_XINDEX)
  375. {
  376. eor->shstrndx = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  377. shdr, sh_link, Elf_Word);
  378. /* Versions of the GNU binutils between 2.12 and 2.18 did
  379. not handle objects with more than SHN_LORESERVE sections
  380. correctly. All large section indexes were offset by
  381. 0x100. There is more information at
  382. http://sourceware.org/bugzilla/show_bug.cgi?id-5900 .
  383. Fortunately these object files are easy to detect, as the
  384. GNU binutils always put the section header string table
  385. near the end of the list of sections. Thus if the
  386. section header string table index is larger than the
  387. number of sections, then we know we have to subtract
  388. 0x100 to get the real section index. */
  389. if (eor->shstrndx >= eor->shnum
  390. && eor->shstrndx >= SHN_LORESERVE + 0x100)
  391. eor->shstrndx -= 0x100;
  392. }
  393. }
  394. if (eor->shstrndx >= eor->shnum)
  395. {
  396. *errmsg = "invalid ELF shstrndx >= shnum";
  397. *err = 0;
  398. XDELETE (eor);
  399. return NULL;
  400. }
  401. return (void *) eor;
  402. }
  403. /* Find all sections in an ELF file. */
  404. static const char *
  405. simple_object_elf_find_sections (simple_object_read *sobj,
  406. int (*pfn) (void *, const char *,
  407. off_t offset, off_t length),
  408. void *data,
  409. int *err)
  410. {
  411. struct simple_object_elf_read *eor =
  412. (struct simple_object_elf_read *) sobj->data;
  413. const struct elf_type_functions *type_functions = eor->type_functions;
  414. unsigned char ei_class = eor->ei_class;
  415. size_t shdr_size;
  416. unsigned int shnum;
  417. unsigned char *shdrs;
  418. const char *errmsg;
  419. unsigned char *shstrhdr;
  420. size_t name_size;
  421. off_t shstroff;
  422. unsigned char *names;
  423. unsigned int i;
  424. shdr_size = (ei_class == ELFCLASS32
  425. ? sizeof (Elf32_External_Shdr)
  426. : sizeof (Elf64_External_Shdr));
  427. /* Read the section headers. We skip section 0, which is not a
  428. useful section. */
  429. shnum = eor->shnum;
  430. shdrs = XNEWVEC (unsigned char, shdr_size * (shnum - 1));
  431. if (!simple_object_internal_read (sobj->descriptor,
  432. sobj->offset + eor->shoff + shdr_size,
  433. shdrs,
  434. shdr_size * (shnum - 1),
  435. &errmsg, err))
  436. {
  437. XDELETEVEC (shdrs);
  438. return errmsg;
  439. }
  440. /* Read the section names. */
  441. shstrhdr = shdrs + (eor->shstrndx - 1) * shdr_size;
  442. name_size = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  443. shstrhdr, sh_size, Elf_Addr);
  444. shstroff = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  445. shstrhdr, sh_offset, Elf_Addr);
  446. names = XNEWVEC (unsigned char, name_size);
  447. if (!simple_object_internal_read (sobj->descriptor,
  448. sobj->offset + shstroff,
  449. names, name_size, &errmsg, err))
  450. {
  451. XDELETEVEC (names);
  452. XDELETEVEC (shdrs);
  453. return errmsg;
  454. }
  455. for (i = 1; i < shnum; ++i)
  456. {
  457. unsigned char *shdr;
  458. unsigned int sh_name;
  459. const char *name;
  460. off_t offset;
  461. off_t length;
  462. shdr = shdrs + (i - 1) * shdr_size;
  463. sh_name = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  464. shdr, sh_name, Elf_Word);
  465. if (sh_name >= name_size)
  466. {
  467. *err = 0;
  468. XDELETEVEC (names);
  469. XDELETEVEC (shdrs);
  470. return "ELF section name out of range";
  471. }
  472. name = (const char *) names + sh_name;
  473. offset = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  474. shdr, sh_offset, Elf_Addr);
  475. length = ELF_FETCH_FIELD (type_functions, ei_class, Shdr,
  476. shdr, sh_size, Elf_Addr);
  477. if (!(*pfn) (data, name, offset, length))
  478. break;
  479. }
  480. XDELETEVEC (names);
  481. XDELETEVEC (shdrs);
  482. return NULL;
  483. }
  484. /* Fetch the attributes for an simple_object_read. */
  485. static void *
  486. simple_object_elf_fetch_attributes (simple_object_read *sobj,
  487. const char **errmsg ATTRIBUTE_UNUSED,
  488. int *err ATTRIBUTE_UNUSED)
  489. {
  490. struct simple_object_elf_read *eor =
  491. (struct simple_object_elf_read *) sobj->data;
  492. struct simple_object_elf_attributes *ret;
  493. ret = XNEW (struct simple_object_elf_attributes);
  494. ret->type_functions = eor->type_functions;
  495. ret->ei_data = eor->ei_data;
  496. ret->ei_class = eor->ei_class;
  497. ret->ei_osabi = eor->ei_osabi;
  498. ret->machine = eor->machine;
  499. ret->flags = eor->flags;
  500. return ret;
  501. }
  502. /* Release the privata data for an simple_object_read. */
  503. static void
  504. simple_object_elf_release_read (void *data)
  505. {
  506. XDELETE (data);
  507. }
  508. /* Compare two attributes structures. */
  509. static const char *
  510. simple_object_elf_attributes_merge (void *todata, void *fromdata, int *err)
  511. {
  512. struct simple_object_elf_attributes *to =
  513. (struct simple_object_elf_attributes *) todata;
  514. struct simple_object_elf_attributes *from =
  515. (struct simple_object_elf_attributes *) fromdata;
  516. if (to->ei_data != from->ei_data || to->ei_class != from->ei_class)
  517. {
  518. *err = 0;
  519. return "ELF object format mismatch";
  520. }
  521. if (to->machine != from->machine)
  522. {
  523. int ok;
  524. /* EM_SPARC and EM_SPARC32PLUS are compatible and force an
  525. output of EM_SPARC32PLUS. */
  526. ok = 0;
  527. switch (to->machine)
  528. {
  529. case EM_SPARC:
  530. if (from->machine == EM_SPARC32PLUS)
  531. {
  532. to->machine = from->machine;
  533. ok = 1;
  534. }
  535. break;
  536. case EM_SPARC32PLUS:
  537. if (from->machine == EM_SPARC)
  538. ok = 1;
  539. break;
  540. default:
  541. break;
  542. }
  543. if (!ok)
  544. {
  545. *err = 0;
  546. return "ELF machine number mismatch";
  547. }
  548. }
  549. return NULL;
  550. }
  551. /* Release the private data for an attributes structure. */
  552. static void
  553. simple_object_elf_release_attributes (void *data)
  554. {
  555. XDELETE (data);
  556. }
  557. /* Prepare to write out a file. */
  558. static void *
  559. simple_object_elf_start_write (void *attributes_data,
  560. const char **errmsg ATTRIBUTE_UNUSED,
  561. int *err ATTRIBUTE_UNUSED)
  562. {
  563. struct simple_object_elf_attributes *attrs =
  564. (struct simple_object_elf_attributes *) attributes_data;
  565. struct simple_object_elf_attributes *ret;
  566. /* We're just going to record the attributes, but we need to make a
  567. copy because the user may delete them. */
  568. ret = XNEW (struct simple_object_elf_attributes);
  569. *ret = *attrs;
  570. return ret;
  571. }
  572. /* Write out an ELF ehdr. */
  573. static int
  574. simple_object_elf_write_ehdr (simple_object_write *sobj, int descriptor,
  575. const char **errmsg, int *err)
  576. {
  577. struct simple_object_elf_attributes *attrs =
  578. (struct simple_object_elf_attributes *) sobj->data;
  579. const struct elf_type_functions* fns;
  580. unsigned char cl;
  581. size_t ehdr_size;
  582. unsigned char buf[sizeof (Elf64_External_Ehdr)];
  583. simple_object_write_section *section;
  584. unsigned int shnum;
  585. unsigned int shstrndx;
  586. fns = attrs->type_functions;
  587. cl = attrs->ei_class;
  588. shnum = 0;
  589. for (section = sobj->sections; section != NULL; section = section->next)
  590. ++shnum;
  591. if (shnum > 0)
  592. {
  593. /* Add a section header for the dummy section and one for
  594. .shstrtab. */
  595. shnum += 2;
  596. }
  597. ehdr_size = (cl == ELFCLASS32
  598. ? sizeof (Elf32_External_Ehdr)
  599. : sizeof (Elf64_External_Ehdr));
  600. memset (buf, 0, sizeof (Elf64_External_Ehdr));
  601. buf[EI_MAG0] = ELFMAG0;
  602. buf[EI_MAG1] = ELFMAG1;
  603. buf[EI_MAG2] = ELFMAG2;
  604. buf[EI_MAG3] = ELFMAG3;
  605. buf[EI_CLASS] = cl;
  606. buf[EI_DATA] = attrs->ei_data;
  607. buf[EI_VERSION] = EV_CURRENT;
  608. buf[EI_OSABI] = attrs->ei_osabi;
  609. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_type, Elf_Half, ET_REL);
  610. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_machine, Elf_Half, attrs->machine);
  611. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_version, Elf_Word, EV_CURRENT);
  612. /* e_entry left as zero. */
  613. /* e_phoff left as zero. */
  614. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shoff, Elf_Addr, ehdr_size);
  615. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_flags, Elf_Word, attrs->flags);
  616. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_ehsize, Elf_Half, ehdr_size);
  617. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_phentsize, Elf_Half,
  618. (cl == ELFCLASS32
  619. ? sizeof (Elf32_External_Phdr)
  620. : sizeof (Elf64_External_Phdr)));
  621. /* e_phnum left as zero. */
  622. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shentsize, Elf_Half,
  623. (cl == ELFCLASS32
  624. ? sizeof (Elf32_External_Shdr)
  625. : sizeof (Elf64_External_Shdr)));
  626. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shnum, Elf_Half,
  627. shnum >= SHN_LORESERVE ? 0 : shnum);
  628. if (shnum == 0)
  629. shstrndx = 0;
  630. else
  631. {
  632. shstrndx = shnum - 1;
  633. if (shstrndx >= SHN_LORESERVE)
  634. shstrndx = SHN_XINDEX;
  635. }
  636. ELF_SET_FIELD (fns, cl, Ehdr, buf, e_shstrndx, Elf_Half, shstrndx);
  637. return simple_object_internal_write (descriptor, 0, buf, ehdr_size,
  638. errmsg, err);
  639. }
  640. /* Write out an ELF shdr. */
  641. static int
  642. simple_object_elf_write_shdr (simple_object_write *sobj, int descriptor,
  643. off_t offset, unsigned int sh_name,
  644. unsigned int sh_type, unsigned int sh_flags,
  645. unsigned int sh_offset, unsigned int sh_size,
  646. unsigned int sh_link, unsigned int sh_addralign,
  647. const char **errmsg, int *err)
  648. {
  649. struct simple_object_elf_attributes *attrs =
  650. (struct simple_object_elf_attributes *) sobj->data;
  651. const struct elf_type_functions* fns;
  652. unsigned char cl;
  653. size_t shdr_size;
  654. unsigned char buf[sizeof (Elf64_External_Shdr)];
  655. fns = attrs->type_functions;
  656. cl = attrs->ei_class;
  657. shdr_size = (cl == ELFCLASS32
  658. ? sizeof (Elf32_External_Shdr)
  659. : sizeof (Elf64_External_Shdr));
  660. memset (buf, 0, sizeof (Elf64_External_Shdr));
  661. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_name, Elf_Word, sh_name);
  662. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_type, Elf_Word, sh_type);
  663. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_flags, Elf_Addr, sh_flags);
  664. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_offset, Elf_Addr, sh_offset);
  665. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_size, Elf_Addr, sh_size);
  666. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_link, Elf_Word, sh_link);
  667. /* sh_info left as zero. */
  668. ELF_SET_FIELD (fns, cl, Shdr, buf, sh_addralign, Elf_Addr, sh_addralign);
  669. /* sh_entsize left as zero. */
  670. return simple_object_internal_write (descriptor, offset, buf, shdr_size,
  671. errmsg, err);
  672. }
  673. /* Write out a complete ELF file.
  674. Ehdr
  675. initial dummy Shdr
  676. user-created Shdrs
  677. .shstrtab Shdr
  678. user-created section data
  679. .shstrtab data */
  680. static const char *
  681. simple_object_elf_write_to_file (simple_object_write *sobj, int descriptor,
  682. int *err)
  683. {
  684. struct simple_object_elf_attributes *attrs =
  685. (struct simple_object_elf_attributes *) sobj->data;
  686. unsigned char cl;
  687. size_t ehdr_size;
  688. size_t shdr_size;
  689. const char *errmsg;
  690. simple_object_write_section *section;
  691. unsigned int shnum;
  692. size_t shdr_offset;
  693. size_t sh_offset;
  694. unsigned int first_sh_size;
  695. unsigned int first_sh_link;
  696. size_t sh_name;
  697. unsigned char zero;
  698. if (!simple_object_elf_write_ehdr (sobj, descriptor, &errmsg, err))
  699. return errmsg;
  700. cl = attrs->ei_class;
  701. if (cl == ELFCLASS32)
  702. {
  703. ehdr_size = sizeof (Elf32_External_Ehdr);
  704. shdr_size = sizeof (Elf32_External_Shdr);
  705. }
  706. else
  707. {
  708. ehdr_size = sizeof (Elf64_External_Ehdr);
  709. shdr_size = sizeof (Elf64_External_Shdr);
  710. }
  711. shnum = 0;
  712. for (section = sobj->sections; section != NULL; section = section->next)
  713. ++shnum;
  714. if (shnum == 0)
  715. return NULL;
  716. /* Add initial dummy Shdr and .shstrtab. */
  717. shnum += 2;
  718. shdr_offset = ehdr_size;
  719. sh_offset = shdr_offset + shnum * shdr_size;
  720. if (shnum < SHN_LORESERVE)
  721. first_sh_size = 0;
  722. else
  723. first_sh_size = shnum;
  724. if (shnum - 1 < SHN_LORESERVE)
  725. first_sh_link = 0;
  726. else
  727. first_sh_link = shnum - 1;
  728. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  729. 0, 0, 0, 0, first_sh_size, first_sh_link,
  730. 0, &errmsg, err))
  731. return errmsg;
  732. shdr_offset += shdr_size;
  733. sh_name = 1;
  734. for (section = sobj->sections; section != NULL; section = section->next)
  735. {
  736. size_t mask;
  737. size_t new_sh_offset;
  738. size_t sh_size;
  739. struct simple_object_write_section_buffer *buffer;
  740. mask = (1U << section->align) - 1;
  741. new_sh_offset = sh_offset + mask;
  742. new_sh_offset &= ~ mask;
  743. while (new_sh_offset > sh_offset)
  744. {
  745. unsigned char zeroes[16];
  746. size_t write;
  747. memset (zeroes, 0, sizeof zeroes);
  748. write = new_sh_offset - sh_offset;
  749. if (write > sizeof zeroes)
  750. write = sizeof zeroes;
  751. if (!simple_object_internal_write (descriptor, sh_offset, zeroes,
  752. write, &errmsg, err))
  753. return errmsg;
  754. sh_offset += write;
  755. }
  756. sh_size = 0;
  757. for (buffer = section->buffers; buffer != NULL; buffer = buffer->next)
  758. {
  759. if (!simple_object_internal_write (descriptor, sh_offset + sh_size,
  760. ((const unsigned char *)
  761. buffer->buffer),
  762. buffer->size, &errmsg, err))
  763. return errmsg;
  764. sh_size += buffer->size;
  765. }
  766. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  767. sh_name, SHT_PROGBITS, 0, sh_offset,
  768. sh_size, 0, 1U << section->align,
  769. &errmsg, err))
  770. return errmsg;
  771. shdr_offset += shdr_size;
  772. sh_name += strlen (section->name) + 1;
  773. sh_offset += sh_size;
  774. }
  775. if (!simple_object_elf_write_shdr (sobj, descriptor, shdr_offset,
  776. sh_name, SHT_STRTAB, 0, sh_offset,
  777. sh_name + strlen (".shstrtab") + 1, 0,
  778. 1, &errmsg, err))
  779. return errmsg;
  780. /* .shstrtab has a leading zero byte. */
  781. zero = 0;
  782. if (!simple_object_internal_write (descriptor, sh_offset, &zero, 1,
  783. &errmsg, err))
  784. return errmsg;
  785. ++sh_offset;
  786. for (section = sobj->sections; section != NULL; section = section->next)
  787. {
  788. size_t len;
  789. len = strlen (section->name) + 1;
  790. if (!simple_object_internal_write (descriptor, sh_offset,
  791. (const unsigned char *) section->name,
  792. len, &errmsg, err))
  793. return errmsg;
  794. sh_offset += len;
  795. }
  796. if (!simple_object_internal_write (descriptor, sh_offset,
  797. (const unsigned char *) ".shstrtab",
  798. strlen (".shstrtab") + 1, &errmsg, err))
  799. return errmsg;
  800. return NULL;
  801. }
  802. /* Release the private data for an simple_object_write structure. */
  803. static void
  804. simple_object_elf_release_write (void *data)
  805. {
  806. XDELETE (data);
  807. }
  808. /* The ELF functions. */
  809. const struct simple_object_functions simple_object_elf_functions =
  810. {
  811. simple_object_elf_match,
  812. simple_object_elf_find_sections,
  813. simple_object_elf_fetch_attributes,
  814. simple_object_elf_release_read,
  815. simple_object_elf_attributes_merge,
  816. simple_object_elf_release_attributes,
  817. simple_object_elf_start_write,
  818. simple_object_elf_write_to_file,
  819. simple_object_elf_release_write
  820. };