]>
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 *)); | |
eb4267a3 ILT |
34 | static boolean elf_i386_check_relocs |
35 | PARAMS ((bfd *, struct bfd_link_info *, asection *, | |
36 | const Elf_Internal_Rela *)); | |
013dec1a ILT |
37 | static boolean elf_i386_adjust_dynamic_symbol |
38 | PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *)); | |
013dec1a ILT |
39 | static boolean elf_i386_size_dynamic_sections |
40 | PARAMS ((bfd *, struct bfd_link_info *)); | |
41 | static boolean elf_i386_relocate_section | |
42 | PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *, | |
eb4267a3 | 43 | Elf_Internal_Rela *, Elf_Internal_Sym *, asection **)); |
013dec1a ILT |
44 | static boolean elf_i386_finish_dynamic_symbol |
45 | PARAMS ((bfd *, struct bfd_link_info *, struct elf_link_hash_entry *, | |
46 | Elf_Internal_Sym *)); | |
47 | static boolean elf_i386_finish_dynamic_sections | |
48 | PARAMS ((bfd *, struct bfd_link_info *)); | |
49 | ||
e4b6b3e7 ILT |
50 | #define USE_REL 1 /* 386 uses REL relocations instead of RELA */ |
51 | ||
52 | enum reloc_type | |
53 | { | |
54 | R_386_NONE = 0, | |
68241b2b ILT |
55 | R_386_32, |
56 | R_386_PC32, | |
57 | R_386_GOT32, | |
58 | R_386_PLT32, | |
e4b6b3e7 | 59 | R_386_COPY, |
68241b2b ILT |
60 | R_386_GLOB_DAT, |
61 | R_386_JUMP_SLOT, | |
e4b6b3e7 | 62 | R_386_RELATIVE, |
68241b2b ILT |
63 | R_386_GOTOFF, |
64 | R_386_GOTPC, | |
e4b6b3e7 ILT |
65 | R_386_max |
66 | }; | |
67 | ||
68 | #if 0 | |
69 | static CONST char *CONST reloc_type_names[] = | |
70 | { | |
71 | "R_386_NONE", | |
68241b2b ILT |
72 | "R_386_32", |
73 | "R_386_PC32", | |
74 | "R_386_GOT32", | |
75 | "R_386_PLT32", | |
e4b6b3e7 | 76 | "R_386_COPY", |
68241b2b ILT |
77 | "R_386_GLOB_DAT", |
78 | "R_386_JUMP_SLOT", | |
e4b6b3e7 | 79 | "R_386_RELATIVE", |
68241b2b ILT |
80 | "R_386_GOTOFF", |
81 | "R_386_GOTPC", | |
e4b6b3e7 ILT |
82 | }; |
83 | #endif | |
84 | ||
e4b6b3e7 ILT |
85 | static reloc_howto_type elf_howto_table[]= |
86 | { | |
68241b2b ILT |
87 | HOWTO(R_386_NONE, 0,0, 0,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_NONE", true,0x00000000,0x00000000,false), |
88 | HOWTO(R_386_32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_32", true,0xffffffff,0xffffffff,false), | |
89 | HOWTO(R_386_PC32, 0,2,32,true, 0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PC32", true,0xffffffff,0xffffffff,true), | |
90 | HOWTO(R_386_GOT32, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOT32", true,0xffffffff,0xffffffff,false), | |
eb4267a3 | 91 | HOWTO(R_386_PLT32, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_PLT32", true,0xffffffff,0xffffffff,true), |
68241b2b ILT |
92 | HOWTO(R_386_COPY, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_COPY", true,0xffffffff,0xffffffff,false), |
93 | 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), | |
94 | 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), | |
95 | HOWTO(R_386_RELATIVE, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_RELATIVE", true,0xffffffff,0xffffffff,false), | |
96 | HOWTO(R_386_GOTOFF, 0,2,32,false,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTOFF", true,0xffffffff,0xffffffff,false), | |
eb4267a3 | 97 | HOWTO(R_386_GOTPC, 0,2,32,true,0,complain_overflow_bitfield, bfd_elf_generic_reloc,"R_386_GOTPC", true,0xffffffff,0xffffffff,true), |
e4b6b3e7 ILT |
98 | }; |
99 | ||
100 | #ifdef DEBUG_GEN_RELOC | |
101 | #define TRACE(str) fprintf (stderr, "i386 bfd reloc lookup %d (%s)\n", code, str) | |
102 | #else | |
103 | #define TRACE(str) | |
104 | #endif | |
105 | ||
106 | static CONST struct reloc_howto_struct * | |
013dec1a ILT |
107 | elf_i386_reloc_type_lookup (abfd, code) |
108 | bfd *abfd; | |
109 | bfd_reloc_code_real_type code; | |
e4b6b3e7 ILT |
110 | { |
111 | switch (code) | |
112 | { | |
113 | case BFD_RELOC_NONE: | |
114 | TRACE ("BFD_RELOC_NONE"); | |
115 | return &elf_howto_table[ (int)R_386_NONE ]; | |
116 | ||
117 | case BFD_RELOC_32: | |
118 | TRACE ("BFD_RELOC_32"); | |
119 | return &elf_howto_table[ (int)R_386_32 ]; | |
120 | ||
121 | case BFD_RELOC_32_PCREL: | |
122 | TRACE ("BFD_RELOC_PC32"); | |
123 | return &elf_howto_table[ (int)R_386_PC32 ]; | |
124 | ||
68241b2b ILT |
125 | case BFD_RELOC_386_GOT32: |
126 | TRACE ("BFD_RELOC_386_GOT32"); | |
127 | return &elf_howto_table[ (int)R_386_GOT32 ]; | |
128 | ||
129 | case BFD_RELOC_386_PLT32: | |
130 | TRACE ("BFD_RELOC_386_PLT32"); | |
131 | return &elf_howto_table[ (int)R_386_PLT32 ]; | |
132 | ||
133 | case BFD_RELOC_386_COPY: | |
134 | TRACE ("BFD_RELOC_386_COPY"); | |
135 | return &elf_howto_table[ (int)R_386_COPY ]; | |
136 | ||
137 | case BFD_RELOC_386_GLOB_DAT: | |
138 | TRACE ("BFD_RELOC_386_GLOB_DAT"); | |
139 | return &elf_howto_table[ (int)R_386_GLOB_DAT ]; | |
140 | ||
141 | case BFD_RELOC_386_JUMP_SLOT: | |
142 | TRACE ("BFD_RELOC_386_JUMP_SLOT"); | |
143 | return &elf_howto_table[ (int)R_386_JUMP_SLOT ]; | |
144 | ||
145 | case BFD_RELOC_386_RELATIVE: | |
146 | TRACE ("BFD_RELOC_386_RELATIVE"); | |
147 | return &elf_howto_table[ (int)R_386_RELATIVE ]; | |
148 | ||
149 | case BFD_RELOC_386_GOTOFF: | |
150 | TRACE ("BFD_RELOC_386_GOTOFF"); | |
151 | return &elf_howto_table[ (int)R_386_GOTOFF ]; | |
152 | ||
153 | case BFD_RELOC_386_GOTPC: | |
154 | TRACE ("BFD_RELOC_386_GOTPC"); | |
155 | return &elf_howto_table[ (int)R_386_GOTPC ]; | |
156 | ||
e4b6b3e7 | 157 | default: |
68241b2b | 158 | break; |
e4b6b3e7 ILT |
159 | } |
160 | ||
161 | TRACE ("Unknown"); | |
162 | return 0; | |
163 | } | |
164 | ||
165 | static void | |
013dec1a ILT |
166 | elf_i386_info_to_howto (abfd, cache_ptr, dst) |
167 | bfd *abfd; | |
168 | arelent *cache_ptr; | |
169 | Elf32_Internal_Rela *dst; | |
e4b6b3e7 | 170 | { |
68241b2b | 171 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max); |
e4b6b3e7 ILT |
172 | |
173 | cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
174 | } | |
175 | ||
176 | static void | |
013dec1a ILT |
177 | elf_i386_info_to_howto_rel (abfd, cache_ptr, dst) |
178 | bfd *abfd; | |
179 | arelent *cache_ptr; | |
180 | Elf32_Internal_Rel *dst; | |
e4b6b3e7 | 181 | { |
68241b2b | 182 | BFD_ASSERT (ELF32_R_TYPE(dst->r_info) < (unsigned int) R_386_max); |
e4b6b3e7 ILT |
183 | |
184 | cache_ptr->howto = &elf_howto_table[ELF32_R_TYPE(dst->r_info)]; | |
185 | } | |
013dec1a ILT |
186 | \f |
187 | /* Functions for the i386 ELF linker. */ | |
188 | ||
189 | /* The name of the dynamic interpreter. This is put in the .interp | |
190 | section. */ | |
191 | ||
192 | #define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1" | |
193 | ||
194 | /* The size in bytes of an entry in the procedure linkage table. */ | |
195 | ||
196 | #define PLT_ENTRY_SIZE 16 | |
197 | ||
198 | /* The first entry in an absolute procedure linkage table looks like | |
199 | this. See the SVR4 ABI i386 supplement to see how this works. */ | |
200 | ||
201 | static bfd_byte elf_i386_plt0_entry[PLT_ENTRY_SIZE] = | |
202 | { | |
203 | 0xff, 0x35, /* pushl contents of address */ | |
204 | 0, 0, 0, 0, /* replaced with address of .got + 4. */ | |
205 | 0xff, 0x25, /* jmp indirect */ | |
206 | 0, 0, 0, 0, /* replaced with address of .got + 8. */ | |
207 | 0, 0, 0, 0 /* pad out to 16 bytes. */ | |
208 | }; | |
209 | ||
210 | /* Subsequent entries in an absolute procedure linkage table look like | |
211 | this. */ | |
212 | ||
213 | static bfd_byte elf_i386_plt_entry[PLT_ENTRY_SIZE] = | |
214 | { | |
215 | 0xff, 0x25, /* jmp indirect */ | |
216 | 0, 0, 0, 0, /* replaced with address of this symbol in .got. */ | |
217 | 0x68, /* pushl immediate */ | |
218 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
219 | 0xe9, /* jmp relative */ | |
220 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
221 | }; | |
222 | ||
eb4267a3 ILT |
223 | /* The first entry in a PIC procedure linkage table look like this. */ |
224 | ||
225 | static bfd_byte elf_i386_pic_plt0_entry[PLT_ENTRY_SIZE] = | |
226 | { | |
227 | 0xff, 0xb3, 4, 0, 0, 0, /* pushl 4(%ebx) */ | |
228 | 0xff, 0xa3, 8, 0, 0, 0, /* jmp *8(%ebx) */ | |
229 | 0, 0, 0, 0 /* pad out to 16 bytes. */ | |
230 | }; | |
231 | ||
232 | /* Subsequent entries in a PIC procedure linkage table look like this. */ | |
233 | ||
234 | static bfd_byte elf_i386_pic_plt_entry[PLT_ENTRY_SIZE] = | |
235 | { | |
236 | 0xff, 0xa3, /* jmp *offset(%ebx) */ | |
237 | 0, 0, 0, 0, /* replaced with offset of this symbol in .got. */ | |
238 | 0x68, /* pushl immediate */ | |
239 | 0, 0, 0, 0, /* replaced with offset into relocation table. */ | |
240 | 0xe9, /* jmp relative */ | |
241 | 0, 0, 0, 0 /* replaced with offset to start of .plt. */ | |
242 | }; | |
243 | ||
013dec1a ILT |
244 | /* Create dynamic sections when linking against a dynamic object. */ |
245 | ||
246 | static boolean | |
247 | elf_i386_create_dynamic_sections (abfd, info) | |
248 | bfd *abfd; | |
249 | struct bfd_link_info *info; | |
250 | { | |
251 | flagword flags; | |
252 | register asection *s; | |
253 | struct elf_link_hash_entry *h; | |
254 | ||
eb4267a3 ILT |
255 | /* We need to create .plt, .rel.plt, .got, .got.plt, .dynbss, and |
256 | .rel.bss sections. */ | |
013dec1a ILT |
257 | |
258 | flags = SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY; | |
259 | ||
260 | s = bfd_make_section (abfd, ".plt"); | |
261 | if (s == NULL | |
262 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY | SEC_CODE) | |
263 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
264 | return false; | |
265 | ||
266 | s = bfd_make_section (abfd, ".rel.plt"); | |
267 | if (s == NULL | |
268 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
269 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
270 | return false; | |
271 | ||
272 | s = bfd_make_section (abfd, ".got"); | |
273 | if (s == NULL | |
274 | || ! bfd_set_section_flags (abfd, s, flags) | |
275 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
276 | return false; | |
277 | ||
eb4267a3 ILT |
278 | s = bfd_make_section (abfd, ".got.plt"); |
279 | if (s == NULL | |
280 | || ! bfd_set_section_flags (abfd, s, flags) | |
281 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
282 | return false; | |
283 | ||
284 | /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the | |
285 | .got.plt section, which will be placed at the start of the output | |
286 | .got section. We don't do this in the linker script because we | |
287 | don't want to define the symbol if we are not creating a global | |
288 | offset table. */ | |
013dec1a ILT |
289 | h = NULL; |
290 | if (! (_bfd_generic_link_add_one_symbol | |
291 | (info, abfd, "_GLOBAL_OFFSET_TABLE_", BSF_GLOBAL, s, (bfd_vma) 0, | |
292 | (const char *) NULL, false, get_elf_backend_data (abfd)->collect, | |
293 | (struct bfd_link_hash_entry **) &h))) | |
294 | return false; | |
295 | h->elf_link_hash_flags |= ELF_LINK_HASH_DEF_REGULAR; | |
eb4267a3 ILT |
296 | h->type = STT_OBJECT; |
297 | ||
298 | if (info->shared | |
299 | && ! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
300 | return false; | |
013dec1a ILT |
301 | |
302 | /* The first three global offset table entries are reserved. */ | |
303 | s->_raw_size += 3 * 4; | |
304 | ||
305 | /* The .dynbss section is a place to put symbols which are defined | |
306 | by dynamic objects, are referenced by regular objects, and are | |
307 | not functions. We must allocate space for them in the process | |
308 | image and use a R_386_COPY reloc to tell the dynamic linker to | |
309 | initialize them at run time. The linker script puts the .dynbss | |
310 | section into the .bss section of the final image. */ | |
311 | s = bfd_make_section (abfd, ".dynbss"); | |
312 | if (s == NULL | |
313 | || ! bfd_set_section_flags (abfd, s, SEC_ALLOC)) | |
314 | return false; | |
315 | ||
316 | /* The .rel.bss section holds copy relocs. This section is not | |
317 | normally needed. We need to create it here, though, so that the | |
eb4267a3 ILT |
318 | linker will map it to an output section. We can't just create it |
319 | only if we need it, because we will not know whether we need it | |
320 | until we have seen all the input files, and the first time the | |
321 | main linker code calls BFD after examining all the input files | |
322 | (size_dynamic_sections) the input sections have already been | |
323 | mapped to the output sections. If the section turns out not to | |
324 | be needed, we can discard it later. We will never need this | |
325 | section when generating a shared object, since they do not use | |
326 | copy relocs. */ | |
327 | if (! info->shared) | |
328 | { | |
329 | s = bfd_make_section (abfd, ".rel.bss"); | |
330 | if (s == NULL | |
331 | || ! bfd_set_section_flags (abfd, s, flags | SEC_READONLY) | |
332 | || ! bfd_set_section_alignment (abfd, s, 2)) | |
333 | return false; | |
334 | } | |
335 | ||
336 | return true; | |
337 | } | |
338 | ||
339 | /* Look through the relocs for a section during the first phase, and | |
340 | allocate space in the global offset table or procedure linkage | |
341 | table. */ | |
342 | ||
343 | static boolean | |
344 | elf_i386_check_relocs (abfd, info, sec, relocs) | |
345 | bfd *abfd; | |
346 | struct bfd_link_info *info; | |
347 | asection *sec; | |
348 | const Elf_Internal_Rela *relocs; | |
349 | { | |
350 | bfd *dynobj; | |
351 | Elf_Internal_Shdr *symtab_hdr; | |
352 | struct elf_link_hash_entry **sym_hashes; | |
353 | bfd_vma *local_got_offsets; | |
354 | const Elf_Internal_Rela *rel; | |
355 | const Elf_Internal_Rela *rel_end; | |
356 | asection *sgot; | |
357 | asection *srelgot; | |
358 | asection *splt; | |
359 | asection *sgotplt; | |
360 | asection *srelplt; | |
361 | asection *sreloc; | |
362 | ||
363 | if (info->relocateable) | |
364 | return true; | |
365 | ||
366 | dynobj = elf_hash_table (info)->dynobj; | |
367 | symtab_hdr = &elf_tdata (abfd)->symtab_hdr; | |
368 | sym_hashes = elf_sym_hashes (abfd); | |
369 | local_got_offsets = elf_local_got_offsets (abfd); | |
370 | ||
371 | sgot = NULL; | |
372 | srelgot = NULL; | |
373 | splt = NULL; | |
374 | sgotplt = NULL; | |
375 | srelplt = NULL; | |
376 | sreloc = NULL; | |
377 | ||
378 | rel_end = relocs + sec->reloc_count; | |
379 | for (rel = relocs; rel < rel_end; rel++) | |
380 | { | |
381 | long r_symndx; | |
382 | struct elf_link_hash_entry *h; | |
383 | ||
384 | r_symndx = ELF32_R_SYM (rel->r_info); | |
385 | ||
386 | if (r_symndx < symtab_hdr->sh_info) | |
387 | h = NULL; | |
388 | else | |
389 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; | |
390 | ||
391 | /* Some relocs require a global offset table. FIXME: If this is | |
392 | a static link of PIC code, we need a global offset table but | |
393 | we don't really need to create the full dynamic linking | |
394 | information. */ | |
395 | if (dynobj == NULL) | |
396 | { | |
397 | switch (ELF32_R_TYPE (rel->r_info)) | |
398 | { | |
399 | case R_386_GOT32: | |
400 | case R_386_PLT32: | |
401 | case R_386_GOTOFF: | |
402 | case R_386_GOTPC: | |
403 | elf_hash_table (info)->dynobj = dynobj = abfd; | |
404 | if (! bfd_elf32_link_create_dynamic_sections (dynobj, info)) | |
405 | return false; | |
406 | break; | |
407 | ||
408 | default: | |
409 | break; | |
410 | } | |
411 | } | |
412 | ||
413 | switch (ELF32_R_TYPE (rel->r_info)) | |
414 | { | |
415 | case R_386_GOT32: | |
416 | /* This symbol requires a global offset table entry. */ | |
417 | ||
418 | if (sgot == NULL) | |
419 | { | |
420 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
421 | srelgot = bfd_get_section_by_name (dynobj, ".rel.got"); | |
422 | if (srelgot == NULL) | |
423 | { | |
424 | srelgot = bfd_make_section (dynobj, ".rel.got"); | |
425 | if (srelgot == NULL | |
426 | || ! bfd_set_section_flags (dynobj, srelgot, | |
427 | (SEC_ALLOC | |
428 | | SEC_LOAD | |
429 | | SEC_HAS_CONTENTS | |
430 | | SEC_IN_MEMORY | |
431 | | SEC_READONLY)) | |
432 | || ! bfd_set_section_alignment (dynobj, srelgot, 2)) | |
433 | return false; | |
434 | } | |
435 | BFD_ASSERT (sgot != NULL && srelgot != NULL); | |
436 | } | |
437 | ||
438 | if (h != NULL) | |
439 | { | |
440 | if (h->got_offset != (bfd_vma) -1) | |
441 | { | |
442 | /* We have already allocated space in the .got. */ | |
443 | break; | |
444 | } | |
445 | h->got_offset = sgot->_raw_size; | |
446 | ||
447 | /* Make sure this symbol is output as a dynamic symbol. */ | |
448 | if (h->dynindx == -1) | |
449 | { | |
450 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
451 | return false; | |
452 | } | |
453 | } | |
454 | else | |
455 | { | |
456 | /* This is a global offset table entry for a local | |
457 | symbol. */ | |
458 | if (local_got_offsets == NULL) | |
459 | { | |
460 | size_t size; | |
461 | register int i; | |
462 | ||
463 | size = symtab_hdr->sh_info * sizeof (bfd_vma); | |
464 | local_got_offsets = (bfd_vma *) bfd_alloc (abfd, size); | |
465 | if (local_got_offsets == NULL) | |
466 | { | |
467 | bfd_set_error (bfd_error_no_memory); | |
468 | return false; | |
469 | } | |
470 | elf_local_got_offsets (abfd) = local_got_offsets; | |
471 | for (i = 0; i < symtab_hdr->sh_info; i++) | |
472 | local_got_offsets[i] = (bfd_vma) -1; | |
473 | } | |
474 | if (local_got_offsets[r_symndx] != (bfd_vma) -1) | |
475 | { | |
476 | /* We have already allocated space in the .got. */ | |
477 | break; | |
478 | } | |
479 | local_got_offsets[r_symndx] = sgot->_raw_size; | |
480 | } | |
481 | ||
482 | sgot->_raw_size += 4; | |
483 | srelgot->_raw_size += sizeof (Elf32_External_Rel); | |
484 | ||
485 | break; | |
486 | ||
487 | case R_386_PLT32: | |
488 | /* This symbol requires a procedure linkage table entry. */ | |
489 | ||
490 | /* If this is a local symbol, we resolve it directly without | |
491 | creating a procedure linkage table entry. */ | |
492 | if (h == NULL) | |
493 | continue; | |
494 | ||
495 | if (h->plt_offset != (bfd_vma) -1) | |
496 | { | |
497 | /* There is already an entry for this symbol in the | |
498 | procedure linkage table. */ | |
499 | break; | |
500 | } | |
501 | ||
502 | if (splt == NULL) | |
503 | { | |
504 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
505 | sgotplt = bfd_get_section_by_name (dynobj, ".got.plt"); | |
506 | srelplt = bfd_get_section_by_name (dynobj, ".rel.plt"); | |
507 | BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL); | |
508 | } | |
509 | ||
510 | /* If this is the first .plt entry, make room for the | |
511 | special first entry. */ | |
512 | if (splt->_raw_size == 0) | |
513 | splt->_raw_size += PLT_ENTRY_SIZE; | |
514 | ||
515 | /* Make sure this symbol is output as a dynamic symbol. */ | |
516 | if (h->dynindx == -1) | |
517 | { | |
518 | if (! bfd_elf32_link_record_dynamic_symbol (info, h)) | |
519 | return false; | |
520 | } | |
521 | ||
522 | h->plt_offset = splt->_raw_size; | |
523 | ||
524 | /* Make room for this entry. We need a procedure linkage | |
525 | table entry in .plt, a global offset table entry in | |
526 | .got.plt (which is placed in .got by the linker script), | |
527 | and a relocation in .rel.plt. */ | |
528 | splt->_raw_size += PLT_ENTRY_SIZE; | |
529 | sgotplt->_raw_size += 4; | |
530 | srelplt->_raw_size += sizeof (Elf32_External_Rel); | |
531 | ||
532 | break; | |
533 | ||
534 | case R_386_32: | |
535 | case R_386_PC32: | |
536 | if (info->shared | |
537 | && (sec->flags & SEC_ALLOC) != 0) | |
538 | { | |
539 | /* When creating a shared object, we must output a | |
540 | R_386_RELATIVE reloc for this location. We create a | |
541 | reloc section in dynobj and make room for this reloc. */ | |
542 | if (sreloc == NULL) | |
543 | { | |
544 | const char *name; | |
545 | ||
546 | name = (elf_string_from_elf_section | |
547 | (abfd, | |
548 | elf_elfheader (abfd)->e_shstrndx, | |
549 | elf_section_data (sec)->rel_hdr.sh_name)); | |
550 | if (name == NULL) | |
551 | return false; | |
552 | ||
553 | BFD_ASSERT (strncmp (name, ".rel", 4) == 0 | |
554 | && strcmp (bfd_get_section_name (abfd, sec), | |
555 | name + 4) == 0); | |
556 | ||
557 | sreloc = bfd_get_section_by_name (dynobj, name); | |
558 | if (sreloc == NULL) | |
559 | { | |
560 | sreloc = bfd_make_section (dynobj, name); | |
561 | if (sreloc == NULL | |
562 | || ! bfd_set_section_flags (dynobj, sreloc, | |
563 | (SEC_ALLOC | |
564 | | SEC_LOAD | |
565 | | SEC_HAS_CONTENTS | |
566 | | SEC_IN_MEMORY | |
567 | | SEC_READONLY)) | |
568 | || ! bfd_set_section_alignment (dynobj, sreloc, 2)) | |
569 | return false; | |
570 | } | |
571 | } | |
572 | ||
573 | sreloc->_raw_size += sizeof (Elf32_External_Rel); | |
574 | } | |
575 | ||
576 | break; | |
577 | ||
578 | default: | |
579 | break; | |
580 | } | |
581 | } | |
013dec1a ILT |
582 | |
583 | return true; | |
584 | } | |
585 | ||
586 | /* Adjust a symbol defined by a dynamic object and referenced by a | |
587 | regular object. The current definition is in some section of the | |
588 | dynamic object, but we're not including those sections. We have to | |
589 | change the definition to something the rest of the link can | |
590 | understand. */ | |
591 | ||
592 | static boolean | |
593 | elf_i386_adjust_dynamic_symbol (info, h) | |
594 | struct bfd_link_info *info; | |
595 | struct elf_link_hash_entry *h; | |
596 | { | |
597 | bfd *dynobj; | |
598 | asection *s; | |
599 | unsigned int power_of_two; | |
013dec1a ILT |
600 | |
601 | dynobj = elf_hash_table (info)->dynobj; | |
602 | ||
603 | /* Make sure we know what is going on here. */ | |
604 | BFD_ASSERT (dynobj != NULL | |
605 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_DYNAMIC) != 0 | |
606 | && (h->elf_link_hash_flags & ELF_LINK_HASH_REF_REGULAR) != 0 | |
607 | && (h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0 | |
608 | && h->root.type == bfd_link_hash_defined | |
609 | && (bfd_get_flavour (h->root.u.def.section->owner) | |
610 | == bfd_target_elf_flavour) | |
611 | && (elf_elfheader (h->root.u.def.section->owner)->e_type | |
612 | == ET_DYN) | |
613 | && h->root.u.def.section->output_section == NULL); | |
614 | ||
615 | /* If this is a function, put it in the procedure linkage table. We | |
616 | will fill in the contents of the procedure linkage table later, | |
617 | when we know the address of the .got section. */ | |
618 | if (h->type == STT_FUNC) | |
619 | { | |
620 | s = bfd_get_section_by_name (dynobj, ".plt"); | |
621 | BFD_ASSERT (s != NULL); | |
622 | ||
eb4267a3 ILT |
623 | if (h->plt_offset != (bfd_vma) -1) |
624 | { | |
625 | h->root.u.def.section = s; | |
626 | h->root.u.def.value = h->plt_offset; | |
627 | } | |
628 | else | |
629 | { | |
630 | /* If this is the first .plt entry, make room for the | |
631 | special first entry. */ | |
632 | if (s->_raw_size == 0) | |
633 | s->_raw_size += PLT_ENTRY_SIZE; | |
013dec1a | 634 | |
eb4267a3 ILT |
635 | /* Set the symbol to this location in the .plt. */ |
636 | h->root.u.def.section = s; | |
637 | h->root.u.def.value = s->_raw_size; | |
013dec1a | 638 | |
eb4267a3 | 639 | h->plt_offset = s->_raw_size; |
013dec1a | 640 | |
eb4267a3 ILT |
641 | /* Make room for this entry. */ |
642 | s->_raw_size += PLT_ENTRY_SIZE; | |
013dec1a | 643 | |
eb4267a3 ILT |
644 | /* We also need to make an entry in the .got.plt section, |
645 | which will be placed in the .got section by the linker | |
646 | script. */ | |
013dec1a | 647 | |
eb4267a3 ILT |
648 | s = bfd_get_section_by_name (dynobj, ".got.plt"); |
649 | BFD_ASSERT (s != NULL); | |
650 | s->_raw_size += 4; | |
013dec1a | 651 | |
eb4267a3 ILT |
652 | /* We also need to make an entry in the .rel.plt section. */ |
653 | ||
654 | s = bfd_get_section_by_name (dynobj, ".rel.plt"); | |
655 | BFD_ASSERT (s != NULL); | |
656 | s->_raw_size += sizeof (Elf32_External_Rel); | |
657 | } | |
013dec1a ILT |
658 | |
659 | return true; | |
660 | } | |
661 | ||
662 | /* If this is a weak symbol, and there is a real definition, the | |
663 | processor independent code will have arranged for us to see the | |
664 | real definition first, and we can just use the same value. */ | |
665 | if (h->weakdef != NULL) | |
666 | { | |
667 | BFD_ASSERT (h->weakdef->root.type == bfd_link_hash_defined); | |
668 | h->root.u.def.section = h->weakdef->root.u.def.section; | |
669 | h->root.u.def.value = h->weakdef->root.u.def.value; | |
013dec1a ILT |
670 | return true; |
671 | } | |
672 | ||
673 | /* This is a reference to a symbol defined by a dynamic object which | |
eb4267a3 ILT |
674 | is not a function. */ |
675 | ||
676 | /* If we are creating a shared library, we must presume that the | |
677 | only references to the symbol are via the global offset table. | |
678 | For such cases we need not do anything here; the relocations will | |
679 | be handled correctly by relocate_section. */ | |
680 | if (info->shared) | |
681 | return true; | |
682 | ||
683 | /* We must allocate the symbol in our .dynbss section, which will | |
684 | become part of the .bss section of the executable. There will be | |
685 | an entry for this symbol in the .dynsym section. The dynamic | |
686 | object will contain position independent code, so all references | |
687 | from the dynamic object to this symbol will go through the global | |
688 | offset table. The dynamic linker will use the .dynsym entry to | |
689 | determine the address it must put in the global offset table, so | |
690 | both the dynamic object and the regular object will refer to the | |
691 | same memory location for the variable. */ | |
013dec1a ILT |
692 | |
693 | s = bfd_get_section_by_name (dynobj, ".dynbss"); | |
694 | BFD_ASSERT (s != NULL); | |
695 | ||
696 | /* If the symbol is currently defined in the .bss section of the | |
697 | dynamic object, then it is OK to simply initialize it to zero. | |
698 | If the symbol is in some other section, we must generate a | |
699 | R_386_COPY reloc to tell the dynamic linker to copy the initial | |
700 | value out of the dynamic object and into the runtime process | |
701 | image. We need to remember the offset into the .rel.bss section | |
7c6da9ca | 702 | we are going to use. */ |
eb4267a3 | 703 | if ((h->root.u.def.section->flags & SEC_LOAD) != 0) |
013dec1a ILT |
704 | { |
705 | asection *srel; | |
706 | ||
707 | srel = bfd_get_section_by_name (dynobj, ".rel.bss"); | |
708 | BFD_ASSERT (srel != NULL); | |
013dec1a | 709 | srel->_raw_size += sizeof (Elf32_External_Rel); |
eb4267a3 | 710 | h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY; |
013dec1a ILT |
711 | } |
712 | ||
713 | /* We need to figure out the alignment required for this symbol. I | |
714 | have no idea how ELF linkers handle this. */ | |
7c6da9ca ILT |
715 | power_of_two = bfd_log2 (h->size); |
716 | if (power_of_two > 3) | |
717 | power_of_two = 3; | |
013dec1a ILT |
718 | |
719 | /* Apply the required alignment. */ | |
7c6da9ca ILT |
720 | s->_raw_size = BFD_ALIGN (s->_raw_size, |
721 | (bfd_size_type) (1 << power_of_two)); | |
013dec1a ILT |
722 | if (power_of_two > bfd_get_section_alignment (dynobj, s)) |
723 | { | |
724 | if (! bfd_set_section_alignment (dynobj, s, power_of_two)) | |
725 | return false; | |
726 | } | |
727 | ||
728 | /* Define the symbol as being at this point in the section. */ | |
729 | h->root.u.def.section = s; | |
730 | h->root.u.def.value = s->_raw_size; | |
731 | ||
732 | /* Increment the section size to make room for the symbol. */ | |
733 | s->_raw_size += h->size; | |
734 | ||
735 | return true; | |
736 | } | |
737 | ||
013dec1a ILT |
738 | /* Set the sizes of the dynamic sections. */ |
739 | ||
740 | static boolean | |
741 | elf_i386_size_dynamic_sections (output_bfd, info) | |
742 | bfd *output_bfd; | |
743 | struct bfd_link_info *info; | |
744 | { | |
745 | bfd *dynobj; | |
746 | asection *s; | |
eb4267a3 ILT |
747 | boolean plt; |
748 | boolean relocs; | |
749 | boolean reltext; | |
013dec1a ILT |
750 | |
751 | dynobj = elf_hash_table (info)->dynobj; | |
752 | BFD_ASSERT (dynobj != NULL); | |
753 | ||
754 | /* Set the contents of the .interp section to the interpreter. */ | |
8af74670 ILT |
755 | if (! info->shared) |
756 | { | |
757 | s = bfd_get_section_by_name (dynobj, ".interp"); | |
758 | BFD_ASSERT (s != NULL); | |
759 | s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER; | |
760 | s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER; | |
761 | } | |
013dec1a | 762 | |
eb4267a3 ILT |
763 | /* The check_relocs and adjust_dynamic_symbol entry points have |
764 | determined the sizes of the various dynamic sections. Allocate | |
765 | memory for them. */ | |
766 | plt = false; | |
767 | relocs = false; | |
768 | reltext = false; | |
769 | for (s = dynobj->sections; s != NULL; s = s->next) | |
770 | { | |
771 | const char *name; | |
772 | boolean strip; | |
773 | ||
774 | if ((s->flags & SEC_IN_MEMORY) == 0) | |
775 | continue; | |
776 | ||
777 | /* It's OK to base decisions on the section name, because none | |
778 | of the dynobj section names depend upon the input files. */ | |
779 | name = bfd_get_section_name (dynobj, s); | |
780 | ||
781 | strip = false; | |
782 | ||
783 | if (strcmp (name, ".plt") == 0) | |
784 | { | |
785 | if (s->_raw_size == 0) | |
786 | { | |
787 | /* Strip this section if we don't need it; see the | |
788 | comment below. */ | |
789 | strip = true; | |
790 | } | |
791 | else | |
792 | { | |
793 | /* Remember whether there is a PLT. */ | |
794 | plt = true; | |
795 | } | |
796 | } | |
797 | else if (strncmp (name, ".rel", 4) == 0) | |
798 | { | |
799 | if (s->_raw_size == 0) | |
800 | { | |
801 | /* If we don't need this section, strip it from the | |
802 | output file. This is mostly to handle .rel.bss and | |
803 | .rel.plt. We must create both sections in | |
804 | create_dynamic_sections, because they must be created | |
805 | before the linker maps input sections to output | |
806 | sections. The linker does that before | |
807 | adjust_dynamic_symbol is called, and it is that | |
808 | function which decides whether anything needs to go | |
809 | into these sections. */ | |
810 | strip = true; | |
811 | } | |
812 | else | |
813 | { | |
814 | asection *target; | |
815 | ||
816 | /* Remember whether there are any reloc sections other | |
817 | than .rel.plt. */ | |
818 | if (strcmp (name, ".rel.plt") != 0) | |
819 | relocs = true; | |
820 | ||
821 | /* If this relocation section applies to a read only | |
822 | section, then we probably need a DT_TEXTREL entry. */ | |
823 | target = bfd_get_section_by_name (output_bfd, name + 4); | |
824 | if (target != NULL | |
825 | && (target->flags & SEC_READONLY) != 0) | |
826 | reltext = true; | |
827 | ||
828 | /* We use the reloc_count field as a counter if we need | |
829 | to copy relocs into the output file. */ | |
830 | s->reloc_count = 0; | |
831 | } | |
832 | } | |
833 | else if (strncmp (name, ".got", 4) != 0) | |
834 | { | |
835 | /* It's not one of our sections, so don't allocate space. */ | |
836 | continue; | |
837 | } | |
838 | ||
839 | if (strip) | |
840 | { | |
841 | asection **spp; | |
842 | ||
843 | for (spp = &s->output_section->owner->sections; | |
844 | *spp != s->output_section; | |
845 | spp = &(*spp)->next) | |
846 | ; | |
847 | *spp = s->output_section->next; | |
848 | --s->output_section->owner->section_count; | |
849 | ||
850 | continue; | |
851 | } | |
013dec1a | 852 | |
eb4267a3 ILT |
853 | /* Allocate memory for the section contents. */ |
854 | s->contents = (bfd_byte *) bfd_alloc (dynobj, s->_raw_size); | |
855 | if (s->contents == NULL && s->_raw_size != 0) | |
856 | { | |
857 | bfd_set_error (bfd_error_no_memory); | |
858 | return false; | |
859 | } | |
860 | } | |
861 | ||
013dec1a ILT |
862 | /* Add some entries to the .dynamic section. We fill in the values |
863 | later, in elf_i386_finish_dynamic_sections, but we must add the | |
864 | entries now so that we get the correct size for the .dynamic | |
8af74670 ILT |
865 | section. The DT_DEBUG entry is filled in by the dynamic linker |
866 | and used by the debugger. */ | |
eb4267a3 ILT |
867 | if (! info->shared) |
868 | { | |
869 | if (! bfd_elf32_add_dynamic_entry (info, DT_DEBUG, 0)) | |
870 | return false; | |
871 | } | |
013dec1a | 872 | |
eb4267a3 | 873 | if (plt) |
013dec1a | 874 | { |
eb4267a3 ILT |
875 | if (! bfd_elf32_add_dynamic_entry (info, DT_PLTGOT, 0) |
876 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTRELSZ, 0) | |
013dec1a ILT |
877 | || ! bfd_elf32_add_dynamic_entry (info, DT_PLTREL, DT_REL) |
878 | || ! bfd_elf32_add_dynamic_entry (info, DT_JMPREL, 0)) | |
879 | return false; | |
880 | } | |
881 | ||
eb4267a3 | 882 | if (relocs) |
013dec1a ILT |
883 | { |
884 | if (! bfd_elf32_add_dynamic_entry (info, DT_REL, 0) | |
885 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELSZ, 0) | |
886 | || ! bfd_elf32_add_dynamic_entry (info, DT_RELENT, | |
887 | sizeof (Elf32_External_Rel))) | |
888 | return false; | |
889 | } | |
890 | ||
eb4267a3 ILT |
891 | if (reltext) |
892 | { | |
893 | if (! bfd_elf32_add_dynamic_entry (info, DT_TEXTREL, 0)) | |
894 | return false; | |
895 | } | |
896 | ||
013dec1a ILT |
897 | return true; |
898 | } | |
899 | ||
900 | /* Relocate an i386 ELF section. */ | |
901 | ||
902 | static boolean | |
903 | elf_i386_relocate_section (output_bfd, info, input_bfd, input_section, | |
eb4267a3 | 904 | contents, relocs, local_syms, local_sections) |
013dec1a ILT |
905 | bfd *output_bfd; |
906 | struct bfd_link_info *info; | |
907 | bfd *input_bfd; | |
908 | asection *input_section; | |
909 | bfd_byte *contents; | |
910 | Elf_Internal_Rela *relocs; | |
911 | Elf_Internal_Sym *local_syms; | |
912 | asection **local_sections; | |
7c6da9ca | 913 | char *output_names; |
013dec1a | 914 | { |
eb4267a3 | 915 | bfd *dynobj; |
013dec1a | 916 | Elf_Internal_Shdr *symtab_hdr; |
eb4267a3 ILT |
917 | struct elf_link_hash_entry **sym_hashes; |
918 | bfd_vma *local_got_offsets; | |
919 | asection *sgot; | |
920 | asection *splt; | |
921 | asection *sreloc; | |
013dec1a ILT |
922 | Elf_Internal_Rela *rel; |
923 | Elf_Internal_Rela *relend; | |
924 | ||
eb4267a3 | 925 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a | 926 | symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr; |
eb4267a3 ILT |
927 | sym_hashes = elf_sym_hashes (input_bfd); |
928 | local_got_offsets = elf_local_got_offsets (input_bfd); | |
929 | ||
930 | sgot = NULL; | |
931 | splt = NULL; | |
932 | sreloc = NULL; | |
013dec1a ILT |
933 | |
934 | rel = relocs; | |
935 | relend = relocs + input_section->reloc_count; | |
936 | for (; rel < relend; rel++) | |
937 | { | |
938 | int r_type; | |
939 | const reloc_howto_type *howto; | |
940 | long r_symndx; | |
941 | struct elf_link_hash_entry *h; | |
942 | Elf_Internal_Sym *sym; | |
943 | asection *sec; | |
944 | bfd_vma relocation; | |
945 | bfd_reloc_status_type r; | |
946 | ||
947 | r_type = ELF32_R_TYPE (rel->r_info); | |
948 | if (r_type < 0 || r_type >= (int) R_386_max) | |
949 | { | |
950 | bfd_set_error (bfd_error_bad_value); | |
951 | return false; | |
952 | } | |
953 | howto = elf_howto_table + r_type; | |
954 | ||
955 | r_symndx = ELF32_R_SYM (rel->r_info); | |
956 | ||
957 | if (info->relocateable) | |
958 | { | |
959 | /* This is a relocateable link. We don't have to change | |
960 | anything, unless the reloc is against a section symbol, | |
961 | in which case we have to adjust according to where the | |
962 | section symbol winds up in the output section. */ | |
963 | if (r_symndx < symtab_hdr->sh_info) | |
964 | { | |
965 | sym = local_syms + r_symndx; | |
966 | if (ELF_ST_TYPE (sym->st_info) == STT_SECTION) | |
967 | { | |
968 | bfd_vma val; | |
969 | ||
970 | sec = local_sections[r_symndx]; | |
971 | val = bfd_get_32 (input_bfd, contents + rel->r_offset); | |
972 | val += sec->output_offset + sym->st_value; | |
973 | bfd_put_32 (input_bfd, val, contents + rel->r_offset); | |
974 | } | |
975 | } | |
976 | ||
977 | continue; | |
978 | } | |
979 | ||
980 | /* This is a final link. */ | |
981 | h = NULL; | |
982 | sym = NULL; | |
983 | sec = NULL; | |
984 | if (r_symndx < symtab_hdr->sh_info) | |
985 | { | |
986 | sym = local_syms + r_symndx; | |
987 | sec = local_sections[r_symndx]; | |
988 | relocation = (sec->output_section->vma | |
989 | + sec->output_offset | |
990 | + sym->st_value); | |
991 | } | |
992 | else | |
993 | { | |
eb4267a3 | 994 | h = sym_hashes[r_symndx - symtab_hdr->sh_info]; |
013dec1a ILT |
995 | if (h->root.type == bfd_link_hash_defined) |
996 | { | |
997 | sec = h->root.u.def.section; | |
998 | relocation = (h->root.u.def.value | |
999 | + sec->output_section->vma | |
1000 | + sec->output_offset); | |
1001 | } | |
1002 | else if (h->root.type == bfd_link_hash_weak) | |
1003 | relocation = 0; | |
eb4267a3 ILT |
1004 | else if (info->shared) |
1005 | relocation = 0; | |
013dec1a ILT |
1006 | else |
1007 | { | |
1008 | if (! ((*info->callbacks->undefined_symbol) | |
1009 | (info, h->root.root.string, input_bfd, | |
1010 | input_section, rel->r_offset))) | |
1011 | return false; | |
1012 | relocation = 0; | |
1013 | } | |
1014 | } | |
1015 | ||
eb4267a3 ILT |
1016 | switch (r_type) |
1017 | { | |
1018 | case R_386_GOT32: | |
1019 | /* Relocation is to the entry for this symbol in the global | |
1020 | offset table. */ | |
1021 | if (sgot == NULL) | |
1022 | { | |
1023 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1024 | BFD_ASSERT (sgot != NULL); | |
1025 | } | |
1026 | ||
1027 | if (h != NULL) | |
1028 | { | |
1029 | BFD_ASSERT (h->got_offset != (bfd_vma) -1); | |
1030 | relocation = sgot->output_offset + h->got_offset; | |
1031 | } | |
1032 | else | |
1033 | { | |
1034 | bfd_vma off; | |
1035 | ||
1036 | BFD_ASSERT (local_got_offsets != NULL | |
1037 | && local_got_offsets[r_symndx] != (bfd_vma) -1); | |
1038 | ||
1039 | off = local_got_offsets[r_symndx]; | |
1040 | ||
1041 | /* The offset must always be a multiple of 4. We use | |
1042 | the least significant bit to record whether we have | |
1043 | already generated the necessary reloc. */ | |
1044 | if ((off & 1) != 0) | |
1045 | off &= ~1; | |
1046 | else | |
1047 | { | |
1048 | asection *srelgot; | |
1049 | Elf_Internal_Rel outrel; | |
1050 | ||
1051 | bfd_put_32 (output_bfd, relocation, sgot->contents + off); | |
1052 | ||
1053 | srelgot = bfd_get_section_by_name (dynobj, ".rel.got"); | |
1054 | BFD_ASSERT (srelgot != NULL); | |
1055 | ||
1056 | outrel.r_offset = (sgot->output_section->vma | |
1057 | + sgot->output_offset | |
1058 | + off); | |
1059 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
1060 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, | |
1061 | (((Elf32_External_Rel *) | |
1062 | srelgot->contents) | |
1063 | + srelgot->reloc_count)); | |
1064 | ++srelgot->reloc_count; | |
1065 | ||
1066 | local_got_offsets[r_symndx] |= 1; | |
1067 | } | |
1068 | ||
1069 | relocation = sgot->output_offset + off; | |
1070 | } | |
1071 | ||
1072 | break; | |
1073 | ||
1074 | case R_386_GOTOFF: | |
1075 | /* Relocation is relative to the start of the global offset | |
1076 | table. */ | |
1077 | ||
1078 | if (sgot == NULL) | |
1079 | { | |
1080 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1081 | BFD_ASSERT (sgot != NULL); | |
1082 | } | |
1083 | ||
1084 | /* Note that sgot->output_offset is not involved in this | |
1085 | calculation. We always want the start of .got. If we | |
1086 | defined _GLOBAL_OFFSET_TABLE in a different way, as is | |
1087 | permitted by the ABI, we might have to change this | |
1088 | calculation. */ | |
1089 | relocation -= sgot->output_section->vma; | |
1090 | ||
1091 | break; | |
1092 | ||
1093 | case R_386_GOTPC: | |
1094 | /* Use global offset table as symbol value. */ | |
1095 | ||
1096 | if (sgot == NULL) | |
1097 | { | |
1098 | sgot = bfd_get_section_by_name (dynobj, ".got"); | |
1099 | BFD_ASSERT (sgot != NULL); | |
1100 | } | |
1101 | ||
1102 | relocation = sgot->output_section->vma; | |
1103 | ||
1104 | break; | |
1105 | ||
1106 | case R_386_PLT32: | |
1107 | /* Relocation is to the entry for this symbol in the | |
1108 | procedure linkage table. */ | |
1109 | ||
1110 | /* Resolve a PLT32 reloc again a local symbol directly, | |
1111 | without using the procedure linkage table. */ | |
1112 | if (h == NULL) | |
1113 | break; | |
1114 | ||
1115 | if (splt == NULL) | |
1116 | { | |
1117 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1118 | BFD_ASSERT (splt != NULL); | |
1119 | } | |
1120 | ||
1121 | BFD_ASSERT (h != NULL && h->plt_offset != (bfd_vma) -1); | |
1122 | relocation = (splt->output_section->vma | |
1123 | + splt->output_offset | |
1124 | + h->plt_offset); | |
1125 | ||
1126 | break; | |
1127 | ||
1128 | case R_386_32: | |
1129 | case R_386_PC32: | |
1130 | if (info->shared | |
1131 | && (input_section->flags & SEC_ALLOC) != 0) | |
1132 | { | |
1133 | Elf_Internal_Rel outrel; | |
1134 | ||
1135 | /* When generating a shared object, these relocations | |
1136 | are copied into the output file to be resolved at run | |
1137 | time. */ | |
1138 | ||
1139 | if (sreloc == NULL) | |
1140 | { | |
1141 | const char *name; | |
1142 | ||
1143 | name = (elf_string_from_elf_section | |
1144 | (input_bfd, | |
1145 | elf_elfheader (input_bfd)->e_shstrndx, | |
1146 | elf_section_data (input_section)->rel_hdr.sh_name)); | |
1147 | if (name == NULL) | |
1148 | return false; | |
1149 | ||
1150 | BFD_ASSERT (strncmp (name, ".rel", 4) == 0 | |
1151 | && strcmp (bfd_get_section_name (input_bfd, | |
1152 | input_section), | |
1153 | name + 4) == 0); | |
1154 | ||
1155 | sreloc = bfd_get_section_by_name (dynobj, name); | |
1156 | BFD_ASSERT (sreloc != NULL); | |
1157 | } | |
1158 | ||
1159 | outrel.r_offset = (rel->r_offset | |
1160 | + input_section->output_section->vma | |
1161 | + input_section->output_offset); | |
1162 | if (r_type == R_386_PC32) | |
1163 | { | |
1164 | BFD_ASSERT (h != NULL && h->dynindx != (bfd_vma) -1); | |
1165 | outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_PC32); | |
1166 | } | |
1167 | else | |
1168 | { | |
1169 | if (h == NULL) | |
1170 | outrel.r_info = ELF32_R_INFO (0, R_386_RELATIVE); | |
1171 | else | |
1172 | { | |
1173 | BFD_ASSERT (h->dynindx != (bfd_vma) -1); | |
1174 | outrel.r_info = ELF32_R_INFO (h->dynindx, R_386_32); | |
1175 | } | |
1176 | } | |
1177 | ||
1178 | bfd_elf32_swap_reloc_out (output_bfd, &outrel, | |
1179 | (((Elf32_External_Rel *) | |
1180 | sreloc->contents) | |
1181 | + sreloc->reloc_count)); | |
1182 | ++sreloc->reloc_count; | |
1183 | ||
1184 | /* If this reloc is against an external symbol, we do | |
1185 | not want to fiddle with the addend. Otherwise, we | |
1186 | need to include the symbol value so that it becomes | |
1187 | an addend for the dynamic reloc. */ | |
1188 | if (h != NULL) | |
1189 | continue; | |
1190 | } | |
1191 | ||
1192 | break; | |
1193 | ||
1194 | default: | |
1195 | break; | |
1196 | } | |
1197 | ||
013dec1a ILT |
1198 | r = _bfd_final_link_relocate (howto, input_bfd, input_section, |
1199 | contents, rel->r_offset, | |
1200 | relocation, (bfd_vma) 0); | |
1201 | ||
1202 | if (r != bfd_reloc_ok) | |
1203 | { | |
1204 | switch (r) | |
1205 | { | |
1206 | default: | |
1207 | case bfd_reloc_outofrange: | |
1208 | abort (); | |
1209 | case bfd_reloc_overflow: | |
1210 | { | |
1211 | const char *name; | |
1212 | ||
1213 | if (h != NULL) | |
1214 | name = h->root.root.string; | |
1215 | else | |
1216 | { | |
eb4267a3 ILT |
1217 | name = elf_string_from_elf_section (input_bfd, |
1218 | symtab_hdr->sh_link, | |
1219 | sym->st_name); | |
013dec1a ILT |
1220 | if (name == NULL) |
1221 | return false; | |
1222 | if (*name == '\0') | |
1223 | name = bfd_section_name (input_bfd, sec); | |
1224 | } | |
1225 | if (! ((*info->callbacks->reloc_overflow) | |
1226 | (info, name, howto->name, (bfd_vma) 0, | |
1227 | input_bfd, input_section, rel->r_offset))) | |
1228 | return false; | |
1229 | } | |
1230 | break; | |
1231 | } | |
1232 | } | |
1233 | } | |
1234 | ||
1235 | return true; | |
1236 | } | |
1237 | ||
1238 | /* Finish up dynamic symbol handling. We set the contents of various | |
1239 | dynamic sections here. */ | |
1240 | ||
1241 | static boolean | |
1242 | elf_i386_finish_dynamic_symbol (output_bfd, info, h, sym) | |
1243 | bfd *output_bfd; | |
1244 | struct bfd_link_info *info; | |
1245 | struct elf_link_hash_entry *h; | |
1246 | Elf_Internal_Sym *sym; | |
1247 | { | |
eb4267a3 | 1248 | bfd *dynobj; |
013dec1a | 1249 | |
eb4267a3 | 1250 | dynobj = elf_hash_table (info)->dynobj; |
013dec1a | 1251 | |
eb4267a3 | 1252 | if (h->plt_offset != (bfd_vma) -1) |
013dec1a ILT |
1253 | { |
1254 | asection *splt; | |
1255 | asection *sgot; | |
1256 | asection *srel; | |
1257 | bfd_vma plt_index; | |
1258 | bfd_vma got_offset; | |
1259 | Elf_Internal_Rel rel; | |
1260 | ||
eb4267a3 ILT |
1261 | /* This symbol has an entry in the procedure linkage table. Set |
1262 | it up. */ | |
013dec1a | 1263 | |
eb4267a3 ILT |
1264 | BFD_ASSERT (h->dynindx != -1); |
1265 | ||
1266 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1267 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1268 | srel = bfd_get_section_by_name (dynobj, ".rel.plt"); | |
1269 | BFD_ASSERT (splt != NULL && sgot != NULL && srel != NULL); | |
013dec1a ILT |
1270 | |
1271 | /* Get the index in the procedure linkage table which | |
1272 | corresponds to this symbol. This is the index of this symbol | |
1273 | in all the symbols for which we are making plt entries. The | |
1274 | first entry in the procedure linkage table is reserved. */ | |
eb4267a3 | 1275 | plt_index = h->plt_offset / PLT_ENTRY_SIZE - 1; |
013dec1a ILT |
1276 | |
1277 | /* Get the offset into the .got table of the entry that | |
1278 | corresponds to this function. Each .got entry is 4 bytes. | |
1279 | The first three are reserved. */ | |
1280 | got_offset = (plt_index + 3) * 4; | |
1281 | ||
1282 | /* Fill in the entry in the procedure linkage table. */ | |
eb4267a3 ILT |
1283 | if (! info->shared) |
1284 | { | |
1285 | memcpy (splt->contents + h->plt_offset, elf_i386_plt_entry, | |
1286 | PLT_ENTRY_SIZE); | |
1287 | bfd_put_32 (output_bfd, | |
1288 | (sgot->output_section->vma | |
1289 | + sgot->output_offset | |
1290 | + got_offset), | |
1291 | splt->contents + h->plt_offset + 2); | |
1292 | } | |
1293 | else | |
1294 | { | |
1295 | memcpy (splt->contents + h->plt_offset, elf_i386_pic_plt_entry, | |
1296 | PLT_ENTRY_SIZE); | |
1297 | bfd_put_32 (output_bfd, got_offset, | |
1298 | splt->contents + h->plt_offset + 2); | |
1299 | } | |
1300 | ||
013dec1a | 1301 | bfd_put_32 (output_bfd, plt_index * sizeof (Elf32_External_Rel), |
eb4267a3 ILT |
1302 | splt->contents + h->plt_offset + 7); |
1303 | bfd_put_32 (output_bfd, - (h->plt_offset + PLT_ENTRY_SIZE), | |
1304 | splt->contents + h->plt_offset + 12); | |
013dec1a ILT |
1305 | |
1306 | /* Fill in the entry in the global offset table. */ | |
1307 | bfd_put_32 (output_bfd, | |
1308 | (splt->output_section->vma | |
1309 | + splt->output_offset | |
eb4267a3 | 1310 | + h->plt_offset |
013dec1a ILT |
1311 | + 6), |
1312 | sgot->contents + got_offset); | |
1313 | ||
1314 | /* Fill in the entry in the .rel.plt section. */ | |
1315 | rel.r_offset = (sgot->output_section->vma | |
1316 | + sgot->output_offset | |
1317 | + got_offset); | |
1318 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_JUMP_SLOT); | |
1319 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1320 | ((Elf32_External_Rel *) srel->contents | |
1321 | + plt_index)); | |
1322 | ||
eb4267a3 ILT |
1323 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) == 0) |
1324 | { | |
1325 | /* Mark the symbol as undefined, rather than as defined in | |
1326 | the .plt section. Leave the value alone. */ | |
1327 | sym->st_shndx = SHN_UNDEF; | |
1328 | } | |
013dec1a | 1329 | } |
eb4267a3 ILT |
1330 | |
1331 | if (h->got_offset != (bfd_vma) -1) | |
013dec1a | 1332 | { |
eb4267a3 ILT |
1333 | asection *sgot; |
1334 | asection *srel; | |
1335 | Elf_Internal_Rel rel; | |
013dec1a | 1336 | |
eb4267a3 ILT |
1337 | /* This symbol has an entry in the global offset table. Set it |
1338 | up. */ | |
1339 | ||
1340 | BFD_ASSERT (h->dynindx != -1); | |
013dec1a | 1341 | |
eb4267a3 ILT |
1342 | sgot = bfd_get_section_by_name (dynobj, ".got"); |
1343 | srel = bfd_get_section_by_name (dynobj, ".rel.got"); | |
1344 | BFD_ASSERT (sgot != NULL && srel != NULL); | |
1345 | ||
1346 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got_offset); | |
1347 | ||
1348 | rel.r_offset = (sgot->output_section->vma | |
1349 | + sgot->output_offset | |
1350 | + h->got_offset); | |
1351 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_GLOB_DAT); | |
1352 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1353 | ((Elf32_External_Rel *) srel->contents | |
1354 | + srel->reloc_count)); | |
1355 | ++srel->reloc_count; | |
1356 | } | |
1357 | ||
1358 | if ((h->elf_link_hash_flags & ELF_LINK_HASH_NEEDS_COPY) != 0) | |
1359 | { | |
1360 | asection *s; | |
1361 | Elf_Internal_Rel rel; | |
1362 | ||
1363 | /* This symbol needs a copy reloc. Set it up. */ | |
1364 | ||
1365 | BFD_ASSERT (h->dynindx != -1 | |
1366 | && h->root.type == bfd_link_hash_defined); | |
1367 | ||
1368 | s = bfd_get_section_by_name (h->root.u.def.section->owner, | |
1369 | ".rel.bss"); | |
1370 | BFD_ASSERT (s != NULL); | |
1371 | ||
1372 | rel.r_offset = (h->root.u.def.value | |
1373 | + h->root.u.def.section->output_section->vma | |
1374 | + h->root.u.def.section->output_offset); | |
1375 | rel.r_info = ELF32_R_INFO (h->dynindx, R_386_COPY); | |
1376 | bfd_elf32_swap_reloc_out (output_bfd, &rel, | |
1377 | ((Elf32_External_Rel *) s->contents | |
1378 | + s->reloc_count)); | |
1379 | ++s->reloc_count; | |
013dec1a ILT |
1380 | } |
1381 | ||
eb4267a3 ILT |
1382 | /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute. */ |
1383 | if (strcmp (h->root.root.string, "_DYNAMIC") == 0 | |
1384 | || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0) | |
1385 | sym->st_shndx = SHN_ABS; | |
1386 | ||
013dec1a ILT |
1387 | return true; |
1388 | } | |
1389 | ||
1390 | /* Finish up the dynamic sections. */ | |
1391 | ||
1392 | static boolean | |
1393 | elf_i386_finish_dynamic_sections (output_bfd, info) | |
1394 | bfd *output_bfd; | |
1395 | struct bfd_link_info *info; | |
1396 | { | |
eb4267a3 | 1397 | bfd *dynobj; |
013dec1a ILT |
1398 | asection *splt; |
1399 | asection *sgot; | |
1400 | asection *sdyn; | |
1401 | Elf32_External_Dyn *dyncon, *dynconend; | |
1402 | ||
eb4267a3 ILT |
1403 | dynobj = elf_hash_table (info)->dynobj; |
1404 | ||
1405 | splt = bfd_get_section_by_name (dynobj, ".plt"); | |
1406 | sgot = bfd_get_section_by_name (dynobj, ".got.plt"); | |
1407 | sdyn = bfd_get_section_by_name (dynobj, ".dynamic"); | |
013dec1a ILT |
1408 | BFD_ASSERT (splt != NULL && sgot != NULL && sdyn != NULL); |
1409 | ||
1410 | dyncon = (Elf32_External_Dyn *) sdyn->contents; | |
1411 | dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size); | |
1412 | for (; dyncon < dynconend; dyncon++) | |
1413 | { | |
1414 | Elf_Internal_Dyn dyn; | |
1415 | const char *name; | |
eb4267a3 | 1416 | asection *s; |
013dec1a | 1417 | |
eb4267a3 | 1418 | bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn); |
013dec1a ILT |
1419 | |
1420 | switch (dyn.d_tag) | |
1421 | { | |
eb4267a3 ILT |
1422 | default: |
1423 | break; | |
1424 | ||
1425 | case DT_PLTGOT: | |
1426 | name = ".got"; | |
1427 | goto get_vma; | |
1428 | case DT_JMPREL: | |
1429 | name = ".rel.plt"; | |
1430 | get_vma: | |
013dec1a ILT |
1431 | s = bfd_get_section_by_name (output_bfd, name); |
1432 | BFD_ASSERT (s != NULL); | |
eb4267a3 ILT |
1433 | dyn.d_un.d_ptr = s->vma; |
1434 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1435 | break; | |
1436 | ||
1437 | case DT_PLTRELSZ: | |
1438 | s = bfd_get_section_by_name (output_bfd, ".rel.plt"); | |
1439 | BFD_ASSERT (s != NULL); | |
1440 | if (s->_cooked_size != 0) | |
1441 | dyn.d_un.d_val = s->_cooked_size; | |
013dec1a | 1442 | else |
eb4267a3 ILT |
1443 | dyn.d_un.d_val = s->_raw_size; |
1444 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
1445 | break; | |
1446 | ||
1447 | case DT_RELSZ: | |
1448 | /* My reading of the SVR4 ABI indicates that the procedure | |
1449 | linkage table relocs (DT_JMPREL) should be included in | |
1450 | the overall relocs (DT_REL). This is what Solaris does. | |
1451 | However, UnixWare can not handle that case. Therefore, | |
1452 | we override the DT_RELSZ entry here to make it not | |
1453 | include the JMPREL relocs. Since the linker script | |
1454 | arranges for .rel.plt to follow all other relocation | |
1455 | sections, we don't have to worry about changing the | |
1456 | DT_REL entry. */ | |
1457 | s = bfd_get_section_by_name (output_bfd, ".rel.plt"); | |
1458 | if (s != NULL) | |
013dec1a ILT |
1459 | { |
1460 | if (s->_cooked_size != 0) | |
eb4267a3 | 1461 | dyn.d_un.d_val -= s->_cooked_size; |
013dec1a | 1462 | else |
eb4267a3 | 1463 | dyn.d_un.d_val -= s->_raw_size; |
013dec1a ILT |
1464 | } |
1465 | bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon); | |
eb4267a3 | 1466 | break; |
013dec1a ILT |
1467 | } |
1468 | } | |
1469 | ||
1470 | /* Fill in the first entry in the procedure linkage table. */ | |
1471 | if (splt->_raw_size > 0) | |
1472 | { | |
eb4267a3 ILT |
1473 | if (info->shared) |
1474 | memcpy (splt->contents, elf_i386_pic_plt0_entry, PLT_ENTRY_SIZE); | |
1475 | else | |
1476 | { | |
1477 | memcpy (splt->contents, elf_i386_plt0_entry, PLT_ENTRY_SIZE); | |
1478 | bfd_put_32 (output_bfd, | |
1479 | sgot->output_section->vma + sgot->output_offset + 4, | |
1480 | splt->contents + 2); | |
1481 | bfd_put_32 (output_bfd, | |
1482 | sgot->output_section->vma + sgot->output_offset + 8, | |
1483 | splt->contents + 8); | |
1484 | } | |
013dec1a ILT |
1485 | } |
1486 | ||
1487 | /* Fill in the first three entries in the global offset table. */ | |
1488 | if (sgot->_raw_size > 0) | |
1489 | { | |
1490 | bfd_put_32 (output_bfd, | |
1491 | sdyn->output_section->vma + sdyn->output_offset, | |
1492 | sgot->contents); | |
1493 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4); | |
1494 | bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8); | |
1495 | } | |
1496 | ||
1497 | elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4; | |
1498 | ||
1499 | /* UnixWare sets the entsize of .plt to 4, although that doesn't | |
1500 | really seem like the right value. */ | |
1501 | elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4; | |
1502 | ||
1503 | return true; | |
1504 | } | |
e4b6b3e7 ILT |
1505 | |
1506 | #define TARGET_LITTLE_SYM bfd_elf32_i386_vec | |
1507 | #define TARGET_LITTLE_NAME "elf32-i386" | |
1508 | #define ELF_ARCH bfd_arch_i386 | |
68241b2b | 1509 | #define ELF_MACHINE_CODE EM_386 |
e4b6b3e7 ILT |
1510 | #define elf_info_to_howto elf_i386_info_to_howto |
1511 | #define elf_info_to_howto_rel elf_i386_info_to_howto_rel | |
1512 | #define bfd_elf32_bfd_reloc_type_lookup elf_i386_reloc_type_lookup | |
68241b2b | 1513 | #define ELF_MAXPAGESIZE 0x1000 |
013dec1a ILT |
1514 | #define elf_backend_create_dynamic_sections \ |
1515 | elf_i386_create_dynamic_sections | |
eb4267a3 | 1516 | #define elf_backend_check_relocs elf_i386_check_relocs |
013dec1a ILT |
1517 | #define elf_backend_adjust_dynamic_symbol \ |
1518 | elf_i386_adjust_dynamic_symbol | |
1519 | #define elf_backend_size_dynamic_sections \ | |
1520 | elf_i386_size_dynamic_sections | |
1521 | #define elf_backend_relocate_section elf_i386_relocate_section | |
1522 | #define elf_backend_finish_dynamic_symbol \ | |
1523 | elf_i386_finish_dynamic_symbol | |
1524 | #define elf_backend_finish_dynamic_sections \ | |
1525 | elf_i386_finish_dynamic_sections | |
e4b6b3e7 ILT |
1526 | |
1527 | #include "elf32-target.h" |