Commit | Line | Data |
---|---|---|
0d552306 | 1 | /* Assorted BFD support routines, only used internally. |
09832674 ILT |
2 | Copyright 1990, 91, 92, 93, 94, 95, 96, 97, 1998 |
3 | Free Software Foundation, Inc. | |
f58809fd | 4 | Written by Cygnus Support. |
4a81b561 | 5 | |
f58809fd | 6 | This file is part of BFD, the Binary File Descriptor library. |
4a81b561 | 7 | |
f58809fd | 8 | This program is free software; you can redistribute it and/or modify |
4a81b561 | 9 | it under the terms of the GNU General Public License as published by |
f58809fd SC |
10 | the Free Software Foundation; either version 2 of the License, or |
11 | (at your option) any later version. | |
4a81b561 | 12 | |
f58809fd | 13 | This program is distributed in the hope that it will be useful, |
4a81b561 DHW |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
16 | GNU General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
f58809fd | 19 | along with this program; if not, write to the Free Software |
4fe6d901 | 20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
4a81b561 | 21 | |
4a81b561 | 22 | #include "bfd.h" |
f58809fd | 23 | #include "sysdep.h" |
4a81b561 DHW |
24 | #include "libbfd.h" |
25 | ||
09832674 ILT |
26 | #ifndef HAVE_GETPAGESIZE |
27 | #define getpagesize() 2048 | |
28 | #endif | |
29 | ||
4fe6d901 KR |
30 | static int real_read PARAMS ((PTR, size_t, size_t, FILE *)); |
31 | ||
f8e01940 JG |
32 | /* |
33 | SECTION | |
17ecba1a | 34 | Internal functions |
f8e01940 JG |
35 | |
36 | DESCRIPTION | |
c188b0be | 37 | These routines are used within BFD. |
f8e01940 JG |
38 | They are not intended for export, but are documented here for |
39 | completeness. | |
40 | */ | |
4a81b561 | 41 | |
6812b607 ILT |
42 | /* A routine which is used in target vectors for unsupported |
43 | operations. */ | |
4a81b561 | 44 | |
728472f1 | 45 | /*ARGSUSED*/ |
4a81b561 | 46 | boolean |
f4bd7a8f DM |
47 | bfd_false (ignore) |
48 | bfd *ignore; | |
4a81b561 | 49 | { |
6812b607 | 50 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
51 | return false; |
52 | } | |
53 | ||
6812b607 ILT |
54 | /* A routine which is used in target vectors for supported operations |
55 | which do not actually do anything. */ | |
56 | ||
728472f1 | 57 | /*ARGSUSED*/ |
4a81b561 | 58 | boolean |
f4bd7a8f DM |
59 | bfd_true (ignore) |
60 | bfd *ignore; | |
4a81b561 DHW |
61 | { |
62 | return true; | |
63 | } | |
64 | ||
6812b607 ILT |
65 | /* A routine which is used in target vectors for unsupported |
66 | operations which return a pointer value. */ | |
67 | ||
728472f1 | 68 | /*ARGSUSED*/ |
d0ec7a8e | 69 | PTR |
f4bd7a8f DM |
70 | bfd_nullvoidptr (ignore) |
71 | bfd *ignore; | |
4a81b561 | 72 | { |
6812b607 ILT |
73 | bfd_set_error (bfd_error_invalid_operation); |
74 | return NULL; | |
4a81b561 | 75 | } |
fc723380 | 76 | |
728472f1 | 77 | /*ARGSUSED*/ |
4a81b561 | 78 | int |
f4bd7a8f DM |
79 | bfd_0 (ignore) |
80 | bfd *ignore; | |
4a81b561 DHW |
81 | { |
82 | return 0; | |
83 | } | |
fc723380 | 84 | |
728472f1 | 85 | /*ARGSUSED*/ |
4a81b561 | 86 | unsigned int |
f4bd7a8f DM |
87 | bfd_0u (ignore) |
88 | bfd *ignore; | |
4a81b561 DHW |
89 | { |
90 | return 0; | |
91 | } | |
92 | ||
326e32d7 ILT |
93 | /*ARGUSED*/ |
94 | long | |
95 | bfd_0l (ignore) | |
96 | bfd *ignore; | |
97 | { | |
98 | return 0; | |
99 | } | |
100 | ||
6812b607 ILT |
101 | /* A routine which is used in target vectors for unsupported |
102 | operations which return -1 on error. */ | |
103 | ||
104 | /*ARGSUSED*/ | |
105 | long | |
106 | _bfd_n1 (ignore_abfd) | |
107 | bfd *ignore_abfd; | |
108 | { | |
109 | bfd_set_error (bfd_error_invalid_operation); | |
110 | return -1; | |
111 | } | |
112 | ||
728472f1 | 113 | /*ARGSUSED*/ |
4a81b561 | 114 | void |
f4bd7a8f DM |
115 | bfd_void (ignore) |
116 | bfd *ignore; | |
4a81b561 DHW |
117 | { |
118 | } | |
119 | ||
728472f1 | 120 | /*ARGSUSED*/ |
4a81b561 | 121 | boolean |
6812b607 | 122 | _bfd_nocore_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd) |
f4bd7a8f DM |
123 | bfd *ignore_core_bfd; |
124 | bfd *ignore_exec_bfd; | |
4a81b561 | 125 | { |
120f5bd9 | 126 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
127 | return false; |
128 | } | |
129 | ||
6812b607 ILT |
130 | /* Routine to handle core_file_failing_command entry point for targets |
131 | without core file support. */ | |
4a81b561 | 132 | |
728472f1 | 133 | /*ARGSUSED*/ |
4a81b561 | 134 | char * |
6812b607 | 135 | _bfd_nocore_core_file_failing_command (ignore_abfd) |
f4bd7a8f | 136 | bfd *ignore_abfd; |
4a81b561 | 137 | { |
6812b607 | 138 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
139 | return (char *)NULL; |
140 | } | |
141 | ||
6812b607 ILT |
142 | /* Routine to handle core_file_failing_signal entry point for targets |
143 | without core file support. */ | |
144 | ||
728472f1 | 145 | /*ARGSUSED*/ |
4a81b561 | 146 | int |
6812b607 | 147 | _bfd_nocore_core_file_failing_signal (ignore_abfd) |
f4bd7a8f | 148 | bfd *ignore_abfd; |
4a81b561 | 149 | { |
6812b607 | 150 | bfd_set_error (bfd_error_invalid_operation); |
4a81b561 DHW |
151 | return 0; |
152 | } | |
153 | ||
728472f1 | 154 | /*ARGSUSED*/ |
4fe6d901 | 155 | const bfd_target * |
f4bd7a8f DM |
156 | _bfd_dummy_target (ignore_abfd) |
157 | bfd *ignore_abfd; | |
4a81b561 | 158 | { |
4fe6d901 | 159 | bfd_set_error (bfd_error_wrong_format); |
4a81b561 DHW |
160 | return 0; |
161 | } | |
162 | \f | |
64d5f5d0 | 163 | /* Allocate memory using malloc. */ |
4a81b561 | 164 | |
64d5f5d0 ILT |
165 | PTR |
166 | bfd_malloc (size) | |
167 | size_t size; | |
168 | { | |
169 | PTR ptr; | |
4a81b561 | 170 | |
64d5f5d0 ILT |
171 | ptr = (PTR) malloc (size); |
172 | if (ptr == NULL && size != 0) | |
173 | bfd_set_error (bfd_error_no_memory); | |
174 | return ptr; | |
175 | } | |
176 | ||
177 | /* Reallocate memory using realloc. */ | |
178 | ||
179 | PTR | |
180 | bfd_realloc (ptr, size) | |
181 | PTR ptr; | |
182 | size_t size; | |
183 | { | |
184 | PTR ret; | |
185 | ||
186 | if (ptr == NULL) | |
187 | ret = malloc (size); | |
188 | else | |
189 | ret = realloc (ptr, size); | |
190 | ||
191 | if (ret == NULL) | |
192 | bfd_set_error (bfd_error_no_memory); | |
193 | ||
194 | return ret; | |
195 | } | |
196 | ||
197 | /* Allocate memory using malloc and clear it. */ | |
198 | ||
199 | PTR | |
f4bd7a8f | 200 | bfd_zmalloc (size) |
64d5f5d0 | 201 | size_t size; |
4a81b561 | 202 | { |
64d5f5d0 ILT |
203 | PTR ptr; |
204 | ||
205 | ptr = (PTR) malloc (size); | |
4a81b561 | 206 | |
a9713b91 ILT |
207 | if (size != 0) |
208 | { | |
209 | if (ptr == NULL) | |
210 | bfd_set_error (bfd_error_no_memory); | |
211 | else | |
64d5f5d0 | 212 | memset (ptr, 0, size); |
a9713b91 | 213 | } |
4a81b561 DHW |
214 | |
215 | return ptr; | |
216 | } | |
4a81b561 DHW |
217 | \f |
218 | /* Some IO code */ | |
219 | ||
220 | ||
221 | /* Note that archive entries don't have streams; they share their parent's. | |
f58809fd | 222 | This allows someone to play with the iostream behind BFD's back. |
4a81b561 DHW |
223 | |
224 | Also, note that the origin pointer points to the beginning of a file's | |
225 | contents (0 for non-archive elements). For archive entries this is the | |
226 | first octet in the file, NOT the beginning of the archive header. */ | |
227 | ||
4fe6d901 | 228 | static int |
f4bd7a8f DM |
229 | real_read (where, a,b, file) |
230 | PTR where; | |
4fe6d901 KR |
231 | size_t a; |
232 | size_t b; | |
f4bd7a8f | 233 | FILE *file; |
7ed4093a | 234 | { |
09832674 ILT |
235 | #if defined (__VAX) && defined (VMS) |
236 | /* Apparently fread on Vax VMS does not keep the record length | |
237 | information. */ | |
238 | return read (fileno (file), where, a * b); | |
239 | #else | |
4fe6d901 | 240 | return fread (where, a, b, file); |
09832674 | 241 | #endif |
7ed4093a | 242 | } |
f4bd7a8f | 243 | |
120f5bd9 JL |
244 | /* Return value is amount read (FIXME: how are errors and end of file dealt |
245 | with? We never call bfd_set_error, which is probably a mistake). */ | |
246 | ||
23b0b558 | 247 | bfd_size_type |
f4bd7a8f DM |
248 | bfd_read (ptr, size, nitems, abfd) |
249 | PTR ptr; | |
250 | bfd_size_type size; | |
251 | bfd_size_type nitems; | |
252 | bfd *abfd; | |
4a81b561 | 253 | { |
0d552306 | 254 | int nread; |
64d5f5d0 ILT |
255 | |
256 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
257 | { | |
258 | struct bfd_in_memory *bim; | |
259 | bfd_size_type get; | |
260 | ||
261 | bim = (struct bfd_in_memory *) abfd->iostream; | |
262 | get = size * nitems; | |
263 | if (abfd->where + get > bim->size) | |
264 | { | |
265 | get = bim->size - abfd->where; | |
266 | bfd_set_error (bfd_error_file_truncated); | |
267 | } | |
268 | memcpy (ptr, bim->buffer + abfd->where, get); | |
269 | abfd->where += get; | |
270 | return get; | |
271 | } | |
272 | ||
4fe6d901 | 273 | nread = real_read (ptr, 1, (size_t)(size*nitems), bfd_cache_lookup(abfd)); |
0d552306 KR |
274 | if (nread > 0) |
275 | abfd->where += nread; | |
120f5bd9 JL |
276 | |
277 | /* Set bfd_error if we did not read as much data as we expected. | |
278 | ||
279 | If the read failed due to an error set the bfd_error_system_call, | |
280 | else set bfd_error_file_truncated. | |
281 | ||
282 | A BFD backend may wish to override bfd_error_file_truncated to | |
283 | provide something more useful (eg. no_symbols or wrong_format). */ | |
284 | if (nread < (int)(size * nitems)) | |
285 | { | |
286 | if (ferror (bfd_cache_lookup (abfd))) | |
287 | bfd_set_error (bfd_error_system_call); | |
288 | else | |
289 | bfd_set_error (bfd_error_file_truncated); | |
290 | } | |
291 | ||
0d552306 | 292 | return nread; |
4a81b561 DHW |
293 | } |
294 | ||
4fe6d901 KR |
295 | /* The window support stuff should probably be broken out into |
296 | another file.... */ | |
297 | /* The idea behind the next and refcount fields is that one mapped | |
298 | region can suffice for multiple read-only windows or multiple | |
299 | non-overlapping read-write windows. It's not implemented yet | |
300 | though. */ | |
301 | struct _bfd_window_internal { | |
302 | struct _bfd_window_internal *next; | |
303 | PTR data; | |
304 | bfd_size_type size; | |
305 | int refcount : 31; /* should be enough... */ | |
306 | unsigned mapped : 1; /* 1 = mmap, 0 = malloc */ | |
307 | }; | |
308 | ||
309 | void | |
310 | bfd_init_window (windowp) | |
311 | bfd_window *windowp; | |
312 | { | |
313 | windowp->data = 0; | |
314 | windowp->i = 0; | |
315 | windowp->size = 0; | |
316 | } | |
09832674 ILT |
317 | \f |
318 | /* Currently, if USE_MMAP is undefined, none if the window stuff is | |
319 | used. Okay, so it's mis-named. At least the command-line option | |
320 | "--without-mmap" is more obvious than "--without-windows" or some | |
321 | such. */ | |
322 | #ifdef USE_MMAP | |
4fe6d901 KR |
323 | |
324 | #undef HAVE_MPROTECT /* code's not tested yet */ | |
325 | ||
a9713b91 | 326 | #if HAVE_MMAP || HAVE_MPROTECT || HAVE_MADVISE |
4fe6d901 KR |
327 | #include <sys/mman.h> |
328 | #endif | |
329 | ||
330 | #ifndef MAP_FILE | |
331 | #define MAP_FILE 0 | |
332 | #endif | |
333 | ||
334 | static int debug_windows; | |
335 | ||
336 | void | |
337 | bfd_free_window (windowp) | |
338 | bfd_window *windowp; | |
339 | { | |
340 | bfd_window_internal *i = windowp->i; | |
341 | windowp->i = 0; | |
342 | windowp->data = 0; | |
343 | if (i == 0) | |
344 | return; | |
345 | i->refcount--; | |
346 | if (debug_windows) | |
347 | fprintf (stderr, "freeing window @%p<%p,%lx,%p>\n", | |
348 | windowp, windowp->data, windowp->size, windowp->i); | |
349 | if (i->refcount != 0) | |
350 | return; | |
351 | ||
352 | if (i->mapped) | |
353 | { | |
354 | #ifdef HAVE_MMAP | |
355 | munmap (i->data, i->size); | |
356 | goto no_free; | |
357 | #else | |
358 | abort (); | |
359 | #endif | |
360 | } | |
361 | #ifdef HAVE_MPROTECT | |
362 | mprotect (i->data, i->size, PROT_READ | PROT_WRITE); | |
363 | #endif | |
364 | free (i->data); | |
365 | #ifdef HAVE_MMAP | |
366 | no_free: | |
367 | #endif | |
368 | i->data = 0; | |
369 | /* There should be no more references to i at this point. */ | |
370 | free (i); | |
371 | } | |
372 | ||
373 | static int ok_to_map = 1; | |
374 | ||
092abcdf | 375 | boolean |
4fe6d901 KR |
376 | bfd_get_file_window (abfd, offset, size, windowp, writable) |
377 | bfd *abfd; | |
378 | file_ptr offset; | |
379 | bfd_size_type size; | |
380 | bfd_window *windowp; | |
0bb8ff19 | 381 | boolean writable; |
4fe6d901 KR |
382 | { |
383 | static size_t pagesize; | |
384 | bfd_window_internal *i = windowp->i; | |
385 | size_t size_to_alloc = size; | |
386 | ||
387 | if (debug_windows) | |
388 | fprintf (stderr, "bfd_get_file_window (%p, %6ld, %6ld, %p<%p,%lx,%p>, %d)", | |
389 | abfd, (long) offset, (long) size, | |
09832674 ILT |
390 | windowp, windowp->data, (unsigned long) windowp->size, |
391 | windowp->i, writable); | |
4fe6d901 KR |
392 | |
393 | /* Make sure we know the page size, so we can be friendly to mmap. */ | |
394 | if (pagesize == 0) | |
395 | pagesize = getpagesize (); | |
396 | if (pagesize == 0) | |
397 | abort (); | |
398 | ||
399 | if (i == 0) | |
400 | { | |
401 | windowp->i = i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal)); | |
402 | if (i == 0) | |
403 | return false; | |
404 | i->data = 0; | |
405 | } | |
406 | #ifdef HAVE_MMAP | |
64d5f5d0 ILT |
407 | if (ok_to_map |
408 | && (i->data == 0 || i->mapped == 1) | |
409 | && (abfd->flags & BFD_IN_MEMORY) == 0) | |
4fe6d901 KR |
410 | { |
411 | file_ptr file_offset, offset2; | |
412 | size_t real_size; | |
413 | int fd; | |
414 | FILE *f; | |
415 | ||
416 | /* Find the real file and the real offset into it. */ | |
417 | while (abfd->my_archive != NULL) | |
418 | { | |
419 | offset += abfd->origin; | |
420 | abfd = abfd->my_archive; | |
421 | } | |
422 | f = bfd_cache_lookup (abfd); | |
423 | fd = fileno (f); | |
424 | ||
425 | /* Compute offsets and size for mmap and for the user's data. */ | |
426 | offset2 = offset % pagesize; | |
427 | if (offset2 < 0) | |
428 | abort (); | |
429 | file_offset = offset - offset2; | |
430 | real_size = offset + size - file_offset; | |
431 | real_size = real_size + pagesize - 1; | |
432 | real_size -= real_size % pagesize; | |
433 | ||
434 | /* If we're re-using a memory region, make sure it's big enough. */ | |
435 | if (i->data && i->size < size) | |
436 | { | |
437 | munmap (i->data, i->size); | |
438 | i->data = 0; | |
439 | } | |
440 | i->data = mmap (i->data, real_size, | |
441 | writable ? PROT_WRITE | PROT_READ : PROT_READ, | |
0bb8ff19 ILT |
442 | (writable |
443 | ? MAP_FILE | MAP_PRIVATE | |
444 | : MAP_FILE | MAP_SHARED), | |
4fe6d901 KR |
445 | fd, file_offset); |
446 | if (i->data == (PTR) -1) | |
447 | { | |
448 | /* An error happened. Report it, or try using malloc, or | |
449 | something. */ | |
450 | bfd_set_error (bfd_error_system_call); | |
451 | i->data = 0; | |
452 | windowp->data = 0; | |
453 | if (debug_windows) | |
454 | fprintf (stderr, "\t\tmmap failed!\n"); | |
455 | return false; | |
456 | } | |
457 | if (debug_windows) | |
458 | fprintf (stderr, "\n\tmapped %ld at %p, offset is %ld\n", | |
459 | (long) real_size, i->data, (long) offset2); | |
460 | i->size = real_size; | |
a9713b91 | 461 | windowp->data = (PTR) ((bfd_byte *) i->data + offset2); |
4fe6d901 KR |
462 | windowp->size = size; |
463 | i->mapped = 1; | |
464 | return true; | |
465 | } | |
466 | else if (debug_windows) | |
467 | { | |
468 | if (ok_to_map) | |
09832674 | 469 | fprintf (stderr, _("not mapping: data=%lx mapped=%d\n"), |
3b168da4 | 470 | (unsigned long) i->data, (int) i->mapped); |
4fe6d901 | 471 | else |
09832674 | 472 | fprintf (stderr, _("not mapping: env var not set\n")); |
4fe6d901 KR |
473 | } |
474 | #else | |
475 | ok_to_map = 0; | |
476 | #endif | |
477 | ||
478 | #ifdef HAVE_MPROTECT | |
479 | if (!writable) | |
480 | { | |
481 | size_to_alloc += pagesize - 1; | |
482 | size_to_alloc -= size_to_alloc % pagesize; | |
483 | } | |
484 | #endif | |
485 | if (debug_windows) | |
486 | fprintf (stderr, "\n\t%s(%6ld)", | |
487 | i->data ? "realloc" : " malloc", (long) size_to_alloc); | |
64d5f5d0 | 488 | i->data = (PTR) bfd_realloc (i->data, size_to_alloc); |
4fe6d901 KR |
489 | if (debug_windows) |
490 | fprintf (stderr, "\t-> %p\n", i->data); | |
491 | i->refcount = 1; | |
092abcdf ILT |
492 | if (i->data == NULL) |
493 | { | |
494 | if (size_to_alloc == 0) | |
495 | return true; | |
496 | bfd_set_error (bfd_error_no_memory); | |
497 | return false; | |
498 | } | |
4fe6d901 KR |
499 | if (bfd_seek (abfd, offset, SEEK_SET) != 0) |
500 | return false; | |
501 | i->size = bfd_read (i->data, size, 1, abfd); | |
502 | if (i->size != size) | |
503 | return false; | |
504 | i->mapped = 0; | |
505 | #ifdef HAVE_MPROTECT | |
506 | if (!writable) | |
507 | { | |
508 | if (debug_windows) | |
509 | fprintf (stderr, "\tmprotect (%p, %ld, PROT_READ)\n", i->data, | |
510 | (long) i->size); | |
511 | mprotect (i->data, i->size, PROT_READ); | |
512 | } | |
513 | #endif | |
514 | windowp->data = i->data; | |
515 | windowp->size = i->size; | |
516 | return true; | |
517 | } | |
518 | ||
09832674 ILT |
519 | #endif /* USE_MMAP */ |
520 | \f | |
23b0b558 | 521 | bfd_size_type |
df34342b ILT |
522 | bfd_write (ptr, size, nitems, abfd) |
523 | CONST PTR ptr; | |
524 | bfd_size_type size; | |
525 | bfd_size_type nitems; | |
526 | bfd *abfd; | |
4a81b561 | 527 | { |
64d5f5d0 ILT |
528 | long nwrote; |
529 | ||
530 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
531 | abort (); | |
532 | ||
533 | nwrote = fwrite (ptr, 1, (size_t) (size * nitems), | |
534 | bfd_cache_lookup (abfd)); | |
0d552306 KR |
535 | if (nwrote > 0) |
536 | abfd->where += nwrote; | |
4fe6d901 | 537 | if ((bfd_size_type) nwrote != size * nitems) |
df34342b ILT |
538 | { |
539 | #ifdef ENOSPC | |
540 | if (nwrote >= 0) | |
541 | errno = ENOSPC; | |
542 | #endif | |
120f5bd9 | 543 | bfd_set_error (bfd_error_system_call); |
df34342b | 544 | } |
0d552306 | 545 | return nwrote; |
4a81b561 DHW |
546 | } |
547 | ||
f8e01940 JG |
548 | /* |
549 | INTERNAL_FUNCTION | |
550 | bfd_write_bigendian_4byte_int | |
551 | ||
552 | SYNOPSIS | |
553 | void bfd_write_bigendian_4byte_int(bfd *abfd, int i); | |
554 | ||
555 | DESCRIPTION | |
c188b0be DM |
556 | Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big |
557 | endian order regardless of what else is going on. This is useful in | |
f8e01940 | 558 | archives. |
1e310759 | 559 | |
1e310759 | 560 | */ |
f58809fd | 561 | void |
f4bd7a8f DM |
562 | bfd_write_bigendian_4byte_int (abfd, i) |
563 | bfd *abfd; | |
564 | int i; | |
f58809fd | 565 | { |
1e310759 | 566 | bfd_byte buffer[4]; |
b5b4294e | 567 | bfd_putb32(i, buffer); |
4002f18a ILT |
568 | if (bfd_write((PTR)buffer, 4, 1, abfd) != 4) |
569 | abort (); | |
f58809fd SC |
570 | } |
571 | ||
0d552306 | 572 | long |
f4bd7a8f DM |
573 | bfd_tell (abfd) |
574 | bfd *abfd; | |
0d552306 KR |
575 | { |
576 | file_ptr ptr; | |
577 | ||
64d5f5d0 ILT |
578 | if ((abfd->flags & BFD_IN_MEMORY) != 0) |
579 | return abfd->where; | |
580 | ||
0d552306 KR |
581 | ptr = ftell (bfd_cache_lookup(abfd)); |
582 | ||
583 | if (abfd->my_archive) | |
584 | ptr -= abfd->origin; | |
585 | abfd->where = ptr; | |
586 | return ptr; | |
587 | } | |
588 | ||
b5b4294e | 589 | int |
f4bd7a8f DM |
590 | bfd_flush (abfd) |
591 | bfd *abfd; | |
b5b4294e | 592 | { |
64d5f5d0 ILT |
593 | if ((abfd->flags & BFD_IN_MEMORY) != 0) |
594 | return 0; | |
b5b4294e JG |
595 | return fflush (bfd_cache_lookup(abfd)); |
596 | } | |
597 | ||
4fe6d901 KR |
598 | /* Returns 0 for success, negative value for failure (in which case |
599 | bfd_get_error can retrieve the error code). */ | |
b5b4294e | 600 | int |
f4bd7a8f DM |
601 | bfd_stat (abfd, statbuf) |
602 | bfd *abfd; | |
603 | struct stat *statbuf; | |
b5b4294e | 604 | { |
4fe6d901 KR |
605 | FILE *f; |
606 | int result; | |
64d5f5d0 ILT |
607 | |
608 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
609 | abort (); | |
610 | ||
4fe6d901 KR |
611 | f = bfd_cache_lookup (abfd); |
612 | if (f == NULL) | |
613 | { | |
614 | bfd_set_error (bfd_error_system_call); | |
615 | return -1; | |
616 | } | |
617 | result = fstat (fileno (f), statbuf); | |
618 | if (result < 0) | |
619 | bfd_set_error (bfd_error_system_call); | |
620 | return result; | |
b5b4294e JG |
621 | } |
622 | ||
120f5bd9 JL |
623 | /* Returns 0 for success, nonzero for failure (in which case bfd_get_error |
624 | can retrieve the error code). */ | |
625 | ||
4a81b561 | 626 | int |
f4bd7a8f | 627 | bfd_seek (abfd, position, direction) |
4fe6d901 KR |
628 | bfd *abfd; |
629 | file_ptr position; | |
630 | int direction; | |
4a81b561 | 631 | { |
0d552306 KR |
632 | int result; |
633 | FILE *f; | |
e5b02860 | 634 | file_ptr file_position; |
0d552306 KR |
635 | /* For the time being, a BFD may not seek to it's end. The problem |
636 | is that we don't easily have a way to recognize the end of an | |
637 | element in an archive. */ | |
638 | ||
639 | BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR); | |
640 | ||
b31d06ca | 641 | if (direction == SEEK_CUR && position == 0) |
0d552306 | 642 | return 0; |
64d5f5d0 ILT |
643 | |
644 | if ((abfd->flags & BFD_IN_MEMORY) != 0) | |
645 | { | |
646 | if (direction == SEEK_SET) | |
647 | abfd->where = position; | |
648 | else | |
649 | abfd->where += position; | |
650 | return 0; | |
651 | } | |
652 | ||
b6090f4d JG |
653 | if (abfd->format != bfd_archive && abfd->my_archive == 0) |
654 | { | |
4c3721d5 | 655 | #if 0 |
b6090f4d JG |
656 | /* Explanation for this code: I'm only about 95+% sure that the above |
657 | conditions are sufficient and that all i/o calls are properly | |
658 | adjusting the `where' field. So this is sort of an `assert' | |
659 | that the `where' field is correct. If we can go a while without | |
660 | tripping the abort, we can probably safely disable this code, | |
661 | so that the real optimizations happen. */ | |
662 | file_ptr where_am_i_now; | |
663 | where_am_i_now = ftell (bfd_cache_lookup (abfd)); | |
664 | if (abfd->my_archive) | |
665 | where_am_i_now -= abfd->origin; | |
666 | if (where_am_i_now != abfd->where) | |
667 | abort (); | |
668 | #endif | |
669 | if (direction == SEEK_SET && position == abfd->where) | |
670 | return 0; | |
671 | } | |
672 | else | |
673 | { | |
674 | /* We need something smarter to optimize access to archives. | |
675 | Currently, anything inside an archive is read via the file | |
676 | handle for the archive. Which means that a bfd_seek on one | |
677 | component affects the `current position' in the archive, as | |
678 | well as in any other component. | |
679 | ||
680 | It might be sufficient to put a spike through the cache | |
681 | abstraction, and look to the archive for the file position, | |
682 | but I think we should try for something cleaner. | |
683 | ||
684 | In the meantime, no optimization for archives. */ | |
685 | } | |
4a81b561 | 686 | |
0d552306 | 687 | f = bfd_cache_lookup (abfd); |
e5b02860 | 688 | file_position = position; |
0d552306 | 689 | if (direction == SEEK_SET && abfd->my_archive != NULL) |
e5b02860 KR |
690 | file_position += abfd->origin; |
691 | ||
692 | result = fseek (f, file_position, direction); | |
693 | ||
694 | if (result != 0) | |
df34342b | 695 | { |
09832674 ILT |
696 | int hold_errno = errno; |
697 | ||
df34342b ILT |
698 | /* Force redetermination of `where' field. */ |
699 | bfd_tell (abfd); | |
09832674 ILT |
700 | |
701 | /* An EINVAL error probably means that the file offset was | |
702 | absurd. */ | |
703 | if (hold_errno == EINVAL) | |
704 | bfd_set_error (bfd_error_file_truncated); | |
705 | else | |
706 | { | |
707 | bfd_set_error (bfd_error_system_call); | |
708 | errno = hold_errno; | |
709 | } | |
df34342b | 710 | } |
0d552306 KR |
711 | else |
712 | { | |
e5b02860 KR |
713 | /* Adjust `where' field. */ |
714 | if (direction == SEEK_SET) | |
715 | abfd->where = position; | |
716 | else | |
717 | abfd->where += position; | |
0d552306 | 718 | } |
e5b02860 | 719 | return result; |
4a81b561 DHW |
720 | } |
721 | \f | |
4a81b561 DHW |
722 | /** The do-it-yourself (byte) sex-change kit */ |
723 | ||
724 | /* The middle letter e.g. get<b>short indicates Big or Little endian | |
725 | target machine. It doesn't matter what the byte order of the host | |
726 | machine is; these routines work for either. */ | |
727 | ||
728 | /* FIXME: Should these take a count argument? | |
729 | Answer (gnu@cygnus.com): No, but perhaps they should be inline | |
6f715d66 SC |
730 | functions in swap.h #ifdef __GNUC__. |
731 | Gprof them later and find out. */ | |
732 | ||
f8e01940 JG |
733 | /* |
734 | FUNCTION | |
735 | bfd_put_size | |
736 | FUNCTION | |
737 | bfd_get_size | |
738 | ||
739 | DESCRIPTION | |
740 | These macros as used for reading and writing raw data in | |
741 | sections; each access (except for bytes) is vectored through | |
742 | the target format of the BFD and mangled accordingly. The | |
743 | mangling performs any necessary endian translations and | |
14e3c2e4 JK |
744 | removes alignment restrictions. Note that types accepted and |
745 | returned by these macros are identical so they can be swapped | |
17ecba1a DM |
746 | around in macros---for example, @file{libaout.h} defines <<GET_WORD>> |
747 | to either <<bfd_get_32>> or <<bfd_get_64>>. | |
f8e01940 | 748 | |
17ecba1a | 749 | In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a |
7e4db254 JK |
750 | system without prototypes, the caller is responsible for making |
751 | sure that is true, with a cast if necessary. We don't cast | |
17ecba1a DM |
752 | them in the macro definitions because that would prevent <<lint>> |
753 | or <<gcc -Wall>> from detecting sins such as passing a pointer. | |
754 | To detect calling these with less than a <<bfd_vma>>, use | |
755 | <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s. | |
7e4db254 | 756 | |
da3cd00a KR |
757 | . |
758 | .{* Byte swapping macros for user section data. *} | |
759 | . | |
f8e01940 | 760 | .#define bfd_put_8(abfd, val, ptr) \ |
4c3721d5 | 761 | . (*((unsigned char *)(ptr)) = (unsigned char)(val)) |
da3cd00a KR |
762 | .#define bfd_put_signed_8 \ |
763 | . bfd_put_8 | |
f8e01940 | 764 | .#define bfd_get_8(abfd, ptr) \ |
da3cd00a KR |
765 | . (*(unsigned char *)(ptr)) |
766 | .#define bfd_get_signed_8(abfd, ptr) \ | |
767 | . ((*(unsigned char *)(ptr) ^ 0x80) - 0x80) | |
768 | . | |
f8e01940 | 769 | .#define bfd_put_16(abfd, val, ptr) \ |
7e4db254 | 770 | . BFD_SEND(abfd, bfd_putx16, ((val),(ptr))) |
da3cd00a KR |
771 | .#define bfd_put_signed_16 \ |
772 | . bfd_put_16 | |
f8e01940 JG |
773 | .#define bfd_get_16(abfd, ptr) \ |
774 | . BFD_SEND(abfd, bfd_getx16, (ptr)) | |
14e3c2e4 JK |
775 | .#define bfd_get_signed_16(abfd, ptr) \ |
776 | . BFD_SEND (abfd, bfd_getx_signed_16, (ptr)) | |
da3cd00a | 777 | . |
f8e01940 | 778 | .#define bfd_put_32(abfd, val, ptr) \ |
7e4db254 | 779 | . BFD_SEND(abfd, bfd_putx32, ((val),(ptr))) |
da3cd00a KR |
780 | .#define bfd_put_signed_32 \ |
781 | . bfd_put_32 | |
f8e01940 JG |
782 | .#define bfd_get_32(abfd, ptr) \ |
783 | . BFD_SEND(abfd, bfd_getx32, (ptr)) | |
14e3c2e4 JK |
784 | .#define bfd_get_signed_32(abfd, ptr) \ |
785 | . BFD_SEND(abfd, bfd_getx_signed_32, (ptr)) | |
da3cd00a | 786 | . |
f8e01940 | 787 | .#define bfd_put_64(abfd, val, ptr) \ |
7e4db254 | 788 | . BFD_SEND(abfd, bfd_putx64, ((val), (ptr))) |
da3cd00a KR |
789 | .#define bfd_put_signed_64 \ |
790 | . bfd_put_64 | |
f8e01940 JG |
791 | .#define bfd_get_64(abfd, ptr) \ |
792 | . BFD_SEND(abfd, bfd_getx64, (ptr)) | |
14e3c2e4 JK |
793 | .#define bfd_get_signed_64(abfd, ptr) \ |
794 | . BFD_SEND(abfd, bfd_getx_signed_64, (ptr)) | |
da3cd00a | 795 | . |
f8e01940 JG |
796 | */ |
797 | ||
798 | /* | |
799 | FUNCTION | |
800 | bfd_h_put_size | |
f8e01940 JG |
801 | bfd_h_get_size |
802 | ||
803 | DESCRIPTION | |
804 | These macros have the same function as their <<bfd_get_x>> | |
c188b0be | 805 | bretheren, except that they are used for removing information |
f8e01940 JG |
806 | for the header records of object files. Believe it or not, |
807 | some object files keep their header records in big endian | |
c188b0be | 808 | order and their data in little endian order. |
da3cd00a KR |
809 | . |
810 | .{* Byte swapping macros for file header data. *} | |
811 | . | |
f8e01940 | 812 | .#define bfd_h_put_8(abfd, val, ptr) \ |
da3cd00a KR |
813 | . bfd_put_8 (abfd, val, ptr) |
814 | .#define bfd_h_put_signed_8(abfd, val, ptr) \ | |
815 | . bfd_put_8 (abfd, val, ptr) | |
f8e01940 | 816 | .#define bfd_h_get_8(abfd, ptr) \ |
da3cd00a KR |
817 | . bfd_get_8 (abfd, ptr) |
818 | .#define bfd_h_get_signed_8(abfd, ptr) \ | |
819 | . bfd_get_signed_8 (abfd, ptr) | |
820 | . | |
f8e01940 JG |
821 | .#define bfd_h_put_16(abfd, val, ptr) \ |
822 | . BFD_SEND(abfd, bfd_h_putx16,(val,ptr)) | |
da3cd00a KR |
823 | .#define bfd_h_put_signed_16 \ |
824 | . bfd_h_put_16 | |
f8e01940 JG |
825 | .#define bfd_h_get_16(abfd, ptr) \ |
826 | . BFD_SEND(abfd, bfd_h_getx16,(ptr)) | |
14e3c2e4 JK |
827 | .#define bfd_h_get_signed_16(abfd, ptr) \ |
828 | . BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr)) | |
da3cd00a | 829 | . |
f8e01940 JG |
830 | .#define bfd_h_put_32(abfd, val, ptr) \ |
831 | . BFD_SEND(abfd, bfd_h_putx32,(val,ptr)) | |
da3cd00a KR |
832 | .#define bfd_h_put_signed_32 \ |
833 | . bfd_h_put_32 | |
f8e01940 JG |
834 | .#define bfd_h_get_32(abfd, ptr) \ |
835 | . BFD_SEND(abfd, bfd_h_getx32,(ptr)) | |
14e3c2e4 JK |
836 | .#define bfd_h_get_signed_32(abfd, ptr) \ |
837 | . BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr)) | |
da3cd00a | 838 | . |
f8e01940 JG |
839 | .#define bfd_h_put_64(abfd, val, ptr) \ |
840 | . BFD_SEND(abfd, bfd_h_putx64,(val, ptr)) | |
da3cd00a KR |
841 | .#define bfd_h_put_signed_64 \ |
842 | . bfd_h_put_64 | |
f8e01940 JG |
843 | .#define bfd_h_get_64(abfd, ptr) \ |
844 | . BFD_SEND(abfd, bfd_h_getx64,(ptr)) | |
14e3c2e4 JK |
845 | .#define bfd_h_get_signed_64(abfd, ptr) \ |
846 | . BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr)) | |
da3cd00a | 847 | . |
f8e01940 | 848 | */ |
4a81b561 | 849 | |
14e3c2e4 | 850 | /* Sign extension to bfd_signed_vma. */ |
df34342b ILT |
851 | #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000) |
852 | #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000) | |
96ad107b | 853 | #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32) |
df34342b ILT |
854 | #define COERCE64(x) \ |
855 | (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION) | |
14e3c2e4 | 856 | |
f58809fd | 857 | bfd_vma |
f4bd7a8f DM |
858 | bfd_getb16 (addr) |
859 | register const bfd_byte *addr; | |
4a81b561 | 860 | { |
f4bd7a8f | 861 | return (addr[0] << 8) | addr[1]; |
4a81b561 DHW |
862 | } |
863 | ||
f58809fd | 864 | bfd_vma |
f4bd7a8f DM |
865 | bfd_getl16 (addr) |
866 | register const bfd_byte *addr; | |
4a81b561 | 867 | { |
f4bd7a8f | 868 | return (addr[1] << 8) | addr[0]; |
4a81b561 DHW |
869 | } |
870 | ||
14e3c2e4 | 871 | bfd_signed_vma |
f4bd7a8f DM |
872 | bfd_getb_signed_16 (addr) |
873 | register const bfd_byte *addr; | |
14e3c2e4 | 874 | { |
f4bd7a8f | 875 | return COERCE16((addr[0] << 8) | addr[1]); |
14e3c2e4 JK |
876 | } |
877 | ||
878 | bfd_signed_vma | |
f4bd7a8f DM |
879 | bfd_getl_signed_16 (addr) |
880 | register const bfd_byte *addr; | |
14e3c2e4 | 881 | { |
f4bd7a8f | 882 | return COERCE16((addr[1] << 8) | addr[0]); |
14e3c2e4 JK |
883 | } |
884 | ||
4a81b561 | 885 | void |
f4bd7a8f DM |
886 | bfd_putb16 (data, addr) |
887 | bfd_vma data; | |
888 | register bfd_byte *addr; | |
4a81b561 | 889 | { |
f4bd7a8f DM |
890 | addr[0] = (bfd_byte)(data >> 8); |
891 | addr[1] = (bfd_byte )data; | |
4a81b561 DHW |
892 | } |
893 | ||
894 | void | |
f4bd7a8f DM |
895 | bfd_putl16 (data, addr) |
896 | bfd_vma data; | |
897 | register bfd_byte *addr; | |
4a81b561 | 898 | { |
f4bd7a8f DM |
899 | addr[0] = (bfd_byte )data; |
900 | addr[1] = (bfd_byte)(data >> 8); | |
4a81b561 DHW |
901 | } |
902 | ||
f58809fd | 903 | bfd_vma |
b5b4294e | 904 | bfd_getb32 (addr) |
f4bd7a8f | 905 | register const bfd_byte *addr; |
4a81b561 | 906 | { |
f4bd7a8f DM |
907 | return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
908 | | addr[2]) << 8 | addr[3]; | |
4a81b561 DHW |
909 | } |
910 | ||
f58809fd | 911 | bfd_vma |
b5b4294e | 912 | bfd_getl32 (addr) |
f4bd7a8f | 913 | register const bfd_byte *addr; |
4a81b561 | 914 | { |
f4bd7a8f DM |
915 | return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
916 | | addr[1]) << 8 | addr[0]; | |
4a81b561 DHW |
917 | } |
918 | ||
14e3c2e4 | 919 | bfd_signed_vma |
b5b4294e | 920 | bfd_getb_signed_32 (addr) |
f4bd7a8f | 921 | register const bfd_byte *addr; |
14e3c2e4 | 922 | { |
f4bd7a8f DM |
923 | return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8) |
924 | | addr[2]) << 8 | addr[3]); | |
14e3c2e4 JK |
925 | } |
926 | ||
927 | bfd_signed_vma | |
b5b4294e | 928 | bfd_getl_signed_32 (addr) |
f4bd7a8f | 929 | register const bfd_byte *addr; |
14e3c2e4 | 930 | { |
f4bd7a8f DM |
931 | return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8) |
932 | | addr[1]) << 8 | addr[0]); | |
14e3c2e4 JK |
933 | } |
934 | ||
f58809fd | 935 | bfd_vma |
f4bd7a8f DM |
936 | bfd_getb64 (addr) |
937 | register const bfd_byte *addr; | |
7ed4093a | 938 | { |
b5b4294e | 939 | #ifdef BFD64 |
b6090f4d | 940 | bfd_vma low, high; |
536b27a5 | 941 | |
7ed4093a | 942 | high= ((((((((addr[0]) << 8) | |
6f715d66 SC |
943 | addr[1]) << 8) | |
944 | addr[2]) << 8) | | |
945 | addr[3]) ); | |
7ed4093a | 946 | |
df34342b | 947 | low = (((((((((bfd_vma)addr[4]) << 8) | |
6f715d66 SC |
948 | addr[5]) << 8) | |
949 | addr[6]) << 8) | | |
950 | addr[7])); | |
7ed4093a SC |
951 | |
952 | return high << 32 | low; | |
953 | #else | |
954 | BFD_FAIL(); | |
f58809fd | 955 | return 0; |
7ed4093a | 956 | #endif |
7ed4093a SC |
957 | } |
958 | ||
f58809fd | 959 | bfd_vma |
f4bd7a8f DM |
960 | bfd_getl64 (addr) |
961 | register const bfd_byte *addr; | |
7ed4093a | 962 | { |
b5b4294e | 963 | #ifdef BFD64 |
b6090f4d | 964 | bfd_vma low, high; |
7ed4093a | 965 | high= (((((((addr[7] << 8) | |
6f715d66 SC |
966 | addr[6]) << 8) | |
967 | addr[5]) << 8) | | |
968 | addr[4])); | |
7ed4093a | 969 | |
df34342b | 970 | low = ((((((((bfd_vma)addr[3] << 8) | |
6f715d66 SC |
971 | addr[2]) << 8) | |
972 | addr[1]) << 8) | | |
973 | addr[0]) ); | |
7ed4093a SC |
974 | |
975 | return high << 32 | low; | |
976 | #else | |
977 | BFD_FAIL(); | |
f58809fd | 978 | return 0; |
7ed4093a | 979 | #endif |
6f715d66 | 980 | |
7ed4093a SC |
981 | } |
982 | ||
14e3c2e4 | 983 | bfd_signed_vma |
f4bd7a8f DM |
984 | bfd_getb_signed_64 (addr) |
985 | register const bfd_byte *addr; | |
14e3c2e4 | 986 | { |
b5b4294e | 987 | #ifdef BFD64 |
14e3c2e4 JK |
988 | bfd_vma low, high; |
989 | ||
990 | high= ((((((((addr[0]) << 8) | | |
991 | addr[1]) << 8) | | |
992 | addr[2]) << 8) | | |
993 | addr[3]) ); | |
994 | ||
df34342b | 995 | low = (((((((((bfd_vma)addr[4]) << 8) | |
14e3c2e4 JK |
996 | addr[5]) << 8) | |
997 | addr[6]) << 8) | | |
998 | addr[7])); | |
999 | ||
1000 | return COERCE64(high << 32 | low); | |
1001 | #else | |
1002 | BFD_FAIL(); | |
1003 | return 0; | |
1004 | #endif | |
14e3c2e4 JK |
1005 | } |
1006 | ||
1007 | bfd_signed_vma | |
f4bd7a8f DM |
1008 | bfd_getl_signed_64 (addr) |
1009 | register const bfd_byte *addr; | |
14e3c2e4 | 1010 | { |
b5b4294e | 1011 | #ifdef BFD64 |
14e3c2e4 JK |
1012 | bfd_vma low, high; |
1013 | high= (((((((addr[7] << 8) | | |
1014 | addr[6]) << 8) | | |
1015 | addr[5]) << 8) | | |
1016 | addr[4])); | |
1017 | ||
df34342b | 1018 | low = ((((((((bfd_vma)addr[3] << 8) | |
14e3c2e4 JK |
1019 | addr[2]) << 8) | |
1020 | addr[1]) << 8) | | |
1021 | addr[0]) ); | |
1022 | ||
1023 | return COERCE64(high << 32 | low); | |
1024 | #else | |
1025 | BFD_FAIL(); | |
1026 | return 0; | |
1027 | #endif | |
14e3c2e4 JK |
1028 | } |
1029 | ||
4a81b561 | 1030 | void |
f4bd7a8f DM |
1031 | bfd_putb32 (data, addr) |
1032 | bfd_vma data; | |
1033 | register bfd_byte *addr; | |
4a81b561 | 1034 | { |
6f715d66 SC |
1035 | addr[0] = (bfd_byte)(data >> 24); |
1036 | addr[1] = (bfd_byte)(data >> 16); | |
1037 | addr[2] = (bfd_byte)(data >> 8); | |
1038 | addr[3] = (bfd_byte)data; | |
4a81b561 DHW |
1039 | } |
1040 | ||
1041 | void | |
f4bd7a8f DM |
1042 | bfd_putl32 (data, addr) |
1043 | bfd_vma data; | |
1044 | register bfd_byte *addr; | |
4a81b561 | 1045 | { |
6f715d66 SC |
1046 | addr[0] = (bfd_byte)data; |
1047 | addr[1] = (bfd_byte)(data >> 8); | |
1048 | addr[2] = (bfd_byte)(data >> 16); | |
1049 | addr[3] = (bfd_byte)(data >> 24); | |
4a81b561 | 1050 | } |
f4bd7a8f | 1051 | |
7ed4093a | 1052 | void |
f4bd7a8f DM |
1053 | bfd_putb64 (data, addr) |
1054 | bfd_vma data; | |
1055 | register bfd_byte *addr; | |
7ed4093a | 1056 | { |
b5b4294e | 1057 | #ifdef BFD64 |
536b27a5 SC |
1058 | addr[0] = (bfd_byte)(data >> (7*8)); |
1059 | addr[1] = (bfd_byte)(data >> (6*8)); | |
1060 | addr[2] = (bfd_byte)(data >> (5*8)); | |
1061 | addr[3] = (bfd_byte)(data >> (4*8)); | |
1062 | addr[4] = (bfd_byte)(data >> (3*8)); | |
1063 | addr[5] = (bfd_byte)(data >> (2*8)); | |
1064 | addr[6] = (bfd_byte)(data >> (1*8)); | |
1065 | addr[7] = (bfd_byte)(data >> (0*8)); | |
7ed4093a | 1066 | #else |
536b27a5 | 1067 | BFD_FAIL(); |
7ed4093a | 1068 | #endif |
7ed4093a SC |
1069 | } |
1070 | ||
1071 | void | |
f4bd7a8f DM |
1072 | bfd_putl64 (data, addr) |
1073 | bfd_vma data; | |
1074 | register bfd_byte *addr; | |
7ed4093a | 1075 | { |
b5b4294e | 1076 | #ifdef BFD64 |
536b27a5 SC |
1077 | addr[7] = (bfd_byte)(data >> (7*8)); |
1078 | addr[6] = (bfd_byte)(data >> (6*8)); | |
1079 | addr[5] = (bfd_byte)(data >> (5*8)); | |
1080 | addr[4] = (bfd_byte)(data >> (4*8)); | |
1081 | addr[3] = (bfd_byte)(data >> (3*8)); | |
1082 | addr[2] = (bfd_byte)(data >> (2*8)); | |
1083 | addr[1] = (bfd_byte)(data >> (1*8)); | |
1084 | addr[0] = (bfd_byte)(data >> (0*8)); | |
7ed4093a | 1085 | #else |
536b27a5 | 1086 | BFD_FAIL(); |
7ed4093a | 1087 | #endif |
7ed4093a | 1088 | } |
2203f786 JG |
1089 | \f |
1090 | /* Default implementation */ | |
4a81b561 | 1091 | |
2203f786 | 1092 | boolean |
6812b607 | 1093 | _bfd_generic_get_section_contents (abfd, section, location, offset, count) |
f4bd7a8f DM |
1094 | bfd *abfd; |
1095 | sec_ptr section; | |
1096 | PTR location; | |
1097 | file_ptr offset; | |
1098 | bfd_size_type count; | |
2203f786 JG |
1099 | { |
1100 | if (count == 0) | |
6f715d66 | 1101 | return true; |
f8e01940 JG |
1102 | if ((bfd_size_type)(offset+count) > section->_raw_size |
1103 | || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1 | |
6f715d66 SC |
1104 | || bfd_read(location, (bfd_size_type)1, count, abfd) != count) |
1105 | return (false); /* on error */ | |
2203f786 JG |
1106 | return (true); |
1107 | } | |
6f715d66 | 1108 | |
4fe6d901 KR |
1109 | boolean |
1110 | _bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count) | |
1111 | bfd *abfd; | |
1112 | sec_ptr section; | |
1113 | bfd_window *w; | |
1114 | file_ptr offset; | |
1115 | bfd_size_type count; | |
1116 | { | |
a9713b91 | 1117 | #ifdef USE_MMAP |
4fe6d901 KR |
1118 | if (count == 0) |
1119 | return true; | |
1120 | if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents) | |
1121 | { | |
1122 | /* We don't know what changes the bfd's get_section_contents | |
1123 | method may have to make. So punt trying to map the file | |
1124 | window, and let get_section_contents do its thing. */ | |
1125 | /* @@ FIXME : If the internal window has a refcount of 1 and was | |
1126 | allocated with malloc instead of mmap, just reuse it. */ | |
1127 | bfd_free_window (w); | |
1128 | w->i = (bfd_window_internal *) bfd_zmalloc (sizeof (bfd_window_internal)); | |
1129 | if (w->i == NULL) | |
1130 | return false; | |
64d5f5d0 | 1131 | w->i->data = (PTR) bfd_malloc ((size_t) count); |
4fe6d901 KR |
1132 | if (w->i->data == NULL) |
1133 | { | |
1134 | free (w->i); | |
1135 | w->i = NULL; | |
1136 | return false; | |
1137 | } | |
1138 | w->i->mapped = 0; | |
1139 | w->i->refcount = 1; | |
1140 | w->size = w->i->size = count; | |
1141 | w->data = w->i->data; | |
1142 | return bfd_get_section_contents (abfd, section, w->data, offset, count); | |
1143 | } | |
1144 | if ((bfd_size_type) (offset+count) > section->_raw_size | |
0bb8ff19 | 1145 | || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true) |
4fe6d901 KR |
1146 | == false)) |
1147 | return false; | |
1148 | return true; | |
a9713b91 ILT |
1149 | #else |
1150 | abort (); | |
1151 | #endif | |
4fe6d901 KR |
1152 | } |
1153 | ||
f58809fd SC |
1154 | /* This generic function can only be used in implementations where creating |
1155 | NEW sections is disallowed. It is useful in patching existing sections | |
1156 | in read-write files, though. See other set_section_contents functions | |
1157 | to see why it doesn't work for new sections. */ | |
1158 | boolean | |
6812b607 | 1159 | _bfd_generic_set_section_contents (abfd, section, location, offset, count) |
df34342b ILT |
1160 | bfd *abfd; |
1161 | sec_ptr section; | |
1162 | PTR location; | |
1163 | file_ptr offset; | |
1164 | bfd_size_type count; | |
f58809fd | 1165 | { |
df34342b ILT |
1166 | if (count == 0) |
1167 | return true; | |
1168 | ||
df34342b ILT |
1169 | if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1 |
1170 | || bfd_write (location, (bfd_size_type) 1, count, abfd) != count) | |
1171 | return false; | |
1172 | ||
1173 | return true; | |
f58809fd SC |
1174 | } |
1175 | ||
f8e01940 JG |
1176 | /* |
1177 | INTERNAL_FUNCTION | |
1178 | bfd_log2 | |
1179 | ||
17ecba1a DM |
1180 | SYNOPSIS |
1181 | unsigned int bfd_log2(bfd_vma x); | |
1182 | ||
f8e01940 | 1183 | DESCRIPTION |
c188b0be DM |
1184 | Return the log base 2 of the value supplied, rounded up. E.g., an |
1185 | @var{x} of 1025 returns 11. | |
f8e01940 | 1186 | */ |
6f715d66 | 1187 | |
09832674 ILT |
1188 | unsigned int |
1189 | bfd_log2 (x) | |
b5b4294e | 1190 | bfd_vma x; |
6f715d66 | 1191 | { |
09832674 ILT |
1192 | unsigned int result = 0; |
1193 | ||
1194 | while ((((bfd_vma) 1) << result) < x) | |
1195 | ++result; | |
6f715d66 SC |
1196 | return result; |
1197 | } | |
120f5bd9 JL |
1198 | |
1199 | boolean | |
09832674 | 1200 | bfd_generic_is_local_label_name (abfd, name) |
120f5bd9 | 1201 | bfd *abfd; |
09832674 | 1202 | const char *name; |
120f5bd9 JL |
1203 | { |
1204 | char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.'; | |
1205 | ||
09832674 | 1206 | return (name[0] == locals_prefix); |
120f5bd9 JL |
1207 | } |
1208 |