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