1*fae548d3Szrj /* simple-object-common.h -- common structs for object file manipulation.
2*fae548d3Szrj Copyright (C) 2010-2020 Free Software Foundation, Inc.
3*fae548d3Szrj
4*fae548d3Szrj This file is part of the libiberty library.
5*fae548d3Szrj Libiberty is free software; you can redistribute it and/or
6*fae548d3Szrj modify it under the terms of the GNU Library General Public
7*fae548d3Szrj License as published by the Free Software Foundation; either
8*fae548d3Szrj version 2 of the License, or (at your option) any later version.
9*fae548d3Szrj
10*fae548d3Szrj Libiberty is distributed in the hope that it will be useful,
11*fae548d3Szrj but WITHOUT ANY WARRANTY; without even the implied warranty of
12*fae548d3Szrj MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13*fae548d3Szrj Library General Public License for more details.
14*fae548d3Szrj
15*fae548d3Szrj You should have received a copy of the GNU Library General Public
16*fae548d3Szrj License along with libiberty; see the file COPYING.LIB. If not,
17*fae548d3Szrj write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
18*fae548d3Szrj Boston, MA 02110-1301, USA. */
19*fae548d3Szrj
20*fae548d3Szrj /* Forward reference. */
21*fae548d3Szrj struct simple_object_functions;
22*fae548d3Szrj
23*fae548d3Szrj /* An object file opened for reading. */
24*fae548d3Szrj
25*fae548d3Szrj struct simple_object_read_struct
26*fae548d3Szrj {
27*fae548d3Szrj /* The file descriptor. */
28*fae548d3Szrj int descriptor;
29*fae548d3Szrj /* The offset within the file. */
30*fae548d3Szrj off_t offset;
31*fae548d3Szrj /* The functions which do the actual work. */
32*fae548d3Szrj const struct simple_object_functions *functions;
33*fae548d3Szrj /* Private data for the object file format. */
34*fae548d3Szrj void *data;
35*fae548d3Szrj };
36*fae548d3Szrj
37*fae548d3Szrj /* Object file attributes. */
38*fae548d3Szrj
39*fae548d3Szrj struct simple_object_attributes_struct
40*fae548d3Szrj {
41*fae548d3Szrj /* The functions which do the actual work. */
42*fae548d3Szrj const struct simple_object_functions *functions;
43*fae548d3Szrj /* Private data for the object file format. */
44*fae548d3Szrj void *data;
45*fae548d3Szrj };
46*fae548d3Szrj
47*fae548d3Szrj /* An object file being created. */
48*fae548d3Szrj
49*fae548d3Szrj struct simple_object_write_struct
50*fae548d3Szrj {
51*fae548d3Szrj /* The functions which do the actual work. */
52*fae548d3Szrj const struct simple_object_functions *functions;
53*fae548d3Szrj /* The segment_name argument from the user. */
54*fae548d3Szrj char *segment_name;
55*fae548d3Szrj /* The start of the list of sections. */
56*fae548d3Szrj simple_object_write_section *sections;
57*fae548d3Szrj /* The last entry in the list of sections. */
58*fae548d3Szrj simple_object_write_section *last_section;
59*fae548d3Szrj /* Private data for the object file format. */
60*fae548d3Szrj void *data;
61*fae548d3Szrj };
62*fae548d3Szrj
63*fae548d3Szrj /* A section in an object file being created. */
64*fae548d3Szrj
65*fae548d3Szrj struct simple_object_write_section_struct
66*fae548d3Szrj {
67*fae548d3Szrj /* Next in the list of sections attached to an
68*fae548d3Szrj simple_object_write. */
69*fae548d3Szrj simple_object_write_section *next;
70*fae548d3Szrj /* The name of this section. */
71*fae548d3Szrj char *name;
72*fae548d3Szrj /* The required alignment. */
73*fae548d3Szrj unsigned int align;
74*fae548d3Szrj /* The first data attached to this section. */
75*fae548d3Szrj struct simple_object_write_section_buffer *buffers;
76*fae548d3Szrj /* The last data attached to this section. */
77*fae548d3Szrj struct simple_object_write_section_buffer *last_buffer;
78*fae548d3Szrj };
79*fae548d3Szrj
80*fae548d3Szrj /* Data attached to a section. */
81*fae548d3Szrj
82*fae548d3Szrj struct simple_object_write_section_buffer
83*fae548d3Szrj {
84*fae548d3Szrj /* The next data for this section. */
85*fae548d3Szrj struct simple_object_write_section_buffer *next;
86*fae548d3Szrj /* The size of the buffer. */
87*fae548d3Szrj size_t size;
88*fae548d3Szrj /* The actual bytes. */
89*fae548d3Szrj const void *buffer;
90*fae548d3Szrj /* A buffer to free, or NULL. */
91*fae548d3Szrj void *free_buffer;
92*fae548d3Szrj };
93*fae548d3Szrj
94*fae548d3Szrj /* The number of bytes we read from the start of the file to pass to
95*fae548d3Szrj the match function. */
96*fae548d3Szrj #define SIMPLE_OBJECT_MATCH_HEADER_LEN (16)
97*fae548d3Szrj
98*fae548d3Szrj /* Format-specific object file functions. */
99*fae548d3Szrj
100*fae548d3Szrj struct simple_object_functions
101*fae548d3Szrj {
102*fae548d3Szrj /* If this file matches these functions, return a new value for the
103*fae548d3Szrj private data for an simple_object_read. HEADER is the first 16
104*fae548d3Szrj bytes of the file. DESCRIPTOR, OFFSET, SEGMENT_NAME, ERRMSG, and
105*fae548d3Szrj ERR are as for simple_object_open_read. If this file does not
106*fae548d3Szrj match, this function should return NULL with *ERRMSG set to
107*fae548d3Szrj NULL. */
108*fae548d3Szrj void *(*match) (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
109*fae548d3Szrj int descriptor, off_t offset, const char *segment_name,
110*fae548d3Szrj const char **errmsg, int *err);
111*fae548d3Szrj
112*fae548d3Szrj /* Implement simple_object_find_sections. */
113*fae548d3Szrj const char *(*find_sections) (simple_object_read *,
114*fae548d3Szrj int (*pfn) (void *, const char *,
115*fae548d3Szrj off_t offset, off_t length),
116*fae548d3Szrj void *data,
117*fae548d3Szrj int *err);
118*fae548d3Szrj
119*fae548d3Szrj /* Return the private data for the attributes for SOBJ. */
120*fae548d3Szrj void *(*fetch_attributes) (simple_object_read *sobj, const char **errmsg,
121*fae548d3Szrj int *err);
122*fae548d3Szrj
123*fae548d3Szrj /* Release the private data for an simple_object_read. */
124*fae548d3Szrj void (*release_read) (void *);
125*fae548d3Szrj
126*fae548d3Szrj /* Merge the private data for the attributes of two files. If they
127*fae548d3Szrj could be linked together, return NULL. Otherwise return an error
128*fae548d3Szrj message. */
129*fae548d3Szrj const char *(*attributes_merge) (void *, void *, int *err);
130*fae548d3Szrj
131*fae548d3Szrj /* Release the private data for an simple_object_attributes. */
132*fae548d3Szrj void (*release_attributes) (void *);
133*fae548d3Szrj
134*fae548d3Szrj /* Start creating an object file. */
135*fae548d3Szrj void *(*start_write) (void *attributes_data, const char **errmsg,
136*fae548d3Szrj int *err);
137*fae548d3Szrj
138*fae548d3Szrj /* Write the complete object file. */
139*fae548d3Szrj const char *(*write_to_file) (simple_object_write *sobj, int descriptor,
140*fae548d3Szrj int *err);
141*fae548d3Szrj
142*fae548d3Szrj /* Release the private data for an simple_object_write. */
143*fae548d3Szrj void (*release_write) (void *);
144*fae548d3Szrj
145*fae548d3Szrj /* Copy LTO debug sections. */
146*fae548d3Szrj const char *(*copy_lto_debug_sections) (simple_object_read *sobj,
147*fae548d3Szrj simple_object_write *dobj,
148*fae548d3Szrj char *(*pfn) (const char *),
149*fae548d3Szrj int *err);
150*fae548d3Szrj };
151*fae548d3Szrj
152*fae548d3Szrj /* The known object file formats. */
153*fae548d3Szrj
154*fae548d3Szrj extern const struct simple_object_functions simple_object_coff_functions;
155*fae548d3Szrj extern const struct simple_object_functions simple_object_elf_functions;
156*fae548d3Szrj extern const struct simple_object_functions simple_object_mach_o_functions;
157*fae548d3Szrj extern const struct simple_object_functions simple_object_xcoff_functions;
158*fae548d3Szrj
159*fae548d3Szrj /* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
160*fae548d3Szrj Return non-zero on success. On failure return 0 and set *ERRMSG
161*fae548d3Szrj and *ERR. */
162*fae548d3Szrj
163*fae548d3Szrj extern int
164*fae548d3Szrj simple_object_internal_read (int descriptor, off_t offset,
165*fae548d3Szrj unsigned char *buffer, size_t size,
166*fae548d3Szrj const char **errmsg, int *err);
167*fae548d3Szrj
168*fae548d3Szrj /* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
169*fae548d3Szrj Return non-zero on success. On failure return 0 and set *ERRMSG
170*fae548d3Szrj and *ERR. */
171*fae548d3Szrj
172*fae548d3Szrj extern int
173*fae548d3Szrj simple_object_internal_write (int descriptor, off_t offset,
174*fae548d3Szrj const unsigned char *buffer, size_t size,
175*fae548d3Szrj const char **errmsg, int *err);
176*fae548d3Szrj
177*fae548d3Szrj /* Define ulong_type as an unsigned 64-bit type if available.
178*fae548d3Szrj Otherwise just make it unsigned long. */
179*fae548d3Szrj
180*fae548d3Szrj #ifdef UNSIGNED_64BIT_TYPE
181*fae548d3Szrj __extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
182*fae548d3Szrj #else
183*fae548d3Szrj typedef unsigned long ulong_type;
184*fae548d3Szrj #endif
185*fae548d3Szrj
186*fae548d3Szrj /* Fetch a big-endian 16-bit value. */
187*fae548d3Szrj
188*fae548d3Szrj static inline unsigned short
simple_object_fetch_big_16(const unsigned char * buf)189*fae548d3Szrj simple_object_fetch_big_16 (const unsigned char *buf)
190*fae548d3Szrj {
191*fae548d3Szrj return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
192*fae548d3Szrj }
193*fae548d3Szrj
194*fae548d3Szrj /* Fetch a little-endian 16-bit value. */
195*fae548d3Szrj
196*fae548d3Szrj static inline unsigned short
simple_object_fetch_little_16(const unsigned char * buf)197*fae548d3Szrj simple_object_fetch_little_16 (const unsigned char *buf)
198*fae548d3Szrj {
199*fae548d3Szrj return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
200*fae548d3Szrj }
201*fae548d3Szrj
202*fae548d3Szrj /* Fetch a big-endian 32-bit value. */
203*fae548d3Szrj
204*fae548d3Szrj static inline unsigned int
simple_object_fetch_big_32(const unsigned char * buf)205*fae548d3Szrj simple_object_fetch_big_32 (const unsigned char *buf)
206*fae548d3Szrj {
207*fae548d3Szrj return (((unsigned int) buf[0] << 24)
208*fae548d3Szrj | ((unsigned int) buf[1] << 16)
209*fae548d3Szrj | ((unsigned int) buf[2] << 8)
210*fae548d3Szrj | (unsigned int) buf[3]);
211*fae548d3Szrj }
212*fae548d3Szrj
213*fae548d3Szrj /* Fetch a little-endian 32-bit value. */
214*fae548d3Szrj
215*fae548d3Szrj static inline unsigned int
simple_object_fetch_little_32(const unsigned char * buf)216*fae548d3Szrj simple_object_fetch_little_32 (const unsigned char *buf)
217*fae548d3Szrj {
218*fae548d3Szrj return (((unsigned int) buf[3] << 24)
219*fae548d3Szrj | ((unsigned int) buf[2] << 16)
220*fae548d3Szrj | ((unsigned int) buf[1] << 8)
221*fae548d3Szrj | (unsigned int) buf[0]);
222*fae548d3Szrj }
223*fae548d3Szrj
224*fae548d3Szrj /* Fetch a big-endian 32-bit value as a ulong_type. */
225*fae548d3Szrj
226*fae548d3Szrj static inline ulong_type
simple_object_fetch_big_32_ulong(const unsigned char * buf)227*fae548d3Szrj simple_object_fetch_big_32_ulong (const unsigned char *buf)
228*fae548d3Szrj {
229*fae548d3Szrj return (ulong_type) simple_object_fetch_big_32 (buf);
230*fae548d3Szrj }
231*fae548d3Szrj
232*fae548d3Szrj /* Fetch a little-endian 32-bit value as a ulong_type. */
233*fae548d3Szrj
234*fae548d3Szrj static inline ulong_type
simple_object_fetch_little_32_ulong(const unsigned char * buf)235*fae548d3Szrj simple_object_fetch_little_32_ulong (const unsigned char *buf)
236*fae548d3Szrj {
237*fae548d3Szrj return (ulong_type) simple_object_fetch_little_32 (buf);
238*fae548d3Szrj }
239*fae548d3Szrj
240*fae548d3Szrj #ifdef UNSIGNED_64BIT_TYPE
241*fae548d3Szrj
242*fae548d3Szrj /* Fetch a big-endian 64-bit value. */
243*fae548d3Szrj
244*fae548d3Szrj static inline ulong_type
simple_object_fetch_big_64(const unsigned char * buf)245*fae548d3Szrj simple_object_fetch_big_64 (const unsigned char *buf)
246*fae548d3Szrj {
247*fae548d3Szrj return (((ulong_type) buf[0] << 56)
248*fae548d3Szrj | ((ulong_type) buf[1] << 48)
249*fae548d3Szrj | ((ulong_type) buf[2] << 40)
250*fae548d3Szrj | ((ulong_type) buf[3] << 32)
251*fae548d3Szrj | ((ulong_type) buf[4] << 24)
252*fae548d3Szrj | ((ulong_type) buf[5] << 16)
253*fae548d3Szrj | ((ulong_type) buf[6] << 8)
254*fae548d3Szrj | (ulong_type) buf[7]);
255*fae548d3Szrj }
256*fae548d3Szrj
257*fae548d3Szrj /* Fetch a little-endian 64-bit value. */
258*fae548d3Szrj
259*fae548d3Szrj static inline ulong_type
simple_object_fetch_little_64(const unsigned char * buf)260*fae548d3Szrj simple_object_fetch_little_64 (const unsigned char *buf)
261*fae548d3Szrj {
262*fae548d3Szrj return (((ulong_type) buf[7] << 56)
263*fae548d3Szrj | ((ulong_type) buf[6] << 48)
264*fae548d3Szrj | ((ulong_type) buf[5] << 40)
265*fae548d3Szrj | ((ulong_type) buf[4] << 32)
266*fae548d3Szrj | ((ulong_type) buf[3] << 24)
267*fae548d3Szrj | ((ulong_type) buf[2] << 16)
268*fae548d3Szrj | ((ulong_type) buf[1] << 8)
269*fae548d3Szrj | (ulong_type) buf[0]);
270*fae548d3Szrj }
271*fae548d3Szrj
272*fae548d3Szrj #endif
273*fae548d3Szrj
274*fae548d3Szrj /* Store a big-endian 16-bit value. */
275*fae548d3Szrj
276*fae548d3Szrj static inline void
simple_object_set_big_16(unsigned char * buf,unsigned short val)277*fae548d3Szrj simple_object_set_big_16 (unsigned char *buf, unsigned short val)
278*fae548d3Szrj {
279*fae548d3Szrj buf[0] = (val >> 8) & 0xff;
280*fae548d3Szrj buf[1] = val & 0xff;
281*fae548d3Szrj }
282*fae548d3Szrj
283*fae548d3Szrj /* Store a little-endian 16-bit value. */
284*fae548d3Szrj
285*fae548d3Szrj static inline void
simple_object_set_little_16(unsigned char * buf,unsigned short val)286*fae548d3Szrj simple_object_set_little_16 (unsigned char *buf, unsigned short val)
287*fae548d3Szrj {
288*fae548d3Szrj buf[1] = (val >> 8) & 0xff;
289*fae548d3Szrj buf[0] = val & 0xff;
290*fae548d3Szrj }
291*fae548d3Szrj
292*fae548d3Szrj /* Store a big-endian 32-bit value. */
293*fae548d3Szrj
294*fae548d3Szrj static inline void
simple_object_set_big_32(unsigned char * buf,unsigned int val)295*fae548d3Szrj simple_object_set_big_32 (unsigned char *buf, unsigned int val)
296*fae548d3Szrj {
297*fae548d3Szrj buf[0] = (val >> 24) & 0xff;
298*fae548d3Szrj buf[1] = (val >> 16) & 0xff;
299*fae548d3Szrj buf[2] = (val >> 8) & 0xff;
300*fae548d3Szrj buf[3] = val & 0xff;
301*fae548d3Szrj }
302*fae548d3Szrj
303*fae548d3Szrj /* Store a little-endian 32-bit value. */
304*fae548d3Szrj
305*fae548d3Szrj static inline void
simple_object_set_little_32(unsigned char * buf,unsigned int val)306*fae548d3Szrj simple_object_set_little_32 (unsigned char *buf, unsigned int val)
307*fae548d3Szrj {
308*fae548d3Szrj buf[3] = (val >> 24) & 0xff;
309*fae548d3Szrj buf[2] = (val >> 16) & 0xff;
310*fae548d3Szrj buf[1] = (val >> 8) & 0xff;
311*fae548d3Szrj buf[0] = val & 0xff;
312*fae548d3Szrj }
313*fae548d3Szrj
314*fae548d3Szrj /* Store a big-endian 32-bit value coming in as a ulong_type. */
315*fae548d3Szrj
316*fae548d3Szrj static inline void
simple_object_set_big_32_ulong(unsigned char * buf,ulong_type val)317*fae548d3Szrj simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
318*fae548d3Szrj {
319*fae548d3Szrj simple_object_set_big_32 (buf, val);
320*fae548d3Szrj }
321*fae548d3Szrj
322*fae548d3Szrj /* Store a little-endian 32-bit value coming in as a ulong_type. */
323*fae548d3Szrj
324*fae548d3Szrj static inline void
simple_object_set_little_32_ulong(unsigned char * buf,ulong_type val)325*fae548d3Szrj simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
326*fae548d3Szrj {
327*fae548d3Szrj simple_object_set_little_32 (buf, val);
328*fae548d3Szrj }
329*fae548d3Szrj
330*fae548d3Szrj #ifdef UNSIGNED_64BIT_TYPE
331*fae548d3Szrj
332*fae548d3Szrj /* Store a big-endian 64-bit value. */
333*fae548d3Szrj
334*fae548d3Szrj static inline void
simple_object_set_big_64(unsigned char * buf,ulong_type val)335*fae548d3Szrj simple_object_set_big_64 (unsigned char *buf, ulong_type val)
336*fae548d3Szrj {
337*fae548d3Szrj buf[0] = (val >> 56) & 0xff;
338*fae548d3Szrj buf[1] = (val >> 48) & 0xff;
339*fae548d3Szrj buf[2] = (val >> 40) & 0xff;
340*fae548d3Szrj buf[3] = (val >> 32) & 0xff;
341*fae548d3Szrj buf[4] = (val >> 24) & 0xff;
342*fae548d3Szrj buf[5] = (val >> 16) & 0xff;
343*fae548d3Szrj buf[6] = (val >> 8) & 0xff;
344*fae548d3Szrj buf[7] = val & 0xff;
345*fae548d3Szrj }
346*fae548d3Szrj
347*fae548d3Szrj /* Store a little-endian 64-bit value. */
348*fae548d3Szrj
349*fae548d3Szrj static inline void
simple_object_set_little_64(unsigned char * buf,ulong_type val)350*fae548d3Szrj simple_object_set_little_64 (unsigned char *buf, ulong_type val)
351*fae548d3Szrj {
352*fae548d3Szrj buf[7] = (val >> 56) & 0xff;
353*fae548d3Szrj buf[6] = (val >> 48) & 0xff;
354*fae548d3Szrj buf[5] = (val >> 40) & 0xff;
355*fae548d3Szrj buf[4] = (val >> 32) & 0xff;
356*fae548d3Szrj buf[3] = (val >> 24) & 0xff;
357*fae548d3Szrj buf[2] = (val >> 16) & 0xff;
358*fae548d3Szrj buf[1] = (val >> 8) & 0xff;
359*fae548d3Szrj buf[0] = val & 0xff;
360*fae548d3Szrj }
361*fae548d3Szrj
362*fae548d3Szrj #endif
363