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