daily update
[binutils.git] / gold / i386.cc
bloba137b2ef1f44c192193b795706bbb565973bd965
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 // A class to handle the PLT data.
50 class Output_data_plt_i386 : public Output_section_data
52 public:
53 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
55 Output_data_plt_i386(Symbol_table*, Layout*, Output_data_space*);
57 // Add an entry to the PLT.
58 void
59 add_entry(Symbol* gsym);
61 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
62 unsigned int
63 add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
64 unsigned int local_sym_index);
66 // Return the .rel.plt section data.
67 Reloc_section*
68 rel_plt() const
69 { return this->rel_; }
71 // Return where the TLS_DESC relocations should go.
72 Reloc_section*
73 rel_tls_desc(Layout*);
75 // Return the number of PLT entries.
76 unsigned int
77 entry_count() const
78 { return this->count_; }
80 // Return the offset of the first non-reserved PLT entry.
81 static unsigned int
82 first_plt_entry_offset()
83 { return plt_entry_size; }
85 // Return the size of a PLT entry.
86 static unsigned int
87 get_plt_entry_size()
88 { return plt_entry_size; }
90 protected:
91 void
92 do_adjust_output_section(Output_section* os);
94 // Write to a map file.
95 void
96 do_print_to_mapfile(Mapfile* mapfile) const
97 { mapfile->print_output_data(this, _("** PLT")); }
99 private:
100 // The size of an entry in the PLT.
101 static const int plt_entry_size = 16;
103 // The first entry in the PLT for an executable.
104 static unsigned char exec_first_plt_entry[plt_entry_size];
106 // The first entry in the PLT for a shared object.
107 static unsigned char dyn_first_plt_entry[plt_entry_size];
109 // Other entries in the PLT for an executable.
110 static unsigned char exec_plt_entry[plt_entry_size];
112 // Other entries in the PLT for a shared object.
113 static unsigned char dyn_plt_entry[plt_entry_size];
115 // Set the final size.
116 void
117 set_final_data_size()
118 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
120 // Write out the PLT data.
121 void
122 do_write(Output_file*);
124 // We keep a list of global STT_GNU_IFUNC symbols, each with its
125 // offset in the GOT.
126 struct Global_ifunc
128 Symbol* sym;
129 unsigned int got_offset;
132 // We keep a list of local STT_GNU_IFUNC symbols, each with its
133 // offset in the GOT.
134 struct Local_ifunc
136 Sized_relobj<32, false>* object;
137 unsigned int local_sym_index;
138 unsigned int got_offset;
141 // The reloc section.
142 Reloc_section* rel_;
143 // The TLS_DESC relocations, if necessary. These must follow the
144 // regular PLT relocs.
145 Reloc_section* tls_desc_rel_;
146 // The .got.plt section.
147 Output_data_space* got_plt_;
148 // The number of PLT entries.
149 unsigned int count_;
150 // Global STT_GNU_IFUNC symbols.
151 std::vector<Global_ifunc> global_ifuncs_;
152 // Local STT_GNU_IFUNC symbols.
153 std::vector<Local_ifunc> local_ifuncs_;
156 // The i386 target class.
157 // TLS info comes from
158 // http://people.redhat.com/drepper/tls.pdf
159 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
161 class Target_i386 : public Target_freebsd<32, false>
163 public:
164 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
166 Target_i386()
167 : Target_freebsd<32, false>(&i386_info),
168 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
169 global_offset_table_(NULL), rel_dyn_(NULL),
170 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
171 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
174 inline bool
175 can_check_for_function_pointers() const
176 { return true; }
178 virtual bool
179 can_icf_inline_merge_sections () const
180 { return true; }
182 // Process the relocations to determine unreferenced sections for
183 // garbage collection.
184 void
185 gc_process_relocs(Symbol_table* symtab,
186 Layout* layout,
187 Sized_relobj<32, false>* object,
188 unsigned int data_shndx,
189 unsigned int sh_type,
190 const unsigned char* prelocs,
191 size_t reloc_count,
192 Output_section* output_section,
193 bool needs_special_offset_handling,
194 size_t local_symbol_count,
195 const unsigned char* plocal_symbols);
197 // Scan the relocations to look for symbol adjustments.
198 void
199 scan_relocs(Symbol_table* symtab,
200 Layout* layout,
201 Sized_relobj<32, false>* object,
202 unsigned int data_shndx,
203 unsigned int sh_type,
204 const unsigned char* prelocs,
205 size_t reloc_count,
206 Output_section* output_section,
207 bool needs_special_offset_handling,
208 size_t local_symbol_count,
209 const unsigned char* plocal_symbols);
211 // Finalize the sections.
212 void
213 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
215 // Return the value to use for a dynamic which requires special
216 // treatment.
217 uint64_t
218 do_dynsym_value(const Symbol*) const;
220 // Relocate a section.
221 void
222 relocate_section(const Relocate_info<32, false>*,
223 unsigned int sh_type,
224 const unsigned char* prelocs,
225 size_t reloc_count,
226 Output_section* output_section,
227 bool needs_special_offset_handling,
228 unsigned char* view,
229 elfcpp::Elf_types<32>::Elf_Addr view_address,
230 section_size_type view_size,
231 const Reloc_symbol_changes*);
233 // Scan the relocs during a relocatable link.
234 void
235 scan_relocatable_relocs(Symbol_table* symtab,
236 Layout* layout,
237 Sized_relobj<32, false>* object,
238 unsigned int data_shndx,
239 unsigned int sh_type,
240 const unsigned char* prelocs,
241 size_t reloc_count,
242 Output_section* output_section,
243 bool needs_special_offset_handling,
244 size_t local_symbol_count,
245 const unsigned char* plocal_symbols,
246 Relocatable_relocs*);
248 // Relocate a section during a relocatable link.
249 void
250 relocate_for_relocatable(const Relocate_info<32, false>*,
251 unsigned int sh_type,
252 const unsigned char* prelocs,
253 size_t reloc_count,
254 Output_section* output_section,
255 off_t offset_in_output_section,
256 const Relocatable_relocs*,
257 unsigned char* view,
258 elfcpp::Elf_types<32>::Elf_Addr view_address,
259 section_size_type view_size,
260 unsigned char* reloc_view,
261 section_size_type reloc_view_size);
263 // Return a string used to fill a code section with nops.
264 std::string
265 do_code_fill(section_size_type length) const;
267 // Return whether SYM is defined by the ABI.
268 bool
269 do_is_defined_by_abi(const Symbol* sym) const
270 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
272 // Return whether a symbol name implies a local label. The UnixWare
273 // 2.1 cc generates temporary symbols that start with .X, so we
274 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
275 // If so, we should move the .X recognition into
276 // Target::do_is_local_label_name.
277 bool
278 do_is_local_label_name(const char* name) const
280 if (name[0] == '.' && name[1] == 'X')
281 return true;
282 return Target::do_is_local_label_name(name);
285 // Return the PLT section.
286 Output_data*
287 do_plt_section_for_global(const Symbol*) const
288 { return this->plt_section(); }
290 Output_data*
291 do_plt_section_for_local(const Relobj*, unsigned int) const
292 { return this->plt_section(); }
294 // Return whether SYM is call to a non-split function.
295 bool
296 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
298 // Adjust -fsplit-stack code which calls non-split-stack code.
299 void
300 do_calls_non_split(Relobj* object, unsigned int shndx,
301 section_offset_type fnoffset, section_size_type fnsize,
302 unsigned char* view, section_size_type view_size,
303 std::string* from, std::string* to) const;
305 // Return the size of the GOT section.
306 section_size_type
307 got_size() const
309 gold_assert(this->got_ != NULL);
310 return this->got_->data_size();
313 // Return the number of entries in the GOT.
314 unsigned int
315 got_entry_count() const
317 if (this->got_ == NULL)
318 return 0;
319 return this->got_size() / 4;
322 // Return the number of entries in the PLT.
323 unsigned int
324 plt_entry_count() const;
326 // Return the offset of the first non-reserved PLT entry.
327 unsigned int
328 first_plt_entry_offset() const;
330 // Return the size of each PLT entry.
331 unsigned int
332 plt_entry_size() const;
334 private:
335 // The class which scans relocations.
336 struct Scan
338 static inline int
340 get_reference_flags(unsigned int r_type);
342 inline void
343 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
344 Sized_relobj<32, false>* object,
345 unsigned int data_shndx,
346 Output_section* output_section,
347 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
348 const elfcpp::Sym<32, false>& lsym);
350 inline void
351 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
352 Sized_relobj<32, false>* object,
353 unsigned int data_shndx,
354 Output_section* output_section,
355 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
356 Symbol* gsym);
358 inline bool
359 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
360 Target_i386* target,
361 Sized_relobj<32, false>* object,
362 unsigned int data_shndx,
363 Output_section* output_section,
364 const elfcpp::Rel<32, false>& reloc,
365 unsigned int r_type,
366 const elfcpp::Sym<32, false>& lsym);
368 inline bool
369 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
370 Target_i386* target,
371 Sized_relobj<32, false>* object,
372 unsigned int data_shndx,
373 Output_section* output_section,
374 const elfcpp::Rel<32, false>& reloc,
375 unsigned int r_type,
376 Symbol* gsym);
378 inline bool
379 possible_function_pointer_reloc(unsigned int r_type);
381 bool
382 reloc_needs_plt_for_ifunc(Sized_relobj<32, false>*, unsigned int r_type);
384 static void
385 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
387 static void
388 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
389 Symbol*);
392 // The class which implements relocation.
393 class Relocate
395 public:
396 Relocate()
397 : skip_call_tls_get_addr_(false),
398 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
401 ~Relocate()
403 if (this->skip_call_tls_get_addr_)
405 // FIXME: This needs to specify the location somehow.
406 gold_error(_("missing expected TLS relocation"));
410 // Return whether the static relocation needs to be applied.
411 inline bool
412 should_apply_static_reloc(const Sized_symbol<32>* gsym,
413 unsigned int r_type,
414 bool is_32bit,
415 Output_section* output_section);
417 // Do a relocation. Return false if the caller should not issue
418 // any warnings about this relocation.
419 inline bool
420 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
421 size_t relnum, const elfcpp::Rel<32, false>&,
422 unsigned int r_type, const Sized_symbol<32>*,
423 const Symbol_value<32>*,
424 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
425 section_size_type);
427 private:
428 // Do a TLS relocation.
429 inline void
430 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
431 size_t relnum, const elfcpp::Rel<32, false>&,
432 unsigned int r_type, const Sized_symbol<32>*,
433 const Symbol_value<32>*,
434 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
435 section_size_type);
437 // Do a TLS General-Dynamic to Initial-Exec transition.
438 inline void
439 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
440 Output_segment* tls_segment,
441 const elfcpp::Rel<32, false>&, unsigned int r_type,
442 elfcpp::Elf_types<32>::Elf_Addr value,
443 unsigned char* view,
444 section_size_type view_size);
446 // Do a TLS General-Dynamic to Local-Exec transition.
447 inline void
448 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
449 Output_segment* tls_segment,
450 const elfcpp::Rel<32, false>&, unsigned int r_type,
451 elfcpp::Elf_types<32>::Elf_Addr value,
452 unsigned char* view,
453 section_size_type view_size);
455 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
456 // transition.
457 inline void
458 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
459 Output_segment* tls_segment,
460 const elfcpp::Rel<32, false>&, unsigned int r_type,
461 elfcpp::Elf_types<32>::Elf_Addr value,
462 unsigned char* view,
463 section_size_type view_size);
465 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
466 // transition.
467 inline void
468 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
469 Output_segment* tls_segment,
470 const elfcpp::Rel<32, false>&, unsigned int r_type,
471 elfcpp::Elf_types<32>::Elf_Addr value,
472 unsigned char* view,
473 section_size_type view_size);
475 // Do a TLS Local-Dynamic to Local-Exec transition.
476 inline void
477 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
478 Output_segment* tls_segment,
479 const elfcpp::Rel<32, false>&, unsigned int r_type,
480 elfcpp::Elf_types<32>::Elf_Addr value,
481 unsigned char* view,
482 section_size_type view_size);
484 // Do a TLS Initial-Exec to Local-Exec transition.
485 static inline void
486 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
487 Output_segment* tls_segment,
488 const elfcpp::Rel<32, false>&, unsigned int r_type,
489 elfcpp::Elf_types<32>::Elf_Addr value,
490 unsigned char* view,
491 section_size_type view_size);
493 // We need to keep track of which type of local dynamic relocation
494 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
495 enum Local_dynamic_type
497 LOCAL_DYNAMIC_NONE,
498 LOCAL_DYNAMIC_SUN,
499 LOCAL_DYNAMIC_GNU
502 // This is set if we should skip the next reloc, which should be a
503 // PLT32 reloc against ___tls_get_addr.
504 bool skip_call_tls_get_addr_;
505 // The type of local dynamic relocation we have seen in the section
506 // being relocated, if any.
507 Local_dynamic_type local_dynamic_type_;
510 // A class which returns the size required for a relocation type,
511 // used while scanning relocs during a relocatable link.
512 class Relocatable_size_for_reloc
514 public:
515 unsigned int
516 get_size_for_reloc(unsigned int, Relobj*);
519 // Adjust TLS relocation type based on the options and whether this
520 // is a local symbol.
521 static tls::Tls_optimization
522 optimize_tls_reloc(bool is_final, int r_type);
524 // Get the GOT section, creating it if necessary.
525 Output_data_got<32, false>*
526 got_section(Symbol_table*, Layout*);
528 // Get the GOT PLT section.
529 Output_data_space*
530 got_plt_section() const
532 gold_assert(this->got_plt_ != NULL);
533 return this->got_plt_;
536 // Get the GOT section for TLSDESC entries.
537 Output_data_got<32, false>*
538 got_tlsdesc_section() const
540 gold_assert(this->got_tlsdesc_ != NULL);
541 return this->got_tlsdesc_;
544 // Create the PLT section.
545 void
546 make_plt_section(Symbol_table* symtab, Layout* layout);
548 // Create a PLT entry for a global symbol.
549 void
550 make_plt_entry(Symbol_table*, Layout*, Symbol*);
552 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
553 void
554 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
555 Sized_relobj<32, false>* relobj,
556 unsigned int local_sym_index);
558 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
559 void
560 define_tls_base_symbol(Symbol_table*, Layout*);
562 // Create a GOT entry for the TLS module index.
563 unsigned int
564 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
565 Sized_relobj<32, false>* object);
567 // Get the PLT section.
568 Output_data_plt_i386*
569 plt_section() const
571 gold_assert(this->plt_ != NULL);
572 return this->plt_;
575 // Get the dynamic reloc section, creating it if necessary.
576 Reloc_section*
577 rel_dyn_section(Layout*);
579 // Get the section to use for TLS_DESC relocations.
580 Reloc_section*
581 rel_tls_desc_section(Layout*) const;
583 // Add a potential copy relocation.
584 void
585 copy_reloc(Symbol_table* symtab, Layout* layout,
586 Sized_relobj<32, false>* object,
587 unsigned int shndx, Output_section* output_section,
588 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
590 this->copy_relocs_.copy_reloc(symtab, layout,
591 symtab->get_sized_symbol<32>(sym),
592 object, shndx, output_section, reloc,
593 this->rel_dyn_section(layout));
596 // Information about this specific target which we pass to the
597 // general Target structure.
598 static const Target::Target_info i386_info;
600 // The types of GOT entries needed for this platform.
601 // These values are exposed to the ABI in an incremental link.
602 // Do not renumber existing values without changing the version
603 // number of the .gnu_incremental_inputs section.
604 enum Got_type
606 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
607 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
608 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
609 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
610 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
613 // The GOT section.
614 Output_data_got<32, false>* got_;
615 // The PLT section.
616 Output_data_plt_i386* plt_;
617 // The GOT PLT section.
618 Output_data_space* got_plt_;
619 // The GOT section for TLSDESC relocations.
620 Output_data_got<32, false>* got_tlsdesc_;
621 // The _GLOBAL_OFFSET_TABLE_ symbol.
622 Symbol* global_offset_table_;
623 // The dynamic reloc section.
624 Reloc_section* rel_dyn_;
625 // Relocs saved to avoid a COPY reloc.
626 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
627 // Space for variables copied with a COPY reloc.
628 Output_data_space* dynbss_;
629 // Offset of the GOT entry for the TLS module index.
630 unsigned int got_mod_index_offset_;
631 // True if the _TLS_MODULE_BASE_ symbol has been defined.
632 bool tls_base_symbol_defined_;
635 const Target::Target_info Target_i386::i386_info =
637 32, // size
638 false, // is_big_endian
639 elfcpp::EM_386, // machine_code
640 false, // has_make_symbol
641 false, // has_resolve
642 true, // has_code_fill
643 true, // is_default_stack_executable
644 '\0', // wrap_char
645 "/usr/lib/libc.so.1", // dynamic_linker
646 0x08048000, // default_text_segment_address
647 0x1000, // abi_pagesize (overridable by -z max-page-size)
648 0x1000, // common_pagesize (overridable by -z common-page-size)
649 elfcpp::SHN_UNDEF, // small_common_shndx
650 elfcpp::SHN_UNDEF, // large_common_shndx
651 0, // small_common_section_flags
652 0, // large_common_section_flags
653 NULL, // attributes_section
654 NULL // attributes_vendor
657 // Get the GOT section, creating it if necessary.
659 Output_data_got<32, false>*
660 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
662 if (this->got_ == NULL)
664 gold_assert(symtab != NULL && layout != NULL);
666 this->got_ = new Output_data_got<32, false>();
668 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
669 (elfcpp::SHF_ALLOC
670 | elfcpp::SHF_WRITE),
671 this->got_, ORDER_RELRO_LAST, true);
673 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
674 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
675 (elfcpp::SHF_ALLOC
676 | elfcpp::SHF_WRITE),
677 this->got_plt_, ORDER_NON_RELRO_FIRST,
678 false);
680 // The first three entries are reserved.
681 this->got_plt_->set_current_data_size(3 * 4);
683 // Those bytes can go into the relro segment.
684 layout->increase_relro(3 * 4);
686 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
687 this->global_offset_table_ =
688 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
689 Symbol_table::PREDEFINED,
690 this->got_plt_,
691 0, 0, elfcpp::STT_OBJECT,
692 elfcpp::STB_LOCAL,
693 elfcpp::STV_HIDDEN, 0,
694 false, false);
696 // If there are any TLSDESC relocations, they get GOT entries in
697 // .got.plt after the jump slot entries.
698 this->got_tlsdesc_ = new Output_data_got<32, false>();
699 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
700 (elfcpp::SHF_ALLOC
701 | elfcpp::SHF_WRITE),
702 this->got_tlsdesc_,
703 ORDER_NON_RELRO_FIRST, false);
706 return this->got_;
709 // Get the dynamic reloc section, creating it if necessary.
711 Target_i386::Reloc_section*
712 Target_i386::rel_dyn_section(Layout* layout)
714 if (this->rel_dyn_ == NULL)
716 gold_assert(layout != NULL);
717 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
718 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
719 elfcpp::SHF_ALLOC, this->rel_dyn_,
720 ORDER_DYNAMIC_RELOCS, false);
722 return this->rel_dyn_;
725 // Create the PLT section. The ordinary .got section is an argument,
726 // since we need to refer to the start. We also create our own .got
727 // section just for PLT entries.
729 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
730 Layout* layout,
731 Output_data_space* got_plt)
732 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
733 global_ifuncs_(), local_ifuncs_()
735 this->rel_ = new Reloc_section(false);
736 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
737 elfcpp::SHF_ALLOC, this->rel_,
738 ORDER_DYNAMIC_PLT_RELOCS, false);
740 if (parameters->doing_static_link())
742 // A statically linked executable will only have a .rel.plt
743 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
744 // symbols. The library will use these symbols to locate the
745 // IRELATIVE relocs at program startup time.
746 symtab->define_in_output_data("__rel_iplt_start", NULL,
747 Symbol_table::PREDEFINED,
748 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
749 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
750 0, false, true);
751 symtab->define_in_output_data("__rel_iplt_end", NULL,
752 Symbol_table::PREDEFINED,
753 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
754 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
755 0, true, true);
759 void
760 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
762 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
763 // linker, and so do we.
764 os->set_entsize(4);
767 // Add an entry to the PLT.
769 void
770 Output_data_plt_i386::add_entry(Symbol* gsym)
772 gold_assert(!gsym->has_plt_offset());
774 // Note that when setting the PLT offset we skip the initial
775 // reserved PLT entry.
776 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
778 ++this->count_;
780 section_offset_type got_offset = this->got_plt_->current_data_size();
782 // Every PLT entry needs a GOT entry which points back to the PLT
783 // entry (this will be changed by the dynamic linker, normally
784 // lazily when the function is called).
785 this->got_plt_->set_current_data_size(got_offset + 4);
787 // Every PLT entry needs a reloc.
788 if (gsym->type() == elfcpp::STT_GNU_IFUNC
789 && gsym->can_use_relative_reloc(false))
791 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
792 this->got_plt_, got_offset);
793 struct Global_ifunc gi;
794 gi.sym = gsym;
795 gi.got_offset = got_offset;
796 this->global_ifuncs_.push_back(gi);
798 else
800 gsym->set_needs_dynsym_entry();
801 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
802 got_offset);
805 // Note that we don't need to save the symbol. The contents of the
806 // PLT are independent of which symbols are used. The symbols only
807 // appear in the relocations.
810 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
811 // the PLT offset.
813 unsigned int
814 Output_data_plt_i386::add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
815 unsigned int local_sym_index)
817 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
818 ++this->count_;
820 section_offset_type got_offset = this->got_plt_->current_data_size();
822 // Every PLT entry needs a GOT entry which points back to the PLT
823 // entry.
824 this->got_plt_->set_current_data_size(got_offset + 4);
826 // Every PLT entry needs a reloc.
827 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
828 elfcpp::R_386_IRELATIVE,
829 this->got_plt_, got_offset);
831 struct Local_ifunc li;
832 li.object = relobj;
833 li.local_sym_index = local_sym_index;
834 li.got_offset = got_offset;
835 this->local_ifuncs_.push_back(li);
837 return plt_offset;
840 // Return where the TLS_DESC relocations should go, creating it if
841 // necessary. These follow the JUMP_SLOT relocations.
843 Output_data_plt_i386::Reloc_section*
844 Output_data_plt_i386::rel_tls_desc(Layout* layout)
846 if (this->tls_desc_rel_ == NULL)
848 this->tls_desc_rel_ = new Reloc_section(false);
849 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
850 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
851 ORDER_DYNAMIC_PLT_RELOCS, false);
852 gold_assert(this->tls_desc_rel_->output_section() ==
853 this->rel_->output_section());
855 return this->tls_desc_rel_;
858 // The first entry in the PLT for an executable.
860 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
862 0xff, 0x35, // pushl contents of memory address
863 0, 0, 0, 0, // replaced with address of .got + 4
864 0xff, 0x25, // jmp indirect
865 0, 0, 0, 0, // replaced with address of .got + 8
866 0, 0, 0, 0 // unused
869 // The first entry in the PLT for a shared object.
871 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
873 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
874 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
875 0, 0, 0, 0 // unused
878 // Subsequent entries in the PLT for an executable.
880 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
882 0xff, 0x25, // jmp indirect
883 0, 0, 0, 0, // replaced with address of symbol in .got
884 0x68, // pushl immediate
885 0, 0, 0, 0, // replaced with offset into relocation table
886 0xe9, // jmp relative
887 0, 0, 0, 0 // replaced with offset to start of .plt
890 // Subsequent entries in the PLT for a shared object.
892 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
894 0xff, 0xa3, // jmp *offset(%ebx)
895 0, 0, 0, 0, // replaced with offset of symbol in .got
896 0x68, // pushl immediate
897 0, 0, 0, 0, // replaced with offset into relocation table
898 0xe9, // jmp relative
899 0, 0, 0, 0 // replaced with offset to start of .plt
902 // Write out the PLT. This uses the hand-coded instructions above,
903 // and adjusts them as needed. This is all specified by the i386 ELF
904 // Processor Supplement.
906 void
907 Output_data_plt_i386::do_write(Output_file* of)
909 const off_t offset = this->offset();
910 const section_size_type oview_size =
911 convert_to_section_size_type(this->data_size());
912 unsigned char* const oview = of->get_output_view(offset, oview_size);
914 const off_t got_file_offset = this->got_plt_->offset();
915 const section_size_type got_size =
916 convert_to_section_size_type(this->got_plt_->data_size());
917 unsigned char* const got_view = of->get_output_view(got_file_offset,
918 got_size);
920 unsigned char* pov = oview;
922 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
923 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
925 if (parameters->options().output_is_position_independent())
926 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
927 else
929 memcpy(pov, exec_first_plt_entry, plt_entry_size);
930 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
931 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
933 pov += plt_entry_size;
935 unsigned char* got_pov = got_view;
937 memset(got_pov, 0, 12);
938 got_pov += 12;
940 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
942 unsigned int plt_offset = plt_entry_size;
943 unsigned int plt_rel_offset = 0;
944 unsigned int got_offset = 12;
945 const unsigned int count = this->count_;
946 for (unsigned int i = 0;
947 i < count;
948 ++i,
949 pov += plt_entry_size,
950 got_pov += 4,
951 plt_offset += plt_entry_size,
952 plt_rel_offset += rel_size,
953 got_offset += 4)
955 // Set and adjust the PLT entry itself.
957 if (parameters->options().output_is_position_independent())
959 memcpy(pov, dyn_plt_entry, plt_entry_size);
960 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
962 else
964 memcpy(pov, exec_plt_entry, plt_entry_size);
965 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
966 (got_address
967 + got_offset));
970 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
971 elfcpp::Swap<32, false>::writeval(pov + 12,
972 - (plt_offset + plt_entry_size));
974 // Set the entry in the GOT.
975 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
978 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
979 // the GOT to point to the actual symbol value, rather than point to
980 // the PLT entry. That will let the dynamic linker call the right
981 // function when resolving IRELATIVE relocations.
982 for (std::vector<Global_ifunc>::const_iterator p =
983 this->global_ifuncs_.begin();
984 p != this->global_ifuncs_.end();
985 ++p)
987 const Sized_symbol<32>* ssym =
988 static_cast<const Sized_symbol<32>*>(p->sym);
989 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
990 ssym->value());
993 for (std::vector<Local_ifunc>::const_iterator p =
994 this->local_ifuncs_.begin();
995 p != this->local_ifuncs_.end();
996 ++p)
998 const Symbol_value<32>* psymval =
999 p->object->local_symbol(p->local_sym_index);
1000 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
1001 psymval->value(p->object, 0));
1004 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1005 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1007 of->write_output_view(offset, oview_size, oview);
1008 of->write_output_view(got_file_offset, got_size, got_view);
1011 // Create the PLT section.
1013 void
1014 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1016 if (this->plt_ == NULL)
1018 // Create the GOT sections first.
1019 this->got_section(symtab, layout);
1021 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1022 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1023 (elfcpp::SHF_ALLOC
1024 | elfcpp::SHF_EXECINSTR),
1025 this->plt_, ORDER_PLT, false);
1027 // Make the sh_info field of .rel.plt point to .plt.
1028 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1029 rel_plt_os->set_info_section(this->plt_->output_section());
1033 // Create a PLT entry for a global symbol.
1035 void
1036 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1038 if (gsym->has_plt_offset())
1039 return;
1040 if (this->plt_ == NULL)
1041 this->make_plt_section(symtab, layout);
1042 this->plt_->add_entry(gsym);
1045 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1047 void
1048 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1049 Sized_relobj<32, false>* relobj,
1050 unsigned int local_sym_index)
1052 if (relobj->local_has_plt_offset(local_sym_index))
1053 return;
1054 if (this->plt_ == NULL)
1055 this->make_plt_section(symtab, layout);
1056 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1057 local_sym_index);
1058 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1061 // Return the number of entries in the PLT.
1063 unsigned int
1064 Target_i386::plt_entry_count() const
1066 if (this->plt_ == NULL)
1067 return 0;
1068 return this->plt_->entry_count();
1071 // Return the offset of the first non-reserved PLT entry.
1073 unsigned int
1074 Target_i386::first_plt_entry_offset() const
1076 return Output_data_plt_i386::first_plt_entry_offset();
1079 // Return the size of each PLT entry.
1081 unsigned int
1082 Target_i386::plt_entry_size() const
1084 return Output_data_plt_i386::get_plt_entry_size();
1087 // Get the section to use for TLS_DESC relocations.
1089 Target_i386::Reloc_section*
1090 Target_i386::rel_tls_desc_section(Layout* layout) const
1092 return this->plt_section()->rel_tls_desc(layout);
1095 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1097 void
1098 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1100 if (this->tls_base_symbol_defined_)
1101 return;
1103 Output_segment* tls_segment = layout->tls_segment();
1104 if (tls_segment != NULL)
1106 bool is_exec = parameters->options().output_is_executable();
1107 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1108 Symbol_table::PREDEFINED,
1109 tls_segment, 0, 0,
1110 elfcpp::STT_TLS,
1111 elfcpp::STB_LOCAL,
1112 elfcpp::STV_HIDDEN, 0,
1113 (is_exec
1114 ? Symbol::SEGMENT_END
1115 : Symbol::SEGMENT_START),
1116 true);
1118 this->tls_base_symbol_defined_ = true;
1121 // Create a GOT entry for the TLS module index.
1123 unsigned int
1124 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1125 Sized_relobj<32, false>* object)
1127 if (this->got_mod_index_offset_ == -1U)
1129 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1130 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1131 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1132 unsigned int got_offset = got->add_constant(0);
1133 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1134 got_offset);
1135 got->add_constant(0);
1136 this->got_mod_index_offset_ = got_offset;
1138 return this->got_mod_index_offset_;
1141 // Optimize the TLS relocation type based on what we know about the
1142 // symbol. IS_FINAL is true if the final address of this symbol is
1143 // known at link time.
1145 tls::Tls_optimization
1146 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1148 // If we are generating a shared library, then we can't do anything
1149 // in the linker.
1150 if (parameters->options().shared())
1151 return tls::TLSOPT_NONE;
1153 switch (r_type)
1155 case elfcpp::R_386_TLS_GD:
1156 case elfcpp::R_386_TLS_GOTDESC:
1157 case elfcpp::R_386_TLS_DESC_CALL:
1158 // These are General-Dynamic which permits fully general TLS
1159 // access. Since we know that we are generating an executable,
1160 // we can convert this to Initial-Exec. If we also know that
1161 // this is a local symbol, we can further switch to Local-Exec.
1162 if (is_final)
1163 return tls::TLSOPT_TO_LE;
1164 return tls::TLSOPT_TO_IE;
1166 case elfcpp::R_386_TLS_LDM:
1167 // This is Local-Dynamic, which refers to a local symbol in the
1168 // dynamic TLS block. Since we know that we generating an
1169 // executable, we can switch to Local-Exec.
1170 return tls::TLSOPT_TO_LE;
1172 case elfcpp::R_386_TLS_LDO_32:
1173 // Another type of Local-Dynamic relocation.
1174 return tls::TLSOPT_TO_LE;
1176 case elfcpp::R_386_TLS_IE:
1177 case elfcpp::R_386_TLS_GOTIE:
1178 case elfcpp::R_386_TLS_IE_32:
1179 // These are Initial-Exec relocs which get the thread offset
1180 // from the GOT. If we know that we are linking against the
1181 // local symbol, we can switch to Local-Exec, which links the
1182 // thread offset into the instruction.
1183 if (is_final)
1184 return tls::TLSOPT_TO_LE;
1185 return tls::TLSOPT_NONE;
1187 case elfcpp::R_386_TLS_LE:
1188 case elfcpp::R_386_TLS_LE_32:
1189 // When we already have Local-Exec, there is nothing further we
1190 // can do.
1191 return tls::TLSOPT_NONE;
1193 default:
1194 gold_unreachable();
1198 // Get the Reference_flags for a particular relocation.
1201 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1203 switch (r_type)
1205 case elfcpp::R_386_NONE:
1206 case elfcpp::R_386_GNU_VTINHERIT:
1207 case elfcpp::R_386_GNU_VTENTRY:
1208 case elfcpp::R_386_GOTPC:
1209 // No symbol reference.
1210 return 0;
1212 case elfcpp::R_386_32:
1213 case elfcpp::R_386_16:
1214 case elfcpp::R_386_8:
1215 return Symbol::ABSOLUTE_REF;
1217 case elfcpp::R_386_PC32:
1218 case elfcpp::R_386_PC16:
1219 case elfcpp::R_386_PC8:
1220 case elfcpp::R_386_GOTOFF:
1221 return Symbol::RELATIVE_REF;
1223 case elfcpp::R_386_PLT32:
1224 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1226 case elfcpp::R_386_GOT32:
1227 // Absolute in GOT.
1228 return Symbol::ABSOLUTE_REF;
1230 case elfcpp::R_386_TLS_GD: // Global-dynamic
1231 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1232 case elfcpp::R_386_TLS_DESC_CALL:
1233 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1234 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1235 case elfcpp::R_386_TLS_IE: // Initial-exec
1236 case elfcpp::R_386_TLS_IE_32:
1237 case elfcpp::R_386_TLS_GOTIE:
1238 case elfcpp::R_386_TLS_LE: // Local-exec
1239 case elfcpp::R_386_TLS_LE_32:
1240 return Symbol::TLS_REF;
1242 case elfcpp::R_386_COPY:
1243 case elfcpp::R_386_GLOB_DAT:
1244 case elfcpp::R_386_JUMP_SLOT:
1245 case elfcpp::R_386_RELATIVE:
1246 case elfcpp::R_386_IRELATIVE:
1247 case elfcpp::R_386_TLS_TPOFF:
1248 case elfcpp::R_386_TLS_DTPMOD32:
1249 case elfcpp::R_386_TLS_DTPOFF32:
1250 case elfcpp::R_386_TLS_TPOFF32:
1251 case elfcpp::R_386_TLS_DESC:
1252 case elfcpp::R_386_32PLT:
1253 case elfcpp::R_386_TLS_GD_32:
1254 case elfcpp::R_386_TLS_GD_PUSH:
1255 case elfcpp::R_386_TLS_GD_CALL:
1256 case elfcpp::R_386_TLS_GD_POP:
1257 case elfcpp::R_386_TLS_LDM_32:
1258 case elfcpp::R_386_TLS_LDM_PUSH:
1259 case elfcpp::R_386_TLS_LDM_CALL:
1260 case elfcpp::R_386_TLS_LDM_POP:
1261 case elfcpp::R_386_USED_BY_INTEL_200:
1262 default:
1263 // Not expected. We will give an error later.
1264 return 0;
1268 // Report an unsupported relocation against a local symbol.
1270 void
1271 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
1272 unsigned int r_type)
1274 gold_error(_("%s: unsupported reloc %u against local symbol"),
1275 object->name().c_str(), r_type);
1278 // Return whether we need to make a PLT entry for a relocation of a
1279 // given type against a STT_GNU_IFUNC symbol.
1281 bool
1282 Target_i386::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<32, false>* object,
1283 unsigned int r_type)
1285 int flags = Scan::get_reference_flags(r_type);
1286 if (flags & Symbol::TLS_REF)
1287 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1288 object->name().c_str(), r_type);
1289 return flags != 0;
1292 // Scan a relocation for a local symbol.
1294 inline void
1295 Target_i386::Scan::local(Symbol_table* symtab,
1296 Layout* layout,
1297 Target_i386* target,
1298 Sized_relobj<32, false>* object,
1299 unsigned int data_shndx,
1300 Output_section* output_section,
1301 const elfcpp::Rel<32, false>& reloc,
1302 unsigned int r_type,
1303 const elfcpp::Sym<32, false>& lsym)
1305 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1306 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1307 && this->reloc_needs_plt_for_ifunc(object, r_type))
1309 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1310 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1313 switch (r_type)
1315 case elfcpp::R_386_NONE:
1316 case elfcpp::R_386_GNU_VTINHERIT:
1317 case elfcpp::R_386_GNU_VTENTRY:
1318 break;
1320 case elfcpp::R_386_32:
1321 // If building a shared library (or a position-independent
1322 // executable), we need to create a dynamic relocation for
1323 // this location. The relocation applied at link time will
1324 // apply the link-time value, so we flag the location with
1325 // an R_386_RELATIVE relocation so the dynamic loader can
1326 // relocate it easily.
1327 if (parameters->options().output_is_position_independent())
1329 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1330 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1331 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1332 output_section, data_shndx,
1333 reloc.get_r_offset());
1335 break;
1337 case elfcpp::R_386_16:
1338 case elfcpp::R_386_8:
1339 // If building a shared library (or a position-independent
1340 // executable), we need to create a dynamic relocation for
1341 // this location. Because the addend needs to remain in the
1342 // data section, we need to be careful not to apply this
1343 // relocation statically.
1344 if (parameters->options().output_is_position_independent())
1346 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1347 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1348 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1349 rel_dyn->add_local(object, r_sym, r_type, output_section,
1350 data_shndx, reloc.get_r_offset());
1351 else
1353 gold_assert(lsym.get_st_value() == 0);
1354 unsigned int shndx = lsym.get_st_shndx();
1355 bool is_ordinary;
1356 shndx = object->adjust_sym_shndx(r_sym, shndx,
1357 &is_ordinary);
1358 if (!is_ordinary)
1359 object->error(_("section symbol %u has bad shndx %u"),
1360 r_sym, shndx);
1361 else
1362 rel_dyn->add_local_section(object, shndx,
1363 r_type, output_section,
1364 data_shndx, reloc.get_r_offset());
1367 break;
1369 case elfcpp::R_386_PC32:
1370 case elfcpp::R_386_PC16:
1371 case elfcpp::R_386_PC8:
1372 break;
1374 case elfcpp::R_386_PLT32:
1375 // Since we know this is a local symbol, we can handle this as a
1376 // PC32 reloc.
1377 break;
1379 case elfcpp::R_386_GOTOFF:
1380 case elfcpp::R_386_GOTPC:
1381 // We need a GOT section.
1382 target->got_section(symtab, layout);
1383 break;
1385 case elfcpp::R_386_GOT32:
1387 // The symbol requires a GOT entry.
1388 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1389 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1391 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1392 // lets function pointers compare correctly with shared
1393 // libraries. Otherwise we would need an IRELATIVE reloc.
1394 bool is_new;
1395 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1396 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1397 else
1398 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1399 if (is_new)
1401 // If we are generating a shared object, we need to add a
1402 // dynamic RELATIVE relocation for this symbol's GOT entry.
1403 if (parameters->options().output_is_position_independent())
1405 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1406 unsigned int got_offset =
1407 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1408 rel_dyn->add_local_relative(object, r_sym,
1409 elfcpp::R_386_RELATIVE,
1410 got, got_offset);
1414 break;
1416 // These are relocations which should only be seen by the
1417 // dynamic linker, and should never be seen here.
1418 case elfcpp::R_386_COPY:
1419 case elfcpp::R_386_GLOB_DAT:
1420 case elfcpp::R_386_JUMP_SLOT:
1421 case elfcpp::R_386_RELATIVE:
1422 case elfcpp::R_386_IRELATIVE:
1423 case elfcpp::R_386_TLS_TPOFF:
1424 case elfcpp::R_386_TLS_DTPMOD32:
1425 case elfcpp::R_386_TLS_DTPOFF32:
1426 case elfcpp::R_386_TLS_TPOFF32:
1427 case elfcpp::R_386_TLS_DESC:
1428 gold_error(_("%s: unexpected reloc %u in object file"),
1429 object->name().c_str(), r_type);
1430 break;
1432 // These are initial TLS relocs, which are expected when
1433 // linking.
1434 case elfcpp::R_386_TLS_GD: // Global-dynamic
1435 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1436 case elfcpp::R_386_TLS_DESC_CALL:
1437 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1438 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1439 case elfcpp::R_386_TLS_IE: // Initial-exec
1440 case elfcpp::R_386_TLS_IE_32:
1441 case elfcpp::R_386_TLS_GOTIE:
1442 case elfcpp::R_386_TLS_LE: // Local-exec
1443 case elfcpp::R_386_TLS_LE_32:
1445 bool output_is_shared = parameters->options().shared();
1446 const tls::Tls_optimization optimized_type
1447 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1448 switch (r_type)
1450 case elfcpp::R_386_TLS_GD: // Global-dynamic
1451 if (optimized_type == tls::TLSOPT_NONE)
1453 // Create a pair of GOT entries for the module index and
1454 // dtv-relative offset.
1455 Output_data_got<32, false>* got
1456 = target->got_section(symtab, layout);
1457 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1458 unsigned int shndx = lsym.get_st_shndx();
1459 bool is_ordinary;
1460 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1461 if (!is_ordinary)
1462 object->error(_("local symbol %u has bad shndx %u"),
1463 r_sym, shndx);
1464 else
1465 got->add_local_pair_with_rel(object, r_sym, shndx,
1466 GOT_TYPE_TLS_PAIR,
1467 target->rel_dyn_section(layout),
1468 elfcpp::R_386_TLS_DTPMOD32, 0);
1470 else if (optimized_type != tls::TLSOPT_TO_LE)
1471 unsupported_reloc_local(object, r_type);
1472 break;
1474 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1475 target->define_tls_base_symbol(symtab, layout);
1476 if (optimized_type == tls::TLSOPT_NONE)
1478 // Create a double GOT entry with an R_386_TLS_DESC
1479 // reloc. The R_386_TLS_DESC reloc is resolved
1480 // lazily, so the GOT entry needs to be in an area in
1481 // .got.plt, not .got. Call got_section to make sure
1482 // the section has been created.
1483 target->got_section(symtab, layout);
1484 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1485 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1486 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1488 unsigned int got_offset = got->add_constant(0);
1489 // The local symbol value is stored in the second
1490 // GOT entry.
1491 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1492 // That set the GOT offset of the local symbol to
1493 // point to the second entry, but we want it to
1494 // point to the first.
1495 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1496 got_offset);
1497 Reloc_section* rt = target->rel_tls_desc_section(layout);
1498 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1501 else if (optimized_type != tls::TLSOPT_TO_LE)
1502 unsupported_reloc_local(object, r_type);
1503 break;
1505 case elfcpp::R_386_TLS_DESC_CALL:
1506 break;
1508 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1509 if (optimized_type == tls::TLSOPT_NONE)
1511 // Create a GOT entry for the module index.
1512 target->got_mod_index_entry(symtab, layout, object);
1514 else if (optimized_type != tls::TLSOPT_TO_LE)
1515 unsupported_reloc_local(object, r_type);
1516 break;
1518 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1519 break;
1521 case elfcpp::R_386_TLS_IE: // Initial-exec
1522 case elfcpp::R_386_TLS_IE_32:
1523 case elfcpp::R_386_TLS_GOTIE:
1524 layout->set_has_static_tls();
1525 if (optimized_type == tls::TLSOPT_NONE)
1527 // For the R_386_TLS_IE relocation, we need to create a
1528 // dynamic relocation when building a shared library.
1529 if (r_type == elfcpp::R_386_TLS_IE
1530 && parameters->options().shared())
1532 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1533 unsigned int r_sym
1534 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1535 rel_dyn->add_local_relative(object, r_sym,
1536 elfcpp::R_386_RELATIVE,
1537 output_section, data_shndx,
1538 reloc.get_r_offset());
1540 // Create a GOT entry for the tp-relative offset.
1541 Output_data_got<32, false>* got
1542 = target->got_section(symtab, layout);
1543 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1544 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1545 ? elfcpp::R_386_TLS_TPOFF32
1546 : elfcpp::R_386_TLS_TPOFF);
1547 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1548 ? GOT_TYPE_TLS_OFFSET
1549 : GOT_TYPE_TLS_NOFFSET);
1550 got->add_local_with_rel(object, r_sym, got_type,
1551 target->rel_dyn_section(layout),
1552 dyn_r_type);
1554 else if (optimized_type != tls::TLSOPT_TO_LE)
1555 unsupported_reloc_local(object, r_type);
1556 break;
1558 case elfcpp::R_386_TLS_LE: // Local-exec
1559 case elfcpp::R_386_TLS_LE_32:
1560 layout->set_has_static_tls();
1561 if (output_is_shared)
1563 // We need to create a dynamic relocation.
1564 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1565 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1566 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1567 ? elfcpp::R_386_TLS_TPOFF32
1568 : elfcpp::R_386_TLS_TPOFF);
1569 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1570 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1571 data_shndx, reloc.get_r_offset());
1573 break;
1575 default:
1576 gold_unreachable();
1579 break;
1581 case elfcpp::R_386_32PLT:
1582 case elfcpp::R_386_TLS_GD_32:
1583 case elfcpp::R_386_TLS_GD_PUSH:
1584 case elfcpp::R_386_TLS_GD_CALL:
1585 case elfcpp::R_386_TLS_GD_POP:
1586 case elfcpp::R_386_TLS_LDM_32:
1587 case elfcpp::R_386_TLS_LDM_PUSH:
1588 case elfcpp::R_386_TLS_LDM_CALL:
1589 case elfcpp::R_386_TLS_LDM_POP:
1590 case elfcpp::R_386_USED_BY_INTEL_200:
1591 default:
1592 unsupported_reloc_local(object, r_type);
1593 break;
1597 // Report an unsupported relocation against a global symbol.
1599 void
1600 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1601 unsigned int r_type,
1602 Symbol* gsym)
1604 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1605 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1608 inline bool
1609 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1611 switch (r_type)
1613 case elfcpp::R_386_32:
1614 case elfcpp::R_386_16:
1615 case elfcpp::R_386_8:
1616 case elfcpp::R_386_GOTOFF:
1617 case elfcpp::R_386_GOT32:
1619 return true;
1621 default:
1622 return false;
1624 return false;
1627 inline bool
1628 Target_i386::Scan::local_reloc_may_be_function_pointer(
1629 Symbol_table* ,
1630 Layout* ,
1631 Target_i386* ,
1632 Sized_relobj<32, false>* ,
1633 unsigned int ,
1634 Output_section* ,
1635 const elfcpp::Rel<32, false>& ,
1636 unsigned int r_type,
1637 const elfcpp::Sym<32, false>&)
1639 return possible_function_pointer_reloc(r_type);
1642 inline bool
1643 Target_i386::Scan::global_reloc_may_be_function_pointer(
1644 Symbol_table* ,
1645 Layout* ,
1646 Target_i386* ,
1647 Sized_relobj<32, false>* ,
1648 unsigned int ,
1649 Output_section* ,
1650 const elfcpp::Rel<32, false>& ,
1651 unsigned int r_type,
1652 Symbol*)
1654 return possible_function_pointer_reloc(r_type);
1657 // Scan a relocation for a global symbol.
1659 inline void
1660 Target_i386::Scan::global(Symbol_table* symtab,
1661 Layout* layout,
1662 Target_i386* target,
1663 Sized_relobj<32, false>* object,
1664 unsigned int data_shndx,
1665 Output_section* output_section,
1666 const elfcpp::Rel<32, false>& reloc,
1667 unsigned int r_type,
1668 Symbol* gsym)
1670 // A STT_GNU_IFUNC symbol may require a PLT entry.
1671 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1672 && this->reloc_needs_plt_for_ifunc(object, r_type))
1673 target->make_plt_entry(symtab, layout, gsym);
1675 switch (r_type)
1677 case elfcpp::R_386_NONE:
1678 case elfcpp::R_386_GNU_VTINHERIT:
1679 case elfcpp::R_386_GNU_VTENTRY:
1680 break;
1682 case elfcpp::R_386_32:
1683 case elfcpp::R_386_16:
1684 case elfcpp::R_386_8:
1686 // Make a PLT entry if necessary.
1687 if (gsym->needs_plt_entry())
1689 target->make_plt_entry(symtab, layout, gsym);
1690 // Since this is not a PC-relative relocation, we may be
1691 // taking the address of a function. In that case we need to
1692 // set the entry in the dynamic symbol table to the address of
1693 // the PLT entry.
1694 if (gsym->is_from_dynobj() && !parameters->options().shared())
1695 gsym->set_needs_dynsym_value();
1697 // Make a dynamic relocation if necessary.
1698 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1700 if (gsym->may_need_copy_reloc())
1702 target->copy_reloc(symtab, layout, object,
1703 data_shndx, output_section, gsym, reloc);
1705 else if (r_type == elfcpp::R_386_32
1706 && gsym->type() == elfcpp::STT_GNU_IFUNC
1707 && gsym->can_use_relative_reloc(false)
1708 && !gsym->is_from_dynobj()
1709 && !gsym->is_undefined()
1710 && !gsym->is_preemptible())
1712 // Use an IRELATIVE reloc for a locally defined
1713 // STT_GNU_IFUNC symbol. This makes a function
1714 // address in a PIE executable match the address in a
1715 // shared library that it links against.
1716 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1717 rel_dyn->add_symbolless_global_addend(gsym,
1718 elfcpp::R_386_IRELATIVE,
1719 output_section,
1720 object, data_shndx,
1721 reloc.get_r_offset());
1723 else if (r_type == elfcpp::R_386_32
1724 && gsym->can_use_relative_reloc(false))
1726 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1727 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1728 output_section, object,
1729 data_shndx, reloc.get_r_offset());
1731 else
1733 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1734 rel_dyn->add_global(gsym, r_type, output_section, object,
1735 data_shndx, reloc.get_r_offset());
1739 break;
1741 case elfcpp::R_386_PC32:
1742 case elfcpp::R_386_PC16:
1743 case elfcpp::R_386_PC8:
1745 // Make a PLT entry if necessary.
1746 if (gsym->needs_plt_entry())
1748 // These relocations are used for function calls only in
1749 // non-PIC code. For a 32-bit relocation in a shared library,
1750 // we'll need a text relocation anyway, so we can skip the
1751 // PLT entry and let the dynamic linker bind the call directly
1752 // to the target. For smaller relocations, we should use a
1753 // PLT entry to ensure that the call can reach.
1754 if (!parameters->options().shared()
1755 || r_type != elfcpp::R_386_PC32)
1756 target->make_plt_entry(symtab, layout, gsym);
1758 // Make a dynamic relocation if necessary.
1759 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1761 if (gsym->may_need_copy_reloc())
1763 target->copy_reloc(symtab, layout, object,
1764 data_shndx, output_section, gsym, reloc);
1766 else
1768 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1769 rel_dyn->add_global(gsym, r_type, output_section, object,
1770 data_shndx, reloc.get_r_offset());
1774 break;
1776 case elfcpp::R_386_GOT32:
1778 // The symbol requires a GOT entry.
1779 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1780 if (gsym->final_value_is_known())
1782 // For a STT_GNU_IFUNC symbol we want the PLT address.
1783 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1784 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1785 else
1786 got->add_global(gsym, GOT_TYPE_STANDARD);
1788 else
1790 // If this symbol is not fully resolved, we need to add a
1791 // GOT entry with a dynamic relocation.
1792 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1793 if (gsym->is_from_dynobj()
1794 || gsym->is_undefined()
1795 || gsym->is_preemptible()
1796 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1797 && parameters->options().output_is_position_independent()))
1798 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1799 rel_dyn, elfcpp::R_386_GLOB_DAT);
1800 else
1802 // For a STT_GNU_IFUNC symbol we want to write the PLT
1803 // offset into the GOT, so that function pointer
1804 // comparisons work correctly.
1805 bool is_new;
1806 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1807 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1808 else
1810 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1811 // Tell the dynamic linker to use the PLT address
1812 // when resolving relocations.
1813 if (gsym->is_from_dynobj()
1814 && !parameters->options().shared())
1815 gsym->set_needs_dynsym_value();
1817 if (is_new)
1819 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1820 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1821 got, got_off);
1826 break;
1828 case elfcpp::R_386_PLT32:
1829 // If the symbol is fully resolved, this is just a PC32 reloc.
1830 // Otherwise we need a PLT entry.
1831 if (gsym->final_value_is_known())
1832 break;
1833 // If building a shared library, we can also skip the PLT entry
1834 // if the symbol is defined in the output file and is protected
1835 // or hidden.
1836 if (gsym->is_defined()
1837 && !gsym->is_from_dynobj()
1838 && !gsym->is_preemptible())
1839 break;
1840 target->make_plt_entry(symtab, layout, gsym);
1841 break;
1843 case elfcpp::R_386_GOTOFF:
1844 case elfcpp::R_386_GOTPC:
1845 // We need a GOT section.
1846 target->got_section(symtab, layout);
1847 break;
1849 // These are relocations which should only be seen by the
1850 // dynamic linker, and should never be seen here.
1851 case elfcpp::R_386_COPY:
1852 case elfcpp::R_386_GLOB_DAT:
1853 case elfcpp::R_386_JUMP_SLOT:
1854 case elfcpp::R_386_RELATIVE:
1855 case elfcpp::R_386_IRELATIVE:
1856 case elfcpp::R_386_TLS_TPOFF:
1857 case elfcpp::R_386_TLS_DTPMOD32:
1858 case elfcpp::R_386_TLS_DTPOFF32:
1859 case elfcpp::R_386_TLS_TPOFF32:
1860 case elfcpp::R_386_TLS_DESC:
1861 gold_error(_("%s: unexpected reloc %u in object file"),
1862 object->name().c_str(), r_type);
1863 break;
1865 // These are initial tls relocs, which are expected when
1866 // linking.
1867 case elfcpp::R_386_TLS_GD: // Global-dynamic
1868 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1869 case elfcpp::R_386_TLS_DESC_CALL:
1870 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1871 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1872 case elfcpp::R_386_TLS_IE: // Initial-exec
1873 case elfcpp::R_386_TLS_IE_32:
1874 case elfcpp::R_386_TLS_GOTIE:
1875 case elfcpp::R_386_TLS_LE: // Local-exec
1876 case elfcpp::R_386_TLS_LE_32:
1878 const bool is_final = gsym->final_value_is_known();
1879 const tls::Tls_optimization optimized_type
1880 = Target_i386::optimize_tls_reloc(is_final, r_type);
1881 switch (r_type)
1883 case elfcpp::R_386_TLS_GD: // Global-dynamic
1884 if (optimized_type == tls::TLSOPT_NONE)
1886 // Create a pair of GOT entries for the module index and
1887 // dtv-relative offset.
1888 Output_data_got<32, false>* got
1889 = target->got_section(symtab, layout);
1890 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1891 target->rel_dyn_section(layout),
1892 elfcpp::R_386_TLS_DTPMOD32,
1893 elfcpp::R_386_TLS_DTPOFF32);
1895 else if (optimized_type == tls::TLSOPT_TO_IE)
1897 // Create a GOT entry for the tp-relative offset.
1898 Output_data_got<32, false>* got
1899 = target->got_section(symtab, layout);
1900 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1901 target->rel_dyn_section(layout),
1902 elfcpp::R_386_TLS_TPOFF);
1904 else if (optimized_type != tls::TLSOPT_TO_LE)
1905 unsupported_reloc_global(object, r_type, gsym);
1906 break;
1908 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1909 target->define_tls_base_symbol(symtab, layout);
1910 if (optimized_type == tls::TLSOPT_NONE)
1912 // Create a double GOT entry with an R_386_TLS_DESC
1913 // reloc. The R_386_TLS_DESC reloc is resolved
1914 // lazily, so the GOT entry needs to be in an area in
1915 // .got.plt, not .got. Call got_section to make sure
1916 // the section has been created.
1917 target->got_section(symtab, layout);
1918 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1919 Reloc_section* rt = target->rel_tls_desc_section(layout);
1920 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1921 elfcpp::R_386_TLS_DESC, 0);
1923 else if (optimized_type == tls::TLSOPT_TO_IE)
1925 // Create a GOT entry for the tp-relative offset.
1926 Output_data_got<32, false>* got
1927 = target->got_section(symtab, layout);
1928 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1929 target->rel_dyn_section(layout),
1930 elfcpp::R_386_TLS_TPOFF);
1932 else if (optimized_type != tls::TLSOPT_TO_LE)
1933 unsupported_reloc_global(object, r_type, gsym);
1934 break;
1936 case elfcpp::R_386_TLS_DESC_CALL:
1937 break;
1939 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1940 if (optimized_type == tls::TLSOPT_NONE)
1942 // Create a GOT entry for the module index.
1943 target->got_mod_index_entry(symtab, layout, object);
1945 else if (optimized_type != tls::TLSOPT_TO_LE)
1946 unsupported_reloc_global(object, r_type, gsym);
1947 break;
1949 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1950 break;
1952 case elfcpp::R_386_TLS_IE: // Initial-exec
1953 case elfcpp::R_386_TLS_IE_32:
1954 case elfcpp::R_386_TLS_GOTIE:
1955 layout->set_has_static_tls();
1956 if (optimized_type == tls::TLSOPT_NONE)
1958 // For the R_386_TLS_IE relocation, we need to create a
1959 // dynamic relocation when building a shared library.
1960 if (r_type == elfcpp::R_386_TLS_IE
1961 && parameters->options().shared())
1963 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1964 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1965 output_section, object,
1966 data_shndx,
1967 reloc.get_r_offset());
1969 // Create a GOT entry for the tp-relative offset.
1970 Output_data_got<32, false>* got
1971 = target->got_section(symtab, layout);
1972 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1973 ? elfcpp::R_386_TLS_TPOFF32
1974 : elfcpp::R_386_TLS_TPOFF);
1975 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1976 ? GOT_TYPE_TLS_OFFSET
1977 : GOT_TYPE_TLS_NOFFSET);
1978 got->add_global_with_rel(gsym, got_type,
1979 target->rel_dyn_section(layout),
1980 dyn_r_type);
1982 else if (optimized_type != tls::TLSOPT_TO_LE)
1983 unsupported_reloc_global(object, r_type, gsym);
1984 break;
1986 case elfcpp::R_386_TLS_LE: // Local-exec
1987 case elfcpp::R_386_TLS_LE_32:
1988 layout->set_has_static_tls();
1989 if (parameters->options().shared())
1991 // We need to create a dynamic relocation.
1992 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1993 ? elfcpp::R_386_TLS_TPOFF32
1994 : elfcpp::R_386_TLS_TPOFF);
1995 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1996 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1997 data_shndx, reloc.get_r_offset());
1999 break;
2001 default:
2002 gold_unreachable();
2005 break;
2007 case elfcpp::R_386_32PLT:
2008 case elfcpp::R_386_TLS_GD_32:
2009 case elfcpp::R_386_TLS_GD_PUSH:
2010 case elfcpp::R_386_TLS_GD_CALL:
2011 case elfcpp::R_386_TLS_GD_POP:
2012 case elfcpp::R_386_TLS_LDM_32:
2013 case elfcpp::R_386_TLS_LDM_PUSH:
2014 case elfcpp::R_386_TLS_LDM_CALL:
2015 case elfcpp::R_386_TLS_LDM_POP:
2016 case elfcpp::R_386_USED_BY_INTEL_200:
2017 default:
2018 unsupported_reloc_global(object, r_type, gsym);
2019 break;
2023 // Process relocations for gc.
2025 void
2026 Target_i386::gc_process_relocs(Symbol_table* symtab,
2027 Layout* layout,
2028 Sized_relobj<32, false>* object,
2029 unsigned int data_shndx,
2030 unsigned int,
2031 const unsigned char* prelocs,
2032 size_t reloc_count,
2033 Output_section* output_section,
2034 bool needs_special_offset_handling,
2035 size_t local_symbol_count,
2036 const unsigned char* plocal_symbols)
2038 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2039 Target_i386::Scan,
2040 Target_i386::Relocatable_size_for_reloc>(
2041 symtab,
2042 layout,
2043 this,
2044 object,
2045 data_shndx,
2046 prelocs,
2047 reloc_count,
2048 output_section,
2049 needs_special_offset_handling,
2050 local_symbol_count,
2051 plocal_symbols);
2054 // Scan relocations for a section.
2056 void
2057 Target_i386::scan_relocs(Symbol_table* symtab,
2058 Layout* layout,
2059 Sized_relobj<32, false>* object,
2060 unsigned int data_shndx,
2061 unsigned int sh_type,
2062 const unsigned char* prelocs,
2063 size_t reloc_count,
2064 Output_section* output_section,
2065 bool needs_special_offset_handling,
2066 size_t local_symbol_count,
2067 const unsigned char* plocal_symbols)
2069 if (sh_type == elfcpp::SHT_RELA)
2071 gold_error(_("%s: unsupported RELA reloc section"),
2072 object->name().c_str());
2073 return;
2076 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2077 Target_i386::Scan>(
2078 symtab,
2079 layout,
2080 this,
2081 object,
2082 data_shndx,
2083 prelocs,
2084 reloc_count,
2085 output_section,
2086 needs_special_offset_handling,
2087 local_symbol_count,
2088 plocal_symbols);
2091 // Finalize the sections.
2093 void
2094 Target_i386::do_finalize_sections(
2095 Layout* layout,
2096 const Input_objects*,
2097 Symbol_table* symtab)
2099 const Reloc_section* rel_plt = (this->plt_ == NULL
2100 ? NULL
2101 : this->plt_->rel_plt());
2102 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2103 this->rel_dyn_, true, false);
2105 // Emit any relocs we saved in an attempt to avoid generating COPY
2106 // relocs.
2107 if (this->copy_relocs_.any_saved_relocs())
2108 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2110 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2111 // the .got.plt section.
2112 Symbol* sym = this->global_offset_table_;
2113 if (sym != NULL)
2115 uint32_t data_size = this->got_plt_->current_data_size();
2116 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2120 // Return whether a direct absolute static relocation needs to be applied.
2121 // In cases where Scan::local() or Scan::global() has created
2122 // a dynamic relocation other than R_386_RELATIVE, the addend
2123 // of the relocation is carried in the data, and we must not
2124 // apply the static relocation.
2126 inline bool
2127 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2128 unsigned int r_type,
2129 bool is_32bit,
2130 Output_section* output_section)
2132 // If the output section is not allocated, then we didn't call
2133 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2134 // the reloc here.
2135 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2136 return true;
2138 int ref_flags = Scan::get_reference_flags(r_type);
2140 // For local symbols, we will have created a non-RELATIVE dynamic
2141 // relocation only if (a) the output is position independent,
2142 // (b) the relocation is absolute (not pc- or segment-relative), and
2143 // (c) the relocation is not 32 bits wide.
2144 if (gsym == NULL)
2145 return !(parameters->options().output_is_position_independent()
2146 && (ref_flags & Symbol::ABSOLUTE_REF)
2147 && !is_32bit);
2149 // For global symbols, we use the same helper routines used in the
2150 // scan pass. If we did not create a dynamic relocation, or if we
2151 // created a RELATIVE dynamic relocation, we should apply the static
2152 // relocation.
2153 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2154 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2155 && gsym->can_use_relative_reloc(ref_flags
2156 & Symbol::FUNCTION_CALL);
2157 return !has_dyn || is_rel;
2160 // Perform a relocation.
2162 inline bool
2163 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2164 Target_i386* target,
2165 Output_section* output_section,
2166 size_t relnum,
2167 const elfcpp::Rel<32, false>& rel,
2168 unsigned int r_type,
2169 const Sized_symbol<32>* gsym,
2170 const Symbol_value<32>* psymval,
2171 unsigned char* view,
2172 elfcpp::Elf_types<32>::Elf_Addr address,
2173 section_size_type view_size)
2175 if (this->skip_call_tls_get_addr_)
2177 if ((r_type != elfcpp::R_386_PLT32
2178 && r_type != elfcpp::R_386_PC32)
2179 || gsym == NULL
2180 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2181 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2182 _("missing expected TLS relocation"));
2183 else
2185 this->skip_call_tls_get_addr_ = false;
2186 return false;
2190 const Sized_relobj<32, false>* object = relinfo->object;
2192 // Pick the value to use for symbols defined in shared objects.
2193 Symbol_value<32> symval;
2194 if (gsym != NULL
2195 && gsym->type() == elfcpp::STT_GNU_IFUNC
2196 && r_type == elfcpp::R_386_32
2197 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2198 && gsym->can_use_relative_reloc(false)
2199 && !gsym->is_from_dynobj()
2200 && !gsym->is_undefined()
2201 && !gsym->is_preemptible())
2203 // In this case we are generating a R_386_IRELATIVE reloc. We
2204 // want to use the real value of the symbol, not the PLT offset.
2206 else if (gsym != NULL
2207 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2209 symval.set_output_value(target->plt_section()->address()
2210 + gsym->plt_offset());
2211 psymval = &symval;
2213 else if (gsym == NULL && psymval->is_ifunc_symbol())
2215 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2216 if (object->local_has_plt_offset(r_sym))
2218 symval.set_output_value(target->plt_section()->address()
2219 + object->local_plt_offset(r_sym));
2220 psymval = &symval;
2224 // Get the GOT offset if needed.
2225 // The GOT pointer points to the end of the GOT section.
2226 // We need to subtract the size of the GOT section to get
2227 // the actual offset to use in the relocation.
2228 bool have_got_offset = false;
2229 unsigned int got_offset = 0;
2230 switch (r_type)
2232 case elfcpp::R_386_GOT32:
2233 if (gsym != NULL)
2235 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2236 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2237 - target->got_size());
2239 else
2241 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2242 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2243 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2244 - target->got_size());
2246 have_got_offset = true;
2247 break;
2249 default:
2250 break;
2253 switch (r_type)
2255 case elfcpp::R_386_NONE:
2256 case elfcpp::R_386_GNU_VTINHERIT:
2257 case elfcpp::R_386_GNU_VTENTRY:
2258 break;
2260 case elfcpp::R_386_32:
2261 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2262 Relocate_functions<32, false>::rel32(view, object, psymval);
2263 break;
2265 case elfcpp::R_386_PC32:
2266 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2267 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2268 break;
2270 case elfcpp::R_386_16:
2271 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2272 Relocate_functions<32, false>::rel16(view, object, psymval);
2273 break;
2275 case elfcpp::R_386_PC16:
2276 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2277 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2278 break;
2280 case elfcpp::R_386_8:
2281 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2282 Relocate_functions<32, false>::rel8(view, object, psymval);
2283 break;
2285 case elfcpp::R_386_PC8:
2286 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2287 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2288 break;
2290 case elfcpp::R_386_PLT32:
2291 gold_assert(gsym == NULL
2292 || gsym->has_plt_offset()
2293 || gsym->final_value_is_known()
2294 || (gsym->is_defined()
2295 && !gsym->is_from_dynobj()
2296 && !gsym->is_preemptible()));
2297 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2298 break;
2300 case elfcpp::R_386_GOT32:
2301 gold_assert(have_got_offset);
2302 Relocate_functions<32, false>::rel32(view, got_offset);
2303 break;
2305 case elfcpp::R_386_GOTOFF:
2307 elfcpp::Elf_types<32>::Elf_Addr value;
2308 value = (psymval->value(object, 0)
2309 - target->got_plt_section()->address());
2310 Relocate_functions<32, false>::rel32(view, value);
2312 break;
2314 case elfcpp::R_386_GOTPC:
2316 elfcpp::Elf_types<32>::Elf_Addr value;
2317 value = target->got_plt_section()->address();
2318 Relocate_functions<32, false>::pcrel32(view, value, address);
2320 break;
2322 case elfcpp::R_386_COPY:
2323 case elfcpp::R_386_GLOB_DAT:
2324 case elfcpp::R_386_JUMP_SLOT:
2325 case elfcpp::R_386_RELATIVE:
2326 case elfcpp::R_386_IRELATIVE:
2327 // These are outstanding tls relocs, which are unexpected when
2328 // linking.
2329 case elfcpp::R_386_TLS_TPOFF:
2330 case elfcpp::R_386_TLS_DTPMOD32:
2331 case elfcpp::R_386_TLS_DTPOFF32:
2332 case elfcpp::R_386_TLS_TPOFF32:
2333 case elfcpp::R_386_TLS_DESC:
2334 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2335 _("unexpected reloc %u in object file"),
2336 r_type);
2337 break;
2339 // These are initial tls relocs, which are expected when
2340 // linking.
2341 case elfcpp::R_386_TLS_GD: // Global-dynamic
2342 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2343 case elfcpp::R_386_TLS_DESC_CALL:
2344 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2345 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2346 case elfcpp::R_386_TLS_IE: // Initial-exec
2347 case elfcpp::R_386_TLS_IE_32:
2348 case elfcpp::R_386_TLS_GOTIE:
2349 case elfcpp::R_386_TLS_LE: // Local-exec
2350 case elfcpp::R_386_TLS_LE_32:
2351 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2352 view, address, view_size);
2353 break;
2355 case elfcpp::R_386_32PLT:
2356 case elfcpp::R_386_TLS_GD_32:
2357 case elfcpp::R_386_TLS_GD_PUSH:
2358 case elfcpp::R_386_TLS_GD_CALL:
2359 case elfcpp::R_386_TLS_GD_POP:
2360 case elfcpp::R_386_TLS_LDM_32:
2361 case elfcpp::R_386_TLS_LDM_PUSH:
2362 case elfcpp::R_386_TLS_LDM_CALL:
2363 case elfcpp::R_386_TLS_LDM_POP:
2364 case elfcpp::R_386_USED_BY_INTEL_200:
2365 default:
2366 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2367 _("unsupported reloc %u"),
2368 r_type);
2369 break;
2372 return true;
2375 // Perform a TLS relocation.
2377 inline void
2378 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2379 Target_i386* target,
2380 size_t relnum,
2381 const elfcpp::Rel<32, false>& rel,
2382 unsigned int r_type,
2383 const Sized_symbol<32>* gsym,
2384 const Symbol_value<32>* psymval,
2385 unsigned char* view,
2386 elfcpp::Elf_types<32>::Elf_Addr,
2387 section_size_type view_size)
2389 Output_segment* tls_segment = relinfo->layout->tls_segment();
2391 const Sized_relobj<32, false>* object = relinfo->object;
2393 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2395 const bool is_final = (gsym == NULL
2396 ? !parameters->options().shared()
2397 : gsym->final_value_is_known());
2398 const tls::Tls_optimization optimized_type
2399 = Target_i386::optimize_tls_reloc(is_final, r_type);
2400 switch (r_type)
2402 case elfcpp::R_386_TLS_GD: // Global-dynamic
2403 if (optimized_type == tls::TLSOPT_TO_LE)
2405 gold_assert(tls_segment != NULL);
2406 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2407 rel, r_type, value, view,
2408 view_size);
2409 break;
2411 else
2413 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2414 ? GOT_TYPE_TLS_NOFFSET
2415 : GOT_TYPE_TLS_PAIR);
2416 unsigned int got_offset;
2417 if (gsym != NULL)
2419 gold_assert(gsym->has_got_offset(got_type));
2420 got_offset = gsym->got_offset(got_type) - target->got_size();
2422 else
2424 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2425 gold_assert(object->local_has_got_offset(r_sym, got_type));
2426 got_offset = (object->local_got_offset(r_sym, got_type)
2427 - target->got_size());
2429 if (optimized_type == tls::TLSOPT_TO_IE)
2431 gold_assert(tls_segment != NULL);
2432 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2433 got_offset, view, view_size);
2434 break;
2436 else if (optimized_type == tls::TLSOPT_NONE)
2438 // Relocate the field with the offset of the pair of GOT
2439 // entries.
2440 Relocate_functions<32, false>::rel32(view, got_offset);
2441 break;
2444 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2445 _("unsupported reloc %u"),
2446 r_type);
2447 break;
2449 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2450 case elfcpp::R_386_TLS_DESC_CALL:
2451 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2452 if (optimized_type == tls::TLSOPT_TO_LE)
2454 gold_assert(tls_segment != NULL);
2455 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2456 rel, r_type, value, view,
2457 view_size);
2458 break;
2460 else
2462 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2463 ? GOT_TYPE_TLS_NOFFSET
2464 : GOT_TYPE_TLS_DESC);
2465 unsigned int got_offset = 0;
2466 if (r_type == elfcpp::R_386_TLS_GOTDESC
2467 && optimized_type == tls::TLSOPT_NONE)
2469 // We created GOT entries in the .got.tlsdesc portion of
2470 // the .got.plt section, but the offset stored in the
2471 // symbol is the offset within .got.tlsdesc.
2472 got_offset = (target->got_size()
2473 + target->got_plt_section()->data_size());
2475 if (gsym != NULL)
2477 gold_assert(gsym->has_got_offset(got_type));
2478 got_offset += gsym->got_offset(got_type) - target->got_size();
2480 else
2482 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2483 gold_assert(object->local_has_got_offset(r_sym, got_type));
2484 got_offset += (object->local_got_offset(r_sym, got_type)
2485 - target->got_size());
2487 if (optimized_type == tls::TLSOPT_TO_IE)
2489 gold_assert(tls_segment != NULL);
2490 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2491 got_offset, view, view_size);
2492 break;
2494 else if (optimized_type == tls::TLSOPT_NONE)
2496 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2498 // Relocate the field with the offset of the pair of GOT
2499 // entries.
2500 Relocate_functions<32, false>::rel32(view, got_offset);
2502 break;
2505 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2506 _("unsupported reloc %u"),
2507 r_type);
2508 break;
2510 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2511 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2513 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2514 _("both SUN and GNU model "
2515 "TLS relocations"));
2516 break;
2518 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2519 if (optimized_type == tls::TLSOPT_TO_LE)
2521 gold_assert(tls_segment != NULL);
2522 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2523 value, view, view_size);
2524 break;
2526 else if (optimized_type == tls::TLSOPT_NONE)
2528 // Relocate the field with the offset of the GOT entry for
2529 // the module index.
2530 unsigned int got_offset;
2531 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2532 - target->got_size());
2533 Relocate_functions<32, false>::rel32(view, got_offset);
2534 break;
2536 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2537 _("unsupported reloc %u"),
2538 r_type);
2539 break;
2541 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2542 if (optimized_type == tls::TLSOPT_TO_LE)
2544 // This reloc can appear in debugging sections, in which
2545 // case we must not convert to local-exec. We decide what
2546 // to do based on whether the section is marked as
2547 // containing executable code. That is what the GNU linker
2548 // does as well.
2549 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2550 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2552 gold_assert(tls_segment != NULL);
2553 value -= tls_segment->memsz();
2556 Relocate_functions<32, false>::rel32(view, value);
2557 break;
2559 case elfcpp::R_386_TLS_IE: // Initial-exec
2560 case elfcpp::R_386_TLS_GOTIE:
2561 case elfcpp::R_386_TLS_IE_32:
2562 if (optimized_type == tls::TLSOPT_TO_LE)
2564 gold_assert(tls_segment != NULL);
2565 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2566 rel, r_type, value, view,
2567 view_size);
2568 break;
2570 else if (optimized_type == tls::TLSOPT_NONE)
2572 // Relocate the field with the offset of the GOT entry for
2573 // the tp-relative offset of the symbol.
2574 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2575 ? GOT_TYPE_TLS_OFFSET
2576 : GOT_TYPE_TLS_NOFFSET);
2577 unsigned int got_offset;
2578 if (gsym != NULL)
2580 gold_assert(gsym->has_got_offset(got_type));
2581 got_offset = gsym->got_offset(got_type);
2583 else
2585 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2586 gold_assert(object->local_has_got_offset(r_sym, got_type));
2587 got_offset = object->local_got_offset(r_sym, got_type);
2589 // For the R_386_TLS_IE relocation, we need to apply the
2590 // absolute address of the GOT entry.
2591 if (r_type == elfcpp::R_386_TLS_IE)
2592 got_offset += target->got_plt_section()->address();
2593 // All GOT offsets are relative to the end of the GOT.
2594 got_offset -= target->got_size();
2595 Relocate_functions<32, false>::rel32(view, got_offset);
2596 break;
2598 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2599 _("unsupported reloc %u"),
2600 r_type);
2601 break;
2603 case elfcpp::R_386_TLS_LE: // Local-exec
2604 // If we're creating a shared library, a dynamic relocation will
2605 // have been created for this location, so do not apply it now.
2606 if (!parameters->options().shared())
2608 gold_assert(tls_segment != NULL);
2609 value -= tls_segment->memsz();
2610 Relocate_functions<32, false>::rel32(view, value);
2612 break;
2614 case elfcpp::R_386_TLS_LE_32:
2615 // If we're creating a shared library, a dynamic relocation will
2616 // have been created for this location, so do not apply it now.
2617 if (!parameters->options().shared())
2619 gold_assert(tls_segment != NULL);
2620 value = tls_segment->memsz() - value;
2621 Relocate_functions<32, false>::rel32(view, value);
2623 break;
2627 // Do a relocation in which we convert a TLS General-Dynamic to a
2628 // Local-Exec.
2630 inline void
2631 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2632 size_t relnum,
2633 Output_segment* tls_segment,
2634 const elfcpp::Rel<32, false>& rel,
2635 unsigned int,
2636 elfcpp::Elf_types<32>::Elf_Addr value,
2637 unsigned char* view,
2638 section_size_type view_size)
2640 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2641 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2642 // leal foo(%reg),%eax; call ___tls_get_addr
2643 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2645 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2646 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2648 unsigned char op1 = view[-1];
2649 unsigned char op2 = view[-2];
2651 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2652 op2 == 0x8d || op2 == 0x04);
2653 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2655 int roff = 5;
2657 if (op2 == 0x04)
2659 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2660 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2661 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2662 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2663 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2665 else
2667 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2668 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2669 if (rel.get_r_offset() + 9 < view_size
2670 && view[9] == 0x90)
2672 // There is a trailing nop. Use the size byte subl.
2673 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2674 roff = 6;
2676 else
2678 // Use the five byte subl.
2679 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2683 value = tls_segment->memsz() - value;
2684 Relocate_functions<32, false>::rel32(view + roff, value);
2686 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2687 // We can skip it.
2688 this->skip_call_tls_get_addr_ = true;
2691 // Do a relocation in which we convert a TLS General-Dynamic to an
2692 // Initial-Exec.
2694 inline void
2695 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2696 size_t relnum,
2697 Output_segment*,
2698 const elfcpp::Rel<32, false>& rel,
2699 unsigned int,
2700 elfcpp::Elf_types<32>::Elf_Addr value,
2701 unsigned char* view,
2702 section_size_type view_size)
2704 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2705 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2707 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2708 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2710 unsigned char op1 = view[-1];
2711 unsigned char op2 = view[-2];
2713 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2714 op2 == 0x8d || op2 == 0x04);
2715 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2717 int roff = 5;
2719 // FIXME: For now, support only the first (SIB) form.
2720 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2722 if (op2 == 0x04)
2724 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2725 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2726 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2727 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2728 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2730 else
2732 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2733 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2734 if (rel.get_r_offset() + 9 < view_size
2735 && view[9] == 0x90)
2737 // FIXME: This is not the right instruction sequence.
2738 // There is a trailing nop. Use the size byte subl.
2739 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2740 roff = 6;
2742 else
2744 // FIXME: This is not the right instruction sequence.
2745 // Use the five byte subl.
2746 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2750 Relocate_functions<32, false>::rel32(view + roff, value);
2752 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2753 // We can skip it.
2754 this->skip_call_tls_get_addr_ = true;
2757 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2758 // General-Dynamic to a Local-Exec.
2760 inline void
2761 Target_i386::Relocate::tls_desc_gd_to_le(
2762 const Relocate_info<32, false>* relinfo,
2763 size_t relnum,
2764 Output_segment* tls_segment,
2765 const elfcpp::Rel<32, false>& rel,
2766 unsigned int r_type,
2767 elfcpp::Elf_types<32>::Elf_Addr value,
2768 unsigned char* view,
2769 section_size_type view_size)
2771 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2773 // leal foo@TLSDESC(%ebx), %eax
2774 // ==> leal foo@NTPOFF, %eax
2775 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2776 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2777 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2778 view[-2] == 0x8d && view[-1] == 0x83);
2779 view[-1] = 0x05;
2780 value -= tls_segment->memsz();
2781 Relocate_functions<32, false>::rel32(view, value);
2783 else
2785 // call *foo@TLSCALL(%eax)
2786 // ==> nop; nop
2787 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2788 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2789 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2790 view[0] == 0xff && view[1] == 0x10);
2791 view[0] = 0x66;
2792 view[1] = 0x90;
2796 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2797 // General-Dynamic to an Initial-Exec.
2799 inline void
2800 Target_i386::Relocate::tls_desc_gd_to_ie(
2801 const Relocate_info<32, false>* relinfo,
2802 size_t relnum,
2803 Output_segment*,
2804 const elfcpp::Rel<32, false>& rel,
2805 unsigned int r_type,
2806 elfcpp::Elf_types<32>::Elf_Addr value,
2807 unsigned char* view,
2808 section_size_type view_size)
2810 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2812 // leal foo@TLSDESC(%ebx), %eax
2813 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2814 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2815 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2816 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2817 view[-2] == 0x8d && view[-1] == 0x83);
2818 view[-2] = 0x8b;
2819 Relocate_functions<32, false>::rel32(view, value);
2821 else
2823 // call *foo@TLSCALL(%eax)
2824 // ==> nop; nop
2825 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2826 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2827 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2828 view[0] == 0xff && view[1] == 0x10);
2829 view[0] = 0x66;
2830 view[1] = 0x90;
2834 // Do a relocation in which we convert a TLS Local-Dynamic to a
2835 // Local-Exec.
2837 inline void
2838 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2839 size_t relnum,
2840 Output_segment*,
2841 const elfcpp::Rel<32, false>& rel,
2842 unsigned int,
2843 elfcpp::Elf_types<32>::Elf_Addr,
2844 unsigned char* view,
2845 section_size_type view_size)
2847 // leal foo(%reg), %eax; call ___tls_get_addr
2848 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2850 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2851 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2853 // FIXME: Does this test really always pass?
2854 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2855 view[-2] == 0x8d && view[-1] == 0x83);
2857 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2859 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2861 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2862 // We can skip it.
2863 this->skip_call_tls_get_addr_ = true;
2866 // Do a relocation in which we convert a TLS Initial-Exec to a
2867 // Local-Exec.
2869 inline void
2870 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2871 size_t relnum,
2872 Output_segment* tls_segment,
2873 const elfcpp::Rel<32, false>& rel,
2874 unsigned int r_type,
2875 elfcpp::Elf_types<32>::Elf_Addr value,
2876 unsigned char* view,
2877 section_size_type view_size)
2879 // We have to actually change the instructions, which means that we
2880 // need to examine the opcodes to figure out which instruction we
2881 // are looking at.
2882 if (r_type == elfcpp::R_386_TLS_IE)
2884 // movl %gs:XX,%eax ==> movl $YY,%eax
2885 // movl %gs:XX,%reg ==> movl $YY,%reg
2886 // addl %gs:XX,%reg ==> addl $YY,%reg
2887 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2888 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2890 unsigned char op1 = view[-1];
2891 if (op1 == 0xa1)
2893 // movl XX,%eax ==> movl $YY,%eax
2894 view[-1] = 0xb8;
2896 else
2898 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2900 unsigned char op2 = view[-2];
2901 if (op2 == 0x8b)
2903 // movl XX,%reg ==> movl $YY,%reg
2904 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2905 (op1 & 0xc7) == 0x05);
2906 view[-2] = 0xc7;
2907 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2909 else if (op2 == 0x03)
2911 // addl XX,%reg ==> addl $YY,%reg
2912 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2913 (op1 & 0xc7) == 0x05);
2914 view[-2] = 0x81;
2915 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2917 else
2918 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2921 else
2923 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2924 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2925 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2926 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2927 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2929 unsigned char op1 = view[-1];
2930 unsigned char op2 = view[-2];
2931 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2932 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2933 if (op2 == 0x8b)
2935 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2936 view[-2] = 0xc7;
2937 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2939 else if (op2 == 0x2b)
2941 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2942 view[-2] = 0x81;
2943 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2945 else if (op2 == 0x03)
2947 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2948 view[-2] = 0x81;
2949 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2951 else
2952 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2955 value = tls_segment->memsz() - value;
2956 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2957 value = - value;
2959 Relocate_functions<32, false>::rel32(view, value);
2962 // Relocate section data.
2964 void
2965 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2966 unsigned int sh_type,
2967 const unsigned char* prelocs,
2968 size_t reloc_count,
2969 Output_section* output_section,
2970 bool needs_special_offset_handling,
2971 unsigned char* view,
2972 elfcpp::Elf_types<32>::Elf_Addr address,
2973 section_size_type view_size,
2974 const Reloc_symbol_changes* reloc_symbol_changes)
2976 gold_assert(sh_type == elfcpp::SHT_REL);
2978 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2979 Target_i386::Relocate>(
2980 relinfo,
2981 this,
2982 prelocs,
2983 reloc_count,
2984 output_section,
2985 needs_special_offset_handling,
2986 view,
2987 address,
2988 view_size,
2989 reloc_symbol_changes);
2992 // Return the size of a relocation while scanning during a relocatable
2993 // link.
2995 unsigned int
2996 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2997 unsigned int r_type,
2998 Relobj* object)
3000 switch (r_type)
3002 case elfcpp::R_386_NONE:
3003 case elfcpp::R_386_GNU_VTINHERIT:
3004 case elfcpp::R_386_GNU_VTENTRY:
3005 case elfcpp::R_386_TLS_GD: // Global-dynamic
3006 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3007 case elfcpp::R_386_TLS_DESC_CALL:
3008 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3009 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3010 case elfcpp::R_386_TLS_IE: // Initial-exec
3011 case elfcpp::R_386_TLS_IE_32:
3012 case elfcpp::R_386_TLS_GOTIE:
3013 case elfcpp::R_386_TLS_LE: // Local-exec
3014 case elfcpp::R_386_TLS_LE_32:
3015 return 0;
3017 case elfcpp::R_386_32:
3018 case elfcpp::R_386_PC32:
3019 case elfcpp::R_386_GOT32:
3020 case elfcpp::R_386_PLT32:
3021 case elfcpp::R_386_GOTOFF:
3022 case elfcpp::R_386_GOTPC:
3023 return 4;
3025 case elfcpp::R_386_16:
3026 case elfcpp::R_386_PC16:
3027 return 2;
3029 case elfcpp::R_386_8:
3030 case elfcpp::R_386_PC8:
3031 return 1;
3033 // These are relocations which should only be seen by the
3034 // dynamic linker, and should never be seen here.
3035 case elfcpp::R_386_COPY:
3036 case elfcpp::R_386_GLOB_DAT:
3037 case elfcpp::R_386_JUMP_SLOT:
3038 case elfcpp::R_386_RELATIVE:
3039 case elfcpp::R_386_IRELATIVE:
3040 case elfcpp::R_386_TLS_TPOFF:
3041 case elfcpp::R_386_TLS_DTPMOD32:
3042 case elfcpp::R_386_TLS_DTPOFF32:
3043 case elfcpp::R_386_TLS_TPOFF32:
3044 case elfcpp::R_386_TLS_DESC:
3045 object->error(_("unexpected reloc %u in object file"), r_type);
3046 return 0;
3048 case elfcpp::R_386_32PLT:
3049 case elfcpp::R_386_TLS_GD_32:
3050 case elfcpp::R_386_TLS_GD_PUSH:
3051 case elfcpp::R_386_TLS_GD_CALL:
3052 case elfcpp::R_386_TLS_GD_POP:
3053 case elfcpp::R_386_TLS_LDM_32:
3054 case elfcpp::R_386_TLS_LDM_PUSH:
3055 case elfcpp::R_386_TLS_LDM_CALL:
3056 case elfcpp::R_386_TLS_LDM_POP:
3057 case elfcpp::R_386_USED_BY_INTEL_200:
3058 default:
3059 object->error(_("unsupported reloc %u in object file"), r_type);
3060 return 0;
3064 // Scan the relocs during a relocatable link.
3066 void
3067 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3068 Layout* layout,
3069 Sized_relobj<32, false>* object,
3070 unsigned int data_shndx,
3071 unsigned int sh_type,
3072 const unsigned char* prelocs,
3073 size_t reloc_count,
3074 Output_section* output_section,
3075 bool needs_special_offset_handling,
3076 size_t local_symbol_count,
3077 const unsigned char* plocal_symbols,
3078 Relocatable_relocs* rr)
3080 gold_assert(sh_type == elfcpp::SHT_REL);
3082 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3083 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3085 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3086 Scan_relocatable_relocs>(
3087 symtab,
3088 layout,
3089 object,
3090 data_shndx,
3091 prelocs,
3092 reloc_count,
3093 output_section,
3094 needs_special_offset_handling,
3095 local_symbol_count,
3096 plocal_symbols,
3097 rr);
3100 // Relocate a section during a relocatable link.
3102 void
3103 Target_i386::relocate_for_relocatable(
3104 const Relocate_info<32, false>* relinfo,
3105 unsigned int sh_type,
3106 const unsigned char* prelocs,
3107 size_t reloc_count,
3108 Output_section* output_section,
3109 off_t offset_in_output_section,
3110 const Relocatable_relocs* rr,
3111 unsigned char* view,
3112 elfcpp::Elf_types<32>::Elf_Addr view_address,
3113 section_size_type view_size,
3114 unsigned char* reloc_view,
3115 section_size_type reloc_view_size)
3117 gold_assert(sh_type == elfcpp::SHT_REL);
3119 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3120 relinfo,
3121 prelocs,
3122 reloc_count,
3123 output_section,
3124 offset_in_output_section,
3126 view,
3127 view_address,
3128 view_size,
3129 reloc_view,
3130 reloc_view_size);
3133 // Return the value to use for a dynamic which requires special
3134 // treatment. This is how we support equality comparisons of function
3135 // pointers across shared library boundaries, as described in the
3136 // processor specific ABI supplement.
3138 uint64_t
3139 Target_i386::do_dynsym_value(const Symbol* gsym) const
3141 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3142 return this->plt_section()->address() + gsym->plt_offset();
3145 // Return a string used to fill a code section with nops to take up
3146 // the specified length.
3148 std::string
3149 Target_i386::do_code_fill(section_size_type length) const
3151 if (length >= 16)
3153 // Build a jmp instruction to skip over the bytes.
3154 unsigned char jmp[5];
3155 jmp[0] = 0xe9;
3156 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3157 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3158 + std::string(length - 5, '\0'));
3161 // Nop sequences of various lengths.
3162 const char nop1[1] = { 0x90 }; // nop
3163 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3164 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3165 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3166 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3167 0x00 }; // leal 0(%esi,1),%esi
3168 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3169 0x00, 0x00 };
3170 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3171 0x00, 0x00, 0x00 };
3172 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3173 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3174 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3175 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3176 0x00 };
3177 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3178 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3179 0x00, 0x00 };
3180 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3181 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3182 0x00, 0x00, 0x00 };
3183 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3184 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3185 0x00, 0x00, 0x00, 0x00 };
3186 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3187 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3188 0x27, 0x00, 0x00, 0x00,
3189 0x00 };
3190 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3191 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3192 0xbc, 0x27, 0x00, 0x00,
3193 0x00, 0x00 };
3194 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3195 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3196 0x90, 0x90, 0x90, 0x90,
3197 0x90, 0x90, 0x90 };
3199 const char* nops[16] = {
3200 NULL,
3201 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3202 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3205 return std::string(nops[length], length);
3208 // Return whether SYM should be treated as a call to a non-split
3209 // function. We don't want that to be true of a call to a
3210 // get_pc_thunk function.
3212 bool
3213 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3215 return (sym->type() == elfcpp::STT_FUNC
3216 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3219 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3220 // compiled with -fsplit-stack. The function calls non-split-stack
3221 // code. We have to change the function so that it always ensures
3222 // that it has enough stack space to run some random function.
3224 void
3225 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3226 section_offset_type fnoffset,
3227 section_size_type fnsize,
3228 unsigned char* view,
3229 section_size_type view_size,
3230 std::string* from,
3231 std::string* to) const
3233 // The function starts with a comparison of the stack pointer and a
3234 // field in the TCB. This is followed by a jump.
3236 // cmp %gs:NN,%esp
3237 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3238 && fnsize > 7)
3240 // We will call __morestack if the carry flag is set after this
3241 // comparison. We turn the comparison into an stc instruction
3242 // and some nops.
3243 view[fnoffset] = '\xf9';
3244 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3246 // lea NN(%esp),%ecx
3247 // lea NN(%esp),%edx
3248 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3249 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3250 && fnsize > 7)
3252 // This is loading an offset from the stack pointer for a
3253 // comparison. The offset is negative, so we decrease the
3254 // offset by the amount of space we need for the stack. This
3255 // means we will avoid calling __morestack if there happens to
3256 // be plenty of space on the stack already.
3257 unsigned char* pval = view + fnoffset + 3;
3258 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3259 val -= parameters->options().split_stack_adjust_size();
3260 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3262 else
3264 if (!object->has_no_split_stack())
3265 object->error(_("failed to match split-stack sequence at "
3266 "section %u offset %0zx"),
3267 shndx, static_cast<size_t>(fnoffset));
3268 return;
3271 // We have to change the function so that it calls
3272 // __morestack_non_split instead of __morestack. The former will
3273 // allocate additional stack space.
3274 *from = "__morestack";
3275 *to = "__morestack_non_split";
3278 // The selector for i386 object files.
3280 class Target_selector_i386 : public Target_selector_freebsd
3282 public:
3283 Target_selector_i386()
3284 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3285 "elf32-i386", "elf32-i386-freebsd")
3288 Target*
3289 do_instantiate_target()
3290 { return new Target_i386(); }
3293 Target_selector_i386 target_selector_i386;
3295 } // End anonymous namespace.