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 const 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 // Add a potential copy relocation.
384 copy_reloc(Symbol_table* symtab, Layout* layout,
385 Sized_relobj<32, false>* object,
386 unsigned int shndx, Output_section* output_section,
387 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
389 this->copy_relocs_.copy_reloc(symtab, layout,
390 symtab->get_sized_symbol<32>(sym),
391 object, shndx, output_section, reloc,
392 this->rel_dyn_section(layout));
395 // Information about this specific target which we pass to the
396 // general Target structure.
397 static const Target::Target_info i386_info;
399 // The types of GOT entries needed for this platform.
402 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
403 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
404 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
405 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
406 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
410 Output_data_got<32, false>* got_;
412 Output_data_plt_i386* plt_;
413 // The GOT PLT section.
414 Output_data_space* got_plt_;
415 // The _GLOBAL_OFFSET_TABLE_ symbol.
416 Symbol* global_offset_table_;
417 // The dynamic reloc section.
418 Reloc_section* rel_dyn_;
419 // Relocs saved to avoid a COPY reloc.
420 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
421 // Space for variables copied with a COPY reloc.
422 Output_data_space* dynbss_;
423 // Offset of the GOT entry for the TLS module index.
424 unsigned int got_mod_index_offset_;
425 // True if the _TLS_MODULE_BASE_ symbol has been defined.
426 bool tls_base_symbol_defined_;
429 const Target::Target_info Target_i386::i386_info =
432 false, // is_big_endian
433 elfcpp::EM_386, // machine_code
434 false, // has_make_symbol
435 false, // has_resolve
436 true, // has_code_fill
437 true, // is_default_stack_executable
439 "/usr/lib/libc.so.1", // dynamic_linker
440 0x08048000, // default_text_segment_address
441 0x1000, // abi_pagesize (overridable by -z max-page-size)
442 0x1000, // common_pagesize (overridable by -z common-page-size)
443 elfcpp::SHN_UNDEF, // small_common_shndx
444 elfcpp::SHN_UNDEF, // large_common_shndx
445 0, // small_common_section_flags
446 0, // large_common_section_flags
447 NULL, // attributes_section
448 NULL // attributes_vendor
451 // Get the GOT section, creating it if necessary.
453 Output_data_got<32, false>*
454 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
456 if (this->got_ == NULL)
458 gold_assert(symtab != NULL && layout != NULL);
460 this->got_ = new Output_data_got<32, false>();
463 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
465 | elfcpp::SHF_WRITE),
466 this->got_, false, true, true,
469 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
470 os = layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
472 | elfcpp::SHF_WRITE),
473 this->got_plt_, false, false, false,
476 // The first three entries are reserved.
477 this->got_plt_->set_current_data_size(3 * 4);
479 // Those bytes can go into the relro segment.
480 layout->increase_relro(3 * 4);
482 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
483 this->global_offset_table_ =
484 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
485 Symbol_table::PREDEFINED,
487 0, 0, elfcpp::STT_OBJECT,
489 elfcpp::STV_HIDDEN, 0,
496 // Get the dynamic reloc section, creating it if necessary.
498 Target_i386::Reloc_section*
499 Target_i386::rel_dyn_section(Layout* layout)
501 if (this->rel_dyn_ == NULL)
503 gold_assert(layout != NULL);
504 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
505 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
506 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
507 false, false, false);
509 return this->rel_dyn_;
512 // A class to handle the PLT data.
514 class Output_data_plt_i386 : public Output_section_data
517 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
519 Output_data_plt_i386(Layout*, Output_data_space*);
521 // Add an entry to the PLT.
523 add_entry(Symbol* gsym);
525 // Return the .rel.plt section data.
528 { return this->rel_; }
532 do_adjust_output_section(Output_section* os);
534 // Write to a map file.
536 do_print_to_mapfile(Mapfile* mapfile) const
537 { mapfile->print_output_data(this, _("** PLT")); }
540 // The size of an entry in the PLT.
541 static const int plt_entry_size = 16;
543 // The first entry in the PLT for an executable.
544 static unsigned char exec_first_plt_entry[plt_entry_size];
546 // The first entry in the PLT for a shared object.
547 static unsigned char dyn_first_plt_entry[plt_entry_size];
549 // Other entries in the PLT for an executable.
550 static unsigned char exec_plt_entry[plt_entry_size];
552 // Other entries in the PLT for a shared object.
553 static unsigned char dyn_plt_entry[plt_entry_size];
555 // Set the final size.
557 set_final_data_size()
558 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
560 // Write out the PLT data.
562 do_write(Output_file*);
564 // The reloc section.
566 // The .got.plt section.
567 Output_data_space* got_plt_;
568 // The number of PLT entries.
572 // Create the PLT section. The ordinary .got section is an argument,
573 // since we need to refer to the start. We also create our own .got
574 // section just for PLT entries.
576 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
577 Output_data_space* got_plt)
578 : Output_section_data(4), got_plt_(got_plt), count_(0)
580 this->rel_ = new Reloc_section(false);
581 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
582 elfcpp::SHF_ALLOC, this->rel_, true,
583 false, false, false);
587 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
589 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
590 // linker, and so do we.
594 // Add an entry to the PLT.
597 Output_data_plt_i386::add_entry(Symbol* gsym)
599 gold_assert(!gsym->has_plt_offset());
601 // Note that when setting the PLT offset we skip the initial
602 // reserved PLT entry.
603 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
607 section_offset_type got_offset = this->got_plt_->current_data_size();
609 // Every PLT entry needs a GOT entry which points back to the PLT
610 // entry (this will be changed by the dynamic linker, normally
611 // lazily when the function is called).
612 this->got_plt_->set_current_data_size(got_offset + 4);
614 // Every PLT entry needs a reloc.
615 gsym->set_needs_dynsym_entry();
616 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
619 // Note that we don't need to save the symbol. The contents of the
620 // PLT are independent of which symbols are used. The symbols only
621 // appear in the relocations.
624 // The first entry in the PLT for an executable.
626 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
628 0xff, 0x35, // pushl contents of memory address
629 0, 0, 0, 0, // replaced with address of .got + 4
630 0xff, 0x25, // jmp indirect
631 0, 0, 0, 0, // replaced with address of .got + 8
635 // The first entry in the PLT for a shared object.
637 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
639 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
640 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
644 // Subsequent entries in the PLT for an executable.
646 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
648 0xff, 0x25, // jmp indirect
649 0, 0, 0, 0, // replaced with address of symbol in .got
650 0x68, // pushl immediate
651 0, 0, 0, 0, // replaced with offset into relocation table
652 0xe9, // jmp relative
653 0, 0, 0, 0 // replaced with offset to start of .plt
656 // Subsequent entries in the PLT for a shared object.
658 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
660 0xff, 0xa3, // jmp *offset(%ebx)
661 0, 0, 0, 0, // replaced with offset of symbol in .got
662 0x68, // pushl immediate
663 0, 0, 0, 0, // replaced with offset into relocation table
664 0xe9, // jmp relative
665 0, 0, 0, 0 // replaced with offset to start of .plt
668 // Write out the PLT. This uses the hand-coded instructions above,
669 // and adjusts them as needed. This is all specified by the i386 ELF
670 // Processor Supplement.
673 Output_data_plt_i386::do_write(Output_file* of)
675 const off_t offset = this->offset();
676 const section_size_type oview_size =
677 convert_to_section_size_type(this->data_size());
678 unsigned char* const oview = of->get_output_view(offset, oview_size);
680 const off_t got_file_offset = this->got_plt_->offset();
681 const section_size_type got_size =
682 convert_to_section_size_type(this->got_plt_->data_size());
683 unsigned char* const got_view = of->get_output_view(got_file_offset,
686 unsigned char* pov = oview;
688 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
689 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
691 if (parameters->options().output_is_position_independent())
692 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
695 memcpy(pov, exec_first_plt_entry, plt_entry_size);
696 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
697 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
699 pov += plt_entry_size;
701 unsigned char* got_pov = got_view;
703 memset(got_pov, 0, 12);
706 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
708 unsigned int plt_offset = plt_entry_size;
709 unsigned int plt_rel_offset = 0;
710 unsigned int got_offset = 12;
711 const unsigned int count = this->count_;
712 for (unsigned int i = 0;
715 pov += plt_entry_size,
717 plt_offset += plt_entry_size,
718 plt_rel_offset += rel_size,
721 // Set and adjust the PLT entry itself.
723 if (parameters->options().output_is_position_independent())
725 memcpy(pov, dyn_plt_entry, plt_entry_size);
726 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
730 memcpy(pov, exec_plt_entry, plt_entry_size);
731 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
736 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
737 elfcpp::Swap<32, false>::writeval(pov + 12,
738 - (plt_offset + plt_entry_size));
740 // Set the entry in the GOT.
741 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
744 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
745 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
747 of->write_output_view(offset, oview_size, oview);
748 of->write_output_view(got_file_offset, got_size, got_view);
751 // Create a PLT entry for a global symbol.
754 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
756 if (gsym->has_plt_offset())
759 if (this->plt_ == NULL)
761 // Create the GOT sections first.
762 this->got_section(symtab, layout);
764 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
765 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
767 | elfcpp::SHF_EXECINSTR),
768 this->plt_, false, false, false, false);
771 this->plt_->add_entry(gsym);
774 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
777 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
779 if (this->tls_base_symbol_defined_)
782 Output_segment* tls_segment = layout->tls_segment();
783 if (tls_segment != NULL)
785 bool is_exec = parameters->options().output_is_executable();
786 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
787 Symbol_table::PREDEFINED,
791 elfcpp::STV_HIDDEN, 0,
793 ? Symbol::SEGMENT_END
794 : Symbol::SEGMENT_START),
797 this->tls_base_symbol_defined_ = true;
800 // Create a GOT entry for the TLS module index.
803 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
804 Sized_relobj<32, false>* object)
806 if (this->got_mod_index_offset_ == -1U)
808 gold_assert(symtab != NULL && layout != NULL && object != NULL);
809 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
810 Output_data_got<32, false>* got = this->got_section(symtab, layout);
811 unsigned int got_offset = got->add_constant(0);
812 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
814 got->add_constant(0);
815 this->got_mod_index_offset_ = got_offset;
817 return this->got_mod_index_offset_;
820 // Optimize the TLS relocation type based on what we know about the
821 // symbol. IS_FINAL is true if the final address of this symbol is
822 // known at link time.
824 tls::Tls_optimization
825 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
827 // If we are generating a shared library, then we can't do anything
829 if (parameters->options().shared())
830 return tls::TLSOPT_NONE;
834 case elfcpp::R_386_TLS_GD:
835 case elfcpp::R_386_TLS_GOTDESC:
836 case elfcpp::R_386_TLS_DESC_CALL:
837 // These are General-Dynamic which permits fully general TLS
838 // access. Since we know that we are generating an executable,
839 // we can convert this to Initial-Exec. If we also know that
840 // this is a local symbol, we can further switch to Local-Exec.
842 return tls::TLSOPT_TO_LE;
843 return tls::TLSOPT_TO_IE;
845 case elfcpp::R_386_TLS_LDM:
846 // This is Local-Dynamic, which refers to a local symbol in the
847 // dynamic TLS block. Since we know that we generating an
848 // executable, we can switch to Local-Exec.
849 return tls::TLSOPT_TO_LE;
851 case elfcpp::R_386_TLS_LDO_32:
852 // Another type of Local-Dynamic relocation.
853 return tls::TLSOPT_TO_LE;
855 case elfcpp::R_386_TLS_IE:
856 case elfcpp::R_386_TLS_GOTIE:
857 case elfcpp::R_386_TLS_IE_32:
858 // These are Initial-Exec relocs which get the thread offset
859 // from the GOT. If we know that we are linking against the
860 // local symbol, we can switch to Local-Exec, which links the
861 // thread offset into the instruction.
863 return tls::TLSOPT_TO_LE;
864 return tls::TLSOPT_NONE;
866 case elfcpp::R_386_TLS_LE:
867 case elfcpp::R_386_TLS_LE_32:
868 // When we already have Local-Exec, there is nothing further we
870 return tls::TLSOPT_NONE;
877 // Report an unsupported relocation against a local symbol.
880 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
883 gold_error(_("%s: unsupported reloc %u against local symbol"),
884 object->name().c_str(), r_type);
887 // Scan a relocation for a local symbol.
890 Target_i386::Scan::local(Symbol_table* symtab,
893 Sized_relobj<32, false>* object,
894 unsigned int data_shndx,
895 Output_section* output_section,
896 const elfcpp::Rel<32, false>& reloc,
898 const elfcpp::Sym<32, false>& lsym)
902 case elfcpp::R_386_NONE:
903 case elfcpp::R_386_GNU_VTINHERIT:
904 case elfcpp::R_386_GNU_VTENTRY:
907 case elfcpp::R_386_32:
908 // If building a shared library (or a position-independent
909 // executable), we need to create a dynamic relocation for
910 // this location. The relocation applied at link time will
911 // apply the link-time value, so we flag the location with
912 // an R_386_RELATIVE relocation so the dynamic loader can
913 // relocate it easily.
914 if (parameters->options().output_is_position_independent())
916 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
917 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
918 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
919 output_section, data_shndx,
920 reloc.get_r_offset());
924 case elfcpp::R_386_16:
925 case elfcpp::R_386_8:
926 // If building a shared library (or a position-independent
927 // executable), we need to create a dynamic relocation for
928 // this location. Because the addend needs to remain in the
929 // data section, we need to be careful not to apply this
930 // relocation statically.
931 if (parameters->options().output_is_position_independent())
933 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
934 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
935 if (lsym.get_st_type() != elfcpp::STT_SECTION)
936 rel_dyn->add_local(object, r_sym, r_type, output_section,
937 data_shndx, reloc.get_r_offset());
940 gold_assert(lsym.get_st_value() == 0);
941 unsigned int shndx = lsym.get_st_shndx();
943 shndx = object->adjust_sym_shndx(r_sym, shndx,
946 object->error(_("section symbol %u has bad shndx %u"),
949 rel_dyn->add_local_section(object, shndx,
950 r_type, output_section,
951 data_shndx, reloc.get_r_offset());
956 case elfcpp::R_386_PC32:
957 case elfcpp::R_386_PC16:
958 case elfcpp::R_386_PC8:
961 case elfcpp::R_386_PLT32:
962 // Since we know this is a local symbol, we can handle this as a
966 case elfcpp::R_386_GOTOFF:
967 case elfcpp::R_386_GOTPC:
968 // We need a GOT section.
969 target->got_section(symtab, layout);
972 case elfcpp::R_386_GOT32:
974 // The symbol requires a GOT entry.
975 Output_data_got<32, false>* got = target->got_section(symtab, layout);
976 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
977 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
979 // If we are generating a shared object, we need to add a
980 // dynamic RELATIVE relocation for this symbol's GOT entry.
981 if (parameters->options().output_is_position_independent())
983 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
984 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
985 rel_dyn->add_local_relative(
986 object, r_sym, elfcpp::R_386_RELATIVE, got,
987 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
993 // These are relocations which should only be seen by the
994 // dynamic linker, and should never be seen here.
995 case elfcpp::R_386_COPY:
996 case elfcpp::R_386_GLOB_DAT:
997 case elfcpp::R_386_JUMP_SLOT:
998 case elfcpp::R_386_RELATIVE:
999 case elfcpp::R_386_TLS_TPOFF:
1000 case elfcpp::R_386_TLS_DTPMOD32:
1001 case elfcpp::R_386_TLS_DTPOFF32:
1002 case elfcpp::R_386_TLS_TPOFF32:
1003 case elfcpp::R_386_TLS_DESC:
1004 gold_error(_("%s: unexpected reloc %u in object file"),
1005 object->name().c_str(), r_type);
1008 // These are initial TLS relocs, which are expected when
1010 case elfcpp::R_386_TLS_GD: // Global-dynamic
1011 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1012 case elfcpp::R_386_TLS_DESC_CALL:
1013 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1014 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1015 case elfcpp::R_386_TLS_IE: // Initial-exec
1016 case elfcpp::R_386_TLS_IE_32:
1017 case elfcpp::R_386_TLS_GOTIE:
1018 case elfcpp::R_386_TLS_LE: // Local-exec
1019 case elfcpp::R_386_TLS_LE_32:
1021 bool output_is_shared = parameters->options().shared();
1022 const tls::Tls_optimization optimized_type
1023 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1026 case elfcpp::R_386_TLS_GD: // Global-dynamic
1027 if (optimized_type == tls::TLSOPT_NONE)
1029 // Create a pair of GOT entries for the module index and
1030 // dtv-relative offset.
1031 Output_data_got<32, false>* got
1032 = target->got_section(symtab, layout);
1033 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1034 unsigned int shndx = lsym.get_st_shndx();
1036 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1038 object->error(_("local symbol %u has bad shndx %u"),
1041 got->add_local_pair_with_rel(object, r_sym, shndx,
1043 target->rel_dyn_section(layout),
1044 elfcpp::R_386_TLS_DTPMOD32, 0);
1046 else if (optimized_type != tls::TLSOPT_TO_LE)
1047 unsupported_reloc_local(object, r_type);
1050 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1051 target->define_tls_base_symbol(symtab, layout);
1052 if (optimized_type == tls::TLSOPT_NONE)
1054 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1055 Output_data_got<32, false>* got
1056 = target->got_section(symtab, layout);
1057 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1058 unsigned int shndx = lsym.get_st_shndx();
1060 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1062 object->error(_("local symbol %u has bad shndx %u"),
1065 got->add_local_pair_with_rel(object, r_sym, shndx,
1067 target->rel_dyn_section(layout),
1068 elfcpp::R_386_TLS_DESC, 0);
1070 else if (optimized_type != tls::TLSOPT_TO_LE)
1071 unsupported_reloc_local(object, r_type);
1074 case elfcpp::R_386_TLS_DESC_CALL:
1077 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1078 if (optimized_type == tls::TLSOPT_NONE)
1080 // Create a GOT entry for the module index.
1081 target->got_mod_index_entry(symtab, layout, object);
1083 else if (optimized_type != tls::TLSOPT_TO_LE)
1084 unsupported_reloc_local(object, r_type);
1087 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1090 case elfcpp::R_386_TLS_IE: // Initial-exec
1091 case elfcpp::R_386_TLS_IE_32:
1092 case elfcpp::R_386_TLS_GOTIE:
1093 layout->set_has_static_tls();
1094 if (optimized_type == tls::TLSOPT_NONE)
1096 // For the R_386_TLS_IE relocation, we need to create a
1097 // dynamic relocation when building a shared library.
1098 if (r_type == elfcpp::R_386_TLS_IE
1099 && parameters->options().shared())
1101 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1103 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1104 rel_dyn->add_local_relative(object, r_sym,
1105 elfcpp::R_386_RELATIVE,
1106 output_section, data_shndx,
1107 reloc.get_r_offset());
1109 // Create a GOT entry for the tp-relative offset.
1110 Output_data_got<32, false>* got
1111 = target->got_section(symtab, layout);
1112 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1113 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1114 ? elfcpp::R_386_TLS_TPOFF32
1115 : elfcpp::R_386_TLS_TPOFF);
1116 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1117 ? GOT_TYPE_TLS_OFFSET
1118 : GOT_TYPE_TLS_NOFFSET);
1119 got->add_local_with_rel(object, r_sym, got_type,
1120 target->rel_dyn_section(layout),
1123 else if (optimized_type != tls::TLSOPT_TO_LE)
1124 unsupported_reloc_local(object, r_type);
1127 case elfcpp::R_386_TLS_LE: // Local-exec
1128 case elfcpp::R_386_TLS_LE_32:
1129 layout->set_has_static_tls();
1130 if (output_is_shared)
1132 // We need to create a dynamic relocation.
1133 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1134 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1135 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1136 ? elfcpp::R_386_TLS_TPOFF32
1137 : elfcpp::R_386_TLS_TPOFF);
1138 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1139 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1140 data_shndx, reloc.get_r_offset());
1150 case elfcpp::R_386_32PLT:
1151 case elfcpp::R_386_TLS_GD_32:
1152 case elfcpp::R_386_TLS_GD_PUSH:
1153 case elfcpp::R_386_TLS_GD_CALL:
1154 case elfcpp::R_386_TLS_GD_POP:
1155 case elfcpp::R_386_TLS_LDM_32:
1156 case elfcpp::R_386_TLS_LDM_PUSH:
1157 case elfcpp::R_386_TLS_LDM_CALL:
1158 case elfcpp::R_386_TLS_LDM_POP:
1159 case elfcpp::R_386_USED_BY_INTEL_200:
1161 unsupported_reloc_local(object, r_type);
1166 // Report an unsupported relocation against a global symbol.
1169 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1170 unsigned int r_type,
1173 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1174 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1177 // Scan a relocation for a global symbol.
1180 Target_i386::Scan::global(Symbol_table* symtab,
1182 Target_i386* target,
1183 Sized_relobj<32, false>* object,
1184 unsigned int data_shndx,
1185 Output_section* output_section,
1186 const elfcpp::Rel<32, false>& reloc,
1187 unsigned int r_type,
1192 case elfcpp::R_386_NONE:
1193 case elfcpp::R_386_GNU_VTINHERIT:
1194 case elfcpp::R_386_GNU_VTENTRY:
1197 case elfcpp::R_386_32:
1198 case elfcpp::R_386_16:
1199 case elfcpp::R_386_8:
1201 // Make a PLT entry if necessary.
1202 if (gsym->needs_plt_entry())
1204 target->make_plt_entry(symtab, layout, gsym);
1205 // Since this is not a PC-relative relocation, we may be
1206 // taking the address of a function. In that case we need to
1207 // set the entry in the dynamic symbol table to the address of
1209 if (gsym->is_from_dynobj() && !parameters->options().shared())
1210 gsym->set_needs_dynsym_value();
1212 // Make a dynamic relocation if necessary.
1213 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1215 if (gsym->may_need_copy_reloc())
1217 target->copy_reloc(symtab, layout, object,
1218 data_shndx, output_section, gsym, reloc);
1220 else if (r_type == elfcpp::R_386_32
1221 && gsym->can_use_relative_reloc(false))
1223 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1224 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1225 output_section, object,
1226 data_shndx, reloc.get_r_offset());
1230 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1231 rel_dyn->add_global(gsym, r_type, output_section, object,
1232 data_shndx, reloc.get_r_offset());
1238 case elfcpp::R_386_PC32:
1239 case elfcpp::R_386_PC16:
1240 case elfcpp::R_386_PC8:
1242 // Make a PLT entry if necessary.
1243 if (gsym->needs_plt_entry())
1245 // These relocations are used for function calls only in
1246 // non-PIC code. For a 32-bit relocation in a shared library,
1247 // we'll need a text relocation anyway, so we can skip the
1248 // PLT entry and let the dynamic linker bind the call directly
1249 // to the target. For smaller relocations, we should use a
1250 // PLT entry to ensure that the call can reach.
1251 if (!parameters->options().shared()
1252 || r_type != elfcpp::R_386_PC32)
1253 target->make_plt_entry(symtab, layout, gsym);
1255 // Make a dynamic relocation if necessary.
1256 int flags = Symbol::NON_PIC_REF;
1257 if (gsym->is_func())
1258 flags |= Symbol::FUNCTION_CALL;
1259 if (gsym->needs_dynamic_reloc(flags))
1261 if (gsym->may_need_copy_reloc())
1263 target->copy_reloc(symtab, layout, object,
1264 data_shndx, output_section, gsym, reloc);
1268 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1269 rel_dyn->add_global(gsym, r_type, output_section, object,
1270 data_shndx, reloc.get_r_offset());
1276 case elfcpp::R_386_GOT32:
1278 // The symbol requires a GOT entry.
1279 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1280 if (gsym->final_value_is_known())
1281 got->add_global(gsym, GOT_TYPE_STANDARD);
1284 // If this symbol is not fully resolved, we need to add a
1285 // GOT entry with a dynamic relocation.
1286 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1287 if (gsym->is_from_dynobj()
1288 || gsym->is_undefined()
1289 || gsym->is_preemptible())
1290 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1291 rel_dyn, elfcpp::R_386_GLOB_DAT);
1294 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1295 rel_dyn->add_global_relative(
1296 gsym, elfcpp::R_386_RELATIVE, got,
1297 gsym->got_offset(GOT_TYPE_STANDARD));
1303 case elfcpp::R_386_PLT32:
1304 // If the symbol is fully resolved, this is just a PC32 reloc.
1305 // Otherwise we need a PLT entry.
1306 if (gsym->final_value_is_known())
1308 // If building a shared library, we can also skip the PLT entry
1309 // if the symbol is defined in the output file and is protected
1311 if (gsym->is_defined()
1312 && !gsym->is_from_dynobj()
1313 && !gsym->is_preemptible())
1315 target->make_plt_entry(symtab, layout, gsym);
1318 case elfcpp::R_386_GOTOFF:
1319 case elfcpp::R_386_GOTPC:
1320 // We need a GOT section.
1321 target->got_section(symtab, layout);
1324 // These are relocations which should only be seen by the
1325 // dynamic linker, and should never be seen here.
1326 case elfcpp::R_386_COPY:
1327 case elfcpp::R_386_GLOB_DAT:
1328 case elfcpp::R_386_JUMP_SLOT:
1329 case elfcpp::R_386_RELATIVE:
1330 case elfcpp::R_386_TLS_TPOFF:
1331 case elfcpp::R_386_TLS_DTPMOD32:
1332 case elfcpp::R_386_TLS_DTPOFF32:
1333 case elfcpp::R_386_TLS_TPOFF32:
1334 case elfcpp::R_386_TLS_DESC:
1335 gold_error(_("%s: unexpected reloc %u in object file"),
1336 object->name().c_str(), r_type);
1339 // These are initial tls relocs, which are expected when
1341 case elfcpp::R_386_TLS_GD: // Global-dynamic
1342 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1343 case elfcpp::R_386_TLS_DESC_CALL:
1344 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1345 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1346 case elfcpp::R_386_TLS_IE: // Initial-exec
1347 case elfcpp::R_386_TLS_IE_32:
1348 case elfcpp::R_386_TLS_GOTIE:
1349 case elfcpp::R_386_TLS_LE: // Local-exec
1350 case elfcpp::R_386_TLS_LE_32:
1352 const bool is_final = gsym->final_value_is_known();
1353 const tls::Tls_optimization optimized_type
1354 = Target_i386::optimize_tls_reloc(is_final, r_type);
1357 case elfcpp::R_386_TLS_GD: // Global-dynamic
1358 if (optimized_type == tls::TLSOPT_NONE)
1360 // Create a pair of GOT entries for the module index and
1361 // dtv-relative offset.
1362 Output_data_got<32, false>* got
1363 = target->got_section(symtab, layout);
1364 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1365 target->rel_dyn_section(layout),
1366 elfcpp::R_386_TLS_DTPMOD32,
1367 elfcpp::R_386_TLS_DTPOFF32);
1369 else if (optimized_type == tls::TLSOPT_TO_IE)
1371 // Create a GOT entry for the tp-relative offset.
1372 Output_data_got<32, false>* got
1373 = target->got_section(symtab, layout);
1374 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1375 target->rel_dyn_section(layout),
1376 elfcpp::R_386_TLS_TPOFF);
1378 else if (optimized_type != tls::TLSOPT_TO_LE)
1379 unsupported_reloc_global(object, r_type, gsym);
1382 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1383 target->define_tls_base_symbol(symtab, layout);
1384 if (optimized_type == tls::TLSOPT_NONE)
1386 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1387 Output_data_got<32, false>* got
1388 = target->got_section(symtab, layout);
1389 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1390 target->rel_dyn_section(layout),
1391 elfcpp::R_386_TLS_DESC, 0);
1393 else if (optimized_type == tls::TLSOPT_TO_IE)
1395 // Create a GOT entry for the tp-relative offset.
1396 Output_data_got<32, false>* got
1397 = target->got_section(symtab, layout);
1398 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1399 target->rel_dyn_section(layout),
1400 elfcpp::R_386_TLS_TPOFF);
1402 else if (optimized_type != tls::TLSOPT_TO_LE)
1403 unsupported_reloc_global(object, r_type, gsym);
1406 case elfcpp::R_386_TLS_DESC_CALL:
1409 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1410 if (optimized_type == tls::TLSOPT_NONE)
1412 // Create a GOT entry for the module index.
1413 target->got_mod_index_entry(symtab, layout, object);
1415 else if (optimized_type != tls::TLSOPT_TO_LE)
1416 unsupported_reloc_global(object, r_type, gsym);
1419 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1422 case elfcpp::R_386_TLS_IE: // Initial-exec
1423 case elfcpp::R_386_TLS_IE_32:
1424 case elfcpp::R_386_TLS_GOTIE:
1425 layout->set_has_static_tls();
1426 if (optimized_type == tls::TLSOPT_NONE)
1428 // For the R_386_TLS_IE relocation, we need to create a
1429 // dynamic relocation when building a shared library.
1430 if (r_type == elfcpp::R_386_TLS_IE
1431 && parameters->options().shared())
1433 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1434 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1435 output_section, object,
1437 reloc.get_r_offset());
1439 // Create a GOT entry for the tp-relative offset.
1440 Output_data_got<32, false>* got
1441 = target->got_section(symtab, layout);
1442 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1443 ? elfcpp::R_386_TLS_TPOFF32
1444 : elfcpp::R_386_TLS_TPOFF);
1445 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1446 ? GOT_TYPE_TLS_OFFSET
1447 : GOT_TYPE_TLS_NOFFSET);
1448 got->add_global_with_rel(gsym, got_type,
1449 target->rel_dyn_section(layout),
1452 else if (optimized_type != tls::TLSOPT_TO_LE)
1453 unsupported_reloc_global(object, r_type, gsym);
1456 case elfcpp::R_386_TLS_LE: // Local-exec
1457 case elfcpp::R_386_TLS_LE_32:
1458 layout->set_has_static_tls();
1459 if (parameters->options().shared())
1461 // We need to create a dynamic relocation.
1462 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1463 ? elfcpp::R_386_TLS_TPOFF32
1464 : elfcpp::R_386_TLS_TPOFF);
1465 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1466 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1467 data_shndx, reloc.get_r_offset());
1477 case elfcpp::R_386_32PLT:
1478 case elfcpp::R_386_TLS_GD_32:
1479 case elfcpp::R_386_TLS_GD_PUSH:
1480 case elfcpp::R_386_TLS_GD_CALL:
1481 case elfcpp::R_386_TLS_GD_POP:
1482 case elfcpp::R_386_TLS_LDM_32:
1483 case elfcpp::R_386_TLS_LDM_PUSH:
1484 case elfcpp::R_386_TLS_LDM_CALL:
1485 case elfcpp::R_386_TLS_LDM_POP:
1486 case elfcpp::R_386_USED_BY_INTEL_200:
1488 unsupported_reloc_global(object, r_type, gsym);
1493 // Process relocations for gc.
1496 Target_i386::gc_process_relocs(Symbol_table* symtab,
1498 Sized_relobj<32, false>* object,
1499 unsigned int data_shndx,
1501 const unsigned char* prelocs,
1503 Output_section* output_section,
1504 bool needs_special_offset_handling,
1505 size_t local_symbol_count,
1506 const unsigned char* plocal_symbols)
1508 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1518 needs_special_offset_handling,
1523 // Scan relocations for a section.
1526 Target_i386::scan_relocs(Symbol_table* symtab,
1528 Sized_relobj<32, false>* object,
1529 unsigned int data_shndx,
1530 unsigned int sh_type,
1531 const unsigned char* prelocs,
1533 Output_section* output_section,
1534 bool needs_special_offset_handling,
1535 size_t local_symbol_count,
1536 const unsigned char* plocal_symbols)
1538 if (sh_type == elfcpp::SHT_RELA)
1540 gold_error(_("%s: unsupported RELA reloc section"),
1541 object->name().c_str());
1545 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1555 needs_special_offset_handling,
1560 // Finalize the sections.
1563 Target_i386::do_finalize_sections(
1565 const Input_objects*,
1566 Symbol_table* symtab)
1568 const Reloc_section* rel_plt = (this->plt_ == NULL
1570 : this->plt_->rel_plt());
1571 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
1572 this->rel_dyn_, true);
1574 // Emit any relocs we saved in an attempt to avoid generating COPY
1576 if (this->copy_relocs_.any_saved_relocs())
1577 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1579 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1580 // the .got.plt section.
1581 Symbol* sym = this->global_offset_table_;
1584 uint32_t data_size = this->got_plt_->current_data_size();
1585 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
1589 // Return whether a direct absolute static relocation needs to be applied.
1590 // In cases where Scan::local() or Scan::global() has created
1591 // a dynamic relocation other than R_386_RELATIVE, the addend
1592 // of the relocation is carried in the data, and we must not
1593 // apply the static relocation.
1596 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1599 Output_section* output_section)
1601 // If the output section is not allocated, then we didn't call
1602 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1604 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1607 // For local symbols, we will have created a non-RELATIVE dynamic
1608 // relocation only if (a) the output is position independent,
1609 // (b) the relocation is absolute (not pc- or segment-relative), and
1610 // (c) the relocation is not 32 bits wide.
1612 return !(parameters->options().output_is_position_independent()
1613 && (ref_flags & Symbol::ABSOLUTE_REF)
1616 // For global symbols, we use the same helper routines used in the
1617 // scan pass. If we did not create a dynamic relocation, or if we
1618 // created a RELATIVE dynamic relocation, we should apply the static
1620 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1621 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1622 && gsym->can_use_relative_reloc(ref_flags
1623 & Symbol::FUNCTION_CALL);
1624 return !has_dyn || is_rel;
1627 // Perform a relocation.
1630 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1631 Target_i386* target,
1632 Output_section *output_section,
1634 const elfcpp::Rel<32, false>& rel,
1635 unsigned int r_type,
1636 const Sized_symbol<32>* gsym,
1637 const Symbol_value<32>* psymval,
1638 unsigned char* view,
1639 elfcpp::Elf_types<32>::Elf_Addr address,
1640 section_size_type view_size)
1642 if (this->skip_call_tls_get_addr_)
1644 if ((r_type != elfcpp::R_386_PLT32
1645 && r_type != elfcpp::R_386_PC32)
1647 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1648 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1649 _("missing expected TLS relocation"));
1652 this->skip_call_tls_get_addr_ = false;
1657 // Pick the value to use for symbols defined in shared objects.
1658 Symbol_value<32> symval;
1660 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1661 || r_type == elfcpp::R_386_PC16
1662 || r_type == elfcpp::R_386_PC32))
1664 symval.set_output_value(target->plt_section()->address()
1665 + gsym->plt_offset());
1669 const Sized_relobj<32, false>* object = relinfo->object;
1671 // Get the GOT offset if needed.
1672 // The GOT pointer points to the end of the GOT section.
1673 // We need to subtract the size of the GOT section to get
1674 // the actual offset to use in the relocation.
1675 bool have_got_offset = false;
1676 unsigned int got_offset = 0;
1679 case elfcpp::R_386_GOT32:
1682 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1683 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1684 - target->got_size());
1688 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1689 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1690 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1691 - target->got_size());
1693 have_got_offset = true;
1702 case elfcpp::R_386_NONE:
1703 case elfcpp::R_386_GNU_VTINHERIT:
1704 case elfcpp::R_386_GNU_VTENTRY:
1707 case elfcpp::R_386_32:
1708 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1710 Relocate_functions<32, false>::rel32(view, object, psymval);
1713 case elfcpp::R_386_PC32:
1715 int ref_flags = Symbol::NON_PIC_REF;
1716 if (gsym != NULL && gsym->is_func())
1717 ref_flags |= Symbol::FUNCTION_CALL;
1718 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1719 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1723 case elfcpp::R_386_16:
1724 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1726 Relocate_functions<32, false>::rel16(view, object, psymval);
1729 case elfcpp::R_386_PC16:
1731 int ref_flags = Symbol::NON_PIC_REF;
1732 if (gsym != NULL && gsym->is_func())
1733 ref_flags |= Symbol::FUNCTION_CALL;
1734 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1735 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1739 case elfcpp::R_386_8:
1740 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1742 Relocate_functions<32, false>::rel8(view, object, psymval);
1745 case elfcpp::R_386_PC8:
1747 int ref_flags = Symbol::NON_PIC_REF;
1748 if (gsym != NULL && gsym->is_func())
1749 ref_flags |= Symbol::FUNCTION_CALL;
1750 if (should_apply_static_reloc(gsym, ref_flags, false,
1752 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1756 case elfcpp::R_386_PLT32:
1757 gold_assert(gsym == NULL
1758 || gsym->has_plt_offset()
1759 || gsym->final_value_is_known()
1760 || (gsym->is_defined()
1761 && !gsym->is_from_dynobj()
1762 && !gsym->is_preemptible()));
1763 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1766 case elfcpp::R_386_GOT32:
1767 gold_assert(have_got_offset);
1768 Relocate_functions<32, false>::rel32(view, got_offset);
1771 case elfcpp::R_386_GOTOFF:
1773 elfcpp::Elf_types<32>::Elf_Addr value;
1774 value = (psymval->value(object, 0)
1775 - target->got_plt_section()->address());
1776 Relocate_functions<32, false>::rel32(view, value);
1780 case elfcpp::R_386_GOTPC:
1782 elfcpp::Elf_types<32>::Elf_Addr value;
1783 value = target->got_plt_section()->address();
1784 Relocate_functions<32, false>::pcrel32(view, value, address);
1788 case elfcpp::R_386_COPY:
1789 case elfcpp::R_386_GLOB_DAT:
1790 case elfcpp::R_386_JUMP_SLOT:
1791 case elfcpp::R_386_RELATIVE:
1792 // These are outstanding tls relocs, which are unexpected when
1794 case elfcpp::R_386_TLS_TPOFF:
1795 case elfcpp::R_386_TLS_DTPMOD32:
1796 case elfcpp::R_386_TLS_DTPOFF32:
1797 case elfcpp::R_386_TLS_TPOFF32:
1798 case elfcpp::R_386_TLS_DESC:
1799 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1800 _("unexpected reloc %u in object file"),
1804 // These are initial tls relocs, which are expected when
1806 case elfcpp::R_386_TLS_GD: // Global-dynamic
1807 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1808 case elfcpp::R_386_TLS_DESC_CALL:
1809 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1810 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1811 case elfcpp::R_386_TLS_IE: // Initial-exec
1812 case elfcpp::R_386_TLS_IE_32:
1813 case elfcpp::R_386_TLS_GOTIE:
1814 case elfcpp::R_386_TLS_LE: // Local-exec
1815 case elfcpp::R_386_TLS_LE_32:
1816 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1817 view, address, view_size);
1820 case elfcpp::R_386_32PLT:
1821 case elfcpp::R_386_TLS_GD_32:
1822 case elfcpp::R_386_TLS_GD_PUSH:
1823 case elfcpp::R_386_TLS_GD_CALL:
1824 case elfcpp::R_386_TLS_GD_POP:
1825 case elfcpp::R_386_TLS_LDM_32:
1826 case elfcpp::R_386_TLS_LDM_PUSH:
1827 case elfcpp::R_386_TLS_LDM_CALL:
1828 case elfcpp::R_386_TLS_LDM_POP:
1829 case elfcpp::R_386_USED_BY_INTEL_200:
1831 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1832 _("unsupported reloc %u"),
1840 // Perform a TLS relocation.
1843 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1844 Target_i386* target,
1846 const elfcpp::Rel<32, false>& rel,
1847 unsigned int r_type,
1848 const Sized_symbol<32>* gsym,
1849 const Symbol_value<32>* psymval,
1850 unsigned char* view,
1851 elfcpp::Elf_types<32>::Elf_Addr,
1852 section_size_type view_size)
1854 Output_segment* tls_segment = relinfo->layout->tls_segment();
1856 const Sized_relobj<32, false>* object = relinfo->object;
1858 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1860 const bool is_final =
1862 ? !parameters->options().output_is_position_independent()
1863 : gsym->final_value_is_known());
1864 const tls::Tls_optimization optimized_type
1865 = Target_i386::optimize_tls_reloc(is_final, r_type);
1868 case elfcpp::R_386_TLS_GD: // Global-dynamic
1869 if (optimized_type == tls::TLSOPT_TO_LE)
1871 gold_assert(tls_segment != NULL);
1872 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1873 rel, r_type, value, view,
1879 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1880 ? GOT_TYPE_TLS_NOFFSET
1881 : GOT_TYPE_TLS_PAIR);
1882 unsigned int got_offset;
1885 gold_assert(gsym->has_got_offset(got_type));
1886 got_offset = gsym->got_offset(got_type) - target->got_size();
1890 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1891 gold_assert(object->local_has_got_offset(r_sym, got_type));
1892 got_offset = (object->local_got_offset(r_sym, got_type)
1893 - target->got_size());
1895 if (optimized_type == tls::TLSOPT_TO_IE)
1897 gold_assert(tls_segment != NULL);
1898 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1899 got_offset, view, view_size);
1902 else if (optimized_type == tls::TLSOPT_NONE)
1904 // Relocate the field with the offset of the pair of GOT
1906 Relocate_functions<32, false>::rel32(view, got_offset);
1910 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1911 _("unsupported reloc %u"),
1915 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1916 case elfcpp::R_386_TLS_DESC_CALL:
1917 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1918 if (optimized_type == tls::TLSOPT_TO_LE)
1920 gold_assert(tls_segment != NULL);
1921 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1922 rel, r_type, value, view,
1928 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1929 ? GOT_TYPE_TLS_NOFFSET
1930 : GOT_TYPE_TLS_DESC);
1931 unsigned int got_offset;
1934 gold_assert(gsym->has_got_offset(got_type));
1935 got_offset = gsym->got_offset(got_type) - target->got_size();
1939 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1940 gold_assert(object->local_has_got_offset(r_sym, got_type));
1941 got_offset = (object->local_got_offset(r_sym, got_type)
1942 - target->got_size());
1944 if (optimized_type == tls::TLSOPT_TO_IE)
1946 gold_assert(tls_segment != NULL);
1947 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1948 got_offset, view, view_size);
1951 else if (optimized_type == tls::TLSOPT_NONE)
1953 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1955 // Relocate the field with the offset of the pair of GOT
1957 Relocate_functions<32, false>::rel32(view, got_offset);
1962 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1963 _("unsupported reloc %u"),
1967 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1968 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1970 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1971 _("both SUN and GNU model "
1972 "TLS relocations"));
1975 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1976 if (optimized_type == tls::TLSOPT_TO_LE)
1978 gold_assert(tls_segment != NULL);
1979 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1980 value, view, view_size);
1983 else if (optimized_type == tls::TLSOPT_NONE)
1985 // Relocate the field with the offset of the GOT entry for
1986 // the module index.
1987 unsigned int got_offset;
1988 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1989 - target->got_size());
1990 Relocate_functions<32, false>::rel32(view, got_offset);
1993 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1994 _("unsupported reloc %u"),
1998 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1999 if (optimized_type == tls::TLSOPT_TO_LE)
2001 // This reloc can appear in debugging sections, in which
2002 // case we must not convert to local-exec. We decide what
2003 // to do based on whether the section is marked as
2004 // containing executable code. That is what the GNU linker
2006 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2007 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2009 gold_assert(tls_segment != NULL);
2010 value -= tls_segment->memsz();
2013 Relocate_functions<32, false>::rel32(view, value);
2016 case elfcpp::R_386_TLS_IE: // Initial-exec
2017 case elfcpp::R_386_TLS_GOTIE:
2018 case elfcpp::R_386_TLS_IE_32:
2019 if (optimized_type == tls::TLSOPT_TO_LE)
2021 gold_assert(tls_segment != NULL);
2022 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2023 rel, r_type, value, view,
2027 else if (optimized_type == tls::TLSOPT_NONE)
2029 // Relocate the field with the offset of the GOT entry for
2030 // the tp-relative offset of the symbol.
2031 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2032 ? GOT_TYPE_TLS_OFFSET
2033 : GOT_TYPE_TLS_NOFFSET);
2034 unsigned int got_offset;
2037 gold_assert(gsym->has_got_offset(got_type));
2038 got_offset = gsym->got_offset(got_type);
2042 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2043 gold_assert(object->local_has_got_offset(r_sym, got_type));
2044 got_offset = object->local_got_offset(r_sym, got_type);
2046 // For the R_386_TLS_IE relocation, we need to apply the
2047 // absolute address of the GOT entry.
2048 if (r_type == elfcpp::R_386_TLS_IE)
2049 got_offset += target->got_plt_section()->address();
2050 // All GOT offsets are relative to the end of the GOT.
2051 got_offset -= target->got_size();
2052 Relocate_functions<32, false>::rel32(view, got_offset);
2055 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2056 _("unsupported reloc %u"),
2060 case elfcpp::R_386_TLS_LE: // Local-exec
2061 // If we're creating a shared library, a dynamic relocation will
2062 // have been created for this location, so do not apply it now.
2063 if (!parameters->options().shared())
2065 gold_assert(tls_segment != NULL);
2066 value -= tls_segment->memsz();
2067 Relocate_functions<32, false>::rel32(view, value);
2071 case elfcpp::R_386_TLS_LE_32:
2072 // If we're creating a shared library, a dynamic relocation will
2073 // have been created for this location, so do not apply it now.
2074 if (!parameters->options().shared())
2076 gold_assert(tls_segment != NULL);
2077 value = tls_segment->memsz() - value;
2078 Relocate_functions<32, false>::rel32(view, value);
2084 // Do a relocation in which we convert a TLS General-Dynamic to a
2088 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2090 Output_segment* tls_segment,
2091 const elfcpp::Rel<32, false>& rel,
2093 elfcpp::Elf_types<32>::Elf_Addr value,
2094 unsigned char* view,
2095 section_size_type view_size)
2097 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2098 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2099 // leal foo(%reg),%eax; call ___tls_get_addr
2100 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2102 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2103 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2105 unsigned char op1 = view[-1];
2106 unsigned char op2 = view[-2];
2108 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2109 op2 == 0x8d || op2 == 0x04);
2110 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2116 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2117 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2118 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2119 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2120 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2124 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2125 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2126 if (rel.get_r_offset() + 9 < view_size
2129 // There is a trailing nop. Use the size byte subl.
2130 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2135 // Use the five byte subl.
2136 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2140 value = tls_segment->memsz() - value;
2141 Relocate_functions<32, false>::rel32(view + roff, value);
2143 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2145 this->skip_call_tls_get_addr_ = true;
2148 // Do a relocation in which we convert a TLS General-Dynamic to an
2152 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2155 const elfcpp::Rel<32, false>& rel,
2157 elfcpp::Elf_types<32>::Elf_Addr value,
2158 unsigned char* view,
2159 section_size_type view_size)
2161 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2162 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2164 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2165 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2167 unsigned char op1 = view[-1];
2168 unsigned char op2 = view[-2];
2170 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2171 op2 == 0x8d || op2 == 0x04);
2172 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2176 // FIXME: For now, support only the first (SIB) form.
2177 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2181 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2182 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2183 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2184 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2185 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2189 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2190 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2191 if (rel.get_r_offset() + 9 < view_size
2194 // FIXME: This is not the right instruction sequence.
2195 // There is a trailing nop. Use the size byte subl.
2196 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2201 // FIXME: This is not the right instruction sequence.
2202 // Use the five byte subl.
2203 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2207 Relocate_functions<32, false>::rel32(view + roff, value);
2209 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2211 this->skip_call_tls_get_addr_ = true;
2214 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2215 // General-Dynamic to a Local-Exec.
2218 Target_i386::Relocate::tls_desc_gd_to_le(
2219 const Relocate_info<32, false>* relinfo,
2221 Output_segment* tls_segment,
2222 const elfcpp::Rel<32, false>& rel,
2223 unsigned int r_type,
2224 elfcpp::Elf_types<32>::Elf_Addr value,
2225 unsigned char* view,
2226 section_size_type view_size)
2228 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2230 // leal foo@TLSDESC(%ebx), %eax
2231 // ==> leal foo@NTPOFF, %eax
2232 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2233 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2234 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2235 view[-2] == 0x8d && view[-1] == 0x83);
2237 value -= tls_segment->memsz();
2238 Relocate_functions<32, false>::rel32(view, value);
2242 // call *foo@TLSCALL(%eax)
2244 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2245 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2246 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2247 view[0] == 0xff && view[1] == 0x10);
2253 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2254 // General-Dynamic to an Initial-Exec.
2257 Target_i386::Relocate::tls_desc_gd_to_ie(
2258 const Relocate_info<32, false>* relinfo,
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 // ==> movl foo@GOTNTPOFF(%ebx), %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 Relocate_functions<32, false>::rel32(view, value);
2280 // call *foo@TLSCALL(%eax)
2282 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2283 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2284 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2285 view[0] == 0xff && view[1] == 0x10);
2291 // Do a relocation in which we convert a TLS Local-Dynamic to a
2295 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2298 const elfcpp::Rel<32, false>& rel,
2300 elfcpp::Elf_types<32>::Elf_Addr,
2301 unsigned char* view,
2302 section_size_type view_size)
2304 // leal foo(%reg), %eax; call ___tls_get_addr
2305 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2307 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2308 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2310 // FIXME: Does this test really always pass?
2311 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2312 view[-2] == 0x8d && view[-1] == 0x83);
2314 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2316 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2318 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2320 this->skip_call_tls_get_addr_ = true;
2323 // Do a relocation in which we convert a TLS Initial-Exec to a
2327 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2329 Output_segment* tls_segment,
2330 const elfcpp::Rel<32, false>& rel,
2331 unsigned int r_type,
2332 elfcpp::Elf_types<32>::Elf_Addr value,
2333 unsigned char* view,
2334 section_size_type view_size)
2336 // We have to actually change the instructions, which means that we
2337 // need to examine the opcodes to figure out which instruction we
2339 if (r_type == elfcpp::R_386_TLS_IE)
2341 // movl %gs:XX,%eax ==> movl $YY,%eax
2342 // movl %gs:XX,%reg ==> movl $YY,%reg
2343 // addl %gs:XX,%reg ==> addl $YY,%reg
2344 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2345 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2347 unsigned char op1 = view[-1];
2350 // movl XX,%eax ==> movl $YY,%eax
2355 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2357 unsigned char op2 = view[-2];
2360 // movl XX,%reg ==> movl $YY,%reg
2361 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2362 (op1 & 0xc7) == 0x05);
2364 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2366 else if (op2 == 0x03)
2368 // addl XX,%reg ==> addl $YY,%reg
2369 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2370 (op1 & 0xc7) == 0x05);
2372 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2375 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2380 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2381 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2382 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2383 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2384 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2386 unsigned char op1 = view[-1];
2387 unsigned char op2 = view[-2];
2388 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2389 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2392 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2394 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2396 else if (op2 == 0x2b)
2398 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2400 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2402 else if (op2 == 0x03)
2404 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2406 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2409 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2412 value = tls_segment->memsz() - value;
2413 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2416 Relocate_functions<32, false>::rel32(view, value);
2419 // Relocate section data.
2422 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2423 unsigned int sh_type,
2424 const unsigned char* prelocs,
2426 Output_section* output_section,
2427 bool needs_special_offset_handling,
2428 unsigned char* view,
2429 elfcpp::Elf_types<32>::Elf_Addr address,
2430 section_size_type view_size,
2431 const Reloc_symbol_changes* reloc_symbol_changes)
2433 gold_assert(sh_type == elfcpp::SHT_REL);
2435 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2436 Target_i386::Relocate>(
2442 needs_special_offset_handling,
2446 reloc_symbol_changes);
2449 // Return the size of a relocation while scanning during a relocatable
2453 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2454 unsigned int r_type,
2459 case elfcpp::R_386_NONE:
2460 case elfcpp::R_386_GNU_VTINHERIT:
2461 case elfcpp::R_386_GNU_VTENTRY:
2462 case elfcpp::R_386_TLS_GD: // Global-dynamic
2463 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2464 case elfcpp::R_386_TLS_DESC_CALL:
2465 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2466 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2467 case elfcpp::R_386_TLS_IE: // Initial-exec
2468 case elfcpp::R_386_TLS_IE_32:
2469 case elfcpp::R_386_TLS_GOTIE:
2470 case elfcpp::R_386_TLS_LE: // Local-exec
2471 case elfcpp::R_386_TLS_LE_32:
2474 case elfcpp::R_386_32:
2475 case elfcpp::R_386_PC32:
2476 case elfcpp::R_386_GOT32:
2477 case elfcpp::R_386_PLT32:
2478 case elfcpp::R_386_GOTOFF:
2479 case elfcpp::R_386_GOTPC:
2482 case elfcpp::R_386_16:
2483 case elfcpp::R_386_PC16:
2486 case elfcpp::R_386_8:
2487 case elfcpp::R_386_PC8:
2490 // These are relocations which should only be seen by the
2491 // dynamic linker, and should never be seen here.
2492 case elfcpp::R_386_COPY:
2493 case elfcpp::R_386_GLOB_DAT:
2494 case elfcpp::R_386_JUMP_SLOT:
2495 case elfcpp::R_386_RELATIVE:
2496 case elfcpp::R_386_TLS_TPOFF:
2497 case elfcpp::R_386_TLS_DTPMOD32:
2498 case elfcpp::R_386_TLS_DTPOFF32:
2499 case elfcpp::R_386_TLS_TPOFF32:
2500 case elfcpp::R_386_TLS_DESC:
2501 object->error(_("unexpected reloc %u in object file"), r_type);
2504 case elfcpp::R_386_32PLT:
2505 case elfcpp::R_386_TLS_GD_32:
2506 case elfcpp::R_386_TLS_GD_PUSH:
2507 case elfcpp::R_386_TLS_GD_CALL:
2508 case elfcpp::R_386_TLS_GD_POP:
2509 case elfcpp::R_386_TLS_LDM_32:
2510 case elfcpp::R_386_TLS_LDM_PUSH:
2511 case elfcpp::R_386_TLS_LDM_CALL:
2512 case elfcpp::R_386_TLS_LDM_POP:
2513 case elfcpp::R_386_USED_BY_INTEL_200:
2515 object->error(_("unsupported reloc %u in object file"), r_type);
2520 // Scan the relocs during a relocatable link.
2523 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
2525 Sized_relobj<32, false>* object,
2526 unsigned int data_shndx,
2527 unsigned int sh_type,
2528 const unsigned char* prelocs,
2530 Output_section* output_section,
2531 bool needs_special_offset_handling,
2532 size_t local_symbol_count,
2533 const unsigned char* plocal_symbols,
2534 Relocatable_relocs* rr)
2536 gold_assert(sh_type == elfcpp::SHT_REL);
2538 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2539 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2541 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2542 Scan_relocatable_relocs>(
2550 needs_special_offset_handling,
2556 // Relocate a section during a relocatable link.
2559 Target_i386::relocate_for_relocatable(
2560 const Relocate_info<32, false>* relinfo,
2561 unsigned int sh_type,
2562 const unsigned char* prelocs,
2564 Output_section* output_section,
2565 off_t offset_in_output_section,
2566 const Relocatable_relocs* rr,
2567 unsigned char* view,
2568 elfcpp::Elf_types<32>::Elf_Addr view_address,
2569 section_size_type view_size,
2570 unsigned char* reloc_view,
2571 section_size_type reloc_view_size)
2573 gold_assert(sh_type == elfcpp::SHT_REL);
2575 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2580 offset_in_output_section,
2589 // Return the value to use for a dynamic which requires special
2590 // treatment. This is how we support equality comparisons of function
2591 // pointers across shared library boundaries, as described in the
2592 // processor specific ABI supplement.
2595 Target_i386::do_dynsym_value(const Symbol* gsym) const
2597 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2598 return this->plt_section()->address() + gsym->plt_offset();
2601 // Return a string used to fill a code section with nops to take up
2602 // the specified length.
2605 Target_i386::do_code_fill(section_size_type length) const
2609 // Build a jmp instruction to skip over the bytes.
2610 unsigned char jmp[5];
2612 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2613 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2614 + std::string(length - 5, '\0'));
2617 // Nop sequences of various lengths.
2618 const char nop1[1] = { 0x90 }; // nop
2619 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2620 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2621 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2622 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2623 0x00 }; // leal 0(%esi,1),%esi
2624 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2626 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2628 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2629 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2630 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2631 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2633 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2634 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2636 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2637 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2639 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2640 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2641 0x00, 0x00, 0x00, 0x00 };
2642 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2643 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2644 0x27, 0x00, 0x00, 0x00,
2646 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2647 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2648 0xbc, 0x27, 0x00, 0x00,
2650 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2651 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2652 0x90, 0x90, 0x90, 0x90,
2655 const char* nops[16] = {
2657 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2658 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2661 return std::string(nops[length], length);
2664 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2665 // compiled with -fstack-split. The function calls non-stack-split
2666 // code. We have to change the function so that it always ensures
2667 // that it has enough stack space to run some random function.
2670 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2671 section_offset_type fnoffset,
2672 section_size_type fnsize,
2673 unsigned char* view,
2674 section_size_type view_size,
2676 std::string* to) const
2678 // The function starts with a comparison of the stack pointer and a
2679 // field in the TCB. This is followed by a jump.
2682 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2685 // We will call __morestack if the carry flag is set after this
2686 // comparison. We turn the comparison into an stc instruction
2688 view[fnoffset] = '\xf9';
2689 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2691 // lea NN(%esp),%ecx
2692 // lea NN(%esp),%edx
2693 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2694 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
2697 // This is loading an offset from the stack pointer for a
2698 // comparison. The offset is negative, so we decrease the
2699 // offset by the amount of space we need for the stack. This
2700 // means we will avoid calling __morestack if there happens to
2701 // be plenty of space on the stack already.
2702 unsigned char* pval = view + fnoffset + 3;
2703 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2704 val -= parameters->options().split_stack_adjust_size();
2705 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2709 if (!object->has_no_split_stack())
2710 object->error(_("failed to match split-stack sequence at "
2711 "section %u offset %0zx"),
2712 shndx, static_cast<size_t>(fnoffset));
2716 // We have to change the function so that it calls
2717 // __morestack_non_split instead of __morestack. The former will
2718 // allocate additional stack space.
2719 *from = "__morestack";
2720 *to = "__morestack_non_split";
2723 // The selector for i386 object files.
2725 class Target_selector_i386 : public Target_selector_freebsd
2728 Target_selector_i386()
2729 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2730 "elf32-i386", "elf32-i386-freebsd")
2734 do_instantiate_target()
2735 { return new Target_i386(); }
2738 Target_selector_i386 target_selector_i386;
2740 } // End anonymous namespace.