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