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