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