]>
Commit | Line | Data |
---|---|---|
0d552306 | 1 | /* Assorted BFD support routines, only used internally. |
f4bd7a8f | 2 | Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc. |
f58809fd | 3 | Written by Cygnus Support. |
4a81b561 | 4 | |
f58809fd | 5 | This file is part of BFD, the Binary File Descriptor library. |
4a81b561 | 6 | |
f58809fd | 7 | This program is free software; you can redistribute it and/or modify |
4a81b561 | 8 | it under the terms of the GNU General Public License as published by |
f58809fd SC |
9 | the Free Software Foundation; either version 2 of the License, or |
10 | (at your option) any later version. | |
4a81b561 | 11 | |
f58809fd | 12 | This program is distributed in the hope that it will be useful, |
4a81b561 DHW |
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
f58809fd SC |
18 | along with this program; if not, write to the Free Software |
19 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
4a81b561 | 20 | |
4a81b561 | 21 | #include "bfd.h" |
f58809fd | 22 | #include "sysdep.h" |
4a81b561 DHW |
23 | #include "libbfd.h" |
24 | ||
f8e01940 JG |
25 | /* |
26 | SECTION | |
17ecba1a | 27 | Internal functions |
f8e01940 JG |
28 | |
29 | DESCRIPTION | |
c188b0be | 30 | These routines are used within BFD. |
f8e01940 JG |
31 | They are not intended for export, but are documented here for |
32 | completeness. | |
33 | */ | |
4a81b561 | 34 | |
6812b607 ILT |
35 | /* A routine which is used in target vectors for unsupported |
36 | operations. */ | |
4a81b561 | 37 | |
728472f1 | 38 | /*ARGSUSED*/ |
4a81b561 | 39 | boolean |
f4bd7a8f DM |
40 | bfd_false (ignore) |
41 | bfd *ignore; | |
4a81b561 | 42 | { |
6812b607 | 43 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
44 | return false; |
45 | } | |
46 | ||
6812b607 ILT |
47 | /* A routine which is used in target vectors for supported operations |
48 | which do not actually do anything. */ | |
49 | ||
728472f1 | 50 | /*ARGSUSED*/ |
4a81b561 | 51 | boolean |
f4bd7a8f DM |
52 | bfd_true (ignore) |
53 | bfd *ignore; | |
4a81b561 DHW |
54 | { |
55 | return true; | |
56 | } | |
57 | ||
6812b607 ILT |
58 | /* A routine which is used in target vectors for unsupported |
59 | operations which return a pointer value. */ | |
60 | ||
728472f1 | 61 | /*ARGSUSED*/ |
d0ec7a8e | 62 | PTR |
f4bd7a8f DM |
63 | bfd_nullvoidptr (ignore) |
64 | bfd *ignore; | |
4a81b561 | 65 | { |
6812b607 ILT |
66 | bfd_set_error (bfd_error_invalid_operation); |
67 | return NULL; | |
4a81b561 | 68 | } |
fc723380 | 69 | |
728472f1 | 70 | /*ARGSUSED*/ |
4a81b561 | 71 | int |
f4bd7a8f DM |
72 | bfd_0 (ignore) |
73 | bfd *ignore; | |
4a81b561 DHW |
74 | { |
75 | return 0; | |
76 | } | |
fc723380 | 77 | |
728472f1 | 78 | /*ARGSUSED*/ |
4a81b561 | 79 | unsigned int |
f4bd7a8f DM |
80 | bfd_0u (ignore) |
81 | bfd *ignore; | |
4a81b561 DHW |
82 | { |
83 | return 0; | |
84 | } | |
85 | ||
326e32d7 ILT |
86 | /*ARGUSED*/ |
87 | long | |
88 | bfd_0l (ignore) | |
89 | bfd *ignore; | |
90 | { | |
91 | return 0; | |
92 | } | |
93 | ||
6812b607 ILT |
94 | /* A routine which is used in target vectors for unsupported |
95 | operations which return -1 on error. */ | |
96 | ||
97 | /*ARGSUSED*/ | |
98 | long | |
99 | _bfd_n1 (ignore_abfd) | |
100 | bfd *ignore_abfd; | |
101 | { | |
102 | bfd_set_error (bfd_error_invalid_operation); | |
103 | return -1; | |
104 | } | |
105 | ||
728472f1 | 106 | /*ARGSUSED*/ |
4a81b561 | 107 | void |
f4bd7a8f DM |
108 | bfd_void (ignore) |
109 | bfd *ignore; | |
4a81b561 DHW |
110 | { |
111 | } | |
112 | ||
728472f1 | 113 | /*ARGSUSED*/ |
4a81b561 | 114 | boolean |
6812b607 | 115 | _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) |
f4bd7a8f DM |
116 | bfd *ignore_core_bfd; |
117 | bfd *ignore_exec_bfd; | |
4a81b561 | 118 | { |
120f5bd9 | 119 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
120 | return false; |
121 | } | |
122 | ||
6812b607 ILT |
123 | /* Routine to handle core_file_failing_command entry point for targets |
124 | without core file support. */ | |
4a81b561 | 125 | |
728472f1 | 126 | /*ARGSUSED*/ |
4a81b561 | 127 | char * |
6812b607 | 128 | _bfd_nocore_core_file_failing_command (ignore_abfd) |
f4bd7a8f | 129 | bfd *ignore_abfd; |
4a81b561 | 130 | { |
6812b607 | 131 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
132 | return (char *)NULL; |
133 | } | |
134 | ||
6812b607 ILT |
135 | /* Routine to handle core_file_failing_signal entry point for targets |
136 | without core file support. */ | |
137 | ||
728472f1 | 138 | /*ARGSUSED*/ |
4a81b561 | 139 | int |
6812b607 | 140 | _bfd_nocore_core_file_failing_signal (ignore_abfd) |
f4bd7a8f | 141 | bfd *ignore_abfd; |
4a81b561 | 142 | { |
6812b607 | 143 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
144 | return 0; |
145 | } | |
146 | ||
728472f1 | 147 | /*ARGSUSED*/ |
4a81b561 | 148 | bfd_target * |
f4bd7a8f DM |
149 | _bfd_dummy_target (ignore_abfd) |
150 | bfd *ignore_abfd; | |
4a81b561 DHW |
151 | { |
152 | return 0; | |
153 | } | |
154 | \f | |
4a81b561 | 155 | |
f4bd7a8f DM |
156 | #ifndef bfd_zmalloc |
157 | /* allocate and clear storage */ | |
4a81b561 | 158 | |
4a81b561 | 159 | char * |
f4bd7a8f DM |
160 | bfd_zmalloc (size) |
161 | bfd_size_type size; | |
4a81b561 | 162 | { |
120f5bd9 | 163 | char *ptr = (char *) malloc ((size_t) size); |
4a81b561 | 164 | |
120f5bd9 JL |
165 | if (ptr && size) |
166 | memset(ptr, 0, (size_t) size); | |
4a81b561 DHW |
167 | |
168 | return ptr; | |
169 | } | |
f4bd7a8f | 170 | #endif /* bfd_zmalloc */ |
4a81b561 DHW |
171 | \f |
172 | /* Some IO code */ | |
173 | ||
174 | ||
175 | /* Note that archive entries don't have streams; they share their parent's. | |
f58809fd | 176 | This allows someone to play with the iostream behind BFD's back. |
4a81b561 DHW |
177 | |
178 | Also, note that the origin pointer points to the beginning of a file's | |
179 | contents (0 for non-archive elements). For archive entries this is the | |
180 | first octet in the file, NOT the beginning of the archive header. */ | |
181 | ||
7ed4093a | 182 | static |
f4bd7a8f DM |
183 | int |
184 | real_read (where, a,b, file) | |
185 | PTR where; | |
186 | int a; | |
187 | int b; | |
188 | FILE *file; | |
7ed4093a SC |
189 | { |
190 | return fread(where, a,b,file); | |
191 | } | |
f4bd7a8f | 192 | |
120f5bd9 JL |
193 | /* Return value is amount read (FIXME: how are errors and end of file dealt |
194 | with? We never call bfd_set_error, which is probably a mistake). */ | |
195 | ||
23b0b558 | 196 | bfd_size_type |
f4bd7a8f DM |
197 | bfd_read (ptr, size, nitems, abfd) |
198 | PTR ptr; | |
199 | bfd_size_type size; | |
200 | bfd_size_type nitems; | |
201 | bfd *abfd; | |
4a81b561 | 202 | { |
0d552306 KR |
203 | int nread; |
204 | nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd)); | |
205 | #ifdef FILE_OFFSET_IS_CHAR_INDEX | |
206 | if (nread > 0) | |
207 | abfd->where += nread; | |
208 | #endif | |
120f5bd9 JL |
209 | |
210 | /* Set bfd_error if we did not read as much data as we expected. | |
211 | ||
212 | If the read failed due to an error set the bfd_error_system_call, | |
213 | else set bfd_error_file_truncated. | |
214 | ||
215 | A BFD backend may wish to override bfd_error_file_truncated to | |
216 | provide something more useful (eg. no_symbols or wrong_format). */ | |
217 | if (nread < (int)(size * nitems)) | |
218 | { | |
219 | if (ferror (bfd_cache_lookup (abfd))) | |
220 | bfd_set_error (bfd_error_system_call); | |
221 | else | |
222 | bfd_set_error (bfd_error_file_truncated); | |
223 | } | |
224 | ||
0d552306 | 225 | return nread; |
4a81b561 DHW |
226 | } |
227 | ||
23b0b558 | 228 | bfd_size_type |
df34342b ILT |
229 | bfd_write (ptr, size, nitems, abfd) |
230 | CONST PTR ptr; | |
231 | bfd_size_type size; | |
232 | bfd_size_type nitems; | |
233 | bfd *abfd; | |
4a81b561 | 234 | { |
df34342b | 235 | int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd)); |
0d552306 KR |
236 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
237 | if (nwrote > 0) | |
238 | abfd->where += nwrote; | |
239 | #endif | |
df34342b ILT |
240 | if (nwrote != size * nitems) |
241 | { | |
242 | #ifdef ENOSPC | |
243 | if (nwrote >= 0) | |
244 | errno = ENOSPC; | |
245 | #endif | |
120f5bd9 | 246 | bfd_set_error (bfd_error_system_call); |
df34342b | 247 | } |
0d552306 | 248 | return nwrote; |
4a81b561 DHW |
249 | } |
250 | ||
f8e01940 JG |
251 | /* |
252 | INTERNAL_FUNCTION | |
253 | bfd_write_bigendian_4byte_int | |
254 | ||
255 | SYNOPSIS | |
256 | void bfd_write_bigendian_4byte_int(bfd *abfd, int i); | |
257 | ||
258 | DESCRIPTION | |
c188b0be DM |
259 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
260 | endian order regardless of what else is going on. This is useful in | |
f8e01940 | 261 | archives. |
1e310759 | 262 | |
1e310759 | 263 | */ |
f58809fd | 264 | void |
f4bd7a8f DM |
265 | bfd_write_bigendian_4byte_int (abfd, i) |
266 | bfd *abfd; | |
267 | int i; | |
f58809fd | 268 | { |
1e310759 | 269 | bfd_byte buffer[4]; |
b5b4294e | 270 | bfd_putb32(i, buffer); |
4002f18a ILT |
271 | if (bfd_write((PTR)buffer, 4, 1, abfd) != 4) |
272 | abort (); | |
f58809fd SC |
273 | } |
274 | ||
0d552306 | 275 | long |
f4bd7a8f DM |
276 | bfd_tell (abfd) |
277 | bfd *abfd; | |
0d552306 KR |
278 | { |
279 | file_ptr ptr; | |
280 | ||
281 | ptr = ftell (bfd_cache_lookup(abfd)); | |
282 | ||
283 | if (abfd->my_archive) | |
284 | ptr -= abfd->origin; | |
285 | abfd->where = ptr; | |
286 | return ptr; | |
287 | } | |
288 | ||
b5b4294e | 289 | int |
f4bd7a8f DM |
290 | bfd_flush (abfd) |
291 | bfd *abfd; | |
b5b4294e JG |
292 | { |
293 | return fflush (bfd_cache_lookup(abfd)); | |
294 | } | |
295 | ||
296 | int | |
f4bd7a8f DM |
297 | bfd_stat (abfd, statbuf) |
298 | bfd *abfd; | |
299 | struct stat *statbuf; | |
b5b4294e JG |
300 | { |
301 | return fstat (fileno(bfd_cache_lookup(abfd)), statbuf); | |
302 | } | |
303 | ||
120f5bd9 JL |
304 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error |
305 | can retrieve the error code). */ | |
306 | ||
4a81b561 | 307 | int |
f4bd7a8f DM |
308 | bfd_seek (abfd, position, direction) |
309 | bfd * CONST abfd; | |
310 | CONST file_ptr position; | |
311 | CONST int direction; | |
4a81b561 | 312 | { |
0d552306 KR |
313 | int result; |
314 | FILE *f; | |
e5b02860 | 315 | file_ptr file_position; |
0d552306 KR |
316 | /* For the time being, a BFD may not seek to it's end. The problem |
317 | is that we don't easily have a way to recognize the end of an | |
318 | element in an archive. */ | |
319 | ||
320 | BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); | |
321 | ||
b31d06ca | 322 | if (direction == SEEK_CUR && position == 0) |
0d552306 KR |
323 | return 0; |
324 | #ifdef FILE_OFFSET_IS_CHAR_INDEX | |
b6090f4d JG |
325 | if (abfd->format != bfd_archive && abfd->my_archive == 0) |
326 | { | |
4c3721d5 | 327 | #if 0 |
b6090f4d JG |
328 | /* Explanation for this code: I'm only about 95+% sure that the above |
329 | conditions are sufficient and that all i/o calls are properly | |
330 | adjusting the `where' field. So this is sort of an `assert' | |
331 | that the `where' field is correct. If we can go a while without | |
332 | tripping the abort, we can probably safely disable this code, | |
333 | so that the real optimizations happen. */ | |
334 | file_ptr where_am_i_now; | |
335 | where_am_i_now = ftell (bfd_cache_lookup (abfd)); | |
336 | if (abfd->my_archive) | |
337 | where_am_i_now -= abfd->origin; | |
338 | if (where_am_i_now != abfd->where) | |
339 | abort (); | |
340 | #endif | |
341 | if (direction == SEEK_SET && position == abfd->where) | |
342 | return 0; | |
343 | } | |
344 | else | |
345 | { | |
346 | /* We need something smarter to optimize access to archives. | |
347 | Currently, anything inside an archive is read via the file | |
348 | handle for the archive. Which means that a bfd_seek on one | |
349 | component affects the `current position' in the archive, as | |
350 | well as in any other component. | |
351 | ||
352 | It might be sufficient to put a spike through the cache | |
353 | abstraction, and look to the archive for the file position, | |
354 | but I think we should try for something cleaner. | |
355 | ||
356 | In the meantime, no optimization for archives. */ | |
357 | } | |
0d552306 | 358 | #endif |
4a81b561 | 359 | |
0d552306 | 360 | f = bfd_cache_lookup (abfd); |
e5b02860 | 361 | file_position = position; |
0d552306 | 362 | if (direction == SEEK_SET && abfd->my_archive != NULL) |
e5b02860 KR |
363 | file_position += abfd->origin; |
364 | ||
365 | result = fseek (f, file_position, direction); | |
366 | ||
367 | if (result != 0) | |
df34342b ILT |
368 | { |
369 | /* Force redetermination of `where' field. */ | |
370 | bfd_tell (abfd); | |
120f5bd9 | 371 | bfd_set_error (bfd_error_system_call); |
df34342b | 372 | } |
0d552306 KR |
373 | else |
374 | { | |
e5b02860 KR |
375 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
376 | /* Adjust `where' field. */ | |
377 | if (direction == SEEK_SET) | |
378 | abfd->where = position; | |
379 | else | |
380 | abfd->where += position; | |
381 | #endif | |
0d552306 | 382 | } |
e5b02860 | 383 | return result; |
4a81b561 DHW |
384 | } |
385 | \f | |
386 | /** Make a string table */ | |
387 | ||
6f715d66 SC |
388 | /*>bfd.h< |
389 | Add string to table pointed to by table, at location starting with free_ptr. | |
4a81b561 DHW |
390 | resizes the table if necessary (if it's NULL, creates it, ignoring |
391 | table_length). Updates free_ptr, table, table_length */ | |
392 | ||
393 | boolean | |
f4bd7a8f DM |
394 | bfd_add_to_string_table (table, new_string, table_length, free_ptr) |
395 | char **table; | |
396 | char *new_string; | |
397 | unsigned int *table_length; | |
398 | char **free_ptr; | |
4a81b561 DHW |
399 | { |
400 | size_t string_length = strlen (new_string) + 1; /* include null here */ | |
401 | char *base = *table; | |
402 | size_t space_length = *table_length; | |
403 | unsigned int offset = (base ? *free_ptr - base : 0); | |
404 | ||
405 | if (base == NULL) { | |
406 | /* Avoid a useless regrow if we can (but of course we still | |
f4bd7a8f | 407 | take it next time). */ |
4a81b561 | 408 | space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ? |
6f715d66 | 409 | DEFAULT_STRING_SPACE_SIZE : string_length+1); |
f4bd7a8f | 410 | base = bfd_zmalloc ((bfd_size_type) space_length); |
4a81b561 DHW |
411 | |
412 | if (base == NULL) { | |
120f5bd9 | 413 | bfd_set_error (bfd_error_no_memory); |
4a81b561 DHW |
414 | return false; |
415 | } | |
416 | } | |
417 | ||
418 | if ((size_t)(offset + string_length) >= space_length) { | |
419 | /* Make sure we will have enough space */ | |
420 | while ((size_t)(offset + string_length) >= space_length) | |
421 | space_length += space_length/2; /* grow by 50% */ | |
422 | ||
423 | base = (char *) realloc (base, space_length); | |
424 | if (base == NULL) { | |
120f5bd9 | 425 | bfd_set_error (bfd_error_no_memory); |
4a81b561 DHW |
426 | return false; |
427 | } | |
428 | ||
429 | } | |
430 | ||
431 | memcpy (base + offset, new_string, string_length); | |
432 | *table = base; | |
433 | *table_length = space_length; | |
434 | *free_ptr = base + offset + string_length; | |
435 | ||
436 | return true; | |
437 | } | |
438 | \f | |
439 | /** The do-it-yourself (byte) sex-change kit */ | |
440 | ||
441 | /* The middle letter e.g. get<b>short indicates Big or Little endian | |
442 | target machine. It doesn't matter what the byte order of the host | |
443 | machine is; these routines work for either. */ | |
444 | ||
445 | /* FIXME: Should these take a count argument? | |
446 | Answer ([email protected]): No, but perhaps they should be inline | |
6f715d66 SC |
447 | functions in swap.h #ifdef __GNUC__. |
448 | Gprof them later and find out. */ | |
449 | ||
f8e01940 JG |
450 | /* |
451 | FUNCTION | |
452 | bfd_put_size | |
453 | FUNCTION | |
454 | bfd_get_size | |
455 | ||
456 | DESCRIPTION | |
457 | These macros as used for reading and writing raw data in | |
458 | sections; each access (except for bytes) is vectored through | |
459 | the target format of the BFD and mangled accordingly. The | |
460 | mangling performs any necessary endian translations and | |
14e3c2e4 JK |
461 | removes alignment restrictions. Note that types accepted and |
462 | returned by these macros are identical so they can be swapped | |
17ecba1a DM |
463 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> |
464 | to either <<bfd_get_32>> or <<bfd_get_64>>. | |
f8e01940 | 465 | |
17ecba1a | 466 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a |
7e4db254 JK |
467 | system without prototypes, the caller is responsible for making |
468 | sure that is true, with a cast if necessary. We don't cast | |
17ecba1a DM |
469 | them in the macro definitions because that would prevent <<lint>> |
470 | or <<gcc -Wall>> from detecting sins such as passing a pointer. | |
471 | To detect calling these with less than a <<bfd_vma>>, use | |
472 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. | |
7e4db254 | 473 | |
da3cd00a KR |
474 | . |
475 | .{* Byte swapping macros for user section data. *} | |
476 | . | |
f8e01940 | 477 | .#define bfd_put_8(abfd, val, ptr) \ |
4c3721d5 | 478 | . (*((unsigned char *)(ptr)) = (unsigned char)(val)) |
da3cd00a KR |
479 | .#define bfd_put_signed_8 \ |
480 | . bfd_put_8 | |
f8e01940 | 481 | .#define bfd_get_8(abfd, ptr) \ |
da3cd00a KR |
482 | . (*(unsigned char *)(ptr)) |
483 | .#define bfd_get_signed_8(abfd, ptr) \ | |
484 | . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80) | |
485 | . | |
f8e01940 | 486 | .#define bfd_put_16(abfd, val, ptr) \ |
7e4db254 | 487 | . BFD_SEND(abfd, bfd_putx16, ((val),(ptr))) |
da3cd00a KR |
488 | .#define bfd_put_signed_16 \ |
489 | . bfd_put_16 | |
f8e01940 JG |
490 | .#define bfd_get_16(abfd, ptr) \ |
491 | . BFD_SEND(abfd, bfd_getx16, (ptr)) | |
14e3c2e4 JK |
492 | .#define bfd_get_signed_16(abfd, ptr) \ |
493 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) | |
da3cd00a | 494 | . |
f8e01940 | 495 | .#define bfd_put_32(abfd, val, ptr) \ |
7e4db254 | 496 | . BFD_SEND(abfd, bfd_putx32, ((val),(ptr))) |
da3cd00a KR |
497 | .#define bfd_put_signed_32 \ |
498 | . bfd_put_32 | |
f8e01940 JG |
499 | .#define bfd_get_32(abfd, ptr) \ |
500 | . BFD_SEND(abfd, bfd_getx32, (ptr)) | |
14e3c2e4 JK |
501 | .#define bfd_get_signed_32(abfd, ptr) \ |
502 | . BFD_SEND(abfd, bfd_getx_signed_32, (ptr)) | |
da3cd00a | 503 | . |
f8e01940 | 504 | .#define bfd_put_64(abfd, val, ptr) \ |
7e4db254 | 505 | . BFD_SEND(abfd, bfd_putx64, ((val), (ptr))) |
da3cd00a KR |
506 | .#define bfd_put_signed_64 \ |
507 | . bfd_put_64 | |
f8e01940 JG |
508 | .#define bfd_get_64(abfd, ptr) \ |
509 | . BFD_SEND(abfd, bfd_getx64, (ptr)) | |
14e3c2e4 JK |
510 | .#define bfd_get_signed_64(abfd, ptr) \ |
511 | . BFD_SEND(abfd, bfd_getx_signed_64, (ptr)) | |
da3cd00a | 512 | . |
f8e01940 JG |
513 | */ |
514 | ||
515 | /* | |
516 | FUNCTION | |
517 | bfd_h_put_size | |
f8e01940 JG |
518 | bfd_h_get_size |
519 | ||
520 | DESCRIPTION | |
521 | These macros have the same function as their <<bfd_get_x>> | |
c188b0be | 522 | bretheren, except that they are used for removing information |
f8e01940 JG |
523 | for the header records of object files. Believe it or not, |
524 | some object files keep their header records in big endian | |
c188b0be | 525 | order and their data in little endian order. |
da3cd00a KR |
526 | . |
527 | .{* Byte swapping macros for file header data. *} | |
528 | . | |
f8e01940 | 529 | .#define bfd_h_put_8(abfd, val, ptr) \ |
da3cd00a KR |
530 | . bfd_put_8 (abfd, val, ptr) |
531 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ | |
532 | . bfd_put_8 (abfd, val, ptr) | |
f8e01940 | 533 | .#define bfd_h_get_8(abfd, ptr) \ |
da3cd00a KR |
534 | . bfd_get_8 (abfd, ptr) |
535 | .#define bfd_h_get_signed_8(abfd, ptr) \ | |
536 | . bfd_get_signed_8 (abfd, ptr) | |
537 | . | |
f8e01940 JG |
538 | .#define bfd_h_put_16(abfd, val, ptr) \ |
539 | . BFD_SEND(abfd, bfd_h_putx16,(val,ptr)) | |
da3cd00a KR |
540 | .#define bfd_h_put_signed_16 \ |
541 | . bfd_h_put_16 | |
f8e01940 JG |
542 | .#define bfd_h_get_16(abfd, ptr) \ |
543 | . BFD_SEND(abfd, bfd_h_getx16,(ptr)) | |
14e3c2e4 JK |
544 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
545 | . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr)) | |
da3cd00a | 546 | . |
f8e01940 JG |
547 | .#define bfd_h_put_32(abfd, val, ptr) \ |
548 | . BFD_SEND(abfd, bfd_h_putx32,(val,ptr)) | |
da3cd00a KR |
549 | .#define bfd_h_put_signed_32 \ |
550 | . bfd_h_put_32 | |
f8e01940 JG |
551 | .#define bfd_h_get_32(abfd, ptr) \ |
552 | . BFD_SEND(abfd, bfd_h_getx32,(ptr)) | |
14e3c2e4 JK |
553 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
554 | . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr)) | |
da3cd00a | 555 | . |
f8e01940 JG |
556 | .#define bfd_h_put_64(abfd, val, ptr) \ |
557 | . BFD_SEND(abfd, bfd_h_putx64,(val, ptr)) | |
da3cd00a KR |
558 | .#define bfd_h_put_signed_64 \ |
559 | . bfd_h_put_64 | |
f8e01940 JG |
560 | .#define bfd_h_get_64(abfd, ptr) \ |
561 | . BFD_SEND(abfd, bfd_h_getx64,(ptr)) | |
14e3c2e4 JK |
562 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
563 | . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr)) | |
da3cd00a | 564 | . |
f8e01940 | 565 | */ |
4a81b561 | 566 | |
14e3c2e4 | 567 | /* Sign extension to bfd_signed_vma. */ |
df34342b ILT |
568 | #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000) |
569 | #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000) | |
96ad107b | 570 | #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32) |
df34342b ILT |
571 | #define COERCE64(x) \ |
572 | (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION) | |
14e3c2e4 | 573 | |
f58809fd | 574 | bfd_vma |
f4bd7a8f DM |
575 | bfd_getb16 (addr) |
576 | register const bfd_byte *addr; | |
4a81b561 | 577 | { |
f4bd7a8f | 578 | return (addr[0] << 8) | addr[1]; |
4a81b561 DHW |
579 | } |
580 | ||
f58809fd | 581 | bfd_vma |
f4bd7a8f DM |
582 | bfd_getl16 (addr) |
583 | register const bfd_byte *addr; | |
4a81b561 | 584 | { |
f4bd7a8f | 585 | return (addr[1] << 8) | addr[0]; |
4a81b561 DHW |
586 | } |
587 | ||
14e3c2e4 | 588 | bfd_signed_vma |
f4bd7a8f DM |
589 | bfd_getb_signed_16 (addr) |
590 | register const bfd_byte *addr; | |
14e3c2e4 | 591 | { |
f4bd7a8f | 592 | return COERCE16((addr[0] << 8) | addr[1]); |
14e3c2e4 JK |
593 | } |
594 | ||
595 | bfd_signed_vma | |
f4bd7a8f DM |
596 | bfd_getl_signed_16 (addr) |
597 | register const bfd_byte *addr; | |
14e3c2e4 | 598 | { |
f4bd7a8f | 599 | return COERCE16((addr[1] << 8) | addr[0]); |
14e3c2e4 JK |
600 | } |
601 | ||
4a81b561 | 602 | void |
f4bd7a8f DM |
603 | bfd_putb16 (data, addr) |
604 | bfd_vma data; | |
605 | register bfd_byte *addr; | |
4a81b561 | 606 | { |
f4bd7a8f DM |
607 | addr[0] = (bfd_byte)(data >> 8); |
608 | addr[1] = (bfd_byte )data; | |
4a81b561 DHW |
609 | } |
610 | ||
611 | void | |
f4bd7a8f DM |
612 | bfd_putl16 (data, addr) |
613 | bfd_vma data; | |
614 | register bfd_byte *addr; | |
4a81b561 | 615 | { |
f4bd7a8f DM |
616 | addr[0] = (bfd_byte )data; |
617 | addr[1] = (bfd_byte)(data >> 8); | |
4a81b561 DHW |
618 | } |
619 | ||
f58809fd | 620 | bfd_vma |
b5b4294e | 621 | bfd_getb32 (addr) |
f4bd7a8f | 622 | register const bfd_byte *addr; |
4a81b561 | 623 | { |
f4bd7a8f DM |
624 | return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
625 | | addr[2]) << 8 | addr[3]; | |
4a81b561 DHW |
626 | } |
627 | ||
f58809fd | 628 | bfd_vma |
b5b4294e | 629 | bfd_getl32 (addr) |
f4bd7a8f | 630 | register const bfd_byte *addr; |
4a81b561 | 631 | { |
f4bd7a8f DM |
632 | return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
633 | | addr[1]) << 8 | addr[0]; | |
4a81b561 DHW |
634 | } |
635 | ||
14e3c2e4 | 636 | bfd_signed_vma |
b5b4294e | 637 | bfd_getb_signed_32 (addr) |
f4bd7a8f | 638 | register const bfd_byte *addr; |
14e3c2e4 | 639 | { |
f4bd7a8f DM |
640 | return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
641 | | addr[2]) << 8 | addr[3]); | |
14e3c2e4 JK |
642 | } |
643 | ||
644 | bfd_signed_vma | |
b5b4294e | 645 | bfd_getl_signed_32 (addr) |
f4bd7a8f | 646 | register const bfd_byte *addr; |
14e3c2e4 | 647 | { |
f4bd7a8f DM |
648 | return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
649 | | addr[1]) << 8 | addr[0]); | |
14e3c2e4 JK |
650 | } |
651 | ||
f58809fd | 652 | bfd_vma |
f4bd7a8f DM |
653 | bfd_getb64 (addr) |
654 | register const bfd_byte *addr; | |
7ed4093a | 655 | { |
b5b4294e | 656 | #ifdef BFD64 |
b6090f4d | 657 | bfd_vma low, high; |
536b27a5 | 658 | |
7ed4093a | 659 | high= ((((((((addr[0]) << 8) | |
6f715d66 SC |
660 | addr[1]) << 8) | |
661 | addr[2]) << 8) | | |
662 | addr[3]) ); | |
7ed4093a | 663 | |
df34342b | 664 | low = (((((((((bfd_vma)addr[4]) << 8) | |
6f715d66 SC |
665 | addr[5]) << 8) | |
666 | addr[6]) << 8) | | |
667 | addr[7])); | |
7ed4093a SC |
668 | |
669 | return high << 32 | low; | |
670 | #else | |
671 | BFD_FAIL(); | |
f58809fd | 672 | return 0; |
7ed4093a | 673 | #endif |
7ed4093a SC |
674 | } |
675 | ||
f58809fd | 676 | bfd_vma |
f4bd7a8f DM |
677 | bfd_getl64 (addr) |
678 | register const bfd_byte *addr; | |
7ed4093a | 679 | { |
b5b4294e | 680 | #ifdef BFD64 |
b6090f4d | 681 | bfd_vma low, high; |
7ed4093a | 682 | high= (((((((addr[7] << 8) | |
6f715d66 SC |
683 | addr[6]) << 8) | |
684 | addr[5]) << 8) | | |
685 | addr[4])); | |
7ed4093a | 686 | |
df34342b | 687 | low = ((((((((bfd_vma)addr[3] << 8) | |
6f715d66 SC |
688 | addr[2]) << 8) | |
689 | addr[1]) << 8) | | |
690 | addr[0]) ); | |
7ed4093a SC |
691 | |
692 | return high << 32 | low; | |
693 | #else | |
694 | BFD_FAIL(); | |
f58809fd | 695 | return 0; |
7ed4093a | 696 | #endif |
6f715d66 | 697 | |
7ed4093a SC |
698 | } |
699 | ||
14e3c2e4 | 700 | bfd_signed_vma |
f4bd7a8f DM |
701 | bfd_getb_signed_64 (addr) |
702 | register const bfd_byte *addr; | |
14e3c2e4 | 703 | { |
b5b4294e | 704 | #ifdef BFD64 |
14e3c2e4 JK |
705 | bfd_vma low, high; |
706 | ||
707 | high= ((((((((addr[0]) << 8) | | |
708 | addr[1]) << 8) | | |
709 | addr[2]) << 8) | | |
710 | addr[3]) ); | |
711 | ||
df34342b | 712 | low = (((((((((bfd_vma)addr[4]) << 8) | |
14e3c2e4 JK |
713 | addr[5]) << 8) | |
714 | addr[6]) << 8) | | |
715 | addr[7])); | |
716 | ||
717 | return COERCE64(high << 32 | low); | |
718 | #else | |
719 | BFD_FAIL(); | |
720 | return 0; | |
721 | #endif | |
14e3c2e4 JK |
722 | } |
723 | ||
724 | bfd_signed_vma | |
f4bd7a8f DM |
725 | bfd_getl_signed_64 (addr) |
726 | register const bfd_byte *addr; | |
14e3c2e4 | 727 | { |
b5b4294e | 728 | #ifdef BFD64 |
14e3c2e4 JK |
729 | bfd_vma low, high; |
730 | high= (((((((addr[7] << 8) | | |
731 | addr[6]) << 8) | | |
732 | addr[5]) << 8) | | |
733 | addr[4])); | |
734 | ||
df34342b | 735 | low = ((((((((bfd_vma)addr[3] << 8) | |
14e3c2e4 JK |
736 | addr[2]) << 8) | |
737 | addr[1]) << 8) | | |
738 | addr[0]) ); | |
739 | ||
740 | return COERCE64(high << 32 | low); | |
741 | #else | |
742 | BFD_FAIL(); | |
743 | return 0; | |
744 | #endif | |
14e3c2e4 JK |
745 | } |
746 | ||
4a81b561 | 747 | void |
f4bd7a8f DM |
748 | bfd_putb32 (data, addr) |
749 | bfd_vma data; | |
750 | register bfd_byte *addr; | |
4a81b561 | 751 | { |
6f715d66 SC |
752 | addr[0] = (bfd_byte)(data >> 24); |
753 | addr[1] = (bfd_byte)(data >> 16); | |
754 | addr[2] = (bfd_byte)(data >> 8); | |
755 | addr[3] = (bfd_byte)data; | |
4a81b561 DHW |
756 | } |
757 | ||
758 | void | |
f4bd7a8f DM |
759 | bfd_putl32 (data, addr) |
760 | bfd_vma data; | |
761 | register bfd_byte *addr; | |
4a81b561 | 762 | { |
6f715d66 SC |
763 | addr[0] = (bfd_byte)data; |
764 | addr[1] = (bfd_byte)(data >> 8); | |
765 | addr[2] = (bfd_byte)(data >> 16); | |
766 | addr[3] = (bfd_byte)(data >> 24); | |
4a81b561 | 767 | } |
f4bd7a8f | 768 | |
7ed4093a | 769 | void |
f4bd7a8f DM |
770 | bfd_putb64 (data, addr) |
771 | bfd_vma data; | |
772 | register bfd_byte *addr; | |
7ed4093a | 773 | { |
b5b4294e | 774 | #ifdef BFD64 |
536b27a5 SC |
775 | addr[0] = (bfd_byte)(data >> (7*8)); |
776 | addr[1] = (bfd_byte)(data >> (6*8)); | |
777 | addr[2] = (bfd_byte)(data >> (5*8)); | |
778 | addr[3] = (bfd_byte)(data >> (4*8)); | |
779 | addr[4] = (bfd_byte)(data >> (3*8)); | |
780 | addr[5] = (bfd_byte)(data >> (2*8)); | |
781 | addr[6] = (bfd_byte)(data >> (1*8)); | |
782 | addr[7] = (bfd_byte)(data >> (0*8)); | |
7ed4093a | 783 | #else |
536b27a5 | 784 | BFD_FAIL(); |
7ed4093a | 785 | #endif |
7ed4093a SC |
786 | } |
787 | ||
788 | void | |
f4bd7a8f DM |
789 | bfd_putl64 (data, addr) |
790 | bfd_vma data; | |
791 | register bfd_byte *addr; | |
7ed4093a | 792 | { |
b5b4294e | 793 | #ifdef BFD64 |
536b27a5 SC |
794 | addr[7] = (bfd_byte)(data >> (7*8)); |
795 | addr[6] = (bfd_byte)(data >> (6*8)); | |
796 | addr[5] = (bfd_byte)(data >> (5*8)); | |
797 | addr[4] = (bfd_byte)(data >> (4*8)); | |
798 | addr[3] = (bfd_byte)(data >> (3*8)); | |
799 | addr[2] = (bfd_byte)(data >> (2*8)); | |
800 | addr[1] = (bfd_byte)(data >> (1*8)); | |
801 | addr[0] = (bfd_byte)(data >> (0*8)); | |
7ed4093a | 802 | #else |
536b27a5 | 803 | BFD_FAIL(); |
7ed4093a | 804 | #endif |
7ed4093a | 805 | } |
2203f786 JG |
806 | \f |
807 | /* Default implementation */ | |
4a81b561 | 808 | |
2203f786 | 809 | boolean |
6812b607 | 810 | _bfd_generic_get_section_contents (abfd, section, location, offset, count) |
f4bd7a8f DM |
811 | bfd *abfd; |
812 | sec_ptr section; | |
813 | PTR location; | |
814 | file_ptr offset; | |
815 | bfd_size_type count; | |
2203f786 JG |
816 | { |
817 | if (count == 0) | |
6f715d66 | 818 | return true; |
f8e01940 JG |
819 | if ((bfd_size_type)(offset+count) > section->_raw_size |
820 | || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1 | |
6f715d66 SC |
821 | || bfd_read(location, (bfd_size_type)1, count, abfd) != count) |
822 | return (false); /* on error */ | |
2203f786 JG |
823 | return (true); |
824 | } | |
6f715d66 | 825 | |
f58809fd SC |
826 | /* This generic function can only be used in implementations where creating |
827 | NEW sections is disallowed. It is useful in patching existing sections | |
828 | in read-write files, though. See other set_section_contents functions | |
829 | to see why it doesn't work for new sections. */ | |
830 | boolean | |
6812b607 | 831 | _bfd_generic_set_section_contents (abfd, section, location, offset, count) |
df34342b ILT |
832 | bfd *abfd; |
833 | sec_ptr section; | |
834 | PTR location; | |
835 | file_ptr offset; | |
836 | bfd_size_type count; | |
f58809fd | 837 | { |
df34342b ILT |
838 | if (count == 0) |
839 | return true; | |
840 | ||
df34342b ILT |
841 | if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1 |
842 | || bfd_write (location, (bfd_size_type) 1, count, abfd) != count) | |
843 | return false; | |
844 | ||
845 | return true; | |
f58809fd SC |
846 | } |
847 | ||
f8e01940 JG |
848 | /* |
849 | INTERNAL_FUNCTION | |
850 | bfd_log2 | |
851 | ||
17ecba1a DM |
852 | SYNOPSIS |
853 | unsigned int bfd_log2(bfd_vma x); | |
854 | ||
f8e01940 | 855 | DESCRIPTION |
c188b0be DM |
856 | Return the log base 2 of the value supplied, rounded up. E.g., an |
857 | @var{x} of 1025 returns 11. | |
f8e01940 | 858 | */ |
6f715d66 | 859 | |
b5b4294e JG |
860 | unsigned |
861 | bfd_log2(x) | |
862 | bfd_vma x; | |
6f715d66 | 863 | { |
b5b4294e | 864 | unsigned result = 0; |
6f715d66 SC |
865 | while ( (bfd_vma)(1<< result) < x) |
866 | result++; | |
867 | return result; | |
868 | } | |
120f5bd9 JL |
869 | |
870 | boolean | |
871 | bfd_generic_is_local_label (abfd, sym) | |
872 | bfd *abfd; | |
873 | asymbol *sym; | |
874 | { | |
875 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; | |
876 | ||
877 | return (sym->name[0] == locals_prefix); | |
878 | } | |
879 |