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, format %s can't represent section %s\n",
83 bfd_error_vector_type bfd_error_vector =
85 bfd_nonrepresentable_section
88 #if !defined(ANSI_LIBRARIES)
94 extern char *sys_errlist[];
96 return (((code < 0) || (code >= sys_nerr)) ? "(unknown error)" :
99 #endif /* not ANSI_LIBRARIES */
104 bfd_errmsg (error_tag)
109 if (error_tag == system_call_error)
110 return strerror (errno);
112 if ((((int)error_tag <(int) no_error) ||
113 ((int)error_tag > (int)invalid_error_code)))
114 error_tag = invalid_error_code;/* sanity check */
116 return bfd_errmsgs [(int)error_tag];
120 void bfd_default_error_trap(error_tag)
123 printf("bfd assert fail (%s)\n", bfd_errmsg(error_tag));
126 void (*bfd_error_trap)() = bfd_default_error_trap;
127 void (*bfd_error_nonrepresentabltrap)() = bfd_default_error_trap;
129 DEFUN(bfd_perror,(message),
132 if (bfd_error == system_call_error)
133 perror(message); /* must be system error then... */
135 if (message == NULL || *message == '\0')
136 fprintf (stderr, "%s\n", bfd_errmsg (bfd_error));
138 fprintf (stderr, "%s: %s\n", message, bfd_errmsg (bfd_error));
142 /* for error messages */
144 bfd_format_string (format)
147 if (((int)format <(int) bfd_unknown) || ((int)format >=(int) bfd_type_end)) return "invalid";
150 case bfd_object: return "object"; /* linker/assember/compiler output */
151 case bfd_archive: return "archive"; /* object archive file */
152 case bfd_core: return "core"; /* core dump */
153 default: return "unknown";
157 /** Target configurations */
159 extern bfd_target *target_vector[];
161 /* Returns a pointer to the transfer vector for the object target
162 named target_name. If target_name is NULL, chooses the one in the
163 environment variable GNUTARGET; if that is null or not defined then
164 the first entry in the target list is chosen. Passing in the
165 string "default" or setting the environment variable to "default"
166 will cause the first entry in the target list to be returned. */
169 DEFUN(bfd_find_target,(target_name),
170 CONST char *target_name)
173 extern char *getenv ();
174 CONST char *targname = (target_name ? target_name : getenv ("GNUTARGET"));
176 /* This is safe; the vector cannot be null */
177 if (targname == NULL || !strcmp (targname, "default"))
178 return target_vector[0];
180 for (target = &target_vector[0]; *target != NULL; target++) {
181 if (!strcmp (targname, (*target)->name))
185 bfd_error = invalid_target;
189 /* Returns a freshly-consed, NULL-terminated vector of the names of all the
190 valid bfd targets. Do not modify the names */
197 char **name_list, **name_ptr;
199 for (target = &target_vector[0]; *target != NULL; target++)
202 name_ptr = name_list = (char **) zalloc ((vec_length + 1) * sizeof (char **));
204 if (name_list == NULL) {
205 bfd_error = no_memory;
209 for (target = &target_vector[0]; *target != NULL; target++)
210 *(name_ptr++) = (*target)->name;
215 /** Init a bfd for read of the proper format.
218 /* We should be able to find out if the target was defaulted or user-specified.
219 If the user specified the target explicitly then we should do no search.
220 I guess the best way to do this is to pass an extra argument which specifies
223 /* I have chanegd this always to set the filepos to the origin before
224 guessing. -- Gumby, 14 Februar 1991*/
227 bfd_check_format (abfd, format)
234 bfd_target **target, *save_targ, *right_targ;
237 if (!bfd_read_p (abfd) ||
238 ((int)(abfd->format) < (int)bfd_unknown) ||
239 ((int)(abfd->format) >= (int)bfd_type_end)) {
240 bfd_error = invalid_operation;
244 if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
246 /* presume the answer is yes */
247 abfd->format = format;
250 filepos = bfd_tell (abfd);
252 bfd_seek (abfd, (file_ptr)0, SEEK_SET); /* instead, rewind! */
255 right_targ = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
257 abfd->xvec = right_targ; /* Set the target as returned */
258 return true; /* File position has moved, BTW */
261 /* This isn't a <format> file in the specified or defaulted target type.
262 See if we recognize it for any other target type. (We check them
263 all to make sure it's uniquely recognized.) */
265 save_targ = abfd->xvec;
269 for (target = target_vector; *target != NULL; target++) {
272 abfd->xvec = *target; /* Change BFD's target temporarily */
274 bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
276 bfd_seek (abfd, (file_ptr)0, SEEK_SET);
277 temp = BFD_SEND_FMT (abfd, _bfd_check_format, (abfd));
278 if (temp) { /* This format checks out as ok! */
282 /* Big- and little-endian b.out archives look the same, but it doesn't
283 * matter: there is no difference in their headers, and member file byte
284 * orders will (I hope) be handled appropriately by bfd. Ditto for big
285 * and little coff archives. And the 4 coff/b.out object formats are
286 * unambiguous. So accept the first match we find.
293 if (match_count == 1) {
294 abfd->xvec = right_targ; /* Change BFD's target permanently */
295 return true; /* File position has moved, BTW */
298 abfd->xvec = save_targ; /* Restore original target type */
299 abfd->format = bfd_unknown; /* Restore original format */
300 bfd_error = ((match_count == 0) ? file_not_recognized :
301 file_ambiguously_recognized);
303 bfd_seek (abfd, filepos, SEEK_SET); /* Restore original file position */
309 bfd_set_format (abfd, format)
315 if (bfd_read_p (abfd) ||
316 ((int)abfd->format < (int)bfd_unknown) ||
317 ((int)abfd->format >= (int)bfd_type_end)) {
318 bfd_error = invalid_operation;
322 if (abfd->format != bfd_unknown) return (abfd->format == format) ? true:false;
324 /* presume the answer is yes */
325 abfd->format = format;
327 filepos = bfd_tell (abfd);
329 if (!BFD_SEND_FMT (abfd, _bfd_set_format, (abfd))) {
330 abfd->format = bfd_unknown;
331 bfd_seek (abfd, filepos, SEEK_SET);
338 /* Hack object and core file sections */
341 DEFUN(bfd_get_section_by_name,(abfd, name),
347 for (sect = abfd->sections; sect != NULL; sect = sect->next)
348 if (!strcmp (sect->name, name)) return sect;
352 /* If you try to create a section with a name which is already in use,
353 returns the old section by that name instead. */
355 DEFUN(bfd_make_section,(abfd, name),
360 asection ** prev = &abfd->sections;
361 asection * sect = abfd->sections;
363 if (abfd->output_has_begun) {
364 bfd_error = invalid_operation;
369 if (!strcmp(sect->name, name)) return sect;
374 newsect = (asection *) zalloc (sizeof (asection));
375 if (newsect == NULL) {
376 bfd_error = no_memory;
380 newsect->name = name;
381 newsect->index = abfd->section_count++;
382 newsect->flags = SEC_NO_FLAGS;
384 #if ignore /* the compiler doesn't know that zalloc clears the storage */
385 newsect->userdata = 0;
386 newsect->next = (asection *)NULL;
387 newsect->relocation = (arelent *)NULL;
388 newsect->reloc_count = 0;
389 newsect->line_filepos =0;
391 if (BFD_SEND (abfd, _new_section_hook, (abfd, newsect)) != true) {
400 /* Call operation on each section. Operation gets three args: the bfd,
401 the section, and a void * pointer (whatever the user supplied). */
403 /* This is attractive except that without lexical closures its use is hard
404 to make reentrant. */
407 bfd_map_over_sections (abfd, operation, user_storage)
415 for (sect = abfd->sections; sect != NULL; i++, sect = sect->next)
416 (*operation) (abfd, sect, user_storage);
418 if (i != abfd->section_count) /* Debugging */
423 bfd_set_section_flags (abfd, section, flags)
428 if ((flags & bfd_applicable_section_flags (abfd)) != flags) {
429 bfd_error = invalid_operation;
433 section->flags = flags;
439 bfd_set_section_size (abfd, ptr, val)
444 /* Once you've started writing to any section you cannot create or change
445 the size of any others. */
447 if (abfd->output_has_begun) {
448 bfd_error = invalid_operation;
458 bfd_set_section_contents (abfd, section, location, offset, count)
465 if (!(bfd_get_section_flags(abfd, section) &
467 bfd_error = no_contents;
469 } /* if section has no contents */
471 if (BFD_SEND (abfd, _bfd_set_section_contents,
472 (abfd, section, location, offset, count))) {
473 abfd->output_has_begun = true;
481 bfd_get_section_contents (abfd, section, location, offset, count)
488 if (section->flags & SEC_CONSTRUCTOR) {
489 memset(location, 0, count);
493 return (BFD_SEND (abfd, _bfd_get_section_contents,
494 (abfd, section, location, offset, count)));
499 /** Some core file info commands */
501 /* Returns a read-only string explaining what program was running when
505 bfd_core_file_failing_command (abfd)
508 if (abfd->format != bfd_core) {
509 bfd_error = invalid_operation;
512 return BFD_SEND (abfd, _core_file_failing_command, (abfd));
516 bfd_core_file_failing_signal (abfd)
519 if (abfd->format != bfd_core) {
520 bfd_error = invalid_operation;
523 return BFD_SEND (abfd, _core_file_failing_signal, (abfd));
527 core_file_matches_executable_p (core_bfd, exec_bfd)
528 bfd *core_bfd, *exec_bfd;
530 if ((core_bfd->format != bfd_core) || (exec_bfd->format != bfd_object)) {
531 bfd_error = wrong_format;
535 return BFD_SEND (core_bfd, _core_file_matches_executable_p, (core_bfd, exec_bfd));
541 bfd_set_symtab (abfd, location, symcount)
544 unsigned int symcount;
546 if ((abfd->format != bfd_object) || (bfd_read_p (abfd))) {
547 bfd_error = invalid_operation;
551 bfd_get_outsymbols (abfd) = location;
552 bfd_get_symcount (abfd) = symcount;
556 /* returns the number of octets of storage required */
558 get_reloc_upper_bound (abfd, asect)
562 if (abfd->format != bfd_object) {
563 bfd_error = invalid_operation;
567 return BFD_SEND (abfd, _get_reloc_upper_bound, (abfd, asect));
571 bfd_canonicalize_reloc (abfd, asect, location, symbols)
577 if (abfd->format != bfd_object) {
578 bfd_error = invalid_operation;
582 return BFD_SEND (abfd, _bfd_canonicalize_reloc, (abfd, asect, location, symbols));
586 bfd_print_symbol_vandf(file, symbol)
590 flagword type = symbol->flags;
591 if (symbol->section != (asection *)NULL)
593 fprintf(file,"%08lx ", symbol->value+symbol->section->vma);
597 fprintf(file,"%08lx ", symbol->value);
599 fprintf(file,"%c%c%c%c%c%c%c",
600 (type & BSF_LOCAL) ? 'l':' ',
601 (type & BSF_GLOBAL) ? 'g' : ' ',
602 (type & BSF_IMPORT) ? 'i' : ' ',
603 (type & BSF_EXPORT) ? 'e' : ' ',
604 (type & BSF_UNDEFINED) ? 'u' : ' ',
605 (type & BSF_FORT_COMM) ? 'c' : ' ',
606 (type & BSF_DEBUGGING) ? 'd' :' ');
612 bfd_set_file_flags (abfd, flags)
616 if (abfd->format != bfd_object) {
617 bfd_error = wrong_format;
621 if (bfd_read_p (abfd)) {
622 bfd_error = invalid_operation;
626 if ((flags & bfd_applicable_file_flags (abfd)) != flags) {
627 bfd_error = invalid_operation;
631 bfd_get_file_flags (abfd) = flags;
637 bfd_set_reloc (ignore_abfd, asect, location, count)
643 asect->orelocation = location;
644 asect->reloc_count = count;
647 If an output_bfd is supplied to this function the generated image
648 will be relocatable, the relocations are copied to the output file
649 after they have been changed to reflect the new state of the world.
650 There are two ways of reflecting the results of partial linkage in an
651 output file; by modifying the output data in place, and by modifying
652 the relocation record. Some native formats (eg basic a.out and basic
653 coff) have no way of specifying an addend in the relocation type, so
654 the addend has to go in the output data. This is no big deal since in
655 these formats the output data slot will always be big enough for the
656 addend. Complex reloc types with addends were invented to solve just
660 bfd_reloc_status_enum_type
661 bfd_perform_relocation(abfd,
667 arelent *reloc_entry;
669 asection *input_section;
673 bfd_reloc_status_enum_type flag = bfd_reloc_ok;
674 bfd_vma relocation_before;
675 bfd_vma addr = reloc_entry->address ;
676 bfd_vma output_base = 0;
677 CONST struct rint_struct *howto = reloc_entry->howto;
678 asection *reloc_target_output_section;
679 asection *reloc_target_input_section;
682 if (reloc_entry->sym_ptr_ptr) {
683 symbol = *( reloc_entry->sym_ptr_ptr);
684 if ((symbol->flags & BSF_UNDEFINED) && output_bfd == (bfd *)NULL) {
685 flag = bfd_reloc_undefined;
689 symbol = (asymbol*)NULL;
692 if (howto->special_function){
693 bfd_reloc_status_enum_type cont;
694 cont = howto->special_function(abfd,
699 if (cont != bfd_reloc_continue) return cont;
703 Work out which section the relocation is targetted at and the
704 initial relocation command value.
708 if (symbol != (asymbol *)NULL){
709 if (symbol->flags & BSF_FORT_COMM) {
713 relocation = symbol->value;
715 if (symbol->section != (asection *)NULL)
717 reloc_target_input_section = symbol->section;
720 reloc_target_input_section = (asection *)NULL;
723 else if (reloc_entry->section != (asection *)NULL)
726 reloc_target_input_section = reloc_entry->section;
730 reloc_target_input_section = (asection *)NULL;
734 if (reloc_target_input_section != (asection *)NULL) {
736 reloc_target_output_section =
737 reloc_target_input_section->output_section;
739 if (output_bfd && howto->partial_inplace==false) {
743 output_base = reloc_target_output_section->vma;
747 relocation += output_base + reloc_target_input_section->output_offset;
750 relocation += reloc_entry->addend ;
753 if(reloc_entry->address > (bfd_vma)(input_section->size))
755 return bfd_reloc_outofrange;
759 if (howto->pc_relative == true)
762 Anything which started out as pc relative should end up that
767 output_base + input_section->output_offset;
771 if (output_bfd!= (bfd *)NULL) {
772 if ( howto->partial_inplace == false) {
774 This is a partial relocation, and we want to apply the relocation
775 to the reloc entry rather than the raw data. Modify the reloc
776 inplace to reflect what we now know.
778 reloc_entry->addend = relocation ;
779 reloc_entry->section = reloc_target_input_section;
780 if (reloc_target_input_section != (asection *)NULL) {
781 /* If we know the output section we can forget the symbol */
782 reloc_entry->sym_ptr_ptr = (asymbol**)NULL;
784 reloc_entry->address +=
785 input_section->output_offset;
790 /* This is a partial relocation, but inplace, so modify the
797 reloc_entry->addend = 0;
801 Either we are relocating all the way, or we don't want to apply
802 the relocation to the reloc entry (probably because there isn't
803 any room in the output format to describe addends to relocs)
805 relocation >>= howto->rightshift;
807 /* Shift everything up to where it's going to be used */
809 relocation <<= howto->bitpos;
812 /* Wait for the day when all have the mask in them */
816 relocation_before = relocation;
820 i instruction to be left alone
821 o offset within instruction
822 r relocation offset to apply
831 i i i i i o o o o o from bfd_get<size>
832 and S S S S S to get the size offset we want
833 + r r r r r r r r r r to get the final value to place
834 and D D D D D to chop to right size
835 -----------------------
838 ... i i i i i o o o o o from bfd_get<size>
839 and N N N N N get instruction
840 -----------------------
846 -----------------------
847 R R R R R R R R R R put into bfd_put<size>
851 x = ( (x & ~howto->dst_mask) | (((x & howto->src_mask) + relocation) & howto->dst_mask))
857 char x = bfd_getchar(abfd, (char *)data + addr);
859 bfd_putchar(abfd,x, (unsigned char *) data + addr);
865 short x = bfd_getshort(abfd, (bfd_byte *)data + addr);
867 bfd_putshort(abfd, x, (unsigned char *)data + addr);
872 long x = bfd_getlong(abfd, (bfd_byte *) data + addr);
874 bfd_putlong(abfd,x, (bfd_byte *)data + addr);
881 return bfd_reloc_other;
888 bfd_assert(file, line)
892 printf("bfd assertion fail %s:%d\n",file,line);
897 bfd_set_start_address(abfd, vma)
901 abfd->start_address = vma;
910 while ( (bfd_vma)(1<< result) < x)
915 /* bfd_get_mtime: Return cached file modification time (e.g. as read
916 from archive header for archive members, or from file system if we have
917 been called before); else determine modify time, cache it, and
930 fp = bfd_cache_lookup (abfd);
931 if (0 != fstat (fileno (fp), &buf))
934 abfd->mtime_set = true;
935 abfd->mtime = buf.st_mtime;