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