PR binutils/11889
[binutils.git] / gold / i386.cc
blobb27296769a7dd825332852f2ca296f8e0035d1d1
1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010 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"
40 #include "freebsd.h"
41 #include "gc.h"
43 namespace
46 using namespace gold;
48 class Output_data_plt_i386;
50 // The i386 target class.
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_i386 : public Target_freebsd<32, false>
57 public:
58 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
60 Target_i386()
61 : Target_freebsd<32, false>(&i386_info),
62 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
63 global_offset_table_(NULL), rel_dyn_(NULL),
64 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
65 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
66 { }
68 inline bool
69 can_check_for_function_pointers() const
70 { return true; }
72 // Process the relocations to determine unreferenced sections for
73 // garbage collection.
74 void
75 gc_process_relocs(Symbol_table* symtab,
76 Layout* layout,
77 Sized_relobj<32, false>* object,
78 unsigned int data_shndx,
79 unsigned int sh_type,
80 const unsigned char* prelocs,
81 size_t reloc_count,
82 Output_section* output_section,
83 bool needs_special_offset_handling,
84 size_t local_symbol_count,
85 const unsigned char* plocal_symbols);
87 // Scan the relocations to look for symbol adjustments.
88 void
89 scan_relocs(Symbol_table* symtab,
90 Layout* layout,
91 Sized_relobj<32, false>* object,
92 unsigned int data_shndx,
93 unsigned int sh_type,
94 const unsigned char* prelocs,
95 size_t reloc_count,
96 Output_section* output_section,
97 bool needs_special_offset_handling,
98 size_t local_symbol_count,
99 const unsigned char* plocal_symbols);
101 // Finalize the sections.
102 void
103 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
105 // Return the value to use for a dynamic which requires special
106 // treatment.
107 uint64_t
108 do_dynsym_value(const Symbol*) const;
110 // Relocate a section.
111 void
112 relocate_section(const Relocate_info<32, false>*,
113 unsigned int sh_type,
114 const unsigned char* prelocs,
115 size_t reloc_count,
116 Output_section* output_section,
117 bool needs_special_offset_handling,
118 unsigned char* view,
119 elfcpp::Elf_types<32>::Elf_Addr view_address,
120 section_size_type view_size,
121 const Reloc_symbol_changes*);
123 // Scan the relocs during a relocatable link.
124 void
125 scan_relocatable_relocs(Symbol_table* symtab,
126 Layout* layout,
127 Sized_relobj<32, false>* object,
128 unsigned int data_shndx,
129 unsigned int sh_type,
130 const unsigned char* prelocs,
131 size_t reloc_count,
132 Output_section* output_section,
133 bool needs_special_offset_handling,
134 size_t local_symbol_count,
135 const unsigned char* plocal_symbols,
136 Relocatable_relocs*);
138 // Relocate a section during a relocatable link.
139 void
140 relocate_for_relocatable(const Relocate_info<32, false>*,
141 unsigned int sh_type,
142 const unsigned char* prelocs,
143 size_t reloc_count,
144 Output_section* output_section,
145 off_t offset_in_output_section,
146 const Relocatable_relocs*,
147 unsigned char* view,
148 elfcpp::Elf_types<32>::Elf_Addr view_address,
149 section_size_type view_size,
150 unsigned char* reloc_view,
151 section_size_type reloc_view_size);
153 // Return a string used to fill a code section with nops.
154 std::string
155 do_code_fill(section_size_type length) const;
157 // Return whether SYM is defined by the ABI.
158 bool
159 do_is_defined_by_abi(const Symbol* sym) const
160 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
162 // Return whether a symbol name implies a local label. The UnixWare
163 // 2.1 cc generates temporary symbols that start with .X, so we
164 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
165 // If so, we should move the .X recognition into
166 // Target::do_is_local_label_name.
167 bool
168 do_is_local_label_name(const char* name) const
170 if (name[0] == '.' && name[1] == 'X')
171 return true;
172 return Target::do_is_local_label_name(name);
175 // Return whether SYM is call to a non-split function.
176 bool
177 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
179 // Adjust -fstack-split code which calls non-stack-split code.
180 void
181 do_calls_non_split(Relobj* object, unsigned int shndx,
182 section_offset_type fnoffset, section_size_type fnsize,
183 unsigned char* view, section_size_type view_size,
184 std::string* from, std::string* to) const;
186 // Return the size of the GOT section.
187 section_size_type
188 got_size() const
190 gold_assert(this->got_ != NULL);
191 return this->got_->data_size();
194 // Return the number of entries in the GOT.
195 unsigned int
196 got_entry_count() const
198 if (this->got_ == NULL)
199 return 0;
200 return this->got_size() / 4;
203 // Return the number of entries in the PLT.
204 unsigned int
205 plt_entry_count() const;
207 // Return the offset of the first non-reserved PLT entry.
208 unsigned int
209 first_plt_entry_offset() const;
211 // Return the size of each PLT entry.
212 unsigned int
213 plt_entry_size() const;
215 private:
216 // The class which scans relocations.
217 struct Scan
219 inline void
220 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
221 Sized_relobj<32, false>* object,
222 unsigned int data_shndx,
223 Output_section* output_section,
224 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
225 const elfcpp::Sym<32, false>& lsym);
227 inline void
228 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
229 Sized_relobj<32, false>* object,
230 unsigned int data_shndx,
231 Output_section* output_section,
232 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
233 Symbol* gsym);
235 inline bool
236 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
237 Target_i386* target,
238 Sized_relobj<32, false>* object,
239 unsigned int data_shndx,
240 Output_section* output_section,
241 const elfcpp::Rel<32, false>& reloc,
242 unsigned int r_type,
243 const elfcpp::Sym<32, false>& lsym);
245 inline bool
246 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
247 Target_i386* target,
248 Sized_relobj<32, false>* object,
249 unsigned int data_shndx,
250 Output_section* output_section,
251 const elfcpp::Rel<32, false>& reloc,
252 unsigned int r_type,
253 Symbol* gsym);
255 inline bool
256 possible_function_pointer_reloc(unsigned int r_type);
258 static void
259 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
261 static void
262 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
263 Symbol*);
266 // The class which implements relocation.
267 class Relocate
269 public:
270 Relocate()
271 : skip_call_tls_get_addr_(false),
272 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
275 ~Relocate()
277 if (this->skip_call_tls_get_addr_)
279 // FIXME: This needs to specify the location somehow.
280 gold_error(_("missing expected TLS relocation"));
284 // Return whether the static relocation needs to be applied.
285 inline bool
286 should_apply_static_reloc(const Sized_symbol<32>* gsym,
287 int ref_flags,
288 bool is_32bit,
289 Output_section* output_section);
291 // Do a relocation. Return false if the caller should not issue
292 // any warnings about this relocation.
293 inline bool
294 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
295 size_t relnum, const elfcpp::Rel<32, false>&,
296 unsigned int r_type, const Sized_symbol<32>*,
297 const Symbol_value<32>*,
298 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
299 section_size_type);
301 private:
302 // Do a TLS relocation.
303 inline void
304 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
305 size_t relnum, const elfcpp::Rel<32, false>&,
306 unsigned int r_type, const Sized_symbol<32>*,
307 const Symbol_value<32>*,
308 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
309 section_size_type);
311 // Do a TLS General-Dynamic to Initial-Exec transition.
312 inline void
313 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
314 Output_segment* tls_segment,
315 const elfcpp::Rel<32, false>&, unsigned int r_type,
316 elfcpp::Elf_types<32>::Elf_Addr value,
317 unsigned char* view,
318 section_size_type view_size);
320 // Do a TLS General-Dynamic to Local-Exec transition.
321 inline void
322 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
323 Output_segment* tls_segment,
324 const elfcpp::Rel<32, false>&, unsigned int r_type,
325 elfcpp::Elf_types<32>::Elf_Addr value,
326 unsigned char* view,
327 section_size_type view_size);
329 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
330 // transition.
331 inline void
332 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
333 Output_segment* tls_segment,
334 const elfcpp::Rel<32, false>&, unsigned int r_type,
335 elfcpp::Elf_types<32>::Elf_Addr value,
336 unsigned char* view,
337 section_size_type view_size);
339 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
340 // transition.
341 inline void
342 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
343 Output_segment* tls_segment,
344 const elfcpp::Rel<32, false>&, unsigned int r_type,
345 elfcpp::Elf_types<32>::Elf_Addr value,
346 unsigned char* view,
347 section_size_type view_size);
349 // Do a TLS Local-Dynamic to Local-Exec transition.
350 inline void
351 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
352 Output_segment* tls_segment,
353 const elfcpp::Rel<32, false>&, unsigned int r_type,
354 elfcpp::Elf_types<32>::Elf_Addr value,
355 unsigned char* view,
356 section_size_type view_size);
358 // Do a TLS Initial-Exec to Local-Exec transition.
359 static inline void
360 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
361 Output_segment* tls_segment,
362 const elfcpp::Rel<32, false>&, unsigned int r_type,
363 elfcpp::Elf_types<32>::Elf_Addr value,
364 unsigned char* view,
365 section_size_type view_size);
367 // We need to keep track of which type of local dynamic relocation
368 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
369 enum Local_dynamic_type
371 LOCAL_DYNAMIC_NONE,
372 LOCAL_DYNAMIC_SUN,
373 LOCAL_DYNAMIC_GNU
376 // This is set if we should skip the next reloc, which should be a
377 // PLT32 reloc against ___tls_get_addr.
378 bool skip_call_tls_get_addr_;
379 // The type of local dynamic relocation we have seen in the section
380 // being relocated, if any.
381 Local_dynamic_type local_dynamic_type_;
384 // A class which returns the size required for a relocation type,
385 // used while scanning relocs during a relocatable link.
386 class Relocatable_size_for_reloc
388 public:
389 unsigned int
390 get_size_for_reloc(unsigned int, Relobj*);
393 // Adjust TLS relocation type based on the options and whether this
394 // is a local symbol.
395 static tls::Tls_optimization
396 optimize_tls_reloc(bool is_final, int r_type);
398 // Get the GOT section, creating it if necessary.
399 Output_data_got<32, false>*
400 got_section(Symbol_table*, Layout*);
402 // Get the GOT PLT section.
403 Output_data_space*
404 got_plt_section() const
406 gold_assert(this->got_plt_ != NULL);
407 return this->got_plt_;
410 // Get the GOT section for TLSDESC entries.
411 Output_data_got<32, false>*
412 got_tlsdesc_section() const
414 gold_assert(this->got_tlsdesc_ != NULL);
415 return this->got_tlsdesc_;
418 // Create a PLT entry for a global symbol.
419 void
420 make_plt_entry(Symbol_table*, Layout*, Symbol*);
422 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
423 void
424 define_tls_base_symbol(Symbol_table*, Layout*);
426 // Create a GOT entry for the TLS module index.
427 unsigned int
428 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
429 Sized_relobj<32, false>* object);
431 // Get the PLT section.
432 Output_data_plt_i386*
433 plt_section() const
435 gold_assert(this->plt_ != NULL);
436 return this->plt_;
439 // Get the dynamic reloc section, creating it if necessary.
440 Reloc_section*
441 rel_dyn_section(Layout*);
443 // Get the section to use for TLS_DESC relocations.
444 Reloc_section*
445 rel_tls_desc_section(Layout*) const;
447 // Add a potential copy relocation.
448 void
449 copy_reloc(Symbol_table* symtab, Layout* layout,
450 Sized_relobj<32, false>* object,
451 unsigned int shndx, Output_section* output_section,
452 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
454 this->copy_relocs_.copy_reloc(symtab, layout,
455 symtab->get_sized_symbol<32>(sym),
456 object, shndx, output_section, reloc,
457 this->rel_dyn_section(layout));
460 // Information about this specific target which we pass to the
461 // general Target structure.
462 static const Target::Target_info i386_info;
464 // The types of GOT entries needed for this platform.
465 // These values are exposed to the ABI in an incremental link.
466 // Do not renumber existing values without changing the version
467 // number of the .gnu_incremental_inputs section.
468 enum Got_type
470 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
471 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
472 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
473 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
474 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
477 // The GOT section.
478 Output_data_got<32, false>* got_;
479 // The PLT section.
480 Output_data_plt_i386* plt_;
481 // The GOT PLT section.
482 Output_data_space* got_plt_;
483 // The GOT section for TLSDESC relocations.
484 Output_data_got<32, false>* got_tlsdesc_;
485 // The _GLOBAL_OFFSET_TABLE_ symbol.
486 Symbol* global_offset_table_;
487 // The dynamic reloc section.
488 Reloc_section* rel_dyn_;
489 // Relocs saved to avoid a COPY reloc.
490 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
491 // Space for variables copied with a COPY reloc.
492 Output_data_space* dynbss_;
493 // Offset of the GOT entry for the TLS module index.
494 unsigned int got_mod_index_offset_;
495 // True if the _TLS_MODULE_BASE_ symbol has been defined.
496 bool tls_base_symbol_defined_;
499 const Target::Target_info Target_i386::i386_info =
501 32, // size
502 false, // is_big_endian
503 elfcpp::EM_386, // machine_code
504 false, // has_make_symbol
505 false, // has_resolve
506 true, // has_code_fill
507 true, // is_default_stack_executable
508 '\0', // wrap_char
509 "/usr/lib/libc.so.1", // dynamic_linker
510 0x08048000, // default_text_segment_address
511 0x1000, // abi_pagesize (overridable by -z max-page-size)
512 0x1000, // common_pagesize (overridable by -z common-page-size)
513 elfcpp::SHN_UNDEF, // small_common_shndx
514 elfcpp::SHN_UNDEF, // large_common_shndx
515 0, // small_common_section_flags
516 0, // large_common_section_flags
517 NULL, // attributes_section
518 NULL // attributes_vendor
521 // Get the GOT section, creating it if necessary.
523 Output_data_got<32, false>*
524 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
526 if (this->got_ == NULL)
528 gold_assert(symtab != NULL && layout != NULL);
530 this->got_ = new Output_data_got<32, false>();
532 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
533 (elfcpp::SHF_ALLOC
534 | elfcpp::SHF_WRITE),
535 this->got_, ORDER_RELRO_LAST, true);
537 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
538 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
539 (elfcpp::SHF_ALLOC
540 | elfcpp::SHF_WRITE),
541 this->got_plt_, ORDER_NON_RELRO_FIRST,
542 false);
544 // The first three entries are reserved.
545 this->got_plt_->set_current_data_size(3 * 4);
547 // Those bytes can go into the relro segment.
548 layout->increase_relro(3 * 4);
550 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
551 this->global_offset_table_ =
552 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
553 Symbol_table::PREDEFINED,
554 this->got_plt_,
555 0, 0, elfcpp::STT_OBJECT,
556 elfcpp::STB_LOCAL,
557 elfcpp::STV_HIDDEN, 0,
558 false, false);
560 // If there are any TLSDESC relocations, they get GOT entries in
561 // .got.plt after the jump slot entries.
562 this->got_tlsdesc_ = new Output_data_got<32, false>();
563 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
564 (elfcpp::SHF_ALLOC
565 | elfcpp::SHF_WRITE),
566 this->got_tlsdesc_,
567 ORDER_NON_RELRO_FIRST, false);
570 return this->got_;
573 // Get the dynamic reloc section, creating it if necessary.
575 Target_i386::Reloc_section*
576 Target_i386::rel_dyn_section(Layout* layout)
578 if (this->rel_dyn_ == NULL)
580 gold_assert(layout != NULL);
581 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
582 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
583 elfcpp::SHF_ALLOC, this->rel_dyn_,
584 ORDER_DYNAMIC_RELOCS, false);
586 return this->rel_dyn_;
589 // A class to handle the PLT data.
591 class Output_data_plt_i386 : public Output_section_data
593 public:
594 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
596 Output_data_plt_i386(Layout*, Output_data_space*);
598 // Add an entry to the PLT.
599 void
600 add_entry(Symbol* gsym);
602 // Return the .rel.plt section data.
603 const Reloc_section*
604 rel_plt() const
605 { return this->rel_; }
607 // Return where the TLS_DESC relocations should go.
608 Reloc_section*
609 rel_tls_desc(Layout*);
611 // Return the number of PLT entries.
612 unsigned int
613 entry_count() const
614 { return this->count_; }
616 // Return the offset of the first non-reserved PLT entry.
617 static unsigned int
618 first_plt_entry_offset()
619 { return plt_entry_size; }
621 // Return the size of a PLT entry.
622 static unsigned int
623 get_plt_entry_size()
624 { return plt_entry_size; }
626 protected:
627 void
628 do_adjust_output_section(Output_section* os);
630 // Write to a map file.
631 void
632 do_print_to_mapfile(Mapfile* mapfile) const
633 { mapfile->print_output_data(this, _("** PLT")); }
635 private:
636 // The size of an entry in the PLT.
637 static const int plt_entry_size = 16;
639 // The first entry in the PLT for an executable.
640 static unsigned char exec_first_plt_entry[plt_entry_size];
642 // The first entry in the PLT for a shared object.
643 static unsigned char dyn_first_plt_entry[plt_entry_size];
645 // Other entries in the PLT for an executable.
646 static unsigned char exec_plt_entry[plt_entry_size];
648 // Other entries in the PLT for a shared object.
649 static unsigned char dyn_plt_entry[plt_entry_size];
651 // Set the final size.
652 void
653 set_final_data_size()
654 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
656 // Write out the PLT data.
657 void
658 do_write(Output_file*);
660 // The reloc section.
661 Reloc_section* rel_;
662 // The TLS_DESC relocations, if necessary. These must follow the
663 // regular PLT relocs.
664 Reloc_section* tls_desc_rel_;
665 // The .got.plt section.
666 Output_data_space* got_plt_;
667 // The number of PLT entries.
668 unsigned int count_;
671 // Create the PLT section. The ordinary .got section is an argument,
672 // since we need to refer to the start. We also create our own .got
673 // section just for PLT entries.
675 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
676 Output_data_space* got_plt)
677 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0)
679 this->rel_ = new Reloc_section(false);
680 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
681 elfcpp::SHF_ALLOC, this->rel_,
682 ORDER_DYNAMIC_PLT_RELOCS, false);
685 void
686 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
688 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
689 // linker, and so do we.
690 os->set_entsize(4);
693 // Add an entry to the PLT.
695 void
696 Output_data_plt_i386::add_entry(Symbol* gsym)
698 gold_assert(!gsym->has_plt_offset());
700 // Note that when setting the PLT offset we skip the initial
701 // reserved PLT entry.
702 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
704 ++this->count_;
706 section_offset_type got_offset = this->got_plt_->current_data_size();
708 // Every PLT entry needs a GOT entry which points back to the PLT
709 // entry (this will be changed by the dynamic linker, normally
710 // lazily when the function is called).
711 this->got_plt_->set_current_data_size(got_offset + 4);
713 // Every PLT entry needs a reloc.
714 gsym->set_needs_dynsym_entry();
715 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
716 got_offset);
718 // Note that we don't need to save the symbol. The contents of the
719 // PLT are independent of which symbols are used. The symbols only
720 // appear in the relocations.
723 // Return where the TLS_DESC relocations should go, creating it if
724 // necessary. These follow the JUMP_SLOT relocations.
726 Output_data_plt_i386::Reloc_section*
727 Output_data_plt_i386::rel_tls_desc(Layout* layout)
729 if (this->tls_desc_rel_ == NULL)
731 this->tls_desc_rel_ = new Reloc_section(false);
732 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
733 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
734 ORDER_DYNAMIC_PLT_RELOCS, false);
735 gold_assert(this->tls_desc_rel_->output_section() ==
736 this->rel_->output_section());
738 return this->tls_desc_rel_;
741 // The first entry in the PLT for an executable.
743 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
745 0xff, 0x35, // pushl contents of memory address
746 0, 0, 0, 0, // replaced with address of .got + 4
747 0xff, 0x25, // jmp indirect
748 0, 0, 0, 0, // replaced with address of .got + 8
749 0, 0, 0, 0 // unused
752 // The first entry in the PLT for a shared object.
754 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
756 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
757 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
758 0, 0, 0, 0 // unused
761 // Subsequent entries in the PLT for an executable.
763 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
765 0xff, 0x25, // jmp indirect
766 0, 0, 0, 0, // replaced with address of symbol in .got
767 0x68, // pushl immediate
768 0, 0, 0, 0, // replaced with offset into relocation table
769 0xe9, // jmp relative
770 0, 0, 0, 0 // replaced with offset to start of .plt
773 // Subsequent entries in the PLT for a shared object.
775 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
777 0xff, 0xa3, // jmp *offset(%ebx)
778 0, 0, 0, 0, // replaced with offset of symbol in .got
779 0x68, // pushl immediate
780 0, 0, 0, 0, // replaced with offset into relocation table
781 0xe9, // jmp relative
782 0, 0, 0, 0 // replaced with offset to start of .plt
785 // Write out the PLT. This uses the hand-coded instructions above,
786 // and adjusts them as needed. This is all specified by the i386 ELF
787 // Processor Supplement.
789 void
790 Output_data_plt_i386::do_write(Output_file* of)
792 const off_t offset = this->offset();
793 const section_size_type oview_size =
794 convert_to_section_size_type(this->data_size());
795 unsigned char* const oview = of->get_output_view(offset, oview_size);
797 const off_t got_file_offset = this->got_plt_->offset();
798 const section_size_type got_size =
799 convert_to_section_size_type(this->got_plt_->data_size());
800 unsigned char* const got_view = of->get_output_view(got_file_offset,
801 got_size);
803 unsigned char* pov = oview;
805 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
806 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
808 if (parameters->options().output_is_position_independent())
809 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
810 else
812 memcpy(pov, exec_first_plt_entry, plt_entry_size);
813 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
814 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
816 pov += plt_entry_size;
818 unsigned char* got_pov = got_view;
820 memset(got_pov, 0, 12);
821 got_pov += 12;
823 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
825 unsigned int plt_offset = plt_entry_size;
826 unsigned int plt_rel_offset = 0;
827 unsigned int got_offset = 12;
828 const unsigned int count = this->count_;
829 for (unsigned int i = 0;
830 i < count;
831 ++i,
832 pov += plt_entry_size,
833 got_pov += 4,
834 plt_offset += plt_entry_size,
835 plt_rel_offset += rel_size,
836 got_offset += 4)
838 // Set and adjust the PLT entry itself.
840 if (parameters->options().output_is_position_independent())
842 memcpy(pov, dyn_plt_entry, plt_entry_size);
843 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
845 else
847 memcpy(pov, exec_plt_entry, plt_entry_size);
848 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
849 (got_address
850 + got_offset));
853 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
854 elfcpp::Swap<32, false>::writeval(pov + 12,
855 - (plt_offset + plt_entry_size));
857 // Set the entry in the GOT.
858 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
861 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
862 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
864 of->write_output_view(offset, oview_size, oview);
865 of->write_output_view(got_file_offset, got_size, got_view);
868 // Create a PLT entry for a global symbol.
870 void
871 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
873 if (gsym->has_plt_offset())
874 return;
876 if (this->plt_ == NULL)
878 // Create the GOT sections first.
879 this->got_section(symtab, layout);
881 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
882 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
883 (elfcpp::SHF_ALLOC
884 | elfcpp::SHF_EXECINSTR),
885 this->plt_, ORDER_PLT, false);
888 this->plt_->add_entry(gsym);
891 // Return the number of entries in the PLT.
893 unsigned int
894 Target_i386::plt_entry_count() const
896 if (this->plt_ == NULL)
897 return 0;
898 return this->plt_->entry_count();
901 // Return the offset of the first non-reserved PLT entry.
903 unsigned int
904 Target_i386::first_plt_entry_offset() const
906 return Output_data_plt_i386::first_plt_entry_offset();
909 // Return the size of each PLT entry.
911 unsigned int
912 Target_i386::plt_entry_size() const
914 return Output_data_plt_i386::get_plt_entry_size();
917 // Get the section to use for TLS_DESC relocations.
919 Target_i386::Reloc_section*
920 Target_i386::rel_tls_desc_section(Layout* layout) const
922 return this->plt_section()->rel_tls_desc(layout);
925 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
927 void
928 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
930 if (this->tls_base_symbol_defined_)
931 return;
933 Output_segment* tls_segment = layout->tls_segment();
934 if (tls_segment != NULL)
936 bool is_exec = parameters->options().output_is_executable();
937 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
938 Symbol_table::PREDEFINED,
939 tls_segment, 0, 0,
940 elfcpp::STT_TLS,
941 elfcpp::STB_LOCAL,
942 elfcpp::STV_HIDDEN, 0,
943 (is_exec
944 ? Symbol::SEGMENT_END
945 : Symbol::SEGMENT_START),
946 true);
948 this->tls_base_symbol_defined_ = true;
951 // Create a GOT entry for the TLS module index.
953 unsigned int
954 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
955 Sized_relobj<32, false>* object)
957 if (this->got_mod_index_offset_ == -1U)
959 gold_assert(symtab != NULL && layout != NULL && object != NULL);
960 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
961 Output_data_got<32, false>* got = this->got_section(symtab, layout);
962 unsigned int got_offset = got->add_constant(0);
963 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
964 got_offset);
965 got->add_constant(0);
966 this->got_mod_index_offset_ = got_offset;
968 return this->got_mod_index_offset_;
971 // Optimize the TLS relocation type based on what we know about the
972 // symbol. IS_FINAL is true if the final address of this symbol is
973 // known at link time.
975 tls::Tls_optimization
976 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
978 // If we are generating a shared library, then we can't do anything
979 // in the linker.
980 if (parameters->options().shared())
981 return tls::TLSOPT_NONE;
983 switch (r_type)
985 case elfcpp::R_386_TLS_GD:
986 case elfcpp::R_386_TLS_GOTDESC:
987 case elfcpp::R_386_TLS_DESC_CALL:
988 // These are General-Dynamic which permits fully general TLS
989 // access. Since we know that we are generating an executable,
990 // we can convert this to Initial-Exec. If we also know that
991 // this is a local symbol, we can further switch to Local-Exec.
992 if (is_final)
993 return tls::TLSOPT_TO_LE;
994 return tls::TLSOPT_TO_IE;
996 case elfcpp::R_386_TLS_LDM:
997 // This is Local-Dynamic, which refers to a local symbol in the
998 // dynamic TLS block. Since we know that we generating an
999 // executable, we can switch to Local-Exec.
1000 return tls::TLSOPT_TO_LE;
1002 case elfcpp::R_386_TLS_LDO_32:
1003 // Another type of Local-Dynamic relocation.
1004 return tls::TLSOPT_TO_LE;
1006 case elfcpp::R_386_TLS_IE:
1007 case elfcpp::R_386_TLS_GOTIE:
1008 case elfcpp::R_386_TLS_IE_32:
1009 // These are Initial-Exec relocs which get the thread offset
1010 // from the GOT. If we know that we are linking against the
1011 // local symbol, we can switch to Local-Exec, which links the
1012 // thread offset into the instruction.
1013 if (is_final)
1014 return tls::TLSOPT_TO_LE;
1015 return tls::TLSOPT_NONE;
1017 case elfcpp::R_386_TLS_LE:
1018 case elfcpp::R_386_TLS_LE_32:
1019 // When we already have Local-Exec, there is nothing further we
1020 // can do.
1021 return tls::TLSOPT_NONE;
1023 default:
1024 gold_unreachable();
1028 // Report an unsupported relocation against a local symbol.
1030 void
1031 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
1032 unsigned int r_type)
1034 gold_error(_("%s: unsupported reloc %u against local symbol"),
1035 object->name().c_str(), r_type);
1038 // Scan a relocation for a local symbol.
1040 inline void
1041 Target_i386::Scan::local(Symbol_table* symtab,
1042 Layout* layout,
1043 Target_i386* target,
1044 Sized_relobj<32, false>* object,
1045 unsigned int data_shndx,
1046 Output_section* output_section,
1047 const elfcpp::Rel<32, false>& reloc,
1048 unsigned int r_type,
1049 const elfcpp::Sym<32, false>& lsym)
1051 switch (r_type)
1053 case elfcpp::R_386_NONE:
1054 case elfcpp::R_386_GNU_VTINHERIT:
1055 case elfcpp::R_386_GNU_VTENTRY:
1056 break;
1058 case elfcpp::R_386_32:
1059 // If building a shared library (or a position-independent
1060 // executable), we need to create a dynamic relocation for
1061 // this location. The relocation applied at link time will
1062 // apply the link-time value, so we flag the location with
1063 // an R_386_RELATIVE relocation so the dynamic loader can
1064 // relocate it easily.
1065 if (parameters->options().output_is_position_independent())
1067 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1068 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1069 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1070 output_section, data_shndx,
1071 reloc.get_r_offset());
1073 break;
1075 case elfcpp::R_386_16:
1076 case elfcpp::R_386_8:
1077 // If building a shared library (or a position-independent
1078 // executable), we need to create a dynamic relocation for
1079 // this location. Because the addend needs to remain in the
1080 // data section, we need to be careful not to apply this
1081 // relocation statically.
1082 if (parameters->options().output_is_position_independent())
1084 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1085 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1086 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1087 rel_dyn->add_local(object, r_sym, r_type, output_section,
1088 data_shndx, reloc.get_r_offset());
1089 else
1091 gold_assert(lsym.get_st_value() == 0);
1092 unsigned int shndx = lsym.get_st_shndx();
1093 bool is_ordinary;
1094 shndx = object->adjust_sym_shndx(r_sym, shndx,
1095 &is_ordinary);
1096 if (!is_ordinary)
1097 object->error(_("section symbol %u has bad shndx %u"),
1098 r_sym, shndx);
1099 else
1100 rel_dyn->add_local_section(object, shndx,
1101 r_type, output_section,
1102 data_shndx, reloc.get_r_offset());
1105 break;
1107 case elfcpp::R_386_PC32:
1108 case elfcpp::R_386_PC16:
1109 case elfcpp::R_386_PC8:
1110 break;
1112 case elfcpp::R_386_PLT32:
1113 // Since we know this is a local symbol, we can handle this as a
1114 // PC32 reloc.
1115 break;
1117 case elfcpp::R_386_GOTOFF:
1118 case elfcpp::R_386_GOTPC:
1119 // We need a GOT section.
1120 target->got_section(symtab, layout);
1121 break;
1123 case elfcpp::R_386_GOT32:
1125 // The symbol requires a GOT entry.
1126 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1127 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1128 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1130 // If we are generating a shared object, we need to add a
1131 // dynamic RELATIVE relocation for this symbol's GOT entry.
1132 if (parameters->options().output_is_position_independent())
1134 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1135 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1136 rel_dyn->add_local_relative(
1137 object, r_sym, elfcpp::R_386_RELATIVE, got,
1138 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1142 break;
1144 // These are relocations which should only be seen by the
1145 // dynamic linker, and should never be seen here.
1146 case elfcpp::R_386_COPY:
1147 case elfcpp::R_386_GLOB_DAT:
1148 case elfcpp::R_386_JUMP_SLOT:
1149 case elfcpp::R_386_RELATIVE:
1150 case elfcpp::R_386_TLS_TPOFF:
1151 case elfcpp::R_386_TLS_DTPMOD32:
1152 case elfcpp::R_386_TLS_DTPOFF32:
1153 case elfcpp::R_386_TLS_TPOFF32:
1154 case elfcpp::R_386_TLS_DESC:
1155 gold_error(_("%s: unexpected reloc %u in object file"),
1156 object->name().c_str(), r_type);
1157 break;
1159 // These are initial TLS relocs, which are expected when
1160 // linking.
1161 case elfcpp::R_386_TLS_GD: // Global-dynamic
1162 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1163 case elfcpp::R_386_TLS_DESC_CALL:
1164 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1165 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1166 case elfcpp::R_386_TLS_IE: // Initial-exec
1167 case elfcpp::R_386_TLS_IE_32:
1168 case elfcpp::R_386_TLS_GOTIE:
1169 case elfcpp::R_386_TLS_LE: // Local-exec
1170 case elfcpp::R_386_TLS_LE_32:
1172 bool output_is_shared = parameters->options().shared();
1173 const tls::Tls_optimization optimized_type
1174 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1175 switch (r_type)
1177 case elfcpp::R_386_TLS_GD: // Global-dynamic
1178 if (optimized_type == tls::TLSOPT_NONE)
1180 // Create a pair of GOT entries for the module index and
1181 // dtv-relative offset.
1182 Output_data_got<32, false>* got
1183 = target->got_section(symtab, layout);
1184 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1185 unsigned int shndx = lsym.get_st_shndx();
1186 bool is_ordinary;
1187 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1188 if (!is_ordinary)
1189 object->error(_("local symbol %u has bad shndx %u"),
1190 r_sym, shndx);
1191 else
1192 got->add_local_pair_with_rel(object, r_sym, shndx,
1193 GOT_TYPE_TLS_PAIR,
1194 target->rel_dyn_section(layout),
1195 elfcpp::R_386_TLS_DTPMOD32, 0);
1197 else if (optimized_type != tls::TLSOPT_TO_LE)
1198 unsupported_reloc_local(object, r_type);
1199 break;
1201 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1202 target->define_tls_base_symbol(symtab, layout);
1203 if (optimized_type == tls::TLSOPT_NONE)
1205 // Create a double GOT entry with an R_386_TLS_DESC
1206 // reloc. The R_386_TLS_DESC reloc is resolved
1207 // lazily, so the GOT entry needs to be in an area in
1208 // .got.plt, not .got. Call got_section to make sure
1209 // the section has been created.
1210 target->got_section(symtab, layout);
1211 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1212 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1213 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1215 unsigned int got_offset = got->add_constant(0);
1216 // The local symbol value is stored in the second
1217 // GOT entry.
1218 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1219 // That set the GOT offset of the local symbol to
1220 // point to the second entry, but we want it to
1221 // point to the first.
1222 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1223 got_offset);
1224 Reloc_section* rt = target->rel_tls_desc_section(layout);
1225 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1228 else if (optimized_type != tls::TLSOPT_TO_LE)
1229 unsupported_reloc_local(object, r_type);
1230 break;
1232 case elfcpp::R_386_TLS_DESC_CALL:
1233 break;
1235 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1236 if (optimized_type == tls::TLSOPT_NONE)
1238 // Create a GOT entry for the module index.
1239 target->got_mod_index_entry(symtab, layout, object);
1241 else if (optimized_type != tls::TLSOPT_TO_LE)
1242 unsupported_reloc_local(object, r_type);
1243 break;
1245 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1246 break;
1248 case elfcpp::R_386_TLS_IE: // Initial-exec
1249 case elfcpp::R_386_TLS_IE_32:
1250 case elfcpp::R_386_TLS_GOTIE:
1251 layout->set_has_static_tls();
1252 if (optimized_type == tls::TLSOPT_NONE)
1254 // For the R_386_TLS_IE relocation, we need to create a
1255 // dynamic relocation when building a shared library.
1256 if (r_type == elfcpp::R_386_TLS_IE
1257 && parameters->options().shared())
1259 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1260 unsigned int r_sym
1261 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1262 rel_dyn->add_local_relative(object, r_sym,
1263 elfcpp::R_386_RELATIVE,
1264 output_section, data_shndx,
1265 reloc.get_r_offset());
1267 // Create a GOT entry for the tp-relative offset.
1268 Output_data_got<32, false>* got
1269 = target->got_section(symtab, layout);
1270 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1271 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1272 ? elfcpp::R_386_TLS_TPOFF32
1273 : elfcpp::R_386_TLS_TPOFF);
1274 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1275 ? GOT_TYPE_TLS_OFFSET
1276 : GOT_TYPE_TLS_NOFFSET);
1277 got->add_local_with_rel(object, r_sym, got_type,
1278 target->rel_dyn_section(layout),
1279 dyn_r_type);
1281 else if (optimized_type != tls::TLSOPT_TO_LE)
1282 unsupported_reloc_local(object, r_type);
1283 break;
1285 case elfcpp::R_386_TLS_LE: // Local-exec
1286 case elfcpp::R_386_TLS_LE_32:
1287 layout->set_has_static_tls();
1288 if (output_is_shared)
1290 // We need to create a dynamic relocation.
1291 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1292 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1293 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1294 ? elfcpp::R_386_TLS_TPOFF32
1295 : elfcpp::R_386_TLS_TPOFF);
1296 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1297 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1298 data_shndx, reloc.get_r_offset());
1300 break;
1302 default:
1303 gold_unreachable();
1306 break;
1308 case elfcpp::R_386_32PLT:
1309 case elfcpp::R_386_TLS_GD_32:
1310 case elfcpp::R_386_TLS_GD_PUSH:
1311 case elfcpp::R_386_TLS_GD_CALL:
1312 case elfcpp::R_386_TLS_GD_POP:
1313 case elfcpp::R_386_TLS_LDM_32:
1314 case elfcpp::R_386_TLS_LDM_PUSH:
1315 case elfcpp::R_386_TLS_LDM_CALL:
1316 case elfcpp::R_386_TLS_LDM_POP:
1317 case elfcpp::R_386_USED_BY_INTEL_200:
1318 default:
1319 unsupported_reloc_local(object, r_type);
1320 break;
1324 // Report an unsupported relocation against a global symbol.
1326 void
1327 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1328 unsigned int r_type,
1329 Symbol* gsym)
1331 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1332 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1335 inline bool
1336 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1338 switch (r_type)
1340 case elfcpp::R_386_32:
1341 case elfcpp::R_386_16:
1342 case elfcpp::R_386_8:
1343 case elfcpp::R_386_GOTOFF:
1344 case elfcpp::R_386_GOT32:
1346 return true;
1348 default:
1349 return false;
1351 return false;
1354 inline bool
1355 Target_i386::Scan::local_reloc_may_be_function_pointer(
1356 Symbol_table* ,
1357 Layout* ,
1358 Target_i386* ,
1359 Sized_relobj<32, false>* ,
1360 unsigned int ,
1361 Output_section* ,
1362 const elfcpp::Rel<32, false>& ,
1363 unsigned int r_type,
1364 const elfcpp::Sym<32, false>&)
1366 return possible_function_pointer_reloc(r_type);
1369 inline bool
1370 Target_i386::Scan::global_reloc_may_be_function_pointer(
1371 Symbol_table* ,
1372 Layout* ,
1373 Target_i386* ,
1374 Sized_relobj<32, false>* ,
1375 unsigned int ,
1376 Output_section* ,
1377 const elfcpp::Rel<32, false>& ,
1378 unsigned int r_type,
1379 Symbol*)
1381 return possible_function_pointer_reloc(r_type);
1384 // Scan a relocation for a global symbol.
1386 inline void
1387 Target_i386::Scan::global(Symbol_table* symtab,
1388 Layout* layout,
1389 Target_i386* target,
1390 Sized_relobj<32, false>* object,
1391 unsigned int data_shndx,
1392 Output_section* output_section,
1393 const elfcpp::Rel<32, false>& reloc,
1394 unsigned int r_type,
1395 Symbol* gsym)
1397 switch (r_type)
1399 case elfcpp::R_386_NONE:
1400 case elfcpp::R_386_GNU_VTINHERIT:
1401 case elfcpp::R_386_GNU_VTENTRY:
1402 break;
1404 case elfcpp::R_386_32:
1405 case elfcpp::R_386_16:
1406 case elfcpp::R_386_8:
1408 // Make a PLT entry if necessary.
1409 if (gsym->needs_plt_entry())
1411 target->make_plt_entry(symtab, layout, gsym);
1412 // Since this is not a PC-relative relocation, we may be
1413 // taking the address of a function. In that case we need to
1414 // set the entry in the dynamic symbol table to the address of
1415 // the PLT entry.
1416 if (gsym->is_from_dynobj() && !parameters->options().shared())
1417 gsym->set_needs_dynsym_value();
1419 // Make a dynamic relocation if necessary.
1420 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1422 if (gsym->may_need_copy_reloc())
1424 target->copy_reloc(symtab, layout, object,
1425 data_shndx, output_section, gsym, reloc);
1427 else if (r_type == elfcpp::R_386_32
1428 && gsym->can_use_relative_reloc(false))
1430 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1431 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1432 output_section, object,
1433 data_shndx, reloc.get_r_offset());
1435 else
1437 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1438 rel_dyn->add_global(gsym, r_type, output_section, object,
1439 data_shndx, reloc.get_r_offset());
1443 break;
1445 case elfcpp::R_386_PC32:
1446 case elfcpp::R_386_PC16:
1447 case elfcpp::R_386_PC8:
1449 // Make a PLT entry if necessary.
1450 if (gsym->needs_plt_entry())
1452 // These relocations are used for function calls only in
1453 // non-PIC code. For a 32-bit relocation in a shared library,
1454 // we'll need a text relocation anyway, so we can skip the
1455 // PLT entry and let the dynamic linker bind the call directly
1456 // to the target. For smaller relocations, we should use a
1457 // PLT entry to ensure that the call can reach.
1458 if (!parameters->options().shared()
1459 || r_type != elfcpp::R_386_PC32)
1460 target->make_plt_entry(symtab, layout, gsym);
1462 // Make a dynamic relocation if necessary.
1463 int flags = Symbol::NON_PIC_REF;
1464 if (gsym->is_func())
1465 flags |= Symbol::FUNCTION_CALL;
1466 if (gsym->needs_dynamic_reloc(flags))
1468 if (gsym->may_need_copy_reloc())
1470 target->copy_reloc(symtab, layout, object,
1471 data_shndx, output_section, gsym, reloc);
1473 else
1475 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1476 rel_dyn->add_global(gsym, r_type, output_section, object,
1477 data_shndx, reloc.get_r_offset());
1481 break;
1483 case elfcpp::R_386_GOT32:
1485 // The symbol requires a GOT entry.
1486 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1487 if (gsym->final_value_is_known())
1488 got->add_global(gsym, GOT_TYPE_STANDARD);
1489 else
1491 // If this symbol is not fully resolved, we need to add a
1492 // GOT entry with a dynamic relocation.
1493 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1494 if (gsym->is_from_dynobj()
1495 || gsym->is_undefined()
1496 || gsym->is_preemptible())
1497 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1498 rel_dyn, elfcpp::R_386_GLOB_DAT);
1499 else
1501 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1502 rel_dyn->add_global_relative(
1503 gsym, elfcpp::R_386_RELATIVE, got,
1504 gsym->got_offset(GOT_TYPE_STANDARD));
1508 break;
1510 case elfcpp::R_386_PLT32:
1511 // If the symbol is fully resolved, this is just a PC32 reloc.
1512 // Otherwise we need a PLT entry.
1513 if (gsym->final_value_is_known())
1514 break;
1515 // If building a shared library, we can also skip the PLT entry
1516 // if the symbol is defined in the output file and is protected
1517 // or hidden.
1518 if (gsym->is_defined()
1519 && !gsym->is_from_dynobj()
1520 && !gsym->is_preemptible())
1521 break;
1522 target->make_plt_entry(symtab, layout, gsym);
1523 break;
1525 case elfcpp::R_386_GOTOFF:
1526 case elfcpp::R_386_GOTPC:
1527 // We need a GOT section.
1528 target->got_section(symtab, layout);
1529 break;
1531 // These are relocations which should only be seen by the
1532 // dynamic linker, and should never be seen here.
1533 case elfcpp::R_386_COPY:
1534 case elfcpp::R_386_GLOB_DAT:
1535 case elfcpp::R_386_JUMP_SLOT:
1536 case elfcpp::R_386_RELATIVE:
1537 case elfcpp::R_386_TLS_TPOFF:
1538 case elfcpp::R_386_TLS_DTPMOD32:
1539 case elfcpp::R_386_TLS_DTPOFF32:
1540 case elfcpp::R_386_TLS_TPOFF32:
1541 case elfcpp::R_386_TLS_DESC:
1542 gold_error(_("%s: unexpected reloc %u in object file"),
1543 object->name().c_str(), r_type);
1544 break;
1546 // These are initial tls relocs, which are expected when
1547 // linking.
1548 case elfcpp::R_386_TLS_GD: // Global-dynamic
1549 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1550 case elfcpp::R_386_TLS_DESC_CALL:
1551 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1552 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1553 case elfcpp::R_386_TLS_IE: // Initial-exec
1554 case elfcpp::R_386_TLS_IE_32:
1555 case elfcpp::R_386_TLS_GOTIE:
1556 case elfcpp::R_386_TLS_LE: // Local-exec
1557 case elfcpp::R_386_TLS_LE_32:
1559 const bool is_final = gsym->final_value_is_known();
1560 const tls::Tls_optimization optimized_type
1561 = Target_i386::optimize_tls_reloc(is_final, r_type);
1562 switch (r_type)
1564 case elfcpp::R_386_TLS_GD: // Global-dynamic
1565 if (optimized_type == tls::TLSOPT_NONE)
1567 // Create a pair of GOT entries for the module index and
1568 // dtv-relative offset.
1569 Output_data_got<32, false>* got
1570 = target->got_section(symtab, layout);
1571 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1572 target->rel_dyn_section(layout),
1573 elfcpp::R_386_TLS_DTPMOD32,
1574 elfcpp::R_386_TLS_DTPOFF32);
1576 else if (optimized_type == tls::TLSOPT_TO_IE)
1578 // Create a GOT entry for the tp-relative offset.
1579 Output_data_got<32, false>* got
1580 = target->got_section(symtab, layout);
1581 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1582 target->rel_dyn_section(layout),
1583 elfcpp::R_386_TLS_TPOFF);
1585 else if (optimized_type != tls::TLSOPT_TO_LE)
1586 unsupported_reloc_global(object, r_type, gsym);
1587 break;
1589 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1590 target->define_tls_base_symbol(symtab, layout);
1591 if (optimized_type == tls::TLSOPT_NONE)
1593 // Create a double GOT entry with an R_386_TLS_DESC
1594 // reloc. The R_386_TLS_DESC reloc is resolved
1595 // lazily, so the GOT entry needs to be in an area in
1596 // .got.plt, not .got. Call got_section to make sure
1597 // the section has been created.
1598 target->got_section(symtab, layout);
1599 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1600 Reloc_section* rt = target->rel_tls_desc_section(layout);
1601 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1602 elfcpp::R_386_TLS_DESC, 0);
1604 else if (optimized_type == tls::TLSOPT_TO_IE)
1606 // Create a GOT entry for the tp-relative offset.
1607 Output_data_got<32, false>* got
1608 = target->got_section(symtab, layout);
1609 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1610 target->rel_dyn_section(layout),
1611 elfcpp::R_386_TLS_TPOFF);
1613 else if (optimized_type != tls::TLSOPT_TO_LE)
1614 unsupported_reloc_global(object, r_type, gsym);
1615 break;
1617 case elfcpp::R_386_TLS_DESC_CALL:
1618 break;
1620 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1621 if (optimized_type == tls::TLSOPT_NONE)
1623 // Create a GOT entry for the module index.
1624 target->got_mod_index_entry(symtab, layout, object);
1626 else if (optimized_type != tls::TLSOPT_TO_LE)
1627 unsupported_reloc_global(object, r_type, gsym);
1628 break;
1630 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1631 break;
1633 case elfcpp::R_386_TLS_IE: // Initial-exec
1634 case elfcpp::R_386_TLS_IE_32:
1635 case elfcpp::R_386_TLS_GOTIE:
1636 layout->set_has_static_tls();
1637 if (optimized_type == tls::TLSOPT_NONE)
1639 // For the R_386_TLS_IE relocation, we need to create a
1640 // dynamic relocation when building a shared library.
1641 if (r_type == elfcpp::R_386_TLS_IE
1642 && parameters->options().shared())
1644 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1645 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1646 output_section, object,
1647 data_shndx,
1648 reloc.get_r_offset());
1650 // Create a GOT entry for the tp-relative offset.
1651 Output_data_got<32, false>* got
1652 = target->got_section(symtab, layout);
1653 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1654 ? elfcpp::R_386_TLS_TPOFF32
1655 : elfcpp::R_386_TLS_TPOFF);
1656 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1657 ? GOT_TYPE_TLS_OFFSET
1658 : GOT_TYPE_TLS_NOFFSET);
1659 got->add_global_with_rel(gsym, got_type,
1660 target->rel_dyn_section(layout),
1661 dyn_r_type);
1663 else if (optimized_type != tls::TLSOPT_TO_LE)
1664 unsupported_reloc_global(object, r_type, gsym);
1665 break;
1667 case elfcpp::R_386_TLS_LE: // Local-exec
1668 case elfcpp::R_386_TLS_LE_32:
1669 layout->set_has_static_tls();
1670 if (parameters->options().shared())
1672 // We need to create a dynamic relocation.
1673 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1674 ? elfcpp::R_386_TLS_TPOFF32
1675 : elfcpp::R_386_TLS_TPOFF);
1676 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1677 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1678 data_shndx, reloc.get_r_offset());
1680 break;
1682 default:
1683 gold_unreachable();
1686 break;
1688 case elfcpp::R_386_32PLT:
1689 case elfcpp::R_386_TLS_GD_32:
1690 case elfcpp::R_386_TLS_GD_PUSH:
1691 case elfcpp::R_386_TLS_GD_CALL:
1692 case elfcpp::R_386_TLS_GD_POP:
1693 case elfcpp::R_386_TLS_LDM_32:
1694 case elfcpp::R_386_TLS_LDM_PUSH:
1695 case elfcpp::R_386_TLS_LDM_CALL:
1696 case elfcpp::R_386_TLS_LDM_POP:
1697 case elfcpp::R_386_USED_BY_INTEL_200:
1698 default:
1699 unsupported_reloc_global(object, r_type, gsym);
1700 break;
1704 // Process relocations for gc.
1706 void
1707 Target_i386::gc_process_relocs(Symbol_table* symtab,
1708 Layout* layout,
1709 Sized_relobj<32, false>* object,
1710 unsigned int data_shndx,
1711 unsigned int,
1712 const unsigned char* prelocs,
1713 size_t reloc_count,
1714 Output_section* output_section,
1715 bool needs_special_offset_handling,
1716 size_t local_symbol_count,
1717 const unsigned char* plocal_symbols)
1719 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1720 Target_i386::Scan,
1721 Target_i386::Relocatable_size_for_reloc>(
1722 symtab,
1723 layout,
1724 this,
1725 object,
1726 data_shndx,
1727 prelocs,
1728 reloc_count,
1729 output_section,
1730 needs_special_offset_handling,
1731 local_symbol_count,
1732 plocal_symbols);
1735 // Scan relocations for a section.
1737 void
1738 Target_i386::scan_relocs(Symbol_table* symtab,
1739 Layout* layout,
1740 Sized_relobj<32, false>* object,
1741 unsigned int data_shndx,
1742 unsigned int sh_type,
1743 const unsigned char* prelocs,
1744 size_t reloc_count,
1745 Output_section* output_section,
1746 bool needs_special_offset_handling,
1747 size_t local_symbol_count,
1748 const unsigned char* plocal_symbols)
1750 if (sh_type == elfcpp::SHT_RELA)
1752 gold_error(_("%s: unsupported RELA reloc section"),
1753 object->name().c_str());
1754 return;
1757 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1758 Target_i386::Scan>(
1759 symtab,
1760 layout,
1761 this,
1762 object,
1763 data_shndx,
1764 prelocs,
1765 reloc_count,
1766 output_section,
1767 needs_special_offset_handling,
1768 local_symbol_count,
1769 plocal_symbols);
1772 // Finalize the sections.
1774 void
1775 Target_i386::do_finalize_sections(
1776 Layout* layout,
1777 const Input_objects*,
1778 Symbol_table* symtab)
1780 const Reloc_section* rel_plt = (this->plt_ == NULL
1781 ? NULL
1782 : this->plt_->rel_plt());
1783 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
1784 this->rel_dyn_, true, false);
1786 // Emit any relocs we saved in an attempt to avoid generating COPY
1787 // relocs.
1788 if (this->copy_relocs_.any_saved_relocs())
1789 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1791 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1792 // the .got.plt section.
1793 Symbol* sym = this->global_offset_table_;
1794 if (sym != NULL)
1796 uint32_t data_size = this->got_plt_->current_data_size();
1797 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
1801 // Return whether a direct absolute static relocation needs to be applied.
1802 // In cases where Scan::local() or Scan::global() has created
1803 // a dynamic relocation other than R_386_RELATIVE, the addend
1804 // of the relocation is carried in the data, and we must not
1805 // apply the static relocation.
1807 inline bool
1808 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1809 int ref_flags,
1810 bool is_32bit,
1811 Output_section* output_section)
1813 // If the output section is not allocated, then we didn't call
1814 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1815 // the reloc here.
1816 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1817 return true;
1819 // For local symbols, we will have created a non-RELATIVE dynamic
1820 // relocation only if (a) the output is position independent,
1821 // (b) the relocation is absolute (not pc- or segment-relative), and
1822 // (c) the relocation is not 32 bits wide.
1823 if (gsym == NULL)
1824 return !(parameters->options().output_is_position_independent()
1825 && (ref_flags & Symbol::ABSOLUTE_REF)
1826 && !is_32bit);
1828 // For global symbols, we use the same helper routines used in the
1829 // scan pass. If we did not create a dynamic relocation, or if we
1830 // created a RELATIVE dynamic relocation, we should apply the static
1831 // relocation.
1832 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1833 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1834 && gsym->can_use_relative_reloc(ref_flags
1835 & Symbol::FUNCTION_CALL);
1836 return !has_dyn || is_rel;
1839 // Perform a relocation.
1841 inline bool
1842 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1843 Target_i386* target,
1844 Output_section *output_section,
1845 size_t relnum,
1846 const elfcpp::Rel<32, false>& rel,
1847 unsigned int r_type,
1848 const Sized_symbol<32>* gsym,
1849 const Symbol_value<32>* psymval,
1850 unsigned char* view,
1851 elfcpp::Elf_types<32>::Elf_Addr address,
1852 section_size_type view_size)
1854 if (this->skip_call_tls_get_addr_)
1856 if ((r_type != elfcpp::R_386_PLT32
1857 && r_type != elfcpp::R_386_PC32)
1858 || gsym == NULL
1859 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1860 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1861 _("missing expected TLS relocation"));
1862 else
1864 this->skip_call_tls_get_addr_ = false;
1865 return false;
1869 // Pick the value to use for symbols defined in shared objects.
1870 Symbol_value<32> symval;
1871 if (gsym != NULL
1872 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1873 || r_type == elfcpp::R_386_PC16
1874 || r_type == elfcpp::R_386_PC32))
1876 symval.set_output_value(target->plt_section()->address()
1877 + gsym->plt_offset());
1878 psymval = &symval;
1881 const Sized_relobj<32, false>* object = relinfo->object;
1883 // Get the GOT offset if needed.
1884 // The GOT pointer points to the end of the GOT section.
1885 // We need to subtract the size of the GOT section to get
1886 // the actual offset to use in the relocation.
1887 bool have_got_offset = false;
1888 unsigned int got_offset = 0;
1889 switch (r_type)
1891 case elfcpp::R_386_GOT32:
1892 if (gsym != NULL)
1894 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1895 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1896 - target->got_size());
1898 else
1900 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1901 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1902 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1903 - target->got_size());
1905 have_got_offset = true;
1906 break;
1908 default:
1909 break;
1912 switch (r_type)
1914 case elfcpp::R_386_NONE:
1915 case elfcpp::R_386_GNU_VTINHERIT:
1916 case elfcpp::R_386_GNU_VTENTRY:
1917 break;
1919 case elfcpp::R_386_32:
1920 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1921 output_section))
1922 Relocate_functions<32, false>::rel32(view, object, psymval);
1923 break;
1925 case elfcpp::R_386_PC32:
1927 int ref_flags = Symbol::NON_PIC_REF;
1928 if (gsym != NULL && gsym->is_func())
1929 ref_flags |= Symbol::FUNCTION_CALL;
1930 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1931 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1933 break;
1935 case elfcpp::R_386_16:
1936 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1937 output_section))
1938 Relocate_functions<32, false>::rel16(view, object, psymval);
1939 break;
1941 case elfcpp::R_386_PC16:
1943 int ref_flags = Symbol::NON_PIC_REF;
1944 if (gsym != NULL && gsym->is_func())
1945 ref_flags |= Symbol::FUNCTION_CALL;
1946 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1947 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1949 break;
1951 case elfcpp::R_386_8:
1952 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1953 output_section))
1954 Relocate_functions<32, false>::rel8(view, object, psymval);
1955 break;
1957 case elfcpp::R_386_PC8:
1959 int ref_flags = Symbol::NON_PIC_REF;
1960 if (gsym != NULL && gsym->is_func())
1961 ref_flags |= Symbol::FUNCTION_CALL;
1962 if (should_apply_static_reloc(gsym, ref_flags, false,
1963 output_section))
1964 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1966 break;
1968 case elfcpp::R_386_PLT32:
1969 gold_assert(gsym == NULL
1970 || gsym->has_plt_offset()
1971 || gsym->final_value_is_known()
1972 || (gsym->is_defined()
1973 && !gsym->is_from_dynobj()
1974 && !gsym->is_preemptible()));
1975 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1976 break;
1978 case elfcpp::R_386_GOT32:
1979 gold_assert(have_got_offset);
1980 Relocate_functions<32, false>::rel32(view, got_offset);
1981 break;
1983 case elfcpp::R_386_GOTOFF:
1985 elfcpp::Elf_types<32>::Elf_Addr value;
1986 value = (psymval->value(object, 0)
1987 - target->got_plt_section()->address());
1988 Relocate_functions<32, false>::rel32(view, value);
1990 break;
1992 case elfcpp::R_386_GOTPC:
1994 elfcpp::Elf_types<32>::Elf_Addr value;
1995 value = target->got_plt_section()->address();
1996 Relocate_functions<32, false>::pcrel32(view, value, address);
1998 break;
2000 case elfcpp::R_386_COPY:
2001 case elfcpp::R_386_GLOB_DAT:
2002 case elfcpp::R_386_JUMP_SLOT:
2003 case elfcpp::R_386_RELATIVE:
2004 // These are outstanding tls relocs, which are unexpected when
2005 // linking.
2006 case elfcpp::R_386_TLS_TPOFF:
2007 case elfcpp::R_386_TLS_DTPMOD32:
2008 case elfcpp::R_386_TLS_DTPOFF32:
2009 case elfcpp::R_386_TLS_TPOFF32:
2010 case elfcpp::R_386_TLS_DESC:
2011 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2012 _("unexpected reloc %u in object file"),
2013 r_type);
2014 break;
2016 // These are initial tls relocs, which are expected when
2017 // linking.
2018 case elfcpp::R_386_TLS_GD: // Global-dynamic
2019 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2020 case elfcpp::R_386_TLS_DESC_CALL:
2021 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2022 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2023 case elfcpp::R_386_TLS_IE: // Initial-exec
2024 case elfcpp::R_386_TLS_IE_32:
2025 case elfcpp::R_386_TLS_GOTIE:
2026 case elfcpp::R_386_TLS_LE: // Local-exec
2027 case elfcpp::R_386_TLS_LE_32:
2028 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2029 view, address, view_size);
2030 break;
2032 case elfcpp::R_386_32PLT:
2033 case elfcpp::R_386_TLS_GD_32:
2034 case elfcpp::R_386_TLS_GD_PUSH:
2035 case elfcpp::R_386_TLS_GD_CALL:
2036 case elfcpp::R_386_TLS_GD_POP:
2037 case elfcpp::R_386_TLS_LDM_32:
2038 case elfcpp::R_386_TLS_LDM_PUSH:
2039 case elfcpp::R_386_TLS_LDM_CALL:
2040 case elfcpp::R_386_TLS_LDM_POP:
2041 case elfcpp::R_386_USED_BY_INTEL_200:
2042 default:
2043 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2044 _("unsupported reloc %u"),
2045 r_type);
2046 break;
2049 return true;
2052 // Perform a TLS relocation.
2054 inline void
2055 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2056 Target_i386* target,
2057 size_t relnum,
2058 const elfcpp::Rel<32, false>& rel,
2059 unsigned int r_type,
2060 const Sized_symbol<32>* gsym,
2061 const Symbol_value<32>* psymval,
2062 unsigned char* view,
2063 elfcpp::Elf_types<32>::Elf_Addr,
2064 section_size_type view_size)
2066 Output_segment* tls_segment = relinfo->layout->tls_segment();
2068 const Sized_relobj<32, false>* object = relinfo->object;
2070 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2072 const bool is_final = (gsym == NULL
2073 ? !parameters->options().shared()
2074 : gsym->final_value_is_known());
2075 const tls::Tls_optimization optimized_type
2076 = Target_i386::optimize_tls_reloc(is_final, r_type);
2077 switch (r_type)
2079 case elfcpp::R_386_TLS_GD: // Global-dynamic
2080 if (optimized_type == tls::TLSOPT_TO_LE)
2082 gold_assert(tls_segment != NULL);
2083 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2084 rel, r_type, value, view,
2085 view_size);
2086 break;
2088 else
2090 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2091 ? GOT_TYPE_TLS_NOFFSET
2092 : GOT_TYPE_TLS_PAIR);
2093 unsigned int got_offset;
2094 if (gsym != NULL)
2096 gold_assert(gsym->has_got_offset(got_type));
2097 got_offset = gsym->got_offset(got_type) - target->got_size();
2099 else
2101 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2102 gold_assert(object->local_has_got_offset(r_sym, got_type));
2103 got_offset = (object->local_got_offset(r_sym, got_type)
2104 - target->got_size());
2106 if (optimized_type == tls::TLSOPT_TO_IE)
2108 gold_assert(tls_segment != NULL);
2109 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2110 got_offset, view, view_size);
2111 break;
2113 else if (optimized_type == tls::TLSOPT_NONE)
2115 // Relocate the field with the offset of the pair of GOT
2116 // entries.
2117 Relocate_functions<32, false>::rel32(view, got_offset);
2118 break;
2121 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2122 _("unsupported reloc %u"),
2123 r_type);
2124 break;
2126 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2127 case elfcpp::R_386_TLS_DESC_CALL:
2128 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2129 if (optimized_type == tls::TLSOPT_TO_LE)
2131 gold_assert(tls_segment != NULL);
2132 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2133 rel, r_type, value, view,
2134 view_size);
2135 break;
2137 else
2139 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2140 ? GOT_TYPE_TLS_NOFFSET
2141 : GOT_TYPE_TLS_DESC);
2142 unsigned int got_offset = 0;
2143 if (r_type == elfcpp::R_386_TLS_GOTDESC
2144 && optimized_type == tls::TLSOPT_NONE)
2146 // We created GOT entries in the .got.tlsdesc portion of
2147 // the .got.plt section, but the offset stored in the
2148 // symbol is the offset within .got.tlsdesc.
2149 got_offset = (target->got_size()
2150 + target->got_plt_section()->data_size());
2152 if (gsym != NULL)
2154 gold_assert(gsym->has_got_offset(got_type));
2155 got_offset += gsym->got_offset(got_type) - target->got_size();
2157 else
2159 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2160 gold_assert(object->local_has_got_offset(r_sym, got_type));
2161 got_offset += (object->local_got_offset(r_sym, got_type)
2162 - target->got_size());
2164 if (optimized_type == tls::TLSOPT_TO_IE)
2166 gold_assert(tls_segment != NULL);
2167 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2168 got_offset, view, view_size);
2169 break;
2171 else if (optimized_type == tls::TLSOPT_NONE)
2173 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2175 // Relocate the field with the offset of the pair of GOT
2176 // entries.
2177 Relocate_functions<32, false>::rel32(view, got_offset);
2179 break;
2182 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2183 _("unsupported reloc %u"),
2184 r_type);
2185 break;
2187 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2188 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2190 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2191 _("both SUN and GNU model "
2192 "TLS relocations"));
2193 break;
2195 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2196 if (optimized_type == tls::TLSOPT_TO_LE)
2198 gold_assert(tls_segment != NULL);
2199 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2200 value, view, view_size);
2201 break;
2203 else if (optimized_type == tls::TLSOPT_NONE)
2205 // Relocate the field with the offset of the GOT entry for
2206 // the module index.
2207 unsigned int got_offset;
2208 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2209 - target->got_size());
2210 Relocate_functions<32, false>::rel32(view, got_offset);
2211 break;
2213 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2214 _("unsupported reloc %u"),
2215 r_type);
2216 break;
2218 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2219 if (optimized_type == tls::TLSOPT_TO_LE)
2221 // This reloc can appear in debugging sections, in which
2222 // case we must not convert to local-exec. We decide what
2223 // to do based on whether the section is marked as
2224 // containing executable code. That is what the GNU linker
2225 // does as well.
2226 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2227 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2229 gold_assert(tls_segment != NULL);
2230 value -= tls_segment->memsz();
2233 Relocate_functions<32, false>::rel32(view, value);
2234 break;
2236 case elfcpp::R_386_TLS_IE: // Initial-exec
2237 case elfcpp::R_386_TLS_GOTIE:
2238 case elfcpp::R_386_TLS_IE_32:
2239 if (optimized_type == tls::TLSOPT_TO_LE)
2241 gold_assert(tls_segment != NULL);
2242 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2243 rel, r_type, value, view,
2244 view_size);
2245 break;
2247 else if (optimized_type == tls::TLSOPT_NONE)
2249 // Relocate the field with the offset of the GOT entry for
2250 // the tp-relative offset of the symbol.
2251 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2252 ? GOT_TYPE_TLS_OFFSET
2253 : GOT_TYPE_TLS_NOFFSET);
2254 unsigned int got_offset;
2255 if (gsym != NULL)
2257 gold_assert(gsym->has_got_offset(got_type));
2258 got_offset = gsym->got_offset(got_type);
2260 else
2262 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2263 gold_assert(object->local_has_got_offset(r_sym, got_type));
2264 got_offset = object->local_got_offset(r_sym, got_type);
2266 // For the R_386_TLS_IE relocation, we need to apply the
2267 // absolute address of the GOT entry.
2268 if (r_type == elfcpp::R_386_TLS_IE)
2269 got_offset += target->got_plt_section()->address();
2270 // All GOT offsets are relative to the end of the GOT.
2271 got_offset -= target->got_size();
2272 Relocate_functions<32, false>::rel32(view, got_offset);
2273 break;
2275 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2276 _("unsupported reloc %u"),
2277 r_type);
2278 break;
2280 case elfcpp::R_386_TLS_LE: // Local-exec
2281 // If we're creating a shared library, a dynamic relocation will
2282 // have been created for this location, so do not apply it now.
2283 if (!parameters->options().shared())
2285 gold_assert(tls_segment != NULL);
2286 value -= tls_segment->memsz();
2287 Relocate_functions<32, false>::rel32(view, value);
2289 break;
2291 case elfcpp::R_386_TLS_LE_32:
2292 // If we're creating a shared library, a dynamic relocation will
2293 // have been created for this location, so do not apply it now.
2294 if (!parameters->options().shared())
2296 gold_assert(tls_segment != NULL);
2297 value = tls_segment->memsz() - value;
2298 Relocate_functions<32, false>::rel32(view, value);
2300 break;
2304 // Do a relocation in which we convert a TLS General-Dynamic to a
2305 // Local-Exec.
2307 inline void
2308 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2309 size_t relnum,
2310 Output_segment* tls_segment,
2311 const elfcpp::Rel<32, false>& rel,
2312 unsigned int,
2313 elfcpp::Elf_types<32>::Elf_Addr value,
2314 unsigned char* view,
2315 section_size_type view_size)
2317 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2318 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2319 // leal foo(%reg),%eax; call ___tls_get_addr
2320 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2322 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2323 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2325 unsigned char op1 = view[-1];
2326 unsigned char op2 = view[-2];
2328 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2329 op2 == 0x8d || op2 == 0x04);
2330 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2332 int roff = 5;
2334 if (op2 == 0x04)
2336 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2337 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2338 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2339 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2340 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2342 else
2344 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2345 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2346 if (rel.get_r_offset() + 9 < view_size
2347 && view[9] == 0x90)
2349 // There is a trailing nop. Use the size byte subl.
2350 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2351 roff = 6;
2353 else
2355 // Use the five byte subl.
2356 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2360 value = tls_segment->memsz() - value;
2361 Relocate_functions<32, false>::rel32(view + roff, value);
2363 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2364 // We can skip it.
2365 this->skip_call_tls_get_addr_ = true;
2368 // Do a relocation in which we convert a TLS General-Dynamic to an
2369 // Initial-Exec.
2371 inline void
2372 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2373 size_t relnum,
2374 Output_segment*,
2375 const elfcpp::Rel<32, false>& rel,
2376 unsigned int,
2377 elfcpp::Elf_types<32>::Elf_Addr value,
2378 unsigned char* view,
2379 section_size_type view_size)
2381 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2382 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2384 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2385 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2387 unsigned char op1 = view[-1];
2388 unsigned char op2 = view[-2];
2390 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2391 op2 == 0x8d || op2 == 0x04);
2392 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2394 int roff = 5;
2396 // FIXME: For now, support only the first (SIB) form.
2397 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2399 if (op2 == 0x04)
2401 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2402 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2403 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2404 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2405 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2407 else
2409 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2410 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2411 if (rel.get_r_offset() + 9 < view_size
2412 && view[9] == 0x90)
2414 // FIXME: This is not the right instruction sequence.
2415 // There is a trailing nop. Use the size byte subl.
2416 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2417 roff = 6;
2419 else
2421 // FIXME: This is not the right instruction sequence.
2422 // Use the five byte subl.
2423 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2427 Relocate_functions<32, false>::rel32(view + roff, value);
2429 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2430 // We can skip it.
2431 this->skip_call_tls_get_addr_ = true;
2434 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2435 // General-Dynamic to a Local-Exec.
2437 inline void
2438 Target_i386::Relocate::tls_desc_gd_to_le(
2439 const Relocate_info<32, false>* relinfo,
2440 size_t relnum,
2441 Output_segment* tls_segment,
2442 const elfcpp::Rel<32, false>& rel,
2443 unsigned int r_type,
2444 elfcpp::Elf_types<32>::Elf_Addr value,
2445 unsigned char* view,
2446 section_size_type view_size)
2448 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2450 // leal foo@TLSDESC(%ebx), %eax
2451 // ==> leal foo@NTPOFF, %eax
2452 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2453 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2454 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2455 view[-2] == 0x8d && view[-1] == 0x83);
2456 view[-1] = 0x05;
2457 value -= tls_segment->memsz();
2458 Relocate_functions<32, false>::rel32(view, value);
2460 else
2462 // call *foo@TLSCALL(%eax)
2463 // ==> nop; nop
2464 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2465 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2466 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2467 view[0] == 0xff && view[1] == 0x10);
2468 view[0] = 0x66;
2469 view[1] = 0x90;
2473 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2474 // General-Dynamic to an Initial-Exec.
2476 inline void
2477 Target_i386::Relocate::tls_desc_gd_to_ie(
2478 const Relocate_info<32, false>* relinfo,
2479 size_t relnum,
2480 Output_segment*,
2481 const elfcpp::Rel<32, false>& rel,
2482 unsigned int r_type,
2483 elfcpp::Elf_types<32>::Elf_Addr value,
2484 unsigned char* view,
2485 section_size_type view_size)
2487 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2489 // leal foo@TLSDESC(%ebx), %eax
2490 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2491 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2492 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2493 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2494 view[-2] == 0x8d && view[-1] == 0x83);
2495 view[-2] = 0x8b;
2496 Relocate_functions<32, false>::rel32(view, value);
2498 else
2500 // call *foo@TLSCALL(%eax)
2501 // ==> nop; nop
2502 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2503 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2504 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2505 view[0] == 0xff && view[1] == 0x10);
2506 view[0] = 0x66;
2507 view[1] = 0x90;
2511 // Do a relocation in which we convert a TLS Local-Dynamic to a
2512 // Local-Exec.
2514 inline void
2515 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2516 size_t relnum,
2517 Output_segment*,
2518 const elfcpp::Rel<32, false>& rel,
2519 unsigned int,
2520 elfcpp::Elf_types<32>::Elf_Addr,
2521 unsigned char* view,
2522 section_size_type view_size)
2524 // leal foo(%reg), %eax; call ___tls_get_addr
2525 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2527 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2528 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2530 // FIXME: Does this test really always pass?
2531 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2532 view[-2] == 0x8d && view[-1] == 0x83);
2534 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2536 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2538 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2539 // We can skip it.
2540 this->skip_call_tls_get_addr_ = true;
2543 // Do a relocation in which we convert a TLS Initial-Exec to a
2544 // Local-Exec.
2546 inline void
2547 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2548 size_t relnum,
2549 Output_segment* tls_segment,
2550 const elfcpp::Rel<32, false>& rel,
2551 unsigned int r_type,
2552 elfcpp::Elf_types<32>::Elf_Addr value,
2553 unsigned char* view,
2554 section_size_type view_size)
2556 // We have to actually change the instructions, which means that we
2557 // need to examine the opcodes to figure out which instruction we
2558 // are looking at.
2559 if (r_type == elfcpp::R_386_TLS_IE)
2561 // movl %gs:XX,%eax ==> movl $YY,%eax
2562 // movl %gs:XX,%reg ==> movl $YY,%reg
2563 // addl %gs:XX,%reg ==> addl $YY,%reg
2564 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2565 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2567 unsigned char op1 = view[-1];
2568 if (op1 == 0xa1)
2570 // movl XX,%eax ==> movl $YY,%eax
2571 view[-1] = 0xb8;
2573 else
2575 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2577 unsigned char op2 = view[-2];
2578 if (op2 == 0x8b)
2580 // movl XX,%reg ==> movl $YY,%reg
2581 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2582 (op1 & 0xc7) == 0x05);
2583 view[-2] = 0xc7;
2584 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2586 else if (op2 == 0x03)
2588 // addl XX,%reg ==> addl $YY,%reg
2589 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2590 (op1 & 0xc7) == 0x05);
2591 view[-2] = 0x81;
2592 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2594 else
2595 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2598 else
2600 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2601 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2602 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2603 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2604 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2606 unsigned char op1 = view[-1];
2607 unsigned char op2 = view[-2];
2608 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2609 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2610 if (op2 == 0x8b)
2612 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2613 view[-2] = 0xc7;
2614 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2616 else if (op2 == 0x2b)
2618 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2619 view[-2] = 0x81;
2620 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2622 else if (op2 == 0x03)
2624 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2625 view[-2] = 0x81;
2626 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2628 else
2629 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2632 value = tls_segment->memsz() - value;
2633 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2634 value = - value;
2636 Relocate_functions<32, false>::rel32(view, value);
2639 // Relocate section data.
2641 void
2642 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2643 unsigned int sh_type,
2644 const unsigned char* prelocs,
2645 size_t reloc_count,
2646 Output_section* output_section,
2647 bool needs_special_offset_handling,
2648 unsigned char* view,
2649 elfcpp::Elf_types<32>::Elf_Addr address,
2650 section_size_type view_size,
2651 const Reloc_symbol_changes* reloc_symbol_changes)
2653 gold_assert(sh_type == elfcpp::SHT_REL);
2655 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2656 Target_i386::Relocate>(
2657 relinfo,
2658 this,
2659 prelocs,
2660 reloc_count,
2661 output_section,
2662 needs_special_offset_handling,
2663 view,
2664 address,
2665 view_size,
2666 reloc_symbol_changes);
2669 // Return the size of a relocation while scanning during a relocatable
2670 // link.
2672 unsigned int
2673 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2674 unsigned int r_type,
2675 Relobj* object)
2677 switch (r_type)
2679 case elfcpp::R_386_NONE:
2680 case elfcpp::R_386_GNU_VTINHERIT:
2681 case elfcpp::R_386_GNU_VTENTRY:
2682 case elfcpp::R_386_TLS_GD: // Global-dynamic
2683 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2684 case elfcpp::R_386_TLS_DESC_CALL:
2685 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2686 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2687 case elfcpp::R_386_TLS_IE: // Initial-exec
2688 case elfcpp::R_386_TLS_IE_32:
2689 case elfcpp::R_386_TLS_GOTIE:
2690 case elfcpp::R_386_TLS_LE: // Local-exec
2691 case elfcpp::R_386_TLS_LE_32:
2692 return 0;
2694 case elfcpp::R_386_32:
2695 case elfcpp::R_386_PC32:
2696 case elfcpp::R_386_GOT32:
2697 case elfcpp::R_386_PLT32:
2698 case elfcpp::R_386_GOTOFF:
2699 case elfcpp::R_386_GOTPC:
2700 return 4;
2702 case elfcpp::R_386_16:
2703 case elfcpp::R_386_PC16:
2704 return 2;
2706 case elfcpp::R_386_8:
2707 case elfcpp::R_386_PC8:
2708 return 1;
2710 // These are relocations which should only be seen by the
2711 // dynamic linker, and should never be seen here.
2712 case elfcpp::R_386_COPY:
2713 case elfcpp::R_386_GLOB_DAT:
2714 case elfcpp::R_386_JUMP_SLOT:
2715 case elfcpp::R_386_RELATIVE:
2716 case elfcpp::R_386_TLS_TPOFF:
2717 case elfcpp::R_386_TLS_DTPMOD32:
2718 case elfcpp::R_386_TLS_DTPOFF32:
2719 case elfcpp::R_386_TLS_TPOFF32:
2720 case elfcpp::R_386_TLS_DESC:
2721 object->error(_("unexpected reloc %u in object file"), r_type);
2722 return 0;
2724 case elfcpp::R_386_32PLT:
2725 case elfcpp::R_386_TLS_GD_32:
2726 case elfcpp::R_386_TLS_GD_PUSH:
2727 case elfcpp::R_386_TLS_GD_CALL:
2728 case elfcpp::R_386_TLS_GD_POP:
2729 case elfcpp::R_386_TLS_LDM_32:
2730 case elfcpp::R_386_TLS_LDM_PUSH:
2731 case elfcpp::R_386_TLS_LDM_CALL:
2732 case elfcpp::R_386_TLS_LDM_POP:
2733 case elfcpp::R_386_USED_BY_INTEL_200:
2734 default:
2735 object->error(_("unsupported reloc %u in object file"), r_type);
2736 return 0;
2740 // Scan the relocs during a relocatable link.
2742 void
2743 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
2744 Layout* layout,
2745 Sized_relobj<32, false>* object,
2746 unsigned int data_shndx,
2747 unsigned int sh_type,
2748 const unsigned char* prelocs,
2749 size_t reloc_count,
2750 Output_section* output_section,
2751 bool needs_special_offset_handling,
2752 size_t local_symbol_count,
2753 const unsigned char* plocal_symbols,
2754 Relocatable_relocs* rr)
2756 gold_assert(sh_type == elfcpp::SHT_REL);
2758 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2759 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2761 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2762 Scan_relocatable_relocs>(
2763 symtab,
2764 layout,
2765 object,
2766 data_shndx,
2767 prelocs,
2768 reloc_count,
2769 output_section,
2770 needs_special_offset_handling,
2771 local_symbol_count,
2772 plocal_symbols,
2773 rr);
2776 // Relocate a section during a relocatable link.
2778 void
2779 Target_i386::relocate_for_relocatable(
2780 const Relocate_info<32, false>* relinfo,
2781 unsigned int sh_type,
2782 const unsigned char* prelocs,
2783 size_t reloc_count,
2784 Output_section* output_section,
2785 off_t offset_in_output_section,
2786 const Relocatable_relocs* rr,
2787 unsigned char* view,
2788 elfcpp::Elf_types<32>::Elf_Addr view_address,
2789 section_size_type view_size,
2790 unsigned char* reloc_view,
2791 section_size_type reloc_view_size)
2793 gold_assert(sh_type == elfcpp::SHT_REL);
2795 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2796 relinfo,
2797 prelocs,
2798 reloc_count,
2799 output_section,
2800 offset_in_output_section,
2802 view,
2803 view_address,
2804 view_size,
2805 reloc_view,
2806 reloc_view_size);
2809 // Return the value to use for a dynamic which requires special
2810 // treatment. This is how we support equality comparisons of function
2811 // pointers across shared library boundaries, as described in the
2812 // processor specific ABI supplement.
2814 uint64_t
2815 Target_i386::do_dynsym_value(const Symbol* gsym) const
2817 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2818 return this->plt_section()->address() + gsym->plt_offset();
2821 // Return a string used to fill a code section with nops to take up
2822 // the specified length.
2824 std::string
2825 Target_i386::do_code_fill(section_size_type length) const
2827 if (length >= 16)
2829 // Build a jmp instruction to skip over the bytes.
2830 unsigned char jmp[5];
2831 jmp[0] = 0xe9;
2832 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2833 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2834 + std::string(length - 5, '\0'));
2837 // Nop sequences of various lengths.
2838 const char nop1[1] = { 0x90 }; // nop
2839 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2840 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2841 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2842 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2843 0x00 }; // leal 0(%esi,1),%esi
2844 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2845 0x00, 0x00 };
2846 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2847 0x00, 0x00, 0x00 };
2848 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2849 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2850 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2851 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2852 0x00 };
2853 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2854 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2855 0x00, 0x00 };
2856 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2857 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2858 0x00, 0x00, 0x00 };
2859 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2860 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2861 0x00, 0x00, 0x00, 0x00 };
2862 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2863 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2864 0x27, 0x00, 0x00, 0x00,
2865 0x00 };
2866 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2867 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2868 0xbc, 0x27, 0x00, 0x00,
2869 0x00, 0x00 };
2870 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2871 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2872 0x90, 0x90, 0x90, 0x90,
2873 0x90, 0x90, 0x90 };
2875 const char* nops[16] = {
2876 NULL,
2877 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2878 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2881 return std::string(nops[length], length);
2884 // Return whether SYM should be treated as a call to a non-split
2885 // function. We don't want that to be true of a call to a
2886 // get_pc_thunk function.
2888 bool
2889 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
2891 return (sym->type() == elfcpp::STT_FUNC
2892 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
2895 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2896 // compiled with -fstack-split. The function calls non-stack-split
2897 // code. We have to change the function so that it always ensures
2898 // that it has enough stack space to run some random function.
2900 void
2901 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2902 section_offset_type fnoffset,
2903 section_size_type fnsize,
2904 unsigned char* view,
2905 section_size_type view_size,
2906 std::string* from,
2907 std::string* to) const
2909 // The function starts with a comparison of the stack pointer and a
2910 // field in the TCB. This is followed by a jump.
2912 // cmp %gs:NN,%esp
2913 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2914 && fnsize > 7)
2916 // We will call __morestack if the carry flag is set after this
2917 // comparison. We turn the comparison into an stc instruction
2918 // and some nops.
2919 view[fnoffset] = '\xf9';
2920 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2922 // lea NN(%esp),%ecx
2923 // lea NN(%esp),%edx
2924 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2925 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
2926 && fnsize > 7)
2928 // This is loading an offset from the stack pointer for a
2929 // comparison. The offset is negative, so we decrease the
2930 // offset by the amount of space we need for the stack. This
2931 // means we will avoid calling __morestack if there happens to
2932 // be plenty of space on the stack already.
2933 unsigned char* pval = view + fnoffset + 3;
2934 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2935 val -= parameters->options().split_stack_adjust_size();
2936 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2938 else
2940 if (!object->has_no_split_stack())
2941 object->error(_("failed to match split-stack sequence at "
2942 "section %u offset %0zx"),
2943 shndx, static_cast<size_t>(fnoffset));
2944 return;
2947 // We have to change the function so that it calls
2948 // __morestack_non_split instead of __morestack. The former will
2949 // allocate additional stack space.
2950 *from = "__morestack";
2951 *to = "__morestack_non_split";
2954 // The selector for i386 object files.
2956 class Target_selector_i386 : public Target_selector_freebsd
2958 public:
2959 Target_selector_i386()
2960 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2961 "elf32-i386", "elf32-i386-freebsd")
2964 Target*
2965 do_instantiate_target()
2966 { return new Target_i386(); }
2969 Target_selector_i386 target_selector_i386;
2971 } // End anonymous namespace.