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