1 /* libbfd.c -- random BFD support routines, only used internally.
2 Copyright (C) 1990-1991 Free Software Foundation, Inc.
3 Written by Cygnus Support.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
27 /** Dummies for targets that don't want or need to implement
31 DEFUN(_bfd_dummy_new_section_hook
,(ignore
, ignore_newsect
),
33 asection
*ignore_newsect
)
39 DEFUN(bfd_false
,(ignore
),
46 DEFUN(bfd_true
,(ignore
),
53 DEFUN(bfd_nullvoidptr
,(ignore
),
67 DEFUN(bfd_0u
,(ignore
),
74 DEFUN(bfd_void
,(ignore
),
80 DEFUN(_bfd_dummy_core_file_matches_executable_p
,(ignore_core_bfd
, ignore_exec_bfd
),
81 bfd
*ignore_core_bfd AND
84 bfd_error
= invalid_operation
;
88 /* of course you can't initialize a function to be the same as another, grr */
91 DEFUN(_bfd_dummy_core_file_failing_command
,(ignore_abfd
),
98 DEFUN(_bfd_dummy_core_file_failing_signal
,(ignore_abfd
),
105 DEFUN(_bfd_dummy_target
,(ignore_abfd
),
111 /** zalloc -- allocate and clear storage */
119 char *ptr
= (char *) malloc ((int)size
);
121 if ((ptr
!= NULL
) && (size
!= 0))
131 /* Note that archive entries don't have streams; they share their parent's.
132 This allows someone to play with the iostream behind BFD's back.
134 Also, note that the origin pointer points to the beginning of a file's
135 contents (0 for non-archive elements). For archive entries this is the
136 first octet in the file, NOT the beginning of the archive header. */
139 int DEFUN(real_read
,(where
, a
,b
, file
),
145 return fread(where
, a
,b
,file
);
148 DEFUN(bfd_read
,(ptr
, size
, nitems
, abfd
),
150 bfd_size_type size AND
151 bfd_size_type nitems AND
154 return (bfd_size_type
)real_read (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
158 DEFUN(bfd_write
,(ptr
, size
, nitems
, abfd
),
160 bfd_size_type size AND
161 bfd_size_type nitems AND
164 return fwrite (ptr
, 1, (int)(size
*nitems
), bfd_cache_lookup(abfd
));
168 DEFUN(bfd_write_bigendian_4byte_int
,(abfd
, i
),
173 _do_putb32(i
, buffer
);
174 bfd_write(buffer
, 4, 1, abfd
);
178 DEFUN(bfd_seek
,(abfd
, position
, direction
),
180 CONST file_ptr position AND
183 /* For the time being, a BFD may not seek to it's end. The
184 problem is that we don't easily have a way to recognize
185 the end of an element in an archive. */
187 BFD_ASSERT(direction
== SEEK_SET
188 || direction
== SEEK_CUR
);
190 if (direction
== SEEK_SET
&& abfd
->my_archive
!= NULL
)
192 /* This is a set within an archive, so we need to
193 add the base of the object within the archive */
194 return(fseek(bfd_cache_lookup(abfd
),
195 position
+ abfd
->origin
,
200 return(fseek(bfd_cache_lookup(abfd
), position
, direction
));
205 DEFUN(bfd_tell
,(abfd
),
210 ptr
= ftell (bfd_cache_lookup(abfd
));
212 if (abfd
->my_archive
)
217 /** Make a string table */
220 Add string to table pointed to by table, at location starting with free_ptr.
221 resizes the table if necessary (if it's NULL, creates it, ignoring
222 table_length). Updates free_ptr, table, table_length */
225 DEFUN(bfd_add_to_string_table
,(table
, new_string
, table_length
, free_ptr
),
228 unsigned int *table_length AND
231 size_t string_length
= strlen (new_string
) + 1; /* include null here */
233 size_t space_length
= *table_length
;
234 unsigned int offset
= (base
? *free_ptr
- base
: 0);
237 /* Avoid a useless regrow if we can (but of course we still
239 space_length
= (string_length
< DEFAULT_STRING_SPACE_SIZE
?
240 DEFAULT_STRING_SPACE_SIZE
: string_length
+1);
241 base
= zalloc (space_length
);
244 bfd_error
= no_memory
;
249 if ((size_t)(offset
+ string_length
) >= space_length
) {
250 /* Make sure we will have enough space */
251 while ((size_t)(offset
+ string_length
) >= space_length
)
252 space_length
+= space_length
/2; /* grow by 50% */
254 base
= (char *) realloc (base
, space_length
);
256 bfd_error
= no_memory
;
262 memcpy (base
+ offset
, new_string
, string_length
);
264 *table_length
= space_length
;
265 *free_ptr
= base
+ offset
+ string_length
;
270 /** The do-it-yourself (byte) sex-change kit */
272 /* The middle letter e.g. get<b>short indicates Big or Little endian
273 target machine. It doesn't matter what the byte order of the host
274 machine is; these routines work for either. */
276 /* FIXME: Should these take a count argument?
277 Answer (gnu@cygnus.com): No, but perhaps they should be inline
278 functions in swap.h #ifdef __GNUC__.
279 Gprof them later and find out. */
284 These macros as used for reading and writing raw data in sections;
285 each access (except for bytes) is vectored through the target format
286 of the BFD and mangled accordingly. The mangling performs any
287 necessary endian translations and removes alignment restrictions.
289 #define bfd_put_8(abfd, val, ptr) \
290 (*((char *)ptr) = (char)val)
291 #define bfd_get_8(abfd, ptr) \
293 #define bfd_put_16(abfd, val, ptr) \
294 BFD_SEND(abfd, bfd_putx16, (val,ptr))
295 #define bfd_get_16(abfd, ptr) \
296 BFD_SEND(abfd, bfd_getx16, (ptr))
297 #define bfd_put_32(abfd, val, ptr) \
298 BFD_SEND(abfd, bfd_putx32, (val,ptr))
299 #define bfd_get_32(abfd, ptr) \
300 BFD_SEND(abfd, bfd_getx32, (ptr))
301 #define bfd_put_64(abfd, val, ptr) \
302 BFD_SEND(abfd, bfd_putx64, (val, ptr))
303 #define bfd_get_64(abfd, ptr) \
304 BFD_SEND(abfd, bfd_getx64, (ptr))
311 These macros have the same function as their @code{bfd_get_x}
312 bretherin, except that they are used for removing information for the
313 header records of object files. Believe it or not, some object files
314 keep their header records in big endian order, and their data in little
317 #define bfd_h_put_8(abfd, val, ptr) \
318 (*((char *)ptr) = (char)val)
319 #define bfd_h_get_8(abfd, ptr) \
321 #define bfd_h_put_16(abfd, val, ptr) \
322 BFD_SEND(abfd, bfd_h_putx16,(val,ptr))
323 #define bfd_h_get_16(abfd, ptr) \
324 BFD_SEND(abfd, bfd_h_getx16,(ptr))
325 #define bfd_h_put_32(abfd, val, ptr) \
326 BFD_SEND(abfd, bfd_h_putx32,(val,ptr))
327 #define bfd_h_get_32(abfd, ptr) \
328 BFD_SEND(abfd, bfd_h_getx32,(ptr))
329 #define bfd_h_put_64(abfd, val, ptr) \
330 BFD_SEND(abfd, bfd_h_putx64,(val, ptr))
331 #define bfd_h_get_64(abfd, ptr) \
332 BFD_SEND(abfd, bfd_h_getx64,(ptr))
337 DEFUN(_do_getb16
,(addr
),
338 register bfd_byte
*addr
)
340 return (addr
[0] << 8) | addr
[1];
344 DEFUN(_do_getl16
,(addr
),
345 register bfd_byte
*addr
)
347 return (addr
[1] << 8) | addr
[0];
351 DEFUN(_do_putb16
,(data
, addr
),
353 register bfd_byte
*addr
)
355 addr
[0] = (bfd_byte
)(data
>> 8);
356 addr
[1] = (bfd_byte
)data
;
360 DEFUN(_do_putl16
,(data
, addr
),
362 register bfd_byte
*addr
)
364 addr
[0] = (bfd_byte
)data
;
365 addr
[1] = (bfd_byte
)(data
>> 8);
369 DEFUN(_do_getb32
,(addr
),
370 register bfd_byte
*addr
)
372 return ((((addr
[0] << 8) | addr
[1]) << 8) | addr
[2]) << 8 | addr
[3];
377 register bfd_byte
*addr
;
379 return ((((addr
[3] << 8) | addr
[2]) << 8) | addr
[1]) << 8 | addr
[0];
383 DEFUN(_do_getb64
,(addr
),
384 register bfd_byte
*addr
)
387 bfd_64_type low
, high
;
389 high
= ((((((((addr
[0]) << 8) |
394 low
= ((((((((addr
[4]) << 8) |
399 return high
<< 32 | low
;
408 DEFUN(_do_getl64
,(addr
),
409 register bfd_byte
*addr
)
413 bfd_64_type low
, high
;
414 high
= (((((((addr
[7] << 8) |
419 low
= (((((((addr
[3] << 8) |
424 return high
<< 32 | low
;
433 DEFUN(_do_putb32
,(data
, addr
),
435 register bfd_byte
*addr
)
437 addr
[0] = (bfd_byte
)(data
>> 24);
438 addr
[1] = (bfd_byte
)(data
>> 16);
439 addr
[2] = (bfd_byte
)(data
>> 8);
440 addr
[3] = (bfd_byte
)data
;
444 DEFUN(_do_putl32
,(data
, addr
),
446 register bfd_byte
*addr
)
448 addr
[0] = (bfd_byte
)data
;
449 addr
[1] = (bfd_byte
)(data
>> 8);
450 addr
[2] = (bfd_byte
)(data
>> 16);
451 addr
[3] = (bfd_byte
)(data
>> 24);
454 DEFUN(_do_putb64
,(data
, addr
),
456 register bfd_byte
*addr
)
459 addr
[0] = (bfd_byte
)(data
>> (7*8));
460 addr
[1] = (bfd_byte
)(data
>> (6*8));
461 addr
[2] = (bfd_byte
)(data
>> (5*8));
462 addr
[3] = (bfd_byte
)(data
>> (4*8));
463 addr
[4] = (bfd_byte
)(data
>> (3*8));
464 addr
[5] = (bfd_byte
)(data
>> (2*8));
465 addr
[6] = (bfd_byte
)(data
>> (1*8));
466 addr
[7] = (bfd_byte
)(data
>> (0*8));
474 DEFUN(_do_putl64
,(data
, addr
),
476 register bfd_byte
*addr
)
479 addr
[7] = (bfd_byte
)(data
>> (7*8));
480 addr
[6] = (bfd_byte
)(data
>> (6*8));
481 addr
[5] = (bfd_byte
)(data
>> (5*8));
482 addr
[4] = (bfd_byte
)(data
>> (4*8));
483 addr
[3] = (bfd_byte
)(data
>> (3*8));
484 addr
[2] = (bfd_byte
)(data
>> (2*8));
485 addr
[1] = (bfd_byte
)(data
>> (1*8));
486 addr
[0] = (bfd_byte
)(data
>> (0*8));
494 /* Default implementation */
497 DEFUN(bfd_generic_get_section_contents
, (abfd
, section
, location
, offset
, count
),
506 if ((bfd_size_type
)(offset
+count
) > section
->size
507 || bfd_seek(abfd
,(file_ptr
)( section
->filepos
+ offset
), SEEK_SET
) == -1
508 || bfd_read(location
, (bfd_size_type
)1, count
, abfd
) != count
)
509 return (false); /* on error */
513 /* This generic function can only be used in implementations where creating
514 NEW sections is disallowed. It is useful in patching existing sections
515 in read-write files, though. See other set_section_contents functions
516 to see why it doesn't work for new sections. */
518 DEFUN(bfd_generic_set_section_contents
, (abfd
, section
, location
, offset
, count
),
527 if ((bfd_size_type
)(offset
+count
) > section
->size
528 || bfd_seek(abfd
, (file_ptr
)(section
->filepos
+ offset
), SEEK_SET
) == -1
529 || bfd_write(location
, (bfd_size_type
)1, count
, abfd
) != count
)
530 return (false); /* on error */
536 Return the log base 2 of the value supplied, rounded up. eg an arg
537 of 1025 would return 11.
538 *; PROTO(bfd_vma, bfd_log2,(bfd_vma x));
545 while ( (bfd_vma
)(1<< result
) < x
)