1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
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.
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.
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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 class Output_data_plt_i386;
50 // The i386 target class.
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_i386 : public Target_freebsd<32, false>
58 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
61 : Target_freebsd<32, false>(&i386_info),
62 got_(NULL), plt_(NULL), got_plt_(NULL), global_offset_table_(NULL),
63 rel_dyn_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
64 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
67 // Process the relocations to determine unreferenced sections for
68 // garbage collection.
70 gc_process_relocs(Symbol_table* symtab,
72 Sized_relobj<32, false>* object,
73 unsigned int data_shndx,
75 const unsigned char* prelocs,
77 Output_section* output_section,
78 bool needs_special_offset_handling,
79 size_t local_symbol_count,
80 const unsigned char* plocal_symbols);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(Symbol_table* symtab,
86 Sized_relobj<32, false>* object,
87 unsigned int data_shndx,
89 const unsigned char* prelocs,
91 Output_section* output_section,
92 bool needs_special_offset_handling,
93 size_t local_symbol_count,
94 const unsigned char* plocal_symbols);
96 // Finalize the sections.
98 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
100 // Return the value to use for a dynamic which requires special
103 do_dynsym_value(const Symbol*) const;
105 // Relocate a section.
107 relocate_section(const Relocate_info<32, false>*,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
114 elfcpp::Elf_types<32>::Elf_Addr view_address,
115 section_size_type view_size,
116 const Reloc_symbol_changes*);
118 // Scan the relocs during a relocatable link.
120 scan_relocatable_relocs(Symbol_table* symtab,
122 Sized_relobj<32, false>* object,
123 unsigned int data_shndx,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
127 Output_section* output_section,
128 bool needs_special_offset_handling,
129 size_t local_symbol_count,
130 const unsigned char* plocal_symbols,
131 Relocatable_relocs*);
133 // Relocate a section during a relocatable link.
135 relocate_for_relocatable(const Relocate_info<32, false>*,
136 unsigned int sh_type,
137 const unsigned char* prelocs,
139 Output_section* output_section,
140 off_t offset_in_output_section,
141 const Relocatable_relocs*,
143 elfcpp::Elf_types<32>::Elf_Addr view_address,
144 section_size_type view_size,
145 unsigned char* reloc_view,
146 section_size_type reloc_view_size);
148 // Return a string used to fill a code section with nops.
150 do_code_fill(section_size_type length) const;
152 // Return whether SYM is defined by the ABI.
154 do_is_defined_by_abi(const Symbol* sym) const
155 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
157 // Return whether a symbol name implies a local label. The UnixWare
158 // 2.1 cc generates temporary symbols that start with .X, so we
159 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
160 // If so, we should move the .X recognition into
161 // Target::do_is_local_label_name.
163 do_is_local_label_name(const char* name) const
165 if (name[0] == '.' && name[1] == 'X')
167 return Target::do_is_local_label_name(name);
170 // Adjust -fstack-split code which calls non-stack-split code.
172 do_calls_non_split(Relobj* object, unsigned int shndx,
173 section_offset_type fnoffset, section_size_type fnsize,
174 unsigned char* view, section_size_type view_size,
175 std::string* from, std::string* to) const;
177 // Return the size of the GOT section.
181 gold_assert(this->got_ != NULL);
182 return this->got_->data_size();
186 // The class which scans relocations.
190 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
191 Sized_relobj<32, false>* object,
192 unsigned int data_shndx,
193 Output_section* output_section,
194 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
195 const elfcpp::Sym<32, false>& lsym);
198 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
199 Sized_relobj<32, false>* object,
200 unsigned int data_shndx,
201 Output_section* output_section,
202 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
206 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
209 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
213 // The class which implements relocation.
218 : skip_call_tls_get_addr_(false),
219 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
224 if (this->skip_call_tls_get_addr_)
226 // FIXME: This needs to specify the location somehow.
227 gold_error(_("missing expected TLS relocation"));
231 // Return whether the static relocation needs to be applied.
233 should_apply_static_reloc(const Sized_symbol<32>* gsym,
236 Output_section* output_section);
238 // Do a relocation. Return false if the caller should not issue
239 // any warnings about this relocation.
241 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
242 size_t relnum, const elfcpp::Rel<32, false>&,
243 unsigned int r_type, const Sized_symbol<32>*,
244 const Symbol_value<32>*,
245 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
249 // Do a TLS relocation.
251 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
252 size_t relnum, const elfcpp::Rel<32, false>&,
253 unsigned int r_type, const Sized_symbol<32>*,
254 const Symbol_value<32>*,
255 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
258 // Do a TLS General-Dynamic to Initial-Exec transition.
260 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
261 Output_segment* tls_segment,
262 const elfcpp::Rel<32, false>&, unsigned int r_type,
263 elfcpp::Elf_types<32>::Elf_Addr value,
265 section_size_type view_size);
267 // Do a TLS General-Dynamic to Local-Exec transition.
269 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
270 Output_segment* tls_segment,
271 const elfcpp::Rel<32, false>&, unsigned int r_type,
272 elfcpp::Elf_types<32>::Elf_Addr value,
274 section_size_type view_size);
276 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
279 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
280 Output_segment* tls_segment,
281 const elfcpp::Rel<32, false>&, unsigned int r_type,
282 elfcpp::Elf_types<32>::Elf_Addr value,
284 section_size_type view_size);
286 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
289 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
290 Output_segment* tls_segment,
291 const elfcpp::Rel<32, false>&, unsigned int r_type,
292 elfcpp::Elf_types<32>::Elf_Addr value,
294 section_size_type view_size);
296 // Do a TLS Local-Dynamic to Local-Exec transition.
298 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
299 Output_segment* tls_segment,
300 const elfcpp::Rel<32, false>&, unsigned int r_type,
301 elfcpp::Elf_types<32>::Elf_Addr value,
303 section_size_type view_size);
305 // Do a TLS Initial-Exec to Local-Exec transition.
307 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
308 Output_segment* tls_segment,
309 const elfcpp::Rel<32, false>&, unsigned int r_type,
310 elfcpp::Elf_types<32>::Elf_Addr value,
312 section_size_type view_size);
314 // We need to keep track of which type of local dynamic relocation
315 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
316 enum Local_dynamic_type
323 // This is set if we should skip the next reloc, which should be a
324 // PLT32 reloc against ___tls_get_addr.
325 bool skip_call_tls_get_addr_;
326 // The type of local dynamic relocation we have seen in the section
327 // being relocated, if any.
328 Local_dynamic_type local_dynamic_type_;
331 // A class which returns the size required for a relocation type,
332 // used while scanning relocs during a relocatable link.
333 class Relocatable_size_for_reloc
337 get_size_for_reloc(unsigned int, Relobj*);
340 // Adjust TLS relocation type based on the options and whether this
341 // is a local symbol.
342 static tls::Tls_optimization
343 optimize_tls_reloc(bool is_final, int r_type);
345 // Get the GOT section, creating it if necessary.
346 Output_data_got<32, false>*
347 got_section(Symbol_table*, Layout*);
349 // Get the GOT PLT section.
351 got_plt_section() const
353 gold_assert(this->got_plt_ != NULL);
354 return this->got_plt_;
357 // Create a PLT entry for a global symbol.
359 make_plt_entry(Symbol_table*, Layout*, Symbol*);
361 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
363 define_tls_base_symbol(Symbol_table*, Layout*);
365 // Create a GOT entry for the TLS module index.
367 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
368 Sized_relobj<32, false>* object);
370 // Get the PLT section.
371 Output_data_plt_i386*
374 gold_assert(this->plt_ != NULL);
378 // Get the dynamic reloc section, creating it if necessary.
380 rel_dyn_section(Layout*);
382 // Get the section to use for TLS_DESC relocations.
384 rel_tls_desc_section(Layout*) const;
386 // Add a potential copy relocation.
388 copy_reloc(Symbol_table* symtab, Layout* layout,
389 Sized_relobj<32, false>* object,
390 unsigned int shndx, Output_section* output_section,
391 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
393 this->copy_relocs_.copy_reloc(symtab, layout,
394 symtab->get_sized_symbol<32>(sym),
395 object, shndx, output_section, reloc,
396 this->rel_dyn_section(layout));
399 // Information about this specific target which we pass to the
400 // general Target structure.
401 static const Target::Target_info i386_info;
403 // The types of GOT entries needed for this platform.
406 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
407 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
408 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
409 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
410 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
414 Output_data_got<32, false>* got_;
416 Output_data_plt_i386* plt_;
417 // The GOT PLT section.
418 Output_data_space* got_plt_;
419 // The _GLOBAL_OFFSET_TABLE_ symbol.
420 Symbol* global_offset_table_;
421 // The dynamic reloc section.
422 Reloc_section* rel_dyn_;
423 // Relocs saved to avoid a COPY reloc.
424 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
425 // Space for variables copied with a COPY reloc.
426 Output_data_space* dynbss_;
427 // Offset of the GOT entry for the TLS module index.
428 unsigned int got_mod_index_offset_;
429 // True if the _TLS_MODULE_BASE_ symbol has been defined.
430 bool tls_base_symbol_defined_;
433 const Target::Target_info Target_i386::i386_info =
436 false, // is_big_endian
437 elfcpp::EM_386, // machine_code
438 false, // has_make_symbol
439 false, // has_resolve
440 true, // has_code_fill
441 true, // is_default_stack_executable
443 "/usr/lib/libc.so.1", // dynamic_linker
444 0x08048000, // default_text_segment_address
445 0x1000, // abi_pagesize (overridable by -z max-page-size)
446 0x1000, // common_pagesize (overridable by -z common-page-size)
447 elfcpp::SHN_UNDEF, // small_common_shndx
448 elfcpp::SHN_UNDEF, // large_common_shndx
449 0, // small_common_section_flags
450 0, // large_common_section_flags
451 NULL, // attributes_section
452 NULL // attributes_vendor
455 // Get the GOT section, creating it if necessary.
457 Output_data_got<32, false>*
458 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
460 if (this->got_ == NULL)
462 gold_assert(symtab != NULL && layout != NULL);
464 this->got_ = new Output_data_got<32, false>();
467 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
469 | elfcpp::SHF_WRITE),
470 this->got_, false, true, true,
473 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
474 os = layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
476 | elfcpp::SHF_WRITE),
477 this->got_plt_, false, false, false,
480 // The first three entries are reserved.
481 this->got_plt_->set_current_data_size(3 * 4);
483 // Those bytes can go into the relro segment.
484 layout->increase_relro(3 * 4);
486 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
487 this->global_offset_table_ =
488 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
489 Symbol_table::PREDEFINED,
491 0, 0, elfcpp::STT_OBJECT,
493 elfcpp::STV_HIDDEN, 0,
500 // Get the dynamic reloc section, creating it if necessary.
502 Target_i386::Reloc_section*
503 Target_i386::rel_dyn_section(Layout* layout)
505 if (this->rel_dyn_ == NULL)
507 gold_assert(layout != NULL);
508 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
509 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
510 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
511 false, false, false);
513 return this->rel_dyn_;
516 // A class to handle the PLT data.
518 class Output_data_plt_i386 : public Output_section_data
521 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
523 Output_data_plt_i386(Layout*, Output_data_space*);
525 // Add an entry to the PLT.
527 add_entry(Symbol* gsym);
529 // Return the .rel.plt section data.
532 { return this->rel_; }
534 // Return where the TLS_DESC relocations should go.
536 rel_tls_desc(Layout*);
540 do_adjust_output_section(Output_section* os);
542 // Write to a map file.
544 do_print_to_mapfile(Mapfile* mapfile) const
545 { mapfile->print_output_data(this, _("** PLT")); }
548 // The size of an entry in the PLT.
549 static const int plt_entry_size = 16;
551 // The first entry in the PLT for an executable.
552 static unsigned char exec_first_plt_entry[plt_entry_size];
554 // The first entry in the PLT for a shared object.
555 static unsigned char dyn_first_plt_entry[plt_entry_size];
557 // Other entries in the PLT for an executable.
558 static unsigned char exec_plt_entry[plt_entry_size];
560 // Other entries in the PLT for a shared object.
561 static unsigned char dyn_plt_entry[plt_entry_size];
563 // Set the final size.
565 set_final_data_size()
566 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
568 // Write out the PLT data.
570 do_write(Output_file*);
572 // The reloc section.
574 // The TLS_DESC relocations, if necessary. These must follow the
575 // regular PLT relocs.
576 Reloc_section* tls_desc_rel_;
577 // The .got.plt section.
578 Output_data_space* got_plt_;
579 // The number of PLT entries.
583 // Create the PLT section. The ordinary .got section is an argument,
584 // since we need to refer to the start. We also create our own .got
585 // section just for PLT entries.
587 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
588 Output_data_space* got_plt)
589 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0)
591 this->rel_ = new Reloc_section(false);
592 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
593 elfcpp::SHF_ALLOC, this->rel_, true,
594 false, false, false);
598 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
600 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
601 // linker, and so do we.
605 // Add an entry to the PLT.
608 Output_data_plt_i386::add_entry(Symbol* gsym)
610 gold_assert(!gsym->has_plt_offset());
612 // Note that when setting the PLT offset we skip the initial
613 // reserved PLT entry.
614 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
618 section_offset_type got_offset = this->got_plt_->current_data_size();
620 // Every PLT entry needs a GOT entry which points back to the PLT
621 // entry (this will be changed by the dynamic linker, normally
622 // lazily when the function is called).
623 this->got_plt_->set_current_data_size(got_offset + 4);
625 // Every PLT entry needs a reloc.
626 gsym->set_needs_dynsym_entry();
627 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
630 // Note that we don't need to save the symbol. The contents of the
631 // PLT are independent of which symbols are used. The symbols only
632 // appear in the relocations.
635 // Return where the TLS_DESC relocations should go, creating it if
636 // necessary. These follow the JUMP_SLOT relocations.
638 Output_data_plt_i386::Reloc_section*
639 Output_data_plt_i386::rel_tls_desc(Layout* layout)
641 if (this->tls_desc_rel_ == NULL)
643 this->tls_desc_rel_ = new Reloc_section(false);
644 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
645 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
646 true, false, false, false);
647 gold_assert(this->tls_desc_rel_->output_section() ==
648 this->rel_->output_section());
650 return this->tls_desc_rel_;
653 // The first entry in the PLT for an executable.
655 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
657 0xff, 0x35, // pushl contents of memory address
658 0, 0, 0, 0, // replaced with address of .got + 4
659 0xff, 0x25, // jmp indirect
660 0, 0, 0, 0, // replaced with address of .got + 8
664 // The first entry in the PLT for a shared object.
666 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
668 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
669 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
673 // Subsequent entries in the PLT for an executable.
675 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
677 0xff, 0x25, // jmp indirect
678 0, 0, 0, 0, // replaced with address of symbol in .got
679 0x68, // pushl immediate
680 0, 0, 0, 0, // replaced with offset into relocation table
681 0xe9, // jmp relative
682 0, 0, 0, 0 // replaced with offset to start of .plt
685 // Subsequent entries in the PLT for a shared object.
687 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
689 0xff, 0xa3, // jmp *offset(%ebx)
690 0, 0, 0, 0, // replaced with offset of symbol in .got
691 0x68, // pushl immediate
692 0, 0, 0, 0, // replaced with offset into relocation table
693 0xe9, // jmp relative
694 0, 0, 0, 0 // replaced with offset to start of .plt
697 // Write out the PLT. This uses the hand-coded instructions above,
698 // and adjusts them as needed. This is all specified by the i386 ELF
699 // Processor Supplement.
702 Output_data_plt_i386::do_write(Output_file* of)
704 const off_t offset = this->offset();
705 const section_size_type oview_size =
706 convert_to_section_size_type(this->data_size());
707 unsigned char* const oview = of->get_output_view(offset, oview_size);
709 const off_t got_file_offset = this->got_plt_->offset();
710 const section_size_type got_size =
711 convert_to_section_size_type(this->got_plt_->data_size());
712 unsigned char* const got_view = of->get_output_view(got_file_offset,
715 unsigned char* pov = oview;
717 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
718 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
720 if (parameters->options().output_is_position_independent())
721 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
724 memcpy(pov, exec_first_plt_entry, plt_entry_size);
725 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
726 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
728 pov += plt_entry_size;
730 unsigned char* got_pov = got_view;
732 memset(got_pov, 0, 12);
735 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
737 unsigned int plt_offset = plt_entry_size;
738 unsigned int plt_rel_offset = 0;
739 unsigned int got_offset = 12;
740 const unsigned int count = this->count_;
741 for (unsigned int i = 0;
744 pov += plt_entry_size,
746 plt_offset += plt_entry_size,
747 plt_rel_offset += rel_size,
750 // Set and adjust the PLT entry itself.
752 if (parameters->options().output_is_position_independent())
754 memcpy(pov, dyn_plt_entry, plt_entry_size);
755 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
759 memcpy(pov, exec_plt_entry, plt_entry_size);
760 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
765 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
766 elfcpp::Swap<32, false>::writeval(pov + 12,
767 - (plt_offset + plt_entry_size));
769 // Set the entry in the GOT.
770 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
773 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
774 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
776 of->write_output_view(offset, oview_size, oview);
777 of->write_output_view(got_file_offset, got_size, got_view);
780 // Create a PLT entry for a global symbol.
783 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
785 if (gsym->has_plt_offset())
788 if (this->plt_ == NULL)
790 // Create the GOT sections first.
791 this->got_section(symtab, layout);
793 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
794 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
796 | elfcpp::SHF_EXECINSTR),
797 this->plt_, false, false, false, false);
800 this->plt_->add_entry(gsym);
803 // Get the section to use for TLS_DESC relocations.
805 Target_i386::Reloc_section*
806 Target_i386::rel_tls_desc_section(Layout* layout) const
808 return this->plt_section()->rel_tls_desc(layout);
811 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
814 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
816 if (this->tls_base_symbol_defined_)
819 Output_segment* tls_segment = layout->tls_segment();
820 if (tls_segment != NULL)
822 bool is_exec = parameters->options().output_is_executable();
823 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
824 Symbol_table::PREDEFINED,
828 elfcpp::STV_HIDDEN, 0,
830 ? Symbol::SEGMENT_END
831 : Symbol::SEGMENT_START),
834 this->tls_base_symbol_defined_ = true;
837 // Create a GOT entry for the TLS module index.
840 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
841 Sized_relobj<32, false>* object)
843 if (this->got_mod_index_offset_ == -1U)
845 gold_assert(symtab != NULL && layout != NULL && object != NULL);
846 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
847 Output_data_got<32, false>* got = this->got_section(symtab, layout);
848 unsigned int got_offset = got->add_constant(0);
849 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
851 got->add_constant(0);
852 this->got_mod_index_offset_ = got_offset;
854 return this->got_mod_index_offset_;
857 // Optimize the TLS relocation type based on what we know about the
858 // symbol. IS_FINAL is true if the final address of this symbol is
859 // known at link time.
861 tls::Tls_optimization
862 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
864 // If we are generating a shared library, then we can't do anything
866 if (parameters->options().shared())
867 return tls::TLSOPT_NONE;
871 case elfcpp::R_386_TLS_GD:
872 case elfcpp::R_386_TLS_GOTDESC:
873 case elfcpp::R_386_TLS_DESC_CALL:
874 // These are General-Dynamic which permits fully general TLS
875 // access. Since we know that we are generating an executable,
876 // we can convert this to Initial-Exec. If we also know that
877 // this is a local symbol, we can further switch to Local-Exec.
879 return tls::TLSOPT_TO_LE;
880 return tls::TLSOPT_TO_IE;
882 case elfcpp::R_386_TLS_LDM:
883 // This is Local-Dynamic, which refers to a local symbol in the
884 // dynamic TLS block. Since we know that we generating an
885 // executable, we can switch to Local-Exec.
886 return tls::TLSOPT_TO_LE;
888 case elfcpp::R_386_TLS_LDO_32:
889 // Another type of Local-Dynamic relocation.
890 return tls::TLSOPT_TO_LE;
892 case elfcpp::R_386_TLS_IE:
893 case elfcpp::R_386_TLS_GOTIE:
894 case elfcpp::R_386_TLS_IE_32:
895 // These are Initial-Exec relocs which get the thread offset
896 // from the GOT. If we know that we are linking against the
897 // local symbol, we can switch to Local-Exec, which links the
898 // thread offset into the instruction.
900 return tls::TLSOPT_TO_LE;
901 return tls::TLSOPT_NONE;
903 case elfcpp::R_386_TLS_LE:
904 case elfcpp::R_386_TLS_LE_32:
905 // When we already have Local-Exec, there is nothing further we
907 return tls::TLSOPT_NONE;
914 // Report an unsupported relocation against a local symbol.
917 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
920 gold_error(_("%s: unsupported reloc %u against local symbol"),
921 object->name().c_str(), r_type);
924 // Scan a relocation for a local symbol.
927 Target_i386::Scan::local(Symbol_table* symtab,
930 Sized_relobj<32, false>* object,
931 unsigned int data_shndx,
932 Output_section* output_section,
933 const elfcpp::Rel<32, false>& reloc,
935 const elfcpp::Sym<32, false>& lsym)
939 case elfcpp::R_386_NONE:
940 case elfcpp::R_386_GNU_VTINHERIT:
941 case elfcpp::R_386_GNU_VTENTRY:
944 case elfcpp::R_386_32:
945 // If building a shared library (or a position-independent
946 // executable), we need to create a dynamic relocation for
947 // this location. The relocation applied at link time will
948 // apply the link-time value, so we flag the location with
949 // an R_386_RELATIVE relocation so the dynamic loader can
950 // relocate it easily.
951 if (parameters->options().output_is_position_independent())
953 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
954 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
955 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
956 output_section, data_shndx,
957 reloc.get_r_offset());
961 case elfcpp::R_386_16:
962 case elfcpp::R_386_8:
963 // If building a shared library (or a position-independent
964 // executable), we need to create a dynamic relocation for
965 // this location. Because the addend needs to remain in the
966 // data section, we need to be careful not to apply this
967 // relocation statically.
968 if (parameters->options().output_is_position_independent())
970 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
971 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
972 if (lsym.get_st_type() != elfcpp::STT_SECTION)
973 rel_dyn->add_local(object, r_sym, r_type, output_section,
974 data_shndx, reloc.get_r_offset());
977 gold_assert(lsym.get_st_value() == 0);
978 unsigned int shndx = lsym.get_st_shndx();
980 shndx = object->adjust_sym_shndx(r_sym, shndx,
983 object->error(_("section symbol %u has bad shndx %u"),
986 rel_dyn->add_local_section(object, shndx,
987 r_type, output_section,
988 data_shndx, reloc.get_r_offset());
993 case elfcpp::R_386_PC32:
994 case elfcpp::R_386_PC16:
995 case elfcpp::R_386_PC8:
998 case elfcpp::R_386_PLT32:
999 // Since we know this is a local symbol, we can handle this as a
1003 case elfcpp::R_386_GOTOFF:
1004 case elfcpp::R_386_GOTPC:
1005 // We need a GOT section.
1006 target->got_section(symtab, layout);
1009 case elfcpp::R_386_GOT32:
1011 // The symbol requires a GOT entry.
1012 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1013 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1014 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1016 // If we are generating a shared object, we need to add a
1017 // dynamic RELATIVE relocation for this symbol's GOT entry.
1018 if (parameters->options().output_is_position_independent())
1020 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1021 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1022 rel_dyn->add_local_relative(
1023 object, r_sym, elfcpp::R_386_RELATIVE, got,
1024 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1030 // These are relocations which should only be seen by the
1031 // dynamic linker, and should never be seen here.
1032 case elfcpp::R_386_COPY:
1033 case elfcpp::R_386_GLOB_DAT:
1034 case elfcpp::R_386_JUMP_SLOT:
1035 case elfcpp::R_386_RELATIVE:
1036 case elfcpp::R_386_TLS_TPOFF:
1037 case elfcpp::R_386_TLS_DTPMOD32:
1038 case elfcpp::R_386_TLS_DTPOFF32:
1039 case elfcpp::R_386_TLS_TPOFF32:
1040 case elfcpp::R_386_TLS_DESC:
1041 gold_error(_("%s: unexpected reloc %u in object file"),
1042 object->name().c_str(), r_type);
1045 // These are initial TLS relocs, which are expected when
1047 case elfcpp::R_386_TLS_GD: // Global-dynamic
1048 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1049 case elfcpp::R_386_TLS_DESC_CALL:
1050 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1051 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1052 case elfcpp::R_386_TLS_IE: // Initial-exec
1053 case elfcpp::R_386_TLS_IE_32:
1054 case elfcpp::R_386_TLS_GOTIE:
1055 case elfcpp::R_386_TLS_LE: // Local-exec
1056 case elfcpp::R_386_TLS_LE_32:
1058 bool output_is_shared = parameters->options().shared();
1059 const tls::Tls_optimization optimized_type
1060 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1063 case elfcpp::R_386_TLS_GD: // Global-dynamic
1064 if (optimized_type == tls::TLSOPT_NONE)
1066 // Create a pair of GOT entries for the module index and
1067 // dtv-relative offset.
1068 Output_data_got<32, false>* got
1069 = target->got_section(symtab, layout);
1070 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1071 unsigned int shndx = lsym.get_st_shndx();
1073 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1075 object->error(_("local symbol %u has bad shndx %u"),
1078 got->add_local_pair_with_rel(object, r_sym, shndx,
1080 target->rel_dyn_section(layout),
1081 elfcpp::R_386_TLS_DTPMOD32, 0);
1083 else if (optimized_type != tls::TLSOPT_TO_LE)
1084 unsupported_reloc_local(object, r_type);
1087 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1088 target->define_tls_base_symbol(symtab, layout);
1089 if (optimized_type == tls::TLSOPT_NONE)
1091 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1092 Output_data_got<32, false>* got
1093 = target->got_section(symtab, layout);
1094 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1095 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1097 unsigned int got_offset = got->add_constant(0);
1098 // The local symbol value is stored in the second
1100 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1101 // That set the GOT offset of the local symbol to
1102 // point to the second entry, but we want it to
1103 // point to the first.
1104 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1106 Reloc_section* rt = target->rel_tls_desc_section(layout);
1107 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1110 else if (optimized_type != tls::TLSOPT_TO_LE)
1111 unsupported_reloc_local(object, r_type);
1114 case elfcpp::R_386_TLS_DESC_CALL:
1117 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1118 if (optimized_type == tls::TLSOPT_NONE)
1120 // Create a GOT entry for the module index.
1121 target->got_mod_index_entry(symtab, layout, object);
1123 else if (optimized_type != tls::TLSOPT_TO_LE)
1124 unsupported_reloc_local(object, r_type);
1127 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1130 case elfcpp::R_386_TLS_IE: // Initial-exec
1131 case elfcpp::R_386_TLS_IE_32:
1132 case elfcpp::R_386_TLS_GOTIE:
1133 layout->set_has_static_tls();
1134 if (optimized_type == tls::TLSOPT_NONE)
1136 // For the R_386_TLS_IE relocation, we need to create a
1137 // dynamic relocation when building a shared library.
1138 if (r_type == elfcpp::R_386_TLS_IE
1139 && parameters->options().shared())
1141 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1143 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1144 rel_dyn->add_local_relative(object, r_sym,
1145 elfcpp::R_386_RELATIVE,
1146 output_section, data_shndx,
1147 reloc.get_r_offset());
1149 // Create a GOT entry for the tp-relative offset.
1150 Output_data_got<32, false>* got
1151 = target->got_section(symtab, layout);
1152 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1153 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1154 ? elfcpp::R_386_TLS_TPOFF32
1155 : elfcpp::R_386_TLS_TPOFF);
1156 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1157 ? GOT_TYPE_TLS_OFFSET
1158 : GOT_TYPE_TLS_NOFFSET);
1159 got->add_local_with_rel(object, r_sym, got_type,
1160 target->rel_dyn_section(layout),
1163 else if (optimized_type != tls::TLSOPT_TO_LE)
1164 unsupported_reloc_local(object, r_type);
1167 case elfcpp::R_386_TLS_LE: // Local-exec
1168 case elfcpp::R_386_TLS_LE_32:
1169 layout->set_has_static_tls();
1170 if (output_is_shared)
1172 // We need to create a dynamic relocation.
1173 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1174 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1175 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1176 ? elfcpp::R_386_TLS_TPOFF32
1177 : elfcpp::R_386_TLS_TPOFF);
1178 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1179 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1180 data_shndx, reloc.get_r_offset());
1190 case elfcpp::R_386_32PLT:
1191 case elfcpp::R_386_TLS_GD_32:
1192 case elfcpp::R_386_TLS_GD_PUSH:
1193 case elfcpp::R_386_TLS_GD_CALL:
1194 case elfcpp::R_386_TLS_GD_POP:
1195 case elfcpp::R_386_TLS_LDM_32:
1196 case elfcpp::R_386_TLS_LDM_PUSH:
1197 case elfcpp::R_386_TLS_LDM_CALL:
1198 case elfcpp::R_386_TLS_LDM_POP:
1199 case elfcpp::R_386_USED_BY_INTEL_200:
1201 unsupported_reloc_local(object, r_type);
1206 // Report an unsupported relocation against a global symbol.
1209 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1210 unsigned int r_type,
1213 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1214 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1217 // Scan a relocation for a global symbol.
1220 Target_i386::Scan::global(Symbol_table* symtab,
1222 Target_i386* target,
1223 Sized_relobj<32, false>* object,
1224 unsigned int data_shndx,
1225 Output_section* output_section,
1226 const elfcpp::Rel<32, false>& reloc,
1227 unsigned int r_type,
1232 case elfcpp::R_386_NONE:
1233 case elfcpp::R_386_GNU_VTINHERIT:
1234 case elfcpp::R_386_GNU_VTENTRY:
1237 case elfcpp::R_386_32:
1238 case elfcpp::R_386_16:
1239 case elfcpp::R_386_8:
1241 // Make a PLT entry if necessary.
1242 if (gsym->needs_plt_entry())
1244 target->make_plt_entry(symtab, layout, gsym);
1245 // Since this is not a PC-relative relocation, we may be
1246 // taking the address of a function. In that case we need to
1247 // set the entry in the dynamic symbol table to the address of
1249 if (gsym->is_from_dynobj() && !parameters->options().shared())
1250 gsym->set_needs_dynsym_value();
1252 // Make a dynamic relocation if necessary.
1253 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1255 if (gsym->may_need_copy_reloc())
1257 target->copy_reloc(symtab, layout, object,
1258 data_shndx, output_section, gsym, reloc);
1260 else if (r_type == elfcpp::R_386_32
1261 && gsym->can_use_relative_reloc(false))
1263 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1264 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1265 output_section, object,
1266 data_shndx, reloc.get_r_offset());
1270 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1271 rel_dyn->add_global(gsym, r_type, output_section, object,
1272 data_shndx, reloc.get_r_offset());
1278 case elfcpp::R_386_PC32:
1279 case elfcpp::R_386_PC16:
1280 case elfcpp::R_386_PC8:
1282 // Make a PLT entry if necessary.
1283 if (gsym->needs_plt_entry())
1285 // These relocations are used for function calls only in
1286 // non-PIC code. For a 32-bit relocation in a shared library,
1287 // we'll need a text relocation anyway, so we can skip the
1288 // PLT entry and let the dynamic linker bind the call directly
1289 // to the target. For smaller relocations, we should use a
1290 // PLT entry to ensure that the call can reach.
1291 if (!parameters->options().shared()
1292 || r_type != elfcpp::R_386_PC32)
1293 target->make_plt_entry(symtab, layout, gsym);
1295 // Make a dynamic relocation if necessary.
1296 int flags = Symbol::NON_PIC_REF;
1297 if (gsym->is_func())
1298 flags |= Symbol::FUNCTION_CALL;
1299 if (gsym->needs_dynamic_reloc(flags))
1301 if (gsym->may_need_copy_reloc())
1303 target->copy_reloc(symtab, layout, object,
1304 data_shndx, output_section, gsym, reloc);
1308 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1309 rel_dyn->add_global(gsym, r_type, output_section, object,
1310 data_shndx, reloc.get_r_offset());
1316 case elfcpp::R_386_GOT32:
1318 // The symbol requires a GOT entry.
1319 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1320 if (gsym->final_value_is_known())
1321 got->add_global(gsym, GOT_TYPE_STANDARD);
1324 // If this symbol is not fully resolved, we need to add a
1325 // GOT entry with a dynamic relocation.
1326 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1327 if (gsym->is_from_dynobj()
1328 || gsym->is_undefined()
1329 || gsym->is_preemptible())
1330 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1331 rel_dyn, elfcpp::R_386_GLOB_DAT);
1334 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1335 rel_dyn->add_global_relative(
1336 gsym, elfcpp::R_386_RELATIVE, got,
1337 gsym->got_offset(GOT_TYPE_STANDARD));
1343 case elfcpp::R_386_PLT32:
1344 // If the symbol is fully resolved, this is just a PC32 reloc.
1345 // Otherwise we need a PLT entry.
1346 if (gsym->final_value_is_known())
1348 // If building a shared library, we can also skip the PLT entry
1349 // if the symbol is defined in the output file and is protected
1351 if (gsym->is_defined()
1352 && !gsym->is_from_dynobj()
1353 && !gsym->is_preemptible())
1355 target->make_plt_entry(symtab, layout, gsym);
1358 case elfcpp::R_386_GOTOFF:
1359 case elfcpp::R_386_GOTPC:
1360 // We need a GOT section.
1361 target->got_section(symtab, layout);
1364 // These are relocations which should only be seen by the
1365 // dynamic linker, and should never be seen here.
1366 case elfcpp::R_386_COPY:
1367 case elfcpp::R_386_GLOB_DAT:
1368 case elfcpp::R_386_JUMP_SLOT:
1369 case elfcpp::R_386_RELATIVE:
1370 case elfcpp::R_386_TLS_TPOFF:
1371 case elfcpp::R_386_TLS_DTPMOD32:
1372 case elfcpp::R_386_TLS_DTPOFF32:
1373 case elfcpp::R_386_TLS_TPOFF32:
1374 case elfcpp::R_386_TLS_DESC:
1375 gold_error(_("%s: unexpected reloc %u in object file"),
1376 object->name().c_str(), r_type);
1379 // These are initial tls relocs, which are expected when
1381 case elfcpp::R_386_TLS_GD: // Global-dynamic
1382 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1383 case elfcpp::R_386_TLS_DESC_CALL:
1384 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1385 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1386 case elfcpp::R_386_TLS_IE: // Initial-exec
1387 case elfcpp::R_386_TLS_IE_32:
1388 case elfcpp::R_386_TLS_GOTIE:
1389 case elfcpp::R_386_TLS_LE: // Local-exec
1390 case elfcpp::R_386_TLS_LE_32:
1392 const bool is_final = gsym->final_value_is_known();
1393 const tls::Tls_optimization optimized_type
1394 = Target_i386::optimize_tls_reloc(is_final, r_type);
1397 case elfcpp::R_386_TLS_GD: // Global-dynamic
1398 if (optimized_type == tls::TLSOPT_NONE)
1400 // Create a pair of GOT entries for the module index and
1401 // dtv-relative offset.
1402 Output_data_got<32, false>* got
1403 = target->got_section(symtab, layout);
1404 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1405 target->rel_dyn_section(layout),
1406 elfcpp::R_386_TLS_DTPMOD32,
1407 elfcpp::R_386_TLS_DTPOFF32);
1409 else if (optimized_type == tls::TLSOPT_TO_IE)
1411 // Create a GOT entry for the tp-relative offset.
1412 Output_data_got<32, false>* got
1413 = target->got_section(symtab, layout);
1414 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1415 target->rel_dyn_section(layout),
1416 elfcpp::R_386_TLS_TPOFF);
1418 else if (optimized_type != tls::TLSOPT_TO_LE)
1419 unsupported_reloc_global(object, r_type, gsym);
1422 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1423 target->define_tls_base_symbol(symtab, layout);
1424 if (optimized_type == tls::TLSOPT_NONE)
1426 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1427 Output_data_got<32, false>* got
1428 = target->got_section(symtab, layout);
1429 Reloc_section* rt = target->rel_tls_desc_section(layout);
1430 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1431 elfcpp::R_386_TLS_DESC, 0);
1433 else if (optimized_type == tls::TLSOPT_TO_IE)
1435 // Create a GOT entry for the tp-relative offset.
1436 Output_data_got<32, false>* got
1437 = target->got_section(symtab, layout);
1438 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1439 target->rel_dyn_section(layout),
1440 elfcpp::R_386_TLS_TPOFF);
1442 else if (optimized_type != tls::TLSOPT_TO_LE)
1443 unsupported_reloc_global(object, r_type, gsym);
1446 case elfcpp::R_386_TLS_DESC_CALL:
1449 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1450 if (optimized_type == tls::TLSOPT_NONE)
1452 // Create a GOT entry for the module index.
1453 target->got_mod_index_entry(symtab, layout, object);
1455 else if (optimized_type != tls::TLSOPT_TO_LE)
1456 unsupported_reloc_global(object, r_type, gsym);
1459 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1462 case elfcpp::R_386_TLS_IE: // Initial-exec
1463 case elfcpp::R_386_TLS_IE_32:
1464 case elfcpp::R_386_TLS_GOTIE:
1465 layout->set_has_static_tls();
1466 if (optimized_type == tls::TLSOPT_NONE)
1468 // For the R_386_TLS_IE relocation, we need to create a
1469 // dynamic relocation when building a shared library.
1470 if (r_type == elfcpp::R_386_TLS_IE
1471 && parameters->options().shared())
1473 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1474 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1475 output_section, object,
1477 reloc.get_r_offset());
1479 // Create a GOT entry for the tp-relative offset.
1480 Output_data_got<32, false>* got
1481 = target->got_section(symtab, layout);
1482 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1483 ? elfcpp::R_386_TLS_TPOFF32
1484 : elfcpp::R_386_TLS_TPOFF);
1485 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1486 ? GOT_TYPE_TLS_OFFSET
1487 : GOT_TYPE_TLS_NOFFSET);
1488 got->add_global_with_rel(gsym, got_type,
1489 target->rel_dyn_section(layout),
1492 else if (optimized_type != tls::TLSOPT_TO_LE)
1493 unsupported_reloc_global(object, r_type, gsym);
1496 case elfcpp::R_386_TLS_LE: // Local-exec
1497 case elfcpp::R_386_TLS_LE_32:
1498 layout->set_has_static_tls();
1499 if (parameters->options().shared())
1501 // We need to create a dynamic relocation.
1502 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1503 ? elfcpp::R_386_TLS_TPOFF32
1504 : elfcpp::R_386_TLS_TPOFF);
1505 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1506 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1507 data_shndx, reloc.get_r_offset());
1517 case elfcpp::R_386_32PLT:
1518 case elfcpp::R_386_TLS_GD_32:
1519 case elfcpp::R_386_TLS_GD_PUSH:
1520 case elfcpp::R_386_TLS_GD_CALL:
1521 case elfcpp::R_386_TLS_GD_POP:
1522 case elfcpp::R_386_TLS_LDM_32:
1523 case elfcpp::R_386_TLS_LDM_PUSH:
1524 case elfcpp::R_386_TLS_LDM_CALL:
1525 case elfcpp::R_386_TLS_LDM_POP:
1526 case elfcpp::R_386_USED_BY_INTEL_200:
1528 unsupported_reloc_global(object, r_type, gsym);
1533 // Process relocations for gc.
1536 Target_i386::gc_process_relocs(Symbol_table* symtab,
1538 Sized_relobj<32, false>* object,
1539 unsigned int data_shndx,
1541 const unsigned char* prelocs,
1543 Output_section* output_section,
1544 bool needs_special_offset_handling,
1545 size_t local_symbol_count,
1546 const unsigned char* plocal_symbols)
1548 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1558 needs_special_offset_handling,
1563 // Scan relocations for a section.
1566 Target_i386::scan_relocs(Symbol_table* symtab,
1568 Sized_relobj<32, false>* object,
1569 unsigned int data_shndx,
1570 unsigned int sh_type,
1571 const unsigned char* prelocs,
1573 Output_section* output_section,
1574 bool needs_special_offset_handling,
1575 size_t local_symbol_count,
1576 const unsigned char* plocal_symbols)
1578 if (sh_type == elfcpp::SHT_RELA)
1580 gold_error(_("%s: unsupported RELA reloc section"),
1581 object->name().c_str());
1585 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1595 needs_special_offset_handling,
1600 // Finalize the sections.
1603 Target_i386::do_finalize_sections(
1605 const Input_objects*,
1606 Symbol_table* symtab)
1608 const Reloc_section* rel_plt = (this->plt_ == NULL
1610 : this->plt_->rel_plt());
1611 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
1612 this->rel_dyn_, true, false);
1614 // Emit any relocs we saved in an attempt to avoid generating COPY
1616 if (this->copy_relocs_.any_saved_relocs())
1617 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1619 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1620 // the .got.plt section.
1621 Symbol* sym = this->global_offset_table_;
1624 uint32_t data_size = this->got_plt_->current_data_size();
1625 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
1629 // Return whether a direct absolute static relocation needs to be applied.
1630 // In cases where Scan::local() or Scan::global() has created
1631 // a dynamic relocation other than R_386_RELATIVE, the addend
1632 // of the relocation is carried in the data, and we must not
1633 // apply the static relocation.
1636 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1639 Output_section* output_section)
1641 // If the output section is not allocated, then we didn't call
1642 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1644 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1647 // For local symbols, we will have created a non-RELATIVE dynamic
1648 // relocation only if (a) the output is position independent,
1649 // (b) the relocation is absolute (not pc- or segment-relative), and
1650 // (c) the relocation is not 32 bits wide.
1652 return !(parameters->options().output_is_position_independent()
1653 && (ref_flags & Symbol::ABSOLUTE_REF)
1656 // For global symbols, we use the same helper routines used in the
1657 // scan pass. If we did not create a dynamic relocation, or if we
1658 // created a RELATIVE dynamic relocation, we should apply the static
1660 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1661 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1662 && gsym->can_use_relative_reloc(ref_flags
1663 & Symbol::FUNCTION_CALL);
1664 return !has_dyn || is_rel;
1667 // Perform a relocation.
1670 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1671 Target_i386* target,
1672 Output_section *output_section,
1674 const elfcpp::Rel<32, false>& rel,
1675 unsigned int r_type,
1676 const Sized_symbol<32>* gsym,
1677 const Symbol_value<32>* psymval,
1678 unsigned char* view,
1679 elfcpp::Elf_types<32>::Elf_Addr address,
1680 section_size_type view_size)
1682 if (this->skip_call_tls_get_addr_)
1684 if ((r_type != elfcpp::R_386_PLT32
1685 && r_type != elfcpp::R_386_PC32)
1687 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1688 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1689 _("missing expected TLS relocation"));
1692 this->skip_call_tls_get_addr_ = false;
1697 // Pick the value to use for symbols defined in shared objects.
1698 Symbol_value<32> symval;
1700 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1701 || r_type == elfcpp::R_386_PC16
1702 || r_type == elfcpp::R_386_PC32))
1704 symval.set_output_value(target->plt_section()->address()
1705 + gsym->plt_offset());
1709 const Sized_relobj<32, false>* object = relinfo->object;
1711 // Get the GOT offset if needed.
1712 // The GOT pointer points to the end of the GOT section.
1713 // We need to subtract the size of the GOT section to get
1714 // the actual offset to use in the relocation.
1715 bool have_got_offset = false;
1716 unsigned int got_offset = 0;
1719 case elfcpp::R_386_GOT32:
1722 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1723 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1724 - target->got_size());
1728 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1729 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1730 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1731 - target->got_size());
1733 have_got_offset = true;
1742 case elfcpp::R_386_NONE:
1743 case elfcpp::R_386_GNU_VTINHERIT:
1744 case elfcpp::R_386_GNU_VTENTRY:
1747 case elfcpp::R_386_32:
1748 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1750 Relocate_functions<32, false>::rel32(view, object, psymval);
1753 case elfcpp::R_386_PC32:
1755 int ref_flags = Symbol::NON_PIC_REF;
1756 if (gsym != NULL && gsym->is_func())
1757 ref_flags |= Symbol::FUNCTION_CALL;
1758 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1759 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1763 case elfcpp::R_386_16:
1764 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1766 Relocate_functions<32, false>::rel16(view, object, psymval);
1769 case elfcpp::R_386_PC16:
1771 int ref_flags = Symbol::NON_PIC_REF;
1772 if (gsym != NULL && gsym->is_func())
1773 ref_flags |= Symbol::FUNCTION_CALL;
1774 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1775 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1779 case elfcpp::R_386_8:
1780 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1782 Relocate_functions<32, false>::rel8(view, object, psymval);
1785 case elfcpp::R_386_PC8:
1787 int ref_flags = Symbol::NON_PIC_REF;
1788 if (gsym != NULL && gsym->is_func())
1789 ref_flags |= Symbol::FUNCTION_CALL;
1790 if (should_apply_static_reloc(gsym, ref_flags, false,
1792 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1796 case elfcpp::R_386_PLT32:
1797 gold_assert(gsym == NULL
1798 || gsym->has_plt_offset()
1799 || gsym->final_value_is_known()
1800 || (gsym->is_defined()
1801 && !gsym->is_from_dynobj()
1802 && !gsym->is_preemptible()));
1803 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1806 case elfcpp::R_386_GOT32:
1807 gold_assert(have_got_offset);
1808 Relocate_functions<32, false>::rel32(view, got_offset);
1811 case elfcpp::R_386_GOTOFF:
1813 elfcpp::Elf_types<32>::Elf_Addr value;
1814 value = (psymval->value(object, 0)
1815 - target->got_plt_section()->address());
1816 Relocate_functions<32, false>::rel32(view, value);
1820 case elfcpp::R_386_GOTPC:
1822 elfcpp::Elf_types<32>::Elf_Addr value;
1823 value = target->got_plt_section()->address();
1824 Relocate_functions<32, false>::pcrel32(view, value, address);
1828 case elfcpp::R_386_COPY:
1829 case elfcpp::R_386_GLOB_DAT:
1830 case elfcpp::R_386_JUMP_SLOT:
1831 case elfcpp::R_386_RELATIVE:
1832 // These are outstanding tls relocs, which are unexpected when
1834 case elfcpp::R_386_TLS_TPOFF:
1835 case elfcpp::R_386_TLS_DTPMOD32:
1836 case elfcpp::R_386_TLS_DTPOFF32:
1837 case elfcpp::R_386_TLS_TPOFF32:
1838 case elfcpp::R_386_TLS_DESC:
1839 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1840 _("unexpected reloc %u in object file"),
1844 // These are initial tls relocs, which are expected when
1846 case elfcpp::R_386_TLS_GD: // Global-dynamic
1847 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1848 case elfcpp::R_386_TLS_DESC_CALL:
1849 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1850 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1851 case elfcpp::R_386_TLS_IE: // Initial-exec
1852 case elfcpp::R_386_TLS_IE_32:
1853 case elfcpp::R_386_TLS_GOTIE:
1854 case elfcpp::R_386_TLS_LE: // Local-exec
1855 case elfcpp::R_386_TLS_LE_32:
1856 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1857 view, address, view_size);
1860 case elfcpp::R_386_32PLT:
1861 case elfcpp::R_386_TLS_GD_32:
1862 case elfcpp::R_386_TLS_GD_PUSH:
1863 case elfcpp::R_386_TLS_GD_CALL:
1864 case elfcpp::R_386_TLS_GD_POP:
1865 case elfcpp::R_386_TLS_LDM_32:
1866 case elfcpp::R_386_TLS_LDM_PUSH:
1867 case elfcpp::R_386_TLS_LDM_CALL:
1868 case elfcpp::R_386_TLS_LDM_POP:
1869 case elfcpp::R_386_USED_BY_INTEL_200:
1871 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1872 _("unsupported reloc %u"),
1880 // Perform a TLS relocation.
1883 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1884 Target_i386* target,
1886 const elfcpp::Rel<32, false>& rel,
1887 unsigned int r_type,
1888 const Sized_symbol<32>* gsym,
1889 const Symbol_value<32>* psymval,
1890 unsigned char* view,
1891 elfcpp::Elf_types<32>::Elf_Addr,
1892 section_size_type view_size)
1894 Output_segment* tls_segment = relinfo->layout->tls_segment();
1896 const Sized_relobj<32, false>* object = relinfo->object;
1898 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1900 const bool is_final = (gsym == NULL
1901 ? !parameters->options().shared()
1902 : gsym->final_value_is_known());
1903 const tls::Tls_optimization optimized_type
1904 = Target_i386::optimize_tls_reloc(is_final, r_type);
1907 case elfcpp::R_386_TLS_GD: // Global-dynamic
1908 if (optimized_type == tls::TLSOPT_TO_LE)
1910 gold_assert(tls_segment != NULL);
1911 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1912 rel, r_type, value, view,
1918 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1919 ? GOT_TYPE_TLS_NOFFSET
1920 : GOT_TYPE_TLS_PAIR);
1921 unsigned int got_offset;
1924 gold_assert(gsym->has_got_offset(got_type));
1925 got_offset = gsym->got_offset(got_type) - target->got_size();
1929 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1930 gold_assert(object->local_has_got_offset(r_sym, got_type));
1931 got_offset = (object->local_got_offset(r_sym, got_type)
1932 - target->got_size());
1934 if (optimized_type == tls::TLSOPT_TO_IE)
1936 gold_assert(tls_segment != NULL);
1937 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1938 got_offset, view, view_size);
1941 else if (optimized_type == tls::TLSOPT_NONE)
1943 // Relocate the field with the offset of the pair of GOT
1945 Relocate_functions<32, false>::rel32(view, got_offset);
1949 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1950 _("unsupported reloc %u"),
1954 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1955 case elfcpp::R_386_TLS_DESC_CALL:
1956 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1957 if (optimized_type == tls::TLSOPT_TO_LE)
1959 gold_assert(tls_segment != NULL);
1960 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1961 rel, r_type, value, view,
1967 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1968 ? GOT_TYPE_TLS_NOFFSET
1969 : GOT_TYPE_TLS_DESC);
1970 unsigned int got_offset;
1973 gold_assert(gsym->has_got_offset(got_type));
1974 got_offset = gsym->got_offset(got_type) - target->got_size();
1978 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1979 gold_assert(object->local_has_got_offset(r_sym, got_type));
1980 got_offset = (object->local_got_offset(r_sym, got_type)
1981 - target->got_size());
1983 if (optimized_type == tls::TLSOPT_TO_IE)
1985 gold_assert(tls_segment != NULL);
1986 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1987 got_offset, view, view_size);
1990 else if (optimized_type == tls::TLSOPT_NONE)
1992 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1994 // Relocate the field with the offset of the pair of GOT
1996 Relocate_functions<32, false>::rel32(view, got_offset);
2001 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2002 _("unsupported reloc %u"),
2006 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2007 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2009 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2010 _("both SUN and GNU model "
2011 "TLS relocations"));
2014 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2015 if (optimized_type == tls::TLSOPT_TO_LE)
2017 gold_assert(tls_segment != NULL);
2018 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2019 value, view, view_size);
2022 else if (optimized_type == tls::TLSOPT_NONE)
2024 // Relocate the field with the offset of the GOT entry for
2025 // the module index.
2026 unsigned int got_offset;
2027 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2028 - target->got_size());
2029 Relocate_functions<32, false>::rel32(view, got_offset);
2032 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2033 _("unsupported reloc %u"),
2037 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2038 if (optimized_type == tls::TLSOPT_TO_LE)
2040 // This reloc can appear in debugging sections, in which
2041 // case we must not convert to local-exec. We decide what
2042 // to do based on whether the section is marked as
2043 // containing executable code. That is what the GNU linker
2045 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2046 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2048 gold_assert(tls_segment != NULL);
2049 value -= tls_segment->memsz();
2052 Relocate_functions<32, false>::rel32(view, value);
2055 case elfcpp::R_386_TLS_IE: // Initial-exec
2056 case elfcpp::R_386_TLS_GOTIE:
2057 case elfcpp::R_386_TLS_IE_32:
2058 if (optimized_type == tls::TLSOPT_TO_LE)
2060 gold_assert(tls_segment != NULL);
2061 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2062 rel, r_type, value, view,
2066 else if (optimized_type == tls::TLSOPT_NONE)
2068 // Relocate the field with the offset of the GOT entry for
2069 // the tp-relative offset of the symbol.
2070 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2071 ? GOT_TYPE_TLS_OFFSET
2072 : GOT_TYPE_TLS_NOFFSET);
2073 unsigned int got_offset;
2076 gold_assert(gsym->has_got_offset(got_type));
2077 got_offset = gsym->got_offset(got_type);
2081 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2082 gold_assert(object->local_has_got_offset(r_sym, got_type));
2083 got_offset = object->local_got_offset(r_sym, got_type);
2085 // For the R_386_TLS_IE relocation, we need to apply the
2086 // absolute address of the GOT entry.
2087 if (r_type == elfcpp::R_386_TLS_IE)
2088 got_offset += target->got_plt_section()->address();
2089 // All GOT offsets are relative to the end of the GOT.
2090 got_offset -= target->got_size();
2091 Relocate_functions<32, false>::rel32(view, got_offset);
2094 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2095 _("unsupported reloc %u"),
2099 case elfcpp::R_386_TLS_LE: // Local-exec
2100 // If we're creating a shared library, a dynamic relocation will
2101 // have been created for this location, so do not apply it now.
2102 if (!parameters->options().shared())
2104 gold_assert(tls_segment != NULL);
2105 value -= tls_segment->memsz();
2106 Relocate_functions<32, false>::rel32(view, value);
2110 case elfcpp::R_386_TLS_LE_32:
2111 // If we're creating a shared library, a dynamic relocation will
2112 // have been created for this location, so do not apply it now.
2113 if (!parameters->options().shared())
2115 gold_assert(tls_segment != NULL);
2116 value = tls_segment->memsz() - value;
2117 Relocate_functions<32, false>::rel32(view, value);
2123 // Do a relocation in which we convert a TLS General-Dynamic to a
2127 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2129 Output_segment* tls_segment,
2130 const elfcpp::Rel<32, false>& rel,
2132 elfcpp::Elf_types<32>::Elf_Addr value,
2133 unsigned char* view,
2134 section_size_type view_size)
2136 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2137 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2138 // leal foo(%reg),%eax; call ___tls_get_addr
2139 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2141 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2142 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2144 unsigned char op1 = view[-1];
2145 unsigned char op2 = view[-2];
2147 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2148 op2 == 0x8d || op2 == 0x04);
2149 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2155 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2156 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2157 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2158 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2159 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2163 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2164 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2165 if (rel.get_r_offset() + 9 < view_size
2168 // There is a trailing nop. Use the size byte subl.
2169 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2174 // Use the five byte subl.
2175 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2179 value = tls_segment->memsz() - value;
2180 Relocate_functions<32, false>::rel32(view + roff, value);
2182 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2184 this->skip_call_tls_get_addr_ = true;
2187 // Do a relocation in which we convert a TLS General-Dynamic to an
2191 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2194 const elfcpp::Rel<32, false>& rel,
2196 elfcpp::Elf_types<32>::Elf_Addr value,
2197 unsigned char* view,
2198 section_size_type view_size)
2200 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2201 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2203 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2204 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2206 unsigned char op1 = view[-1];
2207 unsigned char op2 = view[-2];
2209 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2210 op2 == 0x8d || op2 == 0x04);
2211 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2215 // FIXME: For now, support only the first (SIB) form.
2216 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2220 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2221 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2222 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2223 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2224 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2228 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2229 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2230 if (rel.get_r_offset() + 9 < view_size
2233 // FIXME: This is not the right instruction sequence.
2234 // There is a trailing nop. Use the size byte subl.
2235 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2240 // FIXME: This is not the right instruction sequence.
2241 // Use the five byte subl.
2242 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2246 Relocate_functions<32, false>::rel32(view + roff, value);
2248 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2250 this->skip_call_tls_get_addr_ = true;
2253 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2254 // General-Dynamic to a Local-Exec.
2257 Target_i386::Relocate::tls_desc_gd_to_le(
2258 const Relocate_info<32, false>* relinfo,
2260 Output_segment* tls_segment,
2261 const elfcpp::Rel<32, false>& rel,
2262 unsigned int r_type,
2263 elfcpp::Elf_types<32>::Elf_Addr value,
2264 unsigned char* view,
2265 section_size_type view_size)
2267 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2269 // leal foo@TLSDESC(%ebx), %eax
2270 // ==> leal foo@NTPOFF, %eax
2271 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2272 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2273 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2274 view[-2] == 0x8d && view[-1] == 0x83);
2276 value -= tls_segment->memsz();
2277 Relocate_functions<32, false>::rel32(view, value);
2281 // call *foo@TLSCALL(%eax)
2283 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2284 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2285 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2286 view[0] == 0xff && view[1] == 0x10);
2292 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2293 // General-Dynamic to an Initial-Exec.
2296 Target_i386::Relocate::tls_desc_gd_to_ie(
2297 const Relocate_info<32, false>* relinfo,
2300 const elfcpp::Rel<32, false>& rel,
2301 unsigned int r_type,
2302 elfcpp::Elf_types<32>::Elf_Addr value,
2303 unsigned char* view,
2304 section_size_type view_size)
2306 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2308 // leal foo@TLSDESC(%ebx), %eax
2309 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2310 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2311 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2312 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2313 view[-2] == 0x8d && view[-1] == 0x83);
2315 Relocate_functions<32, false>::rel32(view, value);
2319 // call *foo@TLSCALL(%eax)
2321 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2322 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2323 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2324 view[0] == 0xff && view[1] == 0x10);
2330 // Do a relocation in which we convert a TLS Local-Dynamic to a
2334 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2337 const elfcpp::Rel<32, false>& rel,
2339 elfcpp::Elf_types<32>::Elf_Addr,
2340 unsigned char* view,
2341 section_size_type view_size)
2343 // leal foo(%reg), %eax; call ___tls_get_addr
2344 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2346 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2347 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2349 // FIXME: Does this test really always pass?
2350 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2351 view[-2] == 0x8d && view[-1] == 0x83);
2353 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2355 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2357 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2359 this->skip_call_tls_get_addr_ = true;
2362 // Do a relocation in which we convert a TLS Initial-Exec to a
2366 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2368 Output_segment* tls_segment,
2369 const elfcpp::Rel<32, false>& rel,
2370 unsigned int r_type,
2371 elfcpp::Elf_types<32>::Elf_Addr value,
2372 unsigned char* view,
2373 section_size_type view_size)
2375 // We have to actually change the instructions, which means that we
2376 // need to examine the opcodes to figure out which instruction we
2378 if (r_type == elfcpp::R_386_TLS_IE)
2380 // movl %gs:XX,%eax ==> movl $YY,%eax
2381 // movl %gs:XX,%reg ==> movl $YY,%reg
2382 // addl %gs:XX,%reg ==> addl $YY,%reg
2383 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2384 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2386 unsigned char op1 = view[-1];
2389 // movl XX,%eax ==> movl $YY,%eax
2394 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2396 unsigned char op2 = view[-2];
2399 // movl XX,%reg ==> movl $YY,%reg
2400 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2401 (op1 & 0xc7) == 0x05);
2403 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2405 else if (op2 == 0x03)
2407 // addl XX,%reg ==> addl $YY,%reg
2408 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2409 (op1 & 0xc7) == 0x05);
2411 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2414 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2419 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2420 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2421 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2422 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2423 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2425 unsigned char op1 = view[-1];
2426 unsigned char op2 = view[-2];
2427 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2428 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2431 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2433 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2435 else if (op2 == 0x2b)
2437 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2439 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2441 else if (op2 == 0x03)
2443 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2445 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2448 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2451 value = tls_segment->memsz() - value;
2452 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2455 Relocate_functions<32, false>::rel32(view, value);
2458 // Relocate section data.
2461 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2462 unsigned int sh_type,
2463 const unsigned char* prelocs,
2465 Output_section* output_section,
2466 bool needs_special_offset_handling,
2467 unsigned char* view,
2468 elfcpp::Elf_types<32>::Elf_Addr address,
2469 section_size_type view_size,
2470 const Reloc_symbol_changes* reloc_symbol_changes)
2472 gold_assert(sh_type == elfcpp::SHT_REL);
2474 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2475 Target_i386::Relocate>(
2481 needs_special_offset_handling,
2485 reloc_symbol_changes);
2488 // Return the size of a relocation while scanning during a relocatable
2492 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2493 unsigned int r_type,
2498 case elfcpp::R_386_NONE:
2499 case elfcpp::R_386_GNU_VTINHERIT:
2500 case elfcpp::R_386_GNU_VTENTRY:
2501 case elfcpp::R_386_TLS_GD: // Global-dynamic
2502 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2503 case elfcpp::R_386_TLS_DESC_CALL:
2504 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2505 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2506 case elfcpp::R_386_TLS_IE: // Initial-exec
2507 case elfcpp::R_386_TLS_IE_32:
2508 case elfcpp::R_386_TLS_GOTIE:
2509 case elfcpp::R_386_TLS_LE: // Local-exec
2510 case elfcpp::R_386_TLS_LE_32:
2513 case elfcpp::R_386_32:
2514 case elfcpp::R_386_PC32:
2515 case elfcpp::R_386_GOT32:
2516 case elfcpp::R_386_PLT32:
2517 case elfcpp::R_386_GOTOFF:
2518 case elfcpp::R_386_GOTPC:
2521 case elfcpp::R_386_16:
2522 case elfcpp::R_386_PC16:
2525 case elfcpp::R_386_8:
2526 case elfcpp::R_386_PC8:
2529 // These are relocations which should only be seen by the
2530 // dynamic linker, and should never be seen here.
2531 case elfcpp::R_386_COPY:
2532 case elfcpp::R_386_GLOB_DAT:
2533 case elfcpp::R_386_JUMP_SLOT:
2534 case elfcpp::R_386_RELATIVE:
2535 case elfcpp::R_386_TLS_TPOFF:
2536 case elfcpp::R_386_TLS_DTPMOD32:
2537 case elfcpp::R_386_TLS_DTPOFF32:
2538 case elfcpp::R_386_TLS_TPOFF32:
2539 case elfcpp::R_386_TLS_DESC:
2540 object->error(_("unexpected reloc %u in object file"), r_type);
2543 case elfcpp::R_386_32PLT:
2544 case elfcpp::R_386_TLS_GD_32:
2545 case elfcpp::R_386_TLS_GD_PUSH:
2546 case elfcpp::R_386_TLS_GD_CALL:
2547 case elfcpp::R_386_TLS_GD_POP:
2548 case elfcpp::R_386_TLS_LDM_32:
2549 case elfcpp::R_386_TLS_LDM_PUSH:
2550 case elfcpp::R_386_TLS_LDM_CALL:
2551 case elfcpp::R_386_TLS_LDM_POP:
2552 case elfcpp::R_386_USED_BY_INTEL_200:
2554 object->error(_("unsupported reloc %u in object file"), r_type);
2559 // Scan the relocs during a relocatable link.
2562 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
2564 Sized_relobj<32, false>* object,
2565 unsigned int data_shndx,
2566 unsigned int sh_type,
2567 const unsigned char* prelocs,
2569 Output_section* output_section,
2570 bool needs_special_offset_handling,
2571 size_t local_symbol_count,
2572 const unsigned char* plocal_symbols,
2573 Relocatable_relocs* rr)
2575 gold_assert(sh_type == elfcpp::SHT_REL);
2577 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2578 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2580 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2581 Scan_relocatable_relocs>(
2589 needs_special_offset_handling,
2595 // Relocate a section during a relocatable link.
2598 Target_i386::relocate_for_relocatable(
2599 const Relocate_info<32, false>* relinfo,
2600 unsigned int sh_type,
2601 const unsigned char* prelocs,
2603 Output_section* output_section,
2604 off_t offset_in_output_section,
2605 const Relocatable_relocs* rr,
2606 unsigned char* view,
2607 elfcpp::Elf_types<32>::Elf_Addr view_address,
2608 section_size_type view_size,
2609 unsigned char* reloc_view,
2610 section_size_type reloc_view_size)
2612 gold_assert(sh_type == elfcpp::SHT_REL);
2614 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2619 offset_in_output_section,
2628 // Return the value to use for a dynamic which requires special
2629 // treatment. This is how we support equality comparisons of function
2630 // pointers across shared library boundaries, as described in the
2631 // processor specific ABI supplement.
2634 Target_i386::do_dynsym_value(const Symbol* gsym) const
2636 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2637 return this->plt_section()->address() + gsym->plt_offset();
2640 // Return a string used to fill a code section with nops to take up
2641 // the specified length.
2644 Target_i386::do_code_fill(section_size_type length) const
2648 // Build a jmp instruction to skip over the bytes.
2649 unsigned char jmp[5];
2651 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2652 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2653 + std::string(length - 5, '\0'));
2656 // Nop sequences of various lengths.
2657 const char nop1[1] = { 0x90 }; // nop
2658 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2659 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2660 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2661 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2662 0x00 }; // leal 0(%esi,1),%esi
2663 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2665 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2667 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2668 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2669 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2670 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2672 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2673 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2675 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2676 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2678 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2679 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2680 0x00, 0x00, 0x00, 0x00 };
2681 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2682 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2683 0x27, 0x00, 0x00, 0x00,
2685 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2686 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2687 0xbc, 0x27, 0x00, 0x00,
2689 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2690 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2691 0x90, 0x90, 0x90, 0x90,
2694 const char* nops[16] = {
2696 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2697 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2700 return std::string(nops[length], length);
2703 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2704 // compiled with -fstack-split. The function calls non-stack-split
2705 // code. We have to change the function so that it always ensures
2706 // that it has enough stack space to run some random function.
2709 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2710 section_offset_type fnoffset,
2711 section_size_type fnsize,
2712 unsigned char* view,
2713 section_size_type view_size,
2715 std::string* to) const
2717 // The function starts with a comparison of the stack pointer and a
2718 // field in the TCB. This is followed by a jump.
2721 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2724 // We will call __morestack if the carry flag is set after this
2725 // comparison. We turn the comparison into an stc instruction
2727 view[fnoffset] = '\xf9';
2728 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2730 // lea NN(%esp),%ecx
2731 // lea NN(%esp),%edx
2732 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2733 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
2736 // This is loading an offset from the stack pointer for a
2737 // comparison. The offset is negative, so we decrease the
2738 // offset by the amount of space we need for the stack. This
2739 // means we will avoid calling __morestack if there happens to
2740 // be plenty of space on the stack already.
2741 unsigned char* pval = view + fnoffset + 3;
2742 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2743 val -= parameters->options().split_stack_adjust_size();
2744 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2748 if (!object->has_no_split_stack())
2749 object->error(_("failed to match split-stack sequence at "
2750 "section %u offset %0zx"),
2751 shndx, static_cast<size_t>(fnoffset));
2755 // We have to change the function so that it calls
2756 // __morestack_non_split instead of __morestack. The former will
2757 // allocate additional stack space.
2758 *from = "__morestack";
2759 *to = "__morestack_non_split";
2762 // The selector for i386 object files.
2764 class Target_selector_i386 : public Target_selector_freebsd
2767 Target_selector_i386()
2768 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2769 "elf32-i386", "elf32-i386-freebsd")
2773 do_instantiate_target()
2774 { return new Target_i386(); }
2777 Target_selector_i386 target_selector_i386;
2779 } // End anonymous namespace.