3 /*** bfd -- binary file diddling routines by Gumby Wallace of Cygnus Support.
4 Every definition in this file should be exported and declared
5 in bfd.c. If you don't want it to be user-visible, put it in
9 /* Copyright (C) 1990, 1991 Free Software Foundation, Inc.
11 This file is part of BFD, the Binary File Diddler.
13 BFD is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 1, or (at your option)
18 BFD is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with BFD; see the file COPYING. If not, write to
25 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
32 short _bfd_host_big_endian
= 0x0100;
33 /* Accessing the above as (*(char*)&_bfd_host_big_endian), will
34 return 1 if the host is big-endian, 0 otherwise.
35 (assuming that a short is two bytes long!!! FIXME)
36 (See HOST_IS_BIG_ENDIAN_P in bfd.h.) */
39 o - Most functions return nonzero on success (check doc for
40 precise semantics); 0 or NULL on error.
41 o - Internal errors are documented by the value of bfd_error.
42 If that is system_call_error then check errno.
43 o - The easiest way to report this to the user is to use bfd_perror.
46 bfd_ec bfd_error
= no_error
;
48 char *bfd_errmsgs
[] = { "No error",
51 "File in wrong format",
56 "No more archived files",
59 "File format not recognized",
60 "File format is ambiguous",
61 "Section has no contents",
62 "Nonrepresentable section on output",
63 "#<Invalid error code>"
68 DEFUN(bfd_nonrepresentable_section
,(abfd
, name
),
69 CONST bfd
* CONST abfd AND
70 CONST
char * CONST name
)
72 printf("bfd error writing file %s, format %s can't represent section %s\n",
79 bfd_error_vector_type bfd_error_vector
=
81 bfd_nonrepresentable_section
84 #if !defined(ANSI_LIBRARIES)
90 extern char *sys_errlist
[];
92 return (((code
< 0) || (code
>= sys_nerr
)) ? "(unknown error)" :
95 #endif /* not ANSI_LIBRARIES */
99 bfd_errmsg (error_tag
)
103 if (error_tag
== system_call_error
)
104 return strerror (errno
);
106 if ((((int)error_tag
<(int) no_error
) ||
107 ((int)error_tag
> (int)invalid_error_code
)))
108 error_tag
= invalid_error_code
;/* sanity check */
110 return bfd_errmsgs
[(int)error_tag
];
114 void bfd_default_error_trap(error_tag
)
117 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag
));
120 void (*bfd_error_trap
)() = bfd_default_error_trap
;
121 void (*bfd_error_nonrepresentabltrap
)() = bfd_default_error_trap
;
124 DEFUN(bfd_perror
,(message
),
127 if (bfd_error
== system_call_error
)
128 perror((char *)message
); /* must be system error then... */
130 if (message
== NULL
|| *message
== '\0')
131 fprintf (stderr
, "%s\n", bfd_errmsg (bfd_error
));
133 fprintf (stderr
, "%s: %s\n", message
, bfd_errmsg (bfd_error
));
137 /* for error messages */
139 bfd_format_string (format
)
142 if (((int)format
<(int) bfd_unknown
) || ((int)format
>=(int) bfd_type_end
)) return "invalid";
145 case bfd_object
: return "object"; /* linker/assember/compiler output */
146 case bfd_archive
: return "archive"; /* object archive file */
147 case bfd_core
: return "core"; /* core dump */
148 default: return "unknown";
152 /** Target configurations */
154 extern bfd_target
*target_vector
[];
156 /* Returns a pointer to the transfer vector for the object target
157 named target_name. If target_name is NULL, chooses the one in the
158 environment variable GNUTARGET; if that is null or not defined then
159 the first entry in the target list is chosen. Passing in the
160 string "default" or setting the environment variable to "default"
161 will cause the first entry in the target list to be returned. */
164 DEFUN(bfd_find_target
,(target_name
),
165 CONST
char *target_name
)
168 extern char *getenv ();
169 CONST
char *targname
= (target_name
? target_name
: getenv ("GNUTARGET"));
171 /* This is safe; the vector cannot be null */
172 if (targname
== NULL
|| !strcmp (targname
, "default"))
173 return target_vector
[0];
175 for (target
= &target_vector
[0]; *target
!= NULL
; target
++) {
176 if (!strcmp (targname
, (*target
)->name
))
180 bfd_error
= invalid_target
;
184 /* Returns a freshly-consed, NULL-terminated vector of the names of all the
185 valid bfd targets. Do not modify the names */
192 char **name_list
, **name_ptr
;
194 for (target
= &target_vector
[0]; *target
!= NULL
; target
++)
197 name_ptr
= name_list
= (char **) zalloc ((vec_length
+ 1) * sizeof (char **));
199 if (name_list
== NULL
) {
200 bfd_error
= no_memory
;
204 for (target
= &target_vector
[0]; *target
!= NULL
; target
++)
205 *(name_ptr
++) = (*target
)->name
;
210 /** Init a bfd for read of the proper format.
213 /* We should be able to find out if the target was defaulted or user-specified.
214 If the user specified the target explicitly then we should do no search.
215 I guess the best way to do this is to pass an extra argument which specifies
218 /* I have chanegd this always to set the filepos to the origin before
219 guessing. -- Gumby, 14 Februar 1991*/
222 bfd_check_format (abfd
, format
)
226 bfd_target
**target
, *save_targ
, *right_targ
;
229 if (!bfd_read_p (abfd
) ||
230 ((int)(abfd
->format
) < (int)bfd_unknown
) ||
231 ((int)(abfd
->format
) >= (int)bfd_type_end
)) {
232 bfd_error
= invalid_operation
;
236 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
238 /* presume the answer is yes */
239 abfd
->format
= format
;
241 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
); /* rewind! */
243 right_targ
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
245 abfd
->xvec
= right_targ
; /* Set the target as returned */
246 return true; /* File position has moved, BTW */
249 /* This isn't a <format> file in the specified or defaulted target type.
250 See if we recognize it for any other target type. (We check them
251 all to make sure it's uniquely recognized.) */
253 save_targ
= abfd
->xvec
;
257 for (target
= target_vector
; *target
!= NULL
; target
++) {
260 abfd
->xvec
= *target
; /* Change BFD's target temporarily */
261 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
);
262 temp
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
263 if (temp
) { /* This format checks out as ok! */
267 /* Big- and little-endian b.out archives look the same, but it doesn't
268 * matter: there is no difference in their headers, and member file byte
269 * orders will (I hope) be handled appropriately by bfd. Ditto for big
270 * and little coff archives. And the 4 coff/b.out object formats are
271 * unambiguous. So accept the first match we find.
278 if (match_count
== 1) {
279 abfd
->xvec
= right_targ
; /* Change BFD's target permanently */
280 return true; /* File position has moved, BTW */
283 abfd
->xvec
= save_targ
; /* Restore original target type */
284 abfd
->format
= bfd_unknown
; /* Restore original format */
285 bfd_error
= ((match_count
== 0) ? file_not_recognized
:
286 file_ambiguously_recognized
);
291 bfd_set_format (abfd
, format
)
296 if (bfd_read_p (abfd
) ||
297 ((int)abfd
->format
< (int)bfd_unknown
) ||
298 ((int)abfd
->format
>= (int)bfd_type_end
)) {
299 bfd_error
= invalid_operation
;
303 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
305 /* presume the answer is yes */
306 abfd
->format
= format
;
308 if (!BFD_SEND_FMT (abfd
, _bfd_set_format
, (abfd
))) {
309 abfd
->format
= bfd_unknown
;
316 /* Hack object and core file sections */
319 DEFUN(bfd_get_section_by_name
,(abfd
, name
),
325 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
326 if (!strcmp (sect
->name
, name
)) return sect
;
330 /* If you try to create a section with a name which is already in use,
331 returns the old section by that name instead. */
333 DEFUN(bfd_make_section
,(abfd
, name
),
338 asection
** prev
= &abfd
->sections
;
339 asection
* sect
= abfd
->sections
;
341 if (abfd
->output_has_begun
) {
342 bfd_error
= invalid_operation
;
347 if (!strcmp(sect
->name
, name
)) return sect
;
352 newsect
= (asection
*) bfd_zalloc(abfd
, sizeof (asection
));
353 if (newsect
== NULL
) {
354 bfd_error
= no_memory
;
358 newsect
->name
= name
;
359 newsect
->index
= abfd
->section_count
++;
360 newsect
->flags
= SEC_NO_FLAGS
;
362 newsect
->userdata
= 0;
363 newsect
->next
= (asection
*)NULL
;
364 newsect
->relocation
= (arelent
*)NULL
;
365 newsect
->reloc_count
= 0;
366 newsect
->line_filepos
=0;
368 if (BFD_SEND (abfd
, _new_section_hook
, (abfd
, newsect
)) != true) {
377 /* Call operation on each section. Operation gets three args: the bfd,
378 the section, and a void * pointer (whatever the user supplied). */
380 /* This is attractive except that without lexical closures its use is hard
381 to make reentrant. */
384 bfd_map_over_sections (abfd
, operation
, user_storage
)
392 for (sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
393 (*operation
) (abfd
, sect
, user_storage
);
395 if (i
!= abfd
->section_count
) /* Debugging */
400 bfd_set_section_flags (abfd
, section
, flags
)
405 if ((flags
& bfd_applicable_section_flags (abfd
)) != flags
) {
406 bfd_error
= invalid_operation
;
410 section
->flags
= flags
;
416 bfd_set_section_size (abfd
, ptr
, val
)
421 /* Once you've started writing to any section you cannot create or change
422 the size of any others. */
424 if (abfd
->output_has_begun
) {
425 bfd_error
= invalid_operation
;
435 bfd_set_section_contents (abfd
, section
, location
, offset
, count
)
442 if (!(bfd_get_section_flags(abfd
, section
) &
444 bfd_error
= no_contents
;
446 } /* if section has no contents */
448 if (BFD_SEND (abfd
, _bfd_set_section_contents
,
449 (abfd
, section
, location
, offset
, count
))) {
450 abfd
->output_has_begun
= true;
458 bfd_get_section_contents (abfd
, section
, location
, offset
, count
)
465 if (section
->flags
& SEC_CONSTRUCTOR
) {
466 memset(location
, 0, count
);
470 return (BFD_SEND (abfd
, _bfd_get_section_contents
,
471 (abfd
, section
, location
, offset
, count
)));
476 /** Some core file info commands */
478 /* Returns a read-only string explaining what program was running when
482 bfd_core_file_failing_command (abfd
)
485 if (abfd
->format
!= bfd_core
) {
486 bfd_error
= invalid_operation
;
489 return BFD_SEND (abfd
, _core_file_failing_command
, (abfd
));
493 bfd_core_file_failing_signal (abfd
)
496 if (abfd
->format
!= bfd_core
) {
497 bfd_error
= invalid_operation
;
500 return BFD_SEND (abfd
, _core_file_failing_signal
, (abfd
));
504 core_file_matches_executable_p (core_bfd
, exec_bfd
)
505 bfd
*core_bfd
, *exec_bfd
;
507 if ((core_bfd
->format
!= bfd_core
) || (exec_bfd
->format
!= bfd_object
)) {
508 bfd_error
= wrong_format
;
512 return BFD_SEND (core_bfd
, _core_file_matches_executable_p
, (core_bfd
, exec_bfd
));
518 bfd_set_symtab (abfd
, location
, symcount
)
521 unsigned int symcount
;
523 if ((abfd
->format
!= bfd_object
) || (bfd_read_p (abfd
))) {
524 bfd_error
= invalid_operation
;
528 bfd_get_outsymbols (abfd
) = location
;
529 bfd_get_symcount (abfd
) = symcount
;
533 /* returns the number of octets of storage required */
535 get_reloc_upper_bound (abfd
, asect
)
539 if (abfd
->format
!= bfd_object
) {
540 bfd_error
= invalid_operation
;
544 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
548 bfd_canonicalize_reloc (abfd
, asect
, location
, symbols
)
554 if (abfd
->format
!= bfd_object
) {
555 bfd_error
= invalid_operation
;
559 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
, (abfd
, asect
, location
, symbols
));
563 bfd_print_symbol_vandf(file
, symbol
)
567 flagword type
= symbol
->flags
;
568 if (symbol
->section
!= (asection
*)NULL
)
570 fprintf(file
,"%08lx ", symbol
->value
+symbol
->section
->vma
);
574 fprintf(file
,"%08lx ", symbol
->value
);
576 fprintf(file
,"%c%c%c%c%c%c%c",
577 (type
& BSF_LOCAL
) ? 'l':' ',
578 (type
& BSF_GLOBAL
) ? 'g' : ' ',
579 (type
& BSF_IMPORT
) ? 'i' : ' ',
580 (type
& BSF_EXPORT
) ? 'e' : ' ',
581 (type
& BSF_UNDEFINED
) ? 'u' : ' ',
582 (type
& BSF_FORT_COMM
) ? 'c' : ' ',
583 (type
& BSF_DEBUGGING
) ? 'd' :' ');
589 bfd_set_file_flags (abfd
, flags
)
593 if (abfd
->format
!= bfd_object
) {
594 bfd_error
= wrong_format
;
598 if (bfd_read_p (abfd
)) {
599 bfd_error
= invalid_operation
;
603 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
) {
604 bfd_error
= invalid_operation
;
608 bfd_get_file_flags (abfd
) = flags
;
614 bfd_set_reloc (ignore_abfd
, asect
, location
, count
)
620 asect
->orelocation
= location
;
621 asect
->reloc_count
= count
;
624 If an output_bfd is supplied to this function the generated image
625 will be relocatable, the relocations are copied to the output file
626 after they have been changed to reflect the new state of the world.
627 There are two ways of reflecting the results of partial linkage in an
628 output file; by modifying the output data in place, and by modifying
629 the relocation record. Some native formats (eg basic a.out and basic
630 coff) have no way of specifying an addend in the relocation type, so
631 the addend has to go in the output data. This is no big deal since in
632 these formats the output data slot will always be big enough for the
633 addend. Complex reloc types with addends were invented to solve just
637 bfd_reloc_status_enum_type
638 bfd_perform_relocation(abfd
,
644 arelent
*reloc_entry
;
646 asection
*input_section
;
650 bfd_reloc_status_enum_type flag
= bfd_reloc_ok
;
651 bfd_vma relocation_before
;
652 bfd_vma addr
= reloc_entry
->address
;
653 bfd_vma output_base
= 0;
654 reloc_howto_type
*howto
= reloc_entry
->howto
;
655 asection
*reloc_target_output_section
;
656 asection
*reloc_target_input_section
;
659 if (reloc_entry
->sym_ptr_ptr
) {
660 symbol
= *( reloc_entry
->sym_ptr_ptr
);
661 if ((symbol
->flags
& BSF_UNDEFINED
) && output_bfd
== (bfd
*)NULL
) {
662 flag
= bfd_reloc_undefined
;
666 symbol
= (asymbol
*)NULL
;
669 if (howto
->special_function
){
670 bfd_reloc_status_enum_type cont
;
671 cont
= howto
->special_function(abfd
,
676 if (cont
!= bfd_reloc_continue
) return cont
;
680 Work out which section the relocation is targetted at and the
681 initial relocation command value.
685 if (symbol
!= (asymbol
*)NULL
){
686 if (symbol
->flags
& BSF_FORT_COMM
) {
690 relocation
= symbol
->value
;
692 if (symbol
->section
!= (asection
*)NULL
)
694 reloc_target_input_section
= symbol
->section
;
697 reloc_target_input_section
= (asection
*)NULL
;
700 else if (reloc_entry
->section
!= (asection
*)NULL
)
703 reloc_target_input_section
= reloc_entry
->section
;
707 reloc_target_input_section
= (asection
*)NULL
;
711 if (reloc_target_input_section
!= (asection
*)NULL
) {
713 reloc_target_output_section
=
714 reloc_target_input_section
->output_section
;
716 if (output_bfd
&& howto
->partial_inplace
==false) {
720 output_base
= reloc_target_output_section
->vma
;
724 relocation
+= output_base
+ reloc_target_input_section
->output_offset
;
727 relocation
+= reloc_entry
->addend
;
730 if(reloc_entry
->address
> (bfd_vma
)(input_section
->size
))
732 return bfd_reloc_outofrange
;
736 if (howto
->pc_relative
== true)
739 Anything which started out as pc relative should end up that
742 There are two ways we can see a pcrel instruction. Sometimes
743 the pcrel displacement has been partially calculated, it
744 includes the distance from the start of the section to the
745 instruction in it (eg sun3), and sometimes the field is
746 totally blank - eg m88kbcs.
751 output_base
+ input_section
->output_offset
;
753 if (howto
->pcrel_offset
== true) {
754 relocation
-= reloc_entry
->address
;
759 if (output_bfd
!= (bfd
*)NULL
) {
760 if ( howto
->partial_inplace
== false) {
762 This is a partial relocation, and we want to apply the relocation
763 to the reloc entry rather than the raw data. Modify the reloc
764 inplace to reflect what we now know.
766 reloc_entry
->addend
= relocation
;
767 reloc_entry
->section
= reloc_target_input_section
;
768 if (reloc_target_input_section
!= (asection
*)NULL
) {
769 /* If we know the output section we can forget the symbol */
770 reloc_entry
->sym_ptr_ptr
= (asymbol
**)NULL
;
772 reloc_entry
->address
+=
773 input_section
->output_offset
;
778 /* This is a partial relocation, but inplace, so modify the
785 reloc_entry
->addend
= 0;
789 Either we are relocating all the way, or we don't want to apply
790 the relocation to the reloc entry (probably because there isn't
791 any room in the output format to describe addends to relocs)
793 relocation
>>= howto
->rightshift
;
795 /* Shift everything up to where it's going to be used */
797 relocation
<<= howto
->bitpos
;
800 /* Wait for the day when all have the mask in them */
804 relocation_before
= relocation
;
808 i instruction to be left alone
809 o offset within instruction
810 r relocation offset to apply
819 i i i i i o o o o o from bfd_get<size>
820 and S S S S S to get the size offset we want
821 + r r r r r r r r r r to get the final value to place
822 and D D D D D to chop to right size
823 -----------------------
826 ... i i i i i o o o o o from bfd_get<size>
827 and N N N N N get instruction
828 -----------------------
834 -----------------------
835 R R R R R R R R R R put into bfd_put<size>
839 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
845 char x
= bfd_getchar(abfd
, (char *)data
+ addr
);
847 bfd_putchar(abfd
,x
, (unsigned char *) data
+ addr
);
853 short x
= bfd_getshort(abfd
, (bfd_byte
*)data
+ addr
);
855 bfd_putshort(abfd
, x
, (unsigned char *)data
+ addr
);
860 long x
= bfd_getlong(abfd
, (bfd_byte
*) data
+ addr
);
862 bfd_putlong(abfd
,x
, (bfd_byte
*)data
+ addr
);
869 return bfd_reloc_other
;
876 bfd_assert(file
, line
)
880 printf("bfd assertion fail %s:%d\n",file
,line
);
885 bfd_set_start_address(abfd
, vma
)
889 abfd
->start_address
= vma
;
898 while ( (bfd_vma
)(1<< result
) < x
)
903 /* bfd_get_mtime: Return cached file modification time (e.g. as read
904 from archive header for archive members, or from file system if we have
905 been called before); else determine modify time, cache it, and
918 fp
= bfd_cache_lookup (abfd
);
919 if (0 != fstat (fileno (fp
), &buf
))
922 abfd
->mtime_set
= true;
923 abfd
->mtime
= buf
.st_mtime
;