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 * (See HOST_IS_BIG_ENDIAN_P in bfd.h.)
43 o - Most functions return nonzero on success (check doc for
44 precise semantics); 0 or NULL on error.
45 o - Internal errors are documented by the value of bfd_error.
46 If that is system_call_error then check errno.
47 o - The easiest way to report this to the user is to use bfd_perror.
50 bfd_ec bfd_error
= no_error
;
52 char *bfd_errmsgs
[] = {"No error",
55 "File in wrong format",
60 "No more archived files",
63 "File format not recognized",
64 "File format is ambiguous",
65 "Section has no contents",
66 "Nonrepresentable section on output",
67 "#<Invalid error code>"
73 DEFUN(bfd_nonrepresentable_section
,(abfd
, name
),
74 CONST bfd
* CONST abfd AND
75 CONST
char * CONST name
)
77 printf("bfd error writing file %s, can't represent section name %s\n", abfd
->filename
, name
);
80 bfd_error_vector_type bfd_error_vector
=
82 bfd_nonrepresentable_section
85 #if !defined(ANSI_LIBRARIES)
91 extern char *sys_errlist
[];
93 return (((code
< 0) || (code
>= sys_nerr
)) ? "(unknown error)" :
96 #endif /* not ANSI_LIBRARIES */
101 bfd_errmsg (error_tag
)
106 if (error_tag
== system_call_error
)
107 return strerror (errno
);
109 if ((((int)error_tag
<(int) no_error
) ||
110 ((int)error_tag
> (int)invalid_error_code
)))
111 error_tag
= invalid_error_code
;/* sanity check */
113 return bfd_errmsgs
[(int)error_tag
];
117 void bfd_default_error_trap(error_tag
)
120 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag
));
123 void (*bfd_error_trap
)() = bfd_default_error_trap
;
124 void (*bfd_error_nonrepresentabltrap
)() = bfd_default_error_trap
;
129 if (bfd_error
== system_call_error
)
130 perror(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 bfd_check_format (abfd
, format
)
231 bfd_target
**target
, *save_targ
, *right_targ
;
234 if (!bfd_read_p (abfd
) ||
235 ((int)(abfd
->format
) < (int)bfd_unknown
) ||
236 ((int)(abfd
->format
) >= (int)bfd_type_end
)) {
237 bfd_error
= invalid_operation
;
241 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
243 /* presume the answer is yes */
244 abfd
->format
= format
;
247 filepos
= bfd_tell (abfd
);
249 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
); /* instead, rewind! */
252 right_targ
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
254 abfd
->xvec
= right_targ
; /* Set the target as returned */
255 return true; /* File position has moved, BTW */
258 /* This isn't a <format> file in the specified or defaulted target type.
259 See if we recognize it for any other target type. (We check them
260 all to make sure it's uniquely recognized.) */
262 save_targ
= abfd
->xvec
;
266 for (target
= target_vector
; *target
!= NULL
; target
++) {
269 abfd
->xvec
= *target
; /* Change BFD's target temporarily */
271 bfd_seek (abfd
, filepos
, SEEK_SET
); /* Restore original file position */
273 bfd_seek (abfd
, (file_ptr
)0, SEEK_SET
);
274 temp
= BFD_SEND_FMT (abfd
, _bfd_check_format
, (abfd
));
275 if (temp
) { /* This format checks out as ok! */
279 /* Big- and little-endian b.out archives look the same, but it doesn't
280 * matter: there is no difference in their headers, and member file byte
281 * orders will (I hope) be handled appropriately by bfd. Ditto for big
282 * and little coff archives. And the 4 coff/b.out object formats are
283 * unambiguous. So accept the first match we find.
290 if (match_count
== 1) {
291 abfd
->xvec
= right_targ
; /* Change BFD's target permanently */
292 return true; /* File position has moved, BTW */
295 abfd
->xvec
= save_targ
; /* Restore original target type */
296 abfd
->format
= bfd_unknown
; /* Restore original format */
297 bfd_error
= ((match_count
== 0) ? file_not_recognized
:
298 file_ambiguously_recognized
);
300 bfd_seek (abfd
, filepos
, SEEK_SET
); /* Restore original file position */
306 bfd_set_format (abfd
, format
)
312 if (bfd_read_p (abfd
) ||
313 ((int)abfd
->format
< (int)bfd_unknown
) ||
314 ((int)abfd
->format
>= (int)bfd_type_end
)) {
315 bfd_error
= invalid_operation
;
319 if (abfd
->format
!= bfd_unknown
) return (abfd
->format
== format
) ? true:false;
321 /* presume the answer is yes */
322 abfd
->format
= format
;
324 filepos
= bfd_tell (abfd
);
326 if (!BFD_SEND_FMT (abfd
, _bfd_set_format
, (abfd
))) {
327 abfd
->format
= bfd_unknown
;
328 bfd_seek (abfd
, filepos
, SEEK_SET
);
335 /* Hack object and core file sections */
338 DEFUN(bfd_get_section_by_name
,(abfd
, name
),
344 for (sect
= abfd
->sections
; sect
!= NULL
; sect
= sect
->next
)
345 if (!strcmp (sect
->name
, name
)) return sect
;
349 /* If you try to create a section with a name which is already in use,
350 returns the old section by that name instead. */
352 DEFUN(bfd_make_section
,(abfd
, name
),
357 asection
** prev
= &abfd
->sections
;
358 asection
* sect
= abfd
->sections
;
360 if (abfd
->output_has_begun
) {
361 bfd_error
= invalid_operation
;
366 if (!strcmp(sect
->name
, name
)) return sect
;
371 newsect
= (asection
*) zalloc (sizeof (asection
));
372 if (newsect
== NULL
) {
373 bfd_error
= no_memory
;
377 newsect
->name
= name
;
378 newsect
->index
= abfd
->section_count
++;
379 newsect
->flags
= SEC_NO_FLAGS
;
381 #if ignore /* the compiler doesn't know that zalloc clears the storage */
382 newsect
->userdata
= 0;
383 newsect
->next
= (asection
*)NULL
;
384 newsect
->relocation
= (arelent
*)NULL
;
385 newsect
->reloc_count
= 0;
386 newsect
->line_filepos
=0;
388 if (BFD_SEND (abfd
, _new_section_hook
, (abfd
, newsect
)) != true) {
397 /* Call operation on each section. Operation gets three args: the bfd,
398 the section, and a void * pointer (whatever the user supplied). */
400 /* This is attractive except that without lexical closures its use is hard
401 to make reentrant. */
404 bfd_map_over_sections (abfd
, operation
, user_storage
)
412 for (sect
= abfd
->sections
; sect
!= NULL
; i
++, sect
= sect
->next
)
413 (*operation
) (abfd
, sect
, user_storage
);
415 if (i
!= abfd
->section_count
) /* Debugging */
420 bfd_set_section_flags (abfd
, section
, flags
)
425 if ((flags
& bfd_applicable_section_flags (abfd
)) != flags
) {
426 bfd_error
= invalid_operation
;
430 section
->flags
= flags
;
436 bfd_set_section_size (abfd
, ptr
, val
)
441 /* Once you've started writing to any section you cannot create or change
442 the size of any others. */
444 if (abfd
->output_has_begun
) {
445 bfd_error
= invalid_operation
;
455 bfd_set_section_contents (abfd
, section
, location
, offset
, count
)
462 if (!(bfd_get_section_flags(abfd
, section
) &
464 bfd_error
= no_contents
;
466 } /* if section has no contents */
468 if (BFD_SEND (abfd
, _bfd_set_section_contents
,
469 (abfd
, section
, location
, offset
, count
))) {
470 abfd
->output_has_begun
= true;
478 bfd_get_section_contents (abfd
, section
, location
, offset
, count
)
485 if (section
->flags
& SEC_CONSTRUCTOR
) {
486 memset(location
, 0, count
);
490 return (BFD_SEND (abfd
, _bfd_get_section_contents
,
491 (abfd
, section
, location
, offset
, count
)));
496 /** Some core file info commands */
498 /* Returns a read-only string explaining what program was running when
502 bfd_core_file_failing_command (abfd
)
505 if (abfd
->format
!= bfd_core
) {
506 bfd_error
= invalid_operation
;
509 return BFD_SEND (abfd
, _core_file_failing_command
, (abfd
));
513 bfd_core_file_failing_signal (abfd
)
516 if (abfd
->format
!= bfd_core
) {
517 bfd_error
= invalid_operation
;
520 return BFD_SEND (abfd
, _core_file_failing_signal
, (abfd
));
524 core_file_matches_executable_p (core_bfd
, exec_bfd
)
525 bfd
*core_bfd
, *exec_bfd
;
527 if ((core_bfd
->format
!= bfd_core
) || (exec_bfd
->format
!= bfd_object
)) {
528 bfd_error
= wrong_format
;
532 return BFD_SEND (core_bfd
, _core_file_matches_executable_p
, (core_bfd
, exec_bfd
));
538 bfd_set_symtab (abfd
, location
, symcount
)
541 unsigned int symcount
;
543 if ((abfd
->format
!= bfd_object
) || (bfd_read_p (abfd
))) {
544 bfd_error
= invalid_operation
;
548 bfd_get_outsymbols (abfd
) = location
;
549 bfd_get_symcount (abfd
) = symcount
;
553 /* returns the number of octets of storage required */
555 get_reloc_upper_bound (abfd
, asect
)
559 if (abfd
->format
!= bfd_object
) {
560 bfd_error
= invalid_operation
;
564 return BFD_SEND (abfd
, _get_reloc_upper_bound
, (abfd
, asect
));
568 bfd_canonicalize_reloc (abfd
, asect
, location
, symbols
)
574 if (abfd
->format
!= bfd_object
) {
575 bfd_error
= invalid_operation
;
579 return BFD_SEND (abfd
, _bfd_canonicalize_reloc
, (abfd
, asect
, location
, symbols
));
583 bfd_print_symbol_vandf(file
, symbol
)
587 flagword type
= symbol
->flags
;
588 if (symbol
->section
!= (asection
*)NULL
)
590 fprintf(file
,"%08lx ", symbol
->value
+symbol
->section
->vma
);
594 fprintf(file
,"%08lx ", symbol
->value
);
596 fprintf(file
,"%c%c%c%c%c%c%c",
597 (type
& BSF_LOCAL
) ? 'l':' ',
598 (type
& BSF_GLOBAL
) ? 'g' : ' ',
599 (type
& BSF_IMPORT
) ? 'i' : ' ',
600 (type
& BSF_EXPORT
) ? 'e' : ' ',
601 (type
& BSF_UNDEFINED
) ? 'u' : ' ',
602 (type
& BSF_FORT_COMM
) ? 'c' : ' ',
603 (type
& BSF_DEBUGGING
) ? 'd' :' ');
609 bfd_set_file_flags (abfd
, flags
)
613 if (abfd
->format
!= bfd_object
) {
614 bfd_error
= wrong_format
;
618 if (bfd_read_p (abfd
)) {
619 bfd_error
= invalid_operation
;
623 if ((flags
& bfd_applicable_file_flags (abfd
)) != flags
) {
624 bfd_error
= invalid_operation
;
628 bfd_get_file_flags (abfd
) = flags
;
634 bfd_set_reloc (ignore_abfd
, asect
, location
, count
)
640 asect
->orelocation
= location
;
641 asect
->reloc_count
= count
;
644 If an output_bfd is supplied to this function the generated image
645 will be relocatable, the relocations are copied to the output file
646 after they have been changed to reflect the new state of the world.
647 There are two ways of reflecting the results of partial linkage in an
648 output file; by modifying the output data in place, and by modifying
649 the relocation record. Some native formats (eg basic a.out and basic
650 coff) have no way of specifying an addend in the relocation type, so
651 the addend has to go in the output data. This is no big deal since in
652 these formats the output data slot will always be big enough for the
653 addend. Complex reloc types with addends were invented to solve just
657 bfd_reloc_status_enum_type
658 bfd_perform_relocation(abfd
,
664 arelent
*reloc_entry
;
666 asection
*input_section
;
670 bfd_reloc_status_enum_type flag
= bfd_reloc_ok
;
671 bfd_vma relocation_before
;
672 bfd_vma addr
= reloc_entry
->address
;
673 bfd_vma output_base
= 0;
674 CONST
struct rint_struct
*howto
= reloc_entry
->howto
;
675 asection
*reloc_target_output_section
;
676 asection
*reloc_target_input_section
;
679 if (reloc_entry
->sym_ptr_ptr
) {
680 symbol
= *( reloc_entry
->sym_ptr_ptr
);
681 if ((symbol
->flags
& BSF_UNDEFINED
) && output_bfd
== (bfd
*)NULL
) {
682 flag
= bfd_reloc_undefined
;
686 symbol
= (asymbol
*)NULL
;
689 if (howto
->special_function
){
690 bfd_reloc_status_enum_type cont
;
691 cont
= howto
->special_function(abfd
,
696 if (cont
!= bfd_reloc_continue
) return cont
;
700 Work out which section the relocation is targetted at and the
701 initial relocation command value.
705 if (symbol
!= (asymbol
*)NULL
){
706 if (symbol
->flags
& BSF_FORT_COMM
) {
710 relocation
= symbol
->value
;
712 if (symbol
->section
!= (asection
*)NULL
)
714 reloc_target_input_section
= symbol
->section
;
717 reloc_target_input_section
= (asection
*)NULL
;
720 else if (reloc_entry
->section
!= (asection
*)NULL
)
723 reloc_target_input_section
= reloc_entry
->section
;
727 reloc_target_input_section
= (asection
*)NULL
;
731 if (reloc_target_input_section
!= (asection
*)NULL
) {
733 reloc_target_output_section
=
734 reloc_target_input_section
->output_section
;
736 if (output_bfd
&& howto
->partial_inplace
==false) {
740 output_base
= reloc_target_output_section
->vma
;
744 relocation
+= output_base
+ reloc_target_input_section
->output_offset
;
747 relocation
+= reloc_entry
->addend
;
750 if(reloc_entry
->address
> (bfd_vma
)(input_section
->size
))
752 return bfd_reloc_outofrange
;
756 if (howto
->pc_relative
== true)
759 Anything which started out as pc relative should end up that
764 output_base
+ input_section
->output_offset
;
768 if (output_bfd
!= (bfd
*)NULL
&& howto
->partial_inplace
== false) {
770 This is a partial relocation, and we want to apply the relocation
771 to the reloc entry rather than the raw data. Modify the reloc
772 inplace to reflect what we now know.
774 reloc_entry
->addend
= relocation
;
775 reloc_entry
->section
= reloc_target_input_section
;
776 if (reloc_target_input_section
!= (asection
*)NULL
) {
777 /* If we know the output section we can forget the symbol */
778 reloc_entry
->sym_ptr_ptr
= (asymbol
**)NULL
;
780 reloc_entry
->address
+=
781 input_section
->output_offset
;
784 reloc_entry
->addend
= 0;
788 Either we are relocating all the way, or we don't want to apply
789 the relocation to the reloc entry (probably because there isn't
790 any room in the output format to describe addends to relocs)
792 relocation
>>= howto
->rightshift
;
794 /* Shift everything up to where it's going to be used */
796 relocation
<<= howto
->bitpos
;
799 /* Wait for the day when all have the mask in them */
803 relocation_before
= relocation
;
807 i instruction to be left alone
808 o offset within instruction
809 r relocation offset to apply
818 i i i i i o o o o o from bfd_get<size>
819 and S S S S S to get the size offset we want
820 + r r r r r r r r r r to get the final value to place
821 and D D D D D to chop to right size
822 -----------------------
825 i i i i i o o o o o from bfd_get<size>
826 and N N N N N get instruction
827 -----------------------
833 -----------------------
834 R R R R R R R R R R put into bfd_put<size>
838 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
844 char x
= bfd_getchar(abfd
, (char *)data
+ addr
);
846 bfd_putchar(abfd
,x
, (unsigned char *) data
+ addr
);
852 short x
= bfd_getshort(abfd
, (bfd_byte
*)data
+ addr
);
854 bfd_putshort(abfd
, x
, (unsigned char *)data
+ addr
);
859 long x
= bfd_getlong(abfd
, (bfd_byte
*) data
+ addr
);
861 bfd_putlong(abfd
,x
, (bfd_byte
*)data
+ addr
);
868 return bfd_reloc_other
;
875 bfd_assert(file
, line
)
879 printf("bfd assertion fail %s:%d\n",file
,line
);
884 bfd_set_start_address(abfd
, vma
)
888 abfd
->start_address
= vma
;
897 while ( (bfd_vma
)(1<< result
) < x
)
902 /* bfd_get_mtime: Return cached file modification time (e.g. as read
903 from archive header for archive members, or from file system if we have
904 been called before); else determine modify time, cache it, and
917 fp
= bfd_cache_lookup (abfd
);
918 if (0 != fstat (fileno (fp
), &buf
))
921 abfd
->mtime_set
= true;
922 abfd
->mtime
= buf
.st_mtime
;