aboutsummaryrefslogtreecommitdiff
path: root/libiberty/simple-object-common.h
blob: 52c487383f648c8b93ce1a5b910555dabe547d1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* simple-object-common.h -- common structs for object file manipulation.
   Copyright (C) 2010-2022 Free Software Foundation, Inc.

This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

Libiberty is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB.  If not,
write to the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
Boston, MA 02110-1301, USA.  */

/* Forward reference.  */
struct simple_object_functions;

/* An object file opened for reading.  */

struct simple_object_read_struct
{
  /* The file descriptor.  */
  int descriptor;
  /* The offset within the file.  */
  off_t offset;
  /* The functions which do the actual work.  */
  const struct simple_object_functions *functions;
  /* Private data for the object file format.  */
  void *data;
};

/* Object file attributes.  */

struct simple_object_attributes_struct
{
  /* The functions which do the actual work.  */
  const struct simple_object_functions *functions;
  /* Private data for the object file format.  */
  void *data;
};

/* An object file being created.  */

struct simple_object_write_struct
{
  /* The functions which do the actual work.  */
  const struct simple_object_functions *functions;
  /* The segment_name argument from the user.  */
  char *segment_name;
  /* The start of the list of sections.  */
  simple_object_write_section *sections;
  /* The last entry in the list of sections.  */
  simple_object_write_section *last_section;
  /* Private data for the object file format.  */
  void *data;
};

/* A section in an object file being created.  */

struct simple_object_write_section_struct
{
  /* Next in the list of sections attached to an
     simple_object_write.  */
  simple_object_write_section *next;
  /* The name of this section.  */
  char *name;
  /* The required alignment.  */
  unsigned int align;
  /* The first data attached to this section.  */
  struct simple_object_write_section_buffer *buffers;
  /* The last data attached to this section.  */
  struct simple_object_write_section_buffer *last_buffer;
};

/* Data attached to a section.  */

struct simple_object_write_section_buffer
{
  /* The next data for this section.  */
  struct simple_object_write_section_buffer *next;
  /* The size of the buffer.  */
  size_t size;
  /* The actual bytes.  */
  const void *buffer;
  /* A buffer to free, or NULL.  */
  void *free_buffer;
};

/* The number of bytes we read from the start of the file to pass to
   the match function.  */
#define SIMPLE_OBJECT_MATCH_HEADER_LEN (16)

/* Format-specific object file functions.  */

struct simple_object_functions
{
  /* If this file matches these functions, return a new value for the
     private data for an simple_object_read.  HEADER is the first 16
     bytes of the file.  DESCRIPTOR, OFFSET, SEGMENT_NAME, ERRMSG, and
     ERR are as for simple_object_open_read.  If this file does not
     match, this function should return NULL with *ERRMSG set to
     NULL.  */
  void *(*match) (unsigned char header[SIMPLE_OBJECT_MATCH_HEADER_LEN],
		  int descriptor, off_t offset, const char *segment_name,
		  const char **errmsg, int *err);

  /* Implement simple_object_find_sections.  */
  const char *(*find_sections) (simple_object_read *,
				int (*pfn) (void *, const char *,
					    off_t offset, off_t length),
				void *data,
				int *err);

  /* Return the private data for the attributes for SOBJ.  */
  void *(*fetch_attributes) (simple_object_read *sobj, const char **errmsg,
			     int *err);

  /* Release the private data for an simple_object_read.  */
  void (*release_read) (void *);

  /* Merge the private data for the attributes of two files.  If they
     could be linked together, return NULL.  Otherwise return an error
     message.  */
  const char *(*attributes_merge) (void *, void *, int *err);

  /* Release the private data for an simple_object_attributes.  */
  void (*release_attributes) (void *);

  /* Start creating an object file.  */
  void *(*start_write) (void *attributes_data, const char **errmsg,
			int *err);

  /* Write the complete object file.  */
  const char *(*write_to_file) (simple_object_write *sobj, int descriptor,
				int *err);

  /* Release the private data for an simple_object_write.  */
  void (*release_write) (void *);

  /* Copy LTO debug sections.  */
  const char *(*copy_lto_debug_sections) (simple_object_read *sobj,
					  simple_object_write *dobj,
					  char *(*pfn) (const char *),
					  int *err);
};

/* The known object file formats.  */

extern const struct simple_object_functions simple_object_coff_functions;
extern const struct simple_object_functions simple_object_elf_functions;
extern const struct simple_object_functions simple_object_mach_o_functions;
extern const struct simple_object_functions simple_object_xcoff_functions;

/* Read SIZE bytes from DESCRIPTOR at file offset OFFSET into BUFFER.
   Return non-zero on success.  On failure return 0 and set *ERRMSG
   and *ERR.  */

extern int
simple_object_internal_read (int descriptor, off_t offset,
			     unsigned char *buffer, size_t size,
			     const char **errmsg, int *err);

/* Write SIZE bytes from BUFFER to DESCRIPTOR at file offset OFFSET.
   Return non-zero on success.  On failure return 0 and set *ERRMSG
   and *ERR.  */

extern int
simple_object_internal_write (int descriptor, off_t offset,
			      const unsigned char *buffer, size_t size,
			      const char **errmsg, int *err);

/* Define ulong_type as an unsigned 64-bit type if available.
   Otherwise just make it unsigned long.  */

#ifdef UNSIGNED_64BIT_TYPE
__extension__ typedef UNSIGNED_64BIT_TYPE ulong_type;
#else
typedef unsigned long ulong_type;
#endif

/* Fetch a big-endian 16-bit value.  */

static inline unsigned short
simple_object_fetch_big_16 (const unsigned char *buf)
{
  return ((unsigned short) buf[0] << 8) | (unsigned short) buf[1];
}

/* Fetch a little-endian 16-bit value.  */

static inline unsigned short
simple_object_fetch_little_16 (const unsigned char *buf)
{
  return ((unsigned short) buf[1] << 8) | (unsigned short) buf[0];
}

/* Fetch a big-endian 32-bit value.  */

static inline unsigned int
simple_object_fetch_big_32 (const unsigned char *buf)
{
  return (((unsigned int) buf[0] << 24)
	  | ((unsigned int) buf[1] << 16)
	  | ((unsigned int) buf[2] << 8)
	  | (unsigned int) buf[3]);
}

/* Fetch a little-endian 32-bit value.  */

static inline unsigned int
simple_object_fetch_little_32 (const unsigned char *buf)
{
  return (((unsigned int) buf[3] << 24)
	  | ((unsigned int) buf[2] << 16)
	  | ((unsigned int) buf[1] << 8)
	  | (unsigned int) buf[0]);
}

/* Fetch a big-endian 32-bit value as a ulong_type.  */

static inline ulong_type
simple_object_fetch_big_32_ulong (const unsigned char *buf)
{
  return (ulong_type) simple_object_fetch_big_32 (buf);
}

/* Fetch a little-endian 32-bit value as a ulong_type.  */

static inline ulong_type
simple_object_fetch_little_32_ulong (const unsigned char *buf)
{
  return (ulong_type) simple_object_fetch_little_32 (buf);
}

#ifdef UNSIGNED_64BIT_TYPE

/* Fetch a big-endian 64-bit value.  */

static inline ulong_type
simple_object_fetch_big_64 (const unsigned char *buf)
{
  return (((ulong_type) buf[0] << 56)
	  | ((ulong_type) buf[1] << 48)
	  | ((ulong_type) buf[2] << 40)
	  | ((ulong_type) buf[3] << 32)
	  | ((ulong_type) buf[4] << 24)
	  | ((ulong_type) buf[5] << 16)
	  | ((ulong_type) buf[6] << 8)
	  | (ulong_type) buf[7]);
}

/* Fetch a little-endian 64-bit value.  */

static inline ulong_type
simple_object_fetch_little_64 (const unsigned char *buf)
{
  return (((ulong_type) buf[7] << 56)
	  | ((ulong_type) buf[6] << 48)
	  | ((ulong_type) buf[5] << 40)
	  | ((ulong_type) buf[4] << 32)
	  | ((ulong_type) buf[3] << 24)
	  | ((ulong_type) buf[2] << 16)
	  | ((ulong_type) buf[1] << 8)
	  | (ulong_type) buf[0]);
}

#endif

/* Store a big-endian 16-bit value.  */

static inline void
simple_object_set_big_16 (unsigned char *buf, unsigned short val)
{
  buf[0] = (val >> 8) & 0xff;
  buf[1] = val & 0xff;
}

/* Store a little-endian 16-bit value.  */

static inline void
simple_object_set_little_16 (unsigned char *buf, unsigned short val)
{
  buf[1] = (val >> 8) & 0xff;
  buf[0] = val & 0xff;
}

/* Store a big-endian 32-bit value.  */

static inline void
simple_object_set_big_32 (unsigned char *buf, unsigned int val)
{
  buf[0] = (val >> 24) & 0xff;
  buf[1] = (val >> 16) & 0xff;
  buf[2] = (val >> 8) & 0xff;
  buf[3] = val & 0xff;
}

/* Store a little-endian 32-bit value.  */

static inline void
simple_object_set_little_32 (unsigned char *buf, unsigned int val)
{
  buf[3] = (val >> 24) & 0xff;
  buf[2] = (val >> 16) & 0xff;
  buf[1] = (val >> 8) & 0xff;
  buf[0] = val & 0xff;
}

/* Store a big-endian 32-bit value coming in as a ulong_type.  */

static inline void
simple_object_set_big_32_ulong (unsigned char *buf, ulong_type val)
{
  simple_object_set_big_32 (buf, val);
}

/* Store a little-endian 32-bit value coming in as a ulong_type.  */

static inline void
simple_object_set_little_32_ulong (unsigned char *buf, ulong_type val)
{
  simple_object_set_little_32 (buf, val);
}

#ifdef UNSIGNED_64BIT_TYPE

/* Store a big-endian 64-bit value.  */

static inline void
simple_object_set_big_64 (unsigned char *buf, ulong_type val)
{
  buf[0] = (val >> 56) & 0xff;
  buf[1] = (val >> 48) & 0xff;
  buf[2] = (val >> 40) & 0xff;
  buf[3] = (val >> 32) & 0xff;
  buf[4] = (val >> 24) & 0xff;
  buf[5] = (val >> 16) & 0xff;
  buf[6] = (val >> 8) & 0xff;
  buf[7] = val & 0xff;
}

/* Store a little-endian 64-bit value.  */

static inline void
simple_object_set_little_64 (unsigned char *buf, ulong_type val)
{
  buf[7] = (val >> 56) & 0xff;
  buf[6] = (val >> 48) & 0xff;
  buf[5] = (val >> 40) & 0xff;
  buf[4] = (val >> 32) & 0xff;
  buf[3] = (val >> 24) & 0xff;
  buf[2] = (val >> 16) & 0xff;
  buf[1] = (val >> 8) & 0xff;
  buf[0] = val & 0xff;
}

#endif