1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008 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"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_i386;
47 // The i386 target class.
48 // TLS info comes from
49 // http://people.redhat.com/drepper/tls.pdf
50 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52 class Target_i386 : public Sized_target<32, false>
55 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
58 : Sized_target<32, false>(&i386_info),
59 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
60 copy_relocs_(NULL), dynbss_(NULL), got_mod_index_offset_(-1U)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options& options,
68 Sized_relobj<32, false>* object,
69 unsigned int data_shndx,
71 const unsigned char* prelocs,
73 Output_section* output_section,
74 bool needs_special_offset_handling,
75 size_t local_symbol_count,
76 const unsigned char* plocal_symbols);
78 // Finalize the sections.
80 do_finalize_sections(Layout*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info<32, false>*,
91 const unsigned char* prelocs,
93 Output_section* output_section,
94 bool needs_special_offset_handling,
96 elfcpp::Elf_types<32>::Elf_Addr view_address,
97 section_size_type view_size);
99 // Scan the relocs during a relocatable link.
101 scan_relocatable_relocs(const General_options& options,
102 Symbol_table* symtab,
104 Sized_relobj<32, false>* object,
105 unsigned int data_shndx,
106 unsigned int sh_type,
107 const unsigned char* prelocs,
109 Output_section* output_section,
110 bool needs_special_offset_handling,
111 size_t local_symbol_count,
112 const unsigned char* plocal_symbols,
113 Relocatable_relocs*);
115 // Relocate a section during a relocatable link.
117 relocate_for_relocatable(const Relocate_info<32, false>*,
118 unsigned int sh_type,
119 const unsigned char* prelocs,
121 Output_section* output_section,
122 off_t offset_in_output_section,
123 const Relocatable_relocs*,
125 elfcpp::Elf_types<32>::Elf_Addr view_address,
126 section_size_type view_size,
127 unsigned char* reloc_view,
128 section_size_type reloc_view_size);
130 // Return a string used to fill a code section with nops.
132 do_code_fill(section_size_type length) const;
134 // Return whether SYM is defined by the ABI.
136 do_is_defined_by_abi(Symbol* sym) const
137 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
139 // Return the size of the GOT section.
143 gold_assert(this->got_ != NULL);
144 return this->got_->data_size();
148 // The class which scans relocations.
152 local(const General_options& options, Symbol_table* symtab,
153 Layout* layout, Target_i386* target,
154 Sized_relobj<32, false>* object,
155 unsigned int data_shndx,
156 Output_section* output_section,
157 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
158 const elfcpp::Sym<32, false>& lsym);
161 global(const General_options& options, Symbol_table* symtab,
162 Layout* layout, Target_i386* target,
163 Sized_relobj<32, false>* object,
164 unsigned int data_shndx,
165 Output_section* output_section,
166 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
170 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
173 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
177 // The class which implements relocation.
182 : skip_call_tls_get_addr_(false),
183 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
188 if (this->skip_call_tls_get_addr_)
190 // FIXME: This needs to specify the location somehow.
191 gold_error(_("missing expected TLS relocation"));
195 // Return whether the static relocation needs to be applied.
197 should_apply_static_reloc(const Sized_symbol<32>* gsym,
201 // Do a relocation. Return false if the caller should not issue
202 // any warnings about this relocation.
204 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
205 const elfcpp::Rel<32, false>&,
206 unsigned int r_type, const Sized_symbol<32>*,
207 const Symbol_value<32>*,
208 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
212 // Do a TLS relocation.
214 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
215 size_t relnum, const elfcpp::Rel<32, false>&,
216 unsigned int r_type, const Sized_symbol<32>*,
217 const Symbol_value<32>*,
218 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
221 // Do a TLS General-Dynamic to Initial-Exec transition.
223 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
224 Output_segment* tls_segment,
225 const elfcpp::Rel<32, false>&, unsigned int r_type,
226 elfcpp::Elf_types<32>::Elf_Addr value,
228 section_size_type view_size);
230 // Do a TLS General-Dynamic to Local-Exec transition.
232 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
233 Output_segment* tls_segment,
234 const elfcpp::Rel<32, false>&, unsigned int r_type,
235 elfcpp::Elf_types<32>::Elf_Addr value,
237 section_size_type view_size);
239 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
242 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
243 Output_segment* tls_segment,
244 const elfcpp::Rel<32, false>&, unsigned int r_type,
245 elfcpp::Elf_types<32>::Elf_Addr value,
247 section_size_type view_size);
249 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
252 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
253 Output_segment* tls_segment,
254 const elfcpp::Rel<32, false>&, unsigned int r_type,
255 elfcpp::Elf_types<32>::Elf_Addr value,
257 section_size_type view_size);
259 // Do a TLS Local-Dynamic to Local-Exec transition.
261 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
262 Output_segment* tls_segment,
263 const elfcpp::Rel<32, false>&, unsigned int r_type,
264 elfcpp::Elf_types<32>::Elf_Addr value,
266 section_size_type view_size);
268 // Do a TLS Initial-Exec to Local-Exec transition.
270 tls_ie_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 // We need to keep track of which type of local dynamic relocation
278 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
279 enum Local_dynamic_type
286 // This is set if we should skip the next reloc, which should be a
287 // PLT32 reloc against ___tls_get_addr.
288 bool skip_call_tls_get_addr_;
289 // The type of local dynamic relocation we have seen in the section
290 // being relocated, if any.
291 Local_dynamic_type local_dynamic_type_;
294 // A class which returns the size required for a relocation type,
295 // used while scanning relocs during a relocatable link.
296 class Relocatable_size_for_reloc
300 get_size_for_reloc(unsigned int, Relobj*);
303 // Adjust TLS relocation type based on the options and whether this
304 // is a local symbol.
305 static tls::Tls_optimization
306 optimize_tls_reloc(bool is_final, int r_type);
308 // Get the GOT section, creating it if necessary.
309 Output_data_got<32, false>*
310 got_section(Symbol_table*, Layout*);
312 // Get the GOT PLT section.
314 got_plt_section() const
316 gold_assert(this->got_plt_ != NULL);
317 return this->got_plt_;
320 // Create a PLT entry for a global symbol.
322 make_plt_entry(Symbol_table*, Layout*, Symbol*);
324 // Create a GOT entry for the TLS module index.
326 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
327 Sized_relobj<32, false>* object);
329 // Get the PLT section.
330 const Output_data_plt_i386*
333 gold_assert(this->plt_ != NULL);
337 // Get the dynamic reloc section, creating it if necessary.
339 rel_dyn_section(Layout*);
341 // Return true if the symbol may need a COPY relocation.
342 // References from an executable object to non-function symbols
343 // defined in a dynamic object may need a COPY relocation.
345 may_need_copy_reloc(Symbol* gsym)
347 return (!parameters->options().shared()
348 && gsym->is_from_dynobj()
349 && gsym->type() != elfcpp::STT_FUNC);
352 // Copy a relocation against a global symbol.
354 copy_reloc(const General_options*, Symbol_table*, Layout*,
355 Sized_relobj<32, false>*, unsigned int,
356 Output_section*, Symbol*, const elfcpp::Rel<32, false>&);
358 // Information about this specific target which we pass to the
359 // general Target structure.
360 static const Target::Target_info i386_info;
362 // The types of GOT entries needed for this platform.
365 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
366 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
367 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
368 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
369 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
373 Output_data_got<32, false>* got_;
375 Output_data_plt_i386* plt_;
376 // The GOT PLT section.
377 Output_data_space* got_plt_;
378 // The dynamic reloc section.
379 Reloc_section* rel_dyn_;
380 // Relocs saved to avoid a COPY reloc.
381 Copy_relocs<32, false>* copy_relocs_;
382 // Space for variables copied with a COPY reloc.
383 Output_data_space* dynbss_;
384 // Offset of the GOT entry for the TLS module index.
385 unsigned int got_mod_index_offset_;
388 const Target::Target_info Target_i386::i386_info =
391 false, // is_big_endian
392 elfcpp::EM_386, // machine_code
393 false, // has_make_symbol
394 false, // has_resolve
395 true, // has_code_fill
396 true, // is_default_stack_executable
398 "/usr/lib/libc.so.1", // dynamic_linker
399 0x08048000, // default_text_segment_address
400 0x1000, // abi_pagesize (overridable by -z max-page-size)
401 0x1000 // common_pagesize (overridable by -z common-page-size)
404 // Get the GOT section, creating it if necessary.
406 Output_data_got<32, false>*
407 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
409 if (this->got_ == NULL)
411 gold_assert(symtab != NULL && layout != NULL);
413 this->got_ = new Output_data_got<32, false>();
415 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
416 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
419 // The old GNU linker creates a .got.plt section. We just
420 // create another set of data in the .got section. Note that we
421 // always create a PLT if we create a GOT, although the PLT
423 this->got_plt_ = new Output_data_space(4);
424 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
425 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
428 // The first three entries are reserved.
429 this->got_plt_->set_current_data_size(3 * 4);
431 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
432 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
434 0, 0, elfcpp::STT_OBJECT,
436 elfcpp::STV_HIDDEN, 0,
443 // Get the dynamic reloc section, creating it if necessary.
445 Target_i386::Reloc_section*
446 Target_i386::rel_dyn_section(Layout* layout)
448 if (this->rel_dyn_ == NULL)
450 gold_assert(layout != NULL);
451 this->rel_dyn_ = new Reloc_section();
452 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
453 elfcpp::SHF_ALLOC, this->rel_dyn_);
455 return this->rel_dyn_;
458 // A class to handle the PLT data.
460 class Output_data_plt_i386 : public Output_section_data
463 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
465 Output_data_plt_i386(Layout*, Output_data_space*);
467 // Add an entry to the PLT.
469 add_entry(Symbol* gsym);
471 // Return the .rel.plt section data.
474 { return this->rel_; }
478 do_adjust_output_section(Output_section* os);
481 // The size of an entry in the PLT.
482 static const int plt_entry_size = 16;
484 // The first entry in the PLT for an executable.
485 static unsigned char exec_first_plt_entry[plt_entry_size];
487 // The first entry in the PLT for a shared object.
488 static unsigned char dyn_first_plt_entry[plt_entry_size];
490 // Other entries in the PLT for an executable.
491 static unsigned char exec_plt_entry[plt_entry_size];
493 // Other entries in the PLT for a shared object.
494 static unsigned char dyn_plt_entry[plt_entry_size];
496 // Set the final size.
498 set_final_data_size()
499 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
501 // Write out the PLT data.
503 do_write(Output_file*);
505 // The reloc section.
507 // The .got.plt section.
508 Output_data_space* got_plt_;
509 // The number of PLT entries.
513 // Create the PLT section. The ordinary .got section is an argument,
514 // since we need to refer to the start. We also create our own .got
515 // section just for PLT entries.
517 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
518 Output_data_space* got_plt)
519 : Output_section_data(4), got_plt_(got_plt), count_(0)
521 this->rel_ = new Reloc_section();
522 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
523 elfcpp::SHF_ALLOC, this->rel_);
527 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
529 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
530 // linker, and so do we.
534 // Add an entry to the PLT.
537 Output_data_plt_i386::add_entry(Symbol* gsym)
539 gold_assert(!gsym->has_plt_offset());
541 // Note that when setting the PLT offset we skip the initial
542 // reserved PLT entry.
543 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
547 section_offset_type got_offset = this->got_plt_->current_data_size();
549 // Every PLT entry needs a GOT entry which points back to the PLT
550 // entry (this will be changed by the dynamic linker, normally
551 // lazily when the function is called).
552 this->got_plt_->set_current_data_size(got_offset + 4);
554 // Every PLT entry needs a reloc.
555 gsym->set_needs_dynsym_entry();
556 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
559 // Note that we don't need to save the symbol. The contents of the
560 // PLT are independent of which symbols are used. The symbols only
561 // appear in the relocations.
564 // The first entry in the PLT for an executable.
566 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
568 0xff, 0x35, // pushl contents of memory address
569 0, 0, 0, 0, // replaced with address of .got + 4
570 0xff, 0x25, // jmp indirect
571 0, 0, 0, 0, // replaced with address of .got + 8
575 // The first entry in the PLT for a shared object.
577 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
579 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
580 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
584 // Subsequent entries in the PLT for an executable.
586 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
588 0xff, 0x25, // jmp indirect
589 0, 0, 0, 0, // replaced with address of symbol in .got
590 0x68, // pushl immediate
591 0, 0, 0, 0, // replaced with offset into relocation table
592 0xe9, // jmp relative
593 0, 0, 0, 0 // replaced with offset to start of .plt
596 // Subsequent entries in the PLT for a shared object.
598 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
600 0xff, 0xa3, // jmp *offset(%ebx)
601 0, 0, 0, 0, // replaced with offset of symbol in .got
602 0x68, // pushl immediate
603 0, 0, 0, 0, // replaced with offset into relocation table
604 0xe9, // jmp relative
605 0, 0, 0, 0 // replaced with offset to start of .plt
608 // Write out the PLT. This uses the hand-coded instructions above,
609 // and adjusts them as needed. This is all specified by the i386 ELF
610 // Processor Supplement.
613 Output_data_plt_i386::do_write(Output_file* of)
615 const off_t offset = this->offset();
616 const section_size_type oview_size =
617 convert_to_section_size_type(this->data_size());
618 unsigned char* const oview = of->get_output_view(offset, oview_size);
620 const off_t got_file_offset = this->got_plt_->offset();
621 const section_size_type got_size =
622 convert_to_section_size_type(this->got_plt_->data_size());
623 unsigned char* const got_view = of->get_output_view(got_file_offset,
626 unsigned char* pov = oview;
628 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
629 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
631 if (parameters->options().shared())
632 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
635 memcpy(pov, exec_first_plt_entry, plt_entry_size);
636 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
637 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
639 pov += plt_entry_size;
641 unsigned char* got_pov = got_view;
643 memset(got_pov, 0, 12);
646 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
648 unsigned int plt_offset = plt_entry_size;
649 unsigned int plt_rel_offset = 0;
650 unsigned int got_offset = 12;
651 const unsigned int count = this->count_;
652 for (unsigned int i = 0;
655 pov += plt_entry_size,
657 plt_offset += plt_entry_size,
658 plt_rel_offset += rel_size,
661 // Set and adjust the PLT entry itself.
663 if (parameters->options().shared())
665 memcpy(pov, dyn_plt_entry, plt_entry_size);
666 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
670 memcpy(pov, exec_plt_entry, plt_entry_size);
671 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
676 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
677 elfcpp::Swap<32, false>::writeval(pov + 12,
678 - (plt_offset + plt_entry_size));
680 // Set the entry in the GOT.
681 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
684 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
685 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
687 of->write_output_view(offset, oview_size, oview);
688 of->write_output_view(got_file_offset, got_size, got_view);
691 // Create a PLT entry for a global symbol.
694 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
696 if (gsym->has_plt_offset())
699 if (this->plt_ == NULL)
701 // Create the GOT sections first.
702 this->got_section(symtab, layout);
704 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
705 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
707 | elfcpp::SHF_EXECINSTR),
711 this->plt_->add_entry(gsym);
714 // Create a GOT entry for the TLS module index.
717 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
718 Sized_relobj<32, false>* object)
720 if (this->got_mod_index_offset_ == -1U)
722 gold_assert(symtab != NULL && layout != NULL && object != NULL);
723 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
724 Output_data_got<32, false>* got = this->got_section(symtab, layout);
725 unsigned int got_offset = got->add_constant(0);
726 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
728 this->got_mod_index_offset_ = got_offset;
730 return this->got_mod_index_offset_;
733 // Handle a relocation against a non-function symbol defined in a
734 // dynamic object. The traditional way to handle this is to generate
735 // a COPY relocation to copy the variable at runtime from the shared
736 // object into the executable's data segment. However, this is
737 // undesirable in general, as if the size of the object changes in the
738 // dynamic object, the executable will no longer work correctly. If
739 // this relocation is in a writable section, then we can create a
740 // dynamic reloc and the dynamic linker will resolve it to the correct
741 // address at runtime. However, we do not want do that if the
742 // relocation is in a read-only section, as it would prevent the
743 // readonly segment from being shared. And if we have to eventually
744 // generate a COPY reloc, then any dynamic relocations will be
745 // useless. So this means that if this is a writable section, we need
746 // to save the relocation until we see whether we have to create a
747 // COPY relocation for this symbol for any other relocation.
750 Target_i386::copy_reloc(const General_options* options,
751 Symbol_table* symtab,
753 Sized_relobj<32, false>* object,
754 unsigned int data_shndx,
755 Output_section* output_section,
757 const elfcpp::Rel<32, false>& rel)
759 Sized_symbol<32>* ssym = symtab->get_sized_symbol<32>(gsym);
761 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
764 // So far we do not need a COPY reloc. Save this relocation.
765 // If it turns out that we never need a COPY reloc for this
766 // symbol, then we will emit the relocation.
767 if (this->copy_relocs_ == NULL)
768 this->copy_relocs_ = new Copy_relocs<32, false>();
769 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rel);
773 // Allocate space for this symbol in the .bss section.
775 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
777 // There is no defined way to determine the required alignment
778 // of the symbol. We pick the alignment based on the size. We
779 // set an arbitrary maximum of 256.
781 for (align = 1; align < 512; align <<= 1)
782 if ((symsize & align) != 0)
785 if (this->dynbss_ == NULL)
787 this->dynbss_ = new Output_data_space(align);
788 layout->add_output_section_data(".bss",
791 | elfcpp::SHF_WRITE),
795 Output_data_space* dynbss = this->dynbss_;
797 if (align > dynbss->addralign())
798 dynbss->set_space_alignment(align);
800 section_size_type dynbss_size =
801 convert_to_section_size_type(dynbss->current_data_size());
802 dynbss_size = align_address(dynbss_size, align);
803 section_size_type offset = dynbss_size;
804 dynbss->set_current_data_size(dynbss_size + symsize);
806 symtab->define_with_copy_reloc(ssym, dynbss, offset);
808 // Add the COPY reloc.
809 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
810 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
814 // Optimize the TLS relocation type based on what we know about the
815 // symbol. IS_FINAL is true if the final address of this symbol is
816 // known at link time.
818 tls::Tls_optimization
819 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
821 // If we are generating a shared library, then we can't do anything
823 if (parameters->options().shared())
824 return tls::TLSOPT_NONE;
828 case elfcpp::R_386_TLS_GD:
829 case elfcpp::R_386_TLS_GOTDESC:
830 case elfcpp::R_386_TLS_DESC_CALL:
831 // These are General-Dynamic which permits fully general TLS
832 // access. Since we know that we are generating an executable,
833 // we can convert this to Initial-Exec. If we also know that
834 // this is a local symbol, we can further switch to Local-Exec.
836 return tls::TLSOPT_TO_LE;
837 return tls::TLSOPT_TO_IE;
839 case elfcpp::R_386_TLS_LDM:
840 // This is Local-Dynamic, which refers to a local symbol in the
841 // dynamic TLS block. Since we know that we generating an
842 // executable, we can switch to Local-Exec.
843 return tls::TLSOPT_TO_LE;
845 case elfcpp::R_386_TLS_LDO_32:
846 // Another type of Local-Dynamic relocation.
847 return tls::TLSOPT_TO_LE;
849 case elfcpp::R_386_TLS_IE:
850 case elfcpp::R_386_TLS_GOTIE:
851 case elfcpp::R_386_TLS_IE_32:
852 // These are Initial-Exec relocs which get the thread offset
853 // from the GOT. If we know that we are linking against the
854 // local symbol, we can switch to Local-Exec, which links the
855 // thread offset into the instruction.
857 return tls::TLSOPT_TO_LE;
858 return tls::TLSOPT_NONE;
860 case elfcpp::R_386_TLS_LE:
861 case elfcpp::R_386_TLS_LE_32:
862 // When we already have Local-Exec, there is nothing further we
864 return tls::TLSOPT_NONE;
871 // Report an unsupported relocation against a local symbol.
874 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
877 gold_error(_("%s: unsupported reloc %u against local symbol"),
878 object->name().c_str(), r_type);
881 // Scan a relocation for a local symbol.
884 Target_i386::Scan::local(const General_options&,
885 Symbol_table* symtab,
888 Sized_relobj<32, false>* object,
889 unsigned int data_shndx,
890 Output_section* output_section,
891 const elfcpp::Rel<32, false>& reloc,
893 const elfcpp::Sym<32, false>& lsym)
897 case elfcpp::R_386_NONE:
898 case elfcpp::R_386_GNU_VTINHERIT:
899 case elfcpp::R_386_GNU_VTENTRY:
902 case elfcpp::R_386_32:
903 // If building a shared library (or a position-independent
904 // executable), we need to create a dynamic relocation for
905 // this location. The relocation applied at link time will
906 // apply the link-time value, so we flag the location with
907 // an R_386_RELATIVE relocation so the dynamic loader can
908 // relocate it easily.
909 if (parameters->options().output_is_position_independent())
911 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
912 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
913 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
914 output_section, data_shndx,
915 reloc.get_r_offset());
919 case elfcpp::R_386_16:
920 case elfcpp::R_386_8:
921 // If building a shared library (or a position-independent
922 // executable), we need to create a dynamic relocation for
923 // this location. Because the addend needs to remain in the
924 // data section, we need to be careful not to apply this
925 // relocation statically.
926 if (parameters->options().output_is_position_independent())
928 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
929 if (lsym.get_st_type() != elfcpp::STT_SECTION)
931 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
932 rel_dyn->add_local(object, r_sym, r_type, output_section,
933 data_shndx, reloc.get_r_offset());
937 gold_assert(lsym.get_st_value() == 0);
938 rel_dyn->add_local_section(object, lsym.get_st_shndx(),
939 r_type, output_section,
940 data_shndx, reloc.get_r_offset());
945 case elfcpp::R_386_PC32:
946 case elfcpp::R_386_PC16:
947 case elfcpp::R_386_PC8:
950 case elfcpp::R_386_PLT32:
951 // Since we know this is a local symbol, we can handle this as a
955 case elfcpp::R_386_GOTOFF:
956 case elfcpp::R_386_GOTPC:
957 // We need a GOT section.
958 target->got_section(symtab, layout);
961 case elfcpp::R_386_GOT32:
963 // The symbol requires a GOT entry.
964 Output_data_got<32, false>* got = target->got_section(symtab, layout);
965 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
966 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
968 // If we are generating a shared object, we need to add a
969 // dynamic RELATIVE relocation for this symbol's GOT entry.
970 if (parameters->options().output_is_position_independent())
972 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
973 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
974 rel_dyn->add_local_relative(
975 object, r_sym, elfcpp::R_386_RELATIVE, got,
976 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
982 // These are relocations which should only be seen by the
983 // dynamic linker, and should never be seen here.
984 case elfcpp::R_386_COPY:
985 case elfcpp::R_386_GLOB_DAT:
986 case elfcpp::R_386_JUMP_SLOT:
987 case elfcpp::R_386_RELATIVE:
988 case elfcpp::R_386_TLS_TPOFF:
989 case elfcpp::R_386_TLS_DTPMOD32:
990 case elfcpp::R_386_TLS_DTPOFF32:
991 case elfcpp::R_386_TLS_TPOFF32:
992 case elfcpp::R_386_TLS_DESC:
993 gold_error(_("%s: unexpected reloc %u in object file"),
994 object->name().c_str(), r_type);
997 // These are initial TLS relocs, which are expected when
999 case elfcpp::R_386_TLS_GD: // Global-dynamic
1000 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1001 case elfcpp::R_386_TLS_DESC_CALL:
1002 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1003 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1004 case elfcpp::R_386_TLS_IE: // Initial-exec
1005 case elfcpp::R_386_TLS_IE_32:
1006 case elfcpp::R_386_TLS_GOTIE:
1007 case elfcpp::R_386_TLS_LE: // Local-exec
1008 case elfcpp::R_386_TLS_LE_32:
1010 bool output_is_shared = parameters->options().shared();
1011 const tls::Tls_optimization optimized_type
1012 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1015 case elfcpp::R_386_TLS_GD: // Global-dynamic
1016 if (optimized_type == tls::TLSOPT_NONE)
1018 // Create a pair of GOT entries for the module index and
1019 // dtv-relative offset.
1020 Output_data_got<32, false>* got
1021 = target->got_section(symtab, layout);
1022 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1023 got->add_local_pair_with_rel(object, r_sym,
1024 lsym.get_st_shndx(),
1026 target->rel_dyn_section(layout),
1027 elfcpp::R_386_TLS_DTPMOD32, 0);
1029 else if (optimized_type != tls::TLSOPT_TO_LE)
1030 unsupported_reloc_local(object, r_type);
1033 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1034 if (optimized_type == tls::TLSOPT_NONE)
1036 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1037 Output_data_got<32, false>* got
1038 = target->got_section(symtab, layout);
1039 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1040 got->add_local_pair_with_rel(object, r_sym,
1041 lsym.get_st_shndx(),
1043 target->rel_dyn_section(layout),
1044 elfcpp::R_386_TLS_DESC, 0);
1046 else if (optimized_type != tls::TLSOPT_TO_LE)
1047 unsupported_reloc_local(object, r_type);
1050 case elfcpp::R_386_TLS_DESC_CALL:
1053 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1054 if (optimized_type == tls::TLSOPT_NONE)
1056 // Create a GOT entry for the module index.
1057 target->got_mod_index_entry(symtab, layout, object);
1059 else if (optimized_type != tls::TLSOPT_TO_LE)
1060 unsupported_reloc_local(object, r_type);
1063 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1066 case elfcpp::R_386_TLS_IE: // Initial-exec
1067 case elfcpp::R_386_TLS_IE_32:
1068 case elfcpp::R_386_TLS_GOTIE:
1069 layout->set_has_static_tls();
1070 if (optimized_type == tls::TLSOPT_NONE)
1072 // For the R_386_TLS_IE relocation, we need to create a
1073 // dynamic relocation when building a shared library.
1074 if (r_type == elfcpp::R_386_TLS_IE
1075 && parameters->options().shared())
1077 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1079 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1080 rel_dyn->add_local_relative(object, r_sym,
1081 elfcpp::R_386_RELATIVE,
1082 output_section, data_shndx,
1083 reloc.get_r_offset());
1085 // Create a GOT entry for the tp-relative offset.
1086 Output_data_got<32, false>* got
1087 = target->got_section(symtab, layout);
1088 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1089 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1090 ? elfcpp::R_386_TLS_TPOFF32
1091 : elfcpp::R_386_TLS_TPOFF);
1092 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1093 ? GOT_TYPE_TLS_OFFSET
1094 : GOT_TYPE_TLS_NOFFSET);
1095 got->add_local_with_rel(object, r_sym, got_type,
1096 target->rel_dyn_section(layout),
1099 else if (optimized_type != tls::TLSOPT_TO_LE)
1100 unsupported_reloc_local(object, r_type);
1103 case elfcpp::R_386_TLS_LE: // Local-exec
1104 case elfcpp::R_386_TLS_LE_32:
1105 layout->set_has_static_tls();
1106 if (output_is_shared)
1108 // We need to create a dynamic relocation.
1109 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1110 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1111 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1112 ? elfcpp::R_386_TLS_TPOFF32
1113 : elfcpp::R_386_TLS_TPOFF);
1114 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1115 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1116 data_shndx, reloc.get_r_offset());
1126 case elfcpp::R_386_32PLT:
1127 case elfcpp::R_386_TLS_GD_32:
1128 case elfcpp::R_386_TLS_GD_PUSH:
1129 case elfcpp::R_386_TLS_GD_CALL:
1130 case elfcpp::R_386_TLS_GD_POP:
1131 case elfcpp::R_386_TLS_LDM_32:
1132 case elfcpp::R_386_TLS_LDM_PUSH:
1133 case elfcpp::R_386_TLS_LDM_CALL:
1134 case elfcpp::R_386_TLS_LDM_POP:
1135 case elfcpp::R_386_USED_BY_INTEL_200:
1137 unsupported_reloc_local(object, r_type);
1142 // Report an unsupported relocation against a global symbol.
1145 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1146 unsigned int r_type,
1149 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1150 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1153 // Scan a relocation for a global symbol.
1156 Target_i386::Scan::global(const General_options& options,
1157 Symbol_table* symtab,
1159 Target_i386* target,
1160 Sized_relobj<32, false>* object,
1161 unsigned int data_shndx,
1162 Output_section* output_section,
1163 const elfcpp::Rel<32, false>& reloc,
1164 unsigned int r_type,
1169 case elfcpp::R_386_NONE:
1170 case elfcpp::R_386_GNU_VTINHERIT:
1171 case elfcpp::R_386_GNU_VTENTRY:
1174 case elfcpp::R_386_32:
1175 case elfcpp::R_386_16:
1176 case elfcpp::R_386_8:
1178 // Make a PLT entry if necessary.
1179 if (gsym->needs_plt_entry())
1181 target->make_plt_entry(symtab, layout, gsym);
1182 // Since this is not a PC-relative relocation, we may be
1183 // taking the address of a function. In that case we need to
1184 // set the entry in the dynamic symbol table to the address of
1186 if (gsym->is_from_dynobj() && !parameters->options().shared())
1187 gsym->set_needs_dynsym_value();
1189 // Make a dynamic relocation if necessary.
1190 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1192 if (target->may_need_copy_reloc(gsym))
1194 target->copy_reloc(&options, symtab, layout, object,
1195 data_shndx, output_section, gsym, reloc);
1197 else if (r_type == elfcpp::R_386_32
1198 && gsym->can_use_relative_reloc(false))
1200 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1201 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1202 output_section, object,
1203 data_shndx, reloc.get_r_offset());
1207 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1208 rel_dyn->add_global(gsym, r_type, output_section, object,
1209 data_shndx, reloc.get_r_offset());
1215 case elfcpp::R_386_PC32:
1216 case elfcpp::R_386_PC16:
1217 case elfcpp::R_386_PC8:
1219 // Make a PLT entry if necessary.
1220 if (gsym->needs_plt_entry())
1222 // These relocations are used for function calls only in
1223 // non-PIC code. For a 32-bit relocation in a shared library,
1224 // we'll need a text relocation anyway, so we can skip the
1225 // PLT entry and let the dynamic linker bind the call directly
1226 // to the target. For smaller relocations, we should use a
1227 // PLT entry to ensure that the call can reach.
1228 if (!parameters->options().shared()
1229 || r_type != elfcpp::R_386_PC32)
1230 target->make_plt_entry(symtab, layout, gsym);
1232 // Make a dynamic relocation if necessary.
1233 int flags = Symbol::NON_PIC_REF;
1234 if (gsym->type() == elfcpp::STT_FUNC)
1235 flags |= Symbol::FUNCTION_CALL;
1236 if (gsym->needs_dynamic_reloc(flags))
1238 if (target->may_need_copy_reloc(gsym))
1240 target->copy_reloc(&options, symtab, layout, object,
1241 data_shndx, output_section, gsym, reloc);
1245 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1246 rel_dyn->add_global(gsym, r_type, output_section, object,
1247 data_shndx, reloc.get_r_offset());
1253 case elfcpp::R_386_GOT32:
1255 // The symbol requires a GOT entry.
1256 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1257 if (gsym->final_value_is_known())
1258 got->add_global(gsym, GOT_TYPE_STANDARD);
1261 // If this symbol is not fully resolved, we need to add a
1262 // GOT entry with a dynamic relocation.
1263 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1264 if (gsym->is_from_dynobj()
1265 || gsym->is_undefined()
1266 || gsym->is_preemptible())
1267 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1268 rel_dyn, elfcpp::R_386_GLOB_DAT);
1271 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1272 rel_dyn->add_global_relative(
1273 gsym, elfcpp::R_386_RELATIVE, got,
1274 gsym->got_offset(GOT_TYPE_STANDARD));
1280 case elfcpp::R_386_PLT32:
1281 // If the symbol is fully resolved, this is just a PC32 reloc.
1282 // Otherwise we need a PLT entry.
1283 if (gsym->final_value_is_known())
1285 // If building a shared library, we can also skip the PLT entry
1286 // if the symbol is defined in the output file and is protected
1288 if (gsym->is_defined()
1289 && !gsym->is_from_dynobj()
1290 && !gsym->is_preemptible())
1292 target->make_plt_entry(symtab, layout, gsym);
1295 case elfcpp::R_386_GOTOFF:
1296 case elfcpp::R_386_GOTPC:
1297 // We need a GOT section.
1298 target->got_section(symtab, layout);
1301 // These are relocations which should only be seen by the
1302 // dynamic linker, and should never be seen here.
1303 case elfcpp::R_386_COPY:
1304 case elfcpp::R_386_GLOB_DAT:
1305 case elfcpp::R_386_JUMP_SLOT:
1306 case elfcpp::R_386_RELATIVE:
1307 case elfcpp::R_386_TLS_TPOFF:
1308 case elfcpp::R_386_TLS_DTPMOD32:
1309 case elfcpp::R_386_TLS_DTPOFF32:
1310 case elfcpp::R_386_TLS_TPOFF32:
1311 case elfcpp::R_386_TLS_DESC:
1312 gold_error(_("%s: unexpected reloc %u in object file"),
1313 object->name().c_str(), r_type);
1316 // These are initial tls relocs, which are expected when
1318 case elfcpp::R_386_TLS_GD: // Global-dynamic
1319 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1320 case elfcpp::R_386_TLS_DESC_CALL:
1321 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1322 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1323 case elfcpp::R_386_TLS_IE: // Initial-exec
1324 case elfcpp::R_386_TLS_IE_32:
1325 case elfcpp::R_386_TLS_GOTIE:
1326 case elfcpp::R_386_TLS_LE: // Local-exec
1327 case elfcpp::R_386_TLS_LE_32:
1329 const bool is_final = gsym->final_value_is_known();
1330 const tls::Tls_optimization optimized_type
1331 = Target_i386::optimize_tls_reloc(is_final, r_type);
1334 case elfcpp::R_386_TLS_GD: // Global-dynamic
1335 if (optimized_type == tls::TLSOPT_NONE)
1337 // Create a pair of GOT entries for the module index and
1338 // dtv-relative offset.
1339 Output_data_got<32, false>* got
1340 = target->got_section(symtab, layout);
1341 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1342 target->rel_dyn_section(layout),
1343 elfcpp::R_386_TLS_DTPMOD32,
1344 elfcpp::R_386_TLS_DTPOFF32);
1346 else if (optimized_type == tls::TLSOPT_TO_IE)
1348 // Create a GOT entry for the tp-relative offset.
1349 Output_data_got<32, false>* got
1350 = target->got_section(symtab, layout);
1351 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1352 target->rel_dyn_section(layout),
1353 elfcpp::R_386_TLS_TPOFF);
1355 else if (optimized_type != tls::TLSOPT_TO_LE)
1356 unsupported_reloc_global(object, r_type, gsym);
1359 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1360 if (optimized_type == tls::TLSOPT_NONE)
1362 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1363 Output_data_got<32, false>* got
1364 = target->got_section(symtab, layout);
1365 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1366 target->rel_dyn_section(layout),
1367 elfcpp::R_386_TLS_DESC, 0);
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_DESC_CALL:
1385 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1386 if (optimized_type == tls::TLSOPT_NONE)
1388 // Create a GOT entry for the module index.
1389 target->got_mod_index_entry(symtab, layout, object);
1391 else if (optimized_type != tls::TLSOPT_TO_LE)
1392 unsupported_reloc_global(object, r_type, gsym);
1395 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1398 case elfcpp::R_386_TLS_IE: // Initial-exec
1399 case elfcpp::R_386_TLS_IE_32:
1400 case elfcpp::R_386_TLS_GOTIE:
1401 layout->set_has_static_tls();
1402 if (optimized_type == tls::TLSOPT_NONE)
1404 // For the R_386_TLS_IE relocation, we need to create a
1405 // dynamic relocation when building a shared library.
1406 if (r_type == elfcpp::R_386_TLS_IE
1407 && parameters->options().shared())
1409 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1410 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1411 output_section, object,
1413 reloc.get_r_offset());
1415 // Create a GOT entry for the tp-relative offset.
1416 Output_data_got<32, false>* got
1417 = target->got_section(symtab, layout);
1418 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1419 ? elfcpp::R_386_TLS_TPOFF32
1420 : elfcpp::R_386_TLS_TPOFF);
1421 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1422 ? GOT_TYPE_TLS_OFFSET
1423 : GOT_TYPE_TLS_NOFFSET);
1424 got->add_global_with_rel(gsym, got_type,
1425 target->rel_dyn_section(layout),
1428 else if (optimized_type != tls::TLSOPT_TO_LE)
1429 unsupported_reloc_global(object, r_type, gsym);
1432 case elfcpp::R_386_TLS_LE: // Local-exec
1433 case elfcpp::R_386_TLS_LE_32:
1434 layout->set_has_static_tls();
1435 if (parameters->options().shared())
1437 // We need to create a dynamic relocation.
1438 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1439 ? elfcpp::R_386_TLS_TPOFF32
1440 : elfcpp::R_386_TLS_TPOFF);
1441 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1442 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1443 data_shndx, reloc.get_r_offset());
1453 case elfcpp::R_386_32PLT:
1454 case elfcpp::R_386_TLS_GD_32:
1455 case elfcpp::R_386_TLS_GD_PUSH:
1456 case elfcpp::R_386_TLS_GD_CALL:
1457 case elfcpp::R_386_TLS_GD_POP:
1458 case elfcpp::R_386_TLS_LDM_32:
1459 case elfcpp::R_386_TLS_LDM_PUSH:
1460 case elfcpp::R_386_TLS_LDM_CALL:
1461 case elfcpp::R_386_TLS_LDM_POP:
1462 case elfcpp::R_386_USED_BY_INTEL_200:
1464 unsupported_reloc_global(object, r_type, gsym);
1469 // Scan relocations for a section.
1472 Target_i386::scan_relocs(const General_options& options,
1473 Symbol_table* symtab,
1475 Sized_relobj<32, false>* object,
1476 unsigned int data_shndx,
1477 unsigned int sh_type,
1478 const unsigned char* prelocs,
1480 Output_section* output_section,
1481 bool needs_special_offset_handling,
1482 size_t local_symbol_count,
1483 const unsigned char* plocal_symbols)
1485 if (sh_type == elfcpp::SHT_RELA)
1487 gold_error(_("%s: unsupported RELA reloc section"),
1488 object->name().c_str());
1492 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1503 needs_special_offset_handling,
1508 // Finalize the sections.
1511 Target_i386::do_finalize_sections(Layout* layout)
1513 // Fill in some more dynamic tags.
1514 Output_data_dynamic* const odyn = layout->dynamic_data();
1517 if (this->got_plt_ != NULL)
1518 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1520 if (this->plt_ != NULL)
1522 const Output_data* od = this->plt_->rel_plt();
1523 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1524 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1525 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1528 if (this->rel_dyn_ != NULL)
1530 const Output_data* od = this->rel_dyn_;
1531 odyn->add_section_address(elfcpp::DT_REL, od);
1532 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1533 odyn->add_constant(elfcpp::DT_RELENT,
1534 elfcpp::Elf_sizes<32>::rel_size);
1537 if (!parameters->options().shared())
1539 // The value of the DT_DEBUG tag is filled in by the dynamic
1540 // linker at run time, and used by the debugger.
1541 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1545 // Emit any relocs we saved in an attempt to avoid generating COPY
1547 if (this->copy_relocs_ == NULL)
1549 if (this->copy_relocs_->any_to_emit())
1551 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1552 this->copy_relocs_->emit(rel_dyn);
1554 delete this->copy_relocs_;
1555 this->copy_relocs_ = NULL;
1558 // Return whether a direct absolute static relocation needs to be applied.
1559 // In cases where Scan::local() or Scan::global() has created
1560 // a dynamic relocation other than R_386_RELATIVE, the addend
1561 // of the relocation is carried in the data, and we must not
1562 // apply the static relocation.
1565 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1569 // For local symbols, we will have created a non-RELATIVE dynamic
1570 // relocation only if (a) the output is position independent,
1571 // (b) the relocation is absolute (not pc- or segment-relative), and
1572 // (c) the relocation is not 32 bits wide.
1574 return !(parameters->options().output_is_position_independent()
1575 && (ref_flags & Symbol::ABSOLUTE_REF)
1578 // For global symbols, we use the same helper routines used in the
1579 // scan pass. If we did not create a dynamic relocation, or if we
1580 // created a RELATIVE dynamic relocation, we should apply the static
1582 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1583 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1584 && gsym->can_use_relative_reloc(ref_flags
1585 & Symbol::FUNCTION_CALL);
1586 return !has_dyn || is_rel;
1589 // Perform a relocation.
1592 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1593 Target_i386* target,
1595 const elfcpp::Rel<32, false>& rel,
1596 unsigned int r_type,
1597 const Sized_symbol<32>* gsym,
1598 const Symbol_value<32>* psymval,
1599 unsigned char* view,
1600 elfcpp::Elf_types<32>::Elf_Addr address,
1601 section_size_type view_size)
1603 if (this->skip_call_tls_get_addr_)
1605 if (r_type != elfcpp::R_386_PLT32
1607 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1608 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1609 _("missing expected TLS relocation"));
1612 this->skip_call_tls_get_addr_ = false;
1617 // Pick the value to use for symbols defined in shared objects.
1618 Symbol_value<32> symval;
1619 bool is_nonpic = (r_type == elfcpp::R_386_PC8
1620 || r_type == elfcpp::R_386_PC16
1621 || r_type == elfcpp::R_386_PC32);
1623 && (gsym->is_from_dynobj()
1624 || (parameters->options().shared()
1625 && (gsym->is_undefined() || gsym->is_preemptible())))
1626 && gsym->has_plt_offset()
1627 && (!is_nonpic || !parameters->options().shared()))
1629 symval.set_output_value(target->plt_section()->address()
1630 + gsym->plt_offset());
1634 const Sized_relobj<32, false>* object = relinfo->object;
1636 // Get the GOT offset if needed.
1637 // The GOT pointer points to the end of the GOT section.
1638 // We need to subtract the size of the GOT section to get
1639 // the actual offset to use in the relocation.
1640 bool have_got_offset = false;
1641 unsigned int got_offset = 0;
1644 case elfcpp::R_386_GOT32:
1647 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1648 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1649 - target->got_size());
1653 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1654 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1655 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1656 - target->got_size());
1658 have_got_offset = true;
1667 case elfcpp::R_386_NONE:
1668 case elfcpp::R_386_GNU_VTINHERIT:
1669 case elfcpp::R_386_GNU_VTENTRY:
1672 case elfcpp::R_386_32:
1673 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1674 Relocate_functions<32, false>::rel32(view, object, psymval);
1677 case elfcpp::R_386_PC32:
1679 int ref_flags = Symbol::NON_PIC_REF;
1680 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1681 ref_flags |= Symbol::FUNCTION_CALL;
1682 if (should_apply_static_reloc(gsym, ref_flags, true))
1683 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1687 case elfcpp::R_386_16:
1688 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1689 Relocate_functions<32, false>::rel16(view, object, psymval);
1692 case elfcpp::R_386_PC16:
1694 int ref_flags = Symbol::NON_PIC_REF;
1695 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1696 ref_flags |= Symbol::FUNCTION_CALL;
1697 if (should_apply_static_reloc(gsym, ref_flags, false))
1698 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1702 case elfcpp::R_386_8:
1703 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1704 Relocate_functions<32, false>::rel8(view, object, psymval);
1707 case elfcpp::R_386_PC8:
1709 int ref_flags = Symbol::NON_PIC_REF;
1710 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1711 ref_flags |= Symbol::FUNCTION_CALL;
1712 if (should_apply_static_reloc(gsym, ref_flags, false))
1713 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1717 case elfcpp::R_386_PLT32:
1718 gold_assert(gsym == NULL
1719 || gsym->has_plt_offset()
1720 || gsym->final_value_is_known()
1721 || (gsym->is_defined()
1722 && !gsym->is_from_dynobj()
1723 && !gsym->is_preemptible()));
1724 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1727 case elfcpp::R_386_GOT32:
1728 gold_assert(have_got_offset);
1729 Relocate_functions<32, false>::rel32(view, got_offset);
1732 case elfcpp::R_386_GOTOFF:
1734 elfcpp::Elf_types<32>::Elf_Addr value;
1735 value = (psymval->value(object, 0)
1736 - target->got_plt_section()->address());
1737 Relocate_functions<32, false>::rel32(view, value);
1741 case elfcpp::R_386_GOTPC:
1743 elfcpp::Elf_types<32>::Elf_Addr value;
1744 value = target->got_plt_section()->address();
1745 Relocate_functions<32, false>::pcrel32(view, value, address);
1749 case elfcpp::R_386_COPY:
1750 case elfcpp::R_386_GLOB_DAT:
1751 case elfcpp::R_386_JUMP_SLOT:
1752 case elfcpp::R_386_RELATIVE:
1753 // These are outstanding tls relocs, which are unexpected when
1755 case elfcpp::R_386_TLS_TPOFF:
1756 case elfcpp::R_386_TLS_DTPMOD32:
1757 case elfcpp::R_386_TLS_DTPOFF32:
1758 case elfcpp::R_386_TLS_TPOFF32:
1759 case elfcpp::R_386_TLS_DESC:
1760 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1761 _("unexpected reloc %u in object file"),
1765 // These are initial tls relocs, which are expected when
1767 case elfcpp::R_386_TLS_GD: // Global-dynamic
1768 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1769 case elfcpp::R_386_TLS_DESC_CALL:
1770 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1771 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1772 case elfcpp::R_386_TLS_IE: // Initial-exec
1773 case elfcpp::R_386_TLS_IE_32:
1774 case elfcpp::R_386_TLS_GOTIE:
1775 case elfcpp::R_386_TLS_LE: // Local-exec
1776 case elfcpp::R_386_TLS_LE_32:
1777 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1778 view, address, view_size);
1781 case elfcpp::R_386_32PLT:
1782 case elfcpp::R_386_TLS_GD_32:
1783 case elfcpp::R_386_TLS_GD_PUSH:
1784 case elfcpp::R_386_TLS_GD_CALL:
1785 case elfcpp::R_386_TLS_GD_POP:
1786 case elfcpp::R_386_TLS_LDM_32:
1787 case elfcpp::R_386_TLS_LDM_PUSH:
1788 case elfcpp::R_386_TLS_LDM_CALL:
1789 case elfcpp::R_386_TLS_LDM_POP:
1790 case elfcpp::R_386_USED_BY_INTEL_200:
1792 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1793 _("unsupported reloc %u"),
1801 // Perform a TLS relocation.
1804 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1805 Target_i386* target,
1807 const elfcpp::Rel<32, false>& rel,
1808 unsigned int r_type,
1809 const Sized_symbol<32>* gsym,
1810 const Symbol_value<32>* psymval,
1811 unsigned char* view,
1812 elfcpp::Elf_types<32>::Elf_Addr,
1813 section_size_type view_size)
1815 Output_segment* tls_segment = relinfo->layout->tls_segment();
1817 const Sized_relobj<32, false>* object = relinfo->object;
1819 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1821 const bool is_final =
1823 ? !parameters->options().output_is_position_independent()
1824 : gsym->final_value_is_known());
1825 const tls::Tls_optimization optimized_type
1826 = Target_i386::optimize_tls_reloc(is_final, r_type);
1829 case elfcpp::R_386_TLS_GD: // Global-dynamic
1830 if (optimized_type == tls::TLSOPT_TO_LE)
1832 gold_assert(tls_segment != NULL);
1833 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1834 rel, r_type, value, view,
1840 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1841 ? GOT_TYPE_TLS_NOFFSET
1842 : GOT_TYPE_TLS_PAIR);
1843 unsigned int got_offset;
1846 gold_assert(gsym->has_got_offset(got_type));
1847 got_offset = gsym->got_offset(got_type) - target->got_size();
1851 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1852 gold_assert(object->local_has_got_offset(r_sym, got_type));
1853 got_offset = (object->local_got_offset(r_sym, got_type)
1854 - target->got_size());
1856 if (optimized_type == tls::TLSOPT_TO_IE)
1858 gold_assert(tls_segment != NULL);
1859 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1860 got_offset, view, view_size);
1863 else if (optimized_type == tls::TLSOPT_NONE)
1865 // Relocate the field with the offset of the pair of GOT
1867 Relocate_functions<32, false>::rel32(view, got_offset);
1871 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1872 _("unsupported reloc %u"),
1876 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1877 case elfcpp::R_386_TLS_DESC_CALL:
1878 if (optimized_type == tls::TLSOPT_TO_LE)
1880 gold_assert(tls_segment != NULL);
1881 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1882 rel, r_type, value, view,
1888 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1889 ? GOT_TYPE_TLS_NOFFSET
1890 : GOT_TYPE_TLS_DESC);
1891 unsigned int got_offset;
1894 gold_assert(gsym->has_got_offset(got_type));
1895 got_offset = gsym->got_offset(got_type) - target->got_size();
1899 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1900 gold_assert(object->local_has_got_offset(r_sym, got_type));
1901 got_offset = (object->local_got_offset(r_sym, got_type)
1902 - target->got_size());
1904 if (optimized_type == tls::TLSOPT_TO_IE)
1906 gold_assert(tls_segment != NULL);
1907 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1908 got_offset, view, view_size);
1911 else if (optimized_type == tls::TLSOPT_NONE)
1913 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1915 // Relocate the field with the offset of the pair of GOT
1917 Relocate_functions<32, false>::rel32(view, got_offset);
1922 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1923 _("unsupported reloc %u"),
1927 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1928 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1930 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1931 _("both SUN and GNU model "
1932 "TLS relocations"));
1935 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1936 if (optimized_type == tls::TLSOPT_TO_LE)
1938 gold_assert(tls_segment != NULL);
1939 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1940 value, view, view_size);
1943 else if (optimized_type == tls::TLSOPT_NONE)
1945 // Relocate the field with the offset of the GOT entry for
1946 // the module index.
1947 unsigned int got_offset;
1948 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1949 - target->got_size());
1950 Relocate_functions<32, false>::rel32(view, got_offset);
1953 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1954 _("unsupported reloc %u"),
1958 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1959 // This reloc can appear in debugging sections, in which case we
1960 // won't see the TLS_LDM reloc. The local_dynamic_type field
1962 if (optimized_type == tls::TLSOPT_TO_LE)
1964 gold_assert(tls_segment != NULL);
1965 value -= tls_segment->memsz();
1967 Relocate_functions<32, false>::rel32(view, value);
1970 case elfcpp::R_386_TLS_IE: // Initial-exec
1971 case elfcpp::R_386_TLS_GOTIE:
1972 case elfcpp::R_386_TLS_IE_32:
1973 if (optimized_type == tls::TLSOPT_TO_LE)
1975 gold_assert(tls_segment != NULL);
1976 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1977 rel, r_type, value, view,
1981 else if (optimized_type == tls::TLSOPT_NONE)
1983 // Relocate the field with the offset of the GOT entry for
1984 // the tp-relative offset of the symbol.
1985 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1986 ? GOT_TYPE_TLS_OFFSET
1987 : GOT_TYPE_TLS_NOFFSET);
1988 unsigned int got_offset;
1991 gold_assert(gsym->has_got_offset(got_type));
1992 got_offset = gsym->got_offset(got_type);
1996 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1997 gold_assert(object->local_has_got_offset(r_sym, got_type));
1998 got_offset = object->local_got_offset(r_sym, got_type);
2000 // For the R_386_TLS_IE relocation, we need to apply the
2001 // absolute address of the GOT entry.
2002 if (r_type == elfcpp::R_386_TLS_IE)
2003 got_offset += target->got_plt_section()->address();
2004 // All GOT offsets are relative to the end of the GOT.
2005 got_offset -= target->got_size();
2006 Relocate_functions<32, false>::rel32(view, got_offset);
2009 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2010 _("unsupported reloc %u"),
2014 case elfcpp::R_386_TLS_LE: // Local-exec
2015 // If we're creating a shared library, a dynamic relocation will
2016 // have been created for this location, so do not apply it now.
2017 if (!parameters->options().shared())
2019 gold_assert(tls_segment != NULL);
2020 value -= tls_segment->memsz();
2021 Relocate_functions<32, false>::rel32(view, value);
2025 case elfcpp::R_386_TLS_LE_32:
2026 // If we're creating a shared library, a dynamic relocation will
2027 // have been created for this location, so do not apply it now.
2028 if (!parameters->options().shared())
2030 gold_assert(tls_segment != NULL);
2031 value = tls_segment->memsz() - value;
2032 Relocate_functions<32, false>::rel32(view, value);
2038 // Do a relocation in which we convert a TLS General-Dynamic to a
2042 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2044 Output_segment* tls_segment,
2045 const elfcpp::Rel<32, false>& rel,
2047 elfcpp::Elf_types<32>::Elf_Addr value,
2048 unsigned char* view,
2049 section_size_type view_size)
2051 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2052 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2053 // leal foo(%reg),%eax; call ___tls_get_addr
2054 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2056 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2057 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2059 unsigned char op1 = view[-1];
2060 unsigned char op2 = view[-2];
2062 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2063 op2 == 0x8d || op2 == 0x04);
2064 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2070 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2071 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2072 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2073 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2074 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2078 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2079 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2080 if (rel.get_r_offset() + 9 < view_size
2083 // There is a trailing nop. Use the size byte subl.
2084 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2089 // Use the five byte subl.
2090 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2094 value = tls_segment->memsz() - value;
2095 Relocate_functions<32, false>::rel32(view + roff, value);
2097 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2099 this->skip_call_tls_get_addr_ = true;
2102 // Do a relocation in which we convert a TLS General-Dynamic to an
2106 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2109 const elfcpp::Rel<32, false>& rel,
2111 elfcpp::Elf_types<32>::Elf_Addr value,
2112 unsigned char* view,
2113 section_size_type view_size)
2115 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2116 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2118 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2119 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2121 unsigned char op1 = view[-1];
2122 unsigned char op2 = view[-2];
2124 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2125 op2 == 0x8d || op2 == 0x04);
2126 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2130 // FIXME: For now, support only the first (SIB) form.
2131 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2135 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2136 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2137 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2138 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2139 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2143 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2144 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2145 if (rel.get_r_offset() + 9 < view_size
2148 // FIXME: This is not the right instruction sequence.
2149 // There is a trailing nop. Use the size byte subl.
2150 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2155 // FIXME: This is not the right instruction sequence.
2156 // Use the five byte subl.
2157 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2161 Relocate_functions<32, false>::rel32(view + roff, value);
2163 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2165 this->skip_call_tls_get_addr_ = true;
2168 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2169 // General-Dynamic to a Local-Exec.
2172 Target_i386::Relocate::tls_desc_gd_to_le(
2173 const Relocate_info<32, false>* relinfo,
2175 Output_segment* tls_segment,
2176 const elfcpp::Rel<32, false>& rel,
2177 unsigned int r_type,
2178 elfcpp::Elf_types<32>::Elf_Addr value,
2179 unsigned char* view,
2180 section_size_type view_size)
2182 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2184 // leal foo@TLSDESC(%ebx), %eax
2185 // ==> leal foo@NTPOFF, %eax
2186 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2187 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2188 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2189 view[-2] == 0x8d && view[-1] == 0x83);
2191 value -= tls_segment->memsz();
2192 Relocate_functions<32, false>::rel32(view, value);
2196 // call *foo@TLSCALL(%eax)
2198 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2199 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2200 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2201 view[0] == 0xff && view[1] == 0x10);
2207 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2208 // General-Dynamic to an Initial-Exec.
2211 Target_i386::Relocate::tls_desc_gd_to_ie(
2212 const Relocate_info<32, false>* relinfo,
2215 const elfcpp::Rel<32, false>& rel,
2216 unsigned int r_type,
2217 elfcpp::Elf_types<32>::Elf_Addr value,
2218 unsigned char* view,
2219 section_size_type view_size)
2221 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2223 // leal foo@TLSDESC(%ebx), %eax
2224 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2225 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2226 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2227 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2228 view[-2] == 0x8d && view[-1] == 0x83);
2230 Relocate_functions<32, false>::rel32(view, value);
2234 // call *foo@TLSCALL(%eax)
2236 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2237 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2238 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2239 view[0] == 0xff && view[1] == 0x10);
2245 // Do a relocation in which we convert a TLS Local-Dynamic to a
2249 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2252 const elfcpp::Rel<32, false>& rel,
2254 elfcpp::Elf_types<32>::Elf_Addr,
2255 unsigned char* view,
2256 section_size_type view_size)
2258 // leal foo(%reg), %eax; call ___tls_get_addr
2259 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2261 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2262 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2264 // FIXME: Does this test really always pass?
2265 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2266 view[-2] == 0x8d && view[-1] == 0x83);
2268 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2270 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2272 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2274 this->skip_call_tls_get_addr_ = true;
2277 // Do a relocation in which we convert a TLS Initial-Exec to a
2281 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2283 Output_segment* tls_segment,
2284 const elfcpp::Rel<32, false>& rel,
2285 unsigned int r_type,
2286 elfcpp::Elf_types<32>::Elf_Addr value,
2287 unsigned char* view,
2288 section_size_type view_size)
2290 // We have to actually change the instructions, which means that we
2291 // need to examine the opcodes to figure out which instruction we
2293 if (r_type == elfcpp::R_386_TLS_IE)
2295 // movl %gs:XX,%eax ==> movl $YY,%eax
2296 // movl %gs:XX,%reg ==> movl $YY,%reg
2297 // addl %gs:XX,%reg ==> addl $YY,%reg
2298 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2299 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2301 unsigned char op1 = view[-1];
2304 // movl XX,%eax ==> movl $YY,%eax
2309 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2311 unsigned char op2 = view[-2];
2314 // movl XX,%reg ==> movl $YY,%reg
2315 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2316 (op1 & 0xc7) == 0x05);
2318 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2320 else if (op2 == 0x03)
2322 // addl XX,%reg ==> addl $YY,%reg
2323 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2324 (op1 & 0xc7) == 0x05);
2326 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2329 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2334 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2335 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2336 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2337 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2338 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2340 unsigned char op1 = view[-1];
2341 unsigned char op2 = view[-2];
2342 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2343 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2346 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2348 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2350 else if (op2 == 0x2b)
2352 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2354 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2356 else if (op2 == 0x03)
2358 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2360 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2363 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2366 value = tls_segment->memsz() - value;
2367 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2370 Relocate_functions<32, false>::rel32(view, value);
2373 // Relocate section data.
2376 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2377 unsigned int sh_type,
2378 const unsigned char* prelocs,
2380 Output_section* output_section,
2381 bool needs_special_offset_handling,
2382 unsigned char* view,
2383 elfcpp::Elf_types<32>::Elf_Addr address,
2384 section_size_type view_size)
2386 gold_assert(sh_type == elfcpp::SHT_REL);
2388 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2389 Target_i386::Relocate>(
2395 needs_special_offset_handling,
2401 // Return the size of a relocation while scanning during a relocatable
2405 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2406 unsigned int r_type,
2411 case elfcpp::R_386_NONE:
2412 case elfcpp::R_386_GNU_VTINHERIT:
2413 case elfcpp::R_386_GNU_VTENTRY:
2414 case elfcpp::R_386_TLS_GD: // Global-dynamic
2415 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2416 case elfcpp::R_386_TLS_DESC_CALL:
2417 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2418 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2419 case elfcpp::R_386_TLS_IE: // Initial-exec
2420 case elfcpp::R_386_TLS_IE_32:
2421 case elfcpp::R_386_TLS_GOTIE:
2422 case elfcpp::R_386_TLS_LE: // Local-exec
2423 case elfcpp::R_386_TLS_LE_32:
2426 case elfcpp::R_386_32:
2427 case elfcpp::R_386_PC32:
2428 case elfcpp::R_386_GOT32:
2429 case elfcpp::R_386_PLT32:
2430 case elfcpp::R_386_GOTOFF:
2431 case elfcpp::R_386_GOTPC:
2434 case elfcpp::R_386_16:
2435 case elfcpp::R_386_PC16:
2438 case elfcpp::R_386_8:
2439 case elfcpp::R_386_PC8:
2442 // These are relocations which should only be seen by the
2443 // dynamic linker, and should never be seen here.
2444 case elfcpp::R_386_COPY:
2445 case elfcpp::R_386_GLOB_DAT:
2446 case elfcpp::R_386_JUMP_SLOT:
2447 case elfcpp::R_386_RELATIVE:
2448 case elfcpp::R_386_TLS_TPOFF:
2449 case elfcpp::R_386_TLS_DTPMOD32:
2450 case elfcpp::R_386_TLS_DTPOFF32:
2451 case elfcpp::R_386_TLS_TPOFF32:
2452 case elfcpp::R_386_TLS_DESC:
2453 object->error(_("unexpected reloc %u in object file"), r_type);
2456 case elfcpp::R_386_32PLT:
2457 case elfcpp::R_386_TLS_GD_32:
2458 case elfcpp::R_386_TLS_GD_PUSH:
2459 case elfcpp::R_386_TLS_GD_CALL:
2460 case elfcpp::R_386_TLS_GD_POP:
2461 case elfcpp::R_386_TLS_LDM_32:
2462 case elfcpp::R_386_TLS_LDM_PUSH:
2463 case elfcpp::R_386_TLS_LDM_CALL:
2464 case elfcpp::R_386_TLS_LDM_POP:
2465 case elfcpp::R_386_USED_BY_INTEL_200:
2467 object->error(_("unsupported reloc %u in object file"), r_type);
2472 // Scan the relocs during a relocatable link.
2475 Target_i386::scan_relocatable_relocs(const General_options& options,
2476 Symbol_table* symtab,
2478 Sized_relobj<32, false>* object,
2479 unsigned int data_shndx,
2480 unsigned int sh_type,
2481 const unsigned char* prelocs,
2483 Output_section* output_section,
2484 bool needs_special_offset_handling,
2485 size_t local_symbol_count,
2486 const unsigned char* plocal_symbols,
2487 Relocatable_relocs* rr)
2489 gold_assert(sh_type == elfcpp::SHT_REL);
2491 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2492 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2494 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2495 Scan_relocatable_relocs>(
2504 needs_special_offset_handling,
2510 // Relocate a section during a relocatable link.
2513 Target_i386::relocate_for_relocatable(
2514 const Relocate_info<32, false>* relinfo,
2515 unsigned int sh_type,
2516 const unsigned char* prelocs,
2518 Output_section* output_section,
2519 off_t offset_in_output_section,
2520 const Relocatable_relocs* rr,
2521 unsigned char* view,
2522 elfcpp::Elf_types<32>::Elf_Addr view_address,
2523 section_size_type view_size,
2524 unsigned char* reloc_view,
2525 section_size_type reloc_view_size)
2527 gold_assert(sh_type == elfcpp::SHT_REL);
2529 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2534 offset_in_output_section,
2543 // Return the value to use for a dynamic which requires special
2544 // treatment. This is how we support equality comparisons of function
2545 // pointers across shared library boundaries, as described in the
2546 // processor specific ABI supplement.
2549 Target_i386::do_dynsym_value(const Symbol* gsym) const
2551 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2552 return this->plt_section()->address() + gsym->plt_offset();
2555 // Return a string used to fill a code section with nops to take up
2556 // the specified length.
2559 Target_i386::do_code_fill(section_size_type length) const
2563 // Build a jmp instruction to skip over the bytes.
2564 unsigned char jmp[5];
2566 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2567 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2568 + std::string(length - 5, '\0'));
2571 // Nop sequences of various lengths.
2572 const char nop1[1] = { 0x90 }; // nop
2573 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2574 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2575 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2576 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2577 0x00 }; // leal 0(%esi,1),%esi
2578 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2580 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2582 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2583 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2584 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2585 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2587 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2588 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2590 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2591 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2593 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2594 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2595 0x00, 0x00, 0x00, 0x00 };
2596 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2597 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2598 0x27, 0x00, 0x00, 0x00,
2600 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2601 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2602 0xbc, 0x27, 0x00, 0x00,
2604 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2605 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2606 0x90, 0x90, 0x90, 0x90,
2609 const char* nops[16] = {
2611 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2612 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2615 return std::string(nops[length], length);
2618 // The selector for i386 object files.
2620 class Target_selector_i386 : public Target_selector
2623 Target_selector_i386()
2624 : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
2628 do_instantiate_target()
2629 { return new Target_i386(); }
2632 Target_selector_i386 target_selector_i386;
2634 } // End anonymous namespace.