* arm-dis.c: Correct top-level comment.
[binutils.git] / bfd / libbfd.c
blob2791ca32a70593df16647bd0aae5c0201cb07dbb
1 /* Assorted BFD support routines, only used internally.
2 Copyright 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004
4 Free Software Foundation, Inc.
5 Written by Cygnus Support.
7 This file is part of BFD, the Binary File Descriptor library.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
23 #include "bfd.h"
24 #include "sysdep.h"
25 #include "libbfd.h"
27 #ifndef HAVE_GETPAGESIZE
28 #define getpagesize() 2048
29 #endif
32 SECTION
33 Internal functions
35 DESCRIPTION
36 These routines are used within BFD.
37 They are not intended for export, but are documented here for
38 completeness.
41 /* A routine which is used in target vectors for unsupported
42 operations. */
44 bfd_boolean
45 bfd_false (bfd *ignore ATTRIBUTE_UNUSED)
47 bfd_set_error (bfd_error_invalid_operation);
48 return FALSE;
51 /* A routine which is used in target vectors for supported operations
52 which do not actually do anything. */
54 bfd_boolean
55 bfd_true (bfd *ignore ATTRIBUTE_UNUSED)
57 return TRUE;
60 /* A routine which is used in target vectors for unsupported
61 operations which return a pointer value. */
63 void *
64 bfd_nullvoidptr (bfd *ignore ATTRIBUTE_UNUSED)
66 bfd_set_error (bfd_error_invalid_operation);
67 return NULL;
70 int
71 bfd_0 (bfd *ignore ATTRIBUTE_UNUSED)
73 return 0;
76 unsigned int
77 bfd_0u (bfd *ignore ATTRIBUTE_UNUSED)
79 return 0;
82 long
83 bfd_0l (bfd *ignore ATTRIBUTE_UNUSED)
85 return 0;
88 /* A routine which is used in target vectors for unsupported
89 operations which return -1 on error. */
91 long
92 _bfd_n1 (bfd *ignore_abfd ATTRIBUTE_UNUSED)
94 bfd_set_error (bfd_error_invalid_operation);
95 return -1;
98 void
99 bfd_void (bfd *ignore ATTRIBUTE_UNUSED)
103 bfd_boolean
104 _bfd_nocore_core_file_matches_executable_p
105 (bfd *ignore_core_bfd ATTRIBUTE_UNUSED,
106 bfd *ignore_exec_bfd ATTRIBUTE_UNUSED)
108 bfd_set_error (bfd_error_invalid_operation);
109 return FALSE;
112 /* Routine to handle core_file_failing_command entry point for targets
113 without core file support. */
115 char *
116 _bfd_nocore_core_file_failing_command (bfd *ignore_abfd ATTRIBUTE_UNUSED)
118 bfd_set_error (bfd_error_invalid_operation);
119 return NULL;
122 /* Routine to handle core_file_failing_signal entry point for targets
123 without core file support. */
126 _bfd_nocore_core_file_failing_signal (bfd *ignore_abfd ATTRIBUTE_UNUSED)
128 bfd_set_error (bfd_error_invalid_operation);
129 return 0;
132 const bfd_target *
133 _bfd_dummy_target (bfd *ignore_abfd ATTRIBUTE_UNUSED)
135 bfd_set_error (bfd_error_wrong_format);
136 return 0;
139 /* Allocate memory using malloc. */
141 void *
142 bfd_malloc (bfd_size_type size)
144 void *ptr;
146 if (size != (size_t) size)
148 bfd_set_error (bfd_error_no_memory);
149 return NULL;
152 ptr = malloc ((size_t) size);
153 if (ptr == NULL && (size_t) size != 0)
154 bfd_set_error (bfd_error_no_memory);
156 return ptr;
159 /* Reallocate memory using realloc. */
161 void *
162 bfd_realloc (void *ptr, bfd_size_type size)
164 void *ret;
166 if (size != (size_t) size)
168 bfd_set_error (bfd_error_no_memory);
169 return NULL;
172 if (ptr == NULL)
173 ret = malloc ((size_t) size);
174 else
175 ret = realloc (ptr, (size_t) size);
177 if (ret == NULL && (size_t) size != 0)
178 bfd_set_error (bfd_error_no_memory);
180 return ret;
183 /* Allocate memory using malloc and clear it. */
185 void *
186 bfd_zmalloc (bfd_size_type size)
188 void *ptr;
190 if (size != (size_t) size)
192 bfd_set_error (bfd_error_no_memory);
193 return NULL;
196 ptr = malloc ((size_t) size);
198 if ((size_t) size != 0)
200 if (ptr == NULL)
201 bfd_set_error (bfd_error_no_memory);
202 else
203 memset (ptr, 0, (size_t) size);
206 return ptr;
209 INTERNAL_FUNCTION
210 bfd_write_bigendian_4byte_int
212 SYNOPSIS
213 bfd_boolean bfd_write_bigendian_4byte_int (bfd *, unsigned int);
215 DESCRIPTION
216 Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
217 endian order regardless of what else is going on. This is useful in
218 archives.
221 bfd_boolean
222 bfd_write_bigendian_4byte_int (bfd *abfd, unsigned int i)
224 bfd_byte buffer[4];
225 bfd_putb32 ((bfd_vma) i, buffer);
226 return bfd_bwrite (buffer, (bfd_size_type) 4, abfd) == 4;
230 /** The do-it-yourself (byte) sex-change kit */
232 /* The middle letter e.g. get<b>short indicates Big or Little endian
233 target machine. It doesn't matter what the byte order of the host
234 machine is; these routines work for either. */
236 /* FIXME: Should these take a count argument?
237 Answer (gnu@cygnus.com): No, but perhaps they should be inline
238 functions in swap.h #ifdef __GNUC__.
239 Gprof them later and find out. */
242 FUNCTION
243 bfd_put_size
244 FUNCTION
245 bfd_get_size
247 DESCRIPTION
248 These macros as used for reading and writing raw data in
249 sections; each access (except for bytes) is vectored through
250 the target format of the BFD and mangled accordingly. The
251 mangling performs any necessary endian translations and
252 removes alignment restrictions. Note that types accepted and
253 returned by these macros are identical so they can be swapped
254 around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
255 to either <<bfd_get_32>> or <<bfd_get_64>>.
257 In the put routines, @var{val} must be a <<bfd_vma>>. If we are on a
258 system without prototypes, the caller is responsible for making
259 sure that is true, with a cast if necessary. We don't cast
260 them in the macro definitions because that would prevent <<lint>>
261 or <<gcc -Wall>> from detecting sins such as passing a pointer.
262 To detect calling these with less than a <<bfd_vma>>, use
263 <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
266 .{* Byte swapping macros for user section data. *}
268 .#define bfd_put_8(abfd, val, ptr) \
269 . ((void) (*((unsigned char *) (ptr)) = (val) & 0xff))
270 .#define bfd_put_signed_8 \
271 . bfd_put_8
272 .#define bfd_get_8(abfd, ptr) \
273 . (*(unsigned char *) (ptr) & 0xff)
274 .#define bfd_get_signed_8(abfd, ptr) \
275 . (((*(unsigned char *) (ptr) & 0xff) ^ 0x80) - 0x80)
277 .#define bfd_put_16(abfd, val, ptr) \
278 . BFD_SEND (abfd, bfd_putx16, ((val),(ptr)))
279 .#define bfd_put_signed_16 \
280 . bfd_put_16
281 .#define bfd_get_16(abfd, ptr) \
282 . BFD_SEND (abfd, bfd_getx16, (ptr))
283 .#define bfd_get_signed_16(abfd, ptr) \
284 . BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
286 .#define bfd_put_32(abfd, val, ptr) \
287 . BFD_SEND (abfd, bfd_putx32, ((val),(ptr)))
288 .#define bfd_put_signed_32 \
289 . bfd_put_32
290 .#define bfd_get_32(abfd, ptr) \
291 . BFD_SEND (abfd, bfd_getx32, (ptr))
292 .#define bfd_get_signed_32(abfd, ptr) \
293 . BFD_SEND (abfd, bfd_getx_signed_32, (ptr))
295 .#define bfd_put_64(abfd, val, ptr) \
296 . BFD_SEND (abfd, bfd_putx64, ((val), (ptr)))
297 .#define bfd_put_signed_64 \
298 . bfd_put_64
299 .#define bfd_get_64(abfd, ptr) \
300 . BFD_SEND (abfd, bfd_getx64, (ptr))
301 .#define bfd_get_signed_64(abfd, ptr) \
302 . BFD_SEND (abfd, bfd_getx_signed_64, (ptr))
304 .#define bfd_get(bits, abfd, ptr) \
305 . ((bits) == 8 ? (bfd_vma) bfd_get_8 (abfd, ptr) \
306 . : (bits) == 16 ? bfd_get_16 (abfd, ptr) \
307 . : (bits) == 32 ? bfd_get_32 (abfd, ptr) \
308 . : (bits) == 64 ? bfd_get_64 (abfd, ptr) \
309 . : (abort (), (bfd_vma) - 1))
311 .#define bfd_put(bits, abfd, val, ptr) \
312 . ((bits) == 8 ? bfd_put_8 (abfd, val, ptr) \
313 . : (bits) == 16 ? bfd_put_16 (abfd, val, ptr) \
314 . : (bits) == 32 ? bfd_put_32 (abfd, val, ptr) \
315 . : (bits) == 64 ? bfd_put_64 (abfd, val, ptr) \
316 . : (abort (), (void) 0))
321 FUNCTION
322 bfd_h_put_size
323 bfd_h_get_size
325 DESCRIPTION
326 These macros have the same function as their <<bfd_get_x>>
327 brethren, except that they are used for removing information
328 for the header records of object files. Believe it or not,
329 some object files keep their header records in big endian
330 order and their data in little endian order.
332 .{* Byte swapping macros for file header data. *}
334 .#define bfd_h_put_8(abfd, val, ptr) \
335 . bfd_put_8 (abfd, val, ptr)
336 .#define bfd_h_put_signed_8(abfd, val, ptr) \
337 . bfd_put_8 (abfd, val, ptr)
338 .#define bfd_h_get_8(abfd, ptr) \
339 . bfd_get_8 (abfd, ptr)
340 .#define bfd_h_get_signed_8(abfd, ptr) \
341 . bfd_get_signed_8 (abfd, ptr)
343 .#define bfd_h_put_16(abfd, val, ptr) \
344 . BFD_SEND (abfd, bfd_h_putx16, (val, ptr))
345 .#define bfd_h_put_signed_16 \
346 . bfd_h_put_16
347 .#define bfd_h_get_16(abfd, ptr) \
348 . BFD_SEND (abfd, bfd_h_getx16, (ptr))
349 .#define bfd_h_get_signed_16(abfd, ptr) \
350 . BFD_SEND (abfd, bfd_h_getx_signed_16, (ptr))
352 .#define bfd_h_put_32(abfd, val, ptr) \
353 . BFD_SEND (abfd, bfd_h_putx32, (val, ptr))
354 .#define bfd_h_put_signed_32 \
355 . bfd_h_put_32
356 .#define bfd_h_get_32(abfd, ptr) \
357 . BFD_SEND (abfd, bfd_h_getx32, (ptr))
358 .#define bfd_h_get_signed_32(abfd, ptr) \
359 . BFD_SEND (abfd, bfd_h_getx_signed_32, (ptr))
361 .#define bfd_h_put_64(abfd, val, ptr) \
362 . BFD_SEND (abfd, bfd_h_putx64, (val, ptr))
363 .#define bfd_h_put_signed_64 \
364 . bfd_h_put_64
365 .#define bfd_h_get_64(abfd, ptr) \
366 . BFD_SEND (abfd, bfd_h_getx64, (ptr))
367 .#define bfd_h_get_signed_64(abfd, ptr) \
368 . BFD_SEND (abfd, bfd_h_getx_signed_64, (ptr))
370 .{* Aliases for the above, which should eventually go away. *}
372 .#define H_PUT_64 bfd_h_put_64
373 .#define H_PUT_32 bfd_h_put_32
374 .#define H_PUT_16 bfd_h_put_16
375 .#define H_PUT_8 bfd_h_put_8
376 .#define H_PUT_S64 bfd_h_put_signed_64
377 .#define H_PUT_S32 bfd_h_put_signed_32
378 .#define H_PUT_S16 bfd_h_put_signed_16
379 .#define H_PUT_S8 bfd_h_put_signed_8
380 .#define H_GET_64 bfd_h_get_64
381 .#define H_GET_32 bfd_h_get_32
382 .#define H_GET_16 bfd_h_get_16
383 .#define H_GET_8 bfd_h_get_8
384 .#define H_GET_S64 bfd_h_get_signed_64
385 .#define H_GET_S32 bfd_h_get_signed_32
386 .#define H_GET_S16 bfd_h_get_signed_16
387 .#define H_GET_S8 bfd_h_get_signed_8
391 /* Sign extension to bfd_signed_vma. */
392 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
393 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
394 #define EIGHT_GAZILLION ((bfd_int64_t) 1 << 63)
395 #define COERCE64(x) \
396 (((bfd_int64_t) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
398 bfd_vma
399 bfd_getb16 (const void *p)
401 const bfd_byte *addr = p;
402 return (addr[0] << 8) | addr[1];
405 bfd_vma
406 bfd_getl16 (const void *p)
408 const bfd_byte *addr = p;
409 return (addr[1] << 8) | addr[0];
412 bfd_signed_vma
413 bfd_getb_signed_16 (const void *p)
415 const bfd_byte *addr = p;
416 return COERCE16 ((addr[0] << 8) | addr[1]);
419 bfd_signed_vma
420 bfd_getl_signed_16 (const void *p)
422 const bfd_byte *addr = p;
423 return COERCE16 ((addr[1] << 8) | addr[0]);
426 void
427 bfd_putb16 (bfd_vma data, void *p)
429 bfd_byte *addr = p;
430 addr[0] = (data >> 8) & 0xff;
431 addr[1] = data & 0xff;
434 void
435 bfd_putl16 (bfd_vma data, void *p)
437 bfd_byte *addr = p;
438 addr[0] = data & 0xff;
439 addr[1] = (data >> 8) & 0xff;
442 bfd_vma
443 bfd_getb32 (const void *p)
445 const bfd_byte *addr = p;
446 unsigned long v;
448 v = (unsigned long) addr[0] << 24;
449 v |= (unsigned long) addr[1] << 16;
450 v |= (unsigned long) addr[2] << 8;
451 v |= (unsigned long) addr[3];
452 return v;
455 bfd_vma
456 bfd_getl32 (const void *p)
458 const bfd_byte *addr = p;
459 unsigned long v;
461 v = (unsigned long) addr[0];
462 v |= (unsigned long) addr[1] << 8;
463 v |= (unsigned long) addr[2] << 16;
464 v |= (unsigned long) addr[3] << 24;
465 return v;
468 bfd_signed_vma
469 bfd_getb_signed_32 (const void *p)
471 const bfd_byte *addr = p;
472 unsigned long v;
474 v = (unsigned long) addr[0] << 24;
475 v |= (unsigned long) addr[1] << 16;
476 v |= (unsigned long) addr[2] << 8;
477 v |= (unsigned long) addr[3];
478 return COERCE32 (v);
481 bfd_signed_vma
482 bfd_getl_signed_32 (const void *p)
484 const bfd_byte *addr = p;
485 unsigned long v;
487 v = (unsigned long) addr[0];
488 v |= (unsigned long) addr[1] << 8;
489 v |= (unsigned long) addr[2] << 16;
490 v |= (unsigned long) addr[3] << 24;
491 return COERCE32 (v);
494 bfd_uint64_t
495 bfd_getb64 (const void *p ATTRIBUTE_UNUSED)
497 #ifdef BFD_HOST_64_BIT
498 const bfd_byte *addr = p;
499 bfd_uint64_t v;
501 v = addr[0]; v <<= 8;
502 v |= addr[1]; v <<= 8;
503 v |= addr[2]; v <<= 8;
504 v |= addr[3]; v <<= 8;
505 v |= addr[4]; v <<= 8;
506 v |= addr[5]; v <<= 8;
507 v |= addr[6]; v <<= 8;
508 v |= addr[7];
510 return v;
511 #else
512 BFD_FAIL();
513 return 0;
514 #endif
517 bfd_uint64_t
518 bfd_getl64 (const void *p ATTRIBUTE_UNUSED)
520 #ifdef BFD_HOST_64_BIT
521 const bfd_byte *addr = p;
522 bfd_uint64_t v;
524 v = addr[7]; v <<= 8;
525 v |= addr[6]; v <<= 8;
526 v |= addr[5]; v <<= 8;
527 v |= addr[4]; v <<= 8;
528 v |= addr[3]; v <<= 8;
529 v |= addr[2]; v <<= 8;
530 v |= addr[1]; v <<= 8;
531 v |= addr[0];
533 return v;
534 #else
535 BFD_FAIL();
536 return 0;
537 #endif
541 bfd_int64_t
542 bfd_getb_signed_64 (const void *p ATTRIBUTE_UNUSED)
544 #ifdef BFD_HOST_64_BIT
545 const bfd_byte *addr = p;
546 bfd_uint64_t v;
548 v = addr[0]; v <<= 8;
549 v |= addr[1]; v <<= 8;
550 v |= addr[2]; v <<= 8;
551 v |= addr[3]; v <<= 8;
552 v |= addr[4]; v <<= 8;
553 v |= addr[5]; v <<= 8;
554 v |= addr[6]; v <<= 8;
555 v |= addr[7];
557 return COERCE64 (v);
558 #else
559 BFD_FAIL();
560 return 0;
561 #endif
564 bfd_int64_t
565 bfd_getl_signed_64 (const void *p ATTRIBUTE_UNUSED)
567 #ifdef BFD_HOST_64_BIT
568 const bfd_byte *addr = p;
569 bfd_uint64_t v;
571 v = addr[7]; v <<= 8;
572 v |= addr[6]; v <<= 8;
573 v |= addr[5]; v <<= 8;
574 v |= addr[4]; v <<= 8;
575 v |= addr[3]; v <<= 8;
576 v |= addr[2]; v <<= 8;
577 v |= addr[1]; v <<= 8;
578 v |= addr[0];
580 return COERCE64 (v);
581 #else
582 BFD_FAIL();
583 return 0;
584 #endif
587 void
588 bfd_putb32 (bfd_vma data, void *p)
590 bfd_byte *addr = p;
591 addr[0] = (data >> 24) & 0xff;
592 addr[1] = (data >> 16) & 0xff;
593 addr[2] = (data >> 8) & 0xff;
594 addr[3] = data & 0xff;
597 void
598 bfd_putl32 (bfd_vma data, void *p)
600 bfd_byte *addr = p;
601 addr[0] = data & 0xff;
602 addr[1] = (data >> 8) & 0xff;
603 addr[2] = (data >> 16) & 0xff;
604 addr[3] = (data >> 24) & 0xff;
607 void
608 bfd_putb64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
610 #ifdef BFD_HOST_64_BIT
611 bfd_byte *addr = p;
612 addr[0] = (data >> (7*8)) & 0xff;
613 addr[1] = (data >> (6*8)) & 0xff;
614 addr[2] = (data >> (5*8)) & 0xff;
615 addr[3] = (data >> (4*8)) & 0xff;
616 addr[4] = (data >> (3*8)) & 0xff;
617 addr[5] = (data >> (2*8)) & 0xff;
618 addr[6] = (data >> (1*8)) & 0xff;
619 addr[7] = (data >> (0*8)) & 0xff;
620 #else
621 BFD_FAIL();
622 #endif
625 void
626 bfd_putl64 (bfd_uint64_t data ATTRIBUTE_UNUSED, void *p ATTRIBUTE_UNUSED)
628 #ifdef BFD_HOST_64_BIT
629 bfd_byte *addr = p;
630 addr[7] = (data >> (7*8)) & 0xff;
631 addr[6] = (data >> (6*8)) & 0xff;
632 addr[5] = (data >> (5*8)) & 0xff;
633 addr[4] = (data >> (4*8)) & 0xff;
634 addr[3] = (data >> (3*8)) & 0xff;
635 addr[2] = (data >> (2*8)) & 0xff;
636 addr[1] = (data >> (1*8)) & 0xff;
637 addr[0] = (data >> (0*8)) & 0xff;
638 #else
639 BFD_FAIL();
640 #endif
643 void
644 bfd_put_bits (bfd_uint64_t data, void *p, int bits, bfd_boolean big_p)
646 bfd_byte *addr = p;
647 int i;
648 int bytes;
650 if (bits % 8 != 0)
651 abort ();
653 bytes = bits / 8;
654 for (i = 0; i < bytes; i++)
656 int index = big_p ? bytes - i - 1 : i;
658 addr[index] = data & 0xff;
659 data >>= 8;
663 bfd_uint64_t
664 bfd_get_bits (const void *p, int bits, bfd_boolean big_p)
666 const bfd_byte *addr = p;
667 bfd_uint64_t data;
668 int i;
669 int bytes;
671 if (bits % 8 != 0)
672 abort ();
674 data = 0;
675 bytes = bits / 8;
676 for (i = 0; i < bytes; i++)
678 int index = big_p ? i : bytes - i - 1;
680 data = (data << 8) | addr[index];
683 return data;
686 /* Default implementation */
688 bfd_boolean
689 _bfd_generic_get_section_contents (bfd *abfd,
690 sec_ptr section,
691 void *location,
692 file_ptr offset,
693 bfd_size_type count)
695 bfd_size_type sz;
696 if (count == 0)
697 return TRUE;
699 sz = section->rawsize ? section->rawsize : section->size;
700 if (offset + count > sz)
702 bfd_set_error (bfd_error_invalid_operation);
703 return FALSE;
706 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
707 || bfd_bread (location, count, abfd) != count)
708 return FALSE;
710 return TRUE;
713 bfd_boolean
714 _bfd_generic_get_section_contents_in_window
715 (bfd *abfd ATTRIBUTE_UNUSED,
716 sec_ptr section ATTRIBUTE_UNUSED,
717 bfd_window *w ATTRIBUTE_UNUSED,
718 file_ptr offset ATTRIBUTE_UNUSED,
719 bfd_size_type count ATTRIBUTE_UNUSED)
721 #ifdef USE_MMAP
722 bfd_size_type sz;
724 if (count == 0)
725 return TRUE;
726 if (abfd->xvec->_bfd_get_section_contents
727 != _bfd_generic_get_section_contents)
729 /* We don't know what changes the bfd's get_section_contents
730 method may have to make. So punt trying to map the file
731 window, and let get_section_contents do its thing. */
732 /* @@ FIXME : If the internal window has a refcount of 1 and was
733 allocated with malloc instead of mmap, just reuse it. */
734 bfd_free_window (w);
735 w->i = bfd_zmalloc (sizeof (bfd_window_internal));
736 if (w->i == NULL)
737 return FALSE;
738 w->i->data = bfd_malloc (count);
739 if (w->i->data == NULL)
741 free (w->i);
742 w->i = NULL;
743 return FALSE;
745 w->i->mapped = 0;
746 w->i->refcount = 1;
747 w->size = w->i->size = count;
748 w->data = w->i->data;
749 return bfd_get_section_contents (abfd, section, w->data, offset, count);
751 sz = section->rawsize ? section->rawsize : section->size;
752 if (offset + count > sz
753 || ! bfd_get_file_window (abfd, section->filepos + offset, count, w,
754 TRUE))
755 return FALSE;
756 return TRUE;
757 #else
758 abort ();
759 #endif
762 /* This generic function can only be used in implementations where creating
763 NEW sections is disallowed. It is useful in patching existing sections
764 in read-write files, though. See other set_section_contents functions
765 to see why it doesn't work for new sections. */
766 bfd_boolean
767 _bfd_generic_set_section_contents (bfd *abfd,
768 sec_ptr section,
769 const void *location,
770 file_ptr offset,
771 bfd_size_type count)
773 if (count == 0)
774 return TRUE;
776 if (bfd_seek (abfd, section->filepos + offset, SEEK_SET) != 0
777 || bfd_bwrite (location, count, abfd) != count)
778 return FALSE;
780 return TRUE;
784 INTERNAL_FUNCTION
785 bfd_log2
787 SYNOPSIS
788 unsigned int bfd_log2 (bfd_vma x);
790 DESCRIPTION
791 Return the log base 2 of the value supplied, rounded up. E.g., an
792 @var{x} of 1025 returns 11. A @var{x} of 0 returns 0.
795 unsigned int
796 bfd_log2 (bfd_vma x)
798 unsigned int result = 0;
800 while ((x = (x >> 1)) != 0)
801 ++result;
802 return result;
805 bfd_boolean
806 bfd_generic_is_local_label_name (bfd *abfd, const char *name)
808 char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
810 return name[0] == locals_prefix;
813 /* Can be used from / for bfd_merge_private_bfd_data to check that
814 endianness matches between input and output file. Returns
815 TRUE for a match, otherwise returns FALSE and emits an error. */
816 bfd_boolean
817 _bfd_generic_verify_endian_match (bfd *ibfd, bfd *obfd)
819 if (ibfd->xvec->byteorder != obfd->xvec->byteorder
820 && ibfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN
821 && obfd->xvec->byteorder != BFD_ENDIAN_UNKNOWN)
823 const char *msg;
825 if (bfd_big_endian (ibfd))
826 msg = _("%B: compiled for a big endian system and target is little endian");
827 else
828 msg = _("%B: compiled for a little endian system and target is big endian");
830 (*_bfd_error_handler) (msg, ibfd);
832 bfd_set_error (bfd_error_wrong_format);
833 return FALSE;
836 return TRUE;
839 /* Give a warning at runtime if someone compiles code which calls
840 old routines. */
842 void
843 warn_deprecated (const char *what,
844 const char *file,
845 int line,
846 const char *func)
848 /* Poor man's tracking of functions we've already warned about. */
849 static size_t mask = 0;
851 if (~(size_t) func & ~mask)
853 /* Note: separate sentences in order to allow
854 for translation into other languages. */
855 if (func)
856 fprintf (stderr, _("Deprecated %s called at %s line %d in %s\n"),
857 what, file, line, func);
858 else
859 fprintf (stderr, _("Deprecated %s called\n"), what);
860 mask |= ~(size_t) func;