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.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 // A class to handle the PLT data.
50 class Output_data_plt_i386
: public Output_section_data
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.
59 add_entry(Symbol
* gsym
);
61 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
63 add_local_ifunc_entry(Sized_relobj_file
<32, false>* relobj
,
64 unsigned int local_sym_index
);
66 // Return the .rel.plt section data.
69 { return this->rel_
; }
71 // Return where the TLS_DESC relocations should go.
73 rel_tls_desc(Layout
*);
75 // Return the number of PLT entries.
78 { return this->count_
; }
80 // Return the offset of the first non-reserved PLT entry.
82 first_plt_entry_offset()
83 { return plt_entry_size
; }
85 // Return the size of a PLT entry.
88 { return plt_entry_size
; }
92 do_adjust_output_section(Output_section
* os
);
94 // Write to a map file.
96 do_print_to_mapfile(Mapfile
* mapfile
) const
97 { mapfile
->print_output_data(this, _("** PLT")); }
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.
117 set_final_data_size()
118 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
120 // Write out the PLT data.
122 do_write(Output_file
*);
124 // We keep a list of global STT_GNU_IFUNC symbols, each with its
125 // offset in the GOT.
129 unsigned int got_offset
;
132 // We keep a list of local STT_GNU_IFUNC symbols, each with its
133 // offset in the GOT.
136 Sized_relobj_file
<32, false>* object
;
137 unsigned int local_sym_index
;
138 unsigned int got_offset
;
141 // The reloc section.
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.
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>
164 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
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)
175 can_check_for_function_pointers() const
179 can_icf_inline_merge_sections () const
182 // Process the relocations to determine unreferenced sections for
183 // garbage collection.
185 gc_process_relocs(Symbol_table
* symtab
,
187 Sized_relobj_file
<32, false>* object
,
188 unsigned int data_shndx
,
189 unsigned int sh_type
,
190 const unsigned char* prelocs
,
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.
199 scan_relocs(Symbol_table
* symtab
,
201 Sized_relobj_file
<32, false>* object
,
202 unsigned int data_shndx
,
203 unsigned int sh_type
,
204 const unsigned char* prelocs
,
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.
213 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
215 // Return the value to use for a dynamic which requires special
218 do_dynsym_value(const Symbol
*) const;
220 // Relocate a section.
222 relocate_section(const Relocate_info
<32, false>*,
223 unsigned int sh_type
,
224 const unsigned char* prelocs
,
226 Output_section
* output_section
,
227 bool needs_special_offset_handling
,
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.
235 scan_relocatable_relocs(Symbol_table
* symtab
,
237 Sized_relobj_file
<32, false>* object
,
238 unsigned int data_shndx
,
239 unsigned int sh_type
,
240 const unsigned char* prelocs
,
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.
250 relocate_for_relocatable(const Relocate_info
<32, false>*,
251 unsigned int sh_type
,
252 const unsigned char* prelocs
,
254 Output_section
* output_section
,
255 off_t offset_in_output_section
,
256 const Relocatable_relocs
*,
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.
265 do_code_fill(section_size_type length
) const;
267 // Return whether SYM is defined by the ABI.
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.
278 do_is_local_label_name(const char* name
) const
280 if (name
[0] == '.' && name
[1] == 'X')
282 return Target::do_is_local_label_name(name
);
285 // Return the PLT section.
287 do_plt_section_for_global(const Symbol
*) const
288 { return this->plt_section(); }
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.
296 do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const;
298 // Adjust -fsplit-stack code which calls non-split-stack code.
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.
309 gold_assert(this->got_
!= NULL
);
310 return this->got_
->data_size();
313 // Return the number of entries in the GOT.
315 got_entry_count() const
317 if (this->got_
== NULL
)
319 return this->got_size() / 4;
322 // Return the number of entries in the PLT.
324 plt_entry_count() const;
326 // Return the offset of the first non-reserved PLT entry.
328 first_plt_entry_offset() const;
330 // Return the size of each PLT entry.
332 plt_entry_size() const;
335 // The class which scans relocations.
340 get_reference_flags(unsigned int r_type
);
343 local(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
344 Sized_relobj_file
<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
);
351 global(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
352 Sized_relobj_file
<32, false>* object
,
353 unsigned int data_shndx
,
354 Output_section
* output_section
,
355 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
359 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
361 Sized_relobj_file
<32, false>* object
,
362 unsigned int data_shndx
,
363 Output_section
* output_section
,
364 const elfcpp::Rel
<32, false>& reloc
,
366 const elfcpp::Sym
<32, false>& lsym
);
369 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
371 Sized_relobj_file
<32, false>* object
,
372 unsigned int data_shndx
,
373 Output_section
* output_section
,
374 const elfcpp::Rel
<32, false>& reloc
,
379 possible_function_pointer_reloc(unsigned int r_type
);
382 reloc_needs_plt_for_ifunc(Sized_relobj_file
<32, false>*,
383 unsigned int r_type
);
386 unsupported_reloc_local(Sized_relobj_file
<32, false>*, unsigned int r_type
);
389 unsupported_reloc_global(Sized_relobj_file
<32, false>*, unsigned int r_type
,
393 // The class which implements relocation.
398 : skip_call_tls_get_addr_(false),
399 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
404 if (this->skip_call_tls_get_addr_
)
406 // FIXME: This needs to specify the location somehow.
407 gold_error(_("missing expected TLS relocation"));
411 // Return whether the static relocation needs to be applied.
413 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
416 Output_section
* output_section
);
418 // Do a relocation. Return false if the caller should not issue
419 // any warnings about this relocation.
421 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
422 size_t relnum
, const elfcpp::Rel
<32, false>&,
423 unsigned int r_type
, const Sized_symbol
<32>*,
424 const Symbol_value
<32>*,
425 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
429 // Do a TLS relocation.
431 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
432 size_t relnum
, const elfcpp::Rel
<32, false>&,
433 unsigned int r_type
, const Sized_symbol
<32>*,
434 const Symbol_value
<32>*,
435 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
438 // Do a TLS General-Dynamic to Initial-Exec transition.
440 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
441 Output_segment
* tls_segment
,
442 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
443 elfcpp::Elf_types
<32>::Elf_Addr value
,
445 section_size_type view_size
);
447 // Do a TLS General-Dynamic to Local-Exec transition.
449 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
450 Output_segment
* tls_segment
,
451 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
452 elfcpp::Elf_types
<32>::Elf_Addr value
,
454 section_size_type view_size
);
456 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
459 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
460 Output_segment
* tls_segment
,
461 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
462 elfcpp::Elf_types
<32>::Elf_Addr value
,
464 section_size_type view_size
);
466 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
469 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
470 Output_segment
* tls_segment
,
471 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
472 elfcpp::Elf_types
<32>::Elf_Addr value
,
474 section_size_type view_size
);
476 // Do a TLS Local-Dynamic to Local-Exec transition.
478 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
479 Output_segment
* tls_segment
,
480 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
481 elfcpp::Elf_types
<32>::Elf_Addr value
,
483 section_size_type view_size
);
485 // Do a TLS Initial-Exec to Local-Exec transition.
487 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
488 Output_segment
* tls_segment
,
489 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
490 elfcpp::Elf_types
<32>::Elf_Addr value
,
492 section_size_type view_size
);
494 // We need to keep track of which type of local dynamic relocation
495 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
496 enum Local_dynamic_type
503 // This is set if we should skip the next reloc, which should be a
504 // PLT32 reloc against ___tls_get_addr.
505 bool skip_call_tls_get_addr_
;
506 // The type of local dynamic relocation we have seen in the section
507 // being relocated, if any.
508 Local_dynamic_type local_dynamic_type_
;
511 // A class which returns the size required for a relocation type,
512 // used while scanning relocs during a relocatable link.
513 class Relocatable_size_for_reloc
517 get_size_for_reloc(unsigned int, Relobj
*);
520 // Adjust TLS relocation type based on the options and whether this
521 // is a local symbol.
522 static tls::Tls_optimization
523 optimize_tls_reloc(bool is_final
, int r_type
);
525 // Get the GOT section, creating it if necessary.
526 Output_data_got
<32, false>*
527 got_section(Symbol_table
*, Layout
*);
529 // Get the GOT PLT section.
531 got_plt_section() const
533 gold_assert(this->got_plt_
!= NULL
);
534 return this->got_plt_
;
537 // Get the GOT section for TLSDESC entries.
538 Output_data_got
<32, false>*
539 got_tlsdesc_section() const
541 gold_assert(this->got_tlsdesc_
!= NULL
);
542 return this->got_tlsdesc_
;
545 // Create the PLT section.
547 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
549 // Create a PLT entry for a global symbol.
551 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
553 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
555 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
556 Sized_relobj_file
<32, false>* relobj
,
557 unsigned int local_sym_index
);
559 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
561 define_tls_base_symbol(Symbol_table
*, Layout
*);
563 // Create a GOT entry for the TLS module index.
565 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
566 Sized_relobj_file
<32, false>* object
);
568 // Get the PLT section.
569 Output_data_plt_i386
*
572 gold_assert(this->plt_
!= NULL
);
576 // Get the dynamic reloc section, creating it if necessary.
578 rel_dyn_section(Layout
*);
580 // Get the section to use for TLS_DESC relocations.
582 rel_tls_desc_section(Layout
*) const;
584 // Add a potential copy relocation.
586 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
587 Sized_relobj_file
<32, false>* object
,
588 unsigned int shndx
, Output_section
* output_section
,
589 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
591 this->copy_relocs_
.copy_reloc(symtab
, layout
,
592 symtab
->get_sized_symbol
<32>(sym
),
593 object
, shndx
, output_section
, reloc
,
594 this->rel_dyn_section(layout
));
597 // Information about this specific target which we pass to the
598 // general Target structure.
599 static const Target::Target_info i386_info
;
601 // The types of GOT entries needed for this platform.
602 // These values are exposed to the ABI in an incremental link.
603 // Do not renumber existing values without changing the version
604 // number of the .gnu_incremental_inputs section.
607 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
608 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
609 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
610 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
611 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
615 Output_data_got
<32, false>* got_
;
617 Output_data_plt_i386
* plt_
;
618 // The GOT PLT section.
619 Output_data_space
* got_plt_
;
620 // The GOT section for TLSDESC relocations.
621 Output_data_got
<32, false>* got_tlsdesc_
;
622 // The _GLOBAL_OFFSET_TABLE_ symbol.
623 Symbol
* global_offset_table_
;
624 // The dynamic reloc section.
625 Reloc_section
* rel_dyn_
;
626 // Relocs saved to avoid a COPY reloc.
627 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
628 // Space for variables copied with a COPY reloc.
629 Output_data_space
* dynbss_
;
630 // Offset of the GOT entry for the TLS module index.
631 unsigned int got_mod_index_offset_
;
632 // True if the _TLS_MODULE_BASE_ symbol has been defined.
633 bool tls_base_symbol_defined_
;
636 const Target::Target_info
Target_i386::i386_info
=
639 false, // is_big_endian
640 elfcpp::EM_386
, // machine_code
641 false, // has_make_symbol
642 false, // has_resolve
643 true, // has_code_fill
644 true, // is_default_stack_executable
646 "/usr/lib/libc.so.1", // dynamic_linker
647 0x08048000, // default_text_segment_address
648 0x1000, // abi_pagesize (overridable by -z max-page-size)
649 0x1000, // common_pagesize (overridable by -z common-page-size)
650 elfcpp::SHN_UNDEF
, // small_common_shndx
651 elfcpp::SHN_UNDEF
, // large_common_shndx
652 0, // small_common_section_flags
653 0, // large_common_section_flags
654 NULL
, // attributes_section
655 NULL
// attributes_vendor
658 // Get the GOT section, creating it if necessary.
660 Output_data_got
<32, false>*
661 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
663 if (this->got_
== NULL
)
665 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
667 this->got_
= new Output_data_got
<32, false>();
669 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
671 | elfcpp::SHF_WRITE
),
672 this->got_
, ORDER_RELRO_LAST
, true);
674 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
675 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
677 | elfcpp::SHF_WRITE
),
678 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
681 // The first three entries are reserved.
682 this->got_plt_
->set_current_data_size(3 * 4);
684 // Those bytes can go into the relro segment.
685 layout
->increase_relro(3 * 4);
687 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
688 this->global_offset_table_
=
689 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
690 Symbol_table::PREDEFINED
,
692 0, 0, elfcpp::STT_OBJECT
,
694 elfcpp::STV_HIDDEN
, 0,
697 // If there are any TLSDESC relocations, they get GOT entries in
698 // .got.plt after the jump slot entries.
699 this->got_tlsdesc_
= new Output_data_got
<32, false>();
700 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
702 | elfcpp::SHF_WRITE
),
704 ORDER_NON_RELRO_FIRST
, false);
710 // Get the dynamic reloc section, creating it if necessary.
712 Target_i386::Reloc_section
*
713 Target_i386::rel_dyn_section(Layout
* layout
)
715 if (this->rel_dyn_
== NULL
)
717 gold_assert(layout
!= NULL
);
718 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
719 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
720 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
721 ORDER_DYNAMIC_RELOCS
, false);
723 return this->rel_dyn_
;
726 // Create the PLT section. The ordinary .got section is an argument,
727 // since we need to refer to the start. We also create our own .got
728 // section just for PLT entries.
730 Output_data_plt_i386::Output_data_plt_i386(Symbol_table
* symtab
,
732 Output_data_space
* got_plt
)
733 : Output_section_data(4), tls_desc_rel_(NULL
), got_plt_(got_plt
), count_(0),
734 global_ifuncs_(), local_ifuncs_()
736 this->rel_
= new Reloc_section(false);
737 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
738 elfcpp::SHF_ALLOC
, this->rel_
,
739 ORDER_DYNAMIC_PLT_RELOCS
, false);
741 if (parameters
->doing_static_link())
743 // A statically linked executable will only have a .rel.plt
744 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
745 // symbols. The library will use these symbols to locate the
746 // IRELATIVE relocs at program startup time.
747 symtab
->define_in_output_data("__rel_iplt_start", NULL
,
748 Symbol_table::PREDEFINED
,
749 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
750 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
752 symtab
->define_in_output_data("__rel_iplt_end", NULL
,
753 Symbol_table::PREDEFINED
,
754 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
755 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
761 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
763 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
764 // linker, and so do we.
768 // Add an entry to the PLT.
771 Output_data_plt_i386::add_entry(Symbol
* gsym
)
773 gold_assert(!gsym
->has_plt_offset());
775 // Note that when setting the PLT offset we skip the initial
776 // reserved PLT entry.
777 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
781 section_offset_type got_offset
= this->got_plt_
->current_data_size();
783 // Every PLT entry needs a GOT entry which points back to the PLT
784 // entry (this will be changed by the dynamic linker, normally
785 // lazily when the function is called).
786 this->got_plt_
->set_current_data_size(got_offset
+ 4);
788 // Every PLT entry needs a reloc.
789 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
790 && gsym
->can_use_relative_reloc(false))
792 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_386_IRELATIVE
,
793 this->got_plt_
, got_offset
);
794 struct Global_ifunc gi
;
796 gi
.got_offset
= got_offset
;
797 this->global_ifuncs_
.push_back(gi
);
801 gsym
->set_needs_dynsym_entry();
802 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
806 // Note that we don't need to save the symbol. The contents of the
807 // PLT are independent of which symbols are used. The symbols only
808 // appear in the relocations.
811 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
815 Output_data_plt_i386::add_local_ifunc_entry(
816 Sized_relobj_file
<32, false>* relobj
,
817 unsigned int local_sym_index
)
819 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
822 section_offset_type got_offset
= this->got_plt_
->current_data_size();
824 // Every PLT entry needs a GOT entry which points back to the PLT
826 this->got_plt_
->set_current_data_size(got_offset
+ 4);
828 // Every PLT entry needs a reloc.
829 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
830 elfcpp::R_386_IRELATIVE
,
831 this->got_plt_
, got_offset
);
833 struct Local_ifunc li
;
835 li
.local_sym_index
= local_sym_index
;
836 li
.got_offset
= got_offset
;
837 this->local_ifuncs_
.push_back(li
);
842 // Return where the TLS_DESC relocations should go, creating it if
843 // necessary. These follow the JUMP_SLOT relocations.
845 Output_data_plt_i386::Reloc_section
*
846 Output_data_plt_i386::rel_tls_desc(Layout
* layout
)
848 if (this->tls_desc_rel_
== NULL
)
850 this->tls_desc_rel_
= new Reloc_section(false);
851 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
852 elfcpp::SHF_ALLOC
, this->tls_desc_rel_
,
853 ORDER_DYNAMIC_PLT_RELOCS
, false);
854 gold_assert(this->tls_desc_rel_
->output_section() ==
855 this->rel_
->output_section());
857 return this->tls_desc_rel_
;
860 // The first entry in the PLT for an executable.
862 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
864 0xff, 0x35, // pushl contents of memory address
865 0, 0, 0, 0, // replaced with address of .got + 4
866 0xff, 0x25, // jmp indirect
867 0, 0, 0, 0, // replaced with address of .got + 8
871 // The first entry in the PLT for a shared object.
873 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
875 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
876 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
880 // Subsequent entries in the PLT for an executable.
882 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
884 0xff, 0x25, // jmp indirect
885 0, 0, 0, 0, // replaced with address of symbol in .got
886 0x68, // pushl immediate
887 0, 0, 0, 0, // replaced with offset into relocation table
888 0xe9, // jmp relative
889 0, 0, 0, 0 // replaced with offset to start of .plt
892 // Subsequent entries in the PLT for a shared object.
894 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
896 0xff, 0xa3, // jmp *offset(%ebx)
897 0, 0, 0, 0, // replaced with offset of symbol in .got
898 0x68, // pushl immediate
899 0, 0, 0, 0, // replaced with offset into relocation table
900 0xe9, // jmp relative
901 0, 0, 0, 0 // replaced with offset to start of .plt
904 // Write out the PLT. This uses the hand-coded instructions above,
905 // and adjusts them as needed. This is all specified by the i386 ELF
906 // Processor Supplement.
909 Output_data_plt_i386::do_write(Output_file
* of
)
911 const off_t offset
= this->offset();
912 const section_size_type oview_size
=
913 convert_to_section_size_type(this->data_size());
914 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
916 const off_t got_file_offset
= this->got_plt_
->offset();
917 const section_size_type got_size
=
918 convert_to_section_size_type(this->got_plt_
->data_size());
919 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
922 unsigned char* pov
= oview
;
924 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
925 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
927 if (parameters
->options().output_is_position_independent())
928 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
931 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
932 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
933 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
935 pov
+= plt_entry_size
;
937 unsigned char* got_pov
= got_view
;
939 memset(got_pov
, 0, 12);
942 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
944 unsigned int plt_offset
= plt_entry_size
;
945 unsigned int plt_rel_offset
= 0;
946 unsigned int got_offset
= 12;
947 const unsigned int count
= this->count_
;
948 for (unsigned int i
= 0;
951 pov
+= plt_entry_size
,
953 plt_offset
+= plt_entry_size
,
954 plt_rel_offset
+= rel_size
,
957 // Set and adjust the PLT entry itself.
959 if (parameters
->options().output_is_position_independent())
961 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
962 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
966 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
967 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
972 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
973 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
974 - (plt_offset
+ plt_entry_size
));
976 // Set the entry in the GOT.
977 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
980 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
981 // the GOT to point to the actual symbol value, rather than point to
982 // the PLT entry. That will let the dynamic linker call the right
983 // function when resolving IRELATIVE relocations.
984 for (std::vector
<Global_ifunc
>::const_iterator p
=
985 this->global_ifuncs_
.begin();
986 p
!= this->global_ifuncs_
.end();
989 const Sized_symbol
<32>* ssym
=
990 static_cast<const Sized_symbol
<32>*>(p
->sym
);
991 elfcpp::Swap
<32, false>::writeval(got_view
+ p
->got_offset
,
995 for (std::vector
<Local_ifunc
>::const_iterator p
=
996 this->local_ifuncs_
.begin();
997 p
!= this->local_ifuncs_
.end();
1000 const Symbol_value
<32>* psymval
=
1001 p
->object
->local_symbol(p
->local_sym_index
);
1002 elfcpp::Swap
<32, false>::writeval(got_view
+ p
->got_offset
,
1003 psymval
->value(p
->object
, 0));
1006 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1007 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1009 of
->write_output_view(offset
, oview_size
, oview
);
1010 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1013 // Create the PLT section.
1016 Target_i386::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1018 if (this->plt_
== NULL
)
1020 // Create the GOT sections first.
1021 this->got_section(symtab
, layout
);
1023 this->plt_
= new Output_data_plt_i386(symtab
, layout
, this->got_plt_
);
1024 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1026 | elfcpp::SHF_EXECINSTR
),
1027 this->plt_
, ORDER_PLT
, false);
1029 // Make the sh_info field of .rel.plt point to .plt.
1030 Output_section
* rel_plt_os
= this->plt_
->rel_plt()->output_section();
1031 rel_plt_os
->set_info_section(this->plt_
->output_section());
1035 // Create a PLT entry for a global symbol.
1038 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
1040 if (gsym
->has_plt_offset())
1042 if (this->plt_
== NULL
)
1043 this->make_plt_section(symtab
, layout
);
1044 this->plt_
->add_entry(gsym
);
1047 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1050 Target_i386::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1051 Sized_relobj_file
<32, false>* relobj
,
1052 unsigned int local_sym_index
)
1054 if (relobj
->local_has_plt_offset(local_sym_index
))
1056 if (this->plt_
== NULL
)
1057 this->make_plt_section(symtab
, layout
);
1058 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1060 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1063 // Return the number of entries in the PLT.
1066 Target_i386::plt_entry_count() const
1068 if (this->plt_
== NULL
)
1070 return this->plt_
->entry_count();
1073 // Return the offset of the first non-reserved PLT entry.
1076 Target_i386::first_plt_entry_offset() const
1078 return Output_data_plt_i386::first_plt_entry_offset();
1081 // Return the size of each PLT entry.
1084 Target_i386::plt_entry_size() const
1086 return Output_data_plt_i386::get_plt_entry_size();
1089 // Get the section to use for TLS_DESC relocations.
1091 Target_i386::Reloc_section
*
1092 Target_i386::rel_tls_desc_section(Layout
* layout
) const
1094 return this->plt_section()->rel_tls_desc(layout
);
1097 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1100 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1102 if (this->tls_base_symbol_defined_
)
1105 Output_segment
* tls_segment
= layout
->tls_segment();
1106 if (tls_segment
!= NULL
)
1108 bool is_exec
= parameters
->options().output_is_executable();
1109 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1110 Symbol_table::PREDEFINED
,
1114 elfcpp::STV_HIDDEN
, 0,
1116 ? Symbol::SEGMENT_END
1117 : Symbol::SEGMENT_START
),
1120 this->tls_base_symbol_defined_
= true;
1123 // Create a GOT entry for the TLS module index.
1126 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1127 Sized_relobj_file
<32, false>* object
)
1129 if (this->got_mod_index_offset_
== -1U)
1131 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1132 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1133 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
1134 unsigned int got_offset
= got
->add_constant(0);
1135 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
1137 got
->add_constant(0);
1138 this->got_mod_index_offset_
= got_offset
;
1140 return this->got_mod_index_offset_
;
1143 // Optimize the TLS relocation type based on what we know about the
1144 // symbol. IS_FINAL is true if the final address of this symbol is
1145 // known at link time.
1147 tls::Tls_optimization
1148 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
1150 // If we are generating a shared library, then we can't do anything
1152 if (parameters
->options().shared())
1153 return tls::TLSOPT_NONE
;
1157 case elfcpp::R_386_TLS_GD
:
1158 case elfcpp::R_386_TLS_GOTDESC
:
1159 case elfcpp::R_386_TLS_DESC_CALL
:
1160 // These are General-Dynamic which permits fully general TLS
1161 // access. Since we know that we are generating an executable,
1162 // we can convert this to Initial-Exec. If we also know that
1163 // this is a local symbol, we can further switch to Local-Exec.
1165 return tls::TLSOPT_TO_LE
;
1166 return tls::TLSOPT_TO_IE
;
1168 case elfcpp::R_386_TLS_LDM
:
1169 // This is Local-Dynamic, which refers to a local symbol in the
1170 // dynamic TLS block. Since we know that we generating an
1171 // executable, we can switch to Local-Exec.
1172 return tls::TLSOPT_TO_LE
;
1174 case elfcpp::R_386_TLS_LDO_32
:
1175 // Another type of Local-Dynamic relocation.
1176 return tls::TLSOPT_TO_LE
;
1178 case elfcpp::R_386_TLS_IE
:
1179 case elfcpp::R_386_TLS_GOTIE
:
1180 case elfcpp::R_386_TLS_IE_32
:
1181 // These are Initial-Exec relocs which get the thread offset
1182 // from the GOT. If we know that we are linking against the
1183 // local symbol, we can switch to Local-Exec, which links the
1184 // thread offset into the instruction.
1186 return tls::TLSOPT_TO_LE
;
1187 return tls::TLSOPT_NONE
;
1189 case elfcpp::R_386_TLS_LE
:
1190 case elfcpp::R_386_TLS_LE_32
:
1191 // When we already have Local-Exec, there is nothing further we
1193 return tls::TLSOPT_NONE
;
1200 // Get the Reference_flags for a particular relocation.
1203 Target_i386::Scan::get_reference_flags(unsigned int r_type
)
1207 case elfcpp::R_386_NONE
:
1208 case elfcpp::R_386_GNU_VTINHERIT
:
1209 case elfcpp::R_386_GNU_VTENTRY
:
1210 case elfcpp::R_386_GOTPC
:
1211 // No symbol reference.
1214 case elfcpp::R_386_32
:
1215 case elfcpp::R_386_16
:
1216 case elfcpp::R_386_8
:
1217 return Symbol::ABSOLUTE_REF
;
1219 case elfcpp::R_386_PC32
:
1220 case elfcpp::R_386_PC16
:
1221 case elfcpp::R_386_PC8
:
1222 case elfcpp::R_386_GOTOFF
:
1223 return Symbol::RELATIVE_REF
;
1225 case elfcpp::R_386_PLT32
:
1226 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1228 case elfcpp::R_386_GOT32
:
1230 return Symbol::ABSOLUTE_REF
;
1232 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1233 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1234 case elfcpp::R_386_TLS_DESC_CALL
:
1235 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1236 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1237 case elfcpp::R_386_TLS_IE
: // Initial-exec
1238 case elfcpp::R_386_TLS_IE_32
:
1239 case elfcpp::R_386_TLS_GOTIE
:
1240 case elfcpp::R_386_TLS_LE
: // Local-exec
1241 case elfcpp::R_386_TLS_LE_32
:
1242 return Symbol::TLS_REF
;
1244 case elfcpp::R_386_COPY
:
1245 case elfcpp::R_386_GLOB_DAT
:
1246 case elfcpp::R_386_JUMP_SLOT
:
1247 case elfcpp::R_386_RELATIVE
:
1248 case elfcpp::R_386_IRELATIVE
:
1249 case elfcpp::R_386_TLS_TPOFF
:
1250 case elfcpp::R_386_TLS_DTPMOD32
:
1251 case elfcpp::R_386_TLS_DTPOFF32
:
1252 case elfcpp::R_386_TLS_TPOFF32
:
1253 case elfcpp::R_386_TLS_DESC
:
1254 case elfcpp::R_386_32PLT
:
1255 case elfcpp::R_386_TLS_GD_32
:
1256 case elfcpp::R_386_TLS_GD_PUSH
:
1257 case elfcpp::R_386_TLS_GD_CALL
:
1258 case elfcpp::R_386_TLS_GD_POP
:
1259 case elfcpp::R_386_TLS_LDM_32
:
1260 case elfcpp::R_386_TLS_LDM_PUSH
:
1261 case elfcpp::R_386_TLS_LDM_CALL
:
1262 case elfcpp::R_386_TLS_LDM_POP
:
1263 case elfcpp::R_386_USED_BY_INTEL_200
:
1265 // Not expected. We will give an error later.
1270 // Report an unsupported relocation against a local symbol.
1273 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file
<32, false>* object
,
1274 unsigned int r_type
)
1276 gold_error(_("%s: unsupported reloc %u against local symbol"),
1277 object
->name().c_str(), r_type
);
1280 // Return whether we need to make a PLT entry for a relocation of a
1281 // given type against a STT_GNU_IFUNC symbol.
1284 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1285 Sized_relobj_file
<32, false>* object
,
1286 unsigned int r_type
)
1288 int flags
= Scan::get_reference_flags(r_type
);
1289 if (flags
& Symbol::TLS_REF
)
1290 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1291 object
->name().c_str(), r_type
);
1295 // Scan a relocation for a local symbol.
1298 Target_i386::Scan::local(Symbol_table
* symtab
,
1300 Target_i386
* target
,
1301 Sized_relobj_file
<32, false>* object
,
1302 unsigned int data_shndx
,
1303 Output_section
* output_section
,
1304 const elfcpp::Rel
<32, false>& reloc
,
1305 unsigned int r_type
,
1306 const elfcpp::Sym
<32, false>& lsym
)
1308 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1309 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1310 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1312 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1313 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1318 case elfcpp::R_386_NONE
:
1319 case elfcpp::R_386_GNU_VTINHERIT
:
1320 case elfcpp::R_386_GNU_VTENTRY
:
1323 case elfcpp::R_386_32
:
1324 // If building a shared library (or a position-independent
1325 // executable), we need to create a dynamic relocation for
1326 // this location. The relocation applied at link time will
1327 // apply the link-time value, so we flag the location with
1328 // an R_386_RELATIVE relocation so the dynamic loader can
1329 // relocate it easily.
1330 if (parameters
->options().output_is_position_independent())
1332 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1333 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1334 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
1335 output_section
, data_shndx
,
1336 reloc
.get_r_offset());
1340 case elfcpp::R_386_16
:
1341 case elfcpp::R_386_8
:
1342 // If building a shared library (or a position-independent
1343 // executable), we need to create a dynamic relocation for
1344 // this location. Because the addend needs to remain in the
1345 // data section, we need to be careful not to apply this
1346 // relocation statically.
1347 if (parameters
->options().output_is_position_independent())
1349 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1350 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1351 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1352 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1353 data_shndx
, reloc
.get_r_offset());
1356 gold_assert(lsym
.get_st_value() == 0);
1357 unsigned int shndx
= lsym
.get_st_shndx();
1359 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1362 object
->error(_("section symbol %u has bad shndx %u"),
1365 rel_dyn
->add_local_section(object
, shndx
,
1366 r_type
, output_section
,
1367 data_shndx
, reloc
.get_r_offset());
1372 case elfcpp::R_386_PC32
:
1373 case elfcpp::R_386_PC16
:
1374 case elfcpp::R_386_PC8
:
1377 case elfcpp::R_386_PLT32
:
1378 // Since we know this is a local symbol, we can handle this as a
1382 case elfcpp::R_386_GOTOFF
:
1383 case elfcpp::R_386_GOTPC
:
1384 // We need a GOT section.
1385 target
->got_section(symtab
, layout
);
1388 case elfcpp::R_386_GOT32
:
1390 // The symbol requires a GOT entry.
1391 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1392 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1394 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1395 // lets function pointers compare correctly with shared
1396 // libraries. Otherwise we would need an IRELATIVE reloc.
1398 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1399 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1401 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1404 // If we are generating a shared object, we need to add a
1405 // dynamic RELATIVE relocation for this symbol's GOT entry.
1406 if (parameters
->options().output_is_position_independent())
1408 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1409 unsigned int got_offset
=
1410 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1411 rel_dyn
->add_local_relative(object
, r_sym
,
1412 elfcpp::R_386_RELATIVE
,
1419 // These are relocations which should only be seen by the
1420 // dynamic linker, and should never be seen here.
1421 case elfcpp::R_386_COPY
:
1422 case elfcpp::R_386_GLOB_DAT
:
1423 case elfcpp::R_386_JUMP_SLOT
:
1424 case elfcpp::R_386_RELATIVE
:
1425 case elfcpp::R_386_IRELATIVE
:
1426 case elfcpp::R_386_TLS_TPOFF
:
1427 case elfcpp::R_386_TLS_DTPMOD32
:
1428 case elfcpp::R_386_TLS_DTPOFF32
:
1429 case elfcpp::R_386_TLS_TPOFF32
:
1430 case elfcpp::R_386_TLS_DESC
:
1431 gold_error(_("%s: unexpected reloc %u in object file"),
1432 object
->name().c_str(), r_type
);
1435 // These are initial TLS relocs, which are expected when
1437 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1438 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1439 case elfcpp::R_386_TLS_DESC_CALL
:
1440 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1441 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1442 case elfcpp::R_386_TLS_IE
: // Initial-exec
1443 case elfcpp::R_386_TLS_IE_32
:
1444 case elfcpp::R_386_TLS_GOTIE
:
1445 case elfcpp::R_386_TLS_LE
: // Local-exec
1446 case elfcpp::R_386_TLS_LE_32
:
1448 bool output_is_shared
= parameters
->options().shared();
1449 const tls::Tls_optimization optimized_type
1450 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1453 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1454 if (optimized_type
== tls::TLSOPT_NONE
)
1456 // Create a pair of GOT entries for the module index and
1457 // dtv-relative offset.
1458 Output_data_got
<32, false>* got
1459 = target
->got_section(symtab
, layout
);
1460 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1461 unsigned int shndx
= lsym
.get_st_shndx();
1463 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1465 object
->error(_("local symbol %u has bad shndx %u"),
1468 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1470 target
->rel_dyn_section(layout
),
1471 elfcpp::R_386_TLS_DTPMOD32
, 0);
1473 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1474 unsupported_reloc_local(object
, r_type
);
1477 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1478 target
->define_tls_base_symbol(symtab
, layout
);
1479 if (optimized_type
== tls::TLSOPT_NONE
)
1481 // Create a double GOT entry with an R_386_TLS_DESC
1482 // reloc. The R_386_TLS_DESC reloc is resolved
1483 // lazily, so the GOT entry needs to be in an area in
1484 // .got.plt, not .got. Call got_section to make sure
1485 // the section has been created.
1486 target
->got_section(symtab
, layout
);
1487 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1488 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1489 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1491 unsigned int got_offset
= got
->add_constant(0);
1492 // The local symbol value is stored in the second
1494 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_DESC
);
1495 // That set the GOT offset of the local symbol to
1496 // point to the second entry, but we want it to
1497 // point to the first.
1498 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1500 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1501 rt
->add_absolute(elfcpp::R_386_TLS_DESC
, got
, got_offset
);
1504 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1505 unsupported_reloc_local(object
, r_type
);
1508 case elfcpp::R_386_TLS_DESC_CALL
:
1511 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1512 if (optimized_type
== tls::TLSOPT_NONE
)
1514 // Create a GOT entry for the module index.
1515 target
->got_mod_index_entry(symtab
, layout
, object
);
1517 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1518 unsupported_reloc_local(object
, r_type
);
1521 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1524 case elfcpp::R_386_TLS_IE
: // Initial-exec
1525 case elfcpp::R_386_TLS_IE_32
:
1526 case elfcpp::R_386_TLS_GOTIE
:
1527 layout
->set_has_static_tls();
1528 if (optimized_type
== tls::TLSOPT_NONE
)
1530 // For the R_386_TLS_IE relocation, we need to create a
1531 // dynamic relocation when building a shared library.
1532 if (r_type
== elfcpp::R_386_TLS_IE
1533 && parameters
->options().shared())
1535 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1537 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1538 rel_dyn
->add_local_relative(object
, r_sym
,
1539 elfcpp::R_386_RELATIVE
,
1540 output_section
, data_shndx
,
1541 reloc
.get_r_offset());
1543 // Create a GOT entry for the tp-relative offset.
1544 Output_data_got
<32, false>* got
1545 = target
->got_section(symtab
, layout
);
1546 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1547 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1548 ? elfcpp::R_386_TLS_TPOFF32
1549 : elfcpp::R_386_TLS_TPOFF
);
1550 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1551 ? GOT_TYPE_TLS_OFFSET
1552 : GOT_TYPE_TLS_NOFFSET
);
1553 got
->add_local_with_rel(object
, r_sym
, got_type
,
1554 target
->rel_dyn_section(layout
),
1557 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1558 unsupported_reloc_local(object
, r_type
);
1561 case elfcpp::R_386_TLS_LE
: // Local-exec
1562 case elfcpp::R_386_TLS_LE_32
:
1563 layout
->set_has_static_tls();
1564 if (output_is_shared
)
1566 // We need to create a dynamic relocation.
1567 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1568 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1569 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1570 ? elfcpp::R_386_TLS_TPOFF32
1571 : elfcpp::R_386_TLS_TPOFF
);
1572 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1573 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1574 data_shndx
, reloc
.get_r_offset());
1584 case elfcpp::R_386_32PLT
:
1585 case elfcpp::R_386_TLS_GD_32
:
1586 case elfcpp::R_386_TLS_GD_PUSH
:
1587 case elfcpp::R_386_TLS_GD_CALL
:
1588 case elfcpp::R_386_TLS_GD_POP
:
1589 case elfcpp::R_386_TLS_LDM_32
:
1590 case elfcpp::R_386_TLS_LDM_PUSH
:
1591 case elfcpp::R_386_TLS_LDM_CALL
:
1592 case elfcpp::R_386_TLS_LDM_POP
:
1593 case elfcpp::R_386_USED_BY_INTEL_200
:
1595 unsupported_reloc_local(object
, r_type
);
1600 // Report an unsupported relocation against a global symbol.
1603 Target_i386::Scan::unsupported_reloc_global(
1604 Sized_relobj_file
<32, false>* object
,
1605 unsigned int r_type
,
1608 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1609 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1613 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1617 case elfcpp::R_386_32
:
1618 case elfcpp::R_386_16
:
1619 case elfcpp::R_386_8
:
1620 case elfcpp::R_386_GOTOFF
:
1621 case elfcpp::R_386_GOT32
:
1632 Target_i386::Scan::local_reloc_may_be_function_pointer(
1636 Sized_relobj_file
<32, false>* ,
1639 const elfcpp::Rel
<32, false>& ,
1640 unsigned int r_type
,
1641 const elfcpp::Sym
<32, false>&)
1643 return possible_function_pointer_reloc(r_type
);
1647 Target_i386::Scan::global_reloc_may_be_function_pointer(
1651 Sized_relobj_file
<32, false>* ,
1654 const elfcpp::Rel
<32, false>& ,
1655 unsigned int r_type
,
1658 return possible_function_pointer_reloc(r_type
);
1661 // Scan a relocation for a global symbol.
1664 Target_i386::Scan::global(Symbol_table
* symtab
,
1666 Target_i386
* target
,
1667 Sized_relobj_file
<32, false>* object
,
1668 unsigned int data_shndx
,
1669 Output_section
* output_section
,
1670 const elfcpp::Rel
<32, false>& reloc
,
1671 unsigned int r_type
,
1674 // A STT_GNU_IFUNC symbol may require a PLT entry.
1675 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1676 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1677 target
->make_plt_entry(symtab
, layout
, gsym
);
1681 case elfcpp::R_386_NONE
:
1682 case elfcpp::R_386_GNU_VTINHERIT
:
1683 case elfcpp::R_386_GNU_VTENTRY
:
1686 case elfcpp::R_386_32
:
1687 case elfcpp::R_386_16
:
1688 case elfcpp::R_386_8
:
1690 // Make a PLT entry if necessary.
1691 if (gsym
->needs_plt_entry())
1693 target
->make_plt_entry(symtab
, layout
, gsym
);
1694 // Since this is not a PC-relative relocation, we may be
1695 // taking the address of a function. In that case we need to
1696 // set the entry in the dynamic symbol table to the address of
1698 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1699 gsym
->set_needs_dynsym_value();
1701 // Make a dynamic relocation if necessary.
1702 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1704 if (gsym
->may_need_copy_reloc())
1706 target
->copy_reloc(symtab
, layout
, object
,
1707 data_shndx
, output_section
, gsym
, reloc
);
1709 else if (r_type
== elfcpp::R_386_32
1710 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1711 && gsym
->can_use_relative_reloc(false)
1712 && !gsym
->is_from_dynobj()
1713 && !gsym
->is_undefined()
1714 && !gsym
->is_preemptible())
1716 // Use an IRELATIVE reloc for a locally defined
1717 // STT_GNU_IFUNC symbol. This makes a function
1718 // address in a PIE executable match the address in a
1719 // shared library that it links against.
1720 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1721 rel_dyn
->add_symbolless_global_addend(gsym
,
1722 elfcpp::R_386_IRELATIVE
,
1725 reloc
.get_r_offset());
1727 else if (r_type
== elfcpp::R_386_32
1728 && gsym
->can_use_relative_reloc(false))
1730 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1731 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1732 output_section
, object
,
1733 data_shndx
, reloc
.get_r_offset());
1737 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1738 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1739 data_shndx
, reloc
.get_r_offset());
1745 case elfcpp::R_386_PC32
:
1746 case elfcpp::R_386_PC16
:
1747 case elfcpp::R_386_PC8
:
1749 // Make a PLT entry if necessary.
1750 if (gsym
->needs_plt_entry())
1752 // These relocations are used for function calls only in
1753 // non-PIC code. For a 32-bit relocation in a shared library,
1754 // we'll need a text relocation anyway, so we can skip the
1755 // PLT entry and let the dynamic linker bind the call directly
1756 // to the target. For smaller relocations, we should use a
1757 // PLT entry to ensure that the call can reach.
1758 if (!parameters
->options().shared()
1759 || r_type
!= elfcpp::R_386_PC32
)
1760 target
->make_plt_entry(symtab
, layout
, gsym
);
1762 // Make a dynamic relocation if necessary.
1763 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1765 if (gsym
->may_need_copy_reloc())
1767 target
->copy_reloc(symtab
, layout
, object
,
1768 data_shndx
, output_section
, gsym
, reloc
);
1772 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1773 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1774 data_shndx
, reloc
.get_r_offset());
1780 case elfcpp::R_386_GOT32
:
1782 // The symbol requires a GOT entry.
1783 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1784 if (gsym
->final_value_is_known())
1786 // For a STT_GNU_IFUNC symbol we want the PLT address.
1787 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1788 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1790 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1794 // If this symbol is not fully resolved, we need to add a
1795 // GOT entry with a dynamic relocation.
1796 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1797 if (gsym
->is_from_dynobj()
1798 || gsym
->is_undefined()
1799 || gsym
->is_preemptible()
1800 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
1801 && parameters
->options().output_is_position_independent()))
1802 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1803 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1806 // For a STT_GNU_IFUNC symbol we want to write the PLT
1807 // offset into the GOT, so that function pointer
1808 // comparisons work correctly.
1810 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
1811 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1814 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1815 // Tell the dynamic linker to use the PLT address
1816 // when resolving relocations.
1817 if (gsym
->is_from_dynobj()
1818 && !parameters
->options().shared())
1819 gsym
->set_needs_dynsym_value();
1823 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1824 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1832 case elfcpp::R_386_PLT32
:
1833 // If the symbol is fully resolved, this is just a PC32 reloc.
1834 // Otherwise we need a PLT entry.
1835 if (gsym
->final_value_is_known())
1837 // If building a shared library, we can also skip the PLT entry
1838 // if the symbol is defined in the output file and is protected
1840 if (gsym
->is_defined()
1841 && !gsym
->is_from_dynobj()
1842 && !gsym
->is_preemptible())
1844 target
->make_plt_entry(symtab
, layout
, gsym
);
1847 case elfcpp::R_386_GOTOFF
:
1848 case elfcpp::R_386_GOTPC
:
1849 // We need a GOT section.
1850 target
->got_section(symtab
, layout
);
1853 // These are relocations which should only be seen by the
1854 // dynamic linker, and should never be seen here.
1855 case elfcpp::R_386_COPY
:
1856 case elfcpp::R_386_GLOB_DAT
:
1857 case elfcpp::R_386_JUMP_SLOT
:
1858 case elfcpp::R_386_RELATIVE
:
1859 case elfcpp::R_386_IRELATIVE
:
1860 case elfcpp::R_386_TLS_TPOFF
:
1861 case elfcpp::R_386_TLS_DTPMOD32
:
1862 case elfcpp::R_386_TLS_DTPOFF32
:
1863 case elfcpp::R_386_TLS_TPOFF32
:
1864 case elfcpp::R_386_TLS_DESC
:
1865 gold_error(_("%s: unexpected reloc %u in object file"),
1866 object
->name().c_str(), r_type
);
1869 // These are initial tls relocs, which are expected when
1871 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1872 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1873 case elfcpp::R_386_TLS_DESC_CALL
:
1874 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1875 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1876 case elfcpp::R_386_TLS_IE
: // Initial-exec
1877 case elfcpp::R_386_TLS_IE_32
:
1878 case elfcpp::R_386_TLS_GOTIE
:
1879 case elfcpp::R_386_TLS_LE
: // Local-exec
1880 case elfcpp::R_386_TLS_LE_32
:
1882 const bool is_final
= gsym
->final_value_is_known();
1883 const tls::Tls_optimization optimized_type
1884 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1887 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1888 if (optimized_type
== tls::TLSOPT_NONE
)
1890 // Create a pair of GOT entries for the module index and
1891 // dtv-relative offset.
1892 Output_data_got
<32, false>* got
1893 = target
->got_section(symtab
, layout
);
1894 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1895 target
->rel_dyn_section(layout
),
1896 elfcpp::R_386_TLS_DTPMOD32
,
1897 elfcpp::R_386_TLS_DTPOFF32
);
1899 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1901 // Create a GOT entry for the tp-relative offset.
1902 Output_data_got
<32, false>* got
1903 = target
->got_section(symtab
, layout
);
1904 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1905 target
->rel_dyn_section(layout
),
1906 elfcpp::R_386_TLS_TPOFF
);
1908 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1909 unsupported_reloc_global(object
, r_type
, gsym
);
1912 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1913 target
->define_tls_base_symbol(symtab
, layout
);
1914 if (optimized_type
== tls::TLSOPT_NONE
)
1916 // Create a double GOT entry with an R_386_TLS_DESC
1917 // reloc. The R_386_TLS_DESC reloc is resolved
1918 // lazily, so the GOT entry needs to be in an area in
1919 // .got.plt, not .got. Call got_section to make sure
1920 // the section has been created.
1921 target
->got_section(symtab
, layout
);
1922 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1923 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1924 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
1925 elfcpp::R_386_TLS_DESC
, 0);
1927 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1929 // Create a GOT entry for the tp-relative offset.
1930 Output_data_got
<32, false>* got
1931 = target
->got_section(symtab
, layout
);
1932 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1933 target
->rel_dyn_section(layout
),
1934 elfcpp::R_386_TLS_TPOFF
);
1936 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1937 unsupported_reloc_global(object
, r_type
, gsym
);
1940 case elfcpp::R_386_TLS_DESC_CALL
:
1943 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1944 if (optimized_type
== tls::TLSOPT_NONE
)
1946 // Create a GOT entry for the module index.
1947 target
->got_mod_index_entry(symtab
, layout
, object
);
1949 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1950 unsupported_reloc_global(object
, r_type
, gsym
);
1953 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1956 case elfcpp::R_386_TLS_IE
: // Initial-exec
1957 case elfcpp::R_386_TLS_IE_32
:
1958 case elfcpp::R_386_TLS_GOTIE
:
1959 layout
->set_has_static_tls();
1960 if (optimized_type
== tls::TLSOPT_NONE
)
1962 // For the R_386_TLS_IE relocation, we need to create a
1963 // dynamic relocation when building a shared library.
1964 if (r_type
== elfcpp::R_386_TLS_IE
1965 && parameters
->options().shared())
1967 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1968 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1969 output_section
, object
,
1971 reloc
.get_r_offset());
1973 // Create a GOT entry for the tp-relative offset.
1974 Output_data_got
<32, false>* got
1975 = target
->got_section(symtab
, layout
);
1976 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1977 ? elfcpp::R_386_TLS_TPOFF32
1978 : elfcpp::R_386_TLS_TPOFF
);
1979 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1980 ? GOT_TYPE_TLS_OFFSET
1981 : GOT_TYPE_TLS_NOFFSET
);
1982 got
->add_global_with_rel(gsym
, got_type
,
1983 target
->rel_dyn_section(layout
),
1986 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1987 unsupported_reloc_global(object
, r_type
, gsym
);
1990 case elfcpp::R_386_TLS_LE
: // Local-exec
1991 case elfcpp::R_386_TLS_LE_32
:
1992 layout
->set_has_static_tls();
1993 if (parameters
->options().shared())
1995 // We need to create a dynamic relocation.
1996 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1997 ? elfcpp::R_386_TLS_TPOFF32
1998 : elfcpp::R_386_TLS_TPOFF
);
1999 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2000 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
2001 data_shndx
, reloc
.get_r_offset());
2011 case elfcpp::R_386_32PLT
:
2012 case elfcpp::R_386_TLS_GD_32
:
2013 case elfcpp::R_386_TLS_GD_PUSH
:
2014 case elfcpp::R_386_TLS_GD_CALL
:
2015 case elfcpp::R_386_TLS_GD_POP
:
2016 case elfcpp::R_386_TLS_LDM_32
:
2017 case elfcpp::R_386_TLS_LDM_PUSH
:
2018 case elfcpp::R_386_TLS_LDM_CALL
:
2019 case elfcpp::R_386_TLS_LDM_POP
:
2020 case elfcpp::R_386_USED_BY_INTEL_200
:
2022 unsupported_reloc_global(object
, r_type
, gsym
);
2027 // Process relocations for gc.
2030 Target_i386::gc_process_relocs(Symbol_table
* symtab
,
2032 Sized_relobj_file
<32, false>* object
,
2033 unsigned int data_shndx
,
2035 const unsigned char* prelocs
,
2037 Output_section
* output_section
,
2038 bool needs_special_offset_handling
,
2039 size_t local_symbol_count
,
2040 const unsigned char* plocal_symbols
)
2042 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2044 Target_i386::Relocatable_size_for_reloc
>(
2053 needs_special_offset_handling
,
2058 // Scan relocations for a section.
2061 Target_i386::scan_relocs(Symbol_table
* symtab
,
2063 Sized_relobj_file
<32, false>* object
,
2064 unsigned int data_shndx
,
2065 unsigned int sh_type
,
2066 const unsigned char* prelocs
,
2068 Output_section
* output_section
,
2069 bool needs_special_offset_handling
,
2070 size_t local_symbol_count
,
2071 const unsigned char* plocal_symbols
)
2073 if (sh_type
== elfcpp::SHT_RELA
)
2075 gold_error(_("%s: unsupported RELA reloc section"),
2076 object
->name().c_str());
2080 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2090 needs_special_offset_handling
,
2095 // Finalize the sections.
2098 Target_i386::do_finalize_sections(
2100 const Input_objects
*,
2101 Symbol_table
* symtab
)
2103 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2105 : this->plt_
->rel_plt());
2106 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
2107 this->rel_dyn_
, true, false);
2109 // Emit any relocs we saved in an attempt to avoid generating COPY
2111 if (this->copy_relocs_
.any_saved_relocs())
2112 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
2114 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2115 // the .got.plt section.
2116 Symbol
* sym
= this->global_offset_table_
;
2119 uint32_t data_size
= this->got_plt_
->current_data_size();
2120 symtab
->get_sized_symbol
<32>(sym
)->set_symsize(data_size
);
2124 // Return whether a direct absolute static relocation needs to be applied.
2125 // In cases where Scan::local() or Scan::global() has created
2126 // a dynamic relocation other than R_386_RELATIVE, the addend
2127 // of the relocation is carried in the data, and we must not
2128 // apply the static relocation.
2131 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2132 unsigned int r_type
,
2134 Output_section
* output_section
)
2136 // If the output section is not allocated, then we didn't call
2137 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2139 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
2142 int ref_flags
= Scan::get_reference_flags(r_type
);
2144 // For local symbols, we will have created a non-RELATIVE dynamic
2145 // relocation only if (a) the output is position independent,
2146 // (b) the relocation is absolute (not pc- or segment-relative), and
2147 // (c) the relocation is not 32 bits wide.
2149 return !(parameters
->options().output_is_position_independent()
2150 && (ref_flags
& Symbol::ABSOLUTE_REF
)
2153 // For global symbols, we use the same helper routines used in the
2154 // scan pass. If we did not create a dynamic relocation, or if we
2155 // created a RELATIVE dynamic relocation, we should apply the static
2157 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
2158 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
2159 && gsym
->can_use_relative_reloc(ref_flags
2160 & Symbol::FUNCTION_CALL
);
2161 return !has_dyn
|| is_rel
;
2164 // Perform a relocation.
2167 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
2168 Target_i386
* target
,
2169 Output_section
* output_section
,
2171 const elfcpp::Rel
<32, false>& rel
,
2172 unsigned int r_type
,
2173 const Sized_symbol
<32>* gsym
,
2174 const Symbol_value
<32>* psymval
,
2175 unsigned char* view
,
2176 elfcpp::Elf_types
<32>::Elf_Addr address
,
2177 section_size_type view_size
)
2179 if (this->skip_call_tls_get_addr_
)
2181 if ((r_type
!= elfcpp::R_386_PLT32
2182 && r_type
!= elfcpp::R_386_PC32
)
2184 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
2185 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2186 _("missing expected TLS relocation"));
2189 this->skip_call_tls_get_addr_
= false;
2194 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2196 // Pick the value to use for symbols defined in shared objects.
2197 Symbol_value
<32> symval
;
2199 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2200 && r_type
== elfcpp::R_386_32
2201 && gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
))
2202 && gsym
->can_use_relative_reloc(false)
2203 && !gsym
->is_from_dynobj()
2204 && !gsym
->is_undefined()
2205 && !gsym
->is_preemptible())
2207 // In this case we are generating a R_386_IRELATIVE reloc. We
2208 // want to use the real value of the symbol, not the PLT offset.
2210 else if (gsym
!= NULL
2211 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2213 symval
.set_output_value(target
->plt_section()->address()
2214 + gsym
->plt_offset());
2217 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2219 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2220 if (object
->local_has_plt_offset(r_sym
))
2222 symval
.set_output_value(target
->plt_section()->address()
2223 + object
->local_plt_offset(r_sym
));
2228 // Get the GOT offset if needed.
2229 // The GOT pointer points to the end of the GOT section.
2230 // We need to subtract the size of the GOT section to get
2231 // the actual offset to use in the relocation.
2232 bool have_got_offset
= false;
2233 unsigned int got_offset
= 0;
2236 case elfcpp::R_386_GOT32
:
2239 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2240 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
2241 - target
->got_size());
2245 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2246 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2247 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2248 - target
->got_size());
2250 have_got_offset
= true;
2259 case elfcpp::R_386_NONE
:
2260 case elfcpp::R_386_GNU_VTINHERIT
:
2261 case elfcpp::R_386_GNU_VTENTRY
:
2264 case elfcpp::R_386_32
:
2265 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2266 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
2269 case elfcpp::R_386_PC32
:
2270 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2271 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2274 case elfcpp::R_386_16
:
2275 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2276 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
2279 case elfcpp::R_386_PC16
:
2280 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2281 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
2284 case elfcpp::R_386_8
:
2285 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2286 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
2289 case elfcpp::R_386_PC8
:
2290 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2291 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
2294 case elfcpp::R_386_PLT32
:
2295 gold_assert(gsym
== NULL
2296 || gsym
->has_plt_offset()
2297 || gsym
->final_value_is_known()
2298 || (gsym
->is_defined()
2299 && !gsym
->is_from_dynobj()
2300 && !gsym
->is_preemptible()));
2301 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2304 case elfcpp::R_386_GOT32
:
2305 gold_assert(have_got_offset
);
2306 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2309 case elfcpp::R_386_GOTOFF
:
2311 elfcpp::Elf_types
<32>::Elf_Addr value
;
2312 value
= (psymval
->value(object
, 0)
2313 - target
->got_plt_section()->address());
2314 Relocate_functions
<32, false>::rel32(view
, value
);
2318 case elfcpp::R_386_GOTPC
:
2320 elfcpp::Elf_types
<32>::Elf_Addr value
;
2321 value
= target
->got_plt_section()->address();
2322 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
2326 case elfcpp::R_386_COPY
:
2327 case elfcpp::R_386_GLOB_DAT
:
2328 case elfcpp::R_386_JUMP_SLOT
:
2329 case elfcpp::R_386_RELATIVE
:
2330 case elfcpp::R_386_IRELATIVE
:
2331 // These are outstanding tls relocs, which are unexpected when
2333 case elfcpp::R_386_TLS_TPOFF
:
2334 case elfcpp::R_386_TLS_DTPMOD32
:
2335 case elfcpp::R_386_TLS_DTPOFF32
:
2336 case elfcpp::R_386_TLS_TPOFF32
:
2337 case elfcpp::R_386_TLS_DESC
:
2338 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2339 _("unexpected reloc %u in object file"),
2343 // These are initial tls relocs, which are expected when
2345 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2346 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2347 case elfcpp::R_386_TLS_DESC_CALL
:
2348 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2349 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2350 case elfcpp::R_386_TLS_IE
: // Initial-exec
2351 case elfcpp::R_386_TLS_IE_32
:
2352 case elfcpp::R_386_TLS_GOTIE
:
2353 case elfcpp::R_386_TLS_LE
: // Local-exec
2354 case elfcpp::R_386_TLS_LE_32
:
2355 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
2356 view
, address
, view_size
);
2359 case elfcpp::R_386_32PLT
:
2360 case elfcpp::R_386_TLS_GD_32
:
2361 case elfcpp::R_386_TLS_GD_PUSH
:
2362 case elfcpp::R_386_TLS_GD_CALL
:
2363 case elfcpp::R_386_TLS_GD_POP
:
2364 case elfcpp::R_386_TLS_LDM_32
:
2365 case elfcpp::R_386_TLS_LDM_PUSH
:
2366 case elfcpp::R_386_TLS_LDM_CALL
:
2367 case elfcpp::R_386_TLS_LDM_POP
:
2368 case elfcpp::R_386_USED_BY_INTEL_200
:
2370 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2371 _("unsupported reloc %u"),
2379 // Perform a TLS relocation.
2382 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
2383 Target_i386
* target
,
2385 const elfcpp::Rel
<32, false>& rel
,
2386 unsigned int r_type
,
2387 const Sized_symbol
<32>* gsym
,
2388 const Symbol_value
<32>* psymval
,
2389 unsigned char* view
,
2390 elfcpp::Elf_types
<32>::Elf_Addr
,
2391 section_size_type view_size
)
2393 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2395 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2397 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
2399 const bool is_final
= (gsym
== NULL
2400 ? !parameters
->options().shared()
2401 : gsym
->final_value_is_known());
2402 const tls::Tls_optimization optimized_type
2403 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2406 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2407 if (optimized_type
== tls::TLSOPT_TO_LE
)
2409 gold_assert(tls_segment
!= NULL
);
2410 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2411 rel
, r_type
, value
, view
,
2417 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2418 ? GOT_TYPE_TLS_NOFFSET
2419 : GOT_TYPE_TLS_PAIR
);
2420 unsigned int got_offset
;
2423 gold_assert(gsym
->has_got_offset(got_type
));
2424 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2428 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2429 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2430 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2431 - target
->got_size());
2433 if (optimized_type
== tls::TLSOPT_TO_IE
)
2435 gold_assert(tls_segment
!= NULL
);
2436 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2437 got_offset
, view
, view_size
);
2440 else if (optimized_type
== tls::TLSOPT_NONE
)
2442 // Relocate the field with the offset of the pair of GOT
2444 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2448 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2449 _("unsupported reloc %u"),
2453 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2454 case elfcpp::R_386_TLS_DESC_CALL
:
2455 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2456 if (optimized_type
== tls::TLSOPT_TO_LE
)
2458 gold_assert(tls_segment
!= NULL
);
2459 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2460 rel
, r_type
, value
, view
,
2466 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2467 ? GOT_TYPE_TLS_NOFFSET
2468 : GOT_TYPE_TLS_DESC
);
2469 unsigned int got_offset
= 0;
2470 if (r_type
== elfcpp::R_386_TLS_GOTDESC
2471 && optimized_type
== tls::TLSOPT_NONE
)
2473 // We created GOT entries in the .got.tlsdesc portion of
2474 // the .got.plt section, but the offset stored in the
2475 // symbol is the offset within .got.tlsdesc.
2476 got_offset
= (target
->got_size()
2477 + target
->got_plt_section()->data_size());
2481 gold_assert(gsym
->has_got_offset(got_type
));
2482 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2486 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2487 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2488 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2489 - target
->got_size());
2491 if (optimized_type
== tls::TLSOPT_TO_IE
)
2493 gold_assert(tls_segment
!= NULL
);
2494 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2495 got_offset
, view
, view_size
);
2498 else if (optimized_type
== tls::TLSOPT_NONE
)
2500 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2502 // Relocate the field with the offset of the pair of GOT
2504 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2509 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2510 _("unsupported reloc %u"),
2514 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2515 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
2517 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2518 _("both SUN and GNU model "
2519 "TLS relocations"));
2522 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2523 if (optimized_type
== tls::TLSOPT_TO_LE
)
2525 gold_assert(tls_segment
!= NULL
);
2526 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2527 value
, view
, view_size
);
2530 else if (optimized_type
== tls::TLSOPT_NONE
)
2532 // Relocate the field with the offset of the GOT entry for
2533 // the module index.
2534 unsigned int got_offset
;
2535 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2536 - target
->got_size());
2537 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2540 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2541 _("unsupported reloc %u"),
2545 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2546 if (optimized_type
== tls::TLSOPT_TO_LE
)
2548 // This reloc can appear in debugging sections, in which
2549 // case we must not convert to local-exec. We decide what
2550 // to do based on whether the section is marked as
2551 // containing executable code. That is what the GNU linker
2553 elfcpp::Shdr
<32, false> shdr(relinfo
->data_shdr
);
2554 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
2556 gold_assert(tls_segment
!= NULL
);
2557 value
-= tls_segment
->memsz();
2560 Relocate_functions
<32, false>::rel32(view
, value
);
2563 case elfcpp::R_386_TLS_IE
: // Initial-exec
2564 case elfcpp::R_386_TLS_GOTIE
:
2565 case elfcpp::R_386_TLS_IE_32
:
2566 if (optimized_type
== tls::TLSOPT_TO_LE
)
2568 gold_assert(tls_segment
!= NULL
);
2569 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2570 rel
, r_type
, value
, view
,
2574 else if (optimized_type
== tls::TLSOPT_NONE
)
2576 // Relocate the field with the offset of the GOT entry for
2577 // the tp-relative offset of the symbol.
2578 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2579 ? GOT_TYPE_TLS_OFFSET
2580 : GOT_TYPE_TLS_NOFFSET
);
2581 unsigned int got_offset
;
2584 gold_assert(gsym
->has_got_offset(got_type
));
2585 got_offset
= gsym
->got_offset(got_type
);
2589 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2590 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2591 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2593 // For the R_386_TLS_IE relocation, we need to apply the
2594 // absolute address of the GOT entry.
2595 if (r_type
== elfcpp::R_386_TLS_IE
)
2596 got_offset
+= target
->got_plt_section()->address();
2597 // All GOT offsets are relative to the end of the GOT.
2598 got_offset
-= target
->got_size();
2599 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2602 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2603 _("unsupported reloc %u"),
2607 case elfcpp::R_386_TLS_LE
: // Local-exec
2608 // If we're creating a shared library, a dynamic relocation will
2609 // have been created for this location, so do not apply it now.
2610 if (!parameters
->options().shared())
2612 gold_assert(tls_segment
!= NULL
);
2613 value
-= tls_segment
->memsz();
2614 Relocate_functions
<32, false>::rel32(view
, value
);
2618 case elfcpp::R_386_TLS_LE_32
:
2619 // If we're creating a shared library, a dynamic relocation will
2620 // have been created for this location, so do not apply it now.
2621 if (!parameters
->options().shared())
2623 gold_assert(tls_segment
!= NULL
);
2624 value
= tls_segment
->memsz() - value
;
2625 Relocate_functions
<32, false>::rel32(view
, value
);
2631 // Do a relocation in which we convert a TLS General-Dynamic to a
2635 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2637 Output_segment
* tls_segment
,
2638 const elfcpp::Rel
<32, false>& rel
,
2640 elfcpp::Elf_types
<32>::Elf_Addr value
,
2641 unsigned char* view
,
2642 section_size_type view_size
)
2644 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2645 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2646 // leal foo(%reg),%eax; call ___tls_get_addr
2647 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2649 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2650 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2652 unsigned char op1
= view
[-1];
2653 unsigned char op2
= view
[-2];
2655 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2656 op2
== 0x8d || op2
== 0x04);
2657 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2663 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2664 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2665 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2666 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2667 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2671 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2672 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2673 if (rel
.get_r_offset() + 9 < view_size
2676 // There is a trailing nop. Use the size byte subl.
2677 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2682 // Use the five byte subl.
2683 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2687 value
= tls_segment
->memsz() - value
;
2688 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2690 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2692 this->skip_call_tls_get_addr_
= true;
2695 // Do a relocation in which we convert a TLS General-Dynamic to an
2699 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2702 const elfcpp::Rel
<32, false>& rel
,
2704 elfcpp::Elf_types
<32>::Elf_Addr value
,
2705 unsigned char* view
,
2706 section_size_type view_size
)
2708 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2709 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2711 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2712 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2714 unsigned char op1
= view
[-1];
2715 unsigned char op2
= view
[-2];
2717 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2718 op2
== 0x8d || op2
== 0x04);
2719 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2723 // FIXME: For now, support only the first (SIB) form.
2724 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2728 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2729 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2730 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2731 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2732 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2736 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2737 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2738 if (rel
.get_r_offset() + 9 < view_size
2741 // FIXME: This is not the right instruction sequence.
2742 // There is a trailing nop. Use the size byte subl.
2743 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2748 // FIXME: This is not the right instruction sequence.
2749 // Use the five byte subl.
2750 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2754 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2756 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2758 this->skip_call_tls_get_addr_
= true;
2761 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2762 // General-Dynamic to a Local-Exec.
2765 Target_i386::Relocate::tls_desc_gd_to_le(
2766 const Relocate_info
<32, false>* relinfo
,
2768 Output_segment
* tls_segment
,
2769 const elfcpp::Rel
<32, false>& rel
,
2770 unsigned int r_type
,
2771 elfcpp::Elf_types
<32>::Elf_Addr value
,
2772 unsigned char* view
,
2773 section_size_type view_size
)
2775 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2777 // leal foo@TLSDESC(%ebx), %eax
2778 // ==> leal foo@NTPOFF, %eax
2779 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2780 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2781 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2782 view
[-2] == 0x8d && view
[-1] == 0x83);
2784 value
-= tls_segment
->memsz();
2785 Relocate_functions
<32, false>::rel32(view
, value
);
2789 // call *foo@TLSCALL(%eax)
2791 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2792 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2793 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2794 view
[0] == 0xff && view
[1] == 0x10);
2800 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2801 // General-Dynamic to an Initial-Exec.
2804 Target_i386::Relocate::tls_desc_gd_to_ie(
2805 const Relocate_info
<32, false>* relinfo
,
2808 const elfcpp::Rel
<32, false>& rel
,
2809 unsigned int r_type
,
2810 elfcpp::Elf_types
<32>::Elf_Addr value
,
2811 unsigned char* view
,
2812 section_size_type view_size
)
2814 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2816 // leal foo@TLSDESC(%ebx), %eax
2817 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2818 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2819 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2820 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2821 view
[-2] == 0x8d && view
[-1] == 0x83);
2823 Relocate_functions
<32, false>::rel32(view
, value
);
2827 // call *foo@TLSCALL(%eax)
2829 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2830 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2831 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2832 view
[0] == 0xff && view
[1] == 0x10);
2838 // Do a relocation in which we convert a TLS Local-Dynamic to a
2842 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2845 const elfcpp::Rel
<32, false>& rel
,
2847 elfcpp::Elf_types
<32>::Elf_Addr
,
2848 unsigned char* view
,
2849 section_size_type view_size
)
2851 // leal foo(%reg), %eax; call ___tls_get_addr
2852 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2854 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2855 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2857 // FIXME: Does this test really always pass?
2858 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2859 view
[-2] == 0x8d && view
[-1] == 0x83);
2861 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2863 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2865 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2867 this->skip_call_tls_get_addr_
= true;
2870 // Do a relocation in which we convert a TLS Initial-Exec to a
2874 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2876 Output_segment
* tls_segment
,
2877 const elfcpp::Rel
<32, false>& rel
,
2878 unsigned int r_type
,
2879 elfcpp::Elf_types
<32>::Elf_Addr value
,
2880 unsigned char* view
,
2881 section_size_type view_size
)
2883 // We have to actually change the instructions, which means that we
2884 // need to examine the opcodes to figure out which instruction we
2886 if (r_type
== elfcpp::R_386_TLS_IE
)
2888 // movl %gs:XX,%eax ==> movl $YY,%eax
2889 // movl %gs:XX,%reg ==> movl $YY,%reg
2890 // addl %gs:XX,%reg ==> addl $YY,%reg
2891 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2892 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2894 unsigned char op1
= view
[-1];
2897 // movl XX,%eax ==> movl $YY,%eax
2902 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2904 unsigned char op2
= view
[-2];
2907 // movl XX,%reg ==> movl $YY,%reg
2908 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2909 (op1
& 0xc7) == 0x05);
2911 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2913 else if (op2
== 0x03)
2915 // addl XX,%reg ==> addl $YY,%reg
2916 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2917 (op1
& 0xc7) == 0x05);
2919 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2922 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2927 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2928 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2929 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2930 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2931 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2933 unsigned char op1
= view
[-1];
2934 unsigned char op2
= view
[-2];
2935 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2936 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2939 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2941 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2943 else if (op2
== 0x2b)
2945 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2947 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2949 else if (op2
== 0x03)
2951 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2953 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2956 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2959 value
= tls_segment
->memsz() - value
;
2960 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2963 Relocate_functions
<32, false>::rel32(view
, value
);
2966 // Relocate section data.
2969 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2970 unsigned int sh_type
,
2971 const unsigned char* prelocs
,
2973 Output_section
* output_section
,
2974 bool needs_special_offset_handling
,
2975 unsigned char* view
,
2976 elfcpp::Elf_types
<32>::Elf_Addr address
,
2977 section_size_type view_size
,
2978 const Reloc_symbol_changes
* reloc_symbol_changes
)
2980 gold_assert(sh_type
== elfcpp::SHT_REL
);
2982 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2983 Target_i386::Relocate
>(
2989 needs_special_offset_handling
,
2993 reloc_symbol_changes
);
2996 // Return the size of a relocation while scanning during a relocatable
3000 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3001 unsigned int r_type
,
3006 case elfcpp::R_386_NONE
:
3007 case elfcpp::R_386_GNU_VTINHERIT
:
3008 case elfcpp::R_386_GNU_VTENTRY
:
3009 case elfcpp::R_386_TLS_GD
: // Global-dynamic
3010 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
3011 case elfcpp::R_386_TLS_DESC_CALL
:
3012 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
3013 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
3014 case elfcpp::R_386_TLS_IE
: // Initial-exec
3015 case elfcpp::R_386_TLS_IE_32
:
3016 case elfcpp::R_386_TLS_GOTIE
:
3017 case elfcpp::R_386_TLS_LE
: // Local-exec
3018 case elfcpp::R_386_TLS_LE_32
:
3021 case elfcpp::R_386_32
:
3022 case elfcpp::R_386_PC32
:
3023 case elfcpp::R_386_GOT32
:
3024 case elfcpp::R_386_PLT32
:
3025 case elfcpp::R_386_GOTOFF
:
3026 case elfcpp::R_386_GOTPC
:
3029 case elfcpp::R_386_16
:
3030 case elfcpp::R_386_PC16
:
3033 case elfcpp::R_386_8
:
3034 case elfcpp::R_386_PC8
:
3037 // These are relocations which should only be seen by the
3038 // dynamic linker, and should never be seen here.
3039 case elfcpp::R_386_COPY
:
3040 case elfcpp::R_386_GLOB_DAT
:
3041 case elfcpp::R_386_JUMP_SLOT
:
3042 case elfcpp::R_386_RELATIVE
:
3043 case elfcpp::R_386_IRELATIVE
:
3044 case elfcpp::R_386_TLS_TPOFF
:
3045 case elfcpp::R_386_TLS_DTPMOD32
:
3046 case elfcpp::R_386_TLS_DTPOFF32
:
3047 case elfcpp::R_386_TLS_TPOFF32
:
3048 case elfcpp::R_386_TLS_DESC
:
3049 object
->error(_("unexpected reloc %u in object file"), r_type
);
3052 case elfcpp::R_386_32PLT
:
3053 case elfcpp::R_386_TLS_GD_32
:
3054 case elfcpp::R_386_TLS_GD_PUSH
:
3055 case elfcpp::R_386_TLS_GD_CALL
:
3056 case elfcpp::R_386_TLS_GD_POP
:
3057 case elfcpp::R_386_TLS_LDM_32
:
3058 case elfcpp::R_386_TLS_LDM_PUSH
:
3059 case elfcpp::R_386_TLS_LDM_CALL
:
3060 case elfcpp::R_386_TLS_LDM_POP
:
3061 case elfcpp::R_386_USED_BY_INTEL_200
:
3063 object
->error(_("unsupported reloc %u in object file"), r_type
);
3068 // Scan the relocs during a relocatable link.
3071 Target_i386::scan_relocatable_relocs(Symbol_table
* symtab
,
3073 Sized_relobj_file
<32, false>* object
,
3074 unsigned int data_shndx
,
3075 unsigned int sh_type
,
3076 const unsigned char* prelocs
,
3078 Output_section
* output_section
,
3079 bool needs_special_offset_handling
,
3080 size_t local_symbol_count
,
3081 const unsigned char* plocal_symbols
,
3082 Relocatable_relocs
* rr
)
3084 gold_assert(sh_type
== elfcpp::SHT_REL
);
3086 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
3087 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3089 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
3090 Scan_relocatable_relocs
>(
3098 needs_special_offset_handling
,
3104 // Relocate a section during a relocatable link.
3107 Target_i386::relocate_for_relocatable(
3108 const Relocate_info
<32, false>* relinfo
,
3109 unsigned int sh_type
,
3110 const unsigned char* prelocs
,
3112 Output_section
* output_section
,
3113 off_t offset_in_output_section
,
3114 const Relocatable_relocs
* rr
,
3115 unsigned char* view
,
3116 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
3117 section_size_type view_size
,
3118 unsigned char* reloc_view
,
3119 section_size_type reloc_view_size
)
3121 gold_assert(sh_type
== elfcpp::SHT_REL
);
3123 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
3128 offset_in_output_section
,
3137 // Return the value to use for a dynamic which requires special
3138 // treatment. This is how we support equality comparisons of function
3139 // pointers across shared library boundaries, as described in the
3140 // processor specific ABI supplement.
3143 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
3145 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3146 return this->plt_section()->address() + gsym
->plt_offset();
3149 // Return a string used to fill a code section with nops to take up
3150 // the specified length.
3153 Target_i386::do_code_fill(section_size_type length
) const
3157 // Build a jmp instruction to skip over the bytes.
3158 unsigned char jmp
[5];
3160 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3161 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3162 + std::string(length
- 5, '\0'));
3165 // Nop sequences of various lengths.
3166 const char nop1
[1] = { 0x90 }; // nop
3167 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3168 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3169 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3170 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3171 0x00 }; // leal 0(%esi,1),%esi
3172 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3174 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3176 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3177 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3178 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3179 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3181 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3182 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3184 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3185 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3187 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3188 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3189 0x00, 0x00, 0x00, 0x00 };
3190 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3191 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3192 0x27, 0x00, 0x00, 0x00,
3194 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3195 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3196 0xbc, 0x27, 0x00, 0x00,
3198 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3199 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3200 0x90, 0x90, 0x90, 0x90,
3203 const char* nops
[16] = {
3205 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3206 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3209 return std::string(nops
[length
], length
);
3212 // Return whether SYM should be treated as a call to a non-split
3213 // function. We don't want that to be true of a call to a
3214 // get_pc_thunk function.
3217 Target_i386::do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const
3219 return (sym
->type() == elfcpp::STT_FUNC
3220 && !is_prefix_of("__i686.get_pc_thunk.", sym
->name()));
3223 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3224 // compiled with -fsplit-stack. The function calls non-split-stack
3225 // code. We have to change the function so that it always ensures
3226 // that it has enough stack space to run some random function.
3229 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3230 section_offset_type fnoffset
,
3231 section_size_type fnsize
,
3232 unsigned char* view
,
3233 section_size_type view_size
,
3235 std::string
* to
) const
3237 // The function starts with a comparison of the stack pointer and a
3238 // field in the TCB. This is followed by a jump.
3241 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
3244 // We will call __morestack if the carry flag is set after this
3245 // comparison. We turn the comparison into an stc instruction
3247 view
[fnoffset
] = '\xf9';
3248 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
3250 // lea NN(%esp),%ecx
3251 // lea NN(%esp),%edx
3252 else if ((this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
3253 || this->match_view(view
, view_size
, fnoffset
, "\x8d\x94\x24", 3))
3256 // This is loading an offset from the stack pointer for a
3257 // comparison. The offset is negative, so we decrease the
3258 // offset by the amount of space we need for the stack. This
3259 // means we will avoid calling __morestack if there happens to
3260 // be plenty of space on the stack already.
3261 unsigned char* pval
= view
+ fnoffset
+ 3;
3262 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3263 val
-= parameters
->options().split_stack_adjust_size();
3264 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3268 if (!object
->has_no_split_stack())
3269 object
->error(_("failed to match split-stack sequence at "
3270 "section %u offset %0zx"),
3271 shndx
, static_cast<size_t>(fnoffset
));
3275 // We have to change the function so that it calls
3276 // __morestack_non_split instead of __morestack. The former will
3277 // allocate additional stack space.
3278 *from
= "__morestack";
3279 *to
= "__morestack_non_split";
3282 // The selector for i386 object files.
3284 class Target_selector_i386
: public Target_selector_freebsd
3287 Target_selector_i386()
3288 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
3289 "elf32-i386", "elf32-i386-freebsd")
3293 do_instantiate_target()
3294 { return new Target_i386(); }
3297 Target_selector_i386 target_selector_i386
;
3299 } // End anonymous namespace.