]>
Commit | Line | Data |
---|---|---|
252b5132 | 1 | /* Assorted BFD support routines, only used internally. |
fd67aa11 | 2 | Copyright (C) 1990-2024 Free Software Foundation, Inc. |
252b5132 RH |
3 | Written by Cygnus Support. |
4 | ||
ca09e32b | 5 | This file is part of BFD, the Binary File Descriptor library. |
252b5132 | 6 | |
ca09e32b NC |
7 | This program is free software; you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
cd123cb7 | 9 | the Free Software Foundation; either version 3 of the License, or |
ca09e32b | 10 | (at your option) any later version. |
252b5132 | 11 | |
ca09e32b NC |
12 | This program is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
252b5132 | 16 | |
ca09e32b NC |
17 | You should have received a copy of the GNU General Public License |
18 | along with this program; if not, write to the Free Software | |
cd123cb7 NC |
19 | Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, |
20 | MA 02110-1301, USA. */ | |
252b5132 | 21 | |
252b5132 | 22 | #include "sysdep.h" |
3db64b00 | 23 | #include "bfd.h" |
252b5132 | 24 | #include "libbfd.h" |
a41bd1c8 | 25 | #include "objalloc.h" |
252b5132 RH |
26 | |
27 | #ifndef HAVE_GETPAGESIZE | |
28 | #define getpagesize() 2048 | |
29 | #endif | |
30 | ||
252b5132 RH |
31 | /* |
32 | SECTION | |
1b74d094 BW |
33 | Implementation details |
34 | ||
35 | SUBSECTION | |
252b5132 RH |
36 | Internal functions |
37 | ||
38 | DESCRIPTION | |
39 | These routines are used within BFD. | |
40 | They are not intended for export, but are documented here for | |
41 | completeness. | |
42 | */ | |
43 | ||
0a1b45a2 | 44 | bool |
d00dd7dc AM |
45 | _bfd_bool_bfd_false (bfd *abfd ATTRIBUTE_UNUSED) |
46 | { | |
0a1b45a2 | 47 | return false; |
d00dd7dc AM |
48 | } |
49 | ||
0a1b45a2 | 50 | bool |
d00dd7dc AM |
51 | _bfd_bool_bfd_asymbol_false (bfd *abfd ATTRIBUTE_UNUSED, |
52 | asymbol *sym ATTRIBUTE_UNUSED) | |
53 | { | |
0a1b45a2 | 54 | return false; |
d00dd7dc AM |
55 | } |
56 | ||
252b5132 RH |
57 | /* A routine which is used in target vectors for unsupported |
58 | operations. */ | |
59 | ||
0a1b45a2 | 60 | bool |
d00dd7dc | 61 | _bfd_bool_bfd_false_error (bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
62 | { |
63 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 64 | return false; |
252b5132 RH |
65 | } |
66 | ||
0a1b45a2 | 67 | bool |
d00dd7dc AM |
68 | _bfd_bool_bfd_link_false_error (bfd *abfd, |
69 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
70 | { | |
71 | return _bfd_bool_bfd_false_error (abfd); | |
72 | } | |
73 | ||
252b5132 RH |
74 | /* A routine which is used in target vectors for supported operations |
75 | which do not actually do anything. */ | |
76 | ||
0a1b45a2 | 77 | bool |
d00dd7dc AM |
78 | _bfd_bool_bfd_true (bfd *ignore ATTRIBUTE_UNUSED) |
79 | { | |
0a1b45a2 | 80 | return true; |
d00dd7dc AM |
81 | } |
82 | ||
0a1b45a2 | 83 | bool |
d00dd7dc AM |
84 | _bfd_bool_bfd_link_true (bfd *abfd ATTRIBUTE_UNUSED, |
85 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
86 | { | |
0a1b45a2 | 87 | return true; |
d00dd7dc AM |
88 | } |
89 | ||
0a1b45a2 | 90 | bool |
d00dd7dc AM |
91 | _bfd_bool_bfd_bfd_true (bfd *ibfd ATTRIBUTE_UNUSED, |
92 | bfd *obfd ATTRIBUTE_UNUSED) | |
93 | { | |
0a1b45a2 | 94 | return true; |
d00dd7dc AM |
95 | } |
96 | ||
0a1b45a2 | 97 | bool |
d00dd7dc AM |
98 | _bfd_bool_bfd_uint_true (bfd *abfd ATTRIBUTE_UNUSED, |
99 | unsigned int flags ATTRIBUTE_UNUSED) | |
100 | { | |
0a1b45a2 | 101 | return true; |
d00dd7dc AM |
102 | } |
103 | ||
0a1b45a2 | 104 | bool |
d00dd7dc AM |
105 | _bfd_bool_bfd_asection_bfd_asection_true (bfd *ibfd ATTRIBUTE_UNUSED, |
106 | asection *isec ATTRIBUTE_UNUSED, | |
107 | bfd *obfd ATTRIBUTE_UNUSED, | |
108 | asection *osec ATTRIBUTE_UNUSED) | |
109 | { | |
0a1b45a2 | 110 | return true; |
d00dd7dc AM |
111 | } |
112 | ||
0a1b45a2 | 113 | bool |
d00dd7dc AM |
114 | _bfd_bool_bfd_asymbol_bfd_asymbol_true (bfd *ibfd ATTRIBUTE_UNUSED, |
115 | asymbol *isym ATTRIBUTE_UNUSED, | |
116 | bfd *obfd ATTRIBUTE_UNUSED, | |
117 | asymbol *osym ATTRIBUTE_UNUSED) | |
118 | { | |
0a1b45a2 | 119 | return true; |
d00dd7dc AM |
120 | } |
121 | ||
0a1b45a2 | 122 | bool |
d00dd7dc AM |
123 | _bfd_bool_bfd_ptr_true (bfd *abfd ATTRIBUTE_UNUSED, |
124 | void *ptr ATTRIBUTE_UNUSED) | |
252b5132 | 125 | { |
0a1b45a2 | 126 | return true; |
252b5132 RH |
127 | } |
128 | ||
129 | /* A routine which is used in target vectors for unsupported | |
130 | operations which return a pointer value. */ | |
131 | ||
c58b9523 | 132 | void * |
d00dd7dc | 133 | _bfd_ptr_bfd_null_error (bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
134 | { |
135 | bfd_set_error (bfd_error_invalid_operation); | |
136 | return NULL; | |
137 | } | |
138 | ||
509945ae | 139 | int |
d00dd7dc | 140 | _bfd_int_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
141 | { |
142 | return 0; | |
143 | } | |
144 | ||
509945ae | 145 | unsigned int |
d00dd7dc | 146 | _bfd_uint_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 RH |
147 | { |
148 | return 0; | |
149 | } | |
150 | ||
252b5132 | 151 | long |
d00dd7dc | 152 | _bfd_long_bfd_0 (bfd *ignore ATTRIBUTE_UNUSED) |
252b5132 | 153 | { |
d00dd7dc | 154 | return 0; |
68d20676 NC |
155 | } |
156 | ||
252b5132 RH |
157 | /* A routine which is used in target vectors for unsupported |
158 | operations which return -1 on error. */ | |
159 | ||
252b5132 | 160 | long |
d00dd7dc | 161 | _bfd_long_bfd_n1_error (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
252b5132 RH |
162 | { |
163 | bfd_set_error (bfd_error_invalid_operation); | |
164 | return -1; | |
165 | } | |
166 | ||
509945ae | 167 | void |
d00dd7dc AM |
168 | _bfd_void_bfd (bfd *ignore ATTRIBUTE_UNUSED) |
169 | { | |
170 | } | |
171 | ||
172 | void | |
173 | _bfd_void_bfd_link (bfd *abfd ATTRIBUTE_UNUSED, | |
174 | struct bfd_link_info *info ATTRIBUTE_UNUSED) | |
175 | { | |
176 | } | |
177 | ||
178 | void | |
179 | _bfd_void_bfd_asection (bfd *abfd ATTRIBUTE_UNUSED, | |
180 | asection *sec ATTRIBUTE_UNUSED) | |
252b5132 RH |
181 | { |
182 | } | |
183 | ||
72f6ea61 AM |
184 | long |
185 | _bfd_norelocs_get_reloc_upper_bound (bfd *abfd ATTRIBUTE_UNUSED, | |
186 | asection *sec ATTRIBUTE_UNUSED) | |
187 | { | |
188 | return sizeof (arelent *); | |
189 | } | |
190 | ||
191 | long | |
192 | _bfd_norelocs_canonicalize_reloc (bfd *abfd ATTRIBUTE_UNUSED, | |
193 | asection *sec ATTRIBUTE_UNUSED, | |
194 | arelent **relptr, | |
195 | asymbol **symbols ATTRIBUTE_UNUSED) | |
196 | { | |
197 | *relptr = NULL; | |
198 | return 0; | |
199 | } | |
200 | ||
23186865 JM |
201 | void |
202 | _bfd_norelocs_set_reloc (bfd *abfd ATTRIBUTE_UNUSED, | |
07d6d2b8 AM |
203 | asection *sec ATTRIBUTE_UNUSED, |
204 | arelent **relptr ATTRIBUTE_UNUSED, | |
205 | unsigned int count ATTRIBUTE_UNUSED) | |
23186865 JM |
206 | { |
207 | /* Do nothing. */ | |
208 | } | |
209 | ||
0a1b45a2 | 210 | bool |
c58b9523 AM |
211 | _bfd_nocore_core_file_matches_executable_p |
212 | (bfd *ignore_core_bfd ATTRIBUTE_UNUSED, | |
213 | bfd *ignore_exec_bfd ATTRIBUTE_UNUSED) | |
252b5132 RH |
214 | { |
215 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 216 | return false; |
252b5132 RH |
217 | } |
218 | ||
219 | /* Routine to handle core_file_failing_command entry point for targets | |
220 | without core file support. */ | |
221 | ||
252b5132 | 222 | char * |
c58b9523 | 223 | _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
252b5132 RH |
224 | { |
225 | bfd_set_error (bfd_error_invalid_operation); | |
c58b9523 | 226 | return NULL; |
252b5132 RH |
227 | } |
228 | ||
229 | /* Routine to handle core_file_failing_signal entry point for targets | |
230 | without core file support. */ | |
231 | ||
252b5132 | 232 | int |
c58b9523 | 233 | _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
252b5132 RH |
234 | { |
235 | bfd_set_error (bfd_error_invalid_operation); | |
236 | return 0; | |
237 | } | |
238 | ||
261b8d08 PA |
239 | /* Routine to handle the core_file_pid entry point for targets without |
240 | core file support. */ | |
241 | ||
242 | int | |
243 | _bfd_nocore_core_file_pid (bfd *ignore_abfd ATTRIBUTE_UNUSED) | |
244 | { | |
245 | bfd_set_error (bfd_error_invalid_operation); | |
246 | return 0; | |
247 | } | |
248 | ||
cb001c0d | 249 | bfd_cleanup |
c58b9523 | 250 | _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED) |
252b5132 RH |
251 | { |
252 | bfd_set_error (bfd_error_wrong_format); | |
253 | return 0; | |
254 | } | |
255 | \f | |
256 | /* Allocate memory using malloc. */ | |
257 | ||
cb87d9f1 AM |
258 | #ifndef SSIZE_MAX |
259 | #define SSIZE_MAX ((size_t) -1 >> 1) | |
260 | #endif | |
261 | ||
bd6d8601 NC |
262 | /* |
263 | INTERNAL_FUNCTION | |
264 | bfd_malloc | |
265 | ||
266 | SYNOPSIS | |
717d4bd6 | 267 | void *bfd_malloc (bfd_size_type {*size*}); |
bd6d8601 NC |
268 | |
269 | DESCRIPTION | |
270 | Returns a pointer to an allocated block of memory that is at least | |
271 | SIZE bytes long. If SIZE is 0 then it will be treated as if it were | |
272 | 1. If SIZE is too big then NULL will be returned. | |
273 | ||
274 | Returns NULL upon error and sets bfd_error. | |
275 | */ | |
c58b9523 AM |
276 | void * |
277 | bfd_malloc (bfd_size_type size) | |
252b5132 | 278 | { |
c58b9523 | 279 | void *ptr; |
db6b071a | 280 | size_t sz = (size_t) size; |
252b5132 | 281 | |
db6b071a NC |
282 | if (size != sz |
283 | /* This is to pacify memory checkers like valgrind. */ | |
cb87d9f1 | 284 | || sz > SSIZE_MAX) |
dc810e39 AM |
285 | { |
286 | bfd_set_error (bfd_error_no_memory); | |
287 | return NULL; | |
288 | } | |
289 | ||
bd6d8601 NC |
290 | ptr = malloc (sz ? sz : 1); |
291 | if (ptr == NULL) | |
252b5132 | 292 | bfd_set_error (bfd_error_no_memory); |
dc810e39 | 293 | |
252b5132 RH |
294 | return ptr; |
295 | } | |
296 | ||
bd6d8601 NC |
297 | /* |
298 | INTERNAL_FUNCTION | |
299 | bfd_realloc | |
300 | ||
301 | SYNOPSIS | |
717d4bd6 | 302 | void *bfd_realloc (void *{*mem*}, bfd_size_type {*size*}); |
bd6d8601 NC |
303 | |
304 | DESCRIPTION | |
305 | Returns a pointer to an allocated block of memory that is at least | |
306 | SIZE bytes long. If SIZE is 0 then it will be treated as if it were | |
307 | 1. If SIZE is too big then NULL will be returned. | |
308 | ||
309 | If MEM is not NULL then it must point to an allocated block of memory. | |
310 | If this block is large enough then MEM may be used as the return | |
311 | value for this function, but this is not guaranteed. | |
312 | ||
313 | If MEM is not returned then the first N bytes in the returned block | |
314 | will be identical to the first N bytes in region pointed to by MEM, | |
315 | where N is the lessor of SIZE and the length of the region of memory | |
316 | currently addressed by MEM. | |
317 | ||
318 | Returns NULL upon error and sets bfd_error. | |
319 | */ | |
c58b9523 AM |
320 | void * |
321 | bfd_realloc (void *ptr, bfd_size_type size) | |
252b5132 | 322 | { |
c58b9523 | 323 | void *ret; |
db6b071a | 324 | size_t sz = (size_t) size; |
252b5132 | 325 | |
db6b071a NC |
326 | if (ptr == NULL) |
327 | return bfd_malloc (size); | |
328 | ||
329 | if (size != sz | |
330 | /* This is to pacify memory checkers like valgrind. */ | |
cb87d9f1 | 331 | || sz > SSIZE_MAX) |
dc810e39 AM |
332 | { |
333 | bfd_set_error (bfd_error_no_memory); | |
334 | return NULL; | |
335 | } | |
336 | ||
bd6d8601 NC |
337 | /* The behaviour of realloc(0) is implementation defined, |
338 | but for this function we always allocate memory. */ | |
339 | ret = realloc (ptr, sz ? sz : 1); | |
252b5132 | 340 | |
bd6d8601 | 341 | if (ret == NULL) |
252b5132 RH |
342 | bfd_set_error (bfd_error_no_memory); |
343 | ||
344 | return ret; | |
345 | } | |
346 | ||
bd6d8601 NC |
347 | /* |
348 | INTERNAL_FUNCTION | |
349 | bfd_realloc_or_free | |
350 | ||
351 | SYNOPSIS | |
717d4bd6 | 352 | void *bfd_realloc_or_free (void *{*mem*}, bfd_size_type {*size*}); |
515ef31d | 353 | |
bd6d8601 NC |
354 | DESCRIPTION |
355 | Returns a pointer to an allocated block of memory that is at least | |
356 | SIZE bytes long. If SIZE is 0 then no memory will be allocated, | |
357 | MEM will be freed, and NULL will be returned. This will not cause | |
358 | bfd_error to be set. | |
359 | ||
360 | If SIZE is too big then NULL will be returned and bfd_error will be | |
361 | set. | |
362 | ||
363 | If MEM is not NULL then it must point to an allocated block of memory. | |
364 | If this block is large enough then MEM may be used as the return | |
365 | value for this function, but this is not guaranteed. | |
366 | ||
367 | If MEM is not returned then the first N bytes in the returned block | |
368 | will be identical to the first N bytes in region pointed to by MEM, | |
369 | where N is the lessor of SIZE and the length of the region of memory | |
370 | currently addressed by MEM. | |
371 | */ | |
515ef31d NC |
372 | void * |
373 | bfd_realloc_or_free (void *ptr, bfd_size_type size) | |
374 | { | |
bd6d8601 | 375 | void *ret; |
515ef31d | 376 | |
bd6d8601 NC |
377 | /* The behaviour of realloc(0) is implementation defined, but |
378 | for this function we treat it is always freeing the memory. */ | |
379 | if (size == 0) | |
380 | { | |
381 | free (ptr); | |
382 | return NULL; | |
383 | } | |
384 | ||
385 | ret = bfd_realloc (ptr, size); | |
386 | if (ret == NULL) | |
db6b071a | 387 | free (ptr); |
515ef31d NC |
388 | |
389 | return ret; | |
390 | } | |
391 | ||
bd6d8601 NC |
392 | /* |
393 | INTERNAL_FUNCTION | |
394 | bfd_zmalloc | |
252b5132 | 395 | |
bd6d8601 | 396 | SYNOPSIS |
717d4bd6 | 397 | void *bfd_zmalloc (bfd_size_type {*size*}); |
bd6d8601 NC |
398 | |
399 | DESCRIPTION | |
400 | Returns a pointer to an allocated block of memory that is at least | |
401 | SIZE bytes long. If SIZE is 0 then it will be treated as if it were | |
402 | 1. If SIZE is too big then NULL will be returned. | |
403 | ||
404 | Returns NULL upon error and sets bfd_error. | |
405 | ||
406 | If NULL is not returned then the allocated block of memory will | |
407 | have been cleared. | |
408 | */ | |
c58b9523 AM |
409 | void * |
410 | bfd_zmalloc (bfd_size_type size) | |
252b5132 | 411 | { |
db6b071a | 412 | void *ptr = bfd_malloc (size); |
252b5132 | 413 | |
bd6d8601 NC |
414 | if (ptr != NULL) |
415 | memset (ptr, 0, size ? (size_t) size : 1); | |
252b5132 RH |
416 | |
417 | return ptr; | |
418 | } | |
d0fb9a8d | 419 | |
a41bd1c8 AM |
420 | /* |
421 | FUNCTION | |
422 | bfd_alloc | |
423 | ||
424 | SYNOPSIS | |
425 | void *bfd_alloc (bfd *abfd, bfd_size_type wanted); | |
426 | ||
427 | DESCRIPTION | |
428 | Allocate a block of @var{wanted} bytes of memory attached to | |
429 | <<abfd>> and return a pointer to it. | |
430 | */ | |
431 | ||
432 | void * | |
433 | bfd_alloc (bfd *abfd, bfd_size_type size) | |
434 | { | |
435 | void *ret; | |
436 | unsigned long ul_size = (unsigned long) size; | |
437 | ||
438 | if (size != ul_size | |
439 | /* Note - although objalloc_alloc takes an unsigned long as its | |
440 | argument, internally the size is treated as a signed long. This can | |
441 | lead to problems where, for example, a request to allocate -1 bytes | |
442 | can result in just 1 byte being allocated, rather than | |
443 | ((unsigned long) -1) bytes. Also memory checkers will often | |
444 | complain about attempts to allocate a negative amount of memory. | |
445 | So to stop these problems we fail if the size is negative. */ | |
446 | || ((signed long) ul_size) < 0) | |
447 | { | |
448 | bfd_set_error (bfd_error_no_memory); | |
449 | return NULL; | |
450 | } | |
451 | ||
452 | ret = objalloc_alloc ((struct objalloc *) abfd->memory, ul_size); | |
453 | if (ret == NULL) | |
454 | bfd_set_error (bfd_error_no_memory); | |
455 | else | |
456 | abfd->alloc_size += size; | |
457 | return ret; | |
458 | } | |
459 | ||
460 | /* | |
461 | FUNCTION | |
462 | bfd_zalloc | |
463 | ||
464 | SYNOPSIS | |
465 | void *bfd_zalloc (bfd *abfd, bfd_size_type wanted); | |
466 | ||
467 | DESCRIPTION | |
468 | Allocate a block of @var{wanted} bytes of zeroed memory | |
469 | attached to <<abfd>> and return a pointer to it. | |
470 | */ | |
471 | ||
472 | void * | |
473 | bfd_zalloc (bfd *abfd, bfd_size_type size) | |
474 | { | |
475 | void *res; | |
476 | ||
477 | res = bfd_alloc (abfd, size); | |
478 | if (res) | |
479 | memset (res, 0, (size_t) size); | |
480 | return res; | |
481 | } | |
482 | ||
483 | /* | |
484 | FUNCTION | |
485 | bfd_release | |
486 | ||
487 | SYNOPSIS | |
488 | void bfd_release (bfd *, void *); | |
489 | ||
490 | DESCRIPTION | |
491 | Free a block allocated for a BFD. | |
492 | Note: Also frees all more recently allocated blocks! | |
493 | */ | |
494 | ||
495 | void | |
496 | bfd_release (bfd *abfd, void *block) | |
497 | { | |
498 | objalloc_free_block ((struct objalloc *) abfd->memory, block); | |
499 | } | |
500 | ||
252b5132 RH |
501 | /* |
502 | INTERNAL_FUNCTION | |
503 | bfd_write_bigendian_4byte_int | |
504 | ||
505 | SYNOPSIS | |
0a1b45a2 | 506 | bool bfd_write_bigendian_4byte_int (bfd *, unsigned int); |
252b5132 RH |
507 | |
508 | DESCRIPTION | |
509 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big | |
510 | endian order regardless of what else is going on. This is useful in | |
511 | archives. | |
512 | ||
513 | */ | |
0a1b45a2 | 514 | bool |
c58b9523 | 515 | bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i) |
252b5132 RH |
516 | { |
517 | bfd_byte buffer[4]; | |
226f9f4f AM |
518 | bfd_putb32 (i, buffer); |
519 | return bfd_write (buffer, 4, abfd) == 4; | |
252b5132 RH |
520 | } |
521 | ||
252b5132 RH |
522 | \f |
523 | /** The do-it-yourself (byte) sex-change kit */ | |
524 | ||
525 | /* The middle letter e.g. get<b>short indicates Big or Little endian | |
526 | target machine. It doesn't matter what the byte order of the host | |
527 | machine is; these routines work for either. */ | |
528 | ||
529 | /* FIXME: Should these take a count argument? | |
530 | Answer (gnu@cygnus.com): No, but perhaps they should be inline | |
07d6d2b8 AM |
531 | functions in swap.h #ifdef __GNUC__. |
532 | Gprof them later and find out. */ | |
252b5132 RH |
533 | |
534 | /* | |
535 | FUNCTION | |
536 | bfd_put_size | |
537 | FUNCTION | |
538 | bfd_get_size | |
539 | ||
540 | DESCRIPTION | |
541 | These macros as used for reading and writing raw data in | |
542 | sections; each access (except for bytes) is vectored through | |
543 | the target format of the BFD and mangled accordingly. The | |
544 | mangling performs any necessary endian translations and | |
545 | removes alignment restrictions. Note that types accepted and | |
546 | returned by these macros are identical so they can be swapped | |
547 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> | |
548 | to either <<bfd_get_32>> or <<bfd_get_64>>. | |
549 | ||
550 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a | |
551 | system without prototypes, the caller is responsible for making | |
552 | sure that is true, with a cast if necessary. We don't cast | |
553 | them in the macro definitions because that would prevent <<lint>> | |
554 | or <<gcc -Wall>> from detecting sins such as passing a pointer. | |
555 | To detect calling these with less than a <<bfd_vma>>, use | |
556 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. | |
557 | ||
558 | . | |
559 | .{* Byte swapping macros for user section data. *} | |
560 | . | |
561 | .#define bfd_put_8(abfd, val, ptr) \ | |
f68912e8 | 562 | . ((void) (*((bfd_byte *) (ptr)) = (val) & 0xff)) |
252b5132 | 563 | .#define bfd_put_signed_8 \ |
c58b9523 | 564 | . bfd_put_8 |
252b5132 | 565 | .#define bfd_get_8(abfd, ptr) \ |
f68912e8 | 566 | . ((bfd_vma) *(const bfd_byte *) (ptr) & 0xff) |
252b5132 | 567 | .#define bfd_get_signed_8(abfd, ptr) \ |
f68912e8 | 568 | . ((((bfd_signed_vma) *(const bfd_byte *) (ptr) & 0xff) ^ 0x80) - 0x80) |
252b5132 RH |
569 | . |
570 | .#define bfd_put_16(abfd, val, ptr) \ | |
c58b9523 | 571 | . BFD_SEND (abfd, bfd_putx16, ((val),(ptr))) |
252b5132 | 572 | .#define bfd_put_signed_16 \ |
c58b9523 | 573 | . bfd_put_16 |
252b5132 | 574 | .#define bfd_get_16(abfd, ptr) \ |
c58b9523 | 575 | . BFD_SEND (abfd, bfd_getx16, (ptr)) |
252b5132 | 576 | .#define bfd_get_signed_16(abfd, ptr) \ |
c58b9523 | 577 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) |
252b5132 | 578 | . |
370e4b50 JD |
579 | .#define bfd_put_24(abfd, val, ptr) \ |
580 | . do \ | |
581 | . if (bfd_big_endian (abfd)) \ | |
582 | . bfd_putb24 ((val), (ptr)); \ | |
583 | . else \ | |
584 | . bfd_putl24 ((val), (ptr)); \ | |
585 | . while (0) | |
586 | . | |
587 | .bfd_vma bfd_getb24 (const void *p); | |
588 | .bfd_vma bfd_getl24 (const void *p); | |
589 | . | |
590 | .#define bfd_get_24(abfd, ptr) \ | |
591 | . (bfd_big_endian (abfd) ? bfd_getb24 (ptr) : bfd_getl24 (ptr)) | |
592 | . | |
252b5132 | 593 | .#define bfd_put_32(abfd, val, ptr) \ |
c58b9523 | 594 | . BFD_SEND (abfd, bfd_putx32, ((val),(ptr))) |
252b5132 | 595 | .#define bfd_put_signed_32 \ |
c58b9523 | 596 | . bfd_put_32 |
252b5132 | 597 | .#define bfd_get_32(abfd, ptr) \ |
c58b9523 | 598 | . BFD_SEND (abfd, bfd_getx32, (ptr)) |
252b5132 | 599 | .#define bfd_get_signed_32(abfd, ptr) \ |
c58b9523 | 600 | . BFD_SEND (abfd, bfd_getx_signed_32, (ptr)) |
252b5132 RH |
601 | . |
602 | .#define bfd_put_64(abfd, val, ptr) \ | |
c58b9523 | 603 | . BFD_SEND (abfd, bfd_putx64, ((val), (ptr))) |
252b5132 | 604 | .#define bfd_put_signed_64 \ |
c58b9523 | 605 | . bfd_put_64 |
252b5132 | 606 | .#define bfd_get_64(abfd, ptr) \ |
c58b9523 | 607 | . BFD_SEND (abfd, bfd_getx64, (ptr)) |
252b5132 | 608 | .#define bfd_get_signed_64(abfd, ptr) \ |
c58b9523 | 609 | . BFD_SEND (abfd, bfd_getx_signed_64, (ptr)) |
252b5132 | 610 | . |
c58b9523 | 611 | .#define bfd_get(bits, abfd, ptr) \ |
199d46be | 612 | . ((bits) == 8 ? bfd_get_8 (abfd, ptr) \ |
c58b9523 AM |
613 | . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \ |
614 | . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \ | |
615 | . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \ | |
616 | . : (abort (), (bfd_vma) - 1)) | |
c7ac6ff8 | 617 | . |
c58b9523 AM |
618 | .#define bfd_put(bits, abfd, val, ptr) \ |
619 | . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \ | |
07d6d2b8 AM |
620 | . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \ |
621 | . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \ | |
622 | . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \ | |
c58b9523 | 623 | . : (abort (), (void) 0)) |
c7ac6ff8 | 624 | . |
509945ae | 625 | */ |
252b5132 RH |
626 | |
627 | /* | |
628 | FUNCTION | |
629 | bfd_h_put_size | |
630 | bfd_h_get_size | |
631 | ||
632 | DESCRIPTION | |
633 | These macros have the same function as their <<bfd_get_x>> | |
dc810e39 | 634 | brethren, except that they are used for removing information |
252b5132 RH |
635 | for the header records of object files. Believe it or not, |
636 | some object files keep their header records in big endian | |
637 | order and their data in little endian order. | |
638 | . | |
639 | .{* Byte swapping macros for file header data. *} | |
640 | . | |
641 | .#define bfd_h_put_8(abfd, val, ptr) \ | |
dc810e39 | 642 | . bfd_put_8 (abfd, val, ptr) |
252b5132 | 643 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ |
dc810e39 | 644 | . bfd_put_8 (abfd, val, ptr) |
252b5132 | 645 | .#define bfd_h_get_8(abfd, ptr) \ |
dc810e39 | 646 | . bfd_get_8 (abfd, ptr) |
252b5132 | 647 | .#define bfd_h_get_signed_8(abfd, ptr) \ |
dc810e39 | 648 | . bfd_get_signed_8 (abfd, ptr) |
252b5132 RH |
649 | . |
650 | .#define bfd_h_put_16(abfd, val, ptr) \ | |
dc810e39 | 651 | . BFD_SEND (abfd, bfd_h_putx16, (val, ptr)) |
252b5132 | 652 | .#define bfd_h_put_signed_16 \ |
dc810e39 | 653 | . bfd_h_put_16 |
252b5132 | 654 | .#define bfd_h_get_16(abfd, ptr) \ |
dc810e39 | 655 | . BFD_SEND (abfd, bfd_h_getx16, (ptr)) |
252b5132 | 656 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
dc810e39 | 657 | . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr)) |
252b5132 RH |
658 | . |
659 | .#define bfd_h_put_32(abfd, val, ptr) \ | |
dc810e39 | 660 | . BFD_SEND (abfd, bfd_h_putx32, (val, ptr)) |
252b5132 | 661 | .#define bfd_h_put_signed_32 \ |
dc810e39 | 662 | . bfd_h_put_32 |
252b5132 | 663 | .#define bfd_h_get_32(abfd, ptr) \ |
dc810e39 | 664 | . BFD_SEND (abfd, bfd_h_getx32, (ptr)) |
252b5132 | 665 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
dc810e39 | 666 | . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr)) |
252b5132 RH |
667 | . |
668 | .#define bfd_h_put_64(abfd, val, ptr) \ | |
dc810e39 | 669 | . BFD_SEND (abfd, bfd_h_putx64, (val, ptr)) |
252b5132 | 670 | .#define bfd_h_put_signed_64 \ |
dc810e39 | 671 | . bfd_h_put_64 |
252b5132 | 672 | .#define bfd_h_get_64(abfd, ptr) \ |
dc810e39 | 673 | . BFD_SEND (abfd, bfd_h_getx64, (ptr)) |
252b5132 | 674 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
dc810e39 | 675 | . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr)) |
252b5132 | 676 | . |
edeb6e24 | 677 | .{* Aliases for the above, which should eventually go away. *} |
dc810e39 | 678 | . |
edeb6e24 AM |
679 | .#define H_PUT_64 bfd_h_put_64 |
680 | .#define H_PUT_32 bfd_h_put_32 | |
681 | .#define H_PUT_16 bfd_h_put_16 | |
682 | .#define H_PUT_8 bfd_h_put_8 | |
683 | .#define H_PUT_S64 bfd_h_put_signed_64 | |
684 | .#define H_PUT_S32 bfd_h_put_signed_32 | |
685 | .#define H_PUT_S16 bfd_h_put_signed_16 | |
686 | .#define H_PUT_S8 bfd_h_put_signed_8 | |
687 | .#define H_GET_64 bfd_h_get_64 | |
688 | .#define H_GET_32 bfd_h_get_32 | |
689 | .#define H_GET_16 bfd_h_get_16 | |
690 | .#define H_GET_8 bfd_h_get_8 | |
691 | .#define H_GET_S64 bfd_h_get_signed_64 | |
692 | .#define H_GET_S32 bfd_h_get_signed_32 | |
693 | .#define H_GET_S16 bfd_h_get_signed_16 | |
694 | .#define H_GET_S8 bfd_h_get_signed_8 | |
dc810e39 AM |
695 | . |
696 | .*/ | |
252b5132 RH |
697 | |
698 | /* Sign extension to bfd_signed_vma. */ | |
65879393 AM |
699 | #define COERCE16(x) (((bfd_vma) (x) ^ 0x8000) - 0x8000) |
700 | #define COERCE32(x) (((bfd_vma) (x) ^ 0x80000000) - 0x80000000) | |
252b5132 | 701 | #define COERCE64(x) \ |
0e3c1eeb | 702 | (((uint64_t) (x) ^ ((uint64_t) 1 << 63)) - ((uint64_t) 1 << 63)) |
252b5132 | 703 | |
717d4bd6 AM |
704 | /* |
705 | FUNCTION | |
706 | Byte swapping routines. | |
707 | ||
708 | SYNOPSIS | |
709 | uint64_t bfd_getb64 (const void *); | |
710 | uint64_t bfd_getl64 (const void *); | |
711 | int64_t bfd_getb_signed_64 (const void *); | |
712 | int64_t bfd_getl_signed_64 (const void *); | |
713 | bfd_vma bfd_getb32 (const void *); | |
714 | bfd_vma bfd_getl32 (const void *); | |
715 | bfd_signed_vma bfd_getb_signed_32 (const void *); | |
716 | bfd_signed_vma bfd_getl_signed_32 (const void *); | |
717 | bfd_vma bfd_getb16 (const void *); | |
718 | bfd_vma bfd_getl16 (const void *); | |
719 | bfd_signed_vma bfd_getb_signed_16 (const void *); | |
720 | bfd_signed_vma bfd_getl_signed_16 (const void *); | |
721 | void bfd_putb64 (uint64_t, void *); | |
722 | void bfd_putl64 (uint64_t, void *); | |
723 | void bfd_putb32 (bfd_vma, void *); | |
724 | void bfd_putl32 (bfd_vma, void *); | |
725 | void bfd_putb24 (bfd_vma, void *); | |
726 | void bfd_putl24 (bfd_vma, void *); | |
727 | void bfd_putb16 (bfd_vma, void *); | |
728 | void bfd_putl16 (bfd_vma, void *); | |
729 | uint64_t bfd_get_bits (const void *, int, bool); | |
730 | void bfd_put_bits (uint64_t, void *, int, bool); | |
00aea11f AM |
731 | |
732 | DESCRIPTION | |
733 | Read and write integers in a particular endian order. getb | |
734 | and putb functions handle big-endian, getl and putl handle | |
735 | little-endian. bfd_get_bits and bfd_put_bits specify | |
736 | big-endian by passing TRUE in the last parameter, | |
737 | little-endian by passing FALSE. | |
717d4bd6 AM |
738 | */ |
739 | ||
252b5132 | 740 | bfd_vma |
edeb6e24 | 741 | bfd_getb16 (const void *p) |
252b5132 | 742 | { |
a50b1753 | 743 | const bfd_byte *addr = (const bfd_byte *) p; |
252b5132 RH |
744 | return (addr[0] << 8) | addr[1]; |
745 | } | |
746 | ||
747 | bfd_vma | |
edeb6e24 | 748 | bfd_getl16 (const void *p) |
252b5132 | 749 | { |
a50b1753 | 750 | const bfd_byte *addr = (const bfd_byte *) p; |
252b5132 RH |
751 | return (addr[1] << 8) | addr[0]; |
752 | } | |
753 | ||
754 | bfd_signed_vma | |
edeb6e24 | 755 | bfd_getb_signed_16 (const void *p) |
252b5132 | 756 | { |
a50b1753 | 757 | const bfd_byte *addr = (const bfd_byte *) p; |
c58b9523 | 758 | return COERCE16 ((addr[0] << 8) | addr[1]); |
252b5132 RH |
759 | } |
760 | ||
761 | bfd_signed_vma | |
edeb6e24 | 762 | bfd_getl_signed_16 (const void *p) |
252b5132 | 763 | { |
a50b1753 | 764 | const bfd_byte *addr = (const bfd_byte *) p; |
c58b9523 | 765 | return COERCE16 ((addr[1] << 8) | addr[0]); |
252b5132 RH |
766 | } |
767 | ||
768 | void | |
edeb6e24 | 769 | bfd_putb16 (bfd_vma data, void *p) |
252b5132 | 770 | { |
a50b1753 | 771 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
772 | addr[0] = (data >> 8) & 0xff; |
773 | addr[1] = data & 0xff; | |
252b5132 RH |
774 | } |
775 | ||
776 | void | |
edeb6e24 | 777 | bfd_putl16 (bfd_vma data, void *p) |
252b5132 | 778 | { |
a50b1753 | 779 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
780 | addr[0] = data & 0xff; |
781 | addr[1] = (data >> 8) & 0xff; | |
252b5132 RH |
782 | } |
783 | ||
7b4ae824 JD |
784 | void |
785 | bfd_putb24 (bfd_vma data, void *p) | |
786 | { | |
787 | bfd_byte *addr = (bfd_byte *) p; | |
788 | addr[0] = (data >> 16) & 0xff; | |
789 | addr[1] = (data >> 8) & 0xff; | |
790 | addr[2] = data & 0xff; | |
791 | } | |
792 | ||
7b4ae824 JD |
793 | void |
794 | bfd_putl24 (bfd_vma data, void *p) | |
795 | { | |
796 | bfd_byte *addr = (bfd_byte *) p; | |
797 | addr[0] = data & 0xff; | |
798 | addr[1] = (data >> 8) & 0xff; | |
799 | addr[2] = (data >> 16) & 0xff; | |
800 | } | |
801 | ||
370e4b50 JD |
802 | bfd_vma |
803 | bfd_getb24 (const void *p) | |
804 | { | |
805 | const bfd_byte *addr = (const bfd_byte *) p; | |
f68912e8 | 806 | uint32_t v; |
370e4b50 | 807 | |
f68912e8 AM |
808 | v = (uint32_t) addr[0] << 16; |
809 | v |= (uint32_t) addr[1] << 8; | |
810 | v |= (uint32_t) addr[2]; | |
370e4b50 JD |
811 | return v; |
812 | } | |
813 | ||
814 | bfd_vma | |
815 | bfd_getl24 (const void *p) | |
816 | { | |
817 | const bfd_byte *addr = (const bfd_byte *) p; | |
f68912e8 | 818 | uint32_t v; |
370e4b50 | 819 | |
f68912e8 AM |
820 | v = (uint32_t) addr[0]; |
821 | v |= (uint32_t) addr[1] << 8; | |
822 | v |= (uint32_t) addr[2] << 16; | |
370e4b50 JD |
823 | return v; |
824 | } | |
7b4ae824 | 825 | |
252b5132 | 826 | bfd_vma |
edeb6e24 | 827 | bfd_getb32 (const void *p) |
252b5132 | 828 | { |
a50b1753 | 829 | const bfd_byte *addr = (const bfd_byte *) p; |
f68912e8 | 830 | uint32_t v; |
252b5132 | 831 | |
f68912e8 AM |
832 | v = (uint32_t) addr[0] << 24; |
833 | v |= (uint32_t) addr[1] << 16; | |
834 | v |= (uint32_t) addr[2] << 8; | |
835 | v |= (uint32_t) addr[3]; | |
c58b9523 | 836 | return v; |
252b5132 RH |
837 | } |
838 | ||
839 | bfd_vma | |
edeb6e24 | 840 | bfd_getl32 (const void *p) |
252b5132 | 841 | { |
a50b1753 | 842 | const bfd_byte *addr = (const bfd_byte *) p; |
f68912e8 | 843 | uint32_t v; |
252b5132 | 844 | |
f68912e8 AM |
845 | v = (uint32_t) addr[0]; |
846 | v |= (uint32_t) addr[1] << 8; | |
847 | v |= (uint32_t) addr[2] << 16; | |
848 | v |= (uint32_t) addr[3] << 24; | |
c58b9523 | 849 | return v; |
252b5132 RH |
850 | } |
851 | ||
852 | bfd_signed_vma | |
edeb6e24 | 853 | bfd_getb_signed_32 (const void *p) |
252b5132 | 854 | { |
a50b1753 | 855 | const bfd_byte *addr = (const bfd_byte *) p; |
f68912e8 | 856 | uint32_t v; |
252b5132 | 857 | |
f68912e8 AM |
858 | v = (uint32_t) addr[0] << 24; |
859 | v |= (uint32_t) addr[1] << 16; | |
860 | v |= (uint32_t) addr[2] << 8; | |
861 | v |= (uint32_t) addr[3]; | |
252b5132 RH |
862 | return COERCE32 (v); |
863 | } | |
864 | ||
865 | bfd_signed_vma | |
edeb6e24 | 866 | bfd_getl_signed_32 (const void *p) |
252b5132 | 867 | { |
a50b1753 | 868 | const bfd_byte *addr = (const bfd_byte *) p; |
f68912e8 | 869 | uint32_t v; |
252b5132 | 870 | |
f68912e8 AM |
871 | v = (uint32_t) addr[0]; |
872 | v |= (uint32_t) addr[1] << 8; | |
873 | v |= (uint32_t) addr[2] << 16; | |
874 | v |= (uint32_t) addr[3] << 24; | |
252b5132 RH |
875 | return COERCE32 (v); |
876 | } | |
877 | ||
0e3c1eeb AM |
878 | uint64_t |
879 | bfd_getb64 (const void *p) | |
252b5132 | 880 | { |
a50b1753 | 881 | const bfd_byte *addr = (const bfd_byte *) p; |
0e3c1eeb | 882 | uint64_t v; |
c58b9523 AM |
883 | |
884 | v = addr[0]; v <<= 8; | |
885 | v |= addr[1]; v <<= 8; | |
886 | v |= addr[2]; v <<= 8; | |
887 | v |= addr[3]; v <<= 8; | |
888 | v |= addr[4]; v <<= 8; | |
889 | v |= addr[5]; v <<= 8; | |
890 | v |= addr[6]; v <<= 8; | |
891 | v |= addr[7]; | |
892 | ||
893 | return v; | |
252b5132 RH |
894 | } |
895 | ||
0e3c1eeb AM |
896 | uint64_t |
897 | bfd_getl64 (const void *p) | |
252b5132 | 898 | { |
a50b1753 | 899 | const bfd_byte *addr = (const bfd_byte *) p; |
0e3c1eeb | 900 | uint64_t v; |
c58b9523 AM |
901 | |
902 | v = addr[7]; v <<= 8; | |
903 | v |= addr[6]; v <<= 8; | |
904 | v |= addr[5]; v <<= 8; | |
905 | v |= addr[4]; v <<= 8; | |
906 | v |= addr[3]; v <<= 8; | |
907 | v |= addr[2]; v <<= 8; | |
908 | v |= addr[1]; v <<= 8; | |
909 | v |= addr[0]; | |
910 | ||
911 | return v; | |
252b5132 RH |
912 | } |
913 | ||
0e3c1eeb AM |
914 | int64_t |
915 | bfd_getb_signed_64 (const void *p) | |
252b5132 | 916 | { |
a50b1753 | 917 | const bfd_byte *addr = (const bfd_byte *) p; |
0e3c1eeb | 918 | uint64_t v; |
c58b9523 AM |
919 | |
920 | v = addr[0]; v <<= 8; | |
921 | v |= addr[1]; v <<= 8; | |
922 | v |= addr[2]; v <<= 8; | |
923 | v |= addr[3]; v <<= 8; | |
924 | v |= addr[4]; v <<= 8; | |
925 | v |= addr[5]; v <<= 8; | |
926 | v |= addr[6]; v <<= 8; | |
927 | v |= addr[7]; | |
928 | ||
929 | return COERCE64 (v); | |
252b5132 RH |
930 | } |
931 | ||
0e3c1eeb AM |
932 | int64_t |
933 | bfd_getl_signed_64 (const void *p) | |
252b5132 | 934 | { |
a50b1753 | 935 | const bfd_byte *addr = (const bfd_byte *) p; |
0e3c1eeb | 936 | uint64_t v; |
c58b9523 AM |
937 | |
938 | v = addr[7]; v <<= 8; | |
939 | v |= addr[6]; v <<= 8; | |
940 | v |= addr[5]; v <<= 8; | |
941 | v |= addr[4]; v <<= 8; | |
942 | v |= addr[3]; v <<= 8; | |
943 | v |= addr[2]; v <<= 8; | |
944 | v |= addr[1]; v <<= 8; | |
945 | v |= addr[0]; | |
946 | ||
947 | return COERCE64 (v); | |
252b5132 RH |
948 | } |
949 | ||
950 | void | |
edeb6e24 | 951 | bfd_putb32 (bfd_vma data, void *p) |
252b5132 | 952 | { |
a50b1753 | 953 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
954 | addr[0] = (data >> 24) & 0xff; |
955 | addr[1] = (data >> 16) & 0xff; | |
956 | addr[2] = (data >> 8) & 0xff; | |
957 | addr[3] = data & 0xff; | |
252b5132 RH |
958 | } |
959 | ||
960 | void | |
edeb6e24 | 961 | bfd_putl32 (bfd_vma data, void *p) |
252b5132 | 962 | { |
a50b1753 | 963 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
964 | addr[0] = data & 0xff; |
965 | addr[1] = (data >> 8) & 0xff; | |
966 | addr[2] = (data >> 16) & 0xff; | |
967 | addr[3] = (data >> 24) & 0xff; | |
252b5132 RH |
968 | } |
969 | ||
970 | void | |
0e3c1eeb | 971 | bfd_putb64 (uint64_t data, void *p) |
252b5132 | 972 | { |
a50b1753 | 973 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
974 | addr[0] = (data >> (7*8)) & 0xff; |
975 | addr[1] = (data >> (6*8)) & 0xff; | |
976 | addr[2] = (data >> (5*8)) & 0xff; | |
977 | addr[3] = (data >> (4*8)) & 0xff; | |
978 | addr[4] = (data >> (3*8)) & 0xff; | |
979 | addr[5] = (data >> (2*8)) & 0xff; | |
980 | addr[6] = (data >> (1*8)) & 0xff; | |
981 | addr[7] = (data >> (0*8)) & 0xff; | |
252b5132 RH |
982 | } |
983 | ||
984 | void | |
0e3c1eeb | 985 | bfd_putl64 (uint64_t data, void *p) |
252b5132 | 986 | { |
a50b1753 | 987 | bfd_byte *addr = (bfd_byte *) p; |
edeb6e24 AM |
988 | addr[7] = (data >> (7*8)) & 0xff; |
989 | addr[6] = (data >> (6*8)) & 0xff; | |
990 | addr[5] = (data >> (5*8)) & 0xff; | |
991 | addr[4] = (data >> (4*8)) & 0xff; | |
992 | addr[3] = (data >> (3*8)) & 0xff; | |
993 | addr[2] = (data >> (2*8)) & 0xff; | |
994 | addr[1] = (data >> (1*8)) & 0xff; | |
995 | addr[0] = (data >> (0*8)) & 0xff; | |
252b5132 | 996 | } |
8c603c85 NC |
997 | |
998 | void | |
0e3c1eeb | 999 | bfd_put_bits (uint64_t data, void *p, int bits, bool big_p) |
8c603c85 | 1000 | { |
a50b1753 | 1001 | bfd_byte *addr = (bfd_byte *) p; |
8c603c85 NC |
1002 | int i; |
1003 | int bytes; | |
1004 | ||
1005 | if (bits % 8 != 0) | |
1006 | abort (); | |
1007 | ||
1008 | bytes = bits / 8; | |
1009 | for (i = 0; i < bytes; i++) | |
1010 | { | |
91d6fa6a | 1011 | int addr_index = big_p ? bytes - i - 1 : i; |
8c603c85 | 1012 | |
91d6fa6a | 1013 | addr[addr_index] = data & 0xff; |
8c603c85 NC |
1014 | data >>= 8; |
1015 | } | |
1016 | } | |
1017 | ||
0e3c1eeb | 1018 | uint64_t |
0a1b45a2 | 1019 | bfd_get_bits (const void *p, int bits, bool big_p) |
8c603c85 | 1020 | { |
a50b1753 | 1021 | const bfd_byte *addr = (const bfd_byte *) p; |
0e3c1eeb | 1022 | uint64_t data; |
8c603c85 NC |
1023 | int i; |
1024 | int bytes; | |
1025 | ||
1026 | if (bits % 8 != 0) | |
1027 | abort (); | |
1028 | ||
1029 | data = 0; | |
1030 | bytes = bits / 8; | |
1031 | for (i = 0; i < bytes; i++) | |
1032 | { | |
91d6fa6a | 1033 | int addr_index = big_p ? i : bytes - i - 1; |
509945ae | 1034 | |
91d6fa6a | 1035 | data = (data << 8) | addr[addr_index]; |
8c603c85 NC |
1036 | } |
1037 | ||
1038 | return data; | |
1039 | } | |
252b5132 RH |
1040 | \f |
1041 | /* Default implementation */ | |
1042 | ||
0a1b45a2 | 1043 | bool |
c58b9523 AM |
1044 | _bfd_generic_get_section_contents (bfd *abfd, |
1045 | sec_ptr section, | |
1046 | void *location, | |
1047 | file_ptr offset, | |
1048 | bfd_size_type count) | |
252b5132 | 1049 | { |
eea6121a | 1050 | bfd_size_type sz; |
0bff3f4b | 1051 | if (count == 0) |
0a1b45a2 | 1052 | return true; |
0bff3f4b | 1053 | |
4a114e3e L |
1054 | if (section->compress_status != COMPRESS_SECTION_NONE) |
1055 | { | |
4eca0228 | 1056 | _bfd_error_handler |
695344c0 | 1057 | /* xgettext:c-format */ |
871b3ab2 | 1058 | (_("%pB: unable to get decompressed section %pA"), |
4a114e3e L |
1059 | abfd, section); |
1060 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 1061 | return false; |
4a114e3e L |
1062 | } |
1063 | ||
20d8836e | 1064 | sz = bfd_get_section_limit_octets (abfd, section); |
e62071b6 | 1065 | if (offset + count < count |
0630b49c | 1066 | || offset + count > sz |
328ce951 L |
1067 | || (abfd->my_archive != NULL |
1068 | && !bfd_is_thin_archive (abfd->my_archive) | |
1069 | && ((ufile_ptr) section->filepos + offset + count | |
1070 | > arelt_size (abfd)))) | |
0bff3f4b ILT |
1071 | { |
1072 | bfd_set_error (bfd_error_invalid_operation); | |
0a1b45a2 | 1073 | return false; |
0bff3f4b ILT |
1074 | } |
1075 | ||
1076 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 | |
226f9f4f | 1077 | || bfd_read (location, count, abfd) != count) |
0a1b45a2 | 1078 | return false; |
0bff3f4b | 1079 | |
0a1b45a2 | 1080 | return true; |
252b5132 RH |
1081 | } |
1082 | ||
0a1b45a2 | 1083 | bool |
c58b9523 AM |
1084 | _bfd_generic_get_section_contents_in_window |
1085 | (bfd *abfd ATTRIBUTE_UNUSED, | |
1086 | sec_ptr section ATTRIBUTE_UNUSED, | |
1087 | bfd_window *w ATTRIBUTE_UNUSED, | |
1088 | file_ptr offset ATTRIBUTE_UNUSED, | |
1089 | bfd_size_type count ATTRIBUTE_UNUSED) | |
252b5132 RH |
1090 | { |
1091 | #ifdef USE_MMAP | |
eea6121a AM |
1092 | bfd_size_type sz; |
1093 | ||
252b5132 | 1094 | if (count == 0) |
0a1b45a2 | 1095 | return true; |
c58b9523 AM |
1096 | if (abfd->xvec->_bfd_get_section_contents |
1097 | != _bfd_generic_get_section_contents) | |
252b5132 RH |
1098 | { |
1099 | /* We don't know what changes the bfd's get_section_contents | |
1100 | method may have to make. So punt trying to map the file | |
1101 | window, and let get_section_contents do its thing. */ | |
1102 | /* @@ FIXME : If the internal window has a refcount of 1 and was | |
1103 | allocated with malloc instead of mmap, just reuse it. */ | |
1104 | bfd_free_window (w); | |
c58b9523 | 1105 | w->i = bfd_zmalloc (sizeof (bfd_window_internal)); |
252b5132 | 1106 | if (w->i == NULL) |
0a1b45a2 | 1107 | return false; |
c58b9523 | 1108 | w->i->data = bfd_malloc (count); |
252b5132 RH |
1109 | if (w->i->data == NULL) |
1110 | { | |
1111 | free (w->i); | |
1112 | w->i = NULL; | |
0a1b45a2 | 1113 | return false; |
252b5132 RH |
1114 | } |
1115 | w->i->mapped = 0; | |
1116 | w->i->refcount = 1; | |
1117 | w->size = w->i->size = count; | |
1118 | w->data = w->i->data; | |
1119 | return bfd_get_section_contents (abfd, section, w->data, offset, count); | |
1120 | } | |
e57278ef AM |
1121 | if (abfd->direction != write_direction && section->rawsize != 0) |
1122 | sz = section->rawsize; | |
1123 | else | |
1124 | sz = section->size; | |
328ce951 L |
1125 | if (offset + count < count |
1126 | || offset + count > sz | |
1127 | || (abfd->my_archive != NULL | |
1128 | && !bfd_is_thin_archive (abfd->my_archive) | |
1129 | && ((ufile_ptr) section->filepos + offset + count | |
1130 | > arelt_size (abfd))) | |
82e51918 | 1131 | || ! bfd_get_file_window (abfd, section->filepos + offset, count, w, |
0a1b45a2 AM |
1132 | true)) |
1133 | return false; | |
1134 | return true; | |
252b5132 RH |
1135 | #else |
1136 | abort (); | |
1137 | #endif | |
1138 | } | |
1139 | ||
1140 | /* This generic function can only be used in implementations where creating | |
1141 | NEW sections is disallowed. It is useful in patching existing sections | |
1142 | in read-write files, though. See other set_section_contents functions | |
1143 | to see why it doesn't work for new sections. */ | |
0a1b45a2 | 1144 | bool |
c58b9523 AM |
1145 | _bfd_generic_set_section_contents (bfd *abfd, |
1146 | sec_ptr section, | |
0f867abe | 1147 | const void *location, |
c58b9523 AM |
1148 | file_ptr offset, |
1149 | bfd_size_type count) | |
252b5132 RH |
1150 | { |
1151 | if (count == 0) | |
0a1b45a2 | 1152 | return true; |
252b5132 | 1153 | |
dc810e39 | 1154 | if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0 |
226f9f4f | 1155 | || bfd_write (location, count, abfd) != count) |
0a1b45a2 | 1156 | return false; |
252b5132 | 1157 | |
0a1b45a2 | 1158 | return true; |
252b5132 RH |
1159 | } |
1160 | ||
1161 | /* | |
1162 | INTERNAL_FUNCTION | |
1163 | bfd_log2 | |
1164 | ||
1165 | SYNOPSIS | |
dc810e39 | 1166 | unsigned int bfd_log2 (bfd_vma x); |
252b5132 RH |
1167 | |
1168 | DESCRIPTION | |
1169 | Return the log base 2 of the value supplied, rounded up. E.g., an | |
dc810e39 | 1170 | @var{x} of 1025 returns 11. A @var{x} of 0 returns 0. |
252b5132 RH |
1171 | */ |
1172 | ||
1173 | unsigned int | |
c58b9523 | 1174 | bfd_log2 (bfd_vma x) |
252b5132 RH |
1175 | { |
1176 | unsigned int result = 0; | |
1177 | ||
9e6619e2 AM |
1178 | if (x <= 1) |
1179 | return result; | |
1180 | --x; | |
1181 | do | |
252b5132 | 1182 | ++result; |
9e6619e2 | 1183 | while ((x >>= 1) != 0); |
252b5132 RH |
1184 | return result; |
1185 | } | |
1186 | ||
0a1b45a2 | 1187 | bool |
c58b9523 | 1188 | bfd_generic_is_local_label_name (bfd *abfd, const char *name) |
252b5132 RH |
1189 | { |
1190 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; | |
1191 | ||
b34976b6 | 1192 | return name[0] == locals_prefix; |
252b5132 RH |
1193 | } |
1194 | ||
c0c28ab8 L |
1195 | /* Helper function for reading uleb128 encoded data. */ |
1196 | ||
1197 | bfd_vma | |
4265548c PA |
1198 | _bfd_read_unsigned_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
1199 | bfd_byte *buf, | |
1200 | unsigned int *bytes_read_ptr) | |
c0c28ab8 L |
1201 | { |
1202 | bfd_vma result; | |
1203 | unsigned int num_read; | |
f075ee0c | 1204 | unsigned int shift; |
f68912e8 | 1205 | bfd_byte byte; |
c0c28ab8 L |
1206 | |
1207 | result = 0; | |
1208 | shift = 0; | |
1209 | num_read = 0; | |
1210 | do | |
1211 | { | |
f075ee0c | 1212 | byte = bfd_get_8 (abfd, buf); |
c0c28ab8 L |
1213 | buf++; |
1214 | num_read++; | |
7b54cadd AM |
1215 | if (shift < 8 * sizeof (result)) |
1216 | { | |
1217 | result |= (((bfd_vma) byte & 0x7f) << shift); | |
1218 | shift += 7; | |
1219 | } | |
c0c28ab8 L |
1220 | } |
1221 | while (byte & 0x80); | |
1222 | *bytes_read_ptr = num_read; | |
1223 | return result; | |
1224 | } | |
1225 | ||
574ec108 | 1226 | /* Read in a LEB128 encoded value from ABFD starting at *PTR. |
f64e188b | 1227 | If SIGN is true, return a signed LEB128 value. |
574ec108 | 1228 | *PTR is incremented by the number of bytes read. |
f64e188b NC |
1229 | No bytes will be read at address END or beyond. */ |
1230 | ||
1231 | bfd_vma | |
4265548c | 1232 | _bfd_safe_read_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
574ec108 | 1233 | bfd_byte **ptr, |
0a1b45a2 | 1234 | bool sign, |
4265548c | 1235 | const bfd_byte * const end) |
f64e188b NC |
1236 | { |
1237 | bfd_vma result = 0; | |
f64e188b | 1238 | unsigned int shift = 0; |
f68912e8 | 1239 | bfd_byte byte = 0; |
574ec108 | 1240 | bfd_byte *data = *ptr; |
f64e188b NC |
1241 | |
1242 | while (data < end) | |
1243 | { | |
1244 | byte = bfd_get_8 (abfd, data); | |
1245 | data++; | |
7b54cadd AM |
1246 | if (shift < 8 * sizeof (result)) |
1247 | { | |
1248 | result |= ((bfd_vma) (byte & 0x7f)) << shift; | |
1249 | shift += 7; | |
1250 | } | |
f64e188b NC |
1251 | if ((byte & 0x80) == 0) |
1252 | break; | |
1253 | } | |
1254 | ||
574ec108 | 1255 | *ptr = data; |
f64e188b NC |
1256 | |
1257 | if (sign && (shift < 8 * sizeof (result)) && (byte & 0x40)) | |
29f628db | 1258 | result |= -((bfd_vma) 1 << shift); |
f64e188b NC |
1259 | |
1260 | return result; | |
1261 | } | |
1262 | ||
c0c28ab8 L |
1263 | /* Helper function for reading sleb128 encoded data. */ |
1264 | ||
1265 | bfd_signed_vma | |
4265548c PA |
1266 | _bfd_read_signed_leb128 (bfd *abfd ATTRIBUTE_UNUSED, |
1267 | bfd_byte *buf, | |
1268 | unsigned int *bytes_read_ptr) | |
c0c28ab8 L |
1269 | { |
1270 | bfd_vma result; | |
f075ee0c AM |
1271 | unsigned int shift; |
1272 | unsigned int num_read; | |
f68912e8 | 1273 | bfd_byte byte; |
c0c28ab8 L |
1274 | |
1275 | result = 0; | |
1276 | shift = 0; | |
1277 | num_read = 0; | |
1278 | do | |
1279 | { | |
f075ee0c | 1280 | byte = bfd_get_8 (abfd, buf); |
c0c28ab8 L |
1281 | buf ++; |
1282 | num_read ++; | |
7b54cadd AM |
1283 | if (shift < 8 * sizeof (result)) |
1284 | { | |
1285 | result |= (((bfd_vma) byte & 0x7f) << shift); | |
1286 | shift += 7; | |
1287 | } | |
c0c28ab8 L |
1288 | } |
1289 | while (byte & 0x80); | |
f075ee0c | 1290 | if (shift < 8 * sizeof (result) && (byte & 0x40)) |
c0c28ab8 L |
1291 | result |= (((bfd_vma) -1) << shift); |
1292 | *bytes_read_ptr = num_read; | |
1293 | return result; | |
1294 | } | |
5420f73d | 1295 | |
7d81bc93 JL |
1296 | /* Write VAL in uleb128 format to P. |
1297 | END indicates the last byte of allocated space for the uleb128 value to fit | |
1298 | in. | |
1299 | Return a pointer to the byte following the last byte that was written, or | |
1300 | NULL if the uleb128 value does not fit in the allocated space between P and | |
1301 | END. */ | |
1302 | bfd_byte * | |
1303 | _bfd_write_unsigned_leb128 (bfd_byte *p, bfd_byte *end, bfd_vma val) | |
1304 | { | |
1305 | bfd_byte c; | |
1306 | do | |
1307 | { | |
1308 | if (p > end) | |
1309 | return NULL; | |
1310 | c = val & 0x7f; | |
1311 | val >>= 7; | |
1312 | if (val) | |
1313 | c |= 0x80; | |
1314 | *(p++) = c; | |
1315 | } | |
1316 | while (val); | |
1317 | return p; | |
1318 | } | |
1319 | ||
0a1b45a2 | 1320 | bool |
ccd2ec6a L |
1321 | _bfd_generic_init_private_section_data (bfd *ibfd ATTRIBUTE_UNUSED, |
1322 | asection *isec ATTRIBUTE_UNUSED, | |
1323 | bfd *obfd ATTRIBUTE_UNUSED, | |
1324 | asection *osec ATTRIBUTE_UNUSED, | |
1325 | struct bfd_link_info *link_info ATTRIBUTE_UNUSED) | |
1326 | { | |
0a1b45a2 | 1327 | return true; |
ccd2ec6a | 1328 | } |