2008-01-23 H.J. Lu <hongjiu.lu@intel.com>
[binutils.git] / gold / i386.cc
blobfe8341de727c341efe521c283e1c7fa0484b9363
1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007 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 "target.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
38 #include "tls.h"
40 namespace
43 using namespace gold;
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>
54 public:
55 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
57 Target_i386()
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)
61 { }
63 // Scan the relocations to look for symbol adjustments.
64 void
65 scan_relocs(const General_options& options,
66 Symbol_table* symtab,
67 Layout* layout,
68 Sized_relobj<32, false>* object,
69 unsigned int data_shndx,
70 unsigned int sh_type,
71 const unsigned char* prelocs,
72 size_t reloc_count,
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.
79 void
80 do_finalize_sections(Layout*);
82 // Return the value to use for a dynamic which requires special
83 // treatment.
84 uint64_t
85 do_dynsym_value(const Symbol*) const;
87 // Relocate a section.
88 void
89 relocate_section(const Relocate_info<32, false>*,
90 unsigned int sh_type,
91 const unsigned char* prelocs,
92 size_t reloc_count,
93 Output_section* output_section,
94 bool needs_special_offset_handling,
95 unsigned char* view,
96 elfcpp::Elf_types<32>::Elf_Addr view_address,
97 section_size_type view_size);
99 // Return a string used to fill a code section with nops.
100 std::string
101 do_code_fill(section_size_type length);
103 // Return whether SYM is defined by the ABI.
104 bool
105 do_is_defined_by_abi(Symbol* sym) const
106 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
108 // Return the size of the GOT section.
109 section_size_type
110 got_size()
112 gold_assert(this->got_ != NULL);
113 return this->got_->data_size();
116 private:
117 // The class which scans relocations.
118 struct Scan
120 inline void
121 local(const General_options& options, Symbol_table* symtab,
122 Layout* layout, Target_i386* target,
123 Sized_relobj<32, false>* object,
124 unsigned int data_shndx,
125 Output_section* output_section,
126 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
127 const elfcpp::Sym<32, false>& lsym);
129 inline void
130 global(const General_options& options, Symbol_table* symtab,
131 Layout* layout, Target_i386* target,
132 Sized_relobj<32, false>* object,
133 unsigned int data_shndx,
134 Output_section* output_section,
135 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
136 Symbol* gsym);
138 static void
139 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
141 static void
142 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
143 Symbol*);
146 // The class which implements relocation.
147 class Relocate
149 public:
150 Relocate()
151 : skip_call_tls_get_addr_(false),
152 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
155 ~Relocate()
157 if (this->skip_call_tls_get_addr_)
159 // FIXME: This needs to specify the location somehow.
160 gold_error(_("missing expected TLS relocation"));
164 // Return whether the static relocation needs to be applied.
165 inline bool
166 should_apply_static_reloc(const Sized_symbol<32>* gsym,
167 int ref_flags,
168 bool is_32bit);
170 // Do a relocation. Return false if the caller should not issue
171 // any warnings about this relocation.
172 inline bool
173 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
174 const elfcpp::Rel<32, false>&,
175 unsigned int r_type, const Sized_symbol<32>*,
176 const Symbol_value<32>*,
177 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
178 section_size_type);
180 private:
181 // Do a TLS relocation.
182 inline void
183 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
184 size_t relnum, const elfcpp::Rel<32, false>&,
185 unsigned int r_type, const Sized_symbol<32>*,
186 const Symbol_value<32>*,
187 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
188 section_size_type);
190 // Do a TLS General-Dynamic to Initial-Exec transition.
191 inline void
192 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
193 Output_segment* tls_segment,
194 const elfcpp::Rel<32, false>&, unsigned int r_type,
195 elfcpp::Elf_types<32>::Elf_Addr value,
196 unsigned char* view,
197 section_size_type view_size);
199 // Do a TLS General-Dynamic to Local-Exec transition.
200 inline void
201 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
202 Output_segment* tls_segment,
203 const elfcpp::Rel<32, false>&, unsigned int r_type,
204 elfcpp::Elf_types<32>::Elf_Addr value,
205 unsigned char* view,
206 section_size_type view_size);
208 // Do a TLS Local-Dynamic to Local-Exec transition.
209 inline void
210 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
211 Output_segment* tls_segment,
212 const elfcpp::Rel<32, false>&, unsigned int r_type,
213 elfcpp::Elf_types<32>::Elf_Addr value,
214 unsigned char* view,
215 section_size_type view_size);
217 // Do a TLS Initial-Exec to Local-Exec transition.
218 static inline void
219 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
220 Output_segment* tls_segment,
221 const elfcpp::Rel<32, false>&, unsigned int r_type,
222 elfcpp::Elf_types<32>::Elf_Addr value,
223 unsigned char* view,
224 section_size_type view_size);
226 // We need to keep track of which type of local dynamic relocation
227 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
228 enum Local_dynamic_type
230 LOCAL_DYNAMIC_NONE,
231 LOCAL_DYNAMIC_SUN,
232 LOCAL_DYNAMIC_GNU
235 // This is set if we should skip the next reloc, which should be a
236 // PLT32 reloc against ___tls_get_addr.
237 bool skip_call_tls_get_addr_;
238 // The type of local dynamic relocation we have seen in the section
239 // being relocated, if any.
240 Local_dynamic_type local_dynamic_type_;
243 // Adjust TLS relocation type based on the options and whether this
244 // is a local symbol.
245 static tls::Tls_optimization
246 optimize_tls_reloc(bool is_final, int r_type);
248 // Get the GOT section, creating it if necessary.
249 Output_data_got<32, false>*
250 got_section(Symbol_table*, Layout*);
252 // Get the GOT PLT section.
253 Output_data_space*
254 got_plt_section() const
256 gold_assert(this->got_plt_ != NULL);
257 return this->got_plt_;
260 // Create a PLT entry for a global symbol.
261 void
262 make_plt_entry(Symbol_table*, Layout*, Symbol*);
264 // Create a GOT entry for the TLS module index.
265 unsigned int
266 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
267 Sized_relobj<32, false>* object);
269 // Get the PLT section.
270 const Output_data_plt_i386*
271 plt_section() const
273 gold_assert(this->plt_ != NULL);
274 return this->plt_;
277 // Get the dynamic reloc section, creating it if necessary.
278 Reloc_section*
279 rel_dyn_section(Layout*);
281 // Return true if the symbol may need a COPY relocation.
282 // References from an executable object to non-function symbols
283 // defined in a dynamic object may need a COPY relocation.
284 bool
285 may_need_copy_reloc(Symbol* gsym)
287 return (!parameters->output_is_shared()
288 && gsym->is_from_dynobj()
289 && gsym->type() != elfcpp::STT_FUNC);
292 // Copy a relocation against a global symbol.
293 void
294 copy_reloc(const General_options*, Symbol_table*, Layout*,
295 Sized_relobj<32, false>*, unsigned int,
296 Output_section*, Symbol*, const elfcpp::Rel<32, false>&);
298 // Information about this specific target which we pass to the
299 // general Target structure.
300 static const Target::Target_info i386_info;
302 // The GOT section.
303 Output_data_got<32, false>* got_;
304 // The PLT section.
305 Output_data_plt_i386* plt_;
306 // The GOT PLT section.
307 Output_data_space* got_plt_;
308 // The dynamic reloc section.
309 Reloc_section* rel_dyn_;
310 // Relocs saved to avoid a COPY reloc.
311 Copy_relocs<32, false>* copy_relocs_;
312 // Space for variables copied with a COPY reloc.
313 Output_data_space* dynbss_;
314 // Offset of the GOT entry for the TLS module index;
315 unsigned int got_mod_index_offset_;
318 const Target::Target_info Target_i386::i386_info =
320 32, // size
321 false, // is_big_endian
322 elfcpp::EM_386, // machine_code
323 false, // has_make_symbol
324 false, // has_resolve
325 true, // has_code_fill
326 true, // is_default_stack_executable
327 "/usr/lib/libc.so.1", // dynamic_linker
328 0x08048000, // default_text_segment_address
329 0x1000, // abi_pagesize
330 0x1000 // common_pagesize
333 // Get the GOT section, creating it if necessary.
335 Output_data_got<32, false>*
336 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
338 if (this->got_ == NULL)
340 gold_assert(symtab != NULL && layout != NULL);
342 this->got_ = new Output_data_got<32, false>();
344 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
345 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
346 this->got_);
348 // The old GNU linker creates a .got.plt section. We just
349 // create another set of data in the .got section. Note that we
350 // always create a PLT if we create a GOT, although the PLT
351 // might be empty.
352 this->got_plt_ = new Output_data_space(4);
353 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
354 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
355 this->got_plt_);
357 // The first three entries are reserved.
358 this->got_plt_->set_current_data_size(3 * 4);
360 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
361 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
362 this->got_plt_,
363 0, 0, elfcpp::STT_OBJECT,
364 elfcpp::STB_LOCAL,
365 elfcpp::STV_HIDDEN, 0,
366 false, false);
369 return this->got_;
372 // Get the dynamic reloc section, creating it if necessary.
374 Target_i386::Reloc_section*
375 Target_i386::rel_dyn_section(Layout* layout)
377 if (this->rel_dyn_ == NULL)
379 gold_assert(layout != NULL);
380 this->rel_dyn_ = new Reloc_section();
381 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
382 elfcpp::SHF_ALLOC, this->rel_dyn_);
384 return this->rel_dyn_;
387 // A class to handle the PLT data.
389 class Output_data_plt_i386 : public Output_section_data
391 public:
392 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
394 Output_data_plt_i386(Layout*, Output_data_space*);
396 // Add an entry to the PLT.
397 void
398 add_entry(Symbol* gsym);
400 // Return the .rel.plt section data.
401 const Reloc_section*
402 rel_plt() const
403 { return this->rel_; }
405 protected:
406 void
407 do_adjust_output_section(Output_section* os);
409 private:
410 // The size of an entry in the PLT.
411 static const int plt_entry_size = 16;
413 // The first entry in the PLT for an executable.
414 static unsigned char exec_first_plt_entry[plt_entry_size];
416 // The first entry in the PLT for a shared object.
417 static unsigned char dyn_first_plt_entry[plt_entry_size];
419 // Other entries in the PLT for an executable.
420 static unsigned char exec_plt_entry[plt_entry_size];
422 // Other entries in the PLT for a shared object.
423 static unsigned char dyn_plt_entry[plt_entry_size];
425 // Set the final size.
426 void
427 set_final_data_size()
428 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
430 // Write out the PLT data.
431 void
432 do_write(Output_file*);
434 // The reloc section.
435 Reloc_section* rel_;
436 // The .got.plt section.
437 Output_data_space* got_plt_;
438 // The number of PLT entries.
439 unsigned int count_;
442 // Create the PLT section. The ordinary .got section is an argument,
443 // since we need to refer to the start. We also create our own .got
444 // section just for PLT entries.
446 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
447 Output_data_space* got_plt)
448 : Output_section_data(4), got_plt_(got_plt), count_(0)
450 this->rel_ = new Reloc_section();
451 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
452 elfcpp::SHF_ALLOC, this->rel_);
455 void
456 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
458 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
459 // linker, and so do we.
460 os->set_entsize(4);
463 // Add an entry to the PLT.
465 void
466 Output_data_plt_i386::add_entry(Symbol* gsym)
468 gold_assert(!gsym->has_plt_offset());
470 // Note that when setting the PLT offset we skip the initial
471 // reserved PLT entry.
472 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
474 ++this->count_;
476 section_offset_type got_offset = this->got_plt_->current_data_size();
478 // Every PLT entry needs a GOT entry which points back to the PLT
479 // entry (this will be changed by the dynamic linker, normally
480 // lazily when the function is called).
481 this->got_plt_->set_current_data_size(got_offset + 4);
483 // Every PLT entry needs a reloc.
484 gsym->set_needs_dynsym_entry();
485 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
486 got_offset);
488 // Note that we don't need to save the symbol. The contents of the
489 // PLT are independent of which symbols are used. The symbols only
490 // appear in the relocations.
493 // The first entry in the PLT for an executable.
495 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
497 0xff, 0x35, // pushl contents of memory address
498 0, 0, 0, 0, // replaced with address of .got + 4
499 0xff, 0x25, // jmp indirect
500 0, 0, 0, 0, // replaced with address of .got + 8
501 0, 0, 0, 0 // unused
504 // The first entry in the PLT for a shared object.
506 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
508 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
509 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
510 0, 0, 0, 0 // unused
513 // Subsequent entries in the PLT for an executable.
515 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
517 0xff, 0x25, // jmp indirect
518 0, 0, 0, 0, // replaced with address of symbol in .got
519 0x68, // pushl immediate
520 0, 0, 0, 0, // replaced with offset into relocation table
521 0xe9, // jmp relative
522 0, 0, 0, 0 // replaced with offset to start of .plt
525 // Subsequent entries in the PLT for a shared object.
527 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
529 0xff, 0xa3, // jmp *offset(%ebx)
530 0, 0, 0, 0, // replaced with offset of symbol in .got
531 0x68, // pushl immediate
532 0, 0, 0, 0, // replaced with offset into relocation table
533 0xe9, // jmp relative
534 0, 0, 0, 0 // replaced with offset to start of .plt
537 // Write out the PLT. This uses the hand-coded instructions above,
538 // and adjusts them as needed. This is all specified by the i386 ELF
539 // Processor Supplement.
541 void
542 Output_data_plt_i386::do_write(Output_file* of)
544 const off_t offset = this->offset();
545 const section_size_type oview_size =
546 convert_to_section_size_type(this->data_size());
547 unsigned char* const oview = of->get_output_view(offset, oview_size);
549 const off_t got_file_offset = this->got_plt_->offset();
550 const section_size_type got_size =
551 convert_to_section_size_type(this->got_plt_->data_size());
552 unsigned char* const got_view = of->get_output_view(got_file_offset,
553 got_size);
555 unsigned char* pov = oview;
557 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
558 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
560 if (parameters->output_is_shared())
561 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
562 else
564 memcpy(pov, exec_first_plt_entry, plt_entry_size);
565 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
566 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
568 pov += plt_entry_size;
570 unsigned char* got_pov = got_view;
572 memset(got_pov, 0, 12);
573 got_pov += 12;
575 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
577 unsigned int plt_offset = plt_entry_size;
578 unsigned int plt_rel_offset = 0;
579 unsigned int got_offset = 12;
580 const unsigned int count = this->count_;
581 for (unsigned int i = 0;
582 i < count;
583 ++i,
584 pov += plt_entry_size,
585 got_pov += 4,
586 plt_offset += plt_entry_size,
587 plt_rel_offset += rel_size,
588 got_offset += 4)
590 // Set and adjust the PLT entry itself.
592 if (parameters->output_is_shared())
594 memcpy(pov, dyn_plt_entry, plt_entry_size);
595 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
597 else
599 memcpy(pov, exec_plt_entry, plt_entry_size);
600 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
601 (got_address
602 + got_offset));
605 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
606 elfcpp::Swap<32, false>::writeval(pov + 12,
607 - (plt_offset + plt_entry_size));
609 // Set the entry in the GOT.
610 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
613 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
614 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
616 of->write_output_view(offset, oview_size, oview);
617 of->write_output_view(got_file_offset, got_size, got_view);
620 // Create a PLT entry for a global symbol.
622 void
623 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
625 if (gsym->has_plt_offset())
626 return;
628 if (this->plt_ == NULL)
630 // Create the GOT sections first.
631 this->got_section(symtab, layout);
633 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
634 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
635 (elfcpp::SHF_ALLOC
636 | elfcpp::SHF_EXECINSTR),
637 this->plt_);
640 this->plt_->add_entry(gsym);
643 // Create a GOT entry for the TLS module index.
645 unsigned int
646 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
647 Sized_relobj<32, false>* object)
649 if (this->got_mod_index_offset_ == -1U)
651 gold_assert(symtab != NULL && layout != NULL && object != NULL);
652 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
653 Output_data_got<32, false>* got = this->got_section(symtab, layout);
654 unsigned int got_offset = got->add_constant(0);
655 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
656 got_offset);
657 got->add_constant(0);
658 this->got_mod_index_offset_ = got_offset;
660 return this->got_mod_index_offset_;
663 // Handle a relocation against a non-function symbol defined in a
664 // dynamic object. The traditional way to handle this is to generate
665 // a COPY relocation to copy the variable at runtime from the shared
666 // object into the executable's data segment. However, this is
667 // undesirable in general, as if the size of the object changes in the
668 // dynamic object, the executable will no longer work correctly. If
669 // this relocation is in a writable section, then we can create a
670 // dynamic reloc and the dynamic linker will resolve it to the correct
671 // address at runtime. However, we do not want do that if the
672 // relocation is in a read-only section, as it would prevent the
673 // readonly segment from being shared. And if we have to eventually
674 // generate a COPY reloc, then any dynamic relocations will be
675 // useless. So this means that if this is a writable section, we need
676 // to save the relocation until we see whether we have to create a
677 // COPY relocation for this symbol for any other relocation.
679 void
680 Target_i386::copy_reloc(const General_options* options,
681 Symbol_table* symtab,
682 Layout* layout,
683 Sized_relobj<32, false>* object,
684 unsigned int data_shndx,
685 Output_section* output_section,
686 Symbol* gsym,
687 const elfcpp::Rel<32, false>& rel)
689 Sized_symbol<32>* ssym;
690 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
691 SELECT_SIZE(32));
693 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
694 data_shndx, ssym))
696 // So far we do not need a COPY reloc. Save this relocation.
697 // If it turns out that we never need a COPY reloc for this
698 // symbol, then we will emit the relocation.
699 if (this->copy_relocs_ == NULL)
700 this->copy_relocs_ = new Copy_relocs<32, false>();
701 this->copy_relocs_->save(ssym, object, data_shndx, output_section, rel);
703 else
705 // Allocate space for this symbol in the .bss section.
707 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
709 // There is no defined way to determine the required alignment
710 // of the symbol. We pick the alignment based on the size. We
711 // set an arbitrary maximum of 256.
712 unsigned int align;
713 for (align = 1; align < 512; align <<= 1)
714 if ((symsize & align) != 0)
715 break;
717 if (this->dynbss_ == NULL)
719 this->dynbss_ = new Output_data_space(align);
720 layout->add_output_section_data(".bss",
721 elfcpp::SHT_NOBITS,
722 (elfcpp::SHF_ALLOC
723 | elfcpp::SHF_WRITE),
724 this->dynbss_);
727 Output_data_space* dynbss = this->dynbss_;
729 if (align > dynbss->addralign())
730 dynbss->set_space_alignment(align);
732 section_size_type dynbss_size =
733 convert_to_section_size_type(dynbss->current_data_size());
734 dynbss_size = align_address(dynbss_size, align);
735 section_size_type offset = dynbss_size;
736 dynbss->set_current_data_size(dynbss_size + symsize);
738 symtab->define_with_copy_reloc(this, ssym, dynbss, offset);
740 // Add the COPY reloc.
741 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
742 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
746 // Optimize the TLS relocation type based on what we know about the
747 // symbol. IS_FINAL is true if the final address of this symbol is
748 // known at link time.
750 tls::Tls_optimization
751 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
753 // If we are generating a shared library, then we can't do anything
754 // in the linker.
755 if (parameters->output_is_shared())
756 return tls::TLSOPT_NONE;
758 switch (r_type)
760 case elfcpp::R_386_TLS_GD:
761 case elfcpp::R_386_TLS_GOTDESC:
762 case elfcpp::R_386_TLS_DESC_CALL:
763 // These are General-Dynamic which permits fully general TLS
764 // access. Since we know that we are generating an executable,
765 // we can convert this to Initial-Exec. If we also know that
766 // this is a local symbol, we can further switch to Local-Exec.
767 if (is_final)
768 return tls::TLSOPT_TO_LE;
769 return tls::TLSOPT_TO_IE;
771 case elfcpp::R_386_TLS_LDM:
772 // This is Local-Dynamic, which refers to a local symbol in the
773 // dynamic TLS block. Since we know that we generating an
774 // executable, we can switch to Local-Exec.
775 return tls::TLSOPT_TO_LE;
777 case elfcpp::R_386_TLS_LDO_32:
778 // Another type of Local-Dynamic relocation.
779 return tls::TLSOPT_TO_LE;
781 case elfcpp::R_386_TLS_IE:
782 case elfcpp::R_386_TLS_GOTIE:
783 case elfcpp::R_386_TLS_IE_32:
784 // These are Initial-Exec relocs which get the thread offset
785 // from the GOT. If we know that we are linking against the
786 // local symbol, we can switch to Local-Exec, which links the
787 // thread offset into the instruction.
788 if (is_final)
789 return tls::TLSOPT_TO_LE;
790 return tls::TLSOPT_NONE;
792 case elfcpp::R_386_TLS_LE:
793 case elfcpp::R_386_TLS_LE_32:
794 // When we already have Local-Exec, there is nothing further we
795 // can do.
796 return tls::TLSOPT_NONE;
798 default:
799 gold_unreachable();
803 // Report an unsupported relocation against a local symbol.
805 void
806 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
807 unsigned int r_type)
809 gold_error(_("%s: unsupported reloc %u against local symbol"),
810 object->name().c_str(), r_type);
813 // Scan a relocation for a local symbol.
815 inline void
816 Target_i386::Scan::local(const General_options&,
817 Symbol_table* symtab,
818 Layout* layout,
819 Target_i386* target,
820 Sized_relobj<32, false>* object,
821 unsigned int data_shndx,
822 Output_section* output_section,
823 const elfcpp::Rel<32, false>& reloc,
824 unsigned int r_type,
825 const elfcpp::Sym<32, false>& lsym)
827 switch (r_type)
829 case elfcpp::R_386_NONE:
830 case elfcpp::R_386_GNU_VTINHERIT:
831 case elfcpp::R_386_GNU_VTENTRY:
832 break;
834 case elfcpp::R_386_32:
835 // If building a shared library (or a position-independent
836 // executable), we need to create a dynamic relocation for
837 // this location. The relocation applied at link time will
838 // apply the link-time value, so we flag the location with
839 // an R_386_RELATIVE relocation so the dynamic loader can
840 // relocate it easily.
841 if (parameters->output_is_position_independent())
843 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
844 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
845 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
846 output_section, data_shndx,
847 reloc.get_r_offset());
849 break;
851 case elfcpp::R_386_16:
852 case elfcpp::R_386_8:
853 // If building a shared library (or a position-independent
854 // executable), we need to create a dynamic relocation for
855 // this location. Because the addend needs to remain in the
856 // data section, we need to be careful not to apply this
857 // relocation statically.
858 if (parameters->output_is_position_independent())
860 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
861 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
862 rel_dyn->add_local(object, r_sym, r_type, output_section, data_shndx,
863 reloc.get_r_offset());
865 break;
867 case elfcpp::R_386_PC32:
868 case elfcpp::R_386_PC16:
869 case elfcpp::R_386_PC8:
870 break;
872 case elfcpp::R_386_PLT32:
873 // Since we know this is a local symbol, we can handle this as a
874 // PC32 reloc.
875 break;
877 case elfcpp::R_386_GOTOFF:
878 case elfcpp::R_386_GOTPC:
879 // We need a GOT section.
880 target->got_section(symtab, layout);
881 break;
883 case elfcpp::R_386_GOT32:
885 // The symbol requires a GOT entry.
886 Output_data_got<32, false>* got = target->got_section(symtab, layout);
887 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
888 if (got->add_local(object, r_sym))
890 // If we are generating a shared object, we need to add a
891 // dynamic RELATIVE relocation for this symbol's GOT entry.
892 if (parameters->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 rel_dyn->add_local_relative(object, r_sym,
897 elfcpp::R_386_RELATIVE,
898 got,
899 object->local_got_offset(r_sym));
903 break;
905 // These are relocations which should only be seen by the
906 // dynamic linker, and should never be seen here.
907 case elfcpp::R_386_COPY:
908 case elfcpp::R_386_GLOB_DAT:
909 case elfcpp::R_386_JUMP_SLOT:
910 case elfcpp::R_386_RELATIVE:
911 case elfcpp::R_386_TLS_TPOFF:
912 case elfcpp::R_386_TLS_DTPMOD32:
913 case elfcpp::R_386_TLS_DTPOFF32:
914 case elfcpp::R_386_TLS_TPOFF32:
915 case elfcpp::R_386_TLS_DESC:
916 gold_error(_("%s: unexpected reloc %u in object file"),
917 object->name().c_str(), r_type);
918 break;
920 // These are initial TLS relocs, which are expected when
921 // linking.
922 case elfcpp::R_386_TLS_GD: // Global-dynamic
923 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
924 case elfcpp::R_386_TLS_DESC_CALL:
925 case elfcpp::R_386_TLS_LDM: // Local-dynamic
926 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
927 case elfcpp::R_386_TLS_IE: // Initial-exec
928 case elfcpp::R_386_TLS_IE_32:
929 case elfcpp::R_386_TLS_GOTIE:
930 case elfcpp::R_386_TLS_LE: // Local-exec
931 case elfcpp::R_386_TLS_LE_32:
933 bool output_is_shared = parameters->output_is_shared();
934 const tls::Tls_optimization optimized_type
935 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
936 switch (r_type)
938 case elfcpp::R_386_TLS_GD: // Global-dynamic
939 if (optimized_type == tls::TLSOPT_NONE)
941 // Create a pair of GOT entries for the module index and
942 // dtv-relative offset.
943 Output_data_got<32, false>* got
944 = target->got_section(symtab, layout);
945 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
946 got->add_local_tls_with_rel(object, r_sym,
947 lsym.get_st_shndx(), true,
948 target->rel_dyn_section(layout),
949 elfcpp::R_386_TLS_DTPMOD32);
951 else if (optimized_type != tls::TLSOPT_TO_LE)
952 unsupported_reloc_local(object, r_type);
953 break;
955 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
956 case elfcpp::R_386_TLS_DESC_CALL:
957 // FIXME: If not relaxing to LE, we need to generate
958 // a GOT entry with an R_386_TLS_DESC reloc.
959 if (optimized_type != tls::TLSOPT_TO_LE)
960 unsupported_reloc_local(object, r_type);
961 break;
963 case elfcpp::R_386_TLS_LDM: // Local-dynamic
964 if (optimized_type == tls::TLSOPT_NONE)
966 // Create a GOT entry for the module index.
967 target->got_mod_index_entry(symtab, layout, object);
969 else if (optimized_type != tls::TLSOPT_TO_LE)
970 unsupported_reloc_local(object, r_type);
971 break;
973 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
974 break;
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 layout->set_has_static_tls();
980 if (optimized_type == tls::TLSOPT_NONE)
982 // For the R_386_TLS_IE relocation, we need to create a
983 // dynamic relocation when building a shared library.
984 if (r_type == elfcpp::R_386_TLS_IE
985 && parameters->output_is_shared())
987 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
988 unsigned int r_sym
989 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
990 rel_dyn->add_local_relative(object, r_sym,
991 elfcpp::R_386_RELATIVE,
992 output_section, data_shndx,
993 reloc.get_r_offset());
995 // Create a GOT entry for the tp-relative offset.
996 Output_data_got<32, false>* got
997 = target->got_section(symtab, layout);
998 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
999 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1000 ? elfcpp::R_386_TLS_TPOFF32
1001 : elfcpp::R_386_TLS_TPOFF);
1002 got->add_local_with_rel(object, r_sym,
1003 target->rel_dyn_section(layout),
1004 dyn_r_type);
1006 else if (optimized_type != tls::TLSOPT_TO_LE)
1007 unsupported_reloc_local(object, r_type);
1008 break;
1010 case elfcpp::R_386_TLS_LE: // Local-exec
1011 case elfcpp::R_386_TLS_LE_32:
1012 layout->set_has_static_tls();
1013 if (output_is_shared)
1015 // We need to create a dynamic relocation.
1016 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1017 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1018 ? elfcpp::R_386_TLS_TPOFF32
1019 : elfcpp::R_386_TLS_TPOFF);
1020 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1021 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1022 data_shndx, reloc.get_r_offset());
1024 break;
1026 default:
1027 gold_unreachable();
1030 break;
1032 case elfcpp::R_386_32PLT:
1033 case elfcpp::R_386_TLS_GD_32:
1034 case elfcpp::R_386_TLS_GD_PUSH:
1035 case elfcpp::R_386_TLS_GD_CALL:
1036 case elfcpp::R_386_TLS_GD_POP:
1037 case elfcpp::R_386_TLS_LDM_32:
1038 case elfcpp::R_386_TLS_LDM_PUSH:
1039 case elfcpp::R_386_TLS_LDM_CALL:
1040 case elfcpp::R_386_TLS_LDM_POP:
1041 case elfcpp::R_386_USED_BY_INTEL_200:
1042 default:
1043 unsupported_reloc_local(object, r_type);
1044 break;
1048 // Report an unsupported relocation against a global symbol.
1050 void
1051 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1052 unsigned int r_type,
1053 Symbol* gsym)
1055 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1056 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1059 // Scan a relocation for a global symbol.
1061 inline void
1062 Target_i386::Scan::global(const General_options& options,
1063 Symbol_table* symtab,
1064 Layout* layout,
1065 Target_i386* target,
1066 Sized_relobj<32, false>* object,
1067 unsigned int data_shndx,
1068 Output_section* output_section,
1069 const elfcpp::Rel<32, false>& reloc,
1070 unsigned int r_type,
1071 Symbol* gsym)
1073 switch (r_type)
1075 case elfcpp::R_386_NONE:
1076 case elfcpp::R_386_GNU_VTINHERIT:
1077 case elfcpp::R_386_GNU_VTENTRY:
1078 break;
1080 case elfcpp::R_386_32:
1081 case elfcpp::R_386_16:
1082 case elfcpp::R_386_8:
1084 // Make a PLT entry if necessary.
1085 if (gsym->needs_plt_entry())
1087 target->make_plt_entry(symtab, layout, gsym);
1088 // Since this is not a PC-relative relocation, we may be
1089 // taking the address of a function. In that case we need to
1090 // set the entry in the dynamic symbol table to the address of
1091 // the PLT entry.
1092 if (gsym->is_from_dynobj())
1093 gsym->set_needs_dynsym_value();
1095 // Make a dynamic relocation if necessary.
1096 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1098 if (target->may_need_copy_reloc(gsym))
1100 target->copy_reloc(&options, symtab, layout, object,
1101 data_shndx, output_section, gsym, reloc);
1103 else if (r_type == elfcpp::R_386_32
1104 && gsym->can_use_relative_reloc(false))
1106 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1107 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1108 output_section, object,
1109 data_shndx, reloc.get_r_offset());
1111 else
1113 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1114 rel_dyn->add_global(gsym, r_type, output_section, object,
1115 data_shndx, reloc.get_r_offset());
1119 break;
1121 case elfcpp::R_386_PC32:
1122 case elfcpp::R_386_PC16:
1123 case elfcpp::R_386_PC8:
1125 // Make a PLT entry if necessary.
1126 if (gsym->needs_plt_entry())
1128 // These relocations are used for function calls only in
1129 // non-PIC code. For a 32-bit relocation in a shared library,
1130 // we'll need a text relocation anyway, so we can skip the
1131 // PLT entry and let the dynamic linker bind the call directly
1132 // to the target. For smaller relocations, we should use a
1133 // PLT entry to ensure that the call can reach.
1134 if (!parameters->output_is_shared()
1135 || r_type != elfcpp::R_386_PC32)
1136 target->make_plt_entry(symtab, layout, gsym);
1138 // Make a dynamic relocation if necessary.
1139 int flags = Symbol::NON_PIC_REF;
1140 if (gsym->type() == elfcpp::STT_FUNC)
1141 flags |= Symbol::FUNCTION_CALL;
1142 if (gsym->needs_dynamic_reloc(flags))
1144 if (target->may_need_copy_reloc(gsym))
1146 target->copy_reloc(&options, symtab, layout, object,
1147 data_shndx, output_section, gsym, reloc);
1149 else
1151 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1152 rel_dyn->add_global(gsym, r_type, output_section, object,
1153 data_shndx, reloc.get_r_offset());
1157 break;
1159 case elfcpp::R_386_GOT32:
1161 // The symbol requires a GOT entry.
1162 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1163 if (gsym->final_value_is_known())
1164 got->add_global(gsym);
1165 else
1167 // If this symbol is not fully resolved, we need to add a
1168 // GOT entry with a dynamic relocation.
1169 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1170 if (gsym->is_from_dynobj() || gsym->is_preemptible())
1171 got->add_global_with_rel(gsym, rel_dyn, elfcpp::R_386_GLOB_DAT);
1172 else
1174 if (got->add_global(gsym))
1175 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1176 got, gsym->got_offset());
1180 break;
1182 case elfcpp::R_386_PLT32:
1183 // If the symbol is fully resolved, this is just a PC32 reloc.
1184 // Otherwise we need a PLT entry.
1185 if (gsym->final_value_is_known())
1186 break;
1187 // If building a shared library, we can also skip the PLT entry
1188 // if the symbol is defined in the output file and is protected
1189 // or hidden.
1190 if (gsym->is_defined()
1191 && !gsym->is_from_dynobj()
1192 && !gsym->is_preemptible())
1193 break;
1194 target->make_plt_entry(symtab, layout, gsym);
1195 break;
1197 case elfcpp::R_386_GOTOFF:
1198 case elfcpp::R_386_GOTPC:
1199 // We need a GOT section.
1200 target->got_section(symtab, layout);
1201 break;
1203 // These are relocations which should only be seen by the
1204 // dynamic linker, and should never be seen here.
1205 case elfcpp::R_386_COPY:
1206 case elfcpp::R_386_GLOB_DAT:
1207 case elfcpp::R_386_JUMP_SLOT:
1208 case elfcpp::R_386_RELATIVE:
1209 case elfcpp::R_386_TLS_TPOFF:
1210 case elfcpp::R_386_TLS_DTPMOD32:
1211 case elfcpp::R_386_TLS_DTPOFF32:
1212 case elfcpp::R_386_TLS_TPOFF32:
1213 case elfcpp::R_386_TLS_DESC:
1214 gold_error(_("%s: unexpected reloc %u in object file"),
1215 object->name().c_str(), r_type);
1216 break;
1218 // These are initial tls relocs, which are expected when
1219 // linking.
1220 case elfcpp::R_386_TLS_GD: // Global-dynamic
1221 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1222 case elfcpp::R_386_TLS_DESC_CALL:
1223 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1224 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1225 case elfcpp::R_386_TLS_IE: // Initial-exec
1226 case elfcpp::R_386_TLS_IE_32:
1227 case elfcpp::R_386_TLS_GOTIE:
1228 case elfcpp::R_386_TLS_LE: // Local-exec
1229 case elfcpp::R_386_TLS_LE_32:
1231 const bool is_final = gsym->final_value_is_known();
1232 const tls::Tls_optimization optimized_type
1233 = Target_i386::optimize_tls_reloc(is_final, r_type);
1234 switch (r_type)
1236 case elfcpp::R_386_TLS_GD: // Global-dynamic
1237 if (optimized_type == tls::TLSOPT_NONE)
1239 // Create a pair of GOT entries for the module index and
1240 // dtv-relative offset.
1241 Output_data_got<32, false>* got
1242 = target->got_section(symtab, layout);
1243 got->add_global_tls_with_rel(gsym,
1244 target->rel_dyn_section(layout),
1245 elfcpp::R_386_TLS_DTPMOD32,
1246 elfcpp::R_386_TLS_DTPOFF32);
1248 else if (optimized_type == tls::TLSOPT_TO_IE)
1250 // Create a GOT entry for the tp-relative offset.
1251 Output_data_got<32, false>* got
1252 = target->got_section(symtab, layout);
1253 got->add_global_with_rel(gsym, target->rel_dyn_section(layout),
1254 elfcpp::R_386_TLS_TPOFF32);
1256 else if (optimized_type != tls::TLSOPT_TO_LE)
1257 unsupported_reloc_global(object, r_type, gsym);
1258 break;
1260 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1261 case elfcpp::R_386_TLS_DESC_CALL:
1262 // FIXME: If not relaxing to LE, we need to generate
1263 // a GOT entry with an R_386_TLS_DESC reloc.
1264 if (optimized_type != tls::TLSOPT_TO_LE)
1265 unsupported_reloc_global(object, r_type, gsym);
1266 unsupported_reloc_global(object, r_type, gsym);
1267 break;
1269 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1270 if (optimized_type == tls::TLSOPT_NONE)
1272 // Create a GOT entry for the module index.
1273 target->got_mod_index_entry(symtab, layout, object);
1275 else if (optimized_type != tls::TLSOPT_TO_LE)
1276 unsupported_reloc_global(object, r_type, gsym);
1277 break;
1279 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1280 break;
1282 case elfcpp::R_386_TLS_IE: // Initial-exec
1283 case elfcpp::R_386_TLS_IE_32:
1284 case elfcpp::R_386_TLS_GOTIE:
1285 layout->set_has_static_tls();
1286 if (optimized_type == tls::TLSOPT_NONE)
1288 // For the R_386_TLS_IE relocation, we need to create a
1289 // dynamic relocation when building a shared library.
1290 if (r_type == elfcpp::R_386_TLS_IE
1291 && parameters->output_is_shared())
1293 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1294 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1295 output_section, object,
1296 data_shndx,
1297 reloc.get_r_offset());
1299 // Create a GOT entry for the tp-relative offset.
1300 Output_data_got<32, false>* got
1301 = target->got_section(symtab, layout);
1302 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1303 ? elfcpp::R_386_TLS_TPOFF32
1304 : elfcpp::R_386_TLS_TPOFF);
1305 got->add_global_with_rel(gsym,
1306 target->rel_dyn_section(layout),
1307 dyn_r_type);
1309 else if (optimized_type != tls::TLSOPT_TO_LE)
1310 unsupported_reloc_global(object, r_type, gsym);
1311 break;
1313 case elfcpp::R_386_TLS_LE: // Local-exec
1314 case elfcpp::R_386_TLS_LE_32:
1315 layout->set_has_static_tls();
1316 if (parameters->output_is_shared())
1318 // We need to create a dynamic relocation.
1319 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1320 ? elfcpp::R_386_TLS_TPOFF32
1321 : elfcpp::R_386_TLS_TPOFF);
1322 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1323 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1324 data_shndx, reloc.get_r_offset());
1326 break;
1328 default:
1329 gold_unreachable();
1332 break;
1334 case elfcpp::R_386_32PLT:
1335 case elfcpp::R_386_TLS_GD_32:
1336 case elfcpp::R_386_TLS_GD_PUSH:
1337 case elfcpp::R_386_TLS_GD_CALL:
1338 case elfcpp::R_386_TLS_GD_POP:
1339 case elfcpp::R_386_TLS_LDM_32:
1340 case elfcpp::R_386_TLS_LDM_PUSH:
1341 case elfcpp::R_386_TLS_LDM_CALL:
1342 case elfcpp::R_386_TLS_LDM_POP:
1343 case elfcpp::R_386_USED_BY_INTEL_200:
1344 default:
1345 unsupported_reloc_global(object, r_type, gsym);
1346 break;
1350 // Scan relocations for a section.
1352 void
1353 Target_i386::scan_relocs(const General_options& options,
1354 Symbol_table* symtab,
1355 Layout* layout,
1356 Sized_relobj<32, false>* object,
1357 unsigned int data_shndx,
1358 unsigned int sh_type,
1359 const unsigned char* prelocs,
1360 size_t reloc_count,
1361 Output_section* output_section,
1362 bool needs_special_offset_handling,
1363 size_t local_symbol_count,
1364 const unsigned char* plocal_symbols)
1366 if (sh_type == elfcpp::SHT_RELA)
1368 gold_error(_("%s: unsupported RELA reloc section"),
1369 object->name().c_str());
1370 return;
1373 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1374 Target_i386::Scan>(
1375 options,
1376 symtab,
1377 layout,
1378 this,
1379 object,
1380 data_shndx,
1381 prelocs,
1382 reloc_count,
1383 output_section,
1384 needs_special_offset_handling,
1385 local_symbol_count,
1386 plocal_symbols);
1389 // Finalize the sections.
1391 void
1392 Target_i386::do_finalize_sections(Layout* layout)
1394 // Fill in some more dynamic tags.
1395 Output_data_dynamic* const odyn = layout->dynamic_data();
1396 if (odyn != NULL)
1398 if (this->got_plt_ != NULL)
1399 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1401 if (this->plt_ != NULL)
1403 const Output_data* od = this->plt_->rel_plt();
1404 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1405 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1406 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1409 if (this->rel_dyn_ != NULL)
1411 const Output_data* od = this->rel_dyn_;
1412 odyn->add_section_address(elfcpp::DT_REL, od);
1413 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1414 odyn->add_constant(elfcpp::DT_RELENT,
1415 elfcpp::Elf_sizes<32>::rel_size);
1418 if (!parameters->output_is_shared())
1420 // The value of the DT_DEBUG tag is filled in by the dynamic
1421 // linker at run time, and used by the debugger.
1422 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1426 // Emit any relocs we saved in an attempt to avoid generating COPY
1427 // relocs.
1428 if (this->copy_relocs_ == NULL)
1429 return;
1430 if (this->copy_relocs_->any_to_emit())
1432 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1433 this->copy_relocs_->emit(rel_dyn);
1435 delete this->copy_relocs_;
1436 this->copy_relocs_ = NULL;
1439 // Return whether a direct absolute static relocation needs to be applied.
1440 // In cases where Scan::local() or Scan::global() has created
1441 // a dynamic relocation other than R_386_RELATIVE, the addend
1442 // of the relocation is carried in the data, and we must not
1443 // apply the static relocation.
1445 inline bool
1446 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1447 int ref_flags,
1448 bool is_32bit)
1450 // For local symbols, we will have created a non-RELATIVE dynamic
1451 // relocation only if (a) the output is position independent,
1452 // (b) the relocation is absolute (not pc- or segment-relative), and
1453 // (c) the relocation is not 32 bits wide.
1454 if (gsym == NULL)
1455 return !(parameters->output_is_position_independent()
1456 && (ref_flags & Symbol::ABSOLUTE_REF)
1457 && !is_32bit);
1459 // For global symbols, we use the same helper routines used in the
1460 // scan pass. If we did not create a dynamic relocation, or if we
1461 // created a RELATIVE dynamic relocation, we should apply the static
1462 // relocation.
1463 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1464 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1465 && gsym->can_use_relative_reloc(ref_flags
1466 & Symbol::FUNCTION_CALL);
1467 return !has_dyn || is_rel;
1470 // Perform a relocation.
1472 inline bool
1473 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1474 Target_i386* target,
1475 size_t relnum,
1476 const elfcpp::Rel<32, false>& rel,
1477 unsigned int r_type,
1478 const Sized_symbol<32>* gsym,
1479 const Symbol_value<32>* psymval,
1480 unsigned char* view,
1481 elfcpp::Elf_types<32>::Elf_Addr address,
1482 section_size_type view_size)
1484 if (this->skip_call_tls_get_addr_)
1486 if (r_type != elfcpp::R_386_PLT32
1487 || gsym == NULL
1488 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1489 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1490 _("missing expected TLS relocation"));
1491 else
1493 this->skip_call_tls_get_addr_ = false;
1494 return false;
1498 // Pick the value to use for symbols defined in shared objects.
1499 Symbol_value<32> symval;
1500 bool is_nonpic = (r_type == elfcpp::R_386_PC8
1501 || r_type == elfcpp::R_386_PC16
1502 || r_type == elfcpp::R_386_PC32);
1503 if (gsym != NULL
1504 && (gsym->is_from_dynobj()
1505 || (parameters->output_is_shared()
1506 && gsym->is_preemptible()))
1507 && gsym->has_plt_offset()
1508 && (!is_nonpic || !parameters->output_is_shared()))
1510 symval.set_output_value(target->plt_section()->address()
1511 + gsym->plt_offset());
1512 psymval = &symval;
1515 const Sized_relobj<32, false>* object = relinfo->object;
1517 // Get the GOT offset if needed.
1518 // The GOT pointer points to the end of the GOT section.
1519 // We need to subtract the size of the GOT section to get
1520 // the actual offset to use in the relocation.
1521 bool have_got_offset = false;
1522 unsigned int got_offset = 0;
1523 switch (r_type)
1525 case elfcpp::R_386_GOT32:
1526 if (gsym != NULL)
1528 gold_assert(gsym->has_got_offset());
1529 got_offset = gsym->got_offset() - target->got_size();
1531 else
1533 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1534 gold_assert(object->local_has_got_offset(r_sym));
1535 got_offset = object->local_got_offset(r_sym) - target->got_size();
1537 have_got_offset = true;
1538 break;
1540 default:
1541 break;
1544 switch (r_type)
1546 case elfcpp::R_386_NONE:
1547 case elfcpp::R_386_GNU_VTINHERIT:
1548 case elfcpp::R_386_GNU_VTENTRY:
1549 break;
1551 case elfcpp::R_386_32:
1552 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1553 Relocate_functions<32, false>::rel32(view, object, psymval);
1554 break;
1556 case elfcpp::R_386_PC32:
1558 int ref_flags = Symbol::NON_PIC_REF;
1559 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1560 ref_flags |= Symbol::FUNCTION_CALL;
1561 if (should_apply_static_reloc(gsym, ref_flags, true))
1562 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1564 break;
1566 case elfcpp::R_386_16:
1567 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1568 Relocate_functions<32, false>::rel16(view, object, psymval);
1569 break;
1571 case elfcpp::R_386_PC16:
1573 int ref_flags = Symbol::NON_PIC_REF;
1574 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1575 ref_flags |= Symbol::FUNCTION_CALL;
1576 if (should_apply_static_reloc(gsym, ref_flags, false))
1577 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1579 break;
1581 case elfcpp::R_386_8:
1582 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1583 Relocate_functions<32, false>::rel8(view, object, psymval);
1584 break;
1586 case elfcpp::R_386_PC8:
1588 int ref_flags = Symbol::NON_PIC_REF;
1589 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1590 ref_flags |= Symbol::FUNCTION_CALL;
1591 if (should_apply_static_reloc(gsym, ref_flags, false))
1592 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1594 break;
1596 case elfcpp::R_386_PLT32:
1597 gold_assert(gsym == NULL
1598 || gsym->has_plt_offset()
1599 || gsym->final_value_is_known()
1600 || (gsym->is_defined()
1601 && !gsym->is_from_dynobj()
1602 && !gsym->is_preemptible()));
1603 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1604 break;
1606 case elfcpp::R_386_GOT32:
1607 gold_assert(have_got_offset);
1608 Relocate_functions<32, false>::rel32(view, got_offset);
1609 break;
1611 case elfcpp::R_386_GOTOFF:
1613 elfcpp::Elf_types<32>::Elf_Addr value;
1614 value = (psymval->value(object, 0)
1615 - target->got_plt_section()->address());
1616 Relocate_functions<32, false>::rel32(view, value);
1618 break;
1620 case elfcpp::R_386_GOTPC:
1622 elfcpp::Elf_types<32>::Elf_Addr value;
1623 value = target->got_plt_section()->address();
1624 Relocate_functions<32, false>::pcrel32(view, value, address);
1626 break;
1628 case elfcpp::R_386_COPY:
1629 case elfcpp::R_386_GLOB_DAT:
1630 case elfcpp::R_386_JUMP_SLOT:
1631 case elfcpp::R_386_RELATIVE:
1632 // These are outstanding tls relocs, which are unexpected when
1633 // linking.
1634 case elfcpp::R_386_TLS_TPOFF:
1635 case elfcpp::R_386_TLS_DTPMOD32:
1636 case elfcpp::R_386_TLS_DTPOFF32:
1637 case elfcpp::R_386_TLS_TPOFF32:
1638 case elfcpp::R_386_TLS_DESC:
1639 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1640 _("unexpected reloc %u in object file"),
1641 r_type);
1642 break;
1644 // These are initial tls relocs, which are expected when
1645 // linking.
1646 case elfcpp::R_386_TLS_GD: // Global-dynamic
1647 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1648 case elfcpp::R_386_TLS_DESC_CALL:
1649 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1650 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1651 case elfcpp::R_386_TLS_IE: // Initial-exec
1652 case elfcpp::R_386_TLS_IE_32:
1653 case elfcpp::R_386_TLS_GOTIE:
1654 case elfcpp::R_386_TLS_LE: // Local-exec
1655 case elfcpp::R_386_TLS_LE_32:
1656 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1657 view, address, view_size);
1658 break;
1660 case elfcpp::R_386_32PLT:
1661 case elfcpp::R_386_TLS_GD_32:
1662 case elfcpp::R_386_TLS_GD_PUSH:
1663 case elfcpp::R_386_TLS_GD_CALL:
1664 case elfcpp::R_386_TLS_GD_POP:
1665 case elfcpp::R_386_TLS_LDM_32:
1666 case elfcpp::R_386_TLS_LDM_PUSH:
1667 case elfcpp::R_386_TLS_LDM_CALL:
1668 case elfcpp::R_386_TLS_LDM_POP:
1669 case elfcpp::R_386_USED_BY_INTEL_200:
1670 default:
1671 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1672 _("unsupported reloc %u"),
1673 r_type);
1674 break;
1677 return true;
1680 // Perform a TLS relocation.
1682 inline void
1683 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1684 Target_i386* target,
1685 size_t relnum,
1686 const elfcpp::Rel<32, false>& rel,
1687 unsigned int r_type,
1688 const Sized_symbol<32>* gsym,
1689 const Symbol_value<32>* psymval,
1690 unsigned char* view,
1691 elfcpp::Elf_types<32>::Elf_Addr,
1692 section_size_type view_size)
1694 Output_segment* tls_segment = relinfo->layout->tls_segment();
1696 const Sized_relobj<32, false>* object = relinfo->object;
1698 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1700 const bool is_final = (gsym == NULL
1701 ? !parameters->output_is_position_independent()
1702 : gsym->final_value_is_known());
1703 const tls::Tls_optimization optimized_type
1704 = Target_i386::optimize_tls_reloc(is_final, r_type);
1705 switch (r_type)
1707 case elfcpp::R_386_TLS_GD: // Global-dynamic
1708 if (optimized_type == tls::TLSOPT_TO_LE)
1710 gold_assert(tls_segment != NULL);
1711 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1712 rel, r_type, value, view,
1713 view_size);
1714 break;
1716 else
1718 unsigned int got_offset;
1719 if (gsym != NULL)
1721 gold_assert(gsym->has_tls_got_offset(true));
1722 got_offset = gsym->tls_got_offset(true) - target->got_size();
1724 else
1726 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1727 gold_assert(object->local_has_tls_got_offset(r_sym, true));
1728 got_offset = (object->local_tls_got_offset(r_sym, true)
1729 - target->got_size());
1731 if (optimized_type == tls::TLSOPT_TO_IE)
1733 gold_assert(tls_segment != NULL);
1734 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1735 got_offset, view, view_size);
1736 break;
1738 else if (optimized_type == tls::TLSOPT_NONE)
1740 // Relocate the field with the offset of the pair of GOT
1741 // entries.
1742 Relocate_functions<32, false>::rel32(view, got_offset);
1743 break;
1746 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1747 _("unsupported reloc %u"),
1748 r_type);
1749 break;
1751 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1752 case elfcpp::R_386_TLS_DESC_CALL:
1753 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1754 _("unsupported reloc %u"),
1755 r_type);
1756 break;
1758 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1759 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1761 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1762 _("both SUN and GNU model "
1763 "TLS relocations"));
1764 break;
1766 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1767 if (optimized_type == tls::TLSOPT_TO_LE)
1769 gold_assert(tls_segment != NULL);
1770 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1771 value, view, view_size);
1772 break;
1774 else if (optimized_type == tls::TLSOPT_NONE)
1776 // Relocate the field with the offset of the GOT entry for
1777 // the module index.
1778 unsigned int got_offset;
1779 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1780 - target->got_size());
1781 Relocate_functions<32, false>::rel32(view, got_offset);
1782 break;
1784 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1785 _("unsupported reloc %u"),
1786 r_type);
1787 break;
1789 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1790 // This reloc can appear in debugging sections, in which case we
1791 // won't see the TLS_LDM reloc. The local_dynamic_type field
1792 // tells us this.
1793 if (optimized_type == tls::TLSOPT_TO_LE)
1795 gold_assert(tls_segment != NULL);
1796 value -= tls_segment->memsz();
1798 Relocate_functions<32, false>::rel32(view, value);
1799 break;
1801 case elfcpp::R_386_TLS_IE: // Initial-exec
1802 case elfcpp::R_386_TLS_GOTIE:
1803 case elfcpp::R_386_TLS_IE_32:
1804 if (optimized_type == tls::TLSOPT_TO_LE)
1806 gold_assert(tls_segment != NULL);
1807 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1808 rel, r_type, value, view,
1809 view_size);
1810 break;
1812 else if (optimized_type == tls::TLSOPT_NONE)
1814 // Relocate the field with the offset of the GOT entry for
1815 // the tp-relative offset of the symbol.
1816 unsigned int got_offset;
1817 if (gsym != NULL)
1819 gold_assert(gsym->has_got_offset());
1820 got_offset = gsym->got_offset();
1822 else
1824 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1825 gold_assert(object->local_has_got_offset(r_sym));
1826 got_offset = object->local_got_offset(r_sym);
1828 // For the R_386_TLS_IE relocation, we need to apply the
1829 // absolute address of the GOT entry.
1830 if (r_type == elfcpp::R_386_TLS_IE)
1831 got_offset += target->got_plt_section()->address();
1832 // All GOT offsets are relative to the end of the GOT.
1833 got_offset -= target->got_size();
1834 Relocate_functions<32, false>::rel32(view, got_offset);
1835 break;
1837 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1838 _("unsupported reloc %u"),
1839 r_type);
1840 break;
1842 case elfcpp::R_386_TLS_LE: // Local-exec
1843 // If we're creating a shared library, a dynamic relocation will
1844 // have been created for this location, so do not apply it now.
1845 if (!parameters->output_is_shared())
1847 gold_assert(tls_segment != NULL);
1848 value -= tls_segment->memsz();
1849 Relocate_functions<32, false>::rel32(view, value);
1851 break;
1853 case elfcpp::R_386_TLS_LE_32:
1854 // If we're creating a shared library, a dynamic relocation will
1855 // have been created for this location, so do not apply it now.
1856 if (!parameters->output_is_shared())
1858 gold_assert(tls_segment != NULL);
1859 value = tls_segment->memsz() - value;
1860 Relocate_functions<32, false>::rel32(view, value);
1862 break;
1866 // Do a relocation in which we convert a TLS General-Dynamic to a
1867 // Local-Exec.
1869 inline void
1870 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1871 size_t relnum,
1872 Output_segment* tls_segment,
1873 const elfcpp::Rel<32, false>& rel,
1874 unsigned int,
1875 elfcpp::Elf_types<32>::Elf_Addr value,
1876 unsigned char* view,
1877 section_size_type view_size)
1879 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1880 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1881 // leal foo(%reg),%eax; call ___tls_get_addr
1882 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1884 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1885 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1887 unsigned char op1 = view[-1];
1888 unsigned char op2 = view[-2];
1890 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1891 op2 == 0x8d || op2 == 0x04);
1892 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1894 int roff = 5;
1896 if (op2 == 0x04)
1898 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1899 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1900 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1901 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1902 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1904 else
1906 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1907 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1908 if (rel.get_r_offset() + 9 < view_size
1909 && view[9] == 0x90)
1911 // There is a trailing nop. Use the size byte subl.
1912 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1913 roff = 6;
1915 else
1917 // Use the five byte subl.
1918 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1922 value = tls_segment->memsz() - value;
1923 Relocate_functions<32, false>::rel32(view + roff, value);
1925 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1926 // We can skip it.
1927 this->skip_call_tls_get_addr_ = true;
1930 // Do a relocation in which we convert a TLS General-Dynamic to an
1931 // Initial-Exec.
1933 inline void
1934 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
1935 size_t relnum,
1936 Output_segment* tls_segment,
1937 const elfcpp::Rel<32, false>& rel,
1938 unsigned int,
1939 elfcpp::Elf_types<32>::Elf_Addr value,
1940 unsigned char* view,
1941 section_size_type view_size)
1943 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
1944 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
1946 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1947 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1949 unsigned char op1 = view[-1];
1950 unsigned char op2 = view[-2];
1952 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1953 op2 == 0x8d || op2 == 0x04);
1954 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1956 int roff = 5;
1958 // FIXME: For now, support only one form.
1959 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1960 op1 == 0x8d && op2 == 0x04);
1962 if (op2 == 0x04)
1964 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1965 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1966 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1967 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1968 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
1970 else
1972 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1973 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1974 if (rel.get_r_offset() + 9 < view_size
1975 && view[9] == 0x90)
1977 // FIXME: This is not the right instruction sequence.
1978 // There is a trailing nop. Use the size byte subl.
1979 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1980 roff = 6;
1982 else
1984 // FIXME: This is not the right instruction sequence.
1985 // Use the five byte subl.
1986 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1990 value = tls_segment->memsz() - value;
1991 Relocate_functions<32, false>::rel32(view + roff, value);
1993 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1994 // We can skip it.
1995 this->skip_call_tls_get_addr_ = true;
1998 // Do a relocation in which we convert a TLS Local-Dynamic to a
1999 // Local-Exec.
2001 inline void
2002 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2003 size_t relnum,
2004 Output_segment*,
2005 const elfcpp::Rel<32, false>& rel,
2006 unsigned int,
2007 elfcpp::Elf_types<32>::Elf_Addr,
2008 unsigned char* view,
2009 section_size_type view_size)
2011 // leal foo(%reg), %eax; call ___tls_get_addr
2012 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2014 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2015 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2017 // FIXME: Does this test really always pass?
2018 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2019 view[-2] == 0x8d && view[-1] == 0x83);
2021 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2023 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2025 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2026 // We can skip it.
2027 this->skip_call_tls_get_addr_ = true;
2030 // Do a relocation in which we convert a TLS Initial-Exec to a
2031 // Local-Exec.
2033 inline void
2034 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2035 size_t relnum,
2036 Output_segment* tls_segment,
2037 const elfcpp::Rel<32, false>& rel,
2038 unsigned int r_type,
2039 elfcpp::Elf_types<32>::Elf_Addr value,
2040 unsigned char* view,
2041 section_size_type view_size)
2043 // We have to actually change the instructions, which means that we
2044 // need to examine the opcodes to figure out which instruction we
2045 // are looking at.
2046 if (r_type == elfcpp::R_386_TLS_IE)
2048 // movl %gs:XX,%eax ==> movl $YY,%eax
2049 // movl %gs:XX,%reg ==> movl $YY,%reg
2050 // addl %gs:XX,%reg ==> addl $YY,%reg
2051 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2052 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2054 unsigned char op1 = view[-1];
2055 if (op1 == 0xa1)
2057 // movl XX,%eax ==> movl $YY,%eax
2058 view[-1] = 0xb8;
2060 else
2062 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2064 unsigned char op2 = view[-2];
2065 if (op2 == 0x8b)
2067 // movl XX,%reg ==> movl $YY,%reg
2068 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2069 (op1 & 0xc7) == 0x05);
2070 view[-2] = 0xc7;
2071 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2073 else if (op2 == 0x03)
2075 // addl XX,%reg ==> addl $YY,%reg
2076 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2077 (op1 & 0xc7) == 0x05);
2078 view[-2] = 0x81;
2079 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2081 else
2082 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2085 else
2087 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2088 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2089 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2090 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2091 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2093 unsigned char op1 = view[-1];
2094 unsigned char op2 = view[-2];
2095 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2096 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2097 if (op2 == 0x8b)
2099 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2100 view[-2] = 0xc7;
2101 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2103 else if (op2 == 0x2b)
2105 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2106 view[-2] = 0x81;
2107 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2109 else if (op2 == 0x03)
2111 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2112 view[-2] = 0x81;
2113 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2115 else
2116 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2119 value = tls_segment->memsz() - value;
2120 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2121 value = - value;
2123 Relocate_functions<32, false>::rel32(view, value);
2126 // Relocate section data.
2128 void
2129 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2130 unsigned int sh_type,
2131 const unsigned char* prelocs,
2132 size_t reloc_count,
2133 Output_section* output_section,
2134 bool needs_special_offset_handling,
2135 unsigned char* view,
2136 elfcpp::Elf_types<32>::Elf_Addr address,
2137 section_size_type view_size)
2139 gold_assert(sh_type == elfcpp::SHT_REL);
2141 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2142 Target_i386::Relocate>(
2143 relinfo,
2144 this,
2145 prelocs,
2146 reloc_count,
2147 output_section,
2148 needs_special_offset_handling,
2149 view,
2150 address,
2151 view_size);
2154 // Return the value to use for a dynamic which requires special
2155 // treatment. This is how we support equality comparisons of function
2156 // pointers across shared library boundaries, as described in the
2157 // processor specific ABI supplement.
2159 uint64_t
2160 Target_i386::do_dynsym_value(const Symbol* gsym) const
2162 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2163 return this->plt_section()->address() + gsym->plt_offset();
2166 // Return a string used to fill a code section with nops to take up
2167 // the specified length.
2169 std::string
2170 Target_i386::do_code_fill(section_size_type length)
2172 if (length >= 16)
2174 // Build a jmp instruction to skip over the bytes.
2175 unsigned char jmp[5];
2176 jmp[0] = 0xe9;
2177 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2178 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2179 + std::string(length - 5, '\0'));
2182 // Nop sequences of various lengths.
2183 const char nop1[1] = { 0x90 }; // nop
2184 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2185 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2186 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2187 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2188 0x00 }; // leal 0(%esi,1),%esi
2189 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2190 0x00, 0x00 };
2191 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2192 0x00, 0x00, 0x00 };
2193 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2194 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2195 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2196 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2197 0x00 };
2198 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2199 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2200 0x00, 0x00 };
2201 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2202 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2203 0x00, 0x00, 0x00 };
2204 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2205 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2206 0x00, 0x00, 0x00, 0x00 };
2207 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2208 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2209 0x27, 0x00, 0x00, 0x00,
2210 0x00 };
2211 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2212 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2213 0xbc, 0x27, 0x00, 0x00,
2214 0x00, 0x00 };
2215 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2216 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2217 0x90, 0x90, 0x90, 0x90,
2218 0x90, 0x90, 0x90 };
2220 const char* nops[16] = {
2221 NULL,
2222 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2223 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2226 return std::string(nops[length], length);
2229 // The selector for i386 object files.
2231 class Target_selector_i386 : public Target_selector
2233 public:
2234 Target_selector_i386()
2235 : Target_selector(elfcpp::EM_386, 32, false)
2238 Target*
2239 recognize(int machine, int osabi, int abiversion);
2241 private:
2242 Target_i386* target_;
2245 // Recognize an i386 object file when we already know that the machine
2246 // number is EM_386.
2248 Target*
2249 Target_selector_i386::recognize(int, int, int)
2251 if (this->target_ == NULL)
2252 this->target_ = new Target_i386();
2253 return this->target_;
2256 Target_selector_i386 target_selector_i386;
2258 } // End anonymous namespace.