Arg. Forgot this in yesterday's cvs commit.
[external/binutils.git] / bfd / libbfd.c
1 /* Assorted BFD support routines, only used internally.
2    Copyright 1990, 91, 92, 93, 94 Free Software Foundation, Inc.
3    Written by Cygnus Support.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24
25 /*
26 SECTION
27         Internal functions
28
29 DESCRIPTION
30         These routines are used within BFD.
31         They are not intended for export, but are documented here for
32         completeness.
33 */
34
35 /*ARGSUSED*/
36 boolean
37 _bfd_dummy_new_section_hook (ignore, ignore_newsect)
38      bfd *ignore;
39      asection *ignore_newsect;
40 {
41   return true;
42 }
43
44 /*ARGSUSED*/
45 boolean
46 bfd_false (ignore)
47      bfd *ignore;
48 {
49   return false;
50 }
51
52 /*ARGSUSED*/
53 boolean
54 bfd_true (ignore)
55      bfd *ignore;
56 {
57   return true;
58 }
59
60 /*ARGSUSED*/
61 PTR
62 bfd_nullvoidptr (ignore)
63      bfd *ignore;
64 {
65   return (PTR)NULL;
66 }
67
68 /*ARGSUSED*/
69 int 
70 bfd_0 (ignore)
71      bfd *ignore;
72 {
73   return 0;
74 }
75
76 /*ARGSUSED*/
77 unsigned int 
78 bfd_0u (ignore)
79      bfd *ignore;
80 {
81    return 0;
82 }
83
84 /*ARGSUSED*/
85 void 
86 bfd_void (ignore)
87      bfd *ignore;
88 {
89 }
90
91 /*ARGSUSED*/
92 boolean
93 _bfd_dummy_core_file_matches_executable_p (ignore_core_bfd, ignore_exec_bfd)
94      bfd *ignore_core_bfd;
95      bfd *ignore_exec_bfd;
96 {
97   bfd_set_error (bfd_error_invalid_operation);
98   return false;
99 }
100
101 /* of course you can't initialize a function to be the same as another, grr */
102
103 /*ARGSUSED*/
104 char *
105 _bfd_dummy_core_file_failing_command (ignore_abfd)
106      bfd *ignore_abfd;
107 {
108   return (char *)NULL;
109 }
110
111 /*ARGSUSED*/
112 int
113 _bfd_dummy_core_file_failing_signal (ignore_abfd)
114      bfd *ignore_abfd;
115 {
116   return 0;
117 }
118
119 /*ARGSUSED*/
120 bfd_target *
121 _bfd_dummy_target (ignore_abfd)
122      bfd *ignore_abfd;
123 {
124   return 0;
125 }
126 \f
127
128 #ifndef bfd_zmalloc
129 /* allocate and clear storage */
130
131 char *
132 bfd_zmalloc (size)
133      bfd_size_type size;
134 {
135   char *ptr = (char *) malloc ((size_t) size);
136
137   if (ptr && size)
138    memset(ptr, 0, (size_t) size);
139
140   return ptr;
141 }
142 #endif /* bfd_zmalloc */
143 \f
144 /* Some IO code */
145
146
147 /* Note that archive entries don't have streams; they share their parent's.
148    This allows someone to play with the iostream behind BFD's back.
149
150    Also, note that the origin pointer points to the beginning of a file's
151    contents (0 for non-archive elements).  For archive entries this is the
152    first octet in the file, NOT the beginning of the archive header. */
153
154 static 
155 int
156 real_read (where, a,b, file)
157      PTR where;
158      int a;
159      int b;
160      FILE *file;
161 {
162   return fread(where, a,b,file);
163 }
164
165 /* Return value is amount read (FIXME: how are errors and end of file dealt
166    with?  We never call bfd_set_error, which is probably a mistake).  */
167
168 bfd_size_type
169 bfd_read (ptr, size, nitems, abfd)
170      PTR ptr;
171      bfd_size_type size;
172      bfd_size_type nitems;
173      bfd *abfd;
174 {
175   int nread;
176   nread = real_read (ptr, 1, (int)(size*nitems), bfd_cache_lookup(abfd));
177 #ifdef FILE_OFFSET_IS_CHAR_INDEX
178   if (nread > 0)
179     abfd->where += nread;
180 #endif
181
182   /* Set bfd_error if we did not read as much data as we expected.
183
184      If the read failed due to an error set the bfd_error_system_call,
185      else set bfd_error_file_truncated.
186
187      A BFD backend may wish to override bfd_error_file_truncated to
188      provide something more useful (eg. no_symbols or wrong_format).  */
189   if (nread < (int)(size * nitems))
190     {
191       if (ferror (bfd_cache_lookup (abfd)))
192         bfd_set_error (bfd_error_system_call);
193       else
194         bfd_set_error (bfd_error_file_truncated);
195     }
196
197   return nread;
198 }
199
200 bfd_size_type
201 bfd_write (ptr, size, nitems, abfd)
202      CONST PTR ptr;
203      bfd_size_type size;
204      bfd_size_type nitems;
205      bfd *abfd;
206 {
207   int nwrote = fwrite (ptr, 1, (int) (size * nitems), bfd_cache_lookup (abfd));
208 #ifdef FILE_OFFSET_IS_CHAR_INDEX
209   if (nwrote > 0)
210     abfd->where += nwrote;
211 #endif
212   if (nwrote != size * nitems)
213     {
214 #ifdef ENOSPC
215       if (nwrote >= 0)
216         errno = ENOSPC;
217 #endif
218       bfd_set_error (bfd_error_system_call);
219     }
220   return nwrote;
221 }
222
223 /*
224 INTERNAL_FUNCTION
225         bfd_write_bigendian_4byte_int
226
227 SYNOPSIS
228         void bfd_write_bigendian_4byte_int(bfd *abfd,  int i);
229
230 DESCRIPTION
231         Write a 4 byte integer @var{i} to the output BFD @var{abfd}, in big
232         endian order regardless of what else is going on.  This is useful in
233         archives.
234
235 */
236 void
237 bfd_write_bigendian_4byte_int (abfd, i)
238      bfd *abfd;
239      int i;
240 {
241   bfd_byte buffer[4];
242   bfd_putb32(i, buffer);
243   bfd_write((PTR)buffer, 4, 1, abfd);
244 }
245
246 long
247 bfd_tell (abfd)
248      bfd *abfd;
249 {
250   file_ptr ptr;
251
252   ptr = ftell (bfd_cache_lookup(abfd));
253
254   if (abfd->my_archive)
255     ptr -= abfd->origin;
256   abfd->where = ptr;
257   return ptr;
258 }
259
260 int
261 bfd_flush (abfd)
262      bfd *abfd;
263 {
264   return fflush (bfd_cache_lookup(abfd));
265 }
266
267 int
268 bfd_stat (abfd, statbuf)
269      bfd *abfd;
270      struct stat *statbuf;
271 {
272   return fstat (fileno(bfd_cache_lookup(abfd)), statbuf);
273 }
274
275 /* Returns 0 for success, nonzero for failure (in which case bfd_get_error
276    can retrieve the error code).  */
277
278 int
279 bfd_seek (abfd, position, direction)
280      bfd * CONST abfd;
281      CONST file_ptr position;
282      CONST int direction;
283 {
284   int result;
285   FILE *f;
286   file_ptr file_position;
287   /* For the time being, a BFD may not seek to it's end.  The problem
288      is that we don't easily have a way to recognize the end of an
289      element in an archive. */
290
291   BFD_ASSERT (direction == SEEK_SET || direction == SEEK_CUR);
292
293   if (direction == SEEK_CUR && position == 0)
294     return 0;
295 #ifdef FILE_OFFSET_IS_CHAR_INDEX
296   if (abfd->format != bfd_archive && abfd->my_archive == 0)
297     {
298 #if 0
299       /* Explanation for this code: I'm only about 95+% sure that the above
300          conditions are sufficient and that all i/o calls are properly
301          adjusting the `where' field.  So this is sort of an `assert'
302          that the `where' field is correct.  If we can go a while without
303          tripping the abort, we can probably safely disable this code,
304          so that the real optimizations happen.  */
305       file_ptr where_am_i_now;
306       where_am_i_now = ftell (bfd_cache_lookup (abfd));
307       if (abfd->my_archive)
308         where_am_i_now -= abfd->origin;
309       if (where_am_i_now != abfd->where)
310         abort ();
311 #endif
312       if (direction == SEEK_SET && position == abfd->where)
313         return 0;
314     }
315   else
316     {
317       /* We need something smarter to optimize access to archives.
318          Currently, anything inside an archive is read via the file
319          handle for the archive.  Which means that a bfd_seek on one
320          component affects the `current position' in the archive, as
321          well as in any other component.
322
323          It might be sufficient to put a spike through the cache
324          abstraction, and look to the archive for the file position,
325          but I think we should try for something cleaner.
326
327          In the meantime, no optimization for archives.  */
328     }
329 #endif
330
331   f = bfd_cache_lookup (abfd);
332   file_position = position;
333   if (direction == SEEK_SET && abfd->my_archive != NULL)
334     file_position += abfd->origin;
335
336   result = fseek (f, file_position, direction);
337
338   if (result != 0)
339     {
340       /* Force redetermination of `where' field.  */
341       bfd_tell (abfd);
342       bfd_set_error (bfd_error_system_call);
343     }
344   else
345     {
346 #ifdef FILE_OFFSET_IS_CHAR_INDEX
347       /* Adjust `where' field.  */
348       if (direction == SEEK_SET)
349         abfd->where = position;
350       else
351         abfd->where += position;
352 #endif
353     }
354   return result;
355 }
356 \f
357 /** Make a string table */
358
359 /*>bfd.h<
360  Add string to table pointed to by table, at location starting with free_ptr.
361    resizes the table if necessary (if it's NULL, creates it, ignoring
362    table_length).  Updates free_ptr, table, table_length */
363
364 boolean
365 bfd_add_to_string_table (table, new_string, table_length, free_ptr)
366      char **table;
367      char *new_string;
368      unsigned int *table_length;
369      char **free_ptr;
370 {
371   size_t string_length = strlen (new_string) + 1; /* include null here */
372   char *base = *table;
373   size_t space_length = *table_length;
374   unsigned int offset = (base ? *free_ptr - base : 0);
375
376   if (base == NULL) {
377     /* Avoid a useless regrow if we can (but of course we still
378        take it next time).  */
379     space_length = (string_length < DEFAULT_STRING_SPACE_SIZE ?
380                     DEFAULT_STRING_SPACE_SIZE : string_length+1);
381     base = bfd_zmalloc ((bfd_size_type) space_length);
382
383     if (base == NULL) {
384       bfd_set_error (bfd_error_no_memory);
385       return false;
386     }
387   }
388
389   if ((size_t)(offset + string_length) >= space_length) {
390     /* Make sure we will have enough space */
391     while ((size_t)(offset + string_length) >= space_length) 
392       space_length += space_length/2; /* grow by 50% */
393
394     base = (char *) realloc (base, space_length);
395     if (base == NULL) {
396       bfd_set_error (bfd_error_no_memory);
397       return false;
398     }
399
400   }
401
402   memcpy (base + offset, new_string, string_length);
403   *table = base;
404   *table_length = space_length;
405   *free_ptr = base + offset + string_length;
406   
407   return true;
408 }
409 \f
410 /** The do-it-yourself (byte) sex-change kit */
411
412 /* The middle letter e.g. get<b>short indicates Big or Little endian
413    target machine.  It doesn't matter what the byte order of the host
414    machine is; these routines work for either.  */
415
416 /* FIXME: Should these take a count argument?
417    Answer (gnu@cygnus.com):  No, but perhaps they should be inline
418                              functions in swap.h #ifdef __GNUC__. 
419                              Gprof them later and find out.  */
420
421 /*
422 FUNCTION
423         bfd_put_size
424 FUNCTION
425         bfd_get_size
426
427 DESCRIPTION
428         These macros as used for reading and writing raw data in
429         sections; each access (except for bytes) is vectored through
430         the target format of the BFD and mangled accordingly. The
431         mangling performs any necessary endian translations and
432         removes alignment restrictions.  Note that types accepted and
433         returned by these macros are identical so they can be swapped
434         around in macros---for example, @file{libaout.h} defines <<GET_WORD>>
435         to either <<bfd_get_32>> or <<bfd_get_64>>.
436
437         In the put routines, @var{val} must be a <<bfd_vma>>.  If we are on a
438         system without prototypes, the caller is responsible for making
439         sure that is true, with a cast if necessary.  We don't cast
440         them in the macro definitions because that would prevent <<lint>>
441         or <<gcc -Wall>> from detecting sins such as passing a pointer.
442         To detect calling these with less than a <<bfd_vma>>, use
443         <<gcc -Wconversion>> on a host with 64 bit <<bfd_vma>>'s.
444
445 .
446 .{* Byte swapping macros for user section data.  *}
447 .
448 .#define bfd_put_8(abfd, val, ptr) \
449 .                (*((unsigned char *)(ptr)) = (unsigned char)(val))
450 .#define bfd_put_signed_8 \
451 .               bfd_put_8
452 .#define bfd_get_8(abfd, ptr) \
453 .                (*(unsigned char *)(ptr))
454 .#define bfd_get_signed_8(abfd, ptr) \
455 .               ((*(unsigned char *)(ptr) ^ 0x80) - 0x80)
456 .
457 .#define bfd_put_16(abfd, val, ptr) \
458 .                BFD_SEND(abfd, bfd_putx16, ((val),(ptr)))
459 .#define bfd_put_signed_16 \
460 .                bfd_put_16
461 .#define bfd_get_16(abfd, ptr) \
462 .                BFD_SEND(abfd, bfd_getx16, (ptr))
463 .#define bfd_get_signed_16(abfd, ptr) \
464 .                BFD_SEND (abfd, bfd_getx_signed_16, (ptr))
465 .
466 .#define bfd_put_32(abfd, val, ptr) \
467 .                BFD_SEND(abfd, bfd_putx32, ((val),(ptr)))
468 .#define bfd_put_signed_32 \
469 .                bfd_put_32
470 .#define bfd_get_32(abfd, ptr) \
471 .                BFD_SEND(abfd, bfd_getx32, (ptr))
472 .#define bfd_get_signed_32(abfd, ptr) \
473 .                BFD_SEND(abfd, bfd_getx_signed_32, (ptr))
474 .
475 .#define bfd_put_64(abfd, val, ptr) \
476 .                BFD_SEND(abfd, bfd_putx64, ((val), (ptr)))
477 .#define bfd_put_signed_64 \
478 .                bfd_put_64
479 .#define bfd_get_64(abfd, ptr) \
480 .                BFD_SEND(abfd, bfd_getx64, (ptr))
481 .#define bfd_get_signed_64(abfd, ptr) \
482 .                BFD_SEND(abfd, bfd_getx_signed_64, (ptr))
483 .
484 */ 
485
486 /*
487 FUNCTION
488         bfd_h_put_size
489         bfd_h_get_size
490
491 DESCRIPTION
492         These macros have the same function as their <<bfd_get_x>>
493         bretheren, except that they are used for removing information
494         for the header records of object files. Believe it or not,
495         some object files keep their header records in big endian
496         order and their data in little endian order.
497 .
498 .{* Byte swapping macros for file header data.  *}
499 .
500 .#define bfd_h_put_8(abfd, val, ptr) \
501 .               bfd_put_8 (abfd, val, ptr)
502 .#define bfd_h_put_signed_8(abfd, val, ptr) \
503 .               bfd_put_8 (abfd, val, ptr)
504 .#define bfd_h_get_8(abfd, ptr) \
505 .               bfd_get_8 (abfd, ptr)
506 .#define bfd_h_get_signed_8(abfd, ptr) \
507 .               bfd_get_signed_8 (abfd, ptr)
508 .
509 .#define bfd_h_put_16(abfd, val, ptr) \
510 .                BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
511 .#define bfd_h_put_signed_16 \
512 .                bfd_h_put_16
513 .#define bfd_h_get_16(abfd, ptr) \
514 .                BFD_SEND(abfd, bfd_h_getx16,(ptr))
515 .#define bfd_h_get_signed_16(abfd, ptr) \
516 .                BFD_SEND(abfd, bfd_h_getx_signed_16, (ptr))
517 .
518 .#define bfd_h_put_32(abfd, val, ptr) \
519 .                BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
520 .#define bfd_h_put_signed_32 \
521 .                bfd_h_put_32
522 .#define bfd_h_get_32(abfd, ptr) \
523 .                BFD_SEND(abfd, bfd_h_getx32,(ptr))
524 .#define bfd_h_get_signed_32(abfd, ptr) \
525 .                BFD_SEND(abfd, bfd_h_getx_signed_32, (ptr))
526 .
527 .#define bfd_h_put_64(abfd, val, ptr) \
528 .                BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
529 .#define bfd_h_put_signed_64 \
530 .                bfd_h_put_64
531 .#define bfd_h_get_64(abfd, ptr) \
532 .                BFD_SEND(abfd, bfd_h_getx64,(ptr))
533 .#define bfd_h_get_signed_64(abfd, ptr) \
534 .                BFD_SEND(abfd, bfd_h_getx_signed_64, (ptr))
535 .
536 */ 
537
538 /* Sign extension to bfd_signed_vma.  */
539 #define COERCE16(x) (((bfd_signed_vma) (x) ^ 0x8000) - 0x8000)
540 #define COERCE32(x) (((bfd_signed_vma) (x) ^ 0x80000000) - 0x80000000)
541 #define EIGHT_GAZILLION (((BFD_HOST_64_BIT)0x80000000) << 32)
542 #define COERCE64(x) \
543   (((bfd_signed_vma) (x) ^ EIGHT_GAZILLION) - EIGHT_GAZILLION)
544
545 bfd_vma
546 bfd_getb16 (addr)
547      register const bfd_byte *addr;
548 {
549   return (addr[0] << 8) | addr[1];
550 }
551
552 bfd_vma
553 bfd_getl16 (addr)
554      register const bfd_byte *addr;
555 {
556   return (addr[1] << 8) | addr[0];
557 }
558
559 bfd_signed_vma
560 bfd_getb_signed_16 (addr)
561      register const bfd_byte *addr;
562 {
563   return COERCE16((addr[0] << 8) | addr[1]);
564 }
565
566 bfd_signed_vma
567 bfd_getl_signed_16 (addr)
568      register const bfd_byte *addr;
569 {
570   return COERCE16((addr[1] << 8) | addr[0]);
571 }
572
573 void
574 bfd_putb16 (data, addr)
575      bfd_vma data;
576      register bfd_byte *addr;
577 {
578   addr[0] = (bfd_byte)(data >> 8);
579   addr[1] = (bfd_byte )data;
580 }
581
582 void
583 bfd_putl16 (data, addr)
584      bfd_vma data;             
585      register bfd_byte *addr;
586 {
587   addr[0] = (bfd_byte )data;
588   addr[1] = (bfd_byte)(data >> 8);
589 }
590
591 bfd_vma
592 bfd_getb32 (addr)
593      register const bfd_byte *addr;
594 {
595   return (((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
596           | addr[2]) << 8 | addr[3];
597 }
598
599 bfd_vma
600 bfd_getl32 (addr)
601      register const bfd_byte *addr;
602 {
603   return (((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
604           | addr[1]) << 8 | addr[0];
605 }
606
607 bfd_signed_vma
608 bfd_getb_signed_32 (addr)
609      register const bfd_byte *addr;
610 {
611   return COERCE32((((((bfd_vma)addr[0] << 8) | addr[1]) << 8)
612                    | addr[2]) << 8 | addr[3]);
613 }
614
615 bfd_signed_vma
616 bfd_getl_signed_32 (addr)
617      register const bfd_byte *addr;
618 {
619   return COERCE32((((((bfd_vma)addr[3] << 8) | addr[2]) << 8)
620                    | addr[1]) << 8 | addr[0]);
621 }
622
623 bfd_vma
624 bfd_getb64 (addr)
625      register const bfd_byte *addr;
626 {
627 #ifdef BFD64
628   bfd_vma low, high;
629
630   high= ((((((((addr[0]) << 8) |
631               addr[1]) << 8) |
632             addr[2]) << 8) |
633           addr[3]) );
634
635   low = (((((((((bfd_vma)addr[4]) << 8) |
636               addr[5]) << 8) |
637             addr[6]) << 8) |
638           addr[7]));
639
640   return high << 32 | low;
641 #else
642   BFD_FAIL();
643   return 0;
644 #endif
645 }
646
647 bfd_vma
648 bfd_getl64 (addr)
649      register const bfd_byte *addr;
650 {
651 #ifdef BFD64
652   bfd_vma low, high;
653   high= (((((((addr[7] << 8) |
654               addr[6]) << 8) |
655             addr[5]) << 8) |
656           addr[4]));
657
658   low = ((((((((bfd_vma)addr[3] << 8) |
659               addr[2]) << 8) |
660             addr[1]) << 8) |
661           addr[0]) );
662
663   return high << 32 | low;
664 #else
665   BFD_FAIL();
666   return 0;
667 #endif
668
669 }
670
671 bfd_signed_vma
672 bfd_getb_signed_64 (addr)
673      register const bfd_byte *addr;
674 {
675 #ifdef BFD64
676   bfd_vma low, high;
677
678   high= ((((((((addr[0]) << 8) |
679               addr[1]) << 8) |
680             addr[2]) << 8) |
681           addr[3]) );
682
683   low = (((((((((bfd_vma)addr[4]) << 8) |
684               addr[5]) << 8) |
685             addr[6]) << 8) |
686           addr[7]));
687
688   return COERCE64(high << 32 | low);
689 #else
690   BFD_FAIL();
691   return 0;
692 #endif
693 }
694
695 bfd_signed_vma
696 bfd_getl_signed_64 (addr)
697      register const bfd_byte *addr;
698 {
699 #ifdef BFD64
700   bfd_vma low, high;
701   high= (((((((addr[7] << 8) |
702               addr[6]) << 8) |
703             addr[5]) << 8) |
704           addr[4]));
705
706   low = ((((((((bfd_vma)addr[3] << 8) |
707               addr[2]) << 8) |
708             addr[1]) << 8) |
709           addr[0]) );
710
711   return COERCE64(high << 32 | low);
712 #else
713   BFD_FAIL();
714   return 0;
715 #endif
716 }
717
718 void
719 bfd_putb32 (data, addr)
720      bfd_vma data;
721      register bfd_byte *addr;
722 {
723         addr[0] = (bfd_byte)(data >> 24);
724         addr[1] = (bfd_byte)(data >> 16);
725         addr[2] = (bfd_byte)(data >>  8);
726         addr[3] = (bfd_byte)data;
727 }
728
729 void
730 bfd_putl32 (data, addr)
731      bfd_vma data;
732      register bfd_byte *addr;
733 {
734         addr[0] = (bfd_byte)data;
735         addr[1] = (bfd_byte)(data >>  8);
736         addr[2] = (bfd_byte)(data >> 16);
737         addr[3] = (bfd_byte)(data >> 24);
738 }
739
740 void
741 bfd_putb64 (data, addr)
742      bfd_vma data;
743      register bfd_byte *addr;
744 {
745 #ifdef BFD64
746   addr[0] = (bfd_byte)(data >> (7*8));
747   addr[1] = (bfd_byte)(data >> (6*8));
748   addr[2] = (bfd_byte)(data >> (5*8));
749   addr[3] = (bfd_byte)(data >> (4*8));
750   addr[4] = (bfd_byte)(data >> (3*8));
751   addr[5] = (bfd_byte)(data >> (2*8));
752   addr[6] = (bfd_byte)(data >> (1*8));
753   addr[7] = (bfd_byte)(data >> (0*8));
754 #else
755   BFD_FAIL();
756 #endif
757 }
758
759 void
760 bfd_putl64 (data, addr)
761      bfd_vma data;
762      register bfd_byte *addr;
763 {
764 #ifdef BFD64
765   addr[7] = (bfd_byte)(data >> (7*8));
766   addr[6] = (bfd_byte)(data >> (6*8));
767   addr[5] = (bfd_byte)(data >> (5*8));
768   addr[4] = (bfd_byte)(data >> (4*8));
769   addr[3] = (bfd_byte)(data >> (3*8));
770   addr[2] = (bfd_byte)(data >> (2*8));
771   addr[1] = (bfd_byte)(data >> (1*8));
772   addr[0] = (bfd_byte)(data >> (0*8));
773 #else
774   BFD_FAIL();
775 #endif
776 }
777 \f
778 /* Default implementation */
779
780 boolean
781 bfd_generic_get_section_contents (abfd, section, location, offset, count)
782      bfd *abfd;
783      sec_ptr section;
784      PTR location;
785      file_ptr offset;
786      bfd_size_type count;
787 {
788     if (count == 0)
789         return true;
790     if ((bfd_size_type)(offset+count) > section->_raw_size
791         || bfd_seek(abfd, (file_ptr)(section->filepos + offset), SEEK_SET) == -1
792         || bfd_read(location, (bfd_size_type)1, count, abfd) != count)
793         return (false); /* on error */
794     return (true);
795 }
796
797 /* This generic function can only be used in implementations where creating
798    NEW sections is disallowed.  It is useful in patching existing sections
799    in read-write files, though.  See other set_section_contents functions
800    to see why it doesn't work for new sections.  */
801 boolean
802 bfd_generic_set_section_contents (abfd, section, location, offset, count)
803      bfd *abfd;
804      sec_ptr section;
805      PTR location;
806      file_ptr offset;
807      bfd_size_type count;
808 {
809   if (count == 0)
810     return true;
811
812   if (bfd_seek (abfd, (file_ptr) (section->filepos + offset), SEEK_SET) == -1
813       || bfd_write (location, (bfd_size_type) 1, count, abfd) != count)
814     return false;
815
816   return true;
817 }
818
819 /*
820 INTERNAL_FUNCTION
821         bfd_log2
822
823 SYNOPSIS
824         unsigned int bfd_log2(bfd_vma x);
825
826 DESCRIPTION
827         Return the log base 2 of the value supplied, rounded up.  E.g., an
828         @var{x} of 1025 returns 11.
829 */
830
831 unsigned
832 bfd_log2(x)
833      bfd_vma x;
834 {
835   unsigned result = 0;
836   while ( (bfd_vma)(1<< result) < x)
837     result++;
838   return result;
839 }
840
841 boolean
842 bfd_generic_is_local_label (abfd, sym)
843      bfd *abfd;
844      asymbol *sym;
845 {
846   char locals_prefix = (bfd_get_symbol_leading_char (abfd) == '_') ? 'L' : '.';
847
848   return (sym->name[0] == locals_prefix);
849 }
850