1 /* ELF executable support for BFD.
2 Copyright 1993 Free Software Foundation, Inc.
4 This file is part of BFD, the Binary File Descriptor library.
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
28 #define INLINE __inline__
34 /* Standard ELF hash function. Do not change this function; you will
35 cause invalid hash tables to be generated. (Well, you would if this
36 were being used yet.) */
38 DEFUN (bfd_elf_hash
, (name
),
39 CONST
unsigned char *name
)
45 while ((ch
= *name
++) != '\0')
48 if ((g
= (h
& 0xf0000000)) != 0)
57 /* Read a specified number of bytes at a specified offset in an ELF
58 file, into a newly allocated buffer, and return a pointer to the
62 DEFUN (elf_read
, (abfd
, offset
, size
),
69 if ((buf
= bfd_alloc (abfd
, size
)) == NULL
)
71 bfd_error
= no_memory
;
74 if (bfd_seek (abfd
, offset
, SEEK_SET
) == -1)
76 bfd_error
= system_call_error
;
79 if (bfd_read ((PTR
) buf
, size
, 1, abfd
) != size
)
81 bfd_error
= system_call_error
;
88 DEFUN (elf_mkobject
, (abfd
), bfd
* abfd
)
90 /* this just does initialization */
91 /* coff_mkobject zalloc's space for tdata.coff_obj_data ... */
92 elf_tdata (abfd
) = (struct elf_obj_tdata
*)
93 bfd_zalloc (abfd
, sizeof (struct elf_obj_tdata
));
94 if (elf_tdata (abfd
) == 0)
96 bfd_error
= no_memory
;
99 /* since everything is done at close time, do we need any
106 DEFUN (elf_get_str_section
, (abfd
, shindex
),
108 unsigned int shindex
)
110 Elf_Internal_Shdr
**i_shdrp
;
111 char *shstrtab
= NULL
;
113 unsigned int shstrtabsize
;
115 i_shdrp
= elf_elfsections (abfd
);
116 if (i_shdrp
== 0 || i_shdrp
[shindex
] == 0)
119 shstrtab
= i_shdrp
[shindex
]->rawdata
;
120 if (shstrtab
== NULL
)
122 /* No cached one, attempt to read, and cache what we read. */
123 offset
= i_shdrp
[shindex
]->sh_offset
;
124 shstrtabsize
= i_shdrp
[shindex
]->sh_size
;
125 shstrtab
= elf_read (abfd
, offset
, shstrtabsize
);
126 i_shdrp
[shindex
]->rawdata
= (void *) shstrtab
;
132 DEFUN (elf_string_from_elf_section
, (abfd
, shindex
, strindex
),
134 unsigned int shindex AND
135 unsigned int strindex
)
137 Elf_Internal_Shdr
*hdr
;
142 hdr
= elf_elfsections (abfd
)[shindex
];
145 && elf_get_str_section (abfd
, shindex
) == NULL
)
148 return ((char *) hdr
->rawdata
) + strindex
;
156 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
159 Helper functions for GDB to locate the string tables.
160 Since BFD hides string tables from callers, GDB needs to use an
161 internal hook to find them. Sun's .stabstr, in particular,
162 isn't even pointed to by the .stab section, so ordinary
163 mechanisms wouldn't work to find it, even if we had some.
166 struct elf_internal_shdr
*
167 DEFUN (bfd_elf_find_section
, (abfd
, name
),
171 Elf_Internal_Shdr
**i_shdrp
;
176 i_shdrp
= elf_elfsections (abfd
);
179 shstrtab
= elf_get_str_section (abfd
, elf_elfheader (abfd
)->e_shstrndx
);
180 if (shstrtab
!= NULL
)
182 max
= elf_elfheader (abfd
)->e_shnum
;
183 for (i
= 1; i
< max
; i
++)
184 if (!strcmp (&shstrtab
[i_shdrp
[i
]->sh_name
], name
))
191 const struct bfd_elf_arch_map bfd_elf_arch_map
[] = {
192 { bfd_arch_sparc
, EM_SPARC
},
193 { bfd_arch_i386
, EM_386
},
194 { bfd_arch_m68k
, EM_68K
},
195 { bfd_arch_m88k
, EM_88K
},
196 { bfd_arch_i860
, EM_860
},
197 { bfd_arch_mips
, EM_MIPS
},
198 { bfd_arch_hppa
, EM_HPPA
},
201 const int bfd_elf_arch_map_size
= sizeof (bfd_elf_arch_map
) / sizeof (bfd_elf_arch_map
[0]);
203 const char *const bfd_elf_section_type_names
[] = {
204 "SHT_NULL", "SHT_PROGBITS", "SHT_SYMTAB", "SHT_STRTAB",
205 "SHT_RELA", "SHT_HASH", "SHT_DYNAMIC", "SHT_NOTE",
206 "SHT_NOBITS", "SHT_REL", "SHT_SHLIB", "SHT_DYNSYM",
209 /* ELF relocs are against symbols. If we are producing relocateable
210 output, and the reloc is against an external symbol, and nothing
211 has given us any additional addend, the resulting reloc will also
212 be against the same symbol. In such a case, we don't want to
213 change anything about the way the reloc is handled, since it will
214 all be done at final link time. Rather than put special case code
215 into bfd_perform_relocation, all the reloc types use this howto
216 function. It just short circuits the reloc if producing
217 relocateable output against an external symbol. */
219 bfd_reloc_status_type
220 bfd_elf_generic_reloc (abfd
,
227 arelent
*reloc_entry
;
230 asection
*input_section
;
233 if (output_bfd
!= (bfd
*) NULL
234 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
235 && reloc_entry
->addend
== 0)
237 reloc_entry
->address
+= input_section
->output_offset
;
241 return bfd_reloc_continue
;