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.h. 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 1 || !defined(ANSI_LIBRARIES) && !defined(__STDC__)
90 extern char *sys_errlist
[];
92 return (((code
< 0) || (code
>= sys_nerr
)) ? "(unknown error)" :
95 #endif /* not ANSI_LIBRARIES */
99 bfd_errmsg (error_tag
)
105 if (error_tag
== system_call_error
)
106 return strerror (errno
);
108 if ((((int)error_tag
<(int) no_error
) ||
109 ((int)error_tag
> (int)invalid_error_code
)))
110 error_tag
= invalid_error_code
;/* sanity check */
112 return bfd_errmsgs
[(int)error_tag
];
116 void bfd_default_error_trap(error_tag
)
119 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag
));
122 void (*bfd_error_trap
)() = bfd_default_error_trap
;
123 void (*bfd_error_nonrepresentabltrap
)() = bfd_default_error_trap
;
126 DEFUN(bfd_perror
,(message
),
129 if (bfd_error
== system_call_error
)
130 perror((char *)message
); /* must be system error then... */
132 if (message
== NULL
|| *message
== '\0')
133 fprintf (stderr
, "%s\n", bfd_errmsg (bfd_error
));
135 fprintf (stderr
, "%s: %s\n", message
, bfd_errmsg (bfd_error
));
139 /* for error messages */
141 bfd_format_string (format
)
144 if (((int)format
<(int) bfd_unknown
) || ((int)format
>=(int) bfd_type_end
)) return "invalid";
147 case bfd_object
: return "object"; /* linker/assember/compiler output */
148 case bfd_archive
: return "archive"; /* object archive file */
149 case bfd_core
: return "core"; /* core dump */
150 default: return "unknown";
154 /** Target configurations */
156 extern bfd_target
*target_vector
[];
158 /* Returns a pointer to the transfer vector for the object target
159 named target_name. If target_name is NULL, chooses the one in the
160 environment variable GNUTARGET; if that is null or not defined then
161 the first entry in the target list is chosen. Passing in the
162 string "default" or setting the environment variable to "default"
163 will cause the first entry in the target list to be returned. */
166 DEFUN(bfd_find_target
,(target_name
),
167 CONST
char *target_name
)
170 extern char *getenv ();
171 CONST
char *targname
= (target_name
? target_name
: getenv ("GNUTARGET"));
173 /* This is safe; the vector cannot be null */
174 if (targname
== NULL
|| !strcmp (targname
, "default"))
175 return target_vector
[0];
177 for (target
= &target_vector
[0]; *target
!= NULL
; target
++) {
178 if (!strcmp (targname
, (*target
)->name
))
182 bfd_error
= invalid_target
;
186 /* Returns a freshly-consed, NULL-terminated vector of the names of all the
187 valid bfd targets. Do not modify the names */
194 char **name_list
, **name_ptr
;
196 for (target
= &target_vector
[0]; *target
!= NULL
; target
++)
199 name_ptr
= name_list
= (char **) zalloc ((vec_length
+ 1) * sizeof (char **));
201 if (name_list
== NULL
) {
202 bfd_error
= no_memory
;
206 for (target
= &target_vector
[0]; *target
!= NULL
; target
++)
207 *(name_ptr
++) = (*target
)->name
;
212 /** Init a bfd for read of the proper format.
215 /* We should be able to find out if the target was defaulted or user-specified.
216 If the user specified the target explicitly then we should do no search.
217 I guess the best way to do this is to pass an extra argument which specifies
220 /* I have chanegd this always to set the filepos to the origin before
221 guessing. -- Gumby, 14 Februar 1991*/
224 DEFUN(bfd_check_format
,(abfd
, format
),
228 bfd_target
**target
, *save_targ
, *right_targ
;
231 if (!bfd_read_p (abfd
) ||
232 ((int)(abfd
->format
) < (int)bfd_unknown
) ||
233 ((int)(abfd
->format
) >= (int)bfd_type_end
)) {
234 bfd_error
= invalid_operation
;
238 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
240 /* presume the answer is yes */
241 abfd
->format
= format
;
243 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
); /* rewind! */
245 right_targ
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
247 abfd
->xvec
= right_targ
; /* Set the target as returned */
248 return true; /* File position has moved, BTW */
251 /* This isn't a <format> file in the specified or defaulted target type.
252 See if we recognize it for any other target type. (We check them
253 all to make sure it's uniquely recognized.) */
255 save_targ
= abfd
->xvec
;
259 for (target
= target_vector
; *target
!= NULL
; target
++) {
262 abfd
->xvec
= *target
; /* Change BFD's target temporarily */
263 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
);
264 temp
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
265 if (temp
) { /* This format checks out as ok! */
269 /* Big- and little-endian b.out archives look the same, but it doesn't
270 * matter: there is no difference in their headers, and member file byte
271 * orders will (I hope) be handled appropriately by bfd. Ditto for big
272 * and little coff archives. And the 4 coff/b.out object formats are
273 * unambiguous. So accept the first match we find.
280 if (match_count
== 1) {
281 abfd
->xvec
= right_targ
; /* Change BFD's target permanently */
282 return true; /* File position has moved, BTW */
285 abfd
->xvec
= save_targ
; /* Restore original target type */
286 abfd
->format
= bfd_unknown
; /* Restore original format */
287 bfd_error
= ((match_count
== 0) ? file_not_recognized
:
288 file_ambiguously_recognized
);
293 DEFUN(bfd_set_format
,(abfd
, format
),
298 if (bfd_read_p (abfd
) ||
299 ((int)abfd
->format
< (int)bfd_unknown
) ||
300 ((int)abfd
->format
>= (int)bfd_type_end
)) {
301 bfd_error
= invalid_operation
;
305 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
307 /* presume the answer is yes */
308 abfd
->format
= format
;
310 if (!BFD_SEND_FMT (abfd
, _bfd_set_format
, (abfd
))) {
311 abfd
->format
= bfd_unknown
;
318 /* Hack object and core file sections */
321 DEFUN(bfd_get_section_by_name
,(abfd
, name
),
327 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
328 if (!strcmp (sect
->name
, name
)) return sect
;
332 /* If you try to create a section with a name which is already in use,
333 returns the old section by that name instead. */
335 DEFUN(bfd_make_section
,(abfd
, name
),
337 CONST
char *CONST name
)
340 asection
** prev
= &abfd
->sections
;
341 asection
* sect
= abfd
->sections
;
343 if (abfd
->output_has_begun
) {
344 bfd_error
= invalid_operation
;
349 if (!strcmp(sect
->name
, name
)) return sect
;
354 newsect
= (asection
*) bfd_zalloc(abfd
, sizeof (asection
));
355 if (newsect
== NULL
) {
356 bfd_error
= no_memory
;
360 newsect
->name
= name
;
361 newsect
->index
= abfd
->section_count
++;
362 newsect
->flags
= SEC_NO_FLAGS
;
364 newsect
->userdata
= 0;
365 newsect
->next
= (asection
*)NULL
;
366 newsect
->relocation
= (arelent
*)NULL
;
367 newsect
->reloc_count
= 0;
368 newsect
->line_filepos
=0;
370 if (BFD_SEND (abfd
, _new_section_hook
, (abfd
, newsect
)) != true) {
379 /* Call operation on each section. Operation gets three args: the bfd,
380 the section, and a void * pointer (whatever the user supplied). */
382 /* This is attractive except that without lexical closures its use is hard
383 to make reentrant. */
386 bfd_map_over_sections (abfd
, operation
, user_storage
)
394 for (sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
395 (*operation
) (abfd
, sect
, user_storage
);
397 if (i
!= abfd
->section_count
) /* Debugging */
402 bfd_set_section_flags (abfd
, section
, flags
)
407 if ((flags
& bfd_applicable_section_flags (abfd
)) != flags
) {
408 bfd_error
= invalid_operation
;
412 section
->flags
= flags
;
418 bfd_set_section_size (abfd
, ptr
, val
)
423 /* Once you've started writing to any section you cannot create or change
424 the size of any others. */
426 if (abfd
->output_has_begun
) {
427 bfd_error
= invalid_operation
;
437 DEFUN(bfd_set_section_contents
,(abfd
, section
, location
, offset
, count
),
444 if (!(bfd_get_section_flags(abfd
, section
) &
446 bfd_error
= no_contents
;
448 } /* if section has no contents */
450 if (BFD_SEND (abfd
, _bfd_set_section_contents
,
451 (abfd
, section
, location
, offset
, count
))) {
452 abfd
->output_has_begun
= true;
460 DEFUN(bfd_get_section_contents
,(abfd
, section
, location
, offset
, count
),
467 if (section
->flags
& SEC_CONSTRUCTOR
) {
468 memset(location
, 0, (unsigned)count
);
472 return (BFD_SEND (abfd
, _bfd_get_section_contents
,
473 (abfd
, section
, location
, offset
, count
)));
478 /** Some core file info commands */
480 /* Returns a read-only string explaining what program was running when
484 bfd_core_file_failing_command (abfd
)
487 if (abfd
->format
!= bfd_core
) {
488 bfd_error
= invalid_operation
;
491 return BFD_SEND (abfd
, _core_file_failing_command
, (abfd
));
495 bfd_core_file_failing_signal (abfd
)
498 if (abfd
->format
!= bfd_core
) {
499 bfd_error
= invalid_operation
;
502 return BFD_SEND (abfd
, _core_file_failing_signal
, (abfd
));
506 core_file_matches_executable_p (core_bfd
, exec_bfd
)
507 bfd
*core_bfd
, *exec_bfd
;
509 if ((core_bfd
->format
!= bfd_core
) || (exec_bfd
->format
!= bfd_object
)) {
510 bfd_error
= wrong_format
;
514 return BFD_SEND (core_bfd
, _core_file_matches_executable_p
, (core_bfd
, exec_bfd
));
520 bfd_set_symtab (abfd
, location
, symcount
)
523 unsigned int symcount
;
525 if ((abfd
->format
!= bfd_object
) || (bfd_read_p (abfd
))) {
526 bfd_error
= invalid_operation
;
530 bfd_get_outsymbols (abfd
) = location
;
531 bfd_get_symcount (abfd
) = symcount
;
535 /* returns the number of octets of storage required */
537 get_reloc_upper_bound (abfd
, asect
)
541 if (abfd
->format
!= bfd_object
) {
542 bfd_error
= invalid_operation
;
546 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
550 bfd_canonicalize_reloc (abfd
, asect
, location
, symbols
)
556 if (abfd
->format
!= bfd_object
) {
557 bfd_error
= invalid_operation
;
561 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
, (abfd
, asect
, location
, symbols
));
565 bfd_print_symbol_vandf(file
, symbol
)
569 flagword type
= symbol
->flags
;
570 if (symbol
->section
!= (asection
*)NULL
)
573 fprintf_vma(file
, symbol
->value
+symbol
->section
->vma
);
577 fprintf_vma(file
, symbol
->value
);
579 fprintf(file
," %c%c%c%c%c%c%c",
580 (type
& BSF_LOCAL
) ? 'l':' ',
581 (type
& BSF_GLOBAL
) ? 'g' : ' ',
582 (type
& BSF_IMPORT
) ? 'i' : ' ',
583 (type
& BSF_EXPORT
) ? 'e' : ' ',
584 (type
& BSF_UNDEFINED
) ? 'u' : ' ',
585 (type
& BSF_FORT_COMM
) ? 'c' : ' ',
586 (type
& BSF_DEBUGGING
) ? 'd' :' ');
592 bfd_set_file_flags (abfd
, flags
)
596 if (abfd
->format
!= bfd_object
) {
597 bfd_error
= wrong_format
;
601 if (bfd_read_p (abfd
)) {
602 bfd_error
= invalid_operation
;
606 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
) {
607 bfd_error
= invalid_operation
;
611 bfd_get_file_flags (abfd
) = flags
;
617 bfd_set_reloc (ignore_abfd
, asect
, location
, count
)
623 asect
->orelocation
= location
;
624 asect
->reloc_count
= count
;
627 If an output_bfd is supplied to this function the generated image
628 will be relocatable, the relocations are copied to the output file
629 after they have been changed to reflect the new state of the world.
630 There are two ways of reflecting the results of partial linkage in an
631 output file; by modifying the output data in place, and by modifying
632 the relocation record. Some native formats (eg basic a.out and basic
633 coff) have no way of specifying an addend in the relocation type, so
634 the addend has to go in the output data. This is no big deal since in
635 these formats the output data slot will always be big enough for the
636 addend. Complex reloc types with addends were invented to solve just
640 bfd_reloc_status_enum_type
641 DEFUN(bfd_perform_relocation
,(abfd
,
647 arelent
*reloc_entry AND
649 asection
*input_section AND
653 bfd_reloc_status_enum_type flag
= bfd_reloc_ok
;
654 bfd_vma addr
= reloc_entry
->address
;
655 bfd_vma output_base
= 0;
656 reloc_howto_type
*howto
= reloc_entry
->howto
;
657 asection
*reloc_target_output_section
;
658 asection
*reloc_target_input_section
;
661 if (reloc_entry
->sym_ptr_ptr
) {
662 symbol
= *( reloc_entry
->sym_ptr_ptr
);
663 if ((symbol
->flags
& BSF_UNDEFINED
) && output_bfd
== (bfd
*)NULL
) {
664 flag
= bfd_reloc_undefined
;
668 symbol
= (asymbol
*)NULL
;
671 if (howto
->special_function
){
672 bfd_reloc_status_enum_type cont
;
673 cont
= howto
->special_function(abfd
,
678 if (cont
!= bfd_reloc_continue
) return cont
;
682 Work out which section the relocation is targetted at and the
683 initial relocation command value.
687 if (symbol
!= (asymbol
*)NULL
){
688 if (symbol
->flags
& BSF_FORT_COMM
) {
692 relocation
= symbol
->value
;
694 if (symbol
->section
!= (asection
*)NULL
)
696 reloc_target_input_section
= symbol
->section
;
699 reloc_target_input_section
= (asection
*)NULL
;
702 else if (reloc_entry
->section
!= (asection
*)NULL
)
705 reloc_target_input_section
= reloc_entry
->section
;
709 reloc_target_input_section
= (asection
*)NULL
;
713 if (reloc_target_input_section
!= (asection
*)NULL
) {
715 reloc_target_output_section
=
716 reloc_target_input_section
->output_section
;
718 if (output_bfd
&& howto
->partial_inplace
==false) {
722 output_base
= reloc_target_output_section
->vma
;
726 relocation
+= output_base
+ reloc_target_input_section
->output_offset
;
729 relocation
+= reloc_entry
->addend
;
732 if(reloc_entry
->address
> (bfd_vma
)(input_section
->size
))
734 return bfd_reloc_outofrange
;
738 if (howto
->pc_relative
== true)
741 Anything which started out as pc relative should end up that
744 There are two ways we can see a pcrel instruction. Sometimes
745 the pcrel displacement has been partially calculated, it
746 includes the distance from the start of the section to the
747 instruction in it (eg sun3), and sometimes the field is
748 totally blank - eg m88kbcs.
753 output_base
+ input_section
->output_offset
;
755 if (howto
->pcrel_offset
== true) {
756 relocation
-= reloc_entry
->address
;
761 if (output_bfd
!= (bfd
*)NULL
) {
762 if ( howto
->partial_inplace
== false) {
764 This is a partial relocation, and we want to apply the relocation
765 to the reloc entry rather than the raw data. Modify the reloc
766 inplace to reflect what we now know.
768 reloc_entry
->addend
= relocation
;
769 reloc_entry
->section
= reloc_target_input_section
;
770 if (reloc_target_input_section
!= (asection
*)NULL
) {
771 /* If we know the output section we can forget the symbol */
772 reloc_entry
->sym_ptr_ptr
= (asymbol
**)NULL
;
774 reloc_entry
->address
+=
775 input_section
->output_offset
;
780 /* This is a partial relocation, but inplace, so modify the
787 reloc_entry
->addend
= 0;
791 Either we are relocating all the way, or we don't want to apply
792 the relocation to the reloc entry (probably because there isn't
793 any room in the output format to describe addends to relocs)
795 relocation
>>= howto
->rightshift
;
797 /* Shift everything up to where it's going to be used */
799 relocation
<<= howto
->bitpos
;
801 /* Wait for the day when all have the mask in them */
804 i instruction to be left alone
805 o offset within instruction
806 r relocation offset to apply
815 i i i i i o o o o o from bfd_get<size>
816 and S S S S S to get the size offset we want
817 + r r r r r r r r r r to get the final value to place
818 and D D D D D to chop to right size
819 -----------------------
822 ... i i i i i o o o o o from bfd_get<size>
823 and N N N N N get instruction
824 -----------------------
830 -----------------------
831 R R R R R R R R R R put into bfd_put<size>
835 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
841 char x
= bfd_get_8(abfd
, (char *)data
+ addr
);
843 bfd_put_8(abfd
,x
, (unsigned char *) data
+ addr
);
849 short x
= bfd_get_16(abfd
, (bfd_byte
*)data
+ addr
);
851 bfd_put_16(abfd
, x
, (unsigned char *)data
+ addr
);
856 long x
= bfd_get_32(abfd
, (bfd_byte
*) data
+ addr
);
858 bfd_put_32(abfd
,x
, (bfd_byte
*)data
+ addr
);
865 return bfd_reloc_other
;
872 bfd_assert(file
, line
)
876 printf("bfd assertion fail %s:%d\n",file
,line
);
881 bfd_set_start_address(abfd
, vma
)
885 abfd
->start_address
= vma
;
894 while ( (bfd_vma
)(1<< result
) < x
)
899 /* bfd_get_mtime: Return cached file modification time (e.g. as read
900 from archive header for archive members, or from file system if we have
901 been called before); else determine modify time, cache it, and
914 fp
= bfd_cache_lookup (abfd
);
915 if (0 != fstat (fileno (fp
), &buf
))
918 abfd
->mtime_set
= true;
919 abfd
->mtime
= buf
.st_mtime
;