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