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