* elfcode.h (map_program_segments): Restore check of file_size !=
[deliverable/binutils-gdb.git] / bfd / elf.c
1 /* ELF executable support for BFD.
2 Copyright 1993 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
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.
10
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.
15
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. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #define ARCH_SIZE 0
24 #include "libelf.h"
25
26 #ifndef INLINE
27 #if __GNUC__ >= 2
28 #define INLINE __inline__
29 #else
30 #define INLINE
31 #endif
32 #endif
33
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.) */
37 unsigned long
38 DEFUN (bfd_elf_hash, (name),
39 CONST unsigned char *name)
40 {
41 unsigned long h = 0;
42 unsigned long g;
43 int ch;
44
45 while ((ch = *name++) != '\0')
46 {
47 h = (h << 4) + ch;
48 if ((g = (h & 0xf0000000)) != 0)
49 {
50 h ^= g >> 24;
51 h &= ~g;
52 }
53 }
54 return h;
55 }
56
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
59 buffer. */
60
61 static char *
62 DEFUN (elf_read, (abfd, offset, size),
63 bfd * abfd AND
64 long offset AND
65 int size)
66 {
67 char *buf;
68
69 if ((buf = bfd_alloc (abfd, size)) == NULL)
70 {
71 bfd_error = no_memory;
72 return NULL;
73 }
74 if (bfd_seek (abfd, offset, SEEK_SET) == -1)
75 {
76 bfd_error = system_call_error;
77 return NULL;
78 }
79 if (bfd_read ((PTR) buf, size, 1, abfd) != size)
80 {
81 bfd_error = system_call_error;
82 return NULL;
83 }
84 return buf;
85 }
86
87 boolean
88 DEFUN (elf_mkobject, (abfd), bfd * abfd)
89 {
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)
95 {
96 bfd_error = no_memory;
97 return false;
98 }
99 /* since everything is done at close time, do we need any
100 initialization? */
101
102 return true;
103 }
104
105 char *
106 DEFUN (elf_get_str_section, (abfd, shindex),
107 bfd * abfd AND
108 unsigned int shindex)
109 {
110 Elf_Internal_Shdr **i_shdrp;
111 char *shstrtab = NULL;
112 unsigned int offset;
113 unsigned int shstrtabsize;
114
115 i_shdrp = elf_elfsections (abfd);
116 if (i_shdrp == 0 || i_shdrp[shindex] == 0)
117 return 0;
118
119 shstrtab = i_shdrp[shindex]->rawdata;
120 if (shstrtab == NULL)
121 {
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;
127 }
128 return shstrtab;
129 }
130
131 char *
132 DEFUN (elf_string_from_elf_section, (abfd, shindex, strindex),
133 bfd * abfd AND
134 unsigned int shindex AND
135 unsigned int strindex)
136 {
137 Elf_Internal_Shdr *hdr;
138
139 if (strindex == 0)
140 return "";
141
142 hdr = elf_elfsections (abfd)[shindex];
143
144 if (!hdr->rawdata
145 && elf_get_str_section (abfd, shindex) == NULL)
146 return NULL;
147
148 return ((char *) hdr->rawdata) + strindex;
149 }
150
151 /*
152 INTERNAL_FUNCTION
153 bfd_elf_find_section
154
155 SYNOPSIS
156 struct elf_internal_shdr *bfd_elf_find_section (bfd *abfd, char *name);
157
158 DESCRIPTION
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.
164 */
165
166 struct elf_internal_shdr *
167 DEFUN (bfd_elf_find_section, (abfd, name),
168 bfd * abfd AND
169 char *name)
170 {
171 Elf_Internal_Shdr **i_shdrp;
172 char *shstrtab;
173 unsigned int max;
174 unsigned int i;
175
176 i_shdrp = elf_elfsections (abfd);
177 if (i_shdrp != NULL)
178 {
179 shstrtab = elf_get_str_section (abfd, elf_elfheader (abfd)->e_shstrndx);
180 if (shstrtab != NULL)
181 {
182 max = elf_elfheader (abfd)->e_shnum;
183 for (i = 1; i < max; i++)
184 if (!strcmp (&shstrtab[i_shdrp[i]->sh_name], name))
185 return i_shdrp[i];
186 }
187 }
188 return 0;
189 }
190
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 },
199 };
200
201 const int bfd_elf_arch_map_size = sizeof (bfd_elf_arch_map) / sizeof (bfd_elf_arch_map[0]);
202
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",
207 };
208
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. */
218
219 bfd_reloc_status_type
220 bfd_elf_generic_reloc (abfd,
221 reloc_entry,
222 symbol,
223 data,
224 input_section,
225 output_bfd)
226 bfd *abfd;
227 arelent *reloc_entry;
228 asymbol *symbol;
229 PTR data;
230 asection *input_section;
231 bfd *output_bfd;
232 {
233 if (output_bfd != (bfd *) NULL
234 && (symbol->flags & BSF_SECTION_SYM) == 0
235 && reloc_entry->addend == 0)
236 {
237 reloc_entry->address += input_section->output_offset;
238 return bfd_reloc_ok;
239 }
240
241 return bfd_reloc_continue;
242 }
This page took 0.040075 seconds and 4 git commands to generate.