Commit | Line | Data |
---|---|---|
e4b6b3e7 ILT |
1 | /* Intel 80386/80486-specific support for 32-bit ELF |
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 | ||
e4b6b3e7 ILT |
20 | #include "bfd.h" |
21 | #include "sysdep.h" | |
013dec1a | 22 | #include "bfdlink.h" |
e4b6b3e7 ILT |
23 | #include "libbfd.h" |
24 | #include "libelf.h" | |
25 | ||
013dec1a ILT |
26 | static CONST struct reloc_howto_struct *elf_i386_reloc_type_lookup |
27 | PARAMS ((bfd *, bfd_reloc_code_real_type)); | |
28 | static void elf_i386_info_to_howto | |
29 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *)); | |
30 | static void elf_i386_info_to_howto_rel | |
31 | PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *)); | |
32 | static boolean elf_i386_create_dynamic_sections | |
33 | PARAMS ((bfd *, struct bfd_link_info *)); | |
34 | static boolean elf_i386_adjust_dynamic_symbol | |
35 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
36 | static boolean elf_i386_allocate_dynamic_section | |
37 | PARAMS ((bfd *, const char *)); | |
38 | static boolean elf_i386_size_dynamic_sections | |
39 | PARAMS ((bfd *, struct bfd_link_info *)); | |
40 | static boolean elf_i386_relocate_section | |
41 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
7c6da9ca | 42 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **, char *)); |
013dec1a ILT |
43 | static boolean elf_i386_finish_dynamic_symbol |
44 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
45 | Elf_Internal_Sym *)); | |
46 | static boolean elf_i386_finish_dynamic_sections | |
47 | PARAMS ((bfd *, struct bfd_link_info *)); | |
48 | ||
e4b6b3e7 ILT |
49 | #define USE_REL 1 /* 386 uses REL relocations instead of RELA */ |
50 | ||
51 | enum reloc_type | |
52 | { | |
53 | R_386_NONE = 0, | |
68241b2b ILT |
54 | R_386_32, |
55 | R_386_PC32, | |
56 | R_386_GOT32, | |
57 | R_386_PLT32, | |
e4b6b3e7 | 58 | R_386_COPY, |
68241b2b ILT |
59 | R_386_GLOB_DAT, |
60 | R_386_JUMP_SLOT, | |
e4b6b3e7 | 61 | R_386_RELATIVE, |
68241b2b ILT |
62 | R_386_GOTOFF, |
63 | R_386_GOTPC, | |
e4b6b3e7 ILT |
64 | R_386_max |
65 | }; | |
66 | ||
67 | #if 0 | |
68 | static CONST char *CONST reloc_type_names[] = | |
69 | { | |
70 | "R_386_NONE", | |
68241b2b ILT |
71 | "R_386_32", |
72 | "R_386_PC32", | |
73 | "R_386_GOT32", | |
74 | "R_386_PLT32", | |
e4b6b3e7 | 75 | "R_386_COPY", |
68241b2b ILT |
76 | "R_386_GLOB_DAT", |
77 | "R_386_JUMP_SLOT", | |
e4b6b3e7 | 78 | "R_386_RELATIVE", |
68241b2b ILT |
79 | "R_386_GOTOFF", |
80 | "R_386_GOTPC", | |
e4b6b3e7 ILT |
81 | }; |
82 | #endif | |
83 | ||
e4b6b3e7 ILT |
84 | static reloc_howto_type elf_howto_table[]= |
85 | { | |
68241b2b ILT |
86 | HOWTO(R_386_NONE, 0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE", true,0x00000000,0x00000000,false), |
87 | HOWTO(R_386_32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32", true,0xffffffff,0xffffffff,false), | |
88 | HOWTO(R_386_PC32, 0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32", true,0xffffffff,0xffffffff,true), | |
89 | HOWTO(R_386_GOT32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32", true,0xffffffff,0xffffffff,false), | |
90 | HOWTO(R_386_PLT32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32", true,0xffffffff,0xffffffff,false), | |
91 | HOWTO(R_386_COPY, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY", true,0xffffffff,0xffffffff,false), | |
92 | HOWTO(R_386_GLOB_DAT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GLOB_DAT", true,0xffffffff,0xffffffff,false), | |
93 | HOWTO(R_386_JUMP_SLOT, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_JUMP_SLOT",true,0xffffffff,0xffffffff,false), | |
94 | HOWTO(R_386_RELATIVE, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false), | |
95 | HOWTO(R_386_GOTOFF, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false), | |
96 | HOWTO(R_386_GOTPC, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC", true,0xffffffff,0xffffffff,false), | |
e4b6b3e7 ILT |
97 | }; |
98 | ||
99 | #ifdef DEBUG_GEN_RELOC | |
100 | #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str) | |
101 | #else | |
102 | #define TRACE(str) | |
103 | #endif | |
104 | ||
105 | static CONST struct reloc_howto_struct * | |
013dec1a ILT |
106 | elf_i386_reloc_type_lookup (abfd, code) |
107 | bfd *abfd; | |
108 | bfd_reloc_code_real_type code; | |
e4b6b3e7 ILT |
109 | { |
110 | switch (code) | |
111 | { | |
112 | case BFD_RELOC_NONE: | |
113 | TRACE ("BFD_RELOC_NONE"); | |
114 | return &elf_howto_table[ (int)R_386_NONE ]; | |
115 | ||
116 | case BFD_RELOC_32: | |
117 | TRACE ("BFD_RELOC_32"); | |
118 | return &elf_howto_table[ (int)R_386_32 ]; | |
119 | ||
120 | case BFD_RELOC_32_PCREL: | |
121 | TRACE ("BFD_RELOC_PC32"); | |
122 | return &elf_howto_table[ (int)R_386_PC32 ]; | |
123 | ||
68241b2b ILT |
124 | case BFD_RELOC_386_GOT32: |
125 | TRACE ("BFD_RELOC_386_GOT32"); | |
126 | return &elf_howto_table[ (int)R_386_GOT32 ]; | |
127 | ||
128 | case BFD_RELOC_386_PLT32: | |
129 | TRACE ("BFD_RELOC_386_PLT32"); | |
130 | return &elf_howto_table[ (int)R_386_PLT32 ]; | |
131 | ||
132 | case BFD_RELOC_386_COPY: | |
133 | TRACE ("BFD_RELOC_386_COPY"); | |
134 | return &elf_howto_table[ (int)R_386_COPY ]; | |
135 | ||
136 | case BFD_RELOC_386_GLOB_DAT: | |
137 | TRACE ("BFD_RELOC_386_GLOB_DAT"); | |
138 | return &elf_howto_table[ (int)R_386_GLOB_DAT ]; | |
139 | ||
140 | case BFD_RELOC_386_JUMP_SLOT: | |
141 | TRACE ("BFD_RELOC_386_JUMP_SLOT"); | |
142 | return &elf_howto_table[ (int)R_386_JUMP_SLOT ]; | |
143 | ||
144 | case BFD_RELOC_386_RELATIVE: | |
145 | TRACE ("BFD_RELOC_386_RELATIVE"); | |
146 | return &elf_howto_table[ (int)R_386_RELATIVE ]; | |
147 | ||
148 | case BFD_RELOC_386_GOTOFF: | |
149 | TRACE ("BFD_RELOC_386_GOTOFF"); | |
150 | return &elf_howto_table[ (int)R_386_GOTOFF ]; | |
151 | ||
152 | case BFD_RELOC_386_GOTPC: | |
153 | TRACE ("BFD_RELOC_386_GOTPC"); | |
154 | return &elf_howto_table[ (int)R_386_GOTPC ]; | |
155 | ||
e4b6b3e7 | 156 | default: |
68241b2b | 157 | break; |
e4b6b3e7 ILT |
158 | } |
159 | ||
160 | TRACE ("Unknown"); | |
161 | return 0; | |
162 | } | |
163 | ||
164 | static void | |
013dec1a ILT |
165 | elf_i386_info_to_howto (abfd, cache_ptr, dst) |
166 | bfd *abfd; | |
167 | arelent *cache_ptr; | |
168 | Elf32_Internal_Rela *dst; | |
e4b6b3e7 | 169 | { |
68241b2b | 170 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max); |
e4b6b3e7 ILT |
171 | |
172 | cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
173 | } | |
174 | ||
175 | static void | |
013dec1a ILT |
176 | elf_i386_info_to_howto_rel (abfd, cache_ptr, dst) |
177 | bfd *abfd; | |
178 | arelent *cache_ptr; | |
179 | Elf32_Internal_Rel *dst; | |
e4b6b3e7 | 180 | { |
68241b2b | 181 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max); |
e4b6b3e7 ILT |
182 | |
183 | cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
184 | } | |
013dec1a ILT |
185 | \f |
186 | /* Functions for the i386 ELF linker. */ | |
187 | ||
188 | /* The name of the dynamic interpreter. This is put in the .interp | |
189 | section. */ | |
190 | ||
191 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
192 | ||
193 | /* The size in bytes of an entry in the procedure linkage table. */ | |
194 | ||
195 | #define PLT_ENTRY_SIZE 16 | |
196 | ||
197 | /* The first entry in an absolute procedure linkage table looks like | |
198 | this. See the SVR4 ABI i386 supplement to see how this works. */ | |
199 | ||
200 | static bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] = | |
201 | { | |
202 | 0xff, 0x35, /* pushl contents of address */ | |
203 | 0, 0, 0, 0, /* replaced with address of .got + 4. */ | |
204 | 0xff, 0x25, /* jmp indirect */ | |
205 | 0, 0, 0, 0, /* replaced with address of .got + 8. */ | |
206 | 0, 0, 0, 0 /* pad out to 16 bytes. */ | |
207 | }; | |
208 | ||
209 | /* Subsequent entries in an absolute procedure linkage table look like | |
210 | this. */ | |
211 | ||
212 | static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] = | |
213 | { | |
214 | 0xff, 0x25, /* jmp indirect */ | |
215 | 0, 0, 0, 0, /* replaced with address of this symbol in .got. */ | |
216 | 0x68, /* pushl immediate */ | |
217 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
218 | 0xe9, /* jmp relative */ | |
219 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
220 | }; | |
221 | ||
222 | /* Create dynamic sections when linking against a dynamic object. */ | |
223 | ||
224 | static boolean | |
225 | elf_i386_create_dynamic_sections (abfd, info) | |
226 | bfd *abfd; | |
227 | struct bfd_link_info *info; | |
228 | { | |
229 | flagword flags; | |
230 | register asection *s; | |
231 | struct elf_link_hash_entry *h; | |
232 | ||
233 | /* We need to create .plt, .rel.plt, .got, .dynbss, and .rel.bss | |
234 | sections. */ | |
235 | ||
236 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
237 | ||
238 | s = bfd_make_section (abfd, ".plt"); | |
239 | if (s == NULL | |
240 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY | SEC_CODE) | |
241 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
242 | return false; | |
243 | ||
244 | s = bfd_make_section (abfd, ".rel.plt"); | |
245 | if (s == NULL | |
246 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
247 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
248 | return false; | |
249 | ||
250 | s = bfd_make_section (abfd, ".got"); | |
251 | if (s == NULL | |
252 | || ! bfd_set_section_flags (abfd, s, flags) | |
253 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
254 | return false; | |
255 | ||
256 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got | |
257 | section. We don't do this in the linker script because we don't | |
258 | want to define the symbol if we are not creating a global offset | |
259 | table. */ | |
260 | h = NULL; | |
261 | if (! (_bfd_generic_link_add_one_symbol | |
262 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0, | |
263 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
264 | (struct bfd_link_hash_entry **) &h))) | |
265 | return false; | |
266 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
267 | ||
268 | /* The first three global offset table entries are reserved. */ | |
269 | s->_raw_size += 3 * 4; | |
270 | ||
271 | /* The .dynbss section is a place to put symbols which are defined | |
272 | by dynamic objects, are referenced by regular objects, and are | |
273 | not functions. We must allocate space for them in the process | |
274 | image and use a R_386_COPY reloc to tell the dynamic linker to | |
275 | initialize them at run time. The linker script puts the .dynbss | |
276 | section into the .bss section of the final image. */ | |
277 | s = bfd_make_section (abfd, ".dynbss"); | |
278 | if (s == NULL | |
279 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
280 | return false; | |
281 | ||
282 | /* The .rel.bss section holds copy relocs. This section is not | |
283 | normally needed. We need to create it here, though, so that the | |
284 | linker will map it to an output section. If it turns out not to | |
285 | be needed, we can discard it later. */ | |
286 | s = bfd_make_section (abfd, ".rel.bss"); | |
287 | if (s == NULL | |
288 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
289 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
290 | return false; | |
291 | ||
292 | return true; | |
293 | } | |
294 | ||
295 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
296 | regular object. The current definition is in some section of the | |
297 | dynamic object, but we're not including those sections. We have to | |
298 | change the definition to something the rest of the link can | |
299 | understand. */ | |
300 | ||
301 | static boolean | |
302 | elf_i386_adjust_dynamic_symbol (info, h) | |
303 | struct bfd_link_info *info; | |
304 | struct elf_link_hash_entry *h; | |
305 | { | |
306 | bfd *dynobj; | |
307 | asection *s; | |
308 | unsigned int power_of_two; | |
013dec1a ILT |
309 | |
310 | dynobj = elf_hash_table (info)->dynobj; | |
311 | ||
312 | /* Make sure we know what is going on here. */ | |
313 | BFD_ASSERT (dynobj != NULL | |
314 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
315 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
316 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
317 | && h->root.type == bfd_link_hash_defined | |
318 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
319 | == bfd_target_elf_flavour) | |
320 | && (elf_elfheader (h->root.u.def.section->owner)->e_type | |
321 | == ET_DYN) | |
322 | && h->root.u.def.section->output_section == NULL); | |
323 | ||
324 | /* If this is a function, put it in the procedure linkage table. We | |
325 | will fill in the contents of the procedure linkage table later, | |
326 | when we know the address of the .got section. */ | |
327 | if (h->type == STT_FUNC) | |
328 | { | |
329 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
330 | BFD_ASSERT (s != NULL); | |
331 | ||
332 | /* If this is the first .plt entry, make room for the special | |
333 | first entry. */ | |
334 | if (s->_raw_size == 0) | |
335 | s->_raw_size += PLT_ENTRY_SIZE; | |
336 | ||
337 | /* Set the symbol to this location in the .plt. */ | |
338 | h->root.u.def.section = s; | |
339 | h->root.u.def.value = s->_raw_size; | |
340 | ||
341 | /* Make room for this entry. */ | |
342 | s->_raw_size += PLT_ENTRY_SIZE; | |
343 | ||
344 | /* We also need to make an entry in the .got section. */ | |
345 | ||
346 | s = bfd_get_section_by_name (dynobj, ".got"); | |
347 | BFD_ASSERT (s != NULL); | |
348 | s->_raw_size += 4; | |
349 | ||
350 | /* We also need to make an entry in the .rel.plt section. */ | |
351 | ||
352 | s = bfd_get_section_by_name (dynobj, ".rel.plt"); | |
353 | BFD_ASSERT (s != NULL); | |
354 | s->_raw_size += sizeof (Elf32_External_Rel); | |
355 | ||
356 | return true; | |
357 | } | |
358 | ||
359 | /* If this is a weak symbol, and there is a real definition, the | |
360 | processor independent code will have arranged for us to see the | |
361 | real definition first, and we can just use the same value. */ | |
362 | if (h->weakdef != NULL) | |
363 | { | |
364 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined); | |
365 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
366 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
7c6da9ca | 367 | h->copy_offset = (bfd_vma) -1; |
013dec1a ILT |
368 | return true; |
369 | } | |
370 | ||
371 | /* This is a reference to a symbol defined by a dynamic object which | |
372 | is not a function. We must allocate it in our .dynbss section, | |
373 | which will become part of the .bss section of the executable. | |
374 | There will be an entry for this symbol in the .dynsym section. | |
375 | The dynamic object will contain position independent code, so all | |
376 | references from the dynamic object to this symbol will go through | |
377 | the global offset table. The dynamic linker will use the .dynsym | |
378 | entry to determine the address it must put in the global offset | |
379 | table, so both the dynamic object and the regular object will | |
380 | refer to the same memory location for the variable. */ | |
381 | ||
382 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
383 | BFD_ASSERT (s != NULL); | |
384 | ||
385 | /* If the symbol is currently defined in the .bss section of the | |
386 | dynamic object, then it is OK to simply initialize it to zero. | |
387 | If the symbol is in some other section, we must generate a | |
388 | R_386_COPY reloc to tell the dynamic linker to copy the initial | |
389 | value out of the dynamic object and into the runtime process | |
390 | image. We need to remember the offset into the .rel.bss section | |
7c6da9ca | 391 | we are going to use. */ |
013dec1a | 392 | if ((h->root.u.def.section->flags & SEC_LOAD) == 0) |
7c6da9ca | 393 | h->copy_offset = (bfd_vma) -1; |
013dec1a ILT |
394 | else |
395 | { | |
396 | asection *srel; | |
397 | ||
398 | srel = bfd_get_section_by_name (dynobj, ".rel.bss"); | |
399 | BFD_ASSERT (srel != NULL); | |
7c6da9ca | 400 | h->copy_offset = srel->_raw_size; |
013dec1a ILT |
401 | srel->_raw_size += sizeof (Elf32_External_Rel); |
402 | } | |
403 | ||
404 | /* We need to figure out the alignment required for this symbol. I | |
405 | have no idea how ELF linkers handle this. */ | |
7c6da9ca ILT |
406 | power_of_two = bfd_log2 (h->size); |
407 | if (power_of_two > 3) | |
408 | power_of_two = 3; | |
013dec1a ILT |
409 | |
410 | /* Apply the required alignment. */ | |
7c6da9ca ILT |
411 | s->_raw_size = BFD_ALIGN (s->_raw_size, |
412 | (bfd_size_type) (1 << power_of_two)); | |
013dec1a ILT |
413 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
414 | { | |
415 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
416 | return false; | |
417 | } | |
418 | ||
419 | /* Define the symbol as being at this point in the section. */ | |
420 | h->root.u.def.section = s; | |
421 | h->root.u.def.value = s->_raw_size; | |
422 | ||
423 | /* Increment the section size to make room for the symbol. */ | |
424 | s->_raw_size += h->size; | |
425 | ||
426 | return true; | |
427 | } | |
428 | ||
429 | /* Allocate contents for a section. */ | |
430 | ||
431 | static INLINE boolean | |
432 | elf_i386_allocate_dynamic_section (dynobj, name) | |
433 | bfd *dynobj; | |
434 | const char *name; | |
435 | { | |
436 | register asection *s; | |
437 | ||
438 | s = bfd_get_section_by_name (dynobj, name); | |
439 | BFD_ASSERT (s != NULL); | |
440 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
441 | if (s->contents == NULL && s->_raw_size != 0) | |
442 | { | |
443 | bfd_set_error (bfd_error_no_memory); | |
444 | return false; | |
445 | } | |
446 | return true; | |
447 | } | |
448 | ||
449 | /* Set the sizes of the dynamic sections. */ | |
450 | ||
451 | static boolean | |
452 | elf_i386_size_dynamic_sections (output_bfd, info) | |
453 | bfd *output_bfd; | |
454 | struct bfd_link_info *info; | |
455 | { | |
456 | bfd *dynobj; | |
457 | asection *s; | |
458 | ||
459 | dynobj = elf_hash_table (info)->dynobj; | |
460 | BFD_ASSERT (dynobj != NULL); | |
461 | ||
462 | /* Set the contents of the .interp section to the interpreter. */ | |
8af74670 ILT |
463 | if (! info->shared) |
464 | { | |
465 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
466 | BFD_ASSERT (s != NULL); | |
467 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
468 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
469 | } | |
013dec1a ILT |
470 | |
471 | /* The adjust_dynamic_symbol entry point has determined the sizes of | |
472 | the various dynamic sections. Allocate some memory for them to | |
473 | hold contents. */ | |
474 | if (! elf_i386_allocate_dynamic_section (dynobj, ".plt") | |
475 | || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.plt") | |
476 | || ! elf_i386_allocate_dynamic_section (dynobj, ".got") | |
477 | || ! elf_i386_allocate_dynamic_section (dynobj, ".rel.bss")) | |
478 | return false; | |
479 | ||
480 | /* Add some entries to the .dynamic section. We fill in the values | |
481 | later, in elf_i386_finish_dynamic_sections, but we must add the | |
482 | entries now so that we get the correct size for the .dynamic | |
8af74670 ILT |
483 | section. The DT_DEBUG entry is filled in by the dynamic linker |
484 | and used by the debugger. */ | |
485 | if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0) | |
486 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0)) | |
013dec1a ILT |
487 | return false; |
488 | ||
489 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
490 | BFD_ASSERT (s != NULL); | |
491 | if (s->_raw_size != 0) | |
492 | { | |
493 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
494 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL) | |
495 | || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) | |
496 | return false; | |
497 | } | |
498 | ||
499 | /* If we didn't need the .rel.bss section, then discard it from the | |
500 | output file. This is a hack. We don't bother to do it for the | |
501 | other sections because they normally are needed. */ | |
502 | s = bfd_get_section_by_name (dynobj, ".rel.bss"); | |
503 | BFD_ASSERT (s != NULL); | |
504 | if (s->_raw_size == 0) | |
505 | { | |
506 | asection **spp; | |
507 | ||
508 | for (spp = &s->output_section->owner->sections; | |
509 | *spp != s->output_section; | |
510 | spp = &(*spp)->next) | |
511 | ; | |
512 | *spp = s->output_section->next; | |
513 | --s->output_section->owner->section_count; | |
514 | } | |
515 | else | |
516 | { | |
517 | if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0) | |
518 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0) | |
519 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT, | |
520 | sizeof (Elf32_External_Rel))) | |
521 | return false; | |
522 | } | |
523 | ||
524 | return true; | |
525 | } | |
526 | ||
527 | /* Relocate an i386 ELF section. */ | |
528 | ||
529 | static boolean | |
530 | elf_i386_relocate_section (output_bfd, info, input_bfd, input_section, | |
7c6da9ca ILT |
531 | contents, relocs, local_syms, local_sections, |
532 | output_names) | |
013dec1a ILT |
533 | bfd *output_bfd; |
534 | struct bfd_link_info *info; | |
535 | bfd *input_bfd; | |
536 | asection *input_section; | |
537 | bfd_byte *contents; | |
538 | Elf_Internal_Rela *relocs; | |
539 | Elf_Internal_Sym *local_syms; | |
540 | asection **local_sections; | |
7c6da9ca | 541 | char *output_names; |
013dec1a ILT |
542 | { |
543 | Elf_Internal_Shdr *symtab_hdr; | |
544 | Elf_Internal_Rela *rel; | |
545 | Elf_Internal_Rela *relend; | |
546 | ||
547 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; | |
548 | ||
549 | rel = relocs; | |
550 | relend = relocs + input_section->reloc_count; | |
551 | for (; rel < relend; rel++) | |
552 | { | |
553 | int r_type; | |
554 | const reloc_howto_type *howto; | |
555 | long r_symndx; | |
556 | struct elf_link_hash_entry *h; | |
557 | Elf_Internal_Sym *sym; | |
558 | asection *sec; | |
559 | bfd_vma relocation; | |
560 | bfd_reloc_status_type r; | |
561 | ||
562 | r_type = ELF32_R_TYPE (rel->r_info); | |
563 | if (r_type < 0 || r_type >= (int) R_386_max) | |
564 | { | |
565 | bfd_set_error (bfd_error_bad_value); | |
566 | return false; | |
567 | } | |
568 | howto = elf_howto_table + r_type; | |
569 | ||
570 | r_symndx = ELF32_R_SYM (rel->r_info); | |
571 | ||
572 | if (info->relocateable) | |
573 | { | |
574 | /* This is a relocateable link. We don't have to change | |
575 | anything, unless the reloc is against a section symbol, | |
576 | in which case we have to adjust according to where the | |
577 | section symbol winds up in the output section. */ | |
578 | if (r_symndx < symtab_hdr->sh_info) | |
579 | { | |
580 | sym = local_syms + r_symndx; | |
581 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
582 | { | |
583 | bfd_vma val; | |
584 | ||
585 | sec = local_sections[r_symndx]; | |
586 | val = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
587 | val += sec->output_offset + sym->st_value; | |
588 | bfd_put_32 (input_bfd, val, contents + rel->r_offset); | |
589 | } | |
590 | } | |
591 | ||
592 | continue; | |
593 | } | |
594 | ||
595 | /* This is a final link. */ | |
596 | h = NULL; | |
597 | sym = NULL; | |
598 | sec = NULL; | |
599 | if (r_symndx < symtab_hdr->sh_info) | |
600 | { | |
601 | sym = local_syms + r_symndx; | |
602 | sec = local_sections[r_symndx]; | |
603 | relocation = (sec->output_section->vma | |
604 | + sec->output_offset | |
605 | + sym->st_value); | |
606 | } | |
607 | else | |
608 | { | |
609 | long indx; | |
610 | ||
611 | indx = r_symndx - symtab_hdr->sh_info; | |
612 | h = elf_sym_hashes (input_bfd)[indx]; | |
613 | if (h->root.type == bfd_link_hash_defined) | |
614 | { | |
615 | sec = h->root.u.def.section; | |
616 | relocation = (h->root.u.def.value | |
617 | + sec->output_section->vma | |
618 | + sec->output_offset); | |
619 | } | |
620 | else if (h->root.type == bfd_link_hash_weak) | |
621 | relocation = 0; | |
622 | else | |
623 | { | |
624 | if (! ((*info->callbacks->undefined_symbol) | |
625 | (info, h->root.root.string, input_bfd, | |
626 | input_section, rel->r_offset))) | |
627 | return false; | |
628 | relocation = 0; | |
629 | } | |
630 | } | |
631 | ||
632 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, | |
633 | contents, rel->r_offset, | |
634 | relocation, (bfd_vma) 0); | |
635 | ||
636 | if (r != bfd_reloc_ok) | |
637 | { | |
638 | switch (r) | |
639 | { | |
640 | default: | |
641 | case bfd_reloc_outofrange: | |
642 | abort (); | |
643 | case bfd_reloc_overflow: | |
644 | { | |
645 | const char *name; | |
646 | ||
647 | if (h != NULL) | |
648 | name = h->root.root.string; | |
649 | else | |
650 | { | |
7c6da9ca | 651 | name = output_names + sym->st_name; |
013dec1a ILT |
652 | if (name == NULL) |
653 | return false; | |
654 | if (*name == '\0') | |
655 | name = bfd_section_name (input_bfd, sec); | |
656 | } | |
657 | if (! ((*info->callbacks->reloc_overflow) | |
658 | (info, name, howto->name, (bfd_vma) 0, | |
659 | input_bfd, input_section, rel->r_offset))) | |
660 | return false; | |
661 | } | |
662 | break; | |
663 | } | |
664 | } | |
665 | } | |
666 | ||
667 | return true; | |
668 | } | |
669 | ||
670 | /* Finish up dynamic symbol handling. We set the contents of various | |
671 | dynamic sections here. */ | |
672 | ||
673 | static boolean | |
674 | elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym) | |
675 | bfd *output_bfd; | |
676 | struct bfd_link_info *info; | |
677 | struct elf_link_hash_entry *h; | |
678 | Elf_Internal_Sym *sym; | |
679 | { | |
680 | /* If this symbol is not defined by a dynamic object, or is not | |
681 | referenced by a regular object, ignore it. */ | |
682 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0 | |
683 | || (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) == 0 | |
684 | || (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) == 0) | |
685 | { | |
686 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ | |
687 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
688 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
689 | sym->st_shndx = SHN_ABS; | |
690 | return true; | |
691 | } | |
692 | ||
693 | BFD_ASSERT (h->root.type == bfd_link_hash_defined); | |
694 | BFD_ASSERT (h->dynindx != -1); | |
695 | ||
696 | if (h->type == STT_FUNC) | |
697 | { | |
698 | asection *splt; | |
699 | asection *sgot; | |
700 | asection *srel; | |
701 | bfd_vma plt_index; | |
702 | bfd_vma got_offset; | |
703 | Elf_Internal_Rel rel; | |
704 | ||
705 | splt = h->root.u.def.section; | |
706 | BFD_ASSERT (strcmp (bfd_get_section_name (splt->owner, splt), ".plt") | |
707 | == 0); | |
708 | sgot = bfd_get_section_by_name (splt->owner, ".got"); | |
709 | BFD_ASSERT (sgot != NULL); | |
710 | srel = bfd_get_section_by_name (splt->owner, ".rel.plt"); | |
711 | BFD_ASSERT (srel != NULL); | |
712 | ||
713 | /* FIXME: This only handles an absolute procedure linkage table. | |
714 | When producing a dynamic object, we need to generate a | |
715 | position independent procedure linkage table. */ | |
716 | ||
717 | /* Get the index in the procedure linkage table which | |
718 | corresponds to this symbol. This is the index of this symbol | |
719 | in all the symbols for which we are making plt entries. The | |
720 | first entry in the procedure linkage table is reserved. */ | |
721 | plt_index = h->root.u.def.value / PLT_ENTRY_SIZE - 1; | |
722 | ||
723 | /* Get the offset into the .got table of the entry that | |
724 | corresponds to this function. Each .got entry is 4 bytes. | |
725 | The first three are reserved. */ | |
726 | got_offset = (plt_index + 3) * 4; | |
727 | ||
728 | /* Fill in the entry in the procedure linkage table. */ | |
729 | memcpy (splt->contents + h->root.u.def.value, elf_i386_plt_entry, | |
730 | PLT_ENTRY_SIZE); | |
731 | bfd_put_32 (output_bfd, | |
732 | (sgot->output_section->vma | |
733 | + sgot->output_offset | |
734 | + got_offset), | |
735 | splt->contents + h->root.u.def.value + 2); | |
736 | bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel), | |
737 | splt->contents + h->root.u.def.value + 7); | |
738 | bfd_put_32 (output_bfd, - (h->root.u.def.value + PLT_ENTRY_SIZE), | |
739 | splt->contents + h->root.u.def.value + 12); | |
740 | ||
741 | /* Fill in the entry in the global offset table. */ | |
742 | bfd_put_32 (output_bfd, | |
743 | (splt->output_section->vma | |
744 | + splt->output_offset | |
745 | + h->root.u.def.value | |
746 | + 6), | |
747 | sgot->contents + got_offset); | |
748 | ||
749 | /* Fill in the entry in the .rel.plt section. */ | |
750 | rel.r_offset = (sgot->output_section->vma | |
751 | + sgot->output_offset | |
752 | + got_offset); | |
753 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT); | |
754 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
755 | ((Elf32_External_Rel *) srel->contents | |
756 | + plt_index)); | |
757 | ||
758 | /* Mark the symbol as undefined, rather than as defined in the | |
759 | .plt section. Leave the value alone. */ | |
760 | sym->st_shndx = SHN_UNDEF; | |
761 | } | |
762 | else | |
763 | { | |
764 | /* This is not a function. We have already allocated memory for | |
765 | it in the .bss section (via .dynbss). All we have to do here | |
766 | is create a COPY reloc if required. */ | |
7c6da9ca | 767 | if (h->copy_offset != (bfd_vma) -1) |
013dec1a ILT |
768 | { |
769 | asection *s; | |
770 | Elf_Internal_Rel rel; | |
771 | ||
772 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
773 | ".rel.bss"); | |
774 | BFD_ASSERT (s != NULL); | |
775 | ||
776 | rel.r_offset = (h->root.u.def.value | |
777 | + h->root.u.def.section->output_section->vma | |
778 | + h->root.u.def.section->output_offset); | |
779 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY); | |
780 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
781 | ((Elf32_External_Rel *) | |
7c6da9ca | 782 | (s->contents + h->copy_offset))); |
013dec1a ILT |
783 | } |
784 | } | |
785 | ||
786 | return true; | |
787 | } | |
788 | ||
789 | /* Finish up the dynamic sections. */ | |
790 | ||
791 | static boolean | |
792 | elf_i386_finish_dynamic_sections (output_bfd, info) | |
793 | bfd *output_bfd; | |
794 | struct bfd_link_info *info; | |
795 | { | |
796 | asection *splt; | |
797 | asection *sgot; | |
798 | asection *sdyn; | |
799 | Elf32_External_Dyn *dyncon, *dynconend; | |
800 | ||
801 | splt = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".plt"); | |
802 | sgot = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".got"); | |
803 | sdyn = bfd_get_section_by_name (elf_hash_table (info)->dynobj, ".dynamic"); | |
804 | BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL); | |
805 | ||
806 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
807 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
808 | for (; dyncon < dynconend; dyncon++) | |
809 | { | |
810 | Elf_Internal_Dyn dyn; | |
811 | const char *name; | |
812 | boolean size; | |
813 | ||
814 | bfd_elf32_swap_dyn_in (elf_hash_table (info)->dynobj, dyncon, &dyn); | |
815 | ||
816 | /* My reading of the SVR4 ABI indicates that the procedure | |
817 | linkage table relocs (DT_JMPREL) should be included in the | |
818 | overall relocs (DT_REL). This is what Solaris does. | |
819 | However, UnixWare can not handle that case. Therefore, we | |
820 | override the DT_REL and DT_RELSZ entries here to make them | |
821 | not include the JMPREL relocs. */ | |
822 | ||
823 | switch (dyn.d_tag) | |
824 | { | |
825 | case DT_PLTGOT: name = ".got"; size = false; break; | |
826 | case DT_PLTRELSZ: name = ".rel.plt"; size = true; break; | |
827 | case DT_JMPREL: name = ".rel.plt"; size = false; break; | |
828 | case DT_REL: name = ".rel.bss"; size = false; break; | |
829 | case DT_RELSZ: name = ".rel.bss"; size = true; break; | |
830 | default: name = NULL; size = false; break; | |
831 | } | |
832 | ||
833 | if (name != NULL) | |
834 | { | |
835 | asection *s; | |
836 | ||
837 | s = bfd_get_section_by_name (output_bfd, name); | |
838 | BFD_ASSERT (s != NULL); | |
839 | if (! size) | |
840 | dyn.d_un.d_ptr = s->vma; | |
841 | else | |
842 | { | |
843 | if (s->_cooked_size != 0) | |
844 | dyn.d_un.d_val = s->_cooked_size; | |
845 | else | |
846 | dyn.d_un.d_val = s->_raw_size; | |
847 | } | |
848 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
849 | } | |
850 | } | |
851 | ||
852 | /* Fill in the first entry in the procedure linkage table. */ | |
853 | if (splt->_raw_size > 0) | |
854 | { | |
855 | memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE); | |
856 | bfd_put_32 (output_bfd, | |
857 | sgot->output_section->vma + sgot->output_offset + 4, | |
858 | splt->contents + 2); | |
859 | bfd_put_32 (output_bfd, | |
860 | sgot->output_section->vma + sgot->output_offset + 8, | |
861 | splt->contents + 8); | |
862 | } | |
863 | ||
864 | /* Fill in the first three entries in the global offset table. */ | |
865 | if (sgot->_raw_size > 0) | |
866 | { | |
867 | bfd_put_32 (output_bfd, | |
868 | sdyn->output_section->vma + sdyn->output_offset, | |
869 | sgot->contents); | |
870 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); | |
871 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); | |
872 | } | |
873 | ||
874 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
875 | ||
876 | /* UnixWare sets the entsize of .plt to 4, although that doesn't | |
877 | really seem like the right value. */ | |
878 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4; | |
879 | ||
880 | return true; | |
881 | } | |
e4b6b3e7 ILT |
882 | |
883 | #define TARGET_LITTLE_SYM bfd_elf32_i386_vec | |
884 | #define TARGET_LITTLE_NAME "elf32-i386" | |
885 | #define ELF_ARCH bfd_arch_i386 | |
68241b2b | 886 | #define ELF_MACHINE_CODE EM_386 |
e4b6b3e7 ILT |
887 | #define elf_info_to_howto elf_i386_info_to_howto |
888 | #define elf_info_to_howto_rel elf_i386_info_to_howto_rel | |
889 | #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup | |
68241b2b | 890 | #define ELF_MAXPAGESIZE 0x1000 |
013dec1a ILT |
891 | #define elf_backend_create_dynamic_sections \ |
892 | elf_i386_create_dynamic_sections | |
893 | #define elf_backend_adjust_dynamic_symbol \ | |
894 | elf_i386_adjust_dynamic_symbol | |
895 | #define elf_backend_size_dynamic_sections \ | |
896 | elf_i386_size_dynamic_sections | |
897 | #define elf_backend_relocate_section elf_i386_relocate_section | |
898 | #define elf_backend_finish_dynamic_symbol \ | |
899 | elf_i386_finish_dynamic_symbol | |
900 | #define elf_backend_finish_dynamic_sections \ | |
901 | elf_i386_finish_dynamic_sections | |
e4b6b3e7 ILT |
902 | |
903 | #include "elf32-target.h" |