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.)
42 o - Most functions return nonzero on success (check doc for
43 precise semantics); 0 or NULL on error.
44 o - Internal errors are documented by the value of bfd_error.
45 If that is system_call_error then check errno.
46 o - The easiest way to report this to the user is to use bfd_perror.
49 bfd_ec bfd_error = no_error;
51 char *bfd_errmsgs[] = {"No error",
54 "File in wrong format",
59 "No more archived files",
62 "File format not recognized",
63 "File format is ambiguous",
64 "Section has no contents",
65 "#<Invalid error code>"
68 #if !defined(ANSI_LIBRARIES)
74 extern char *sys_errlist[];
76 return (((code < 0) || (code >= sys_nerr)) ? "(unknown error)" :
79 #endif /* not ANSI_LIBRARIES */
82 bfd_errmsg (error_tag)
87 if (error_tag == system_call_error)
88 return strerror (errno);
90 if ((((int)error_tag <(int) no_error) ||
91 ((int)error_tag > (int)invalid_error_code)))
92 error_tag = invalid_error_code;/* sanity check */
94 return bfd_errmsgs [(int)error_tag];
101 if (bfd_error == system_call_error)
102 perror(message); /* must be system error then... */
104 if (message == NULL || *message == '\0')
105 fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
107 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
111 /* for error messages */
113 bfd_format_string (format)
116 if (((int)format <(int) bfd_unknown) || ((int)format >=(int) bfd_type_end)) return "invalid";
119 case bfd_object: return "object"; /* linker/assember/compiler output */
120 case bfd_archive: return "archive"; /* object archive file */
121 case bfd_core: return "core"; /* core dump */
122 default: return "unknown";
126 /** Target configurations */
128 extern bfd_target *target_vector[];
130 /* Returns a pointer to the transfer vector for the object target
131 named target_name. If target_name is NULL, chooses the one in the
132 environment variable GNUTARGET; if that is null or not defined then
133 the first entry in the target list is chosen. Passing in the
134 string "default" or setting the environment variable to "default"
135 will cause the first entry in the target list to be returned. */
138 bfd_find_target (target_name)
142 extern char *getenv ();
143 char *targname = (target_name ? target_name : getenv ("GNUTARGET"));
145 /* This is safe; the vector cannot be null */
146 if (targname == NULL || !strcmp (targname, "default"))
147 return target_vector[0];
149 for (target = &target_vector[0]; *target != NULL; target++) {
150 if (!strcmp (targname, (*target)->name))
154 bfd_error = invalid_target;
158 /* Returns a freshly-consed, NULL-terminated vector of the names of all the
159 valid bfd targets. Do not modify the names */
166 char **name_list, **name_ptr;
168 for (target = &target_vector[0]; *target != NULL; target++)
171 name_ptr = name_list = (char **) zalloc ((vec_length + 1) * sizeof (char **));
173 if (name_list == NULL) {
174 bfd_error = no_memory;
178 for (target = &target_vector[0]; *target != NULL; target++)
179 *(name_ptr++) = (*target)->name;
184 /** Init a bfd for read of the proper format.
187 /* We should be able to find out if the target was defaulted or user-specified.
188 If the user specified the target explicitly then we should do no search.
189 I guess the best way to do this is to pass an extra argument which specifies
192 /* I have chanegd this always to set the filepos to the origin before
193 guessing. -- Gumby, 14 Februar 1991*/
196 bfd_check_format (abfd, format)
203 bfd_target **target, *save_targ, *right_targ;
206 if (!bfd_read_p (abfd) ||
207 ((int)(abfd->format) < (int)bfd_unknown) ||
208 ((int)(abfd->format) >= (int)bfd_type_end)) {
209 bfd_error = invalid_operation;
213 if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
215 /* presume the answer is yes */
216 abfd->format = format;
219 filepos = bfd_tell (abfd);
221 bfd_seek (abfd, (file_ptr)0, SEEK_SET); /* instead, rewind! */
224 right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
226 abfd->xvec = right_targ; /* Set the target as returned */
227 return true; /* File position has moved, BTW */
230 /* This isn't a <format> file in the specified or defaulted target type.
231 See if we recognize it for any other target type. (We check them
232 all to make sure it's uniquely recognized.) */
234 save_targ = abfd->xvec;
238 for (target = target_vector; *target != NULL; target++) {
241 abfd->xvec = *target; /* Change BFD's target temporarily */
243 bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
245 bfd_seek (abfd, (file_ptr)0, SEEK_SET);
246 temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
247 if (temp) { /* This format checks out as ok! */
253 if (match_count == 1) {
254 abfd->xvec = right_targ; /* Change BFD's target permanently */
255 return true; /* File position has moved, BTW */
258 abfd->xvec = save_targ; /* Restore original target type */
259 abfd->format = bfd_unknown; /* Restore original format */
260 bfd_error = ((match_count == 0) ? file_not_recognized :
261 file_ambiguously_recognized);
263 bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
269 bfd_set_format (abfd, format)
275 if (bfd_read_p (abfd) ||
276 ((int)abfd->format < (int)bfd_unknown) ||
277 ((int)abfd->format >= (int)bfd_type_end)) {
278 bfd_error = invalid_operation;
282 if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
284 /* presume the answer is yes */
285 abfd->format = format;
287 filepos = bfd_tell (abfd);
289 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
290 abfd->format = bfd_unknown;
291 bfd_seek (abfd, filepos, SEEK_SET);
298 /* Hack object and core file sections */
301 bfd_get_section_by_name (abfd, name)
307 for (sect = abfd->sections; sect != NULL; sect = sect->next)
308 if (!strcmp (sect->name, name)) return sect;
312 /* If you try to create a section with a name which is already in use,
313 returns the old section by that name instead. */
315 bfd_make_section (abfd, name)
320 asection ** prev = &abfd->sections;
321 asection * sect = abfd->sections;
323 if (abfd->output_has_begun) {
324 bfd_error = invalid_operation;
329 if (!strcmp(sect->name, name)) return sect;
334 newsect = (asection *) zalloc (sizeof (asection));
335 if (newsect == NULL) {
336 bfd_error = no_memory;
340 newsect->name = name;
341 newsect->index = abfd->section_count++;
342 newsect->flags = SEC_NO_FLAGS;
344 #if ignore /* the compiler doesn't know that zalloc clears the storage */
345 newsect->userdata = 0;
346 newsect->next = (asection *)NULL;
347 newsect->relocation = (arelent *)NULL;
348 newsect->reloc_count = 0;
349 newsect->line_filepos =0;
351 if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
360 /* Call operation on each section. Operation gets three args: the bfd,
361 the section, and a void * pointer (whatever the user supplied). */
363 /* This is attractive except that without lexical closures its use is hard
364 to make reentrant. */
367 bfd_map_over_sections (abfd, operation, user_storage)
375 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
376 (*operation) (abfd, sect, user_storage);
378 if (i != abfd->section_count) /* Debugging */
383 bfd_set_section_flags (abfd, section, flags)
388 if ((flags & bfd_applicable_section_flags (abfd)) != flags) {
389 bfd_error = invalid_operation;
393 section->flags = flags;
399 bfd_set_section_size (abfd, ptr, val)
404 /* Once you've started writing to any section you cannot create or change
405 the size of any others. */
407 if (abfd->output_has_begun) {
408 bfd_error = invalid_operation;
418 bfd_set_section_contents (abfd, section, location, offset, count)
425 if (!(bfd_get_section_flags(abfd, section) &
427 bfd_error = no_contents;
429 } /* if section has no contents */
431 if (BFD_SEND (abfd, _bfd_set_section_contents,
432 (abfd, section, location, offset, count))) {
433 abfd->output_has_begun = true;
441 bfd_get_section_contents (abfd, section, location, offset, count)
448 if (section->flags & SEC_CONSTRUCTOR) {
449 memset(location, 0, count);
453 return (BFD_SEND (abfd, _bfd_get_section_contents,
454 (abfd, section, location, offset, count)));
459 /** Some core file info commands */
461 /* Returns a read-only string explaining what program was running when
465 bfd_core_file_failing_command (abfd)
468 if (abfd->format != bfd_core) {
469 bfd_error = invalid_operation;
472 return BFD_SEND (abfd, _core_file_failing_command, (abfd));
476 bfd_core_file_failing_signal (abfd)
479 if (abfd->format != bfd_core) {
480 bfd_error = invalid_operation;
483 return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
487 core_file_matches_executable_p (core_bfd, exec_bfd)
488 bfd *core_bfd, *exec_bfd;
490 if ((core_bfd->format != bfd_core) || (exec_bfd->format != bfd_object)) {
491 bfd_error = wrong_format;
495 return BFD_SEND (core_bfd, _core_file_matches_executable_p, (core_bfd, exec_bfd));
501 bfd_set_symtab (abfd, location, symcount)
504 unsigned int symcount;
506 if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
507 bfd_error = invalid_operation;
511 bfd_get_outsymbols (abfd) = location;
512 bfd_get_symcount (abfd) = symcount;
516 /* returns the number of octets of storage required */
518 get_reloc_upper_bound (abfd, asect)
522 if (abfd->format != bfd_object) {
523 bfd_error = invalid_operation;
527 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
531 bfd_canonicalize_reloc (abfd, asect, location, symbols)
537 if (abfd->format != bfd_object) {
538 bfd_error = invalid_operation;
542 return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
546 bfd_print_symbol_vandf(file, symbol)
550 flagword type = symbol->flags;
551 if (symbol->section != (asection *)NULL)
553 fprintf(file,"%08lx ", symbol->value+symbol->section->vma);
557 fprintf(file,"%08lx ", symbol->value);
559 fprintf(file,"%c%c%c%c%c%c%c",
560 (type & BSF_LOCAL) ? 'l':' ',
561 (type & BSF_GLOBAL) ? 'g' : ' ',
562 (type & BSF_IMPORT) ? 'i' : ' ',
563 (type & BSF_EXPORT) ? 'e' : ' ',
564 (type & BSF_UNDEFINED) ? 'u' : ' ',
565 (type & BSF_FORT_COMM) ? 'c' : ' ',
566 (type & BSF_DEBUGGING) ? 'd' :' ');
572 bfd_set_file_flags (abfd, flags)
576 if (abfd->format != bfd_object) {
577 bfd_error = wrong_format;
581 if (bfd_read_p (abfd)) {
582 bfd_error = invalid_operation;
586 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
587 bfd_error = invalid_operation;
591 bfd_get_file_flags (abfd) = flags;
597 bfd_set_reloc (ignore_abfd, asect, location, count)
603 asect->orelocation = location;
604 asect->reloc_count = count;
607 If an output_bfd is supplied to this function the generated image
608 will be relocatable, the relocations are copied to the output file
609 after they have been changed to reflect the new state of the world.
610 There are two ways of reflecting the results of partial linkage in an
611 output file; by modifying the output data in place, and by modifying
612 the relocation record. Some native formats (eg basic a.out and basic
613 coff) have no way of specifying an addend in the relocation type, so
614 the addend has to go in the output data. This is no big deal since in
615 these formats the output data slot will always be big enough for the
616 addend. Complex reloc types with addends were invented to solve just
620 bfd_reloc_status_enum_type
621 bfd_perform_relocation(abfd,
627 arelent *reloc_entry;
629 asection *input_section;
633 bfd_reloc_status_enum_type flag = bfd_reloc_ok;
634 bfd_vma relocation_before;
637 bfd_vma addr = reloc_entry->address ;
638 bfd_vma output_base = 0;
639 struct rint_struct *howto = reloc_entry->howto;
640 asection *reloc_target_output_section;
641 asection *reloc_target_input_section;
644 if (reloc_entry->sym_ptr_ptr) {
645 symbol = *( reloc_entry->sym_ptr_ptr);
646 if ((symbol->flags & BSF_UNDEFINED) && output_bfd == (bfd *)NULL) {
647 flag = bfd_reloc_undefined;
651 symbol = (asymbol*)NULL;
654 if (howto->special_function) {
655 bfd_reloc_status_enum_type cont;
656 cont = howto->special_function(abfd,
661 if (cont != bfd_reloc_continue) return cont;
665 Work out which section the relocation is targetted at and the
666 initial relocation command value.
670 if (symbol != (asymbol *)NULL){
671 if (symbol->flags & BSF_FORT_COMM) {
675 relocation = symbol->value;
677 if (symbol->section != (asection *)NULL)
679 reloc_target_input_section = symbol->section;
682 reloc_target_input_section = (asection *)NULL;
685 else if (reloc_entry->section != (asection *)NULL)
688 reloc_target_input_section = reloc_entry->section;
692 reloc_target_input_section = (asection *)NULL;
696 if (reloc_target_input_section != (asection *)NULL) {
698 reloc_target_output_section =
699 reloc_target_input_section->output_section;
701 if (output_bfd && howto->partial_inplace==false) {
705 output_base = reloc_target_output_section->vma;
709 relocation += output_base + reloc_target_input_section->output_offset;
712 relocation += reloc_entry->addend ;
715 if(reloc_entry->address > (bfd_vma)(input_section->size))
717 return bfd_reloc_outofrange;
721 if (howto->pc_relative == true)
724 Anything which started out as pc relative should end up that
729 output_base + input_section->output_offset;
733 if (output_bfd!= (bfd *)NULL && howto->partial_inplace == false) {
735 This is a partial relocation, and we want to apply the relocation
736 to the reloc entry rather than the raw data. Modify the reloc
737 inplace to reflect what we now know.
739 reloc_entry->addend = relocation ;
740 reloc_entry->section = reloc_target_input_section;
741 if (reloc_target_input_section != (asection *)NULL) {
742 /* If we know the output section we can forget the symbol */
743 reloc_entry->sym_ptr_ptr = (asymbol**)NULL;
745 reloc_entry->address +=
746 input_section->output_offset;
749 reloc_entry->addend = 0;
753 Either we are relocating all the way, or we don't want to apply
754 the relocation to the reloc entry (probably because there isn't
755 any room in the output format to describe addends to relocs)
757 relocation >>= howto->rightshift;
758 if (howto->bitsize == 32) {
762 mask = (1L << howto->bitsize) - 1 ;
763 mask |= mask - 1; /* FIXME, what is this? */
768 /* Shift everything up to where it's going to be used */
770 relocation <<= howto->bitpos;
771 mask <<= howto->bitpos;
774 /* Wait for the day when all have the mask in them */
776 BFD_ASSERT(howto->mask == mask);
780 relocation_before = relocation;
787 char x = bfd_getchar(abfd, (char *)data + addr);
788 relocation += x & mask;
790 ( x & target_mask) | ( relocation & mask),
791 (unsigned char *) data + addr);
797 short x = bfd_getshort(abfd, (bfd_byte *)data + addr);
798 relocation += x & mask;
799 bfd_putshort(abfd, ( x & target_mask) | (relocation & mask),
800 (unsigned char *)data + addr);
805 long x = bfd_getlong(abfd, (bfd_byte *) data + addr);
806 relocation += x & mask;
807 bfd_putlong(abfd, ( x & target_mask) | (relocation & mask),
808 (bfd_byte *)data + addr);
815 return bfd_reloc_other;
818 /* See if important parts of the relocation were chopped to make
819 it fit into the relocation field. (ie are there any significant
820 bits left over after the masking ? */
821 if ((relocation_before & target_mask) != 0 &&
822 howto->complain_on_overflow == true)
824 /* Its ok if the bit which fell off is */
825 return bfd_reloc_overflow;
833 bfd_assert(file, line)
837 printf("bfd assertion fail %s:%d\n",file,line);
842 bfd_set_start_address(abfd, vma)
846 abfd->start_address = vma;
855 while ( (bfd_vma)(1<< result) < x)
860 /* bfd_get_mtime: Return cached file modification time (e.g. as read
861 from archive header for archive members, or from file system if we have
862 been called before); else determine modify time, cache it, and
875 fp = bfd_cache_lookup (abfd);
876 if (0 != fstat (fileno (fp), &buf))
879 abfd->mtime_set = true;
880 abfd->mtime = buf.st_mtime;