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"
46 class Output_data_plt_i386;
48 // The i386 target class.
49 // TLS info comes from
50 // http://people.redhat.com/drepper/tls.pdf
51 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
53 class Target_i386 : public Sized_target<32, false>
56 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
59 : Sized_target<32, false>(&i386_info),
60 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
61 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
62 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
68 gc_process_relocs(const General_options& options,
71 Sized_relobj<32, false>* object,
72 unsigned int data_shndx,
74 const unsigned char* prelocs,
76 Output_section* output_section,
77 bool needs_special_offset_handling,
78 size_t local_symbol_count,
79 const unsigned char* plocal_symbols);
81 // Scan the relocations to look for symbol adjustments.
83 scan_relocs(const General_options& options,
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*);
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);
117 // Scan the relocs during a relocatable link.
119 scan_relocatable_relocs(const General_options& options,
120 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 the size of the GOT section.
161 gold_assert(this->got_ != NULL);
162 return this->got_->data_size();
166 // The class which scans relocations.
170 local(const General_options& options, Symbol_table* symtab,
171 Layout* layout, Target_i386* target,
172 Sized_relobj<32, false>* object,
173 unsigned int data_shndx,
174 Output_section* output_section,
175 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
176 const elfcpp::Sym<32, false>& lsym);
179 global(const General_options& options, Symbol_table* symtab,
180 Layout* layout, Target_i386* target,
181 Sized_relobj<32, false>* object,
182 unsigned int data_shndx,
183 Output_section* output_section,
184 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
188 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
191 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
195 // The class which implements relocation.
200 : skip_call_tls_get_addr_(false),
201 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
206 if (this->skip_call_tls_get_addr_)
208 // FIXME: This needs to specify the location somehow.
209 gold_error(_("missing expected TLS relocation"));
213 // Return whether the static relocation needs to be applied.
215 should_apply_static_reloc(const Sized_symbol<32>* gsym,
219 // Do a relocation. Return false if the caller should not issue
220 // any warnings about this relocation.
222 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
223 const elfcpp::Rel<32, false>&,
224 unsigned int r_type, const Sized_symbol<32>*,
225 const Symbol_value<32>*,
226 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
230 // Do a TLS relocation.
232 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
233 size_t relnum, const elfcpp::Rel<32, false>&,
234 unsigned int r_type, const Sized_symbol<32>*,
235 const Symbol_value<32>*,
236 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
239 // Do a TLS General-Dynamic to Initial-Exec transition.
241 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
242 Output_segment* tls_segment,
243 const elfcpp::Rel<32, false>&, unsigned int r_type,
244 elfcpp::Elf_types<32>::Elf_Addr value,
246 section_size_type view_size);
248 // Do a TLS General-Dynamic to Local-Exec transition.
250 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
251 Output_segment* tls_segment,
252 const elfcpp::Rel<32, false>&, unsigned int r_type,
253 elfcpp::Elf_types<32>::Elf_Addr value,
255 section_size_type view_size);
257 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
260 tls_desc_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_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
270 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
271 Output_segment* tls_segment,
272 const elfcpp::Rel<32, false>&, unsigned int r_type,
273 elfcpp::Elf_types<32>::Elf_Addr value,
275 section_size_type view_size);
277 // Do a TLS Local-Dynamic to Local-Exec transition.
279 tls_ld_to_le(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 Initial-Exec to Local-Exec transition.
288 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
289 Output_segment* tls_segment,
290 const elfcpp::Rel<32, false>&, unsigned int r_type,
291 elfcpp::Elf_types<32>::Elf_Addr value,
293 section_size_type view_size);
295 // We need to keep track of which type of local dynamic relocation
296 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
297 enum Local_dynamic_type
304 // This is set if we should skip the next reloc, which should be a
305 // PLT32 reloc against ___tls_get_addr.
306 bool skip_call_tls_get_addr_;
307 // The type of local dynamic relocation we have seen in the section
308 // being relocated, if any.
309 Local_dynamic_type local_dynamic_type_;
312 // A class which returns the size required for a relocation type,
313 // used while scanning relocs during a relocatable link.
314 class Relocatable_size_for_reloc
318 get_size_for_reloc(unsigned int, Relobj*);
321 // Adjust TLS relocation type based on the options and whether this
322 // is a local symbol.
323 static tls::Tls_optimization
324 optimize_tls_reloc(bool is_final, int r_type);
326 // Get the GOT section, creating it if necessary.
327 Output_data_got<32, false>*
328 got_section(Symbol_table*, Layout*);
330 // Get the GOT PLT section.
332 got_plt_section() const
334 gold_assert(this->got_plt_ != NULL);
335 return this->got_plt_;
338 // Create a PLT entry for a global symbol.
340 make_plt_entry(Symbol_table*, Layout*, Symbol*);
342 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
344 define_tls_base_symbol(Symbol_table*, Layout*);
346 // Create a GOT entry for the TLS module index.
348 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
349 Sized_relobj<32, false>* object);
351 // Get the PLT section.
352 const Output_data_plt_i386*
355 gold_assert(this->plt_ != NULL);
359 // Get the dynamic reloc section, creating it if necessary.
361 rel_dyn_section(Layout*);
363 // Return true if the symbol may need a COPY relocation.
364 // References from an executable object to non-function symbols
365 // defined in a dynamic object may need a COPY relocation.
367 may_need_copy_reloc(Symbol* gsym)
369 return (!parameters->options().shared()
370 && gsym->is_from_dynobj()
371 && gsym->type() != elfcpp::STT_FUNC);
374 // Add a potential copy relocation.
376 copy_reloc(Symbol_table* symtab, Layout* layout,
377 Sized_relobj<32, false>* object,
378 unsigned int shndx, Output_section* output_section,
379 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
381 this->copy_relocs_.copy_reloc(symtab, layout,
382 symtab->get_sized_symbol<32>(sym),
383 object, shndx, output_section, reloc,
384 this->rel_dyn_section(layout));
387 // Information about this specific target which we pass to the
388 // general Target structure.
389 static const Target::Target_info i386_info;
391 // The types of GOT entries needed for this platform.
394 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
395 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
396 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
397 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
398 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
402 Output_data_got<32, false>* got_;
404 Output_data_plt_i386* plt_;
405 // The GOT PLT section.
406 Output_data_space* got_plt_;
407 // The dynamic reloc section.
408 Reloc_section* rel_dyn_;
409 // Relocs saved to avoid a COPY reloc.
410 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
411 // Space for variables copied with a COPY reloc.
412 Output_data_space* dynbss_;
413 // Offset of the GOT entry for the TLS module index.
414 unsigned int got_mod_index_offset_;
415 // True if the _TLS_MODULE_BASE_ symbol has been defined.
416 bool tls_base_symbol_defined_;
419 const Target::Target_info Target_i386::i386_info =
422 false, // is_big_endian
423 elfcpp::EM_386, // machine_code
424 false, // has_make_symbol
425 false, // has_resolve
426 true, // has_code_fill
427 true, // is_default_stack_executable
429 "/usr/lib/libc.so.1", // dynamic_linker
430 0x08048000, // default_text_segment_address
431 0x1000, // abi_pagesize (overridable by -z max-page-size)
432 0x1000 // common_pagesize (overridable by -z common-page-size)
435 // Get the GOT section, creating it if necessary.
437 Output_data_got<32, false>*
438 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
440 if (this->got_ == NULL)
442 gold_assert(symtab != NULL && layout != NULL);
444 this->got_ = new Output_data_got<32, false>();
447 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
449 | elfcpp::SHF_WRITE),
453 // The old GNU linker creates a .got.plt section. We just
454 // create another set of data in the .got section. Note that we
455 // always create a PLT if we create a GOT, although the PLT
457 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
458 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
460 | elfcpp::SHF_WRITE),
464 // The first three entries are reserved.
465 this->got_plt_->set_current_data_size(3 * 4);
467 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
468 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
470 0, 0, elfcpp::STT_OBJECT,
472 elfcpp::STV_HIDDEN, 0,
479 // Get the dynamic reloc section, creating it if necessary.
481 Target_i386::Reloc_section*
482 Target_i386::rel_dyn_section(Layout* layout)
484 if (this->rel_dyn_ == NULL)
486 gold_assert(layout != NULL);
487 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
488 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
489 elfcpp::SHF_ALLOC, this->rel_dyn_);
491 return this->rel_dyn_;
494 // A class to handle the PLT data.
496 class Output_data_plt_i386 : public Output_section_data
499 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
501 Output_data_plt_i386(Layout*, Output_data_space*);
503 // Add an entry to the PLT.
505 add_entry(Symbol* gsym);
507 // Return the .rel.plt section data.
510 { return this->rel_; }
514 do_adjust_output_section(Output_section* os);
516 // Write to a map file.
518 do_print_to_mapfile(Mapfile* mapfile) const
519 { mapfile->print_output_data(this, _("** PLT")); }
522 // The size of an entry in the PLT.
523 static const int plt_entry_size = 16;
525 // The first entry in the PLT for an executable.
526 static unsigned char exec_first_plt_entry[plt_entry_size];
528 // The first entry in the PLT for a shared object.
529 static unsigned char dyn_first_plt_entry[plt_entry_size];
531 // Other entries in the PLT for an executable.
532 static unsigned char exec_plt_entry[plt_entry_size];
534 // Other entries in the PLT for a shared object.
535 static unsigned char dyn_plt_entry[plt_entry_size];
537 // Set the final size.
539 set_final_data_size()
540 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
542 // Write out the PLT data.
544 do_write(Output_file*);
546 // The reloc section.
548 // The .got.plt section.
549 Output_data_space* got_plt_;
550 // The number of PLT entries.
554 // Create the PLT section. The ordinary .got section is an argument,
555 // since we need to refer to the start. We also create our own .got
556 // section just for PLT entries.
558 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
559 Output_data_space* got_plt)
560 : Output_section_data(4), got_plt_(got_plt), count_(0)
562 this->rel_ = new Reloc_section(false);
563 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
564 elfcpp::SHF_ALLOC, this->rel_);
568 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
570 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
571 // linker, and so do we.
575 // Add an entry to the PLT.
578 Output_data_plt_i386::add_entry(Symbol* gsym)
580 gold_assert(!gsym->has_plt_offset());
582 // Note that when setting the PLT offset we skip the initial
583 // reserved PLT entry.
584 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
588 section_offset_type got_offset = this->got_plt_->current_data_size();
590 // Every PLT entry needs a GOT entry which points back to the PLT
591 // entry (this will be changed by the dynamic linker, normally
592 // lazily when the function is called).
593 this->got_plt_->set_current_data_size(got_offset + 4);
595 // Every PLT entry needs a reloc.
596 gsym->set_needs_dynsym_entry();
597 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
600 // Note that we don't need to save the symbol. The contents of the
601 // PLT are independent of which symbols are used. The symbols only
602 // appear in the relocations.
605 // The first entry in the PLT for an executable.
607 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
609 0xff, 0x35, // pushl contents of memory address
610 0, 0, 0, 0, // replaced with address of .got + 4
611 0xff, 0x25, // jmp indirect
612 0, 0, 0, 0, // replaced with address of .got + 8
616 // The first entry in the PLT for a shared object.
618 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
620 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
621 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
625 // Subsequent entries in the PLT for an executable.
627 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
629 0xff, 0x25, // jmp indirect
630 0, 0, 0, 0, // replaced with address of symbol in .got
631 0x68, // pushl immediate
632 0, 0, 0, 0, // replaced with offset into relocation table
633 0xe9, // jmp relative
634 0, 0, 0, 0 // replaced with offset to start of .plt
637 // Subsequent entries in the PLT for a shared object.
639 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
641 0xff, 0xa3, // jmp *offset(%ebx)
642 0, 0, 0, 0, // replaced with offset of symbol in .got
643 0x68, // pushl immediate
644 0, 0, 0, 0, // replaced with offset into relocation table
645 0xe9, // jmp relative
646 0, 0, 0, 0 // replaced with offset to start of .plt
649 // Write out the PLT. This uses the hand-coded instructions above,
650 // and adjusts them as needed. This is all specified by the i386 ELF
651 // Processor Supplement.
654 Output_data_plt_i386::do_write(Output_file* of)
656 const off_t offset = this->offset();
657 const section_size_type oview_size =
658 convert_to_section_size_type(this->data_size());
659 unsigned char* const oview = of->get_output_view(offset, oview_size);
661 const off_t got_file_offset = this->got_plt_->offset();
662 const section_size_type got_size =
663 convert_to_section_size_type(this->got_plt_->data_size());
664 unsigned char* const got_view = of->get_output_view(got_file_offset,
667 unsigned char* pov = oview;
669 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
670 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
672 if (parameters->options().shared())
673 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
676 memcpy(pov, exec_first_plt_entry, plt_entry_size);
677 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
678 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
680 pov += plt_entry_size;
682 unsigned char* got_pov = got_view;
684 memset(got_pov, 0, 12);
687 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
689 unsigned int plt_offset = plt_entry_size;
690 unsigned int plt_rel_offset = 0;
691 unsigned int got_offset = 12;
692 const unsigned int count = this->count_;
693 for (unsigned int i = 0;
696 pov += plt_entry_size,
698 plt_offset += plt_entry_size,
699 plt_rel_offset += rel_size,
702 // Set and adjust the PLT entry itself.
704 if (parameters->options().shared())
706 memcpy(pov, dyn_plt_entry, plt_entry_size);
707 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
711 memcpy(pov, exec_plt_entry, plt_entry_size);
712 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
717 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
718 elfcpp::Swap<32, false>::writeval(pov + 12,
719 - (plt_offset + plt_entry_size));
721 // Set the entry in the GOT.
722 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
725 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
726 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
728 of->write_output_view(offset, oview_size, oview);
729 of->write_output_view(got_file_offset, got_size, got_view);
732 // Create a PLT entry for a global symbol.
735 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
737 if (gsym->has_plt_offset())
740 if (this->plt_ == NULL)
742 // Create the GOT sections first.
743 this->got_section(symtab, layout);
745 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
746 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
748 | elfcpp::SHF_EXECINSTR),
752 this->plt_->add_entry(gsym);
755 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
758 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
760 if (this->tls_base_symbol_defined_)
763 Output_segment* tls_segment = layout->tls_segment();
764 if (tls_segment != NULL)
766 bool is_exec = parameters->options().output_is_executable();
767 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
771 elfcpp::STV_HIDDEN, 0,
773 ? Symbol::SEGMENT_END
774 : Symbol::SEGMENT_START),
777 this->tls_base_symbol_defined_ = true;
780 // Create a GOT entry for the TLS module index.
783 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
784 Sized_relobj<32, false>* object)
786 if (this->got_mod_index_offset_ == -1U)
788 gold_assert(symtab != NULL && layout != NULL && object != NULL);
789 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
790 Output_data_got<32, false>* got = this->got_section(symtab, layout);
791 unsigned int got_offset = got->add_constant(0);
792 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
794 got->add_constant(0);
795 this->got_mod_index_offset_ = got_offset;
797 return this->got_mod_index_offset_;
800 // Optimize the TLS relocation type based on what we know about the
801 // symbol. IS_FINAL is true if the final address of this symbol is
802 // known at link time.
804 tls::Tls_optimization
805 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
807 // If we are generating a shared library, then we can't do anything
809 if (parameters->options().shared())
810 return tls::TLSOPT_NONE;
814 case elfcpp::R_386_TLS_GD:
815 case elfcpp::R_386_TLS_GOTDESC:
816 case elfcpp::R_386_TLS_DESC_CALL:
817 // These are General-Dynamic which permits fully general TLS
818 // access. Since we know that we are generating an executable,
819 // we can convert this to Initial-Exec. If we also know that
820 // this is a local symbol, we can further switch to Local-Exec.
822 return tls::TLSOPT_TO_LE;
823 return tls::TLSOPT_TO_IE;
825 case elfcpp::R_386_TLS_LDM:
826 // This is Local-Dynamic, which refers to a local symbol in the
827 // dynamic TLS block. Since we know that we generating an
828 // executable, we can switch to Local-Exec.
829 return tls::TLSOPT_TO_LE;
831 case elfcpp::R_386_TLS_LDO_32:
832 // Another type of Local-Dynamic relocation.
833 return tls::TLSOPT_TO_LE;
835 case elfcpp::R_386_TLS_IE:
836 case elfcpp::R_386_TLS_GOTIE:
837 case elfcpp::R_386_TLS_IE_32:
838 // These are Initial-Exec relocs which get the thread offset
839 // from the GOT. If we know that we are linking against the
840 // local symbol, we can switch to Local-Exec, which links the
841 // thread offset into the instruction.
843 return tls::TLSOPT_TO_LE;
844 return tls::TLSOPT_NONE;
846 case elfcpp::R_386_TLS_LE:
847 case elfcpp::R_386_TLS_LE_32:
848 // When we already have Local-Exec, there is nothing further we
850 return tls::TLSOPT_NONE;
857 // Report an unsupported relocation against a local symbol.
860 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
863 gold_error(_("%s: unsupported reloc %u against local symbol"),
864 object->name().c_str(), r_type);
867 // Scan a relocation for a local symbol.
870 Target_i386::Scan::local(const General_options&,
871 Symbol_table* symtab,
874 Sized_relobj<32, false>* object,
875 unsigned int data_shndx,
876 Output_section* output_section,
877 const elfcpp::Rel<32, false>& reloc,
879 const elfcpp::Sym<32, false>& lsym)
883 case elfcpp::R_386_NONE:
884 case elfcpp::R_386_GNU_VTINHERIT:
885 case elfcpp::R_386_GNU_VTENTRY:
888 case elfcpp::R_386_32:
889 // If building a shared library (or a position-independent
890 // executable), we need to create a dynamic relocation for
891 // this location. The relocation applied at link time will
892 // apply the link-time value, so we flag the location with
893 // an R_386_RELATIVE relocation so the dynamic loader can
894 // relocate it easily.
895 if (parameters->options().output_is_position_independent())
897 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
898 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
899 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
900 output_section, data_shndx,
901 reloc.get_r_offset());
905 case elfcpp::R_386_16:
906 case elfcpp::R_386_8:
907 // If building a shared library (or a position-independent
908 // executable), we need to create a dynamic relocation for
909 // this location. Because the addend needs to remain in the
910 // data section, we need to be careful not to apply this
911 // relocation statically.
912 if (parameters->options().output_is_position_independent())
914 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
915 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
916 if (lsym.get_st_type() != elfcpp::STT_SECTION)
917 rel_dyn->add_local(object, r_sym, r_type, output_section,
918 data_shndx, reloc.get_r_offset());
921 gold_assert(lsym.get_st_value() == 0);
922 unsigned int shndx = lsym.get_st_shndx();
924 shndx = object->adjust_sym_shndx(r_sym, shndx,
927 object->error(_("section symbol %u has bad shndx %u"),
930 rel_dyn->add_local_section(object, shndx,
931 r_type, output_section,
932 data_shndx, reloc.get_r_offset());
937 case elfcpp::R_386_PC32:
938 case elfcpp::R_386_PC16:
939 case elfcpp::R_386_PC8:
942 case elfcpp::R_386_PLT32:
943 // Since we know this is a local symbol, we can handle this as a
947 case elfcpp::R_386_GOTOFF:
948 case elfcpp::R_386_GOTPC:
949 // We need a GOT section.
950 target->got_section(symtab, layout);
953 case elfcpp::R_386_GOT32:
955 // The symbol requires a GOT entry.
956 Output_data_got<32, false>* got = target->got_section(symtab, layout);
957 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
958 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
960 // If we are generating a shared object, we need to add a
961 // dynamic RELATIVE relocation for this symbol's GOT entry.
962 if (parameters->options().output_is_position_independent())
964 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
965 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
966 rel_dyn->add_local_relative(
967 object, r_sym, elfcpp::R_386_RELATIVE, got,
968 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
974 // These are relocations which should only be seen by the
975 // dynamic linker, and should never be seen here.
976 case elfcpp::R_386_COPY:
977 case elfcpp::R_386_GLOB_DAT:
978 case elfcpp::R_386_JUMP_SLOT:
979 case elfcpp::R_386_RELATIVE:
980 case elfcpp::R_386_TLS_TPOFF:
981 case elfcpp::R_386_TLS_DTPMOD32:
982 case elfcpp::R_386_TLS_DTPOFF32:
983 case elfcpp::R_386_TLS_TPOFF32:
984 case elfcpp::R_386_TLS_DESC:
985 gold_error(_("%s: unexpected reloc %u in object file"),
986 object->name().c_str(), r_type);
989 // These are initial TLS relocs, which are expected when
991 case elfcpp::R_386_TLS_GD: // Global-dynamic
992 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
993 case elfcpp::R_386_TLS_DESC_CALL:
994 case elfcpp::R_386_TLS_LDM: // Local-dynamic
995 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
996 case elfcpp::R_386_TLS_IE: // Initial-exec
997 case elfcpp::R_386_TLS_IE_32:
998 case elfcpp::R_386_TLS_GOTIE:
999 case elfcpp::R_386_TLS_LE: // Local-exec
1000 case elfcpp::R_386_TLS_LE_32:
1002 bool output_is_shared = parameters->options().shared();
1003 const tls::Tls_optimization optimized_type
1004 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1007 case elfcpp::R_386_TLS_GD: // Global-dynamic
1008 if (optimized_type == tls::TLSOPT_NONE)
1010 // Create a pair of GOT entries for the module index and
1011 // dtv-relative offset.
1012 Output_data_got<32, false>* got
1013 = target->got_section(symtab, layout);
1014 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1015 unsigned int shndx = lsym.get_st_shndx();
1017 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1019 object->error(_("local symbol %u has bad shndx %u"),
1022 got->add_local_pair_with_rel(object, r_sym, shndx,
1024 target->rel_dyn_section(layout),
1025 elfcpp::R_386_TLS_DTPMOD32, 0);
1027 else if (optimized_type != tls::TLSOPT_TO_LE)
1028 unsupported_reloc_local(object, r_type);
1031 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1032 target->define_tls_base_symbol(symtab, layout);
1033 if (optimized_type == tls::TLSOPT_NONE)
1035 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1036 Output_data_got<32, false>* got
1037 = target->got_section(symtab, layout);
1038 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1039 unsigned int shndx = lsym.get_st_shndx();
1041 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1043 object->error(_("local symbol %u has bad shndx %u"),
1046 got->add_local_pair_with_rel(object, r_sym, shndx,
1048 target->rel_dyn_section(layout),
1049 elfcpp::R_386_TLS_DESC, 0);
1051 else if (optimized_type != tls::TLSOPT_TO_LE)
1052 unsupported_reloc_local(object, r_type);
1055 case elfcpp::R_386_TLS_DESC_CALL:
1058 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1059 if (optimized_type == tls::TLSOPT_NONE)
1061 // Create a GOT entry for the module index.
1062 target->got_mod_index_entry(symtab, layout, object);
1064 else if (optimized_type != tls::TLSOPT_TO_LE)
1065 unsupported_reloc_local(object, r_type);
1068 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1071 case elfcpp::R_386_TLS_IE: // Initial-exec
1072 case elfcpp::R_386_TLS_IE_32:
1073 case elfcpp::R_386_TLS_GOTIE:
1074 layout->set_has_static_tls();
1075 if (optimized_type == tls::TLSOPT_NONE)
1077 // For the R_386_TLS_IE relocation, we need to create a
1078 // dynamic relocation when building a shared library.
1079 if (r_type == elfcpp::R_386_TLS_IE
1080 && parameters->options().shared())
1082 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1084 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1085 rel_dyn->add_local_relative(object, r_sym,
1086 elfcpp::R_386_RELATIVE,
1087 output_section, data_shndx,
1088 reloc.get_r_offset());
1090 // Create a GOT entry for the tp-relative offset.
1091 Output_data_got<32, false>* got
1092 = target->got_section(symtab, layout);
1093 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1094 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1095 ? elfcpp::R_386_TLS_TPOFF32
1096 : elfcpp::R_386_TLS_TPOFF);
1097 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1098 ? GOT_TYPE_TLS_OFFSET
1099 : GOT_TYPE_TLS_NOFFSET);
1100 got->add_local_with_rel(object, r_sym, got_type,
1101 target->rel_dyn_section(layout),
1104 else if (optimized_type != tls::TLSOPT_TO_LE)
1105 unsupported_reloc_local(object, r_type);
1108 case elfcpp::R_386_TLS_LE: // Local-exec
1109 case elfcpp::R_386_TLS_LE_32:
1110 layout->set_has_static_tls();
1111 if (output_is_shared)
1113 // We need to create a dynamic relocation.
1114 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1115 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1116 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1117 ? elfcpp::R_386_TLS_TPOFF32
1118 : elfcpp::R_386_TLS_TPOFF);
1119 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1120 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1121 data_shndx, reloc.get_r_offset());
1131 case elfcpp::R_386_32PLT:
1132 case elfcpp::R_386_TLS_GD_32:
1133 case elfcpp::R_386_TLS_GD_PUSH:
1134 case elfcpp::R_386_TLS_GD_CALL:
1135 case elfcpp::R_386_TLS_GD_POP:
1136 case elfcpp::R_386_TLS_LDM_32:
1137 case elfcpp::R_386_TLS_LDM_PUSH:
1138 case elfcpp::R_386_TLS_LDM_CALL:
1139 case elfcpp::R_386_TLS_LDM_POP:
1140 case elfcpp::R_386_USED_BY_INTEL_200:
1142 unsupported_reloc_local(object, r_type);
1147 // Report an unsupported relocation against a global symbol.
1150 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1151 unsigned int r_type,
1154 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1155 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1158 // Scan a relocation for a global symbol.
1161 Target_i386::Scan::global(const General_options&,
1162 Symbol_table* symtab,
1164 Target_i386* target,
1165 Sized_relobj<32, false>* object,
1166 unsigned int data_shndx,
1167 Output_section* output_section,
1168 const elfcpp::Rel<32, false>& reloc,
1169 unsigned int r_type,
1174 case elfcpp::R_386_NONE:
1175 case elfcpp::R_386_GNU_VTINHERIT:
1176 case elfcpp::R_386_GNU_VTENTRY:
1179 case elfcpp::R_386_32:
1180 case elfcpp::R_386_16:
1181 case elfcpp::R_386_8:
1183 // Make a PLT entry if necessary.
1184 if (gsym->needs_plt_entry())
1186 target->make_plt_entry(symtab, layout, gsym);
1187 // Since this is not a PC-relative relocation, we may be
1188 // taking the address of a function. In that case we need to
1189 // set the entry in the dynamic symbol table to the address of
1191 if (gsym->is_from_dynobj() && !parameters->options().shared())
1192 gsym->set_needs_dynsym_value();
1194 // Make a dynamic relocation if necessary.
1195 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1197 if (target->may_need_copy_reloc(gsym))
1199 target->copy_reloc(symtab, layout, object,
1200 data_shndx, output_section, gsym, reloc);
1202 else if (r_type == elfcpp::R_386_32
1203 && gsym->can_use_relative_reloc(false))
1205 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1206 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1207 output_section, object,
1208 data_shndx, reloc.get_r_offset());
1212 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1213 rel_dyn->add_global(gsym, r_type, output_section, object,
1214 data_shndx, reloc.get_r_offset());
1220 case elfcpp::R_386_PC32:
1221 case elfcpp::R_386_PC16:
1222 case elfcpp::R_386_PC8:
1224 // Make a PLT entry if necessary.
1225 if (gsym->needs_plt_entry())
1227 // These relocations are used for function calls only in
1228 // non-PIC code. For a 32-bit relocation in a shared library,
1229 // we'll need a text relocation anyway, so we can skip the
1230 // PLT entry and let the dynamic linker bind the call directly
1231 // to the target. For smaller relocations, we should use a
1232 // PLT entry to ensure that the call can reach.
1233 if (!parameters->options().shared()
1234 || r_type != elfcpp::R_386_PC32)
1235 target->make_plt_entry(symtab, layout, gsym);
1237 // Make a dynamic relocation if necessary.
1238 int flags = Symbol::NON_PIC_REF;
1239 if (gsym->type() == elfcpp::STT_FUNC)
1240 flags |= Symbol::FUNCTION_CALL;
1241 if (gsym->needs_dynamic_reloc(flags))
1243 if (target->may_need_copy_reloc(gsym))
1245 target->copy_reloc(symtab, layout, object,
1246 data_shndx, output_section, gsym, reloc);
1250 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1251 rel_dyn->add_global(gsym, r_type, output_section, object,
1252 data_shndx, reloc.get_r_offset());
1258 case elfcpp::R_386_GOT32:
1260 // The symbol requires a GOT entry.
1261 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1262 if (gsym->final_value_is_known())
1263 got->add_global(gsym, GOT_TYPE_STANDARD);
1266 // If this symbol is not fully resolved, we need to add a
1267 // GOT entry with a dynamic relocation.
1268 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1269 if (gsym->is_from_dynobj()
1270 || gsym->is_undefined()
1271 || gsym->is_preemptible())
1272 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1273 rel_dyn, elfcpp::R_386_GLOB_DAT);
1276 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1277 rel_dyn->add_global_relative(
1278 gsym, elfcpp::R_386_RELATIVE, got,
1279 gsym->got_offset(GOT_TYPE_STANDARD));
1285 case elfcpp::R_386_PLT32:
1286 // If the symbol is fully resolved, this is just a PC32 reloc.
1287 // Otherwise we need a PLT entry.
1288 if (gsym->final_value_is_known())
1290 // If building a shared library, we can also skip the PLT entry
1291 // if the symbol is defined in the output file and is protected
1293 if (gsym->is_defined()
1294 && !gsym->is_from_dynobj()
1295 && !gsym->is_preemptible())
1297 target->make_plt_entry(symtab, layout, gsym);
1300 case elfcpp::R_386_GOTOFF:
1301 case elfcpp::R_386_GOTPC:
1302 // We need a GOT section.
1303 target->got_section(symtab, layout);
1306 // These are relocations which should only be seen by the
1307 // dynamic linker, and should never be seen here.
1308 case elfcpp::R_386_COPY:
1309 case elfcpp::R_386_GLOB_DAT:
1310 case elfcpp::R_386_JUMP_SLOT:
1311 case elfcpp::R_386_RELATIVE:
1312 case elfcpp::R_386_TLS_TPOFF:
1313 case elfcpp::R_386_TLS_DTPMOD32:
1314 case elfcpp::R_386_TLS_DTPOFF32:
1315 case elfcpp::R_386_TLS_TPOFF32:
1316 case elfcpp::R_386_TLS_DESC:
1317 gold_error(_("%s: unexpected reloc %u in object file"),
1318 object->name().c_str(), r_type);
1321 // These are initial tls relocs, which are expected when
1323 case elfcpp::R_386_TLS_GD: // Global-dynamic
1324 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1325 case elfcpp::R_386_TLS_DESC_CALL:
1326 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1327 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1328 case elfcpp::R_386_TLS_IE: // Initial-exec
1329 case elfcpp::R_386_TLS_IE_32:
1330 case elfcpp::R_386_TLS_GOTIE:
1331 case elfcpp::R_386_TLS_LE: // Local-exec
1332 case elfcpp::R_386_TLS_LE_32:
1334 const bool is_final = gsym->final_value_is_known();
1335 const tls::Tls_optimization optimized_type
1336 = Target_i386::optimize_tls_reloc(is_final, r_type);
1339 case elfcpp::R_386_TLS_GD: // Global-dynamic
1340 if (optimized_type == tls::TLSOPT_NONE)
1342 // Create a pair of GOT entries for the module index and
1343 // dtv-relative offset.
1344 Output_data_got<32, false>* got
1345 = target->got_section(symtab, layout);
1346 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1347 target->rel_dyn_section(layout),
1348 elfcpp::R_386_TLS_DTPMOD32,
1349 elfcpp::R_386_TLS_DTPOFF32);
1351 else if (optimized_type == tls::TLSOPT_TO_IE)
1353 // Create a GOT entry for the tp-relative offset.
1354 Output_data_got<32, false>* got
1355 = target->got_section(symtab, layout);
1356 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1357 target->rel_dyn_section(layout),
1358 elfcpp::R_386_TLS_TPOFF);
1360 else if (optimized_type != tls::TLSOPT_TO_LE)
1361 unsupported_reloc_global(object, r_type, gsym);
1364 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1365 target->define_tls_base_symbol(symtab, layout);
1366 if (optimized_type == tls::TLSOPT_NONE)
1368 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1369 Output_data_got<32, false>* got
1370 = target->got_section(symtab, layout);
1371 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1372 target->rel_dyn_section(layout),
1373 elfcpp::R_386_TLS_DESC, 0);
1375 else if (optimized_type == tls::TLSOPT_TO_IE)
1377 // Create a GOT entry for the tp-relative offset.
1378 Output_data_got<32, false>* got
1379 = target->got_section(symtab, layout);
1380 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1381 target->rel_dyn_section(layout),
1382 elfcpp::R_386_TLS_TPOFF);
1384 else if (optimized_type != tls::TLSOPT_TO_LE)
1385 unsupported_reloc_global(object, r_type, gsym);
1388 case elfcpp::R_386_TLS_DESC_CALL:
1391 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1392 if (optimized_type == tls::TLSOPT_NONE)
1394 // Create a GOT entry for the module index.
1395 target->got_mod_index_entry(symtab, layout, object);
1397 else if (optimized_type != tls::TLSOPT_TO_LE)
1398 unsupported_reloc_global(object, r_type, gsym);
1401 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1404 case elfcpp::R_386_TLS_IE: // Initial-exec
1405 case elfcpp::R_386_TLS_IE_32:
1406 case elfcpp::R_386_TLS_GOTIE:
1407 layout->set_has_static_tls();
1408 if (optimized_type == tls::TLSOPT_NONE)
1410 // For the R_386_TLS_IE relocation, we need to create a
1411 // dynamic relocation when building a shared library.
1412 if (r_type == elfcpp::R_386_TLS_IE
1413 && parameters->options().shared())
1415 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1416 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1417 output_section, object,
1419 reloc.get_r_offset());
1421 // Create a GOT entry for the tp-relative offset.
1422 Output_data_got<32, false>* got
1423 = target->got_section(symtab, layout);
1424 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1425 ? elfcpp::R_386_TLS_TPOFF32
1426 : elfcpp::R_386_TLS_TPOFF);
1427 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1428 ? GOT_TYPE_TLS_OFFSET
1429 : GOT_TYPE_TLS_NOFFSET);
1430 got->add_global_with_rel(gsym, got_type,
1431 target->rel_dyn_section(layout),
1434 else if (optimized_type != tls::TLSOPT_TO_LE)
1435 unsupported_reloc_global(object, r_type, gsym);
1438 case elfcpp::R_386_TLS_LE: // Local-exec
1439 case elfcpp::R_386_TLS_LE_32:
1440 layout->set_has_static_tls();
1441 if (parameters->options().shared())
1443 // We need to create a dynamic relocation.
1444 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1445 ? elfcpp::R_386_TLS_TPOFF32
1446 : elfcpp::R_386_TLS_TPOFF);
1447 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1448 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1449 data_shndx, reloc.get_r_offset());
1459 case elfcpp::R_386_32PLT:
1460 case elfcpp::R_386_TLS_GD_32:
1461 case elfcpp::R_386_TLS_GD_PUSH:
1462 case elfcpp::R_386_TLS_GD_CALL:
1463 case elfcpp::R_386_TLS_GD_POP:
1464 case elfcpp::R_386_TLS_LDM_32:
1465 case elfcpp::R_386_TLS_LDM_PUSH:
1466 case elfcpp::R_386_TLS_LDM_CALL:
1467 case elfcpp::R_386_TLS_LDM_POP:
1468 case elfcpp::R_386_USED_BY_INTEL_200:
1470 unsupported_reloc_global(object, r_type, gsym);
1475 // Process relocations for gc.
1478 Target_i386::gc_process_relocs(const General_options& options,
1479 Symbol_table* symtab,
1481 Sized_relobj<32, false>* object,
1482 unsigned int data_shndx,
1484 const unsigned char* prelocs,
1486 Output_section* output_section,
1487 bool needs_special_offset_handling,
1488 size_t local_symbol_count,
1489 const unsigned char* plocal_symbols)
1491 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1502 needs_special_offset_handling,
1507 // Scan relocations for a section.
1510 Target_i386::scan_relocs(const General_options& options,
1511 Symbol_table* symtab,
1513 Sized_relobj<32, false>* object,
1514 unsigned int data_shndx,
1515 unsigned int sh_type,
1516 const unsigned char* prelocs,
1518 Output_section* output_section,
1519 bool needs_special_offset_handling,
1520 size_t local_symbol_count,
1521 const unsigned char* plocal_symbols)
1523 if (sh_type == elfcpp::SHT_RELA)
1525 gold_error(_("%s: unsupported RELA reloc section"),
1526 object->name().c_str());
1530 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1541 needs_special_offset_handling,
1546 // Finalize the sections.
1549 Target_i386::do_finalize_sections(Layout* layout)
1551 // Fill in some more dynamic tags.
1552 Output_data_dynamic* const odyn = layout->dynamic_data();
1555 if (this->got_plt_ != NULL)
1556 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1558 if (this->plt_ != NULL)
1560 const Output_data* od = this->plt_->rel_plt();
1561 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1562 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1563 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1566 if (this->rel_dyn_ != NULL)
1568 const Output_data* od = this->rel_dyn_;
1569 odyn->add_section_address(elfcpp::DT_REL, od);
1570 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1571 odyn->add_constant(elfcpp::DT_RELENT,
1572 elfcpp::Elf_sizes<32>::rel_size);
1575 if (!parameters->options().shared())
1577 // The value of the DT_DEBUG tag is filled in by the dynamic
1578 // linker at run time, and used by the debugger.
1579 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1583 // Emit any relocs we saved in an attempt to avoid generating COPY
1585 if (this->copy_relocs_.any_saved_relocs())
1586 this->copy_relocs_.emit(this->rel_dyn_section(layout));
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,
1600 // For local symbols, we will have created a non-RELATIVE dynamic
1601 // relocation only if (a) the output is position independent,
1602 // (b) the relocation is absolute (not pc- or segment-relative), and
1603 // (c) the relocation is not 32 bits wide.
1605 return !(parameters->options().output_is_position_independent()
1606 && (ref_flags & Symbol::ABSOLUTE_REF)
1609 // For global symbols, we use the same helper routines used in the
1610 // scan pass. If we did not create a dynamic relocation, or if we
1611 // created a RELATIVE dynamic relocation, we should apply the static
1613 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1614 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1615 && gsym->can_use_relative_reloc(ref_flags
1616 & Symbol::FUNCTION_CALL);
1617 return !has_dyn || is_rel;
1620 // Perform a relocation.
1623 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1624 Target_i386* target,
1626 const elfcpp::Rel<32, false>& rel,
1627 unsigned int r_type,
1628 const Sized_symbol<32>* gsym,
1629 const Symbol_value<32>* psymval,
1630 unsigned char* view,
1631 elfcpp::Elf_types<32>::Elf_Addr address,
1632 section_size_type view_size)
1634 if (this->skip_call_tls_get_addr_)
1636 if ((r_type != elfcpp::R_386_PLT32
1637 && r_type != elfcpp::R_386_PC32)
1639 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1640 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1641 _("missing expected TLS relocation"));
1644 this->skip_call_tls_get_addr_ = false;
1649 // Pick the value to use for symbols defined in shared objects.
1650 Symbol_value<32> symval;
1652 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1653 || r_type == elfcpp::R_386_PC16
1654 || r_type == elfcpp::R_386_PC32))
1656 symval.set_output_value(target->plt_section()->address()
1657 + gsym->plt_offset());
1661 const Sized_relobj<32, false>* object = relinfo->object;
1663 // Get the GOT offset if needed.
1664 // The GOT pointer points to the end of the GOT section.
1665 // We need to subtract the size of the GOT section to get
1666 // the actual offset to use in the relocation.
1667 bool have_got_offset = false;
1668 unsigned int got_offset = 0;
1671 case elfcpp::R_386_GOT32:
1674 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1675 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1676 - target->got_size());
1680 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1681 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1682 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1683 - target->got_size());
1685 have_got_offset = true;
1694 case elfcpp::R_386_NONE:
1695 case elfcpp::R_386_GNU_VTINHERIT:
1696 case elfcpp::R_386_GNU_VTENTRY:
1699 case elfcpp::R_386_32:
1700 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1701 Relocate_functions<32, false>::rel32(view, object, psymval);
1704 case elfcpp::R_386_PC32:
1706 int ref_flags = Symbol::NON_PIC_REF;
1707 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1708 ref_flags |= Symbol::FUNCTION_CALL;
1709 if (should_apply_static_reloc(gsym, ref_flags, true))
1710 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1714 case elfcpp::R_386_16:
1715 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1716 Relocate_functions<32, false>::rel16(view, object, psymval);
1719 case elfcpp::R_386_PC16:
1721 int ref_flags = Symbol::NON_PIC_REF;
1722 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1723 ref_flags |= Symbol::FUNCTION_CALL;
1724 if (should_apply_static_reloc(gsym, ref_flags, false))
1725 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1729 case elfcpp::R_386_8:
1730 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1731 Relocate_functions<32, false>::rel8(view, object, psymval);
1734 case elfcpp::R_386_PC8:
1736 int ref_flags = Symbol::NON_PIC_REF;
1737 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1738 ref_flags |= Symbol::FUNCTION_CALL;
1739 if (should_apply_static_reloc(gsym, ref_flags, false))
1740 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1744 case elfcpp::R_386_PLT32:
1745 gold_assert(gsym == NULL
1746 || gsym->has_plt_offset()
1747 || gsym->final_value_is_known()
1748 || (gsym->is_defined()
1749 && !gsym->is_from_dynobj()
1750 && !gsym->is_preemptible()));
1751 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1754 case elfcpp::R_386_GOT32:
1755 gold_assert(have_got_offset);
1756 Relocate_functions<32, false>::rel32(view, got_offset);
1759 case elfcpp::R_386_GOTOFF:
1761 elfcpp::Elf_types<32>::Elf_Addr value;
1762 value = (psymval->value(object, 0)
1763 - target->got_plt_section()->address());
1764 Relocate_functions<32, false>::rel32(view, value);
1768 case elfcpp::R_386_GOTPC:
1770 elfcpp::Elf_types<32>::Elf_Addr value;
1771 value = target->got_plt_section()->address();
1772 Relocate_functions<32, false>::pcrel32(view, value, address);
1776 case elfcpp::R_386_COPY:
1777 case elfcpp::R_386_GLOB_DAT:
1778 case elfcpp::R_386_JUMP_SLOT:
1779 case elfcpp::R_386_RELATIVE:
1780 // These are outstanding tls relocs, which are unexpected when
1782 case elfcpp::R_386_TLS_TPOFF:
1783 case elfcpp::R_386_TLS_DTPMOD32:
1784 case elfcpp::R_386_TLS_DTPOFF32:
1785 case elfcpp::R_386_TLS_TPOFF32:
1786 case elfcpp::R_386_TLS_DESC:
1787 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1788 _("unexpected reloc %u in object file"),
1792 // These are initial tls relocs, which are expected when
1794 case elfcpp::R_386_TLS_GD: // Global-dynamic
1795 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1796 case elfcpp::R_386_TLS_DESC_CALL:
1797 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1798 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1799 case elfcpp::R_386_TLS_IE: // Initial-exec
1800 case elfcpp::R_386_TLS_IE_32:
1801 case elfcpp::R_386_TLS_GOTIE:
1802 case elfcpp::R_386_TLS_LE: // Local-exec
1803 case elfcpp::R_386_TLS_LE_32:
1804 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1805 view, address, view_size);
1808 case elfcpp::R_386_32PLT:
1809 case elfcpp::R_386_TLS_GD_32:
1810 case elfcpp::R_386_TLS_GD_PUSH:
1811 case elfcpp::R_386_TLS_GD_CALL:
1812 case elfcpp::R_386_TLS_GD_POP:
1813 case elfcpp::R_386_TLS_LDM_32:
1814 case elfcpp::R_386_TLS_LDM_PUSH:
1815 case elfcpp::R_386_TLS_LDM_CALL:
1816 case elfcpp::R_386_TLS_LDM_POP:
1817 case elfcpp::R_386_USED_BY_INTEL_200:
1819 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1820 _("unsupported reloc %u"),
1828 // Perform a TLS relocation.
1831 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1832 Target_i386* target,
1834 const elfcpp::Rel<32, false>& rel,
1835 unsigned int r_type,
1836 const Sized_symbol<32>* gsym,
1837 const Symbol_value<32>* psymval,
1838 unsigned char* view,
1839 elfcpp::Elf_types<32>::Elf_Addr,
1840 section_size_type view_size)
1842 Output_segment* tls_segment = relinfo->layout->tls_segment();
1844 const Sized_relobj<32, false>* object = relinfo->object;
1846 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1848 const bool is_final =
1850 ? !parameters->options().output_is_position_independent()
1851 : gsym->final_value_is_known());
1852 const tls::Tls_optimization optimized_type
1853 = Target_i386::optimize_tls_reloc(is_final, r_type);
1856 case elfcpp::R_386_TLS_GD: // Global-dynamic
1857 if (optimized_type == tls::TLSOPT_TO_LE)
1859 gold_assert(tls_segment != NULL);
1860 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1861 rel, r_type, value, view,
1867 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1868 ? GOT_TYPE_TLS_NOFFSET
1869 : GOT_TYPE_TLS_PAIR);
1870 unsigned int got_offset;
1873 gold_assert(gsym->has_got_offset(got_type));
1874 got_offset = gsym->got_offset(got_type) - target->got_size();
1878 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1879 gold_assert(object->local_has_got_offset(r_sym, got_type));
1880 got_offset = (object->local_got_offset(r_sym, got_type)
1881 - target->got_size());
1883 if (optimized_type == tls::TLSOPT_TO_IE)
1885 gold_assert(tls_segment != NULL);
1886 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1887 got_offset, view, view_size);
1890 else if (optimized_type == tls::TLSOPT_NONE)
1892 // Relocate the field with the offset of the pair of GOT
1894 Relocate_functions<32, false>::rel32(view, got_offset);
1898 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1899 _("unsupported reloc %u"),
1903 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1904 case elfcpp::R_386_TLS_DESC_CALL:
1905 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1906 if (optimized_type == tls::TLSOPT_TO_LE)
1908 gold_assert(tls_segment != NULL);
1909 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1910 rel, r_type, value, view,
1916 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1917 ? GOT_TYPE_TLS_NOFFSET
1918 : GOT_TYPE_TLS_DESC);
1919 unsigned int got_offset;
1922 gold_assert(gsym->has_got_offset(got_type));
1923 got_offset = gsym->got_offset(got_type) - target->got_size();
1927 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1928 gold_assert(object->local_has_got_offset(r_sym, got_type));
1929 got_offset = (object->local_got_offset(r_sym, got_type)
1930 - target->got_size());
1932 if (optimized_type == tls::TLSOPT_TO_IE)
1934 gold_assert(tls_segment != NULL);
1935 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1936 got_offset, view, view_size);
1939 else if (optimized_type == tls::TLSOPT_NONE)
1941 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1943 // Relocate the field with the offset of the pair of GOT
1945 Relocate_functions<32, false>::rel32(view, got_offset);
1950 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1951 _("unsupported reloc %u"),
1955 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1956 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1958 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1959 _("both SUN and GNU model "
1960 "TLS relocations"));
1963 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1964 if (optimized_type == tls::TLSOPT_TO_LE)
1966 gold_assert(tls_segment != NULL);
1967 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1968 value, view, view_size);
1971 else if (optimized_type == tls::TLSOPT_NONE)
1973 // Relocate the field with the offset of the GOT entry for
1974 // the module index.
1975 unsigned int got_offset;
1976 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1977 - target->got_size());
1978 Relocate_functions<32, false>::rel32(view, got_offset);
1981 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1982 _("unsupported reloc %u"),
1986 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1987 // This reloc can appear in debugging sections, in which case we
1988 // won't see the TLS_LDM reloc. The local_dynamic_type field
1990 if (optimized_type == tls::TLSOPT_TO_LE
1991 && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
1993 gold_assert(tls_segment != NULL);
1994 value -= tls_segment->memsz();
1996 Relocate_functions<32, false>::rel32(view, value);
1999 case elfcpp::R_386_TLS_IE: // Initial-exec
2000 case elfcpp::R_386_TLS_GOTIE:
2001 case elfcpp::R_386_TLS_IE_32:
2002 if (optimized_type == tls::TLSOPT_TO_LE)
2004 gold_assert(tls_segment != NULL);
2005 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2006 rel, r_type, value, view,
2010 else if (optimized_type == tls::TLSOPT_NONE)
2012 // Relocate the field with the offset of the GOT entry for
2013 // the tp-relative offset of the symbol.
2014 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2015 ? GOT_TYPE_TLS_OFFSET
2016 : GOT_TYPE_TLS_NOFFSET);
2017 unsigned int got_offset;
2020 gold_assert(gsym->has_got_offset(got_type));
2021 got_offset = gsym->got_offset(got_type);
2025 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2026 gold_assert(object->local_has_got_offset(r_sym, got_type));
2027 got_offset = object->local_got_offset(r_sym, got_type);
2029 // For the R_386_TLS_IE relocation, we need to apply the
2030 // absolute address of the GOT entry.
2031 if (r_type == elfcpp::R_386_TLS_IE)
2032 got_offset += target->got_plt_section()->address();
2033 // All GOT offsets are relative to the end of the GOT.
2034 got_offset -= target->got_size();
2035 Relocate_functions<32, false>::rel32(view, got_offset);
2038 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2039 _("unsupported reloc %u"),
2043 case elfcpp::R_386_TLS_LE: // Local-exec
2044 // If we're creating a shared library, a dynamic relocation will
2045 // have been created for this location, so do not apply it now.
2046 if (!parameters->options().shared())
2048 gold_assert(tls_segment != NULL);
2049 value -= tls_segment->memsz();
2050 Relocate_functions<32, false>::rel32(view, value);
2054 case elfcpp::R_386_TLS_LE_32:
2055 // If we're creating a shared library, a dynamic relocation will
2056 // have been created for this location, so do not apply it now.
2057 if (!parameters->options().shared())
2059 gold_assert(tls_segment != NULL);
2060 value = tls_segment->memsz() - value;
2061 Relocate_functions<32, false>::rel32(view, value);
2067 // Do a relocation in which we convert a TLS General-Dynamic to a
2071 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2073 Output_segment* tls_segment,
2074 const elfcpp::Rel<32, false>& rel,
2076 elfcpp::Elf_types<32>::Elf_Addr value,
2077 unsigned char* view,
2078 section_size_type view_size)
2080 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2081 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2082 // leal foo(%reg),%eax; call ___tls_get_addr
2083 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2085 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2086 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2088 unsigned char op1 = view[-1];
2089 unsigned char op2 = view[-2];
2091 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2092 op2 == 0x8d || op2 == 0x04);
2093 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2099 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2100 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2101 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2102 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2103 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2107 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2108 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2109 if (rel.get_r_offset() + 9 < view_size
2112 // There is a trailing nop. Use the size byte subl.
2113 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2118 // Use the five byte subl.
2119 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2123 value = tls_segment->memsz() - value;
2124 Relocate_functions<32, false>::rel32(view + roff, value);
2126 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2128 this->skip_call_tls_get_addr_ = true;
2131 // Do a relocation in which we convert a TLS General-Dynamic to an
2135 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2138 const elfcpp::Rel<32, false>& rel,
2140 elfcpp::Elf_types<32>::Elf_Addr value,
2141 unsigned char* view,
2142 section_size_type view_size)
2144 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2145 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2147 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2148 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2150 unsigned char op1 = view[-1];
2151 unsigned char op2 = view[-2];
2153 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2154 op2 == 0x8d || op2 == 0x04);
2155 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2159 // FIXME: For now, support only the first (SIB) form.
2160 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2164 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2165 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2166 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2167 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2168 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2172 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2173 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2174 if (rel.get_r_offset() + 9 < view_size
2177 // FIXME: This is not the right instruction sequence.
2178 // There is a trailing nop. Use the size byte subl.
2179 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2184 // FIXME: This is not the right instruction sequence.
2185 // Use the five byte subl.
2186 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2190 Relocate_functions<32, false>::rel32(view + roff, value);
2192 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2194 this->skip_call_tls_get_addr_ = true;
2197 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2198 // General-Dynamic to a Local-Exec.
2201 Target_i386::Relocate::tls_desc_gd_to_le(
2202 const Relocate_info<32, false>* relinfo,
2204 Output_segment* tls_segment,
2205 const elfcpp::Rel<32, false>& rel,
2206 unsigned int r_type,
2207 elfcpp::Elf_types<32>::Elf_Addr value,
2208 unsigned char* view,
2209 section_size_type view_size)
2211 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2213 // leal foo@TLSDESC(%ebx), %eax
2214 // ==> leal foo@NTPOFF, %eax
2215 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2216 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2217 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2218 view[-2] == 0x8d && view[-1] == 0x83);
2220 value -= tls_segment->memsz();
2221 Relocate_functions<32, false>::rel32(view, value);
2225 // call *foo@TLSCALL(%eax)
2227 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2228 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2229 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2230 view[0] == 0xff && view[1] == 0x10);
2236 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2237 // General-Dynamic to an Initial-Exec.
2240 Target_i386::Relocate::tls_desc_gd_to_ie(
2241 const Relocate_info<32, false>* relinfo,
2244 const elfcpp::Rel<32, false>& rel,
2245 unsigned int r_type,
2246 elfcpp::Elf_types<32>::Elf_Addr value,
2247 unsigned char* view,
2248 section_size_type view_size)
2250 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2252 // leal foo@TLSDESC(%ebx), %eax
2253 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2254 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2255 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2256 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2257 view[-2] == 0x8d && view[-1] == 0x83);
2259 Relocate_functions<32, false>::rel32(view, value);
2263 // call *foo@TLSCALL(%eax)
2265 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2266 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2267 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2268 view[0] == 0xff && view[1] == 0x10);
2274 // Do a relocation in which we convert a TLS Local-Dynamic to a
2278 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2281 const elfcpp::Rel<32, false>& rel,
2283 elfcpp::Elf_types<32>::Elf_Addr,
2284 unsigned char* view,
2285 section_size_type view_size)
2287 // leal foo(%reg), %eax; call ___tls_get_addr
2288 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2290 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2291 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2293 // FIXME: Does this test really always pass?
2294 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2295 view[-2] == 0x8d && view[-1] == 0x83);
2297 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2299 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2301 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2303 this->skip_call_tls_get_addr_ = true;
2306 // Do a relocation in which we convert a TLS Initial-Exec to a
2310 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2312 Output_segment* tls_segment,
2313 const elfcpp::Rel<32, false>& rel,
2314 unsigned int r_type,
2315 elfcpp::Elf_types<32>::Elf_Addr value,
2316 unsigned char* view,
2317 section_size_type view_size)
2319 // We have to actually change the instructions, which means that we
2320 // need to examine the opcodes to figure out which instruction we
2322 if (r_type == elfcpp::R_386_TLS_IE)
2324 // movl %gs:XX,%eax ==> movl $YY,%eax
2325 // movl %gs:XX,%reg ==> movl $YY,%reg
2326 // addl %gs:XX,%reg ==> addl $YY,%reg
2327 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2328 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2330 unsigned char op1 = view[-1];
2333 // movl XX,%eax ==> movl $YY,%eax
2338 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2340 unsigned char op2 = view[-2];
2343 // movl XX,%reg ==> movl $YY,%reg
2344 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2345 (op1 & 0xc7) == 0x05);
2347 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2349 else if (op2 == 0x03)
2351 // addl XX,%reg ==> addl $YY,%reg
2352 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2353 (op1 & 0xc7) == 0x05);
2355 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2358 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2363 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2364 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2365 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2366 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2367 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2369 unsigned char op1 = view[-1];
2370 unsigned char op2 = view[-2];
2371 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2372 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2375 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2377 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2379 else if (op2 == 0x2b)
2381 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2383 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2385 else if (op2 == 0x03)
2387 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2389 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2392 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2395 value = tls_segment->memsz() - value;
2396 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2399 Relocate_functions<32, false>::rel32(view, value);
2402 // Relocate section data.
2405 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2406 unsigned int sh_type,
2407 const unsigned char* prelocs,
2409 Output_section* output_section,
2410 bool needs_special_offset_handling,
2411 unsigned char* view,
2412 elfcpp::Elf_types<32>::Elf_Addr address,
2413 section_size_type view_size)
2415 gold_assert(sh_type == elfcpp::SHT_REL);
2417 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2418 Target_i386::Relocate>(
2424 needs_special_offset_handling,
2430 // Return the size of a relocation while scanning during a relocatable
2434 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2435 unsigned int r_type,
2440 case elfcpp::R_386_NONE:
2441 case elfcpp::R_386_GNU_VTINHERIT:
2442 case elfcpp::R_386_GNU_VTENTRY:
2443 case elfcpp::R_386_TLS_GD: // Global-dynamic
2444 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2445 case elfcpp::R_386_TLS_DESC_CALL:
2446 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2447 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2448 case elfcpp::R_386_TLS_IE: // Initial-exec
2449 case elfcpp::R_386_TLS_IE_32:
2450 case elfcpp::R_386_TLS_GOTIE:
2451 case elfcpp::R_386_TLS_LE: // Local-exec
2452 case elfcpp::R_386_TLS_LE_32:
2455 case elfcpp::R_386_32:
2456 case elfcpp::R_386_PC32:
2457 case elfcpp::R_386_GOT32:
2458 case elfcpp::R_386_PLT32:
2459 case elfcpp::R_386_GOTOFF:
2460 case elfcpp::R_386_GOTPC:
2463 case elfcpp::R_386_16:
2464 case elfcpp::R_386_PC16:
2467 case elfcpp::R_386_8:
2468 case elfcpp::R_386_PC8:
2471 // These are relocations which should only be seen by the
2472 // dynamic linker, and should never be seen here.
2473 case elfcpp::R_386_COPY:
2474 case elfcpp::R_386_GLOB_DAT:
2475 case elfcpp::R_386_JUMP_SLOT:
2476 case elfcpp::R_386_RELATIVE:
2477 case elfcpp::R_386_TLS_TPOFF:
2478 case elfcpp::R_386_TLS_DTPMOD32:
2479 case elfcpp::R_386_TLS_DTPOFF32:
2480 case elfcpp::R_386_TLS_TPOFF32:
2481 case elfcpp::R_386_TLS_DESC:
2482 object->error(_("unexpected reloc %u in object file"), r_type);
2485 case elfcpp::R_386_32PLT:
2486 case elfcpp::R_386_TLS_GD_32:
2487 case elfcpp::R_386_TLS_GD_PUSH:
2488 case elfcpp::R_386_TLS_GD_CALL:
2489 case elfcpp::R_386_TLS_GD_POP:
2490 case elfcpp::R_386_TLS_LDM_32:
2491 case elfcpp::R_386_TLS_LDM_PUSH:
2492 case elfcpp::R_386_TLS_LDM_CALL:
2493 case elfcpp::R_386_TLS_LDM_POP:
2494 case elfcpp::R_386_USED_BY_INTEL_200:
2496 object->error(_("unsupported reloc %u in object file"), r_type);
2501 // Scan the relocs during a relocatable link.
2504 Target_i386::scan_relocatable_relocs(const General_options& options,
2505 Symbol_table* symtab,
2507 Sized_relobj<32, false>* object,
2508 unsigned int data_shndx,
2509 unsigned int sh_type,
2510 const unsigned char* prelocs,
2512 Output_section* output_section,
2513 bool needs_special_offset_handling,
2514 size_t local_symbol_count,
2515 const unsigned char* plocal_symbols,
2516 Relocatable_relocs* rr)
2518 gold_assert(sh_type == elfcpp::SHT_REL);
2520 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2521 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2523 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2524 Scan_relocatable_relocs>(
2533 needs_special_offset_handling,
2539 // Relocate a section during a relocatable link.
2542 Target_i386::relocate_for_relocatable(
2543 const Relocate_info<32, false>* relinfo,
2544 unsigned int sh_type,
2545 const unsigned char* prelocs,
2547 Output_section* output_section,
2548 off_t offset_in_output_section,
2549 const Relocatable_relocs* rr,
2550 unsigned char* view,
2551 elfcpp::Elf_types<32>::Elf_Addr view_address,
2552 section_size_type view_size,
2553 unsigned char* reloc_view,
2554 section_size_type reloc_view_size)
2556 gold_assert(sh_type == elfcpp::SHT_REL);
2558 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2563 offset_in_output_section,
2572 // Return the value to use for a dynamic which requires special
2573 // treatment. This is how we support equality comparisons of function
2574 // pointers across shared library boundaries, as described in the
2575 // processor specific ABI supplement.
2578 Target_i386::do_dynsym_value(const Symbol* gsym) const
2580 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2581 return this->plt_section()->address() + gsym->plt_offset();
2584 // Return a string used to fill a code section with nops to take up
2585 // the specified length.
2588 Target_i386::do_code_fill(section_size_type length) const
2592 // Build a jmp instruction to skip over the bytes.
2593 unsigned char jmp[5];
2595 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2596 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2597 + std::string(length - 5, '\0'));
2600 // Nop sequences of various lengths.
2601 const char nop1[1] = { 0x90 }; // nop
2602 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2603 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2604 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2605 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2606 0x00 }; // leal 0(%esi,1),%esi
2607 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2609 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2611 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2612 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2613 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2614 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2616 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2617 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2619 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2620 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2622 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2623 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2624 0x00, 0x00, 0x00, 0x00 };
2625 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2626 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2627 0x27, 0x00, 0x00, 0x00,
2629 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2630 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2631 0xbc, 0x27, 0x00, 0x00,
2633 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2634 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2635 0x90, 0x90, 0x90, 0x90,
2638 const char* nops[16] = {
2640 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2641 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2644 return std::string(nops[length], length);
2647 // The selector for i386 object files.
2649 class Target_selector_i386 : public Target_selector
2652 Target_selector_i386()
2653 : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
2657 do_instantiate_target()
2658 { return new Target_i386(); }
2661 Target_selector_i386 target_selector_i386;
2663 } // End anonymous namespace.