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