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