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 | ||
728472f1 | 84 | /*ARGSUSED*/ |
4a81b561 | 85 | void |
f4bd7a8f DM |
86 | bfd_void (ignore) |
87 | bfd *ignore; | |
4a81b561 DHW |
88 | { |
89 | } | |
90 | ||
728472f1 | 91 | /*ARGSUSED*/ |
4a81b561 | 92 | boolean |
f4bd7a8f DM |
93 | _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) |
94 | bfd *ignore_core_bfd; | |
95 | bfd *ignore_exec_bfd; | |
4a81b561 DHW |
96 | { |
97 | bfd_error = invalid_operation; | |
98 | return false; | |
99 | } | |
100 | ||
101 | /* of course you can't initialize a function to be the same as another, grr */ | |
102 | ||
728472f1 | 103 | /*ARGSUSED*/ |
4a81b561 | 104 | char * |
f4bd7a8f DM |
105 | _bfd_dummy_core_file_failing_command (ignore_abfd) |
106 | bfd *ignore_abfd; | |
4a81b561 DHW |
107 | { |
108 | return (char *)NULL; | |
109 | } | |
110 | ||
728472f1 | 111 | /*ARGSUSED*/ |
4a81b561 | 112 | int |
f4bd7a8f DM |
113 | _bfd_dummy_core_file_failing_signal (ignore_abfd) |
114 | bfd *ignore_abfd; | |
4a81b561 DHW |
115 | { |
116 | return 0; | |
117 | } | |
118 | ||
728472f1 | 119 | /*ARGSUSED*/ |
4a81b561 | 120 | bfd_target * |
f4bd7a8f DM |
121 | _bfd_dummy_target (ignore_abfd) |
122 | bfd *ignore_abfd; | |
4a81b561 DHW |
123 | { |
124 | return 0; | |
125 | } | |
126 | \f | |
4a81b561 | 127 | |
f4bd7a8f DM |
128 | #ifndef bfd_zmalloc |
129 | /* allocate and clear storage */ | |
4a81b561 | 130 | |
4a81b561 | 131 | char * |
f4bd7a8f DM |
132 | bfd_zmalloc (size) |
133 | bfd_size_type size; | |
4a81b561 | 134 | { |
b5b4294e | 135 | char *ptr = (char *) malloc ((size_t)size); |
4a81b561 DHW |
136 | |
137 | if ((ptr != NULL) && (size != 0)) | |
b5b4294e | 138 | memset(ptr,0, (size_t) size); |
4a81b561 DHW |
139 | |
140 | return ptr; | |
141 | } | |
f4bd7a8f | 142 | #endif /* bfd_zmalloc */ |
1e310759 | 143 | |
f8e01940 JG |
144 | /* |
145 | INTERNAL_FUNCTION | |
146 | bfd_xmalloc | |
147 | ||
148 | SYNOPSIS | |
17ecba1a | 149 | PTR bfd_xmalloc (bfd_size_type size); |
f8e01940 JG |
150 | |
151 | DESCRIPTION | |
c188b0be | 152 | Like <<malloc>>, but exit if no more memory. |
f8e01940 | 153 | |
1e310759 | 154 | */ |
f8e01940 | 155 | |
1e310759 JG |
156 | /** There is major inconsistency in how running out of memory is handled. |
157 | Some routines return a NULL, and set bfd_error to no_memory. | |
158 | However, obstack routines can't do this ... */ | |
159 | ||
160 | ||
f4bd7a8f DM |
161 | PTR |
162 | bfd_xmalloc (size) | |
163 | bfd_size_type size; | |
1e310759 | 164 | { |
f8e01940 | 165 | static CONST char no_memory_message[] = "Virtual memory exhausted!\n"; |
1e310759 JG |
166 | PTR ptr; |
167 | if (size == 0) size = 1; | |
b5b4294e | 168 | ptr = (PTR)malloc((size_t) size); |
1e310759 JG |
169 | if (!ptr) |
170 | { | |
171 | write (2, no_memory_message, sizeof(no_memory_message)-1); | |
c188b0be | 172 | exit (1); |
1e310759 JG |
173 | } |
174 | return ptr; | |
175 | } | |
b5b4294e JG |
176 | |
177 | /* | |
178 | INTERNAL_FUNCTION | |
179 | bfd_xmalloc_by_size_t | |
180 | ||
181 | SYNOPSIS | |
17ecba1a | 182 | PTR bfd_xmalloc_by_size_t (size_t size); |
b5b4294e JG |
183 | |
184 | DESCRIPTION | |
c188b0be DM |
185 | Like <<malloc>>, but exit if no more memory. |
186 | Uses <<size_t>>, so it's suitable for use as <<obstack_chunk_alloc>>. | |
b5b4294e JG |
187 | */ |
188 | PTR | |
f4bd7a8f DM |
189 | bfd_xmalloc_by_size_t (size) |
190 | size_t size; | |
b5b4294e JG |
191 | { |
192 | return bfd_xmalloc ((bfd_size_type) size); | |
193 | } | |
4a81b561 DHW |
194 | \f |
195 | /* Some IO code */ | |
196 | ||
197 | ||
198 | /* Note that archive entries don't have streams; they share their parent's. | |
f58809fd | 199 | This allows someone to play with the iostream behind BFD's back. |
4a81b561 DHW |
200 | |
201 | Also, note that the origin pointer points to the beginning of a file's | |
202 | contents (0 for non-archive elements). For archive entries this is the | |
203 | first octet in the file, NOT the beginning of the archive header. */ | |
204 | ||
7ed4093a | 205 | static |
f4bd7a8f DM |
206 | int |
207 | real_read (where, a,b, file) | |
208 | PTR where; | |
209 | int a; | |
210 | int b; | |
211 | FILE *file; | |
7ed4093a SC |
212 | { |
213 | return fread(where, a,b,file); | |
214 | } | |
f4bd7a8f | 215 | |
23b0b558 | 216 | bfd_size_type |
f4bd7a8f DM |
217 | bfd_read (ptr, size, nitems, abfd) |
218 | PTR ptr; | |
219 | bfd_size_type size; | |
220 | bfd_size_type nitems; | |
221 | bfd *abfd; | |
4a81b561 | 222 | { |
0d552306 KR |
223 | int nread; |
224 | nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd)); | |
225 | #ifdef FILE_OFFSET_IS_CHAR_INDEX | |
226 | if (nread > 0) | |
227 | abfd->where += nread; | |
228 | #endif | |
229 | return nread; | |
4a81b561 DHW |
230 | } |
231 | ||
23b0b558 | 232 | bfd_size_type |
df34342b ILT |
233 | bfd_write (ptr, size, nitems, abfd) |
234 | CONST PTR ptr; | |
235 | bfd_size_type size; | |
236 | bfd_size_type nitems; | |
237 | bfd *abfd; | |
4a81b561 | 238 | { |
df34342b | 239 | int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd)); |
0d552306 KR |
240 | #ifdef FILE_OFFSET_IS_CHAR_INDEX |
241 | if (nwrote > 0) | |
242 | abfd->where += nwrote; | |
243 | #endif | |
df34342b ILT |
244 | if (nwrote != size * nitems) |
245 | { | |
246 | #ifdef ENOSPC | |
247 | if (nwrote >= 0) | |
248 | errno = ENOSPC; | |
249 | #endif | |
250 | bfd_error = system_call_error; | |
251 | } | |
0d552306 | 252 | return nwrote; |
4a81b561 DHW |
253 | } |
254 | ||
f8e01940 JG |
255 | /* |
256 | INTERNAL_FUNCTION | |
257 | bfd_write_bigendian_4byte_int | |
258 | ||
259 | SYNOPSIS | |
260 | void bfd_write_bigendian_4byte_int(bfd *abfd, int i); | |
261 | ||
262 | DESCRIPTION | |
c188b0be DM |
263 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
264 | endian order regardless of what else is going on. This is useful in | |
f8e01940 | 265 | archives. |
1e310759 | 266 | |
1e310759 | 267 | */ |
f58809fd | 268 | void |
f4bd7a8f DM |
269 | bfd_write_bigendian_4byte_int (abfd, i) |
270 | bfd *abfd; | |
271 | int i; | |
f58809fd | 272 | { |
1e310759 | 273 | bfd_byte buffer[4]; |
b5b4294e | 274 | bfd_putb32(i, buffer); |
1e310759 | 275 | bfd_write((PTR)buffer, 4, 1, abfd); |
f58809fd SC |
276 | } |
277 | ||
0d552306 | 278 | long |
f4bd7a8f DM |
279 | bfd_tell (abfd) |
280 | bfd *abfd; | |
0d552306 KR |
281 | { |
282 | file_ptr ptr; | |
283 | ||
284 | ptr = ftell (bfd_cache_lookup(abfd)); | |
285 | ||
286 | if (abfd->my_archive) | |
287 | ptr -= abfd->origin; | |
288 | abfd->where = ptr; | |
289 | return ptr; | |
290 | } | |
291 | ||
b5b4294e | 292 | int |
f4bd7a8f DM |
293 | bfd_flush (abfd) |
294 | bfd *abfd; | |
b5b4294e JG |
295 | { |
296 | return fflush (bfd_cache_lookup(abfd)); | |
297 | } | |
298 | ||
299 | int | |
f4bd7a8f DM |
300 | bfd_stat (abfd, statbuf) |
301 | bfd *abfd; | |
302 | struct stat *statbuf; | |
b5b4294e JG |
303 | { |
304 | return fstat (fileno(bfd_cache_lookup(abfd)), statbuf); | |
305 | } | |
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); | |
371 | bfd_error = system_call_error; | |
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) { | |
413 | bfd_error = no_memory; | |
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) { | |
425 | bfd_error = no_memory; | |
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 (gnu@cygnus.com): 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) | |
da3cd00a | 570 | #define EIGHT_GAZILLION (((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 |
f4bd7a8f DM |
810 | bfd_generic_get_section_contents (abfd, section, location, offset, count) |
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 | |
df34342b ILT |
831 | bfd_generic_set_section_contents (abfd, section, location, offset, count) |
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 | } |