* i386.cc (Target_i386::got_section): If -z now, make .got.plt a
[deliverable/binutils-gdb.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
5
6 // This file is part of gold.
7
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
12
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
22
23 #include "gold.h"
24
25 #include <cstring>
26
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "i386.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "freebsd.h"
42 #include "gc.h"
43
44 namespace
45 {
46
47 using namespace gold;
48
49 // A class to handle the PLT data.
50
51 class Output_data_plt_i386 : public Output_section_data
52 {
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
55
56 Output_data_plt_i386(Layout*, Output_data_space*, Output_data_space*);
57
58 // Add an entry to the PLT.
59 void
60 add_entry(Symbol_table*, Layout*, Symbol* gsym);
61
62 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
63 unsigned int
64 add_local_ifunc_entry(Symbol_table*, Layout*,
65 Sized_relobj_file<32, false>* relobj,
66 unsigned int local_sym_index);
67
68 // Return the .rel.plt section data.
69 Reloc_section*
70 rel_plt() const
71 { return this->rel_; }
72
73 // Return where the TLS_DESC relocations should go.
74 Reloc_section*
75 rel_tls_desc(Layout*);
76
77 // Return where the IRELATIVE relocations should go.
78 Reloc_section*
79 rel_irelative(Symbol_table*, Layout*);
80
81 // Return whether we created a section for IRELATIVE relocations.
82 bool
83 has_irelative_section() const
84 { return this->irelative_rel_ != NULL; }
85
86 // Return the number of PLT entries.
87 unsigned int
88 entry_count() const
89 { return this->count_ + this->irelative_count_; }
90
91 // Return the offset of the first non-reserved PLT entry.
92 static unsigned int
93 first_plt_entry_offset()
94 { return plt_entry_size; }
95
96 // Return the size of a PLT entry.
97 static unsigned int
98 get_plt_entry_size()
99 { return plt_entry_size; }
100
101 // Return the PLT address to use for a global symbol.
102 uint64_t
103 address_for_global(const Symbol*);
104
105 // Return the PLT address to use for a local symbol.
106 uint64_t
107 address_for_local(const Relobj*, unsigned int symndx);
108
109 protected:
110 void
111 do_adjust_output_section(Output_section* os);
112
113 // Write to a map file.
114 void
115 do_print_to_mapfile(Mapfile* mapfile) const
116 { mapfile->print_output_data(this, _("** PLT")); }
117
118 private:
119 // The size of an entry in the PLT.
120 static const int plt_entry_size = 16;
121
122 // The first entry in the PLT for an executable.
123 static const unsigned char exec_first_plt_entry[plt_entry_size];
124
125 // The first entry in the PLT for a shared object.
126 static const unsigned char dyn_first_plt_entry[plt_entry_size];
127
128 // Other entries in the PLT for an executable.
129 static const unsigned char exec_plt_entry[plt_entry_size];
130
131 // Other entries in the PLT for a shared object.
132 static const unsigned char dyn_plt_entry[plt_entry_size];
133
134 // The .eh_frame unwind information for the PLT.
135 static const int plt_eh_frame_cie_size = 16;
136 static const int plt_eh_frame_fde_size = 32;
137 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
138 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
139
140 // Set the final size.
141 void
142 set_final_data_size()
143 {
144 this->set_data_size((this->count_ + this->irelative_count_ + 1)
145 * plt_entry_size);
146 }
147
148 // Write out the PLT data.
149 void
150 do_write(Output_file*);
151
152 // We keep a list of global STT_GNU_IFUNC symbols, each with its
153 // offset in the GOT.
154 struct Global_ifunc
155 {
156 Symbol* sym;
157 unsigned int got_offset;
158 };
159
160 // We keep a list of local STT_GNU_IFUNC symbols, each with its
161 // offset in the GOT.
162 struct Local_ifunc
163 {
164 Sized_relobj_file<32, false>* object;
165 unsigned int local_sym_index;
166 unsigned int got_offset;
167 };
168
169 // The reloc section.
170 Reloc_section* rel_;
171 // The TLS_DESC relocations, if necessary. These must follow the
172 // regular PLT relocs.
173 Reloc_section* tls_desc_rel_;
174 // The IRELATIVE relocations, if necessary. These must follow the
175 // regular relocatoins and the TLS_DESC relocations.
176 Reloc_section* irelative_rel_;
177 // The .got.plt section.
178 Output_data_space* got_plt_;
179 // The part of the .got.plt section used for IRELATIVE relocs.
180 Output_data_space* got_irelative_;
181 // The number of PLT entries.
182 unsigned int count_;
183 // Number of PLT entries with R_386_IRELATIVE relocs. These follow
184 // the regular PLT entries.
185 unsigned int irelative_count_;
186 // Global STT_GNU_IFUNC symbols.
187 std::vector<Global_ifunc> global_ifuncs_;
188 // Local STT_GNU_IFUNC symbols.
189 std::vector<Local_ifunc> local_ifuncs_;
190 };
191
192 // The i386 target class.
193 // TLS info comes from
194 // http://people.redhat.com/drepper/tls.pdf
195 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
196
197 class Target_i386 : public Sized_target<32, false>
198 {
199 public:
200 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
201
202 Target_i386()
203 : Sized_target<32, false>(&i386_info),
204 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
205 got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
206 rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
207 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
208 { }
209
210 // Process the relocations to determine unreferenced sections for
211 // garbage collection.
212 void
213 gc_process_relocs(Symbol_table* symtab,
214 Layout* layout,
215 Sized_relobj_file<32, false>* object,
216 unsigned int data_shndx,
217 unsigned int sh_type,
218 const unsigned char* prelocs,
219 size_t reloc_count,
220 Output_section* output_section,
221 bool needs_special_offset_handling,
222 size_t local_symbol_count,
223 const unsigned char* plocal_symbols);
224
225 // Scan the relocations to look for symbol adjustments.
226 void
227 scan_relocs(Symbol_table* symtab,
228 Layout* layout,
229 Sized_relobj_file<32, false>* object,
230 unsigned int data_shndx,
231 unsigned int sh_type,
232 const unsigned char* prelocs,
233 size_t reloc_count,
234 Output_section* output_section,
235 bool needs_special_offset_handling,
236 size_t local_symbol_count,
237 const unsigned char* plocal_symbols);
238
239 // Finalize the sections.
240 void
241 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
242
243 // Return the value to use for a dynamic which requires special
244 // treatment.
245 uint64_t
246 do_dynsym_value(const Symbol*) const;
247
248 // Relocate a section.
249 void
250 relocate_section(const Relocate_info<32, false>*,
251 unsigned int sh_type,
252 const unsigned char* prelocs,
253 size_t reloc_count,
254 Output_section* output_section,
255 bool needs_special_offset_handling,
256 unsigned char* view,
257 elfcpp::Elf_types<32>::Elf_Addr view_address,
258 section_size_type view_size,
259 const Reloc_symbol_changes*);
260
261 // Scan the relocs during a relocatable link.
262 void
263 scan_relocatable_relocs(Symbol_table* symtab,
264 Layout* layout,
265 Sized_relobj_file<32, false>* object,
266 unsigned int data_shndx,
267 unsigned int sh_type,
268 const unsigned char* prelocs,
269 size_t reloc_count,
270 Output_section* output_section,
271 bool needs_special_offset_handling,
272 size_t local_symbol_count,
273 const unsigned char* plocal_symbols,
274 Relocatable_relocs*);
275
276 // Relocate a section during a relocatable link.
277 void
278 relocate_for_relocatable(const Relocate_info<32, false>*,
279 unsigned int sh_type,
280 const unsigned char* prelocs,
281 size_t reloc_count,
282 Output_section* output_section,
283 off_t offset_in_output_section,
284 const Relocatable_relocs*,
285 unsigned char* view,
286 elfcpp::Elf_types<32>::Elf_Addr view_address,
287 section_size_type view_size,
288 unsigned char* reloc_view,
289 section_size_type reloc_view_size);
290
291 // Return a string used to fill a code section with nops.
292 std::string
293 do_code_fill(section_size_type length) const;
294
295 // Return whether SYM is defined by the ABI.
296 bool
297 do_is_defined_by_abi(const Symbol* sym) const
298 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
299
300 // Return whether a symbol name implies a local label. The UnixWare
301 // 2.1 cc generates temporary symbols that start with .X, so we
302 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
303 // If so, we should move the .X recognition into
304 // Target::do_is_local_label_name.
305 bool
306 do_is_local_label_name(const char* name) const
307 {
308 if (name[0] == '.' && name[1] == 'X')
309 return true;
310 return Target::do_is_local_label_name(name);
311 }
312
313 // Return the PLT address to use for a global symbol.
314 uint64_t
315 do_plt_address_for_global(const Symbol* gsym) const
316 { return this->plt_section()->address_for_global(gsym); }
317
318 uint64_t
319 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
320 { return this->plt_section()->address_for_local(relobj, symndx); }
321
322 // We can tell whether we take the address of a function.
323 inline bool
324 do_can_check_for_function_pointers() const
325 { return true; }
326
327 // Return the base for a DW_EH_PE_datarel encoding.
328 uint64_t
329 do_ehframe_datarel_base() const;
330
331 // Return whether SYM is call to a non-split function.
332 bool
333 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
334
335 // Adjust -fsplit-stack code which calls non-split-stack code.
336 void
337 do_calls_non_split(Relobj* object, unsigned int shndx,
338 section_offset_type fnoffset, section_size_type fnsize,
339 unsigned char* view, section_size_type view_size,
340 std::string* from, std::string* to) const;
341
342 // Return the size of the GOT section.
343 section_size_type
344 got_size() const
345 {
346 gold_assert(this->got_ != NULL);
347 return this->got_->data_size();
348 }
349
350 // Return the number of entries in the GOT.
351 unsigned int
352 got_entry_count() const
353 {
354 if (this->got_ == NULL)
355 return 0;
356 return this->got_size() / 4;
357 }
358
359 // Return the number of entries in the PLT.
360 unsigned int
361 plt_entry_count() const;
362
363 // Return the offset of the first non-reserved PLT entry.
364 unsigned int
365 first_plt_entry_offset() const;
366
367 // Return the size of each PLT entry.
368 unsigned int
369 plt_entry_size() const;
370
371 private:
372 // The class which scans relocations.
373 struct Scan
374 {
375 static inline int
376
377 get_reference_flags(unsigned int r_type);
378
379 inline void
380 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
381 Sized_relobj_file<32, false>* object,
382 unsigned int data_shndx,
383 Output_section* output_section,
384 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
385 const elfcpp::Sym<32, false>& lsym);
386
387 inline void
388 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
389 Sized_relobj_file<32, false>* object,
390 unsigned int data_shndx,
391 Output_section* output_section,
392 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
393 Symbol* gsym);
394
395 inline bool
396 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
397 Target_i386* target,
398 Sized_relobj_file<32, false>* object,
399 unsigned int data_shndx,
400 Output_section* output_section,
401 const elfcpp::Rel<32, false>& reloc,
402 unsigned int r_type,
403 const elfcpp::Sym<32, false>& lsym);
404
405 inline bool
406 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
407 Target_i386* target,
408 Sized_relobj_file<32, false>* object,
409 unsigned int data_shndx,
410 Output_section* output_section,
411 const elfcpp::Rel<32, false>& reloc,
412 unsigned int r_type,
413 Symbol* gsym);
414
415 inline bool
416 possible_function_pointer_reloc(unsigned int r_type);
417
418 bool
419 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
420 unsigned int r_type);
421
422 static void
423 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
424
425 static void
426 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
427 Symbol*);
428 };
429
430 // The class which implements relocation.
431 class Relocate
432 {
433 public:
434 Relocate()
435 : skip_call_tls_get_addr_(false),
436 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
437 { }
438
439 ~Relocate()
440 {
441 if (this->skip_call_tls_get_addr_)
442 {
443 // FIXME: This needs to specify the location somehow.
444 gold_error(_("missing expected TLS relocation"));
445 }
446 }
447
448 // Return whether the static relocation needs to be applied.
449 inline bool
450 should_apply_static_reloc(const Sized_symbol<32>* gsym,
451 unsigned int r_type,
452 bool is_32bit,
453 Output_section* output_section);
454
455 // Do a relocation. Return false if the caller should not issue
456 // any warnings about this relocation.
457 inline bool
458 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
459 size_t relnum, const elfcpp::Rel<32, false>&,
460 unsigned int r_type, const Sized_symbol<32>*,
461 const Symbol_value<32>*,
462 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
463 section_size_type);
464
465 private:
466 // Do a TLS relocation.
467 inline void
468 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
469 size_t relnum, const elfcpp::Rel<32, false>&,
470 unsigned int r_type, const Sized_symbol<32>*,
471 const Symbol_value<32>*,
472 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
473 section_size_type);
474
475 // Do a TLS General-Dynamic to Initial-Exec transition.
476 inline void
477 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
478 Output_segment* tls_segment,
479 const elfcpp::Rel<32, false>&, unsigned int r_type,
480 elfcpp::Elf_types<32>::Elf_Addr value,
481 unsigned char* view,
482 section_size_type view_size);
483
484 // Do a TLS General-Dynamic to Local-Exec transition.
485 inline void
486 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
487 Output_segment* tls_segment,
488 const elfcpp::Rel<32, false>&, unsigned int r_type,
489 elfcpp::Elf_types<32>::Elf_Addr value,
490 unsigned char* view,
491 section_size_type view_size);
492
493 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
494 // transition.
495 inline void
496 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
497 Output_segment* tls_segment,
498 const elfcpp::Rel<32, false>&, unsigned int r_type,
499 elfcpp::Elf_types<32>::Elf_Addr value,
500 unsigned char* view,
501 section_size_type view_size);
502
503 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
504 // transition.
505 inline void
506 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
507 Output_segment* tls_segment,
508 const elfcpp::Rel<32, false>&, unsigned int r_type,
509 elfcpp::Elf_types<32>::Elf_Addr value,
510 unsigned char* view,
511 section_size_type view_size);
512
513 // Do a TLS Local-Dynamic to Local-Exec transition.
514 inline void
515 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
516 Output_segment* tls_segment,
517 const elfcpp::Rel<32, false>&, unsigned int r_type,
518 elfcpp::Elf_types<32>::Elf_Addr value,
519 unsigned char* view,
520 section_size_type view_size);
521
522 // Do a TLS Initial-Exec to Local-Exec transition.
523 static inline void
524 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
525 Output_segment* tls_segment,
526 const elfcpp::Rel<32, false>&, unsigned int r_type,
527 elfcpp::Elf_types<32>::Elf_Addr value,
528 unsigned char* view,
529 section_size_type view_size);
530
531 // We need to keep track of which type of local dynamic relocation
532 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
533 enum Local_dynamic_type
534 {
535 LOCAL_DYNAMIC_NONE,
536 LOCAL_DYNAMIC_SUN,
537 LOCAL_DYNAMIC_GNU
538 };
539
540 // This is set if we should skip the next reloc, which should be a
541 // PLT32 reloc against ___tls_get_addr.
542 bool skip_call_tls_get_addr_;
543 // The type of local dynamic relocation we have seen in the section
544 // being relocated, if any.
545 Local_dynamic_type local_dynamic_type_;
546 };
547
548 // A class which returns the size required for a relocation type,
549 // used while scanning relocs during a relocatable link.
550 class Relocatable_size_for_reloc
551 {
552 public:
553 unsigned int
554 get_size_for_reloc(unsigned int, Relobj*);
555 };
556
557 // Adjust TLS relocation type based on the options and whether this
558 // is a local symbol.
559 static tls::Tls_optimization
560 optimize_tls_reloc(bool is_final, int r_type);
561
562 // Get the GOT section, creating it if necessary.
563 Output_data_got<32, false>*
564 got_section(Symbol_table*, Layout*);
565
566 // Get the GOT PLT section.
567 Output_data_space*
568 got_plt_section() const
569 {
570 gold_assert(this->got_plt_ != NULL);
571 return this->got_plt_;
572 }
573
574 // Get the GOT section for TLSDESC entries.
575 Output_data_got<32, false>*
576 got_tlsdesc_section() const
577 {
578 gold_assert(this->got_tlsdesc_ != NULL);
579 return this->got_tlsdesc_;
580 }
581
582 // Create the PLT section.
583 void
584 make_plt_section(Symbol_table* symtab, Layout* layout);
585
586 // Create a PLT entry for a global symbol.
587 void
588 make_plt_entry(Symbol_table*, Layout*, Symbol*);
589
590 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
591 void
592 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
593 Sized_relobj_file<32, false>* relobj,
594 unsigned int local_sym_index);
595
596 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
597 void
598 define_tls_base_symbol(Symbol_table*, Layout*);
599
600 // Create a GOT entry for the TLS module index.
601 unsigned int
602 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
603 Sized_relobj_file<32, false>* object);
604
605 // Get the PLT section.
606 Output_data_plt_i386*
607 plt_section() const
608 {
609 gold_assert(this->plt_ != NULL);
610 return this->plt_;
611 }
612
613 // Get the dynamic reloc section, creating it if necessary.
614 Reloc_section*
615 rel_dyn_section(Layout*);
616
617 // Get the section to use for TLS_DESC relocations.
618 Reloc_section*
619 rel_tls_desc_section(Layout*) const;
620
621 // Get the section to use for IRELATIVE relocations.
622 Reloc_section*
623 rel_irelative_section(Layout*);
624
625 // Add a potential copy relocation.
626 void
627 copy_reloc(Symbol_table* symtab, Layout* layout,
628 Sized_relobj_file<32, false>* object,
629 unsigned int shndx, Output_section* output_section,
630 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
631 {
632 this->copy_relocs_.copy_reloc(symtab, layout,
633 symtab->get_sized_symbol<32>(sym),
634 object, shndx, output_section, reloc,
635 this->rel_dyn_section(layout));
636 }
637
638 // Information about this specific target which we pass to the
639 // general Target structure.
640 static const Target::Target_info i386_info;
641
642 // The types of GOT entries needed for this platform.
643 // These values are exposed to the ABI in an incremental link.
644 // Do not renumber existing values without changing the version
645 // number of the .gnu_incremental_inputs section.
646 enum Got_type
647 {
648 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
649 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
650 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
651 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
652 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
653 };
654
655 // The GOT section.
656 Output_data_got<32, false>* got_;
657 // The PLT section.
658 Output_data_plt_i386* plt_;
659 // The GOT PLT section.
660 Output_data_space* got_plt_;
661 // The GOT section for IRELATIVE relocations.
662 Output_data_space* got_irelative_;
663 // The GOT section for TLSDESC relocations.
664 Output_data_got<32, false>* got_tlsdesc_;
665 // The _GLOBAL_OFFSET_TABLE_ symbol.
666 Symbol* global_offset_table_;
667 // The dynamic reloc section.
668 Reloc_section* rel_dyn_;
669 // The section to use for IRELATIVE relocs.
670 Reloc_section* rel_irelative_;
671 // Relocs saved to avoid a COPY reloc.
672 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
673 // Space for variables copied with a COPY reloc.
674 Output_data_space* dynbss_;
675 // Offset of the GOT entry for the TLS module index.
676 unsigned int got_mod_index_offset_;
677 // True if the _TLS_MODULE_BASE_ symbol has been defined.
678 bool tls_base_symbol_defined_;
679 };
680
681 const Target::Target_info Target_i386::i386_info =
682 {
683 32, // size
684 false, // is_big_endian
685 elfcpp::EM_386, // machine_code
686 false, // has_make_symbol
687 false, // has_resolve
688 true, // has_code_fill
689 true, // is_default_stack_executable
690 true, // can_icf_inline_merge_sections
691 '\0', // wrap_char
692 "/usr/lib/libc.so.1", // dynamic_linker
693 0x08048000, // default_text_segment_address
694 0x1000, // abi_pagesize (overridable by -z max-page-size)
695 0x1000, // common_pagesize (overridable by -z common-page-size)
696 elfcpp::SHN_UNDEF, // small_common_shndx
697 elfcpp::SHN_UNDEF, // large_common_shndx
698 0, // small_common_section_flags
699 0, // large_common_section_flags
700 NULL, // attributes_section
701 NULL // attributes_vendor
702 };
703
704 // Get the GOT section, creating it if necessary.
705
706 Output_data_got<32, false>*
707 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
708 {
709 if (this->got_ == NULL)
710 {
711 gold_assert(symtab != NULL && layout != NULL);
712
713 this->got_ = new Output_data_got<32, false>();
714
715 // When using -z now, we can treat .got.plt as a relro section.
716 // Without -z now, it is modified after program startup by lazy
717 // PLT relocations.
718 bool is_got_plt_relro = parameters->options().now();
719 Output_section_order got_order = (is_got_plt_relro
720 ? ORDER_RELRO
721 : ORDER_RELRO_LAST);
722 Output_section_order got_plt_order = (is_got_plt_relro
723 ? ORDER_RELRO
724 : ORDER_NON_RELRO_FIRST);
725
726 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
727 (elfcpp::SHF_ALLOC
728 | elfcpp::SHF_WRITE),
729 this->got_, got_order, true);
730
731 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
732 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
733 (elfcpp::SHF_ALLOC
734 | elfcpp::SHF_WRITE),
735 this->got_plt_, got_plt_order,
736 is_got_plt_relro);
737
738 // The first three entries are reserved.
739 this->got_plt_->set_current_data_size(3 * 4);
740
741 if (!is_got_plt_relro)
742 {
743 // Those bytes can go into the relro segment.
744 layout->increase_relro(3 * 4);
745 }
746
747 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
748 this->global_offset_table_ =
749 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
750 Symbol_table::PREDEFINED,
751 this->got_plt_,
752 0, 0, elfcpp::STT_OBJECT,
753 elfcpp::STB_LOCAL,
754 elfcpp::STV_HIDDEN, 0,
755 false, false);
756
757 // If there are any IRELATIVE relocations, they get GOT entries
758 // in .got.plt after the jump slot relocations.
759 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
760 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
761 (elfcpp::SHF_ALLOC
762 | elfcpp::SHF_WRITE),
763 this->got_irelative_,
764 got_plt_order, is_got_plt_relro);
765
766 // If there are any TLSDESC relocations, they get GOT entries in
767 // .got.plt after the jump slot entries.
768 this->got_tlsdesc_ = new Output_data_got<32, false>();
769 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
770 (elfcpp::SHF_ALLOC
771 | elfcpp::SHF_WRITE),
772 this->got_tlsdesc_,
773 got_plt_order, is_got_plt_relro);
774 }
775
776 return this->got_;
777 }
778
779 // Get the dynamic reloc section, creating it if necessary.
780
781 Target_i386::Reloc_section*
782 Target_i386::rel_dyn_section(Layout* layout)
783 {
784 if (this->rel_dyn_ == NULL)
785 {
786 gold_assert(layout != NULL);
787 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
788 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
789 elfcpp::SHF_ALLOC, this->rel_dyn_,
790 ORDER_DYNAMIC_RELOCS, false);
791 }
792 return this->rel_dyn_;
793 }
794
795 // Get the section to use for IRELATIVE relocs, creating it if
796 // necessary. These go in .rel.dyn, but only after all other dynamic
797 // relocations. They need to follow the other dynamic relocations so
798 // that they can refer to global variables initialized by those
799 // relocs.
800
801 Target_i386::Reloc_section*
802 Target_i386::rel_irelative_section(Layout* layout)
803 {
804 if (this->rel_irelative_ == NULL)
805 {
806 // Make sure we have already create the dynamic reloc section.
807 this->rel_dyn_section(layout);
808 this->rel_irelative_ = new Reloc_section(false);
809 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
810 elfcpp::SHF_ALLOC, this->rel_irelative_,
811 ORDER_DYNAMIC_RELOCS, false);
812 gold_assert(this->rel_dyn_->output_section()
813 == this->rel_irelative_->output_section());
814 }
815 return this->rel_irelative_;
816 }
817
818 // Create the PLT section. The ordinary .got section is an argument,
819 // since we need to refer to the start. We also create our own .got
820 // section just for PLT entries.
821
822 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
823 Output_data_space* got_plt,
824 Output_data_space* got_irelative)
825 : Output_section_data(16), tls_desc_rel_(NULL), irelative_rel_(NULL),
826 got_plt_(got_plt), got_irelative_(got_irelative), count_(0),
827 irelative_count_(0), global_ifuncs_(), local_ifuncs_()
828 {
829 this->rel_ = new Reloc_section(false);
830 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
831 elfcpp::SHF_ALLOC, this->rel_,
832 ORDER_DYNAMIC_PLT_RELOCS, false);
833
834 // Add unwind information if requested.
835 if (parameters->options().ld_generated_unwind_info())
836 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
837 plt_eh_frame_fde, plt_eh_frame_fde_size);
838 }
839
840 void
841 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
842 {
843 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
844 // linker, and so do we.
845 os->set_entsize(4);
846 }
847
848 // Add an entry to the PLT.
849
850 void
851 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
852 Symbol* gsym)
853 {
854 gold_assert(!gsym->has_plt_offset());
855
856 // Every PLT entry needs a reloc.
857 if (gsym->type() == elfcpp::STT_GNU_IFUNC
858 && gsym->can_use_relative_reloc(false))
859 {
860 gsym->set_plt_offset(this->irelative_count_ * plt_entry_size);
861 ++this->irelative_count_;
862 section_offset_type got_offset =
863 this->got_irelative_->current_data_size();
864 this->got_irelative_->set_current_data_size(got_offset + 4);
865 Reloc_section* rel = this->rel_irelative(symtab, layout);
866 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
867 this->got_irelative_, got_offset);
868 struct Global_ifunc gi;
869 gi.sym = gsym;
870 gi.got_offset = got_offset;
871 this->global_ifuncs_.push_back(gi);
872 }
873 else
874 {
875 // When setting the PLT offset we skip the initial reserved PLT
876 // entry.
877 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
878
879 ++this->count_;
880
881 section_offset_type got_offset = this->got_plt_->current_data_size();
882
883 // Every PLT entry needs a GOT entry which points back to the
884 // PLT entry (this will be changed by the dynamic linker,
885 // normally lazily when the function is called).
886 this->got_plt_->set_current_data_size(got_offset + 4);
887
888 gsym->set_needs_dynsym_entry();
889 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
890 got_offset);
891 }
892
893 // Note that we don't need to save the symbol. The contents of the
894 // PLT are independent of which symbols are used. The symbols only
895 // appear in the relocations.
896 }
897
898 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
899 // the PLT offset.
900
901 unsigned int
902 Output_data_plt_i386::add_local_ifunc_entry(
903 Symbol_table* symtab,
904 Layout* layout,
905 Sized_relobj_file<32, false>* relobj,
906 unsigned int local_sym_index)
907 {
908 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
909 ++this->irelative_count_;
910
911 section_offset_type got_offset = this->got_irelative_->current_data_size();
912
913 // Every PLT entry needs a GOT entry which points back to the PLT
914 // entry.
915 this->got_irelative_->set_current_data_size(got_offset + 4);
916
917 // Every PLT entry needs a reloc.
918 Reloc_section* rel = this->rel_irelative(symtab, layout);
919 rel->add_symbolless_local_addend(relobj, local_sym_index,
920 elfcpp::R_386_IRELATIVE,
921 this->got_irelative_, got_offset);
922
923 struct Local_ifunc li;
924 li.object = relobj;
925 li.local_sym_index = local_sym_index;
926 li.got_offset = got_offset;
927 this->local_ifuncs_.push_back(li);
928
929 return plt_offset;
930 }
931
932 // Return where the TLS_DESC relocations should go, creating it if
933 // necessary. These follow the JUMP_SLOT relocations.
934
935 Output_data_plt_i386::Reloc_section*
936 Output_data_plt_i386::rel_tls_desc(Layout* layout)
937 {
938 if (this->tls_desc_rel_ == NULL)
939 {
940 this->tls_desc_rel_ = new Reloc_section(false);
941 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
942 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
943 ORDER_DYNAMIC_PLT_RELOCS, false);
944 gold_assert(this->tls_desc_rel_->output_section()
945 == this->rel_->output_section());
946 }
947 return this->tls_desc_rel_;
948 }
949
950 // Return where the IRELATIVE relocations should go in the PLT. These
951 // follow the JUMP_SLOT and TLS_DESC relocations.
952
953 Output_data_plt_i386::Reloc_section*
954 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
955 {
956 if (this->irelative_rel_ == NULL)
957 {
958 // Make sure we have a place for the TLS_DESC relocations, in
959 // case we see any later on.
960 this->rel_tls_desc(layout);
961 this->irelative_rel_ = new Reloc_section(false);
962 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
963 elfcpp::SHF_ALLOC, this->irelative_rel_,
964 ORDER_DYNAMIC_PLT_RELOCS, false);
965 gold_assert(this->irelative_rel_->output_section()
966 == this->rel_->output_section());
967
968 if (parameters->doing_static_link())
969 {
970 // A statically linked executable will only have a .rel.plt
971 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
972 // symbols. The library will use these symbols to locate
973 // the IRELATIVE relocs at program startup time.
974 symtab->define_in_output_data("__rel_iplt_start", NULL,
975 Symbol_table::PREDEFINED,
976 this->irelative_rel_, 0, 0,
977 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
978 elfcpp::STV_HIDDEN, 0, false, true);
979 symtab->define_in_output_data("__rel_iplt_end", NULL,
980 Symbol_table::PREDEFINED,
981 this->irelative_rel_, 0, 0,
982 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
983 elfcpp::STV_HIDDEN, 0, true, true);
984 }
985 }
986 return this->irelative_rel_;
987 }
988
989 // Return the PLT address to use for a global symbol.
990
991 uint64_t
992 Output_data_plt_i386::address_for_global(const Symbol* gsym)
993 {
994 uint64_t offset = 0;
995 if (gsym->type() == elfcpp::STT_GNU_IFUNC
996 && gsym->can_use_relative_reloc(false))
997 offset = (this->count_ + 1) * plt_entry_size;
998 return this->address() + offset;
999 }
1000
1001 // Return the PLT address to use for a local symbol. These are always
1002 // IRELATIVE relocs.
1003
1004 uint64_t
1005 Output_data_plt_i386::address_for_local(const Relobj*, unsigned int)
1006 {
1007 return this->address() + (this->count_ + 1) * plt_entry_size;
1008 }
1009
1010 // The first entry in the PLT for an executable.
1011
1012 const unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
1013 {
1014 0xff, 0x35, // pushl contents of memory address
1015 0, 0, 0, 0, // replaced with address of .got + 4
1016 0xff, 0x25, // jmp indirect
1017 0, 0, 0, 0, // replaced with address of .got + 8
1018 0, 0, 0, 0 // unused
1019 };
1020
1021 // The first entry in the PLT for a shared object.
1022
1023 const unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
1024 {
1025 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1026 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1027 0, 0, 0, 0 // unused
1028 };
1029
1030 // Subsequent entries in the PLT for an executable.
1031
1032 const unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
1033 {
1034 0xff, 0x25, // jmp indirect
1035 0, 0, 0, 0, // replaced with address of symbol in .got
1036 0x68, // pushl immediate
1037 0, 0, 0, 0, // replaced with offset into relocation table
1038 0xe9, // jmp relative
1039 0, 0, 0, 0 // replaced with offset to start of .plt
1040 };
1041
1042 // Subsequent entries in the PLT for a shared object.
1043
1044 const unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
1045 {
1046 0xff, 0xa3, // jmp *offset(%ebx)
1047 0, 0, 0, 0, // replaced with offset of symbol in .got
1048 0x68, // pushl immediate
1049 0, 0, 0, 0, // replaced with offset into relocation table
1050 0xe9, // jmp relative
1051 0, 0, 0, 0 // replaced with offset to start of .plt
1052 };
1053
1054 // The .eh_frame unwind information for the PLT.
1055
1056 const unsigned char
1057 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1058 {
1059 1, // CIE version.
1060 'z', // Augmentation: augmentation size included.
1061 'R', // Augmentation: FDE encoding included.
1062 '\0', // End of augmentation string.
1063 1, // Code alignment factor.
1064 0x7c, // Data alignment factor.
1065 8, // Return address column.
1066 1, // Augmentation size.
1067 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1068 | elfcpp::DW_EH_PE_sdata4),
1069 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1070 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1071 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1072 elfcpp::DW_CFA_nop
1073 };
1074
1075 const unsigned char
1076 Output_data_plt_i386::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1077 {
1078 0, 0, 0, 0, // Replaced with offset to .plt.
1079 0, 0, 0, 0, // Replaced with size of .plt.
1080 0, // Augmentation size.
1081 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
1082 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1083 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
1084 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1085 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1086 11, // Block length.
1087 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
1088 elfcpp::DW_OP_breg8, 0, // Push %eip.
1089 elfcpp::DW_OP_lit15, // Push 0xf.
1090 elfcpp::DW_OP_and, // & (%eip & 0xf).
1091 elfcpp::DW_OP_lit11, // Push 0xb.
1092 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
1093 elfcpp::DW_OP_lit2, // Push 2.
1094 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
1095 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1096 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1097 elfcpp::DW_CFA_nop,
1098 elfcpp::DW_CFA_nop,
1099 elfcpp::DW_CFA_nop
1100 };
1101
1102 // Write out the PLT. This uses the hand-coded instructions above,
1103 // and adjusts them as needed. This is all specified by the i386 ELF
1104 // Processor Supplement.
1105
1106 void
1107 Output_data_plt_i386::do_write(Output_file* of)
1108 {
1109 const off_t offset = this->offset();
1110 const section_size_type oview_size =
1111 convert_to_section_size_type(this->data_size());
1112 unsigned char* const oview = of->get_output_view(offset, oview_size);
1113
1114 const off_t got_file_offset = this->got_plt_->offset();
1115 gold_assert(parameters->incremental_update()
1116 || (got_file_offset + this->got_plt_->data_size()
1117 == this->got_irelative_->offset()));
1118 const section_size_type got_size =
1119 convert_to_section_size_type(this->got_plt_->data_size()
1120 + this->got_irelative_->data_size());
1121 unsigned char* const got_view = of->get_output_view(got_file_offset,
1122 got_size);
1123
1124 unsigned char* pov = oview;
1125
1126 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1127 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1128
1129 if (parameters->options().output_is_position_independent())
1130 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
1131 else
1132 {
1133 memcpy(pov, exec_first_plt_entry, plt_entry_size);
1134 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1135 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1136 }
1137 pov += plt_entry_size;
1138
1139 unsigned char* got_pov = got_view;
1140
1141 memset(got_pov, 0, 12);
1142 got_pov += 12;
1143
1144 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1145
1146 unsigned int plt_offset = plt_entry_size;
1147 unsigned int plt_rel_offset = 0;
1148 unsigned int got_offset = 12;
1149 const unsigned int count = this->count_ + this->irelative_count_;
1150 for (unsigned int i = 0;
1151 i < count;
1152 ++i,
1153 pov += plt_entry_size,
1154 got_pov += 4,
1155 plt_offset += plt_entry_size,
1156 plt_rel_offset += rel_size,
1157 got_offset += 4)
1158 {
1159 // Set and adjust the PLT entry itself.
1160
1161 if (parameters->options().output_is_position_independent())
1162 {
1163 memcpy(pov, dyn_plt_entry, plt_entry_size);
1164 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1165 }
1166 else
1167 {
1168 memcpy(pov, exec_plt_entry, plt_entry_size);
1169 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1170 (got_address
1171 + got_offset));
1172 }
1173
1174 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1175 elfcpp::Swap<32, false>::writeval(pov + 12,
1176 - (plt_offset + plt_entry_size));
1177
1178 // Set the entry in the GOT.
1179 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
1180 }
1181
1182 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1183 // the GOT to point to the actual symbol value, rather than point to
1184 // the PLT entry. That will let the dynamic linker call the right
1185 // function when resolving IRELATIVE relocations.
1186 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1187 for (std::vector<Global_ifunc>::const_iterator p =
1188 this->global_ifuncs_.begin();
1189 p != this->global_ifuncs_.end();
1190 ++p)
1191 {
1192 const Sized_symbol<32>* ssym =
1193 static_cast<const Sized_symbol<32>*>(p->sym);
1194 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1195 ssym->value());
1196 }
1197
1198 for (std::vector<Local_ifunc>::const_iterator p =
1199 this->local_ifuncs_.begin();
1200 p != this->local_ifuncs_.end();
1201 ++p)
1202 {
1203 const Symbol_value<32>* psymval =
1204 p->object->local_symbol(p->local_sym_index);
1205 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1206 psymval->value(p->object, 0));
1207 }
1208
1209 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1210 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1211
1212 of->write_output_view(offset, oview_size, oview);
1213 of->write_output_view(got_file_offset, got_size, got_view);
1214 }
1215
1216 // Create the PLT section.
1217
1218 void
1219 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1220 {
1221 if (this->plt_ == NULL)
1222 {
1223 // Create the GOT sections first.
1224 this->got_section(symtab, layout);
1225
1226 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
1227 this->got_irelative_);
1228 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1229 (elfcpp::SHF_ALLOC
1230 | elfcpp::SHF_EXECINSTR),
1231 this->plt_, ORDER_PLT, false);
1232
1233 // Make the sh_info field of .rel.plt point to .plt.
1234 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1235 rel_plt_os->set_info_section(this->plt_->output_section());
1236 }
1237 }
1238
1239 // Create a PLT entry for a global symbol.
1240
1241 void
1242 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1243 {
1244 if (gsym->has_plt_offset())
1245 return;
1246 if (this->plt_ == NULL)
1247 this->make_plt_section(symtab, layout);
1248 this->plt_->add_entry(symtab, layout, gsym);
1249 }
1250
1251 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1252
1253 void
1254 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1255 Sized_relobj_file<32, false>* relobj,
1256 unsigned int local_sym_index)
1257 {
1258 if (relobj->local_has_plt_offset(local_sym_index))
1259 return;
1260 if (this->plt_ == NULL)
1261 this->make_plt_section(symtab, layout);
1262 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1263 relobj,
1264 local_sym_index);
1265 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1266 }
1267
1268 // Return the number of entries in the PLT.
1269
1270 unsigned int
1271 Target_i386::plt_entry_count() const
1272 {
1273 if (this->plt_ == NULL)
1274 return 0;
1275 return this->plt_->entry_count();
1276 }
1277
1278 // Return the offset of the first non-reserved PLT entry.
1279
1280 unsigned int
1281 Target_i386::first_plt_entry_offset() const
1282 {
1283 return Output_data_plt_i386::first_plt_entry_offset();
1284 }
1285
1286 // Return the size of each PLT entry.
1287
1288 unsigned int
1289 Target_i386::plt_entry_size() const
1290 {
1291 return Output_data_plt_i386::get_plt_entry_size();
1292 }
1293
1294 // Get the section to use for TLS_DESC relocations.
1295
1296 Target_i386::Reloc_section*
1297 Target_i386::rel_tls_desc_section(Layout* layout) const
1298 {
1299 return this->plt_section()->rel_tls_desc(layout);
1300 }
1301
1302 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1303
1304 void
1305 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1306 {
1307 if (this->tls_base_symbol_defined_)
1308 return;
1309
1310 Output_segment* tls_segment = layout->tls_segment();
1311 if (tls_segment != NULL)
1312 {
1313 bool is_exec = parameters->options().output_is_executable();
1314 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1315 Symbol_table::PREDEFINED,
1316 tls_segment, 0, 0,
1317 elfcpp::STT_TLS,
1318 elfcpp::STB_LOCAL,
1319 elfcpp::STV_HIDDEN, 0,
1320 (is_exec
1321 ? Symbol::SEGMENT_END
1322 : Symbol::SEGMENT_START),
1323 true);
1324 }
1325 this->tls_base_symbol_defined_ = true;
1326 }
1327
1328 // Create a GOT entry for the TLS module index.
1329
1330 unsigned int
1331 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1332 Sized_relobj_file<32, false>* object)
1333 {
1334 if (this->got_mod_index_offset_ == -1U)
1335 {
1336 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1337 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1338 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1339 unsigned int got_offset = got->add_constant(0);
1340 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1341 got_offset);
1342 got->add_constant(0);
1343 this->got_mod_index_offset_ = got_offset;
1344 }
1345 return this->got_mod_index_offset_;
1346 }
1347
1348 // Optimize the TLS relocation type based on what we know about the
1349 // symbol. IS_FINAL is true if the final address of this symbol is
1350 // known at link time.
1351
1352 tls::Tls_optimization
1353 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1354 {
1355 // If we are generating a shared library, then we can't do anything
1356 // in the linker.
1357 if (parameters->options().shared())
1358 return tls::TLSOPT_NONE;
1359
1360 switch (r_type)
1361 {
1362 case elfcpp::R_386_TLS_GD:
1363 case elfcpp::R_386_TLS_GOTDESC:
1364 case elfcpp::R_386_TLS_DESC_CALL:
1365 // These are General-Dynamic which permits fully general TLS
1366 // access. Since we know that we are generating an executable,
1367 // we can convert this to Initial-Exec. If we also know that
1368 // this is a local symbol, we can further switch to Local-Exec.
1369 if (is_final)
1370 return tls::TLSOPT_TO_LE;
1371 return tls::TLSOPT_TO_IE;
1372
1373 case elfcpp::R_386_TLS_LDM:
1374 // This is Local-Dynamic, which refers to a local symbol in the
1375 // dynamic TLS block. Since we know that we generating an
1376 // executable, we can switch to Local-Exec.
1377 return tls::TLSOPT_TO_LE;
1378
1379 case elfcpp::R_386_TLS_LDO_32:
1380 // Another type of Local-Dynamic relocation.
1381 return tls::TLSOPT_TO_LE;
1382
1383 case elfcpp::R_386_TLS_IE:
1384 case elfcpp::R_386_TLS_GOTIE:
1385 case elfcpp::R_386_TLS_IE_32:
1386 // These are Initial-Exec relocs which get the thread offset
1387 // from the GOT. If we know that we are linking against the
1388 // local symbol, we can switch to Local-Exec, which links the
1389 // thread offset into the instruction.
1390 if (is_final)
1391 return tls::TLSOPT_TO_LE;
1392 return tls::TLSOPT_NONE;
1393
1394 case elfcpp::R_386_TLS_LE:
1395 case elfcpp::R_386_TLS_LE_32:
1396 // When we already have Local-Exec, there is nothing further we
1397 // can do.
1398 return tls::TLSOPT_NONE;
1399
1400 default:
1401 gold_unreachable();
1402 }
1403 }
1404
1405 // Get the Reference_flags for a particular relocation.
1406
1407 int
1408 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1409 {
1410 switch (r_type)
1411 {
1412 case elfcpp::R_386_NONE:
1413 case elfcpp::R_386_GNU_VTINHERIT:
1414 case elfcpp::R_386_GNU_VTENTRY:
1415 case elfcpp::R_386_GOTPC:
1416 // No symbol reference.
1417 return 0;
1418
1419 case elfcpp::R_386_32:
1420 case elfcpp::R_386_16:
1421 case elfcpp::R_386_8:
1422 return Symbol::ABSOLUTE_REF;
1423
1424 case elfcpp::R_386_PC32:
1425 case elfcpp::R_386_PC16:
1426 case elfcpp::R_386_PC8:
1427 case elfcpp::R_386_GOTOFF:
1428 return Symbol::RELATIVE_REF;
1429
1430 case elfcpp::R_386_PLT32:
1431 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1432
1433 case elfcpp::R_386_GOT32:
1434 // Absolute in GOT.
1435 return Symbol::ABSOLUTE_REF;
1436
1437 case elfcpp::R_386_TLS_GD: // Global-dynamic
1438 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1439 case elfcpp::R_386_TLS_DESC_CALL:
1440 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1441 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1442 case elfcpp::R_386_TLS_IE: // Initial-exec
1443 case elfcpp::R_386_TLS_IE_32:
1444 case elfcpp::R_386_TLS_GOTIE:
1445 case elfcpp::R_386_TLS_LE: // Local-exec
1446 case elfcpp::R_386_TLS_LE_32:
1447 return Symbol::TLS_REF;
1448
1449 case elfcpp::R_386_COPY:
1450 case elfcpp::R_386_GLOB_DAT:
1451 case elfcpp::R_386_JUMP_SLOT:
1452 case elfcpp::R_386_RELATIVE:
1453 case elfcpp::R_386_IRELATIVE:
1454 case elfcpp::R_386_TLS_TPOFF:
1455 case elfcpp::R_386_TLS_DTPMOD32:
1456 case elfcpp::R_386_TLS_DTPOFF32:
1457 case elfcpp::R_386_TLS_TPOFF32:
1458 case elfcpp::R_386_TLS_DESC:
1459 case elfcpp::R_386_32PLT:
1460 case elfcpp::R_386_TLS_GD_32:
1461 case elfcpp::R_386_TLS_GD_PUSH:
1462 case elfcpp::R_386_TLS_GD_CALL:
1463 case elfcpp::R_386_TLS_GD_POP:
1464 case elfcpp::R_386_TLS_LDM_32:
1465 case elfcpp::R_386_TLS_LDM_PUSH:
1466 case elfcpp::R_386_TLS_LDM_CALL:
1467 case elfcpp::R_386_TLS_LDM_POP:
1468 case elfcpp::R_386_USED_BY_INTEL_200:
1469 default:
1470 // Not expected. We will give an error later.
1471 return 0;
1472 }
1473 }
1474
1475 // Report an unsupported relocation against a local symbol.
1476
1477 void
1478 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1479 unsigned int r_type)
1480 {
1481 gold_error(_("%s: unsupported reloc %u against local symbol"),
1482 object->name().c_str(), r_type);
1483 }
1484
1485 // Return whether we need to make a PLT entry for a relocation of a
1486 // given type against a STT_GNU_IFUNC symbol.
1487
1488 bool
1489 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1490 Sized_relobj_file<32, false>* object,
1491 unsigned int r_type)
1492 {
1493 int flags = Scan::get_reference_flags(r_type);
1494 if (flags & Symbol::TLS_REF)
1495 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1496 object->name().c_str(), r_type);
1497 return flags != 0;
1498 }
1499
1500 // Scan a relocation for a local symbol.
1501
1502 inline void
1503 Target_i386::Scan::local(Symbol_table* symtab,
1504 Layout* layout,
1505 Target_i386* target,
1506 Sized_relobj_file<32, false>* object,
1507 unsigned int data_shndx,
1508 Output_section* output_section,
1509 const elfcpp::Rel<32, false>& reloc,
1510 unsigned int r_type,
1511 const elfcpp::Sym<32, false>& lsym)
1512 {
1513 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1514 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1515 && this->reloc_needs_plt_for_ifunc(object, r_type))
1516 {
1517 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1518 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1519 }
1520
1521 switch (r_type)
1522 {
1523 case elfcpp::R_386_NONE:
1524 case elfcpp::R_386_GNU_VTINHERIT:
1525 case elfcpp::R_386_GNU_VTENTRY:
1526 break;
1527
1528 case elfcpp::R_386_32:
1529 // If building a shared library (or a position-independent
1530 // executable), we need to create a dynamic relocation for
1531 // this location. The relocation applied at link time will
1532 // apply the link-time value, so we flag the location with
1533 // an R_386_RELATIVE relocation so the dynamic loader can
1534 // relocate it easily.
1535 if (parameters->options().output_is_position_independent())
1536 {
1537 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1538 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1539 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1540 output_section, data_shndx,
1541 reloc.get_r_offset());
1542 }
1543 break;
1544
1545 case elfcpp::R_386_16:
1546 case elfcpp::R_386_8:
1547 // If building a shared library (or a position-independent
1548 // executable), we need to create a dynamic relocation for
1549 // this location. Because the addend needs to remain in the
1550 // data section, we need to be careful not to apply this
1551 // relocation statically.
1552 if (parameters->options().output_is_position_independent())
1553 {
1554 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1555 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1556 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1557 rel_dyn->add_local(object, r_sym, r_type, output_section,
1558 data_shndx, reloc.get_r_offset());
1559 else
1560 {
1561 gold_assert(lsym.get_st_value() == 0);
1562 unsigned int shndx = lsym.get_st_shndx();
1563 bool is_ordinary;
1564 shndx = object->adjust_sym_shndx(r_sym, shndx,
1565 &is_ordinary);
1566 if (!is_ordinary)
1567 object->error(_("section symbol %u has bad shndx %u"),
1568 r_sym, shndx);
1569 else
1570 rel_dyn->add_local_section(object, shndx,
1571 r_type, output_section,
1572 data_shndx, reloc.get_r_offset());
1573 }
1574 }
1575 break;
1576
1577 case elfcpp::R_386_PC32:
1578 case elfcpp::R_386_PC16:
1579 case elfcpp::R_386_PC8:
1580 break;
1581
1582 case elfcpp::R_386_PLT32:
1583 // Since we know this is a local symbol, we can handle this as a
1584 // PC32 reloc.
1585 break;
1586
1587 case elfcpp::R_386_GOTOFF:
1588 case elfcpp::R_386_GOTPC:
1589 // We need a GOT section.
1590 target->got_section(symtab, layout);
1591 break;
1592
1593 case elfcpp::R_386_GOT32:
1594 {
1595 // The symbol requires a GOT entry.
1596 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1597 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1598
1599 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1600 // lets function pointers compare correctly with shared
1601 // libraries. Otherwise we would need an IRELATIVE reloc.
1602 bool is_new;
1603 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1604 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1605 else
1606 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1607 if (is_new)
1608 {
1609 // If we are generating a shared object, we need to add a
1610 // dynamic RELATIVE relocation for this symbol's GOT entry.
1611 if (parameters->options().output_is_position_independent())
1612 {
1613 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1614 unsigned int got_offset =
1615 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1616 rel_dyn->add_local_relative(object, r_sym,
1617 elfcpp::R_386_RELATIVE,
1618 got, got_offset);
1619 }
1620 }
1621 }
1622 break;
1623
1624 // These are relocations which should only be seen by the
1625 // dynamic linker, and should never be seen here.
1626 case elfcpp::R_386_COPY:
1627 case elfcpp::R_386_GLOB_DAT:
1628 case elfcpp::R_386_JUMP_SLOT:
1629 case elfcpp::R_386_RELATIVE:
1630 case elfcpp::R_386_IRELATIVE:
1631 case elfcpp::R_386_TLS_TPOFF:
1632 case elfcpp::R_386_TLS_DTPMOD32:
1633 case elfcpp::R_386_TLS_DTPOFF32:
1634 case elfcpp::R_386_TLS_TPOFF32:
1635 case elfcpp::R_386_TLS_DESC:
1636 gold_error(_("%s: unexpected reloc %u in object file"),
1637 object->name().c_str(), r_type);
1638 break;
1639
1640 // These are initial TLS relocs, which are expected when
1641 // linking.
1642 case elfcpp::R_386_TLS_GD: // Global-dynamic
1643 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1644 case elfcpp::R_386_TLS_DESC_CALL:
1645 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1646 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1647 case elfcpp::R_386_TLS_IE: // Initial-exec
1648 case elfcpp::R_386_TLS_IE_32:
1649 case elfcpp::R_386_TLS_GOTIE:
1650 case elfcpp::R_386_TLS_LE: // Local-exec
1651 case elfcpp::R_386_TLS_LE_32:
1652 {
1653 bool output_is_shared = parameters->options().shared();
1654 const tls::Tls_optimization optimized_type
1655 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1656 switch (r_type)
1657 {
1658 case elfcpp::R_386_TLS_GD: // Global-dynamic
1659 if (optimized_type == tls::TLSOPT_NONE)
1660 {
1661 // Create a pair of GOT entries for the module index and
1662 // dtv-relative offset.
1663 Output_data_got<32, false>* got
1664 = target->got_section(symtab, layout);
1665 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1666 unsigned int shndx = lsym.get_st_shndx();
1667 bool is_ordinary;
1668 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1669 if (!is_ordinary)
1670 object->error(_("local symbol %u has bad shndx %u"),
1671 r_sym, shndx);
1672 else
1673 got->add_local_pair_with_rel(object, r_sym, shndx,
1674 GOT_TYPE_TLS_PAIR,
1675 target->rel_dyn_section(layout),
1676 elfcpp::R_386_TLS_DTPMOD32, 0);
1677 }
1678 else if (optimized_type != tls::TLSOPT_TO_LE)
1679 unsupported_reloc_local(object, r_type);
1680 break;
1681
1682 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1683 target->define_tls_base_symbol(symtab, layout);
1684 if (optimized_type == tls::TLSOPT_NONE)
1685 {
1686 // Create a double GOT entry with an R_386_TLS_DESC
1687 // reloc. The R_386_TLS_DESC reloc is resolved
1688 // lazily, so the GOT entry needs to be in an area in
1689 // .got.plt, not .got. Call got_section to make sure
1690 // the section has been created.
1691 target->got_section(symtab, layout);
1692 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1693 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1694 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1695 {
1696 unsigned int got_offset = got->add_constant(0);
1697 // The local symbol value is stored in the second
1698 // GOT entry.
1699 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1700 // That set the GOT offset of the local symbol to
1701 // point to the second entry, but we want it to
1702 // point to the first.
1703 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1704 got_offset);
1705 Reloc_section* rt = target->rel_tls_desc_section(layout);
1706 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1707 }
1708 }
1709 else if (optimized_type != tls::TLSOPT_TO_LE)
1710 unsupported_reloc_local(object, r_type);
1711 break;
1712
1713 case elfcpp::R_386_TLS_DESC_CALL:
1714 break;
1715
1716 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1717 if (optimized_type == tls::TLSOPT_NONE)
1718 {
1719 // Create a GOT entry for the module index.
1720 target->got_mod_index_entry(symtab, layout, object);
1721 }
1722 else if (optimized_type != tls::TLSOPT_TO_LE)
1723 unsupported_reloc_local(object, r_type);
1724 break;
1725
1726 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1727 break;
1728
1729 case elfcpp::R_386_TLS_IE: // Initial-exec
1730 case elfcpp::R_386_TLS_IE_32:
1731 case elfcpp::R_386_TLS_GOTIE:
1732 layout->set_has_static_tls();
1733 if (optimized_type == tls::TLSOPT_NONE)
1734 {
1735 // For the R_386_TLS_IE relocation, we need to create a
1736 // dynamic relocation when building a shared library.
1737 if (r_type == elfcpp::R_386_TLS_IE
1738 && parameters->options().shared())
1739 {
1740 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1741 unsigned int r_sym
1742 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1743 rel_dyn->add_local_relative(object, r_sym,
1744 elfcpp::R_386_RELATIVE,
1745 output_section, data_shndx,
1746 reloc.get_r_offset());
1747 }
1748 // Create a GOT entry for the tp-relative offset.
1749 Output_data_got<32, false>* got
1750 = target->got_section(symtab, layout);
1751 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1752 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1753 ? elfcpp::R_386_TLS_TPOFF32
1754 : elfcpp::R_386_TLS_TPOFF);
1755 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1756 ? GOT_TYPE_TLS_OFFSET
1757 : GOT_TYPE_TLS_NOFFSET);
1758 got->add_local_with_rel(object, r_sym, got_type,
1759 target->rel_dyn_section(layout),
1760 dyn_r_type);
1761 }
1762 else if (optimized_type != tls::TLSOPT_TO_LE)
1763 unsupported_reloc_local(object, r_type);
1764 break;
1765
1766 case elfcpp::R_386_TLS_LE: // Local-exec
1767 case elfcpp::R_386_TLS_LE_32:
1768 layout->set_has_static_tls();
1769 if (output_is_shared)
1770 {
1771 // We need to create a dynamic relocation.
1772 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1773 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1774 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1775 ? elfcpp::R_386_TLS_TPOFF32
1776 : elfcpp::R_386_TLS_TPOFF);
1777 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1778 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1779 data_shndx, reloc.get_r_offset());
1780 }
1781 break;
1782
1783 default:
1784 gold_unreachable();
1785 }
1786 }
1787 break;
1788
1789 case elfcpp::R_386_32PLT:
1790 case elfcpp::R_386_TLS_GD_32:
1791 case elfcpp::R_386_TLS_GD_PUSH:
1792 case elfcpp::R_386_TLS_GD_CALL:
1793 case elfcpp::R_386_TLS_GD_POP:
1794 case elfcpp::R_386_TLS_LDM_32:
1795 case elfcpp::R_386_TLS_LDM_PUSH:
1796 case elfcpp::R_386_TLS_LDM_CALL:
1797 case elfcpp::R_386_TLS_LDM_POP:
1798 case elfcpp::R_386_USED_BY_INTEL_200:
1799 default:
1800 unsupported_reloc_local(object, r_type);
1801 break;
1802 }
1803 }
1804
1805 // Report an unsupported relocation against a global symbol.
1806
1807 void
1808 Target_i386::Scan::unsupported_reloc_global(
1809 Sized_relobj_file<32, false>* object,
1810 unsigned int r_type,
1811 Symbol* gsym)
1812 {
1813 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1814 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1815 }
1816
1817 inline bool
1818 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1819 {
1820 switch (r_type)
1821 {
1822 case elfcpp::R_386_32:
1823 case elfcpp::R_386_16:
1824 case elfcpp::R_386_8:
1825 case elfcpp::R_386_GOTOFF:
1826 case elfcpp::R_386_GOT32:
1827 {
1828 return true;
1829 }
1830 default:
1831 return false;
1832 }
1833 return false;
1834 }
1835
1836 inline bool
1837 Target_i386::Scan::local_reloc_may_be_function_pointer(
1838 Symbol_table* ,
1839 Layout* ,
1840 Target_i386* ,
1841 Sized_relobj_file<32, false>* ,
1842 unsigned int ,
1843 Output_section* ,
1844 const elfcpp::Rel<32, false>& ,
1845 unsigned int r_type,
1846 const elfcpp::Sym<32, false>&)
1847 {
1848 return possible_function_pointer_reloc(r_type);
1849 }
1850
1851 inline bool
1852 Target_i386::Scan::global_reloc_may_be_function_pointer(
1853 Symbol_table* ,
1854 Layout* ,
1855 Target_i386* ,
1856 Sized_relobj_file<32, false>* ,
1857 unsigned int ,
1858 Output_section* ,
1859 const elfcpp::Rel<32, false>& ,
1860 unsigned int r_type,
1861 Symbol*)
1862 {
1863 return possible_function_pointer_reloc(r_type);
1864 }
1865
1866 // Scan a relocation for a global symbol.
1867
1868 inline void
1869 Target_i386::Scan::global(Symbol_table* symtab,
1870 Layout* layout,
1871 Target_i386* target,
1872 Sized_relobj_file<32, false>* object,
1873 unsigned int data_shndx,
1874 Output_section* output_section,
1875 const elfcpp::Rel<32, false>& reloc,
1876 unsigned int r_type,
1877 Symbol* gsym)
1878 {
1879 // A STT_GNU_IFUNC symbol may require a PLT entry.
1880 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1881 && this->reloc_needs_plt_for_ifunc(object, r_type))
1882 target->make_plt_entry(symtab, layout, gsym);
1883
1884 switch (r_type)
1885 {
1886 case elfcpp::R_386_NONE:
1887 case elfcpp::R_386_GNU_VTINHERIT:
1888 case elfcpp::R_386_GNU_VTENTRY:
1889 break;
1890
1891 case elfcpp::R_386_32:
1892 case elfcpp::R_386_16:
1893 case elfcpp::R_386_8:
1894 {
1895 // Make a PLT entry if necessary.
1896 if (gsym->needs_plt_entry())
1897 {
1898 target->make_plt_entry(symtab, layout, gsym);
1899 // Since this is not a PC-relative relocation, we may be
1900 // taking the address of a function. In that case we need to
1901 // set the entry in the dynamic symbol table to the address of
1902 // the PLT entry.
1903 if (gsym->is_from_dynobj() && !parameters->options().shared())
1904 gsym->set_needs_dynsym_value();
1905 }
1906 // Make a dynamic relocation if necessary.
1907 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1908 {
1909 if (gsym->may_need_copy_reloc())
1910 {
1911 target->copy_reloc(symtab, layout, object,
1912 data_shndx, output_section, gsym, reloc);
1913 }
1914 else if (r_type == elfcpp::R_386_32
1915 && gsym->type() == elfcpp::STT_GNU_IFUNC
1916 && gsym->can_use_relative_reloc(false)
1917 && !gsym->is_from_dynobj()
1918 && !gsym->is_undefined()
1919 && !gsym->is_preemptible())
1920 {
1921 // Use an IRELATIVE reloc for a locally defined
1922 // STT_GNU_IFUNC symbol. This makes a function
1923 // address in a PIE executable match the address in a
1924 // shared library that it links against.
1925 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
1926 rel_dyn->add_symbolless_global_addend(gsym,
1927 elfcpp::R_386_IRELATIVE,
1928 output_section,
1929 object, data_shndx,
1930 reloc.get_r_offset());
1931 }
1932 else if (r_type == elfcpp::R_386_32
1933 && gsym->can_use_relative_reloc(false))
1934 {
1935 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1936 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1937 output_section, object,
1938 data_shndx, reloc.get_r_offset());
1939 }
1940 else
1941 {
1942 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1943 rel_dyn->add_global(gsym, r_type, output_section, object,
1944 data_shndx, reloc.get_r_offset());
1945 }
1946 }
1947 }
1948 break;
1949
1950 case elfcpp::R_386_PC32:
1951 case elfcpp::R_386_PC16:
1952 case elfcpp::R_386_PC8:
1953 {
1954 // Make a PLT entry if necessary.
1955 if (gsym->needs_plt_entry())
1956 {
1957 // These relocations are used for function calls only in
1958 // non-PIC code. For a 32-bit relocation in a shared library,
1959 // we'll need a text relocation anyway, so we can skip the
1960 // PLT entry and let the dynamic linker bind the call directly
1961 // to the target. For smaller relocations, we should use a
1962 // PLT entry to ensure that the call can reach.
1963 if (!parameters->options().shared()
1964 || r_type != elfcpp::R_386_PC32)
1965 target->make_plt_entry(symtab, layout, gsym);
1966 }
1967 // Make a dynamic relocation if necessary.
1968 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1969 {
1970 if (gsym->may_need_copy_reloc())
1971 {
1972 target->copy_reloc(symtab, layout, object,
1973 data_shndx, output_section, gsym, reloc);
1974 }
1975 else
1976 {
1977 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1978 rel_dyn->add_global(gsym, r_type, output_section, object,
1979 data_shndx, reloc.get_r_offset());
1980 }
1981 }
1982 }
1983 break;
1984
1985 case elfcpp::R_386_GOT32:
1986 {
1987 // The symbol requires a GOT entry.
1988 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1989 if (gsym->final_value_is_known())
1990 {
1991 // For a STT_GNU_IFUNC symbol we want the PLT address.
1992 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1993 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1994 else
1995 got->add_global(gsym, GOT_TYPE_STANDARD);
1996 }
1997 else
1998 {
1999 // If this symbol is not fully resolved, we need to add a
2000 // GOT entry with a dynamic relocation.
2001 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2002
2003 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2004 //
2005 // 1) The symbol may be defined in some other module.
2006 //
2007 // 2) We are building a shared library and this is a
2008 // protected symbol; using GLOB_DAT means that the dynamic
2009 // linker can use the address of the PLT in the main
2010 // executable when appropriate so that function address
2011 // comparisons work.
2012 //
2013 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2014 // code, again so that function address comparisons work.
2015 if (gsym->is_from_dynobj()
2016 || gsym->is_undefined()
2017 || gsym->is_preemptible()
2018 || (gsym->visibility() == elfcpp::STV_PROTECTED
2019 && parameters->options().shared())
2020 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2021 && parameters->options().output_is_position_independent()))
2022 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2023 rel_dyn, elfcpp::R_386_GLOB_DAT);
2024 else
2025 {
2026 // For a STT_GNU_IFUNC symbol we want to write the PLT
2027 // offset into the GOT, so that function pointer
2028 // comparisons work correctly.
2029 bool is_new;
2030 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2031 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2032 else
2033 {
2034 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2035 // Tell the dynamic linker to use the PLT address
2036 // when resolving relocations.
2037 if (gsym->is_from_dynobj()
2038 && !parameters->options().shared())
2039 gsym->set_needs_dynsym_value();
2040 }
2041 if (is_new)
2042 {
2043 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2044 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2045 got, got_off);
2046 }
2047 }
2048 }
2049 }
2050 break;
2051
2052 case elfcpp::R_386_PLT32:
2053 // If the symbol is fully resolved, this is just a PC32 reloc.
2054 // Otherwise we need a PLT entry.
2055 if (gsym->final_value_is_known())
2056 break;
2057 // If building a shared library, we can also skip the PLT entry
2058 // if the symbol is defined in the output file and is protected
2059 // or hidden.
2060 if (gsym->is_defined()
2061 && !gsym->is_from_dynobj()
2062 && !gsym->is_preemptible())
2063 break;
2064 target->make_plt_entry(symtab, layout, gsym);
2065 break;
2066
2067 case elfcpp::R_386_GOTOFF:
2068 case elfcpp::R_386_GOTPC:
2069 // We need a GOT section.
2070 target->got_section(symtab, layout);
2071 break;
2072
2073 // These are relocations which should only be seen by the
2074 // dynamic linker, and should never be seen here.
2075 case elfcpp::R_386_COPY:
2076 case elfcpp::R_386_GLOB_DAT:
2077 case elfcpp::R_386_JUMP_SLOT:
2078 case elfcpp::R_386_RELATIVE:
2079 case elfcpp::R_386_IRELATIVE:
2080 case elfcpp::R_386_TLS_TPOFF:
2081 case elfcpp::R_386_TLS_DTPMOD32:
2082 case elfcpp::R_386_TLS_DTPOFF32:
2083 case elfcpp::R_386_TLS_TPOFF32:
2084 case elfcpp::R_386_TLS_DESC:
2085 gold_error(_("%s: unexpected reloc %u in object file"),
2086 object->name().c_str(), r_type);
2087 break;
2088
2089 // These are initial tls relocs, which are expected when
2090 // linking.
2091 case elfcpp::R_386_TLS_GD: // Global-dynamic
2092 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2093 case elfcpp::R_386_TLS_DESC_CALL:
2094 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2095 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2096 case elfcpp::R_386_TLS_IE: // Initial-exec
2097 case elfcpp::R_386_TLS_IE_32:
2098 case elfcpp::R_386_TLS_GOTIE:
2099 case elfcpp::R_386_TLS_LE: // Local-exec
2100 case elfcpp::R_386_TLS_LE_32:
2101 {
2102 const bool is_final = gsym->final_value_is_known();
2103 const tls::Tls_optimization optimized_type
2104 = Target_i386::optimize_tls_reloc(is_final, r_type);
2105 switch (r_type)
2106 {
2107 case elfcpp::R_386_TLS_GD: // Global-dynamic
2108 if (optimized_type == tls::TLSOPT_NONE)
2109 {
2110 // Create a pair of GOT entries for the module index and
2111 // dtv-relative offset.
2112 Output_data_got<32, false>* got
2113 = target->got_section(symtab, layout);
2114 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2115 target->rel_dyn_section(layout),
2116 elfcpp::R_386_TLS_DTPMOD32,
2117 elfcpp::R_386_TLS_DTPOFF32);
2118 }
2119 else if (optimized_type == tls::TLSOPT_TO_IE)
2120 {
2121 // Create a GOT entry for the tp-relative offset.
2122 Output_data_got<32, false>* got
2123 = target->got_section(symtab, layout);
2124 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2125 target->rel_dyn_section(layout),
2126 elfcpp::R_386_TLS_TPOFF);
2127 }
2128 else if (optimized_type != tls::TLSOPT_TO_LE)
2129 unsupported_reloc_global(object, r_type, gsym);
2130 break;
2131
2132 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
2133 target->define_tls_base_symbol(symtab, layout);
2134 if (optimized_type == tls::TLSOPT_NONE)
2135 {
2136 // Create a double GOT entry with an R_386_TLS_DESC
2137 // reloc. The R_386_TLS_DESC reloc is resolved
2138 // lazily, so the GOT entry needs to be in an area in
2139 // .got.plt, not .got. Call got_section to make sure
2140 // the section has been created.
2141 target->got_section(symtab, layout);
2142 Output_data_got<32, false>* got = target->got_tlsdesc_section();
2143 Reloc_section* rt = target->rel_tls_desc_section(layout);
2144 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2145 elfcpp::R_386_TLS_DESC, 0);
2146 }
2147 else if (optimized_type == tls::TLSOPT_TO_IE)
2148 {
2149 // Create a GOT entry for the tp-relative offset.
2150 Output_data_got<32, false>* got
2151 = target->got_section(symtab, layout);
2152 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2153 target->rel_dyn_section(layout),
2154 elfcpp::R_386_TLS_TPOFF);
2155 }
2156 else if (optimized_type != tls::TLSOPT_TO_LE)
2157 unsupported_reloc_global(object, r_type, gsym);
2158 break;
2159
2160 case elfcpp::R_386_TLS_DESC_CALL:
2161 break;
2162
2163 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2164 if (optimized_type == tls::TLSOPT_NONE)
2165 {
2166 // Create a GOT entry for the module index.
2167 target->got_mod_index_entry(symtab, layout, object);
2168 }
2169 else if (optimized_type != tls::TLSOPT_TO_LE)
2170 unsupported_reloc_global(object, r_type, gsym);
2171 break;
2172
2173 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2174 break;
2175
2176 case elfcpp::R_386_TLS_IE: // Initial-exec
2177 case elfcpp::R_386_TLS_IE_32:
2178 case elfcpp::R_386_TLS_GOTIE:
2179 layout->set_has_static_tls();
2180 if (optimized_type == tls::TLSOPT_NONE)
2181 {
2182 // For the R_386_TLS_IE relocation, we need to create a
2183 // dynamic relocation when building a shared library.
2184 if (r_type == elfcpp::R_386_TLS_IE
2185 && parameters->options().shared())
2186 {
2187 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2188 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2189 output_section, object,
2190 data_shndx,
2191 reloc.get_r_offset());
2192 }
2193 // Create a GOT entry for the tp-relative offset.
2194 Output_data_got<32, false>* got
2195 = target->got_section(symtab, layout);
2196 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2197 ? elfcpp::R_386_TLS_TPOFF32
2198 : elfcpp::R_386_TLS_TPOFF);
2199 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2200 ? GOT_TYPE_TLS_OFFSET
2201 : GOT_TYPE_TLS_NOFFSET);
2202 got->add_global_with_rel(gsym, got_type,
2203 target->rel_dyn_section(layout),
2204 dyn_r_type);
2205 }
2206 else if (optimized_type != tls::TLSOPT_TO_LE)
2207 unsupported_reloc_global(object, r_type, gsym);
2208 break;
2209
2210 case elfcpp::R_386_TLS_LE: // Local-exec
2211 case elfcpp::R_386_TLS_LE_32:
2212 layout->set_has_static_tls();
2213 if (parameters->options().shared())
2214 {
2215 // We need to create a dynamic relocation.
2216 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2217 ? elfcpp::R_386_TLS_TPOFF32
2218 : elfcpp::R_386_TLS_TPOFF);
2219 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2220 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2221 data_shndx, reloc.get_r_offset());
2222 }
2223 break;
2224
2225 default:
2226 gold_unreachable();
2227 }
2228 }
2229 break;
2230
2231 case elfcpp::R_386_32PLT:
2232 case elfcpp::R_386_TLS_GD_32:
2233 case elfcpp::R_386_TLS_GD_PUSH:
2234 case elfcpp::R_386_TLS_GD_CALL:
2235 case elfcpp::R_386_TLS_GD_POP:
2236 case elfcpp::R_386_TLS_LDM_32:
2237 case elfcpp::R_386_TLS_LDM_PUSH:
2238 case elfcpp::R_386_TLS_LDM_CALL:
2239 case elfcpp::R_386_TLS_LDM_POP:
2240 case elfcpp::R_386_USED_BY_INTEL_200:
2241 default:
2242 unsupported_reloc_global(object, r_type, gsym);
2243 break;
2244 }
2245 }
2246
2247 // Process relocations for gc.
2248
2249 void
2250 Target_i386::gc_process_relocs(Symbol_table* symtab,
2251 Layout* layout,
2252 Sized_relobj_file<32, false>* object,
2253 unsigned int data_shndx,
2254 unsigned int,
2255 const unsigned char* prelocs,
2256 size_t reloc_count,
2257 Output_section* output_section,
2258 bool needs_special_offset_handling,
2259 size_t local_symbol_count,
2260 const unsigned char* plocal_symbols)
2261 {
2262 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2263 Target_i386::Scan,
2264 Target_i386::Relocatable_size_for_reloc>(
2265 symtab,
2266 layout,
2267 this,
2268 object,
2269 data_shndx,
2270 prelocs,
2271 reloc_count,
2272 output_section,
2273 needs_special_offset_handling,
2274 local_symbol_count,
2275 plocal_symbols);
2276 }
2277
2278 // Scan relocations for a section.
2279
2280 void
2281 Target_i386::scan_relocs(Symbol_table* symtab,
2282 Layout* layout,
2283 Sized_relobj_file<32, false>* object,
2284 unsigned int data_shndx,
2285 unsigned int sh_type,
2286 const unsigned char* prelocs,
2287 size_t reloc_count,
2288 Output_section* output_section,
2289 bool needs_special_offset_handling,
2290 size_t local_symbol_count,
2291 const unsigned char* plocal_symbols)
2292 {
2293 if (sh_type == elfcpp::SHT_RELA)
2294 {
2295 gold_error(_("%s: unsupported RELA reloc section"),
2296 object->name().c_str());
2297 return;
2298 }
2299
2300 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2301 Target_i386::Scan>(
2302 symtab,
2303 layout,
2304 this,
2305 object,
2306 data_shndx,
2307 prelocs,
2308 reloc_count,
2309 output_section,
2310 needs_special_offset_handling,
2311 local_symbol_count,
2312 plocal_symbols);
2313 }
2314
2315 // Finalize the sections.
2316
2317 void
2318 Target_i386::do_finalize_sections(
2319 Layout* layout,
2320 const Input_objects*,
2321 Symbol_table* symtab)
2322 {
2323 const Reloc_section* rel_plt = (this->plt_ == NULL
2324 ? NULL
2325 : this->plt_->rel_plt());
2326 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2327 this->rel_dyn_, true, false);
2328
2329 // Emit any relocs we saved in an attempt to avoid generating COPY
2330 // relocs.
2331 if (this->copy_relocs_.any_saved_relocs())
2332 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2333
2334 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2335 // the .got.plt section.
2336 Symbol* sym = this->global_offset_table_;
2337 if (sym != NULL)
2338 {
2339 uint32_t data_size = this->got_plt_->current_data_size();
2340 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2341 }
2342
2343 if (parameters->doing_static_link()
2344 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2345 {
2346 // If linking statically, make sure that the __rel_iplt symbols
2347 // were defined if necessary, even if we didn't create a PLT.
2348 static const Define_symbol_in_segment syms[] =
2349 {
2350 {
2351 "__rel_iplt_start", // name
2352 elfcpp::PT_LOAD, // segment_type
2353 elfcpp::PF_W, // segment_flags_set
2354 elfcpp::PF(0), // segment_flags_clear
2355 0, // value
2356 0, // size
2357 elfcpp::STT_NOTYPE, // type
2358 elfcpp::STB_GLOBAL, // binding
2359 elfcpp::STV_HIDDEN, // visibility
2360 0, // nonvis
2361 Symbol::SEGMENT_START, // offset_from_base
2362 true // only_if_ref
2363 },
2364 {
2365 "__rel_iplt_end", // name
2366 elfcpp::PT_LOAD, // segment_type
2367 elfcpp::PF_W, // segment_flags_set
2368 elfcpp::PF(0), // segment_flags_clear
2369 0, // value
2370 0, // size
2371 elfcpp::STT_NOTYPE, // type
2372 elfcpp::STB_GLOBAL, // binding
2373 elfcpp::STV_HIDDEN, // visibility
2374 0, // nonvis
2375 Symbol::SEGMENT_START, // offset_from_base
2376 true // only_if_ref
2377 }
2378 };
2379
2380 symtab->define_symbols(layout, 2, syms,
2381 layout->script_options()->saw_sections_clause());
2382 }
2383 }
2384
2385 // Return whether a direct absolute static relocation needs to be applied.
2386 // In cases where Scan::local() or Scan::global() has created
2387 // a dynamic relocation other than R_386_RELATIVE, the addend
2388 // of the relocation is carried in the data, and we must not
2389 // apply the static relocation.
2390
2391 inline bool
2392 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2393 unsigned int r_type,
2394 bool is_32bit,
2395 Output_section* output_section)
2396 {
2397 // If the output section is not allocated, then we didn't call
2398 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2399 // the reloc here.
2400 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2401 return true;
2402
2403 int ref_flags = Scan::get_reference_flags(r_type);
2404
2405 // For local symbols, we will have created a non-RELATIVE dynamic
2406 // relocation only if (a) the output is position independent,
2407 // (b) the relocation is absolute (not pc- or segment-relative), and
2408 // (c) the relocation is not 32 bits wide.
2409 if (gsym == NULL)
2410 return !(parameters->options().output_is_position_independent()
2411 && (ref_flags & Symbol::ABSOLUTE_REF)
2412 && !is_32bit);
2413
2414 // For global symbols, we use the same helper routines used in the
2415 // scan pass. If we did not create a dynamic relocation, or if we
2416 // created a RELATIVE dynamic relocation, we should apply the static
2417 // relocation.
2418 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2419 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2420 && gsym->can_use_relative_reloc(ref_flags
2421 & Symbol::FUNCTION_CALL);
2422 return !has_dyn || is_rel;
2423 }
2424
2425 // Perform a relocation.
2426
2427 inline bool
2428 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2429 Target_i386* target,
2430 Output_section* output_section,
2431 size_t relnum,
2432 const elfcpp::Rel<32, false>& rel,
2433 unsigned int r_type,
2434 const Sized_symbol<32>* gsym,
2435 const Symbol_value<32>* psymval,
2436 unsigned char* view,
2437 elfcpp::Elf_types<32>::Elf_Addr address,
2438 section_size_type view_size)
2439 {
2440 if (this->skip_call_tls_get_addr_)
2441 {
2442 if ((r_type != elfcpp::R_386_PLT32
2443 && r_type != elfcpp::R_386_PC32)
2444 || gsym == NULL
2445 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2446 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2447 _("missing expected TLS relocation"));
2448 else
2449 {
2450 this->skip_call_tls_get_addr_ = false;
2451 return false;
2452 }
2453 }
2454
2455 const Sized_relobj_file<32, false>* object = relinfo->object;
2456
2457 // Pick the value to use for symbols defined in shared objects.
2458 Symbol_value<32> symval;
2459 if (gsym != NULL
2460 && gsym->type() == elfcpp::STT_GNU_IFUNC
2461 && r_type == elfcpp::R_386_32
2462 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2463 && gsym->can_use_relative_reloc(false)
2464 && !gsym->is_from_dynobj()
2465 && !gsym->is_undefined()
2466 && !gsym->is_preemptible())
2467 {
2468 // In this case we are generating a R_386_IRELATIVE reloc. We
2469 // want to use the real value of the symbol, not the PLT offset.
2470 }
2471 else if (gsym != NULL
2472 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2473 {
2474 symval.set_output_value(target->plt_address_for_global(gsym)
2475 + gsym->plt_offset());
2476 psymval = &symval;
2477 }
2478 else if (gsym == NULL && psymval->is_ifunc_symbol())
2479 {
2480 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2481 if (object->local_has_plt_offset(r_sym))
2482 {
2483 symval.set_output_value(target->plt_address_for_local(object, r_sym)
2484 + object->local_plt_offset(r_sym));
2485 psymval = &symval;
2486 }
2487 }
2488
2489 // Get the GOT offset if needed.
2490 // The GOT pointer points to the end of the GOT section.
2491 // We need to subtract the size of the GOT section to get
2492 // the actual offset to use in the relocation.
2493 bool have_got_offset = false;
2494 unsigned int got_offset = 0;
2495 switch (r_type)
2496 {
2497 case elfcpp::R_386_GOT32:
2498 if (gsym != NULL)
2499 {
2500 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2501 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2502 - target->got_size());
2503 }
2504 else
2505 {
2506 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2507 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2508 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2509 - target->got_size());
2510 }
2511 have_got_offset = true;
2512 break;
2513
2514 default:
2515 break;
2516 }
2517
2518 switch (r_type)
2519 {
2520 case elfcpp::R_386_NONE:
2521 case elfcpp::R_386_GNU_VTINHERIT:
2522 case elfcpp::R_386_GNU_VTENTRY:
2523 break;
2524
2525 case elfcpp::R_386_32:
2526 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2527 Relocate_functions<32, false>::rel32(view, object, psymval);
2528 break;
2529
2530 case elfcpp::R_386_PC32:
2531 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2532 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2533 break;
2534
2535 case elfcpp::R_386_16:
2536 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2537 Relocate_functions<32, false>::rel16(view, object, psymval);
2538 break;
2539
2540 case elfcpp::R_386_PC16:
2541 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2542 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2543 break;
2544
2545 case elfcpp::R_386_8:
2546 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2547 Relocate_functions<32, false>::rel8(view, object, psymval);
2548 break;
2549
2550 case elfcpp::R_386_PC8:
2551 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2552 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2553 break;
2554
2555 case elfcpp::R_386_PLT32:
2556 gold_assert(gsym == NULL
2557 || gsym->has_plt_offset()
2558 || gsym->final_value_is_known()
2559 || (gsym->is_defined()
2560 && !gsym->is_from_dynobj()
2561 && !gsym->is_preemptible()));
2562 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2563 break;
2564
2565 case elfcpp::R_386_GOT32:
2566 gold_assert(have_got_offset);
2567 Relocate_functions<32, false>::rel32(view, got_offset);
2568 break;
2569
2570 case elfcpp::R_386_GOTOFF:
2571 {
2572 elfcpp::Elf_types<32>::Elf_Addr value;
2573 value = (psymval->value(object, 0)
2574 - target->got_plt_section()->address());
2575 Relocate_functions<32, false>::rel32(view, value);
2576 }
2577 break;
2578
2579 case elfcpp::R_386_GOTPC:
2580 {
2581 elfcpp::Elf_types<32>::Elf_Addr value;
2582 value = target->got_plt_section()->address();
2583 Relocate_functions<32, false>::pcrel32(view, value, address);
2584 }
2585 break;
2586
2587 case elfcpp::R_386_COPY:
2588 case elfcpp::R_386_GLOB_DAT:
2589 case elfcpp::R_386_JUMP_SLOT:
2590 case elfcpp::R_386_RELATIVE:
2591 case elfcpp::R_386_IRELATIVE:
2592 // These are outstanding tls relocs, which are unexpected when
2593 // linking.
2594 case elfcpp::R_386_TLS_TPOFF:
2595 case elfcpp::R_386_TLS_DTPMOD32:
2596 case elfcpp::R_386_TLS_DTPOFF32:
2597 case elfcpp::R_386_TLS_TPOFF32:
2598 case elfcpp::R_386_TLS_DESC:
2599 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2600 _("unexpected reloc %u in object file"),
2601 r_type);
2602 break;
2603
2604 // These are initial tls relocs, which are expected when
2605 // linking.
2606 case elfcpp::R_386_TLS_GD: // Global-dynamic
2607 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2608 case elfcpp::R_386_TLS_DESC_CALL:
2609 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2610 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2611 case elfcpp::R_386_TLS_IE: // Initial-exec
2612 case elfcpp::R_386_TLS_IE_32:
2613 case elfcpp::R_386_TLS_GOTIE:
2614 case elfcpp::R_386_TLS_LE: // Local-exec
2615 case elfcpp::R_386_TLS_LE_32:
2616 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2617 view, address, view_size);
2618 break;
2619
2620 case elfcpp::R_386_32PLT:
2621 case elfcpp::R_386_TLS_GD_32:
2622 case elfcpp::R_386_TLS_GD_PUSH:
2623 case elfcpp::R_386_TLS_GD_CALL:
2624 case elfcpp::R_386_TLS_GD_POP:
2625 case elfcpp::R_386_TLS_LDM_32:
2626 case elfcpp::R_386_TLS_LDM_PUSH:
2627 case elfcpp::R_386_TLS_LDM_CALL:
2628 case elfcpp::R_386_TLS_LDM_POP:
2629 case elfcpp::R_386_USED_BY_INTEL_200:
2630 default:
2631 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2632 _("unsupported reloc %u"),
2633 r_type);
2634 break;
2635 }
2636
2637 return true;
2638 }
2639
2640 // Perform a TLS relocation.
2641
2642 inline void
2643 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2644 Target_i386* target,
2645 size_t relnum,
2646 const elfcpp::Rel<32, false>& rel,
2647 unsigned int r_type,
2648 const Sized_symbol<32>* gsym,
2649 const Symbol_value<32>* psymval,
2650 unsigned char* view,
2651 elfcpp::Elf_types<32>::Elf_Addr,
2652 section_size_type view_size)
2653 {
2654 Output_segment* tls_segment = relinfo->layout->tls_segment();
2655
2656 const Sized_relobj_file<32, false>* object = relinfo->object;
2657
2658 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2659
2660 const bool is_final = (gsym == NULL
2661 ? !parameters->options().shared()
2662 : gsym->final_value_is_known());
2663 const tls::Tls_optimization optimized_type
2664 = Target_i386::optimize_tls_reloc(is_final, r_type);
2665 switch (r_type)
2666 {
2667 case elfcpp::R_386_TLS_GD: // Global-dynamic
2668 if (optimized_type == tls::TLSOPT_TO_LE)
2669 {
2670 if (tls_segment == NULL)
2671 {
2672 gold_assert(parameters->errors()->error_count() > 0
2673 || issue_undefined_symbol_error(gsym));
2674 return;
2675 }
2676 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2677 rel, r_type, value, view,
2678 view_size);
2679 break;
2680 }
2681 else
2682 {
2683 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2684 ? GOT_TYPE_TLS_NOFFSET
2685 : GOT_TYPE_TLS_PAIR);
2686 unsigned int got_offset;
2687 if (gsym != NULL)
2688 {
2689 gold_assert(gsym->has_got_offset(got_type));
2690 got_offset = gsym->got_offset(got_type) - target->got_size();
2691 }
2692 else
2693 {
2694 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2695 gold_assert(object->local_has_got_offset(r_sym, got_type));
2696 got_offset = (object->local_got_offset(r_sym, got_type)
2697 - target->got_size());
2698 }
2699 if (optimized_type == tls::TLSOPT_TO_IE)
2700 {
2701 if (tls_segment == NULL)
2702 {
2703 gold_assert(parameters->errors()->error_count() > 0
2704 || issue_undefined_symbol_error(gsym));
2705 return;
2706 }
2707 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2708 got_offset, view, view_size);
2709 break;
2710 }
2711 else if (optimized_type == tls::TLSOPT_NONE)
2712 {
2713 // Relocate the field with the offset of the pair of GOT
2714 // entries.
2715 Relocate_functions<32, false>::rel32(view, got_offset);
2716 break;
2717 }
2718 }
2719 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2720 _("unsupported reloc %u"),
2721 r_type);
2722 break;
2723
2724 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2725 case elfcpp::R_386_TLS_DESC_CALL:
2726 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2727 if (optimized_type == tls::TLSOPT_TO_LE)
2728 {
2729 if (tls_segment == NULL)
2730 {
2731 gold_assert(parameters->errors()->error_count() > 0
2732 || issue_undefined_symbol_error(gsym));
2733 return;
2734 }
2735 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2736 rel, r_type, value, view,
2737 view_size);
2738 break;
2739 }
2740 else
2741 {
2742 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2743 ? GOT_TYPE_TLS_NOFFSET
2744 : GOT_TYPE_TLS_DESC);
2745 unsigned int got_offset = 0;
2746 if (r_type == elfcpp::R_386_TLS_GOTDESC
2747 && optimized_type == tls::TLSOPT_NONE)
2748 {
2749 // We created GOT entries in the .got.tlsdesc portion of
2750 // the .got.plt section, but the offset stored in the
2751 // symbol is the offset within .got.tlsdesc.
2752 got_offset = (target->got_size()
2753 + target->got_plt_section()->data_size());
2754 }
2755 if (gsym != NULL)
2756 {
2757 gold_assert(gsym->has_got_offset(got_type));
2758 got_offset += gsym->got_offset(got_type) - target->got_size();
2759 }
2760 else
2761 {
2762 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2763 gold_assert(object->local_has_got_offset(r_sym, got_type));
2764 got_offset += (object->local_got_offset(r_sym, got_type)
2765 - target->got_size());
2766 }
2767 if (optimized_type == tls::TLSOPT_TO_IE)
2768 {
2769 if (tls_segment == NULL)
2770 {
2771 gold_assert(parameters->errors()->error_count() > 0
2772 || issue_undefined_symbol_error(gsym));
2773 return;
2774 }
2775 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2776 got_offset, view, view_size);
2777 break;
2778 }
2779 else if (optimized_type == tls::TLSOPT_NONE)
2780 {
2781 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2782 {
2783 // Relocate the field with the offset of the pair of GOT
2784 // entries.
2785 Relocate_functions<32, false>::rel32(view, got_offset);
2786 }
2787 break;
2788 }
2789 }
2790 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2791 _("unsupported reloc %u"),
2792 r_type);
2793 break;
2794
2795 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2796 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2797 {
2798 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2799 _("both SUN and GNU model "
2800 "TLS relocations"));
2801 break;
2802 }
2803 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2804 if (optimized_type == tls::TLSOPT_TO_LE)
2805 {
2806 if (tls_segment == NULL)
2807 {
2808 gold_assert(parameters->errors()->error_count() > 0
2809 || issue_undefined_symbol_error(gsym));
2810 return;
2811 }
2812 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2813 value, view, view_size);
2814 break;
2815 }
2816 else if (optimized_type == tls::TLSOPT_NONE)
2817 {
2818 // Relocate the field with the offset of the GOT entry for
2819 // the module index.
2820 unsigned int got_offset;
2821 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2822 - target->got_size());
2823 Relocate_functions<32, false>::rel32(view, got_offset);
2824 break;
2825 }
2826 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2827 _("unsupported reloc %u"),
2828 r_type);
2829 break;
2830
2831 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2832 if (optimized_type == tls::TLSOPT_TO_LE)
2833 {
2834 // This reloc can appear in debugging sections, in which
2835 // case we must not convert to local-exec. We decide what
2836 // to do based on whether the section is marked as
2837 // containing executable code. That is what the GNU linker
2838 // does as well.
2839 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2840 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2841 {
2842 if (tls_segment == NULL)
2843 {
2844 gold_assert(parameters->errors()->error_count() > 0
2845 || issue_undefined_symbol_error(gsym));
2846 return;
2847 }
2848 value -= tls_segment->memsz();
2849 }
2850 }
2851 Relocate_functions<32, false>::rel32(view, value);
2852 break;
2853
2854 case elfcpp::R_386_TLS_IE: // Initial-exec
2855 case elfcpp::R_386_TLS_GOTIE:
2856 case elfcpp::R_386_TLS_IE_32:
2857 if (optimized_type == tls::TLSOPT_TO_LE)
2858 {
2859 if (tls_segment == NULL)
2860 {
2861 gold_assert(parameters->errors()->error_count() > 0
2862 || issue_undefined_symbol_error(gsym));
2863 return;
2864 }
2865 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2866 rel, r_type, value, view,
2867 view_size);
2868 break;
2869 }
2870 else if (optimized_type == tls::TLSOPT_NONE)
2871 {
2872 // Relocate the field with the offset of the GOT entry for
2873 // the tp-relative offset of the symbol.
2874 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2875 ? GOT_TYPE_TLS_OFFSET
2876 : GOT_TYPE_TLS_NOFFSET);
2877 unsigned int got_offset;
2878 if (gsym != NULL)
2879 {
2880 gold_assert(gsym->has_got_offset(got_type));
2881 got_offset = gsym->got_offset(got_type);
2882 }
2883 else
2884 {
2885 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2886 gold_assert(object->local_has_got_offset(r_sym, got_type));
2887 got_offset = object->local_got_offset(r_sym, got_type);
2888 }
2889 // For the R_386_TLS_IE relocation, we need to apply the
2890 // absolute address of the GOT entry.
2891 if (r_type == elfcpp::R_386_TLS_IE)
2892 got_offset += target->got_plt_section()->address();
2893 // All GOT offsets are relative to the end of the GOT.
2894 got_offset -= target->got_size();
2895 Relocate_functions<32, false>::rel32(view, got_offset);
2896 break;
2897 }
2898 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2899 _("unsupported reloc %u"),
2900 r_type);
2901 break;
2902
2903 case elfcpp::R_386_TLS_LE: // Local-exec
2904 // If we're creating a shared library, a dynamic relocation will
2905 // have been created for this location, so do not apply it now.
2906 if (!parameters->options().shared())
2907 {
2908 if (tls_segment == NULL)
2909 {
2910 gold_assert(parameters->errors()->error_count() > 0
2911 || issue_undefined_symbol_error(gsym));
2912 return;
2913 }
2914 value -= tls_segment->memsz();
2915 Relocate_functions<32, false>::rel32(view, value);
2916 }
2917 break;
2918
2919 case elfcpp::R_386_TLS_LE_32:
2920 // If we're creating a shared library, a dynamic relocation will
2921 // have been created for this location, so do not apply it now.
2922 if (!parameters->options().shared())
2923 {
2924 if (tls_segment == NULL)
2925 {
2926 gold_assert(parameters->errors()->error_count() > 0
2927 || issue_undefined_symbol_error(gsym));
2928 return;
2929 }
2930 value = tls_segment->memsz() - value;
2931 Relocate_functions<32, false>::rel32(view, value);
2932 }
2933 break;
2934 }
2935 }
2936
2937 // Do a relocation in which we convert a TLS General-Dynamic to a
2938 // Local-Exec.
2939
2940 inline void
2941 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2942 size_t relnum,
2943 Output_segment* tls_segment,
2944 const elfcpp::Rel<32, false>& rel,
2945 unsigned int,
2946 elfcpp::Elf_types<32>::Elf_Addr value,
2947 unsigned char* view,
2948 section_size_type view_size)
2949 {
2950 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2951 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2952 // leal foo(%reg),%eax; call ___tls_get_addr
2953 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2954
2955 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2956 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2957
2958 unsigned char op1 = view[-1];
2959 unsigned char op2 = view[-2];
2960
2961 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2962 op2 == 0x8d || op2 == 0x04);
2963 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2964
2965 int roff = 5;
2966
2967 if (op2 == 0x04)
2968 {
2969 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2970 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2971 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2972 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2973 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2974 }
2975 else
2976 {
2977 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2978 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2979 if (rel.get_r_offset() + 9 < view_size
2980 && view[9] == 0x90)
2981 {
2982 // There is a trailing nop. Use the size byte subl.
2983 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2984 roff = 6;
2985 }
2986 else
2987 {
2988 // Use the five byte subl.
2989 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2990 }
2991 }
2992
2993 value = tls_segment->memsz() - value;
2994 Relocate_functions<32, false>::rel32(view + roff, value);
2995
2996 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2997 // We can skip it.
2998 this->skip_call_tls_get_addr_ = true;
2999 }
3000
3001 // Do a relocation in which we convert a TLS General-Dynamic to an
3002 // Initial-Exec.
3003
3004 inline void
3005 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3006 size_t relnum,
3007 Output_segment*,
3008 const elfcpp::Rel<32, false>& rel,
3009 unsigned int,
3010 elfcpp::Elf_types<32>::Elf_Addr value,
3011 unsigned char* view,
3012 section_size_type view_size)
3013 {
3014 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3015 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3016
3017 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3018 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3019
3020 unsigned char op1 = view[-1];
3021 unsigned char op2 = view[-2];
3022
3023 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3024 op2 == 0x8d || op2 == 0x04);
3025 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3026
3027 int roff = 5;
3028
3029 // FIXME: For now, support only the first (SIB) form.
3030 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
3031
3032 if (op2 == 0x04)
3033 {
3034 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3035 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3036 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3037 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3038 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3039 }
3040 else
3041 {
3042 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3043 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3044 if (rel.get_r_offset() + 9 < view_size
3045 && view[9] == 0x90)
3046 {
3047 // FIXME: This is not the right instruction sequence.
3048 // There is a trailing nop. Use the size byte subl.
3049 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3050 roff = 6;
3051 }
3052 else
3053 {
3054 // FIXME: This is not the right instruction sequence.
3055 // Use the five byte subl.
3056 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3057 }
3058 }
3059
3060 Relocate_functions<32, false>::rel32(view + roff, value);
3061
3062 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3063 // We can skip it.
3064 this->skip_call_tls_get_addr_ = true;
3065 }
3066
3067 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3068 // General-Dynamic to a Local-Exec.
3069
3070 inline void
3071 Target_i386::Relocate::tls_desc_gd_to_le(
3072 const Relocate_info<32, false>* relinfo,
3073 size_t relnum,
3074 Output_segment* tls_segment,
3075 const elfcpp::Rel<32, false>& rel,
3076 unsigned int r_type,
3077 elfcpp::Elf_types<32>::Elf_Addr value,
3078 unsigned char* view,
3079 section_size_type view_size)
3080 {
3081 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3082 {
3083 // leal foo@TLSDESC(%ebx), %eax
3084 // ==> leal foo@NTPOFF, %eax
3085 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3086 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3087 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3088 view[-2] == 0x8d && view[-1] == 0x83);
3089 view[-1] = 0x05;
3090 value -= tls_segment->memsz();
3091 Relocate_functions<32, false>::rel32(view, value);
3092 }
3093 else
3094 {
3095 // call *foo@TLSCALL(%eax)
3096 // ==> nop; nop
3097 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3098 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3099 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3100 view[0] == 0xff && view[1] == 0x10);
3101 view[0] = 0x66;
3102 view[1] = 0x90;
3103 }
3104 }
3105
3106 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3107 // General-Dynamic to an Initial-Exec.
3108
3109 inline void
3110 Target_i386::Relocate::tls_desc_gd_to_ie(
3111 const Relocate_info<32, false>* relinfo,
3112 size_t relnum,
3113 Output_segment*,
3114 const elfcpp::Rel<32, false>& rel,
3115 unsigned int r_type,
3116 elfcpp::Elf_types<32>::Elf_Addr value,
3117 unsigned char* view,
3118 section_size_type view_size)
3119 {
3120 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3121 {
3122 // leal foo@TLSDESC(%ebx), %eax
3123 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3124 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3125 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3126 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3127 view[-2] == 0x8d && view[-1] == 0x83);
3128 view[-2] = 0x8b;
3129 Relocate_functions<32, false>::rel32(view, value);
3130 }
3131 else
3132 {
3133 // call *foo@TLSCALL(%eax)
3134 // ==> nop; nop
3135 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3136 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3137 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3138 view[0] == 0xff && view[1] == 0x10);
3139 view[0] = 0x66;
3140 view[1] = 0x90;
3141 }
3142 }
3143
3144 // Do a relocation in which we convert a TLS Local-Dynamic to a
3145 // Local-Exec.
3146
3147 inline void
3148 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3149 size_t relnum,
3150 Output_segment*,
3151 const elfcpp::Rel<32, false>& rel,
3152 unsigned int,
3153 elfcpp::Elf_types<32>::Elf_Addr,
3154 unsigned char* view,
3155 section_size_type view_size)
3156 {
3157 // leal foo(%reg), %eax; call ___tls_get_addr
3158 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3159
3160 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3161 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3162
3163 // FIXME: Does this test really always pass?
3164 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3165 view[-2] == 0x8d && view[-1] == 0x83);
3166
3167 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3168
3169 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3170
3171 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3172 // We can skip it.
3173 this->skip_call_tls_get_addr_ = true;
3174 }
3175
3176 // Do a relocation in which we convert a TLS Initial-Exec to a
3177 // Local-Exec.
3178
3179 inline void
3180 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3181 size_t relnum,
3182 Output_segment* tls_segment,
3183 const elfcpp::Rel<32, false>& rel,
3184 unsigned int r_type,
3185 elfcpp::Elf_types<32>::Elf_Addr value,
3186 unsigned char* view,
3187 section_size_type view_size)
3188 {
3189 // We have to actually change the instructions, which means that we
3190 // need to examine the opcodes to figure out which instruction we
3191 // are looking at.
3192 if (r_type == elfcpp::R_386_TLS_IE)
3193 {
3194 // movl %gs:XX,%eax ==> movl $YY,%eax
3195 // movl %gs:XX,%reg ==> movl $YY,%reg
3196 // addl %gs:XX,%reg ==> addl $YY,%reg
3197 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3198 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3199
3200 unsigned char op1 = view[-1];
3201 if (op1 == 0xa1)
3202 {
3203 // movl XX,%eax ==> movl $YY,%eax
3204 view[-1] = 0xb8;
3205 }
3206 else
3207 {
3208 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3209
3210 unsigned char op2 = view[-2];
3211 if (op2 == 0x8b)
3212 {
3213 // movl XX,%reg ==> movl $YY,%reg
3214 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3215 (op1 & 0xc7) == 0x05);
3216 view[-2] = 0xc7;
3217 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3218 }
3219 else if (op2 == 0x03)
3220 {
3221 // addl XX,%reg ==> addl $YY,%reg
3222 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3223 (op1 & 0xc7) == 0x05);
3224 view[-2] = 0x81;
3225 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3226 }
3227 else
3228 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3229 }
3230 }
3231 else
3232 {
3233 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3234 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3235 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3236 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3237 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3238
3239 unsigned char op1 = view[-1];
3240 unsigned char op2 = view[-2];
3241 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3242 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3243 if (op2 == 0x8b)
3244 {
3245 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3246 view[-2] = 0xc7;
3247 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3248 }
3249 else if (op2 == 0x2b)
3250 {
3251 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3252 view[-2] = 0x81;
3253 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3254 }
3255 else if (op2 == 0x03)
3256 {
3257 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3258 view[-2] = 0x81;
3259 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3260 }
3261 else
3262 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3263 }
3264
3265 value = tls_segment->memsz() - value;
3266 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3267 value = - value;
3268
3269 Relocate_functions<32, false>::rel32(view, value);
3270 }
3271
3272 // Relocate section data.
3273
3274 void
3275 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3276 unsigned int sh_type,
3277 const unsigned char* prelocs,
3278 size_t reloc_count,
3279 Output_section* output_section,
3280 bool needs_special_offset_handling,
3281 unsigned char* view,
3282 elfcpp::Elf_types<32>::Elf_Addr address,
3283 section_size_type view_size,
3284 const Reloc_symbol_changes* reloc_symbol_changes)
3285 {
3286 gold_assert(sh_type == elfcpp::SHT_REL);
3287
3288 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3289 Target_i386::Relocate>(
3290 relinfo,
3291 this,
3292 prelocs,
3293 reloc_count,
3294 output_section,
3295 needs_special_offset_handling,
3296 view,
3297 address,
3298 view_size,
3299 reloc_symbol_changes);
3300 }
3301
3302 // Return the size of a relocation while scanning during a relocatable
3303 // link.
3304
3305 unsigned int
3306 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3307 unsigned int r_type,
3308 Relobj* object)
3309 {
3310 switch (r_type)
3311 {
3312 case elfcpp::R_386_NONE:
3313 case elfcpp::R_386_GNU_VTINHERIT:
3314 case elfcpp::R_386_GNU_VTENTRY:
3315 case elfcpp::R_386_TLS_GD: // Global-dynamic
3316 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3317 case elfcpp::R_386_TLS_DESC_CALL:
3318 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3319 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3320 case elfcpp::R_386_TLS_IE: // Initial-exec
3321 case elfcpp::R_386_TLS_IE_32:
3322 case elfcpp::R_386_TLS_GOTIE:
3323 case elfcpp::R_386_TLS_LE: // Local-exec
3324 case elfcpp::R_386_TLS_LE_32:
3325 return 0;
3326
3327 case elfcpp::R_386_32:
3328 case elfcpp::R_386_PC32:
3329 case elfcpp::R_386_GOT32:
3330 case elfcpp::R_386_PLT32:
3331 case elfcpp::R_386_GOTOFF:
3332 case elfcpp::R_386_GOTPC:
3333 return 4;
3334
3335 case elfcpp::R_386_16:
3336 case elfcpp::R_386_PC16:
3337 return 2;
3338
3339 case elfcpp::R_386_8:
3340 case elfcpp::R_386_PC8:
3341 return 1;
3342
3343 // These are relocations which should only be seen by the
3344 // dynamic linker, and should never be seen here.
3345 case elfcpp::R_386_COPY:
3346 case elfcpp::R_386_GLOB_DAT:
3347 case elfcpp::R_386_JUMP_SLOT:
3348 case elfcpp::R_386_RELATIVE:
3349 case elfcpp::R_386_IRELATIVE:
3350 case elfcpp::R_386_TLS_TPOFF:
3351 case elfcpp::R_386_TLS_DTPMOD32:
3352 case elfcpp::R_386_TLS_DTPOFF32:
3353 case elfcpp::R_386_TLS_TPOFF32:
3354 case elfcpp::R_386_TLS_DESC:
3355 object->error(_("unexpected reloc %u in object file"), r_type);
3356 return 0;
3357
3358 case elfcpp::R_386_32PLT:
3359 case elfcpp::R_386_TLS_GD_32:
3360 case elfcpp::R_386_TLS_GD_PUSH:
3361 case elfcpp::R_386_TLS_GD_CALL:
3362 case elfcpp::R_386_TLS_GD_POP:
3363 case elfcpp::R_386_TLS_LDM_32:
3364 case elfcpp::R_386_TLS_LDM_PUSH:
3365 case elfcpp::R_386_TLS_LDM_CALL:
3366 case elfcpp::R_386_TLS_LDM_POP:
3367 case elfcpp::R_386_USED_BY_INTEL_200:
3368 default:
3369 object->error(_("unsupported reloc %u in object file"), r_type);
3370 return 0;
3371 }
3372 }
3373
3374 // Scan the relocs during a relocatable link.
3375
3376 void
3377 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3378 Layout* layout,
3379 Sized_relobj_file<32, false>* object,
3380 unsigned int data_shndx,
3381 unsigned int sh_type,
3382 const unsigned char* prelocs,
3383 size_t reloc_count,
3384 Output_section* output_section,
3385 bool needs_special_offset_handling,
3386 size_t local_symbol_count,
3387 const unsigned char* plocal_symbols,
3388 Relocatable_relocs* rr)
3389 {
3390 gold_assert(sh_type == elfcpp::SHT_REL);
3391
3392 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3393 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3394
3395 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3396 Scan_relocatable_relocs>(
3397 symtab,
3398 layout,
3399 object,
3400 data_shndx,
3401 prelocs,
3402 reloc_count,
3403 output_section,
3404 needs_special_offset_handling,
3405 local_symbol_count,
3406 plocal_symbols,
3407 rr);
3408 }
3409
3410 // Relocate a section during a relocatable link.
3411
3412 void
3413 Target_i386::relocate_for_relocatable(
3414 const Relocate_info<32, false>* relinfo,
3415 unsigned int sh_type,
3416 const unsigned char* prelocs,
3417 size_t reloc_count,
3418 Output_section* output_section,
3419 off_t offset_in_output_section,
3420 const Relocatable_relocs* rr,
3421 unsigned char* view,
3422 elfcpp::Elf_types<32>::Elf_Addr view_address,
3423 section_size_type view_size,
3424 unsigned char* reloc_view,
3425 section_size_type reloc_view_size)
3426 {
3427 gold_assert(sh_type == elfcpp::SHT_REL);
3428
3429 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3430 relinfo,
3431 prelocs,
3432 reloc_count,
3433 output_section,
3434 offset_in_output_section,
3435 rr,
3436 view,
3437 view_address,
3438 view_size,
3439 reloc_view,
3440 reloc_view_size);
3441 }
3442
3443 // Return the value to use for a dynamic which requires special
3444 // treatment. This is how we support equality comparisons of function
3445 // pointers across shared library boundaries, as described in the
3446 // processor specific ABI supplement.
3447
3448 uint64_t
3449 Target_i386::do_dynsym_value(const Symbol* gsym) const
3450 {
3451 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3452 return this->plt_address_for_global(gsym) + gsym->plt_offset();
3453 }
3454
3455 // Return a string used to fill a code section with nops to take up
3456 // the specified length.
3457
3458 std::string
3459 Target_i386::do_code_fill(section_size_type length) const
3460 {
3461 if (length >= 16)
3462 {
3463 // Build a jmp instruction to skip over the bytes.
3464 unsigned char jmp[5];
3465 jmp[0] = 0xe9;
3466 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3467 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3468 + std::string(length - 5, '\0'));
3469 }
3470
3471 // Nop sequences of various lengths.
3472 const char nop1[1] = { 0x90 }; // nop
3473 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3474 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3475 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3476 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3477 0x00 }; // leal 0(%esi,1),%esi
3478 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3479 0x00, 0x00 };
3480 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3481 0x00, 0x00, 0x00 };
3482 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3483 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3484 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3485 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3486 0x00 };
3487 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3488 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3489 0x00, 0x00 };
3490 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3491 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3492 0x00, 0x00, 0x00 };
3493 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3494 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3495 0x00, 0x00, 0x00, 0x00 };
3496 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3497 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3498 0x27, 0x00, 0x00, 0x00,
3499 0x00 };
3500 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3501 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3502 0xbc, 0x27, 0x00, 0x00,
3503 0x00, 0x00 };
3504 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3505 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3506 0x90, 0x90, 0x90, 0x90,
3507 0x90, 0x90, 0x90 };
3508
3509 const char* nops[16] = {
3510 NULL,
3511 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3512 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3513 };
3514
3515 return std::string(nops[length], length);
3516 }
3517
3518 // Return the value to use for the base of a DW_EH_PE_datarel offset
3519 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3520 // assembler can not write out the difference between two labels in
3521 // different sections, so instead of using a pc-relative value they
3522 // use an offset from the GOT.
3523
3524 uint64_t
3525 Target_i386::do_ehframe_datarel_base() const
3526 {
3527 gold_assert(this->global_offset_table_ != NULL);
3528 Symbol* sym = this->global_offset_table_;
3529 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3530 return ssym->value();
3531 }
3532
3533 // Return whether SYM should be treated as a call to a non-split
3534 // function. We don't want that to be true of a call to a
3535 // get_pc_thunk function.
3536
3537 bool
3538 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3539 {
3540 return (sym->type() == elfcpp::STT_FUNC
3541 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3542 }
3543
3544 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3545 // compiled with -fsplit-stack. The function calls non-split-stack
3546 // code. We have to change the function so that it always ensures
3547 // that it has enough stack space to run some random function.
3548
3549 void
3550 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3551 section_offset_type fnoffset,
3552 section_size_type fnsize,
3553 unsigned char* view,
3554 section_size_type view_size,
3555 std::string* from,
3556 std::string* to) const
3557 {
3558 // The function starts with a comparison of the stack pointer and a
3559 // field in the TCB. This is followed by a jump.
3560
3561 // cmp %gs:NN,%esp
3562 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3563 && fnsize > 7)
3564 {
3565 // We will call __morestack if the carry flag is set after this
3566 // comparison. We turn the comparison into an stc instruction
3567 // and some nops.
3568 view[fnoffset] = '\xf9';
3569 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3570 }
3571 // lea NN(%esp),%ecx
3572 // lea NN(%esp),%edx
3573 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3574 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3575 && fnsize > 7)
3576 {
3577 // This is loading an offset from the stack pointer for a
3578 // comparison. The offset is negative, so we decrease the
3579 // offset by the amount of space we need for the stack. This
3580 // means we will avoid calling __morestack if there happens to
3581 // be plenty of space on the stack already.
3582 unsigned char* pval = view + fnoffset + 3;
3583 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3584 val -= parameters->options().split_stack_adjust_size();
3585 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3586 }
3587 else
3588 {
3589 if (!object->has_no_split_stack())
3590 object->error(_("failed to match split-stack sequence at "
3591 "section %u offset %0zx"),
3592 shndx, static_cast<size_t>(fnoffset));
3593 return;
3594 }
3595
3596 // We have to change the function so that it calls
3597 // __morestack_non_split instead of __morestack. The former will
3598 // allocate additional stack space.
3599 *from = "__morestack";
3600 *to = "__morestack_non_split";
3601 }
3602
3603 // The selector for i386 object files.
3604
3605 class Target_selector_i386 : public Target_selector_freebsd
3606 {
3607 public:
3608 Target_selector_i386()
3609 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3610 "elf32-i386", "elf32-i386-freebsd",
3611 "elf_i386")
3612 { }
3613
3614 Target*
3615 do_instantiate_target()
3616 { return new Target_i386(); }
3617 };
3618
3619 Target_selector_i386 target_selector_i386;
3620
3621 } // End anonymous namespace.
This page took 0.129737 seconds and 5 git commands to generate.