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