* config/tc-avr.c (mcu_types): Add atmega16u4.
[binutils.git] / gold / i386.cc
blob54f123333b71ded73a7ac7258874d240d6f83337
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.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "i386.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
41 namespace
44 using namespace gold;
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>
55 public:
56 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
58 Target_i386()
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)
63 { }
65 // Scan the relocations to look for symbol adjustments.
66 void
67 scan_relocs(const General_options& options,
68 Symbol_table* symtab,
69 Layout* layout,
70 Sized_relobj<32, false>* object,
71 unsigned int data_shndx,
72 unsigned int sh_type,
73 const unsigned char* prelocs,
74 size_t reloc_count,
75 Output_section* output_section,
76 bool needs_special_offset_handling,
77 size_t local_symbol_count,
78 const unsigned char* plocal_symbols);
80 // Finalize the sections.
81 void
82 do_finalize_sections(Layout*);
84 // Return the value to use for a dynamic which requires special
85 // treatment.
86 uint64_t
87 do_dynsym_value(const Symbol*) const;
89 // Relocate a section.
90 void
91 relocate_section(const Relocate_info<32, false>*,
92 unsigned int sh_type,
93 const unsigned char* prelocs,
94 size_t reloc_count,
95 Output_section* output_section,
96 bool needs_special_offset_handling,
97 unsigned char* view,
98 elfcpp::Elf_types<32>::Elf_Addr view_address,
99 section_size_type view_size);
101 // Scan the relocs during a relocatable link.
102 void
103 scan_relocatable_relocs(const General_options& options,
104 Symbol_table* symtab,
105 Layout* layout,
106 Sized_relobj<32, false>* object,
107 unsigned int data_shndx,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
110 size_t reloc_count,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
113 size_t local_symbol_count,
114 const unsigned char* plocal_symbols,
115 Relocatable_relocs*);
117 // Relocate a section during a relocatable link.
118 void
119 relocate_for_relocatable(const Relocate_info<32, false>*,
120 unsigned int sh_type,
121 const unsigned char* prelocs,
122 size_t reloc_count,
123 Output_section* output_section,
124 off_t offset_in_output_section,
125 const Relocatable_relocs*,
126 unsigned char* view,
127 elfcpp::Elf_types<32>::Elf_Addr view_address,
128 section_size_type view_size,
129 unsigned char* reloc_view,
130 section_size_type reloc_view_size);
132 // Return a string used to fill a code section with nops.
133 std::string
134 do_code_fill(section_size_type length) const;
136 // Return whether SYM is defined by the ABI.
137 bool
138 do_is_defined_by_abi(Symbol* sym) const
139 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
141 // Return the size of the GOT section.
142 section_size_type
143 got_size()
145 gold_assert(this->got_ != NULL);
146 return this->got_->data_size();
149 private:
150 // The class which scans relocations.
151 struct Scan
153 inline void
154 local(const General_options& options, Symbol_table* symtab,
155 Layout* layout, Target_i386* target,
156 Sized_relobj<32, false>* object,
157 unsigned int data_shndx,
158 Output_section* output_section,
159 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
160 const elfcpp::Sym<32, false>& lsym);
162 inline void
163 global(const General_options& options, Symbol_table* symtab,
164 Layout* layout, Target_i386* target,
165 Sized_relobj<32, false>* object,
166 unsigned int data_shndx,
167 Output_section* output_section,
168 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
169 Symbol* gsym);
171 static void
172 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
174 static void
175 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
176 Symbol*);
179 // The class which implements relocation.
180 class Relocate
182 public:
183 Relocate()
184 : skip_call_tls_get_addr_(false),
185 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
188 ~Relocate()
190 if (this->skip_call_tls_get_addr_)
192 // FIXME: This needs to specify the location somehow.
193 gold_error(_("missing expected TLS relocation"));
197 // Return whether the static relocation needs to be applied.
198 inline bool
199 should_apply_static_reloc(const Sized_symbol<32>* gsym,
200 int ref_flags,
201 bool is_32bit);
203 // Do a relocation. Return false if the caller should not issue
204 // any warnings about this relocation.
205 inline bool
206 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
207 const elfcpp::Rel<32, false>&,
208 unsigned int r_type, const Sized_symbol<32>*,
209 const Symbol_value<32>*,
210 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
211 section_size_type);
213 private:
214 // Do a TLS relocation.
215 inline void
216 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
217 size_t relnum, const elfcpp::Rel<32, false>&,
218 unsigned int r_type, const Sized_symbol<32>*,
219 const Symbol_value<32>*,
220 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
221 section_size_type);
223 // Do a TLS General-Dynamic to Initial-Exec transition.
224 inline void
225 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
226 Output_segment* tls_segment,
227 const elfcpp::Rel<32, false>&, unsigned int r_type,
228 elfcpp::Elf_types<32>::Elf_Addr value,
229 unsigned char* view,
230 section_size_type view_size);
232 // Do a TLS General-Dynamic to Local-Exec transition.
233 inline void
234 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
235 Output_segment* tls_segment,
236 const elfcpp::Rel<32, false>&, unsigned int r_type,
237 elfcpp::Elf_types<32>::Elf_Addr value,
238 unsigned char* view,
239 section_size_type view_size);
241 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
242 // transition.
243 inline void
244 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
245 Output_segment* tls_segment,
246 const elfcpp::Rel<32, false>&, unsigned int r_type,
247 elfcpp::Elf_types<32>::Elf_Addr value,
248 unsigned char* view,
249 section_size_type view_size);
251 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
252 // transition.
253 inline void
254 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
255 Output_segment* tls_segment,
256 const elfcpp::Rel<32, false>&, unsigned int r_type,
257 elfcpp::Elf_types<32>::Elf_Addr value,
258 unsigned char* view,
259 section_size_type view_size);
261 // Do a TLS Local-Dynamic to Local-Exec transition.
262 inline void
263 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
264 Output_segment* tls_segment,
265 const elfcpp::Rel<32, false>&, unsigned int r_type,
266 elfcpp::Elf_types<32>::Elf_Addr value,
267 unsigned char* view,
268 section_size_type view_size);
270 // Do a TLS Initial-Exec to Local-Exec transition.
271 static inline void
272 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
273 Output_segment* tls_segment,
274 const elfcpp::Rel<32, false>&, unsigned int r_type,
275 elfcpp::Elf_types<32>::Elf_Addr value,
276 unsigned char* view,
277 section_size_type view_size);
279 // We need to keep track of which type of local dynamic relocation
280 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
281 enum Local_dynamic_type
283 LOCAL_DYNAMIC_NONE,
284 LOCAL_DYNAMIC_SUN,
285 LOCAL_DYNAMIC_GNU
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_;
291 // The type of local dynamic relocation we have seen in the section
292 // being relocated, if any.
293 Local_dynamic_type local_dynamic_type_;
296 // A class which returns the size required for a relocation type,
297 // used while scanning relocs during a relocatable link.
298 class Relocatable_size_for_reloc
300 public:
301 unsigned int
302 get_size_for_reloc(unsigned int, Relobj*);
305 // Adjust TLS relocation type based on the options and whether this
306 // is a local symbol.
307 static tls::Tls_optimization
308 optimize_tls_reloc(bool is_final, int r_type);
310 // Get the GOT section, creating it if necessary.
311 Output_data_got<32, false>*
312 got_section(Symbol_table*, Layout*);
314 // Get the GOT PLT section.
315 Output_data_space*
316 got_plt_section() const
318 gold_assert(this->got_plt_ != NULL);
319 return this->got_plt_;
322 // Create a PLT entry for a global symbol.
323 void
324 make_plt_entry(Symbol_table*, Layout*, Symbol*);
326 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
327 void
328 define_tls_base_symbol(Symbol_table*, Layout*);
330 // Create a GOT entry for the TLS module index.
331 unsigned int
332 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
333 Sized_relobj<32, false>* object);
335 // Get the PLT section.
336 const Output_data_plt_i386*
337 plt_section() const
339 gold_assert(this->plt_ != NULL);
340 return this->plt_;
343 // Get the dynamic reloc section, creating it if necessary.
344 Reloc_section*
345 rel_dyn_section(Layout*);
347 // Return true if the symbol may need a COPY relocation.
348 // References from an executable object to non-function symbols
349 // defined in a dynamic object may need a COPY relocation.
350 bool
351 may_need_copy_reloc(Symbol* gsym)
353 return (!parameters->options().shared()
354 && gsym->is_from_dynobj()
355 && gsym->type() != elfcpp::STT_FUNC);
358 // Add a potential copy relocation.
359 void
360 copy_reloc(Symbol_table* symtab, Layout* layout,
361 Sized_relobj<32, false>* object,
362 unsigned int shndx, Output_section* output_section,
363 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
365 this->copy_relocs_.copy_reloc(symtab, layout,
366 symtab->get_sized_symbol<32>(sym),
367 object, shndx, output_section, reloc,
368 this->rel_dyn_section(layout));
371 // Information about this specific target which we pass to the
372 // general Target structure.
373 static const Target::Target_info i386_info;
375 // The types of GOT entries needed for this platform.
376 enum Got_type
378 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
379 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
380 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
381 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
382 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
385 // The GOT section.
386 Output_data_got<32, false>* got_;
387 // The PLT section.
388 Output_data_plt_i386* plt_;
389 // The GOT PLT section.
390 Output_data_space* got_plt_;
391 // The dynamic reloc section.
392 Reloc_section* rel_dyn_;
393 // Relocs saved to avoid a COPY reloc.
394 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
395 // Space for variables copied with a COPY reloc.
396 Output_data_space* dynbss_;
397 // Offset of the GOT entry for the TLS module index.
398 unsigned int got_mod_index_offset_;
399 // True if the _TLS_MODULE_BASE_ symbol has been defined.
400 bool tls_base_symbol_defined_;
403 const Target::Target_info Target_i386::i386_info =
405 32, // size
406 false, // is_big_endian
407 elfcpp::EM_386, // machine_code
408 false, // has_make_symbol
409 false, // has_resolve
410 true, // has_code_fill
411 true, // is_default_stack_executable
412 '\0', // wrap_char
413 "/usr/lib/libc.so.1", // dynamic_linker
414 0x08048000, // default_text_segment_address
415 0x1000, // abi_pagesize (overridable by -z max-page-size)
416 0x1000 // common_pagesize (overridable by -z common-page-size)
419 // Get the GOT section, creating it if necessary.
421 Output_data_got<32, false>*
422 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
424 if (this->got_ == NULL)
426 gold_assert(symtab != NULL && layout != NULL);
428 this->got_ = new Output_data_got<32, false>();
430 Output_section* os;
431 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
432 (elfcpp::SHF_ALLOC
433 | elfcpp::SHF_WRITE),
434 this->got_);
435 os->set_is_relro();
437 // The old GNU linker creates a .got.plt section. We just
438 // create another set of data in the .got section. Note that we
439 // always create a PLT if we create a GOT, although the PLT
440 // might be empty.
441 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
442 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
443 (elfcpp::SHF_ALLOC
444 | elfcpp::SHF_WRITE),
445 this->got_plt_);
446 os->set_is_relro();
448 // The first three entries are reserved.
449 this->got_plt_->set_current_data_size(3 * 4);
451 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
452 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
453 this->got_plt_,
454 0, 0, elfcpp::STT_OBJECT,
455 elfcpp::STB_LOCAL,
456 elfcpp::STV_HIDDEN, 0,
457 false, false);
460 return this->got_;
463 // Get the dynamic reloc section, creating it if necessary.
465 Target_i386::Reloc_section*
466 Target_i386::rel_dyn_section(Layout* layout)
468 if (this->rel_dyn_ == NULL)
470 gold_assert(layout != NULL);
471 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
472 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
473 elfcpp::SHF_ALLOC, this->rel_dyn_);
475 return this->rel_dyn_;
478 // A class to handle the PLT data.
480 class Output_data_plt_i386 : public Output_section_data
482 public:
483 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
485 Output_data_plt_i386(Layout*, Output_data_space*);
487 // Add an entry to the PLT.
488 void
489 add_entry(Symbol* gsym);
491 // Return the .rel.plt section data.
492 const Reloc_section*
493 rel_plt() const
494 { return this->rel_; }
496 protected:
497 void
498 do_adjust_output_section(Output_section* os);
500 // Write to a map file.
501 void
502 do_print_to_mapfile(Mapfile* mapfile) const
503 { mapfile->print_output_data(this, _("** PLT")); }
505 private:
506 // The size of an entry in the PLT.
507 static const int plt_entry_size = 16;
509 // The first entry in the PLT for an executable.
510 static unsigned char exec_first_plt_entry[plt_entry_size];
512 // The first entry in the PLT for a shared object.
513 static unsigned char dyn_first_plt_entry[plt_entry_size];
515 // Other entries in the PLT for an executable.
516 static unsigned char exec_plt_entry[plt_entry_size];
518 // Other entries in the PLT for a shared object.
519 static unsigned char dyn_plt_entry[plt_entry_size];
521 // Set the final size.
522 void
523 set_final_data_size()
524 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
526 // Write out the PLT data.
527 void
528 do_write(Output_file*);
530 // The reloc section.
531 Reloc_section* rel_;
532 // The .got.plt section.
533 Output_data_space* got_plt_;
534 // The number of PLT entries.
535 unsigned int count_;
538 // Create the PLT section. The ordinary .got section is an argument,
539 // since we need to refer to the start. We also create our own .got
540 // section just for PLT entries.
542 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
543 Output_data_space* got_plt)
544 : Output_section_data(4), got_plt_(got_plt), count_(0)
546 this->rel_ = new Reloc_section(false);
547 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
548 elfcpp::SHF_ALLOC, this->rel_);
551 void
552 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
554 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
555 // linker, and so do we.
556 os->set_entsize(4);
559 // Add an entry to the PLT.
561 void
562 Output_data_plt_i386::add_entry(Symbol* gsym)
564 gold_assert(!gsym->has_plt_offset());
566 // Note that when setting the PLT offset we skip the initial
567 // reserved PLT entry.
568 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
570 ++this->count_;
572 section_offset_type got_offset = this->got_plt_->current_data_size();
574 // Every PLT entry needs a GOT entry which points back to the PLT
575 // entry (this will be changed by the dynamic linker, normally
576 // lazily when the function is called).
577 this->got_plt_->set_current_data_size(got_offset + 4);
579 // Every PLT entry needs a reloc.
580 gsym->set_needs_dynsym_entry();
581 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
582 got_offset);
584 // Note that we don't need to save the symbol. The contents of the
585 // PLT are independent of which symbols are used. The symbols only
586 // appear in the relocations.
589 // The first entry in the PLT for an executable.
591 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
593 0xff, 0x35, // pushl contents of memory address
594 0, 0, 0, 0, // replaced with address of .got + 4
595 0xff, 0x25, // jmp indirect
596 0, 0, 0, 0, // replaced with address of .got + 8
597 0, 0, 0, 0 // unused
600 // The first entry in the PLT for a shared object.
602 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
604 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
605 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
606 0, 0, 0, 0 // unused
609 // Subsequent entries in the PLT for an executable.
611 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
613 0xff, 0x25, // jmp indirect
614 0, 0, 0, 0, // replaced with address of symbol in .got
615 0x68, // pushl immediate
616 0, 0, 0, 0, // replaced with offset into relocation table
617 0xe9, // jmp relative
618 0, 0, 0, 0 // replaced with offset to start of .plt
621 // Subsequent entries in the PLT for a shared object.
623 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
625 0xff, 0xa3, // jmp *offset(%ebx)
626 0, 0, 0, 0, // replaced with offset of symbol in .got
627 0x68, // pushl immediate
628 0, 0, 0, 0, // replaced with offset into relocation table
629 0xe9, // jmp relative
630 0, 0, 0, 0 // replaced with offset to start of .plt
633 // Write out the PLT. This uses the hand-coded instructions above,
634 // and adjusts them as needed. This is all specified by the i386 ELF
635 // Processor Supplement.
637 void
638 Output_data_plt_i386::do_write(Output_file* of)
640 const off_t offset = this->offset();
641 const section_size_type oview_size =
642 convert_to_section_size_type(this->data_size());
643 unsigned char* const oview = of->get_output_view(offset, oview_size);
645 const off_t got_file_offset = this->got_plt_->offset();
646 const section_size_type got_size =
647 convert_to_section_size_type(this->got_plt_->data_size());
648 unsigned char* const got_view = of->get_output_view(got_file_offset,
649 got_size);
651 unsigned char* pov = oview;
653 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
654 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
656 if (parameters->options().shared())
657 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
658 else
660 memcpy(pov, exec_first_plt_entry, plt_entry_size);
661 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
662 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
664 pov += plt_entry_size;
666 unsigned char* got_pov = got_view;
668 memset(got_pov, 0, 12);
669 got_pov += 12;
671 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
673 unsigned int plt_offset = plt_entry_size;
674 unsigned int plt_rel_offset = 0;
675 unsigned int got_offset = 12;
676 const unsigned int count = this->count_;
677 for (unsigned int i = 0;
678 i < count;
679 ++i,
680 pov += plt_entry_size,
681 got_pov += 4,
682 plt_offset += plt_entry_size,
683 plt_rel_offset += rel_size,
684 got_offset += 4)
686 // Set and adjust the PLT entry itself.
688 if (parameters->options().shared())
690 memcpy(pov, dyn_plt_entry, plt_entry_size);
691 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
693 else
695 memcpy(pov, exec_plt_entry, plt_entry_size);
696 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
697 (got_address
698 + got_offset));
701 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
702 elfcpp::Swap<32, false>::writeval(pov + 12,
703 - (plt_offset + plt_entry_size));
705 // Set the entry in the GOT.
706 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
709 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
710 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
712 of->write_output_view(offset, oview_size, oview);
713 of->write_output_view(got_file_offset, got_size, got_view);
716 // Create a PLT entry for a global symbol.
718 void
719 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
721 if (gsym->has_plt_offset())
722 return;
724 if (this->plt_ == NULL)
726 // Create the GOT sections first.
727 this->got_section(symtab, layout);
729 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
730 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
731 (elfcpp::SHF_ALLOC
732 | elfcpp::SHF_EXECINSTR),
733 this->plt_);
736 this->plt_->add_entry(gsym);
739 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
741 void
742 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
744 if (this->tls_base_symbol_defined_)
745 return;
747 Output_segment* tls_segment = layout->tls_segment();
748 if (tls_segment != NULL)
750 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
751 tls_segment, 0, 0,
752 elfcpp::STT_TLS,
753 elfcpp::STB_LOCAL,
754 elfcpp::STV_HIDDEN, 0,
755 Symbol::SEGMENT_END, true);
757 this->tls_base_symbol_defined_ = true;
760 // Create a GOT entry for the TLS module index.
762 unsigned int
763 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
764 Sized_relobj<32, false>* object)
766 if (this->got_mod_index_offset_ == -1U)
768 gold_assert(symtab != NULL && layout != NULL && object != NULL);
769 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
770 Output_data_got<32, false>* got = this->got_section(symtab, layout);
771 unsigned int got_offset = got->add_constant(0);
772 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
773 got_offset);
774 got->add_constant(0);
775 this->got_mod_index_offset_ = got_offset;
777 return this->got_mod_index_offset_;
780 // Optimize the TLS relocation type based on what we know about the
781 // symbol. IS_FINAL is true if the final address of this symbol is
782 // known at link time.
784 tls::Tls_optimization
785 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
787 // If we are generating a shared library, then we can't do anything
788 // in the linker.
789 if (parameters->options().shared())
790 return tls::TLSOPT_NONE;
792 switch (r_type)
794 case elfcpp::R_386_TLS_GD:
795 case elfcpp::R_386_TLS_GOTDESC:
796 case elfcpp::R_386_TLS_DESC_CALL:
797 // These are General-Dynamic which permits fully general TLS
798 // access. Since we know that we are generating an executable,
799 // we can convert this to Initial-Exec. If we also know that
800 // this is a local symbol, we can further switch to Local-Exec.
801 if (is_final)
802 return tls::TLSOPT_TO_LE;
803 return tls::TLSOPT_TO_IE;
805 case elfcpp::R_386_TLS_LDM:
806 // This is Local-Dynamic, which refers to a local symbol in the
807 // dynamic TLS block. Since we know that we generating an
808 // executable, we can switch to Local-Exec.
809 return tls::TLSOPT_TO_LE;
811 case elfcpp::R_386_TLS_LDO_32:
812 // Another type of Local-Dynamic relocation.
813 return tls::TLSOPT_TO_LE;
815 case elfcpp::R_386_TLS_IE:
816 case elfcpp::R_386_TLS_GOTIE:
817 case elfcpp::R_386_TLS_IE_32:
818 // These are Initial-Exec relocs which get the thread offset
819 // from the GOT. If we know that we are linking against the
820 // local symbol, we can switch to Local-Exec, which links the
821 // thread offset into the instruction.
822 if (is_final)
823 return tls::TLSOPT_TO_LE;
824 return tls::TLSOPT_NONE;
826 case elfcpp::R_386_TLS_LE:
827 case elfcpp::R_386_TLS_LE_32:
828 // When we already have Local-Exec, there is nothing further we
829 // can do.
830 return tls::TLSOPT_NONE;
832 default:
833 gold_unreachable();
837 // Report an unsupported relocation against a local symbol.
839 void
840 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
841 unsigned int r_type)
843 gold_error(_("%s: unsupported reloc %u against local symbol"),
844 object->name().c_str(), r_type);
847 // Scan a relocation for a local symbol.
849 inline void
850 Target_i386::Scan::local(const General_options&,
851 Symbol_table* symtab,
852 Layout* layout,
853 Target_i386* target,
854 Sized_relobj<32, false>* object,
855 unsigned int data_shndx,
856 Output_section* output_section,
857 const elfcpp::Rel<32, false>& reloc,
858 unsigned int r_type,
859 const elfcpp::Sym<32, false>& lsym)
861 switch (r_type)
863 case elfcpp::R_386_NONE:
864 case elfcpp::R_386_GNU_VTINHERIT:
865 case elfcpp::R_386_GNU_VTENTRY:
866 break;
868 case elfcpp::R_386_32:
869 // If building a shared library (or a position-independent
870 // executable), we need to create a dynamic relocation for
871 // this location. The relocation applied at link time will
872 // apply the link-time value, so we flag the location with
873 // an R_386_RELATIVE relocation so the dynamic loader can
874 // relocate it easily.
875 if (parameters->options().output_is_position_independent())
877 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
878 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
879 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
880 output_section, data_shndx,
881 reloc.get_r_offset());
883 break;
885 case elfcpp::R_386_16:
886 case elfcpp::R_386_8:
887 // If building a shared library (or a position-independent
888 // executable), we need to create a dynamic relocation for
889 // this location. Because the addend needs to remain in the
890 // data section, we need to be careful not to apply this
891 // relocation statically.
892 if (parameters->options().output_is_position_independent())
894 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
895 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
896 if (lsym.get_st_type() != elfcpp::STT_SECTION)
897 rel_dyn->add_local(object, r_sym, r_type, output_section,
898 data_shndx, reloc.get_r_offset());
899 else
901 gold_assert(lsym.get_st_value() == 0);
902 unsigned int shndx = lsym.get_st_shndx();
903 bool is_ordinary;
904 shndx = object->adjust_sym_shndx(r_sym, shndx,
905 &is_ordinary);
906 if (!is_ordinary)
907 object->error(_("section symbol %u has bad shndx %u"),
908 r_sym, shndx);
909 else
910 rel_dyn->add_local_section(object, shndx,
911 r_type, output_section,
912 data_shndx, reloc.get_r_offset());
915 break;
917 case elfcpp::R_386_PC32:
918 case elfcpp::R_386_PC16:
919 case elfcpp::R_386_PC8:
920 break;
922 case elfcpp::R_386_PLT32:
923 // Since we know this is a local symbol, we can handle this as a
924 // PC32 reloc.
925 break;
927 case elfcpp::R_386_GOTOFF:
928 case elfcpp::R_386_GOTPC:
929 // We need a GOT section.
930 target->got_section(symtab, layout);
931 break;
933 case elfcpp::R_386_GOT32:
935 // The symbol requires a GOT entry.
936 Output_data_got<32, false>* got = target->got_section(symtab, layout);
937 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
938 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
940 // If we are generating a shared object, we need to add a
941 // dynamic RELATIVE relocation for this symbol's GOT entry.
942 if (parameters->options().output_is_position_independent())
944 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
945 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
946 rel_dyn->add_local_relative(
947 object, r_sym, elfcpp::R_386_RELATIVE, got,
948 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
952 break;
954 // These are relocations which should only be seen by the
955 // dynamic linker, and should never be seen here.
956 case elfcpp::R_386_COPY:
957 case elfcpp::R_386_GLOB_DAT:
958 case elfcpp::R_386_JUMP_SLOT:
959 case elfcpp::R_386_RELATIVE:
960 case elfcpp::R_386_TLS_TPOFF:
961 case elfcpp::R_386_TLS_DTPMOD32:
962 case elfcpp::R_386_TLS_DTPOFF32:
963 case elfcpp::R_386_TLS_TPOFF32:
964 case elfcpp::R_386_TLS_DESC:
965 gold_error(_("%s: unexpected reloc %u in object file"),
966 object->name().c_str(), r_type);
967 break;
969 // These are initial TLS relocs, which are expected when
970 // linking.
971 case elfcpp::R_386_TLS_GD: // Global-dynamic
972 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
973 case elfcpp::R_386_TLS_DESC_CALL:
974 case elfcpp::R_386_TLS_LDM: // Local-dynamic
975 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
976 case elfcpp::R_386_TLS_IE: // Initial-exec
977 case elfcpp::R_386_TLS_IE_32:
978 case elfcpp::R_386_TLS_GOTIE:
979 case elfcpp::R_386_TLS_LE: // Local-exec
980 case elfcpp::R_386_TLS_LE_32:
982 bool output_is_shared = parameters->options().shared();
983 const tls::Tls_optimization optimized_type
984 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
985 switch (r_type)
987 case elfcpp::R_386_TLS_GD: // Global-dynamic
988 if (optimized_type == tls::TLSOPT_NONE)
990 // Create a pair of GOT entries for the module index and
991 // dtv-relative offset.
992 Output_data_got<32, false>* got
993 = target->got_section(symtab, layout);
994 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
995 unsigned int shndx = lsym.get_st_shndx();
996 bool is_ordinary;
997 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
998 if (!is_ordinary)
999 object->error(_("local symbol %u has bad shndx %u"),
1000 r_sym, shndx);
1001 else
1002 got->add_local_pair_with_rel(object, r_sym, shndx,
1003 GOT_TYPE_TLS_PAIR,
1004 target->rel_dyn_section(layout),
1005 elfcpp::R_386_TLS_DTPMOD32, 0);
1007 else if (optimized_type != tls::TLSOPT_TO_LE)
1008 unsupported_reloc_local(object, r_type);
1009 break;
1011 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1012 target->define_tls_base_symbol(symtab, layout);
1013 if (optimized_type == tls::TLSOPT_NONE)
1015 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1016 Output_data_got<32, false>* got
1017 = target->got_section(symtab, layout);
1018 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1019 unsigned int shndx = lsym.get_st_shndx();
1020 bool is_ordinary;
1021 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1022 if (!is_ordinary)
1023 object->error(_("local symbol %u has bad shndx %u"),
1024 r_sym, shndx);
1025 else
1026 got->add_local_pair_with_rel(object, r_sym, shndx,
1027 GOT_TYPE_TLS_DESC,
1028 target->rel_dyn_section(layout),
1029 elfcpp::R_386_TLS_DESC, 0);
1031 else if (optimized_type != tls::TLSOPT_TO_LE)
1032 unsupported_reloc_local(object, r_type);
1033 break;
1035 case elfcpp::R_386_TLS_DESC_CALL:
1036 break;
1038 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1039 if (optimized_type == tls::TLSOPT_NONE)
1041 // Create a GOT entry for the module index.
1042 target->got_mod_index_entry(symtab, layout, object);
1044 else if (optimized_type != tls::TLSOPT_TO_LE)
1045 unsupported_reloc_local(object, r_type);
1046 break;
1048 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1049 break;
1051 case elfcpp::R_386_TLS_IE: // Initial-exec
1052 case elfcpp::R_386_TLS_IE_32:
1053 case elfcpp::R_386_TLS_GOTIE:
1054 layout->set_has_static_tls();
1055 if (optimized_type == tls::TLSOPT_NONE)
1057 // For the R_386_TLS_IE relocation, we need to create a
1058 // dynamic relocation when building a shared library.
1059 if (r_type == elfcpp::R_386_TLS_IE
1060 && parameters->options().shared())
1062 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1063 unsigned int r_sym
1064 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1065 rel_dyn->add_local_relative(object, r_sym,
1066 elfcpp::R_386_RELATIVE,
1067 output_section, data_shndx,
1068 reloc.get_r_offset());
1070 // Create a GOT entry for the tp-relative offset.
1071 Output_data_got<32, false>* got
1072 = target->got_section(symtab, layout);
1073 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1074 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1075 ? elfcpp::R_386_TLS_TPOFF32
1076 : elfcpp::R_386_TLS_TPOFF);
1077 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1078 ? GOT_TYPE_TLS_OFFSET
1079 : GOT_TYPE_TLS_NOFFSET);
1080 got->add_local_with_rel(object, r_sym, got_type,
1081 target->rel_dyn_section(layout),
1082 dyn_r_type);
1084 else if (optimized_type != tls::TLSOPT_TO_LE)
1085 unsupported_reloc_local(object, r_type);
1086 break;
1088 case elfcpp::R_386_TLS_LE: // Local-exec
1089 case elfcpp::R_386_TLS_LE_32:
1090 layout->set_has_static_tls();
1091 if (output_is_shared)
1093 // We need to create a dynamic relocation.
1094 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1095 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1096 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1097 ? elfcpp::R_386_TLS_TPOFF32
1098 : elfcpp::R_386_TLS_TPOFF);
1099 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1100 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1101 data_shndx, reloc.get_r_offset());
1103 break;
1105 default:
1106 gold_unreachable();
1109 break;
1111 case elfcpp::R_386_32PLT:
1112 case elfcpp::R_386_TLS_GD_32:
1113 case elfcpp::R_386_TLS_GD_PUSH:
1114 case elfcpp::R_386_TLS_GD_CALL:
1115 case elfcpp::R_386_TLS_GD_POP:
1116 case elfcpp::R_386_TLS_LDM_32:
1117 case elfcpp::R_386_TLS_LDM_PUSH:
1118 case elfcpp::R_386_TLS_LDM_CALL:
1119 case elfcpp::R_386_TLS_LDM_POP:
1120 case elfcpp::R_386_USED_BY_INTEL_200:
1121 default:
1122 unsupported_reloc_local(object, r_type);
1123 break;
1127 // Report an unsupported relocation against a global symbol.
1129 void
1130 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1131 unsigned int r_type,
1132 Symbol* gsym)
1134 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1135 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1138 // Scan a relocation for a global symbol.
1140 inline void
1141 Target_i386::Scan::global(const General_options&,
1142 Symbol_table* symtab,
1143 Layout* layout,
1144 Target_i386* target,
1145 Sized_relobj<32, false>* object,
1146 unsigned int data_shndx,
1147 Output_section* output_section,
1148 const elfcpp::Rel<32, false>& reloc,
1149 unsigned int r_type,
1150 Symbol* gsym)
1152 switch (r_type)
1154 case elfcpp::R_386_NONE:
1155 case elfcpp::R_386_GNU_VTINHERIT:
1156 case elfcpp::R_386_GNU_VTENTRY:
1157 break;
1159 case elfcpp::R_386_32:
1160 case elfcpp::R_386_16:
1161 case elfcpp::R_386_8:
1163 // Make a PLT entry if necessary.
1164 if (gsym->needs_plt_entry())
1166 target->make_plt_entry(symtab, layout, gsym);
1167 // Since this is not a PC-relative relocation, we may be
1168 // taking the address of a function. In that case we need to
1169 // set the entry in the dynamic symbol table to the address of
1170 // the PLT entry.
1171 if (gsym->is_from_dynobj() && !parameters->options().shared())
1172 gsym->set_needs_dynsym_value();
1174 // Make a dynamic relocation if necessary.
1175 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1177 if (target->may_need_copy_reloc(gsym))
1179 target->copy_reloc(symtab, layout, object,
1180 data_shndx, output_section, gsym, reloc);
1182 else if (r_type == elfcpp::R_386_32
1183 && gsym->can_use_relative_reloc(false))
1185 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1186 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1187 output_section, object,
1188 data_shndx, reloc.get_r_offset());
1190 else
1192 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1193 rel_dyn->add_global(gsym, r_type, output_section, object,
1194 data_shndx, reloc.get_r_offset());
1198 break;
1200 case elfcpp::R_386_PC32:
1201 case elfcpp::R_386_PC16:
1202 case elfcpp::R_386_PC8:
1204 // Make a PLT entry if necessary.
1205 if (gsym->needs_plt_entry())
1207 // These relocations are used for function calls only in
1208 // non-PIC code. For a 32-bit relocation in a shared library,
1209 // we'll need a text relocation anyway, so we can skip the
1210 // PLT entry and let the dynamic linker bind the call directly
1211 // to the target. For smaller relocations, we should use a
1212 // PLT entry to ensure that the call can reach.
1213 if (!parameters->options().shared()
1214 || r_type != elfcpp::R_386_PC32)
1215 target->make_plt_entry(symtab, layout, gsym);
1217 // Make a dynamic relocation if necessary.
1218 int flags = Symbol::NON_PIC_REF;
1219 if (gsym->type() == elfcpp::STT_FUNC)
1220 flags |= Symbol::FUNCTION_CALL;
1221 if (gsym->needs_dynamic_reloc(flags))
1223 if (target->may_need_copy_reloc(gsym))
1225 target->copy_reloc(symtab, layout, object,
1226 data_shndx, output_section, gsym, reloc);
1228 else
1230 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1231 rel_dyn->add_global(gsym, r_type, output_section, object,
1232 data_shndx, reloc.get_r_offset());
1236 break;
1238 case elfcpp::R_386_GOT32:
1240 // The symbol requires a GOT entry.
1241 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1242 if (gsym->final_value_is_known())
1243 got->add_global(gsym, GOT_TYPE_STANDARD);
1244 else
1246 // If this symbol is not fully resolved, we need to add a
1247 // GOT entry with a dynamic relocation.
1248 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1249 if (gsym->is_from_dynobj()
1250 || gsym->is_undefined()
1251 || gsym->is_preemptible())
1252 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1253 rel_dyn, elfcpp::R_386_GLOB_DAT);
1254 else
1256 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1257 rel_dyn->add_global_relative(
1258 gsym, elfcpp::R_386_RELATIVE, got,
1259 gsym->got_offset(GOT_TYPE_STANDARD));
1263 break;
1265 case elfcpp::R_386_PLT32:
1266 // If the symbol is fully resolved, this is just a PC32 reloc.
1267 // Otherwise we need a PLT entry.
1268 if (gsym->final_value_is_known())
1269 break;
1270 // If building a shared library, we can also skip the PLT entry
1271 // if the symbol is defined in the output file and is protected
1272 // or hidden.
1273 if (gsym->is_defined()
1274 && !gsym->is_from_dynobj()
1275 && !gsym->is_preemptible())
1276 break;
1277 target->make_plt_entry(symtab, layout, gsym);
1278 break;
1280 case elfcpp::R_386_GOTOFF:
1281 case elfcpp::R_386_GOTPC:
1282 // We need a GOT section.
1283 target->got_section(symtab, layout);
1284 break;
1286 // These are relocations which should only be seen by the
1287 // dynamic linker, and should never be seen here.
1288 case elfcpp::R_386_COPY:
1289 case elfcpp::R_386_GLOB_DAT:
1290 case elfcpp::R_386_JUMP_SLOT:
1291 case elfcpp::R_386_RELATIVE:
1292 case elfcpp::R_386_TLS_TPOFF:
1293 case elfcpp::R_386_TLS_DTPMOD32:
1294 case elfcpp::R_386_TLS_DTPOFF32:
1295 case elfcpp::R_386_TLS_TPOFF32:
1296 case elfcpp::R_386_TLS_DESC:
1297 gold_error(_("%s: unexpected reloc %u in object file"),
1298 object->name().c_str(), r_type);
1299 break;
1301 // These are initial tls relocs, which are expected when
1302 // linking.
1303 case elfcpp::R_386_TLS_GD: // Global-dynamic
1304 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1305 case elfcpp::R_386_TLS_DESC_CALL:
1306 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1307 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1308 case elfcpp::R_386_TLS_IE: // Initial-exec
1309 case elfcpp::R_386_TLS_IE_32:
1310 case elfcpp::R_386_TLS_GOTIE:
1311 case elfcpp::R_386_TLS_LE: // Local-exec
1312 case elfcpp::R_386_TLS_LE_32:
1314 const bool is_final = gsym->final_value_is_known();
1315 const tls::Tls_optimization optimized_type
1316 = Target_i386::optimize_tls_reloc(is_final, r_type);
1317 switch (r_type)
1319 case elfcpp::R_386_TLS_GD: // Global-dynamic
1320 if (optimized_type == tls::TLSOPT_NONE)
1322 // Create a pair of GOT entries for the module index and
1323 // dtv-relative offset.
1324 Output_data_got<32, false>* got
1325 = target->got_section(symtab, layout);
1326 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1327 target->rel_dyn_section(layout),
1328 elfcpp::R_386_TLS_DTPMOD32,
1329 elfcpp::R_386_TLS_DTPOFF32);
1331 else if (optimized_type == tls::TLSOPT_TO_IE)
1333 // Create a GOT entry for the tp-relative offset.
1334 Output_data_got<32, false>* got
1335 = target->got_section(symtab, layout);
1336 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1337 target->rel_dyn_section(layout),
1338 elfcpp::R_386_TLS_TPOFF);
1340 else if (optimized_type != tls::TLSOPT_TO_LE)
1341 unsupported_reloc_global(object, r_type, gsym);
1342 break;
1344 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1345 target->define_tls_base_symbol(symtab, layout);
1346 if (optimized_type == tls::TLSOPT_NONE)
1348 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1349 Output_data_got<32, false>* got
1350 = target->got_section(symtab, layout);
1351 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1352 target->rel_dyn_section(layout),
1353 elfcpp::R_386_TLS_DESC, 0);
1355 else if (optimized_type == tls::TLSOPT_TO_IE)
1357 // Create a GOT entry for the tp-relative offset.
1358 Output_data_got<32, false>* got
1359 = target->got_section(symtab, layout);
1360 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1361 target->rel_dyn_section(layout),
1362 elfcpp::R_386_TLS_TPOFF);
1364 else if (optimized_type != tls::TLSOPT_TO_LE)
1365 unsupported_reloc_global(object, r_type, gsym);
1366 break;
1368 case elfcpp::R_386_TLS_DESC_CALL:
1369 break;
1371 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1372 if (optimized_type == tls::TLSOPT_NONE)
1374 // Create a GOT entry for the module index.
1375 target->got_mod_index_entry(symtab, layout, object);
1377 else if (optimized_type != tls::TLSOPT_TO_LE)
1378 unsupported_reloc_global(object, r_type, gsym);
1379 break;
1381 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1382 break;
1384 case elfcpp::R_386_TLS_IE: // Initial-exec
1385 case elfcpp::R_386_TLS_IE_32:
1386 case elfcpp::R_386_TLS_GOTIE:
1387 layout->set_has_static_tls();
1388 if (optimized_type == tls::TLSOPT_NONE)
1390 // For the R_386_TLS_IE relocation, we need to create a
1391 // dynamic relocation when building a shared library.
1392 if (r_type == elfcpp::R_386_TLS_IE
1393 && parameters->options().shared())
1395 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1396 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1397 output_section, object,
1398 data_shndx,
1399 reloc.get_r_offset());
1401 // Create a GOT entry for the tp-relative offset.
1402 Output_data_got<32, false>* got
1403 = target->got_section(symtab, layout);
1404 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1405 ? elfcpp::R_386_TLS_TPOFF32
1406 : elfcpp::R_386_TLS_TPOFF);
1407 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1408 ? GOT_TYPE_TLS_OFFSET
1409 : GOT_TYPE_TLS_NOFFSET);
1410 got->add_global_with_rel(gsym, got_type,
1411 target->rel_dyn_section(layout),
1412 dyn_r_type);
1414 else if (optimized_type != tls::TLSOPT_TO_LE)
1415 unsupported_reloc_global(object, r_type, gsym);
1416 break;
1418 case elfcpp::R_386_TLS_LE: // Local-exec
1419 case elfcpp::R_386_TLS_LE_32:
1420 layout->set_has_static_tls();
1421 if (parameters->options().shared())
1423 // We need to create a dynamic relocation.
1424 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1425 ? elfcpp::R_386_TLS_TPOFF32
1426 : elfcpp::R_386_TLS_TPOFF);
1427 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1428 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1429 data_shndx, reloc.get_r_offset());
1431 break;
1433 default:
1434 gold_unreachable();
1437 break;
1439 case elfcpp::R_386_32PLT:
1440 case elfcpp::R_386_TLS_GD_32:
1441 case elfcpp::R_386_TLS_GD_PUSH:
1442 case elfcpp::R_386_TLS_GD_CALL:
1443 case elfcpp::R_386_TLS_GD_POP:
1444 case elfcpp::R_386_TLS_LDM_32:
1445 case elfcpp::R_386_TLS_LDM_PUSH:
1446 case elfcpp::R_386_TLS_LDM_CALL:
1447 case elfcpp::R_386_TLS_LDM_POP:
1448 case elfcpp::R_386_USED_BY_INTEL_200:
1449 default:
1450 unsupported_reloc_global(object, r_type, gsym);
1451 break;
1455 // Scan relocations for a section.
1457 void
1458 Target_i386::scan_relocs(const General_options& options,
1459 Symbol_table* symtab,
1460 Layout* layout,
1461 Sized_relobj<32, false>* object,
1462 unsigned int data_shndx,
1463 unsigned int sh_type,
1464 const unsigned char* prelocs,
1465 size_t reloc_count,
1466 Output_section* output_section,
1467 bool needs_special_offset_handling,
1468 size_t local_symbol_count,
1469 const unsigned char* plocal_symbols)
1471 if (sh_type == elfcpp::SHT_RELA)
1473 gold_error(_("%s: unsupported RELA reloc section"),
1474 object->name().c_str());
1475 return;
1478 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1479 Target_i386::Scan>(
1480 options,
1481 symtab,
1482 layout,
1483 this,
1484 object,
1485 data_shndx,
1486 prelocs,
1487 reloc_count,
1488 output_section,
1489 needs_special_offset_handling,
1490 local_symbol_count,
1491 plocal_symbols);
1494 // Finalize the sections.
1496 void
1497 Target_i386::do_finalize_sections(Layout* layout)
1499 // Fill in some more dynamic tags.
1500 Output_data_dynamic* const odyn = layout->dynamic_data();
1501 if (odyn != NULL)
1503 if (this->got_plt_ != NULL)
1504 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1506 if (this->plt_ != NULL)
1508 const Output_data* od = this->plt_->rel_plt();
1509 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1510 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1511 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1514 if (this->rel_dyn_ != NULL)
1516 const Output_data* od = this->rel_dyn_;
1517 odyn->add_section_address(elfcpp::DT_REL, od);
1518 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1519 odyn->add_constant(elfcpp::DT_RELENT,
1520 elfcpp::Elf_sizes<32>::rel_size);
1523 if (!parameters->options().shared())
1525 // The value of the DT_DEBUG tag is filled in by the dynamic
1526 // linker at run time, and used by the debugger.
1527 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1531 // Emit any relocs we saved in an attempt to avoid generating COPY
1532 // relocs.
1533 if (this->copy_relocs_.any_saved_relocs())
1534 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1537 // Return whether a direct absolute static relocation needs to be applied.
1538 // In cases where Scan::local() or Scan::global() has created
1539 // a dynamic relocation other than R_386_RELATIVE, the addend
1540 // of the relocation is carried in the data, and we must not
1541 // apply the static relocation.
1543 inline bool
1544 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1545 int ref_flags,
1546 bool is_32bit)
1548 // For local symbols, we will have created a non-RELATIVE dynamic
1549 // relocation only if (a) the output is position independent,
1550 // (b) the relocation is absolute (not pc- or segment-relative), and
1551 // (c) the relocation is not 32 bits wide.
1552 if (gsym == NULL)
1553 return !(parameters->options().output_is_position_independent()
1554 && (ref_flags & Symbol::ABSOLUTE_REF)
1555 && !is_32bit);
1557 // For global symbols, we use the same helper routines used in the
1558 // scan pass. If we did not create a dynamic relocation, or if we
1559 // created a RELATIVE dynamic relocation, we should apply the static
1560 // relocation.
1561 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1562 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1563 && gsym->can_use_relative_reloc(ref_flags
1564 & Symbol::FUNCTION_CALL);
1565 return !has_dyn || is_rel;
1568 // Perform a relocation.
1570 inline bool
1571 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1572 Target_i386* target,
1573 size_t relnum,
1574 const elfcpp::Rel<32, false>& rel,
1575 unsigned int r_type,
1576 const Sized_symbol<32>* gsym,
1577 const Symbol_value<32>* psymval,
1578 unsigned char* view,
1579 elfcpp::Elf_types<32>::Elf_Addr address,
1580 section_size_type view_size)
1582 if (this->skip_call_tls_get_addr_)
1584 if (r_type != elfcpp::R_386_PLT32
1585 || gsym == NULL
1586 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1587 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1588 _("missing expected TLS relocation"));
1589 else
1591 this->skip_call_tls_get_addr_ = false;
1592 return false;
1596 // Pick the value to use for symbols defined in shared objects.
1597 Symbol_value<32> symval;
1598 if (gsym != NULL
1599 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1600 || r_type == elfcpp::R_386_PC16
1601 || r_type == elfcpp::R_386_PC32))
1603 symval.set_output_value(target->plt_section()->address()
1604 + gsym->plt_offset());
1605 psymval = &symval;
1608 const Sized_relobj<32, false>* object = relinfo->object;
1610 // Get the GOT offset if needed.
1611 // The GOT pointer points to the end of the GOT section.
1612 // We need to subtract the size of the GOT section to get
1613 // the actual offset to use in the relocation.
1614 bool have_got_offset = false;
1615 unsigned int got_offset = 0;
1616 switch (r_type)
1618 case elfcpp::R_386_GOT32:
1619 if (gsym != NULL)
1621 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1622 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1623 - target->got_size());
1625 else
1627 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1628 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1629 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1630 - target->got_size());
1632 have_got_offset = true;
1633 break;
1635 default:
1636 break;
1639 switch (r_type)
1641 case elfcpp::R_386_NONE:
1642 case elfcpp::R_386_GNU_VTINHERIT:
1643 case elfcpp::R_386_GNU_VTENTRY:
1644 break;
1646 case elfcpp::R_386_32:
1647 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1648 Relocate_functions<32, false>::rel32(view, object, psymval);
1649 break;
1651 case elfcpp::R_386_PC32:
1653 int ref_flags = Symbol::NON_PIC_REF;
1654 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1655 ref_flags |= Symbol::FUNCTION_CALL;
1656 if (should_apply_static_reloc(gsym, ref_flags, true))
1657 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1659 break;
1661 case elfcpp::R_386_16:
1662 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1663 Relocate_functions<32, false>::rel16(view, object, psymval);
1664 break;
1666 case elfcpp::R_386_PC16:
1668 int ref_flags = Symbol::NON_PIC_REF;
1669 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1670 ref_flags |= Symbol::FUNCTION_CALL;
1671 if (should_apply_static_reloc(gsym, ref_flags, false))
1672 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1674 break;
1676 case elfcpp::R_386_8:
1677 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1678 Relocate_functions<32, false>::rel8(view, object, psymval);
1679 break;
1681 case elfcpp::R_386_PC8:
1683 int ref_flags = Symbol::NON_PIC_REF;
1684 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1685 ref_flags |= Symbol::FUNCTION_CALL;
1686 if (should_apply_static_reloc(gsym, ref_flags, false))
1687 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1689 break;
1691 case elfcpp::R_386_PLT32:
1692 gold_assert(gsym == NULL
1693 || gsym->has_plt_offset()
1694 || gsym->final_value_is_known()
1695 || (gsym->is_defined()
1696 && !gsym->is_from_dynobj()
1697 && !gsym->is_preemptible()));
1698 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1699 break;
1701 case elfcpp::R_386_GOT32:
1702 gold_assert(have_got_offset);
1703 Relocate_functions<32, false>::rel32(view, got_offset);
1704 break;
1706 case elfcpp::R_386_GOTOFF:
1708 elfcpp::Elf_types<32>::Elf_Addr value;
1709 value = (psymval->value(object, 0)
1710 - target->got_plt_section()->address());
1711 Relocate_functions<32, false>::rel32(view, value);
1713 break;
1715 case elfcpp::R_386_GOTPC:
1717 elfcpp::Elf_types<32>::Elf_Addr value;
1718 value = target->got_plt_section()->address();
1719 Relocate_functions<32, false>::pcrel32(view, value, address);
1721 break;
1723 case elfcpp::R_386_COPY:
1724 case elfcpp::R_386_GLOB_DAT:
1725 case elfcpp::R_386_JUMP_SLOT:
1726 case elfcpp::R_386_RELATIVE:
1727 // These are outstanding tls relocs, which are unexpected when
1728 // linking.
1729 case elfcpp::R_386_TLS_TPOFF:
1730 case elfcpp::R_386_TLS_DTPMOD32:
1731 case elfcpp::R_386_TLS_DTPOFF32:
1732 case elfcpp::R_386_TLS_TPOFF32:
1733 case elfcpp::R_386_TLS_DESC:
1734 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1735 _("unexpected reloc %u in object file"),
1736 r_type);
1737 break;
1739 // These are initial tls relocs, which are expected when
1740 // linking.
1741 case elfcpp::R_386_TLS_GD: // Global-dynamic
1742 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1743 case elfcpp::R_386_TLS_DESC_CALL:
1744 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1745 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1746 case elfcpp::R_386_TLS_IE: // Initial-exec
1747 case elfcpp::R_386_TLS_IE_32:
1748 case elfcpp::R_386_TLS_GOTIE:
1749 case elfcpp::R_386_TLS_LE: // Local-exec
1750 case elfcpp::R_386_TLS_LE_32:
1751 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1752 view, address, view_size);
1753 break;
1755 case elfcpp::R_386_32PLT:
1756 case elfcpp::R_386_TLS_GD_32:
1757 case elfcpp::R_386_TLS_GD_PUSH:
1758 case elfcpp::R_386_TLS_GD_CALL:
1759 case elfcpp::R_386_TLS_GD_POP:
1760 case elfcpp::R_386_TLS_LDM_32:
1761 case elfcpp::R_386_TLS_LDM_PUSH:
1762 case elfcpp::R_386_TLS_LDM_CALL:
1763 case elfcpp::R_386_TLS_LDM_POP:
1764 case elfcpp::R_386_USED_BY_INTEL_200:
1765 default:
1766 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1767 _("unsupported reloc %u"),
1768 r_type);
1769 break;
1772 return true;
1775 // Perform a TLS relocation.
1777 inline void
1778 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1779 Target_i386* target,
1780 size_t relnum,
1781 const elfcpp::Rel<32, false>& rel,
1782 unsigned int r_type,
1783 const Sized_symbol<32>* gsym,
1784 const Symbol_value<32>* psymval,
1785 unsigned char* view,
1786 elfcpp::Elf_types<32>::Elf_Addr,
1787 section_size_type view_size)
1789 Output_segment* tls_segment = relinfo->layout->tls_segment();
1791 const Sized_relobj<32, false>* object = relinfo->object;
1793 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1795 const bool is_final =
1796 (gsym == NULL
1797 ? !parameters->options().output_is_position_independent()
1798 : gsym->final_value_is_known());
1799 const tls::Tls_optimization optimized_type
1800 = Target_i386::optimize_tls_reloc(is_final, r_type);
1801 switch (r_type)
1803 case elfcpp::R_386_TLS_GD: // Global-dynamic
1804 if (optimized_type == tls::TLSOPT_TO_LE)
1806 gold_assert(tls_segment != NULL);
1807 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1808 rel, r_type, value, view,
1809 view_size);
1810 break;
1812 else
1814 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1815 ? GOT_TYPE_TLS_NOFFSET
1816 : GOT_TYPE_TLS_PAIR);
1817 unsigned int got_offset;
1818 if (gsym != NULL)
1820 gold_assert(gsym->has_got_offset(got_type));
1821 got_offset = gsym->got_offset(got_type) - target->got_size();
1823 else
1825 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1826 gold_assert(object->local_has_got_offset(r_sym, got_type));
1827 got_offset = (object->local_got_offset(r_sym, got_type)
1828 - target->got_size());
1830 if (optimized_type == tls::TLSOPT_TO_IE)
1832 gold_assert(tls_segment != NULL);
1833 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1834 got_offset, view, view_size);
1835 break;
1837 else if (optimized_type == tls::TLSOPT_NONE)
1839 // Relocate the field with the offset of the pair of GOT
1840 // entries.
1841 Relocate_functions<32, false>::rel32(view, got_offset);
1842 break;
1845 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1846 _("unsupported reloc %u"),
1847 r_type);
1848 break;
1850 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1851 case elfcpp::R_386_TLS_DESC_CALL:
1852 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1853 if (optimized_type == tls::TLSOPT_TO_LE)
1855 gold_assert(tls_segment != NULL);
1856 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1857 rel, r_type, value, view,
1858 view_size);
1859 break;
1861 else
1863 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1864 ? GOT_TYPE_TLS_NOFFSET
1865 : GOT_TYPE_TLS_DESC);
1866 unsigned int got_offset;
1867 if (gsym != NULL)
1869 gold_assert(gsym->has_got_offset(got_type));
1870 got_offset = gsym->got_offset(got_type) - target->got_size();
1872 else
1874 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1875 gold_assert(object->local_has_got_offset(r_sym, got_type));
1876 got_offset = (object->local_got_offset(r_sym, got_type)
1877 - target->got_size());
1879 if (optimized_type == tls::TLSOPT_TO_IE)
1881 gold_assert(tls_segment != NULL);
1882 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1883 got_offset, view, view_size);
1884 break;
1886 else if (optimized_type == tls::TLSOPT_NONE)
1888 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1890 // Relocate the field with the offset of the pair of GOT
1891 // entries.
1892 Relocate_functions<32, false>::rel32(view, got_offset);
1894 break;
1897 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1898 _("unsupported reloc %u"),
1899 r_type);
1900 break;
1902 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1903 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1905 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1906 _("both SUN and GNU model "
1907 "TLS relocations"));
1908 break;
1910 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1911 if (optimized_type == tls::TLSOPT_TO_LE)
1913 gold_assert(tls_segment != NULL);
1914 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1915 value, view, view_size);
1916 break;
1918 else if (optimized_type == tls::TLSOPT_NONE)
1920 // Relocate the field with the offset of the GOT entry for
1921 // the module index.
1922 unsigned int got_offset;
1923 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1924 - target->got_size());
1925 Relocate_functions<32, false>::rel32(view, got_offset);
1926 break;
1928 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1929 _("unsupported reloc %u"),
1930 r_type);
1931 break;
1933 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1934 // This reloc can appear in debugging sections, in which case we
1935 // won't see the TLS_LDM reloc. The local_dynamic_type field
1936 // tells us this.
1937 if (optimized_type == tls::TLSOPT_TO_LE
1938 && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
1940 gold_assert(tls_segment != NULL);
1941 value -= tls_segment->memsz();
1943 Relocate_functions<32, false>::rel32(view, value);
1944 break;
1946 case elfcpp::R_386_TLS_IE: // Initial-exec
1947 case elfcpp::R_386_TLS_GOTIE:
1948 case elfcpp::R_386_TLS_IE_32:
1949 if (optimized_type == tls::TLSOPT_TO_LE)
1951 gold_assert(tls_segment != NULL);
1952 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1953 rel, r_type, value, view,
1954 view_size);
1955 break;
1957 else if (optimized_type == tls::TLSOPT_NONE)
1959 // Relocate the field with the offset of the GOT entry for
1960 // the tp-relative offset of the symbol.
1961 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1962 ? GOT_TYPE_TLS_OFFSET
1963 : GOT_TYPE_TLS_NOFFSET);
1964 unsigned int got_offset;
1965 if (gsym != NULL)
1967 gold_assert(gsym->has_got_offset(got_type));
1968 got_offset = gsym->got_offset(got_type);
1970 else
1972 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1973 gold_assert(object->local_has_got_offset(r_sym, got_type));
1974 got_offset = object->local_got_offset(r_sym, got_type);
1976 // For the R_386_TLS_IE relocation, we need to apply the
1977 // absolute address of the GOT entry.
1978 if (r_type == elfcpp::R_386_TLS_IE)
1979 got_offset += target->got_plt_section()->address();
1980 // All GOT offsets are relative to the end of the GOT.
1981 got_offset -= target->got_size();
1982 Relocate_functions<32, false>::rel32(view, got_offset);
1983 break;
1985 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1986 _("unsupported reloc %u"),
1987 r_type);
1988 break;
1990 case elfcpp::R_386_TLS_LE: // Local-exec
1991 // If we're creating a shared library, a dynamic relocation will
1992 // have been created for this location, so do not apply it now.
1993 if (!parameters->options().shared())
1995 gold_assert(tls_segment != NULL);
1996 value -= tls_segment->memsz();
1997 Relocate_functions<32, false>::rel32(view, value);
1999 break;
2001 case elfcpp::R_386_TLS_LE_32:
2002 // If we're creating a shared library, a dynamic relocation will
2003 // have been created for this location, so do not apply it now.
2004 if (!parameters->options().shared())
2006 gold_assert(tls_segment != NULL);
2007 value = tls_segment->memsz() - value;
2008 Relocate_functions<32, false>::rel32(view, value);
2010 break;
2014 // Do a relocation in which we convert a TLS General-Dynamic to a
2015 // Local-Exec.
2017 inline void
2018 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2019 size_t relnum,
2020 Output_segment* tls_segment,
2021 const elfcpp::Rel<32, false>& rel,
2022 unsigned int,
2023 elfcpp::Elf_types<32>::Elf_Addr value,
2024 unsigned char* view,
2025 section_size_type view_size)
2027 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2028 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2029 // leal foo(%reg),%eax; call ___tls_get_addr
2030 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2032 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2033 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2035 unsigned char op1 = view[-1];
2036 unsigned char op2 = view[-2];
2038 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2039 op2 == 0x8d || op2 == 0x04);
2040 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2042 int roff = 5;
2044 if (op2 == 0x04)
2046 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2047 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2048 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2049 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2050 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2052 else
2054 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2055 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2056 if (rel.get_r_offset() + 9 < view_size
2057 && view[9] == 0x90)
2059 // There is a trailing nop. Use the size byte subl.
2060 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2061 roff = 6;
2063 else
2065 // Use the five byte subl.
2066 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2070 value = tls_segment->memsz() - value;
2071 Relocate_functions<32, false>::rel32(view + roff, value);
2073 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2074 // We can skip it.
2075 this->skip_call_tls_get_addr_ = true;
2078 // Do a relocation in which we convert a TLS General-Dynamic to an
2079 // Initial-Exec.
2081 inline void
2082 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2083 size_t relnum,
2084 Output_segment*,
2085 const elfcpp::Rel<32, false>& rel,
2086 unsigned int,
2087 elfcpp::Elf_types<32>::Elf_Addr value,
2088 unsigned char* view,
2089 section_size_type view_size)
2091 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2092 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2094 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2095 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2097 unsigned char op1 = view[-1];
2098 unsigned char op2 = view[-2];
2100 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2101 op2 == 0x8d || op2 == 0x04);
2102 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2104 int roff = 5;
2106 // FIXME: For now, support only the first (SIB) form.
2107 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2109 if (op2 == 0x04)
2111 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2112 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2113 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2114 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2115 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2117 else
2119 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2120 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2121 if (rel.get_r_offset() + 9 < view_size
2122 && view[9] == 0x90)
2124 // FIXME: This is not the right instruction sequence.
2125 // There is a trailing nop. Use the size byte subl.
2126 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2127 roff = 6;
2129 else
2131 // FIXME: This is not the right instruction sequence.
2132 // Use the five byte subl.
2133 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2137 Relocate_functions<32, false>::rel32(view + roff, value);
2139 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2140 // We can skip it.
2141 this->skip_call_tls_get_addr_ = true;
2144 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2145 // General-Dynamic to a Local-Exec.
2147 inline void
2148 Target_i386::Relocate::tls_desc_gd_to_le(
2149 const Relocate_info<32, false>* relinfo,
2150 size_t relnum,
2151 Output_segment* tls_segment,
2152 const elfcpp::Rel<32, false>& rel,
2153 unsigned int r_type,
2154 elfcpp::Elf_types<32>::Elf_Addr value,
2155 unsigned char* view,
2156 section_size_type view_size)
2158 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2160 // leal foo@TLSDESC(%ebx), %eax
2161 // ==> leal foo@NTPOFF, %eax
2162 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2163 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2164 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2165 view[-2] == 0x8d && view[-1] == 0x83);
2166 view[-1] = 0x05;
2167 value -= tls_segment->memsz();
2168 Relocate_functions<32, false>::rel32(view, value);
2170 else
2172 // call *foo@TLSCALL(%eax)
2173 // ==> nop; nop
2174 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2175 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2176 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2177 view[0] == 0xff && view[1] == 0x10);
2178 view[0] = 0x66;
2179 view[1] = 0x90;
2183 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2184 // General-Dynamic to an Initial-Exec.
2186 inline void
2187 Target_i386::Relocate::tls_desc_gd_to_ie(
2188 const Relocate_info<32, false>* relinfo,
2189 size_t relnum,
2190 Output_segment*,
2191 const elfcpp::Rel<32, false>& rel,
2192 unsigned int r_type,
2193 elfcpp::Elf_types<32>::Elf_Addr value,
2194 unsigned char* view,
2195 section_size_type view_size)
2197 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2199 // leal foo@TLSDESC(%ebx), %eax
2200 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2201 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2202 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2203 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2204 view[-2] == 0x8d && view[-1] == 0x83);
2205 view[-2] = 0x8b;
2206 Relocate_functions<32, false>::rel32(view, value);
2208 else
2210 // call *foo@TLSCALL(%eax)
2211 // ==> nop; nop
2212 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2213 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2214 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2215 view[0] == 0xff && view[1] == 0x10);
2216 view[0] = 0x66;
2217 view[1] = 0x90;
2221 // Do a relocation in which we convert a TLS Local-Dynamic to a
2222 // Local-Exec.
2224 inline void
2225 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2226 size_t relnum,
2227 Output_segment*,
2228 const elfcpp::Rel<32, false>& rel,
2229 unsigned int,
2230 elfcpp::Elf_types<32>::Elf_Addr,
2231 unsigned char* view,
2232 section_size_type view_size)
2234 // leal foo(%reg), %eax; call ___tls_get_addr
2235 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2237 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2238 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2240 // FIXME: Does this test really always pass?
2241 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2242 view[-2] == 0x8d && view[-1] == 0x83);
2244 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2246 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2248 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2249 // We can skip it.
2250 this->skip_call_tls_get_addr_ = true;
2253 // Do a relocation in which we convert a TLS Initial-Exec to a
2254 // Local-Exec.
2256 inline void
2257 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2258 size_t relnum,
2259 Output_segment* tls_segment,
2260 const elfcpp::Rel<32, false>& rel,
2261 unsigned int r_type,
2262 elfcpp::Elf_types<32>::Elf_Addr value,
2263 unsigned char* view,
2264 section_size_type view_size)
2266 // We have to actually change the instructions, which means that we
2267 // need to examine the opcodes to figure out which instruction we
2268 // are looking at.
2269 if (r_type == elfcpp::R_386_TLS_IE)
2271 // movl %gs:XX,%eax ==> movl $YY,%eax
2272 // movl %gs:XX,%reg ==> movl $YY,%reg
2273 // addl %gs:XX,%reg ==> addl $YY,%reg
2274 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2275 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2277 unsigned char op1 = view[-1];
2278 if (op1 == 0xa1)
2280 // movl XX,%eax ==> movl $YY,%eax
2281 view[-1] = 0xb8;
2283 else
2285 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2287 unsigned char op2 = view[-2];
2288 if (op2 == 0x8b)
2290 // movl XX,%reg ==> movl $YY,%reg
2291 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2292 (op1 & 0xc7) == 0x05);
2293 view[-2] = 0xc7;
2294 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2296 else if (op2 == 0x03)
2298 // addl XX,%reg ==> addl $YY,%reg
2299 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2300 (op1 & 0xc7) == 0x05);
2301 view[-2] = 0x81;
2302 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2304 else
2305 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2308 else
2310 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2311 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2312 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2313 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2314 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2316 unsigned char op1 = view[-1];
2317 unsigned char op2 = view[-2];
2318 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2319 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2320 if (op2 == 0x8b)
2322 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2323 view[-2] = 0xc7;
2324 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2326 else if (op2 == 0x2b)
2328 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2329 view[-2] = 0x81;
2330 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2332 else if (op2 == 0x03)
2334 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2335 view[-2] = 0x81;
2336 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2338 else
2339 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2342 value = tls_segment->memsz() - value;
2343 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2344 value = - value;
2346 Relocate_functions<32, false>::rel32(view, value);
2349 // Relocate section data.
2351 void
2352 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2353 unsigned int sh_type,
2354 const unsigned char* prelocs,
2355 size_t reloc_count,
2356 Output_section* output_section,
2357 bool needs_special_offset_handling,
2358 unsigned char* view,
2359 elfcpp::Elf_types<32>::Elf_Addr address,
2360 section_size_type view_size)
2362 gold_assert(sh_type == elfcpp::SHT_REL);
2364 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2365 Target_i386::Relocate>(
2366 relinfo,
2367 this,
2368 prelocs,
2369 reloc_count,
2370 output_section,
2371 needs_special_offset_handling,
2372 view,
2373 address,
2374 view_size);
2377 // Return the size of a relocation while scanning during a relocatable
2378 // link.
2380 unsigned int
2381 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2382 unsigned int r_type,
2383 Relobj* object)
2385 switch (r_type)
2387 case elfcpp::R_386_NONE:
2388 case elfcpp::R_386_GNU_VTINHERIT:
2389 case elfcpp::R_386_GNU_VTENTRY:
2390 case elfcpp::R_386_TLS_GD: // Global-dynamic
2391 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2392 case elfcpp::R_386_TLS_DESC_CALL:
2393 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2394 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2395 case elfcpp::R_386_TLS_IE: // Initial-exec
2396 case elfcpp::R_386_TLS_IE_32:
2397 case elfcpp::R_386_TLS_GOTIE:
2398 case elfcpp::R_386_TLS_LE: // Local-exec
2399 case elfcpp::R_386_TLS_LE_32:
2400 return 0;
2402 case elfcpp::R_386_32:
2403 case elfcpp::R_386_PC32:
2404 case elfcpp::R_386_GOT32:
2405 case elfcpp::R_386_PLT32:
2406 case elfcpp::R_386_GOTOFF:
2407 case elfcpp::R_386_GOTPC:
2408 return 4;
2410 case elfcpp::R_386_16:
2411 case elfcpp::R_386_PC16:
2412 return 2;
2414 case elfcpp::R_386_8:
2415 case elfcpp::R_386_PC8:
2416 return 1;
2418 // These are relocations which should only be seen by the
2419 // dynamic linker, and should never be seen here.
2420 case elfcpp::R_386_COPY:
2421 case elfcpp::R_386_GLOB_DAT:
2422 case elfcpp::R_386_JUMP_SLOT:
2423 case elfcpp::R_386_RELATIVE:
2424 case elfcpp::R_386_TLS_TPOFF:
2425 case elfcpp::R_386_TLS_DTPMOD32:
2426 case elfcpp::R_386_TLS_DTPOFF32:
2427 case elfcpp::R_386_TLS_TPOFF32:
2428 case elfcpp::R_386_TLS_DESC:
2429 object->error(_("unexpected reloc %u in object file"), r_type);
2430 return 0;
2432 case elfcpp::R_386_32PLT:
2433 case elfcpp::R_386_TLS_GD_32:
2434 case elfcpp::R_386_TLS_GD_PUSH:
2435 case elfcpp::R_386_TLS_GD_CALL:
2436 case elfcpp::R_386_TLS_GD_POP:
2437 case elfcpp::R_386_TLS_LDM_32:
2438 case elfcpp::R_386_TLS_LDM_PUSH:
2439 case elfcpp::R_386_TLS_LDM_CALL:
2440 case elfcpp::R_386_TLS_LDM_POP:
2441 case elfcpp::R_386_USED_BY_INTEL_200:
2442 default:
2443 object->error(_("unsupported reloc %u in object file"), r_type);
2444 return 0;
2448 // Scan the relocs during a relocatable link.
2450 void
2451 Target_i386::scan_relocatable_relocs(const General_options& options,
2452 Symbol_table* symtab,
2453 Layout* layout,
2454 Sized_relobj<32, false>* object,
2455 unsigned int data_shndx,
2456 unsigned int sh_type,
2457 const unsigned char* prelocs,
2458 size_t reloc_count,
2459 Output_section* output_section,
2460 bool needs_special_offset_handling,
2461 size_t local_symbol_count,
2462 const unsigned char* plocal_symbols,
2463 Relocatable_relocs* rr)
2465 gold_assert(sh_type == elfcpp::SHT_REL);
2467 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2468 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2470 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2471 Scan_relocatable_relocs>(
2472 options,
2473 symtab,
2474 layout,
2475 object,
2476 data_shndx,
2477 prelocs,
2478 reloc_count,
2479 output_section,
2480 needs_special_offset_handling,
2481 local_symbol_count,
2482 plocal_symbols,
2483 rr);
2486 // Relocate a section during a relocatable link.
2488 void
2489 Target_i386::relocate_for_relocatable(
2490 const Relocate_info<32, false>* relinfo,
2491 unsigned int sh_type,
2492 const unsigned char* prelocs,
2493 size_t reloc_count,
2494 Output_section* output_section,
2495 off_t offset_in_output_section,
2496 const Relocatable_relocs* rr,
2497 unsigned char* view,
2498 elfcpp::Elf_types<32>::Elf_Addr view_address,
2499 section_size_type view_size,
2500 unsigned char* reloc_view,
2501 section_size_type reloc_view_size)
2503 gold_assert(sh_type == elfcpp::SHT_REL);
2505 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2506 relinfo,
2507 prelocs,
2508 reloc_count,
2509 output_section,
2510 offset_in_output_section,
2512 view,
2513 view_address,
2514 view_size,
2515 reloc_view,
2516 reloc_view_size);
2519 // Return the value to use for a dynamic which requires special
2520 // treatment. This is how we support equality comparisons of function
2521 // pointers across shared library boundaries, as described in the
2522 // processor specific ABI supplement.
2524 uint64_t
2525 Target_i386::do_dynsym_value(const Symbol* gsym) const
2527 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2528 return this->plt_section()->address() + gsym->plt_offset();
2531 // Return a string used to fill a code section with nops to take up
2532 // the specified length.
2534 std::string
2535 Target_i386::do_code_fill(section_size_type length) const
2537 if (length >= 16)
2539 // Build a jmp instruction to skip over the bytes.
2540 unsigned char jmp[5];
2541 jmp[0] = 0xe9;
2542 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2543 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2544 + std::string(length - 5, '\0'));
2547 // Nop sequences of various lengths.
2548 const char nop1[1] = { 0x90 }; // nop
2549 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2550 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2551 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2552 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2553 0x00 }; // leal 0(%esi,1),%esi
2554 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2555 0x00, 0x00 };
2556 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2557 0x00, 0x00, 0x00 };
2558 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2559 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2560 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2561 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2562 0x00 };
2563 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2564 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2565 0x00, 0x00 };
2566 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2567 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2568 0x00, 0x00, 0x00 };
2569 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2570 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2571 0x00, 0x00, 0x00, 0x00 };
2572 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2573 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2574 0x27, 0x00, 0x00, 0x00,
2575 0x00 };
2576 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2577 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2578 0xbc, 0x27, 0x00, 0x00,
2579 0x00, 0x00 };
2580 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2581 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2582 0x90, 0x90, 0x90, 0x90,
2583 0x90, 0x90, 0x90 };
2585 const char* nops[16] = {
2586 NULL,
2587 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2588 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2591 return std::string(nops[length], length);
2594 // The selector for i386 object files.
2596 class Target_selector_i386 : public Target_selector
2598 public:
2599 Target_selector_i386()
2600 : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
2603 Target*
2604 do_instantiate_target()
2605 { return new Target_i386(); }
2608 Target_selector_i386 target_selector_i386;
2610 } // End anonymous namespace.