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