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