* config/tc-mn10300.c (tc_gen_reloc): Don't emit an *ABS*
[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
1e738b87 584 errno = ENOSPC;
252b5132
RH
585#endif
586 bfd_set_error (bfd_error_system_call);
587 }
588 return nwrote;
589}
590
591/*
592INTERNAL_FUNCTION
593 bfd_write_bigendian_4byte_int
594
595SYNOPSIS
dc810e39 596 void bfd_write_bigendian_4byte_int (bfd *, unsigned int);
252b5132
RH
597
598DESCRIPTION
599 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
600 endian order regardless of what else is going on. This is useful in
601 archives.
602
603*/
604void
605bfd_write_bigendian_4byte_int (abfd, i)
606 bfd *abfd;
dc810e39 607 unsigned int i;
252b5132
RH
608{
609 bfd_byte buffer[4];
dc810e39
AM
610 bfd_putb32 ((bfd_vma) i, buffer);
611 if (bfd_bwrite ((PTR) buffer, (bfd_size_type) 4, abfd) != 4)
252b5132
RH
612 abort ();
613}
614
dc810e39 615bfd_vma
252b5132
RH
616bfd_tell (abfd)
617 bfd *abfd;
618{
619 file_ptr ptr;
620
621 if ((abfd->flags & BFD_IN_MEMORY) != 0)
622 return abfd->where;
623
dc810e39 624 ptr = ftell (bfd_cache_lookup (abfd));
252b5132
RH
625
626 if (abfd->my_archive)
627 ptr -= abfd->origin;
628 abfd->where = ptr;
629 return ptr;
630}
631
632int
633bfd_flush (abfd)
634 bfd *abfd;
635{
636 if ((abfd->flags & BFD_IN_MEMORY) != 0)
637 return 0;
638 return fflush (bfd_cache_lookup(abfd));
639}
640
641/* Returns 0 for success, negative value for failure (in which case
642 bfd_get_error can retrieve the error code). */
643int
644bfd_stat (abfd, statbuf)
645 bfd *abfd;
646 struct stat *statbuf;
647{
648 FILE *f;
649 int result;
650
651 if ((abfd->flags & BFD_IN_MEMORY) != 0)
652 abort ();
653
654 f = bfd_cache_lookup (abfd);
655 if (f == NULL)
656 {
657 bfd_set_error (bfd_error_system_call);
658 return -1;
659 }
660 result = fstat (fileno (f), statbuf);
661 if (result < 0)
662 bfd_set_error (bfd_error_system_call);
663 return result;
664}
665
666/* Returns 0 for success, nonzero for failure (in which case bfd_get_error
667 can retrieve the error code). */
668
669int
670bfd_seek (abfd, position, direction)
671 bfd *abfd;
672 file_ptr position;
673 int direction;
674{
675 int result;
676 FILE *f;
dc810e39 677 long file_position;
252b5132
RH
678 /* For the time being, a BFD may not seek to it's end. The problem
679 is that we don't easily have a way to recognize the end of an
509945ae 680 element in an archive. */
252b5132
RH
681
682 BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
683
684 if (direction == SEEK_CUR && position == 0)
685 return 0;
686
687 if ((abfd->flags & BFD_IN_MEMORY) != 0)
688 {
2e4bb80e
NC
689 struct bfd_in_memory *bim;
690
691 bim = (struct bfd_in_memory *) abfd->iostream;
509945ae 692
252b5132
RH
693 if (direction == SEEK_SET)
694 abfd->where = position;
695 else
696 abfd->where += position;
509945ae 697
dc810e39 698 if (abfd->where > bim->size)
2e4bb80e 699 {
509945ae 700 if ((abfd->direction == write_direction) ||
e67f03db
DD
701 (abfd->direction == both_direction))
702 {
dc810e39
AM
703 bfd_size_type newsize, oldsize;
704 oldsize = (bim->size + 127) & ~(bfd_size_type) 127;
e67f03db
DD
705 bim->size = abfd->where;
706 /* Round up to cut down on memory fragmentation */
dc810e39 707 newsize = (bim->size + 127) & ~(bfd_size_type) 127;
e67f03db
DD
708 if (newsize > oldsize)
709 {
710 bim->buffer = bfd_realloc (bim->buffer, newsize);
711 if (bim->buffer == 0)
712 {
713 bim->size = 0;
e67f03db
DD
714 return -1;
715 }
716 }
717 }
718 else
719 {
720 abfd->where = bim->size;
721 bfd_set_error (bfd_error_file_truncated);
722 return -1;
509945ae 723 }
a022216b 724 }
252b5132
RH
725 return 0;
726 }
727
728 if (abfd->format != bfd_archive && abfd->my_archive == 0)
729 {
730#if 0
731 /* Explanation for this code: I'm only about 95+% sure that the above
732 conditions are sufficient and that all i/o calls are properly
733 adjusting the `where' field. So this is sort of an `assert'
734 that the `where' field is correct. If we can go a while without
735 tripping the abort, we can probably safely disable this code,
736 so that the real optimizations happen. */
737 file_ptr where_am_i_now;
738 where_am_i_now = ftell (bfd_cache_lookup (abfd));
739 if (abfd->my_archive)
740 where_am_i_now -= abfd->origin;
741 if (where_am_i_now != abfd->where)
742 abort ();
743#endif
dc810e39 744 if (direction == SEEK_SET && (bfd_vma) position == abfd->where)
252b5132
RH
745 return 0;
746 }
747 else
748 {
749 /* We need something smarter to optimize access to archives.
750 Currently, anything inside an archive is read via the file
751 handle for the archive. Which means that a bfd_seek on one
752 component affects the `current position' in the archive, as
753 well as in any other component.
754
755 It might be sufficient to put a spike through the cache
756 abstraction, and look to the archive for the file position,
757 but I think we should try for something cleaner.
758
759 In the meantime, no optimization for archives. */
760 }
761
762 f = bfd_cache_lookup (abfd);
763 file_position = position;
764 if (direction == SEEK_SET && abfd->my_archive != NULL)
765 file_position += abfd->origin;
766
767 result = fseek (f, file_position, direction);
252b5132
RH
768 if (result != 0)
769 {
770 int hold_errno = errno;
771
772 /* Force redetermination of `where' field. */
773 bfd_tell (abfd);
774
775 /* An EINVAL error probably means that the file offset was
776 absurd. */
777 if (hold_errno == EINVAL)
778 bfd_set_error (bfd_error_file_truncated);
779 else
780 {
781 bfd_set_error (bfd_error_system_call);
782 errno = hold_errno;
783 }
784 }
785 else
786 {
787 /* Adjust `where' field. */
788 if (direction == SEEK_SET)
789 abfd->where = position;
790 else
791 abfd->where += position;
792 }
793 return result;
794}
795\f
796/** The do-it-yourself (byte) sex-change kit */
797
798/* The middle letter e.g. get<b>short indicates Big or Little endian
799 target machine. It doesn't matter what the byte order of the host
800 machine is; these routines work for either. */
801
802/* FIXME: Should these take a count argument?
803 Answer (gnu@cygnus.com): No, but perhaps they should be inline
509945ae 804 functions in swap.h #ifdef __GNUC__.
252b5132
RH
805 Gprof them later and find out. */
806
807/*
808FUNCTION
809 bfd_put_size
810FUNCTION
811 bfd_get_size
812
813DESCRIPTION
814 These macros as used for reading and writing raw data in
815 sections; each access (except for bytes) is vectored through
816 the target format of the BFD and mangled accordingly. The
817 mangling performs any necessary endian translations and
818 removes alignment restrictions. Note that types accepted and
819 returned by these macros are identical so they can be swapped
820 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
821 to either <<bfd_get_32>> or <<bfd_get_64>>.
822
823 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
824 system without prototypes, the caller is responsible for making
825 sure that is true, with a cast if necessary. We don't cast
826 them in the macro definitions because that would prevent <<lint>>
827 or <<gcc -Wall>> from detecting sins such as passing a pointer.
828 To detect calling these with less than a <<bfd_vma>>, use
829 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
830
831.
832.{* Byte swapping macros for user section data. *}
833.
834.#define bfd_put_8(abfd, val, ptr) \
509945ae 835. ((void) (*((unsigned char *) (ptr)) = (unsigned char) (val)))
252b5132
RH
836.#define bfd_put_signed_8 \
837. bfd_put_8
838.#define bfd_get_8(abfd, ptr) \
dc810e39 839. (*(unsigned char *) (ptr) & 0xff)
252b5132 840.#define bfd_get_signed_8(abfd, ptr) \
dc810e39 841. (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
252b5132
RH
842.
843.#define bfd_put_16(abfd, val, ptr) \
844. BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
845.#define bfd_put_signed_16 \
846. bfd_put_16
847.#define bfd_get_16(abfd, ptr) \
848. BFD_SEND(abfd, bfd_getx16, (ptr))
849.#define bfd_get_signed_16(abfd, ptr) \
850. BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
851.
852.#define bfd_put_32(abfd, val, ptr) \
853. BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
854.#define bfd_put_signed_32 \
855. bfd_put_32
856.#define bfd_get_32(abfd, ptr) \
857. BFD_SEND(abfd, bfd_getx32, (ptr))
858.#define bfd_get_signed_32(abfd, ptr) \
859. BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
860.
861.#define bfd_put_64(abfd, val, ptr) \
862. BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
863.#define bfd_put_signed_64 \
864. bfd_put_64
865.#define bfd_get_64(abfd, ptr) \
866. BFD_SEND(abfd, bfd_getx64, (ptr))
867.#define bfd_get_signed_64(abfd, ptr) \
868. BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
869.
c7ac6ff8 870.#define bfd_get(bits, abfd, ptr) \
1e738b87 871. ( (bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
c7ac6ff8
MM
872. : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
873. : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
874. : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
875. : (abort (), (bfd_vma) - 1))
876.
877.#define bfd_put(bits, abfd, val, ptr) \
1e738b87 878. ( (bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
c7ac6ff8
MM
879. : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
880. : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
881. : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
882. : (abort (), (void) 0))
883.
509945ae 884*/
252b5132
RH
885
886/*
887FUNCTION
888 bfd_h_put_size
889 bfd_h_get_size
890
891DESCRIPTION
892 These macros have the same function as their <<bfd_get_x>>
dc810e39 893 brethren, except that they are used for removing information
252b5132
RH
894 for the header records of object files. Believe it or not,
895 some object files keep their header records in big endian
896 order and their data in little endian order.
897.
898.{* Byte swapping macros for file header data. *}
899.
900.#define bfd_h_put_8(abfd, val, ptr) \
dc810e39 901. bfd_put_8 (abfd, val, ptr)
252b5132 902.#define bfd_h_put_signed_8(abfd, val, ptr) \
dc810e39 903. bfd_put_8 (abfd, val, ptr)
252b5132 904.#define bfd_h_get_8(abfd, ptr) \
dc810e39 905. bfd_get_8 (abfd, ptr)
252b5132 906.#define bfd_h_get_signed_8(abfd, ptr) \
dc810e39 907. bfd_get_signed_8 (abfd, ptr)
252b5132
RH
908.
909.#define bfd_h_put_16(abfd, val, ptr) \
dc810e39 910. BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
252b5132 911.#define bfd_h_put_signed_16 \
dc810e39 912. bfd_h_put_16
252b5132 913.#define bfd_h_get_16(abfd, ptr) \
dc810e39 914. BFD_SEND (abfd, bfd_h_getx16, (ptr))
252b5132 915.#define bfd_h_get_signed_16(abfd, ptr) \
dc810e39 916. BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
252b5132
RH
917.
918.#define bfd_h_put_32(abfd, val, ptr) \
dc810e39 919. BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
252b5132 920.#define bfd_h_put_signed_32 \
dc810e39 921. bfd_h_put_32
252b5132 922.#define bfd_h_get_32(abfd, ptr) \
dc810e39 923. BFD_SEND (abfd, bfd_h_getx32, (ptr))
252b5132 924.#define bfd_h_get_signed_32(abfd, ptr) \
dc810e39 925. BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
252b5132
RH
926.
927.#define bfd_h_put_64(abfd, val, ptr) \
dc810e39 928. BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
252b5132 929.#define bfd_h_put_signed_64 \
dc810e39 930. bfd_h_put_64
252b5132 931.#define bfd_h_get_64(abfd, ptr) \
dc810e39 932. BFD_SEND (abfd, bfd_h_getx64, (ptr))
252b5132 933.#define bfd_h_get_signed_64(abfd, ptr) \
dc810e39 934. BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
252b5132 935.
dc810e39
AM
936.{* Refinements on the above, which should eventually go away. Save
937. cluttering the source with (bfd_vma) and (bfd_byte *) casts. *}
938.
939.#define H_PUT_64(abfd, val, where) \
940. bfd_h_put_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
941.
942.#define H_PUT_32(abfd, val, where) \
943. bfd_h_put_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
944.
945.#define H_PUT_16(abfd, val, where) \
946. bfd_h_put_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
947.
948.#define H_PUT_8 bfd_h_put_8
949.
950.#define H_PUT_S64(abfd, val, where) \
951. bfd_h_put_signed_64 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
952.
953.#define H_PUT_S32(abfd, val, where) \
954. bfd_h_put_signed_32 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
955.
956.#define H_PUT_S16(abfd, val, where) \
957. bfd_h_put_signed_16 ((abfd), (bfd_vma) (val), (bfd_byte *) (where))
958.
959.#define H_PUT_S8 bfd_h_put_signed_8
960.
961.#define H_GET_64(abfd, where) \
962. bfd_h_get_64 ((abfd), (bfd_byte *) (where))
963.
964.#define H_GET_32(abfd, where) \
965. bfd_h_get_32 ((abfd), (bfd_byte *) (where))
966.
967.#define H_GET_16(abfd, where) \
968. bfd_h_get_16 ((abfd), (bfd_byte *) (where))
969.
970.#define H_GET_8 bfd_h_get_8
971.
972.#define H_GET_S64(abfd, where) \
973. bfd_h_get_signed_64 ((abfd), (bfd_byte *) (where))
974.
975.#define H_GET_S32(abfd, where) \
976. bfd_h_get_signed_32 ((abfd), (bfd_byte *) (where))
977.
978.#define H_GET_S16(abfd, where) \
979. bfd_h_get_signed_16 ((abfd), (bfd_byte *) (where))
980.
981.#define H_GET_S8 bfd_h_get_signed_8
982.
983.*/
252b5132
RH
984
985/* Sign extension to bfd_signed_vma. */
986#define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
987#define COERCE32(x) \
988 ((bfd_signed_vma) (long) (((unsigned long) (x) ^ 0x80000000) - 0x80000000))
989#define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
990#define COERCE64(x) \
991 (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
992
993bfd_vma
994bfd_getb16 (addr)
995 register const bfd_byte *addr;
996{
997 return (addr[0] << 8) | addr[1];
998}
999
1000bfd_vma
1001bfd_getl16 (addr)
1002 register const bfd_byte *addr;
1003{
1004 return (addr[1] << 8) | addr[0];
1005}
1006
1007bfd_signed_vma
1008bfd_getb_signed_16 (addr)
1009 register const bfd_byte *addr;
1010{
1011 return COERCE16((addr[0] << 8) | addr[1]);
1012}
1013
1014bfd_signed_vma
1015bfd_getl_signed_16 (addr)
1016 register const bfd_byte *addr;
1017{
1018 return COERCE16((addr[1] << 8) | addr[0]);
1019}
1020
1021void
1022bfd_putb16 (data, addr)
1023 bfd_vma data;
1024 register bfd_byte *addr;
1025{
509945ae 1026 addr[0] = (bfd_byte) (data >> 8);
708b82c7 1027 addr[1] = (bfd_byte) data;
252b5132
RH
1028}
1029
1030void
1031bfd_putl16 (data, addr)
509945ae 1032 bfd_vma data;
252b5132
RH
1033 register bfd_byte *addr;
1034{
708b82c7 1035 addr[0] = (bfd_byte) data;
509945ae 1036 addr[1] = (bfd_byte) (data >> 8);
252b5132
RH
1037}
1038
1039bfd_vma
1040bfd_getb32 (addr)
1041 register const bfd_byte *addr;
1042{
1043 unsigned long v;
1044
1045 v = (unsigned long) addr[0] << 24;
1046 v |= (unsigned long) addr[1] << 16;
1047 v |= (unsigned long) addr[2] << 8;
1048 v |= (unsigned long) addr[3];
1049 return (bfd_vma) v;
1050}
1051
1052bfd_vma
1053bfd_getl32 (addr)
1054 register const bfd_byte *addr;
1055{
1056 unsigned long v;
1057
1058 v = (unsigned long) addr[0];
1059 v |= (unsigned long) addr[1] << 8;
1060 v |= (unsigned long) addr[2] << 16;
1061 v |= (unsigned long) addr[3] << 24;
1062 return (bfd_vma) v;
1063}
1064
1065bfd_signed_vma
1066bfd_getb_signed_32 (addr)
1067 register const bfd_byte *addr;
1068{
1069 unsigned long v;
1070
1071 v = (unsigned long) addr[0] << 24;
1072 v |= (unsigned long) addr[1] << 16;
1073 v |= (unsigned long) addr[2] << 8;
1074 v |= (unsigned long) addr[3];
1075 return COERCE32 (v);
1076}
1077
1078bfd_signed_vma
1079bfd_getl_signed_32 (addr)
1080 register const bfd_byte *addr;
1081{
1082 unsigned long v;
1083
1084 v = (unsigned long) addr[0];
1085 v |= (unsigned long) addr[1] << 8;
1086 v |= (unsigned long) addr[2] << 16;
1087 v |= (unsigned long) addr[3] << 24;
1088 return COERCE32 (v);
1089}
1090
1091bfd_vma
1092bfd_getb64 (addr)
7442e600 1093 register const bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1094{
1095#ifdef BFD64
1096 bfd_vma low, high;
1097
1098 high= ((((((((addr[0]) << 8) |
1099 addr[1]) << 8) |
1100 addr[2]) << 8) |
1101 addr[3]) );
1102
1103 low = (((((((((bfd_vma)addr[4]) << 8) |
1104 addr[5]) << 8) |
1105 addr[6]) << 8) |
1106 addr[7]));
1107
1108 return high << 32 | low;
1109#else
1110 BFD_FAIL();
1111 return 0;
1112#endif
1113}
1114
1115bfd_vma
1116bfd_getl64 (addr)
7442e600 1117 register const bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1118{
1119#ifdef BFD64
1120 bfd_vma low, high;
1121 high= (((((((addr[7] << 8) |
1122 addr[6]) << 8) |
1123 addr[5]) << 8) |
1124 addr[4]));
1125
1126 low = ((((((((bfd_vma)addr[3] << 8) |
1127 addr[2]) << 8) |
1128 addr[1]) << 8) |
1129 addr[0]) );
1130
1131 return high << 32 | low;
1132#else
1133 BFD_FAIL();
1134 return 0;
1135#endif
1136
1137}
1138
1139bfd_signed_vma
1140bfd_getb_signed_64 (addr)
7442e600 1141 register const bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1142{
1143#ifdef BFD64
1144 bfd_vma low, high;
1145
1146 high= ((((((((addr[0]) << 8) |
1147 addr[1]) << 8) |
1148 addr[2]) << 8) |
1149 addr[3]) );
1150
1151 low = (((((((((bfd_vma)addr[4]) << 8) |
1152 addr[5]) << 8) |
1153 addr[6]) << 8) |
1154 addr[7]));
1155
1156 return COERCE64(high << 32 | low);
1157#else
1158 BFD_FAIL();
1159 return 0;
1160#endif
1161}
1162
1163bfd_signed_vma
1164bfd_getl_signed_64 (addr)
7442e600 1165 register const bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1166{
1167#ifdef BFD64
1168 bfd_vma low, high;
1169 high= (((((((addr[7] << 8) |
1170 addr[6]) << 8) |
1171 addr[5]) << 8) |
1172 addr[4]));
1173
1174 low = ((((((((bfd_vma)addr[3] << 8) |
1175 addr[2]) << 8) |
1176 addr[1]) << 8) |
1177 addr[0]) );
1178
1179 return COERCE64(high << 32 | low);
1180#else
1181 BFD_FAIL();
1182 return 0;
1183#endif
1184}
1185
1186void
1187bfd_putb32 (data, addr)
1188 bfd_vma data;
1189 register bfd_byte *addr;
1190{
509945ae
KH
1191 addr[0] = (bfd_byte) (data >> 24);
1192 addr[1] = (bfd_byte) (data >> 16);
1193 addr[2] = (bfd_byte) (data >> 8);
708b82c7 1194 addr[3] = (bfd_byte) data;
252b5132
RH
1195}
1196
1197void
1198bfd_putl32 (data, addr)
1199 bfd_vma data;
1200 register bfd_byte *addr;
1201{
708b82c7 1202 addr[0] = (bfd_byte) data;
509945ae
KH
1203 addr[1] = (bfd_byte) (data >> 8);
1204 addr[2] = (bfd_byte) (data >> 16);
1205 addr[3] = (bfd_byte) (data >> 24);
252b5132
RH
1206}
1207
1208void
1209bfd_putb64 (data, addr)
7442e600
ILT
1210 bfd_vma data ATTRIBUTE_UNUSED;
1211 register bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1212{
1213#ifdef BFD64
509945ae
KH
1214 addr[0] = (bfd_byte) (data >> (7*8));
1215 addr[1] = (bfd_byte) (data >> (6*8));
1216 addr[2] = (bfd_byte) (data >> (5*8));
1217 addr[3] = (bfd_byte) (data >> (4*8));
1218 addr[4] = (bfd_byte) (data >> (3*8));
1219 addr[5] = (bfd_byte) (data >> (2*8));
1220 addr[6] = (bfd_byte) (data >> (1*8));
1221 addr[7] = (bfd_byte) (data >> (0*8));
252b5132
RH
1222#else
1223 BFD_FAIL();
1224#endif
1225}
1226
1227void
1228bfd_putl64 (data, addr)
7442e600
ILT
1229 bfd_vma data ATTRIBUTE_UNUSED;
1230 register bfd_byte *addr ATTRIBUTE_UNUSED;
252b5132
RH
1231{
1232#ifdef BFD64
509945ae
KH
1233 addr[7] = (bfd_byte) (data >> (7*8));
1234 addr[6] = (bfd_byte) (data >> (6*8));
1235 addr[5] = (bfd_byte) (data >> (5*8));
1236 addr[4] = (bfd_byte) (data >> (4*8));
1237 addr[3] = (bfd_byte) (data >> (3*8));
1238 addr[2] = (bfd_byte) (data >> (2*8));
1239 addr[1] = (bfd_byte) (data >> (1*8));
1240 addr[0] = (bfd_byte) (data >> (0*8));
252b5132
RH
1241#else
1242 BFD_FAIL();
1243#endif
1244}
8c603c85
NC
1245
1246void
1247bfd_put_bits (data, addr, bits, big_p)
1248 bfd_vma data;
1249 bfd_byte *addr;
1250 int bits;
1251 boolean big_p;
1252{
1253 int i;
1254 int bytes;
1255
1256 if (bits % 8 != 0)
1257 abort ();
1258
1259 bytes = bits / 8;
1260 for (i = 0; i < bytes; i++)
1261 {
1262 int index = big_p ? bytes - i - 1 : i;
1263
1264 addr[index] = (bfd_byte) data;
1265 data >>= 8;
1266 }
1267}
1268
1269bfd_vma
1270bfd_get_bits (addr, bits, big_p)
1271 bfd_byte *addr;
1272 int bits;
1273 boolean big_p;
1274{
1275 bfd_vma data;
1276 int i;
1277 int bytes;
1278
1279 if (bits % 8 != 0)
1280 abort ();
1281
1282 data = 0;
1283 bytes = bits / 8;
1284 for (i = 0; i < bytes; i++)
1285 {
1286 int index = big_p ? i : bytes - i - 1;
509945ae 1287
8c603c85
NC
1288 data = (data << 8) | addr[index];
1289 }
1290
1291 return data;
1292}
252b5132
RH
1293\f
1294/* Default implementation */
1295
1296boolean
1297_bfd_generic_get_section_contents (abfd, section, location, offset, count)
1298 bfd *abfd;
1299 sec_ptr section;
1300 PTR location;
1301 file_ptr offset;
1302 bfd_size_type count;
1303{
0bff3f4b
ILT
1304 if (count == 0)
1305 return true;
1306
dc810e39 1307 if (offset + count > section->_raw_size)
0bff3f4b
ILT
1308 {
1309 bfd_set_error (bfd_error_invalid_operation);
1310 return false;
1311 }
1312
1313 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
dc810e39 1314 || bfd_bread (location, count, abfd) != count)
0bff3f4b
ILT
1315 return false;
1316
1317 return true;
252b5132
RH
1318}
1319
1320boolean
1321_bfd_generic_get_section_contents_in_window (abfd, section, w, offset, count)
7442e600
ILT
1322 bfd *abfd ATTRIBUTE_UNUSED;
1323 sec_ptr section ATTRIBUTE_UNUSED;
1324 bfd_window *w ATTRIBUTE_UNUSED;
1325 file_ptr offset ATTRIBUTE_UNUSED;
1326 bfd_size_type count ATTRIBUTE_UNUSED;
252b5132
RH
1327{
1328#ifdef USE_MMAP
1329 if (count == 0)
1330 return true;
1331 if (abfd->xvec->_bfd_get_section_contents != _bfd_generic_get_section_contents)
1332 {
1333 /* We don't know what changes the bfd's get_section_contents
1334 method may have to make. So punt trying to map the file
1335 window, and let get_section_contents do its thing. */
1336 /* @@ FIXME : If the internal window has a refcount of 1 and was
1337 allocated with malloc instead of mmap, just reuse it. */
1338 bfd_free_window (w);
dc810e39
AM
1339 w->i = ((bfd_window_internal *)
1340 bfd_zmalloc ((bfd_size_type) sizeof (bfd_window_internal)));
252b5132
RH
1341 if (w->i == NULL)
1342 return false;
dc810e39 1343 w->i->data = (PTR) bfd_malloc (count);
252b5132
RH
1344 if (w->i->data == NULL)
1345 {
1346 free (w->i);
1347 w->i = NULL;
1348 return false;
1349 }
1350 w->i->mapped = 0;
1351 w->i->refcount = 1;
1352 w->size = w->i->size = count;
1353 w->data = w->i->data;
1354 return bfd_get_section_contents (abfd, section, w->data, offset, count);
1355 }
dc810e39 1356 if (offset + count > section->_raw_size
252b5132
RH
1357 || (bfd_get_file_window (abfd, section->filepos + offset, count, w, true)
1358 == false))
1359 return false;
1360 return true;
1361#else
1362 abort ();
1363#endif
1364}
1365
1366/* This generic function can only be used in implementations where creating
1367 NEW sections is disallowed. It is useful in patching existing sections
1368 in read-write files, though. See other set_section_contents functions
1369 to see why it doesn't work for new sections. */
1370boolean
1371_bfd_generic_set_section_contents (abfd, section, location, offset, count)
1372 bfd *abfd;
1373 sec_ptr section;
1374 PTR location;
1375 file_ptr offset;
1376 bfd_size_type count;
1377{
1378 if (count == 0)
1379 return true;
1380
dc810e39
AM
1381 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
1382 || bfd_bwrite (location, count, abfd) != count)
252b5132
RH
1383 return false;
1384
1385 return true;
1386}
1387
1388/*
1389INTERNAL_FUNCTION
1390 bfd_log2
1391
1392SYNOPSIS
dc810e39 1393 unsigned int bfd_log2 (bfd_vma x);
252b5132
RH
1394
1395DESCRIPTION
1396 Return the log base 2 of the value supplied, rounded up. E.g., an
dc810e39 1397 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
252b5132
RH
1398*/
1399
1400unsigned int
1401bfd_log2 (x)
1402 bfd_vma x;
1403{
1404 unsigned int result = 0;
1405
86b21447 1406 while ((x = (x >> 1)) != 0)
252b5132
RH
1407 ++result;
1408 return result;
1409}
1410
1411boolean
1412bfd_generic_is_local_label_name (abfd, name)
1413 bfd *abfd;
1414 const char *name;
1415{
1416 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
1417
1418 return (name[0] == locals_prefix);
1419}
1420
875f7f69
JR
1421/* Can be used from / for bfd_merge_private_bfd_data to check that
1422 endianness matches between input and output file. Returns
1423 true for a match, otherwise returns false and emits an error. */
1424boolean
1425_bfd_generic_verify_endian_match (ibfd, obfd)
1426 bfd *ibfd;
1427 bfd *obfd;
1428{
1429 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
1fe494a5 1430 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
875f7f69
JR
1431 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
1432 {
1fe494a5
NC
1433 const char *msg;
1434
1435 if (bfd_big_endian (ibfd))
1436 msg = _("%s: compiled for a big endian system and target is little endian");
1437 else
1438 msg = _("%s: compiled for a little endian system and target is big endian");
1439
8f615d07 1440 (*_bfd_error_handler) (msg, bfd_archive_filename (ibfd));
875f7f69
JR
1441
1442 bfd_set_error (bfd_error_wrong_format);
1443 return false;
1444 }
1445
1446 return true;
1447}
dc810e39
AM
1448
1449/* Give a warning at runtime if someone compiles code which calls
1450 old routines. */
1451void
1452warn_deprecated (what, file, line, func)
1453 const char *what;
1454 const char *file;
1455 int line;
1456 const char *func;
1457{
1458 /* Poor man's tracking of functions we've already warned about. */
1459 static size_t mask = 0;
1460
1461 if (~(size_t) func & ~mask)
1462 {
1463 fprintf (stderr, _("Deprecated %s called"), what);
1464 if (func)
1465 fprintf (stderr, _(" at %s line %d in %s\n"), file, line, func);
1466 else
1467 fprintf (stderr, "\n");
1468 mask |= ~(size_t) func;
1469 }
1470}
This page took 0.194478 seconds and 4 git commands to generate.