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