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