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
<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
<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
<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
<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
<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 -fstack-split code which calls non-stack-split 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.
339 local(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
340 Sized_relobj
<32, false>* object
,
341 unsigned int data_shndx
,
342 Output_section
* output_section
,
343 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
344 const elfcpp::Sym
<32, false>& lsym
);
347 global(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
348 Sized_relobj
<32, false>* object
,
349 unsigned int data_shndx
,
350 Output_section
* output_section
,
351 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
355 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
357 Sized_relobj
<32, false>* object
,
358 unsigned int data_shndx
,
359 Output_section
* output_section
,
360 const elfcpp::Rel
<32, false>& reloc
,
362 const elfcpp::Sym
<32, false>& lsym
);
365 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
367 Sized_relobj
<32, false>* object
,
368 unsigned int data_shndx
,
369 Output_section
* output_section
,
370 const elfcpp::Rel
<32, false>& reloc
,
375 possible_function_pointer_reloc(unsigned int r_type
);
378 reloc_needs_plt_for_ifunc(Sized_relobj
<32, false>*, unsigned int r_type
);
381 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
384 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
388 // The class which implements relocation.
393 : skip_call_tls_get_addr_(false),
394 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
399 if (this->skip_call_tls_get_addr_
)
401 // FIXME: This needs to specify the location somehow.
402 gold_error(_("missing expected TLS relocation"));
406 // Return whether the static relocation needs to be applied.
408 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
411 Output_section
* output_section
);
413 // Do a relocation. Return false if the caller should not issue
414 // any warnings about this relocation.
416 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
417 size_t relnum
, const elfcpp::Rel
<32, false>&,
418 unsigned int r_type
, const Sized_symbol
<32>*,
419 const Symbol_value
<32>*,
420 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
424 // Do a TLS relocation.
426 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
427 size_t relnum
, const elfcpp::Rel
<32, false>&,
428 unsigned int r_type
, const Sized_symbol
<32>*,
429 const Symbol_value
<32>*,
430 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
433 // Do a TLS General-Dynamic to Initial-Exec transition.
435 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
436 Output_segment
* tls_segment
,
437 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
438 elfcpp::Elf_types
<32>::Elf_Addr value
,
440 section_size_type view_size
);
442 // Do a TLS General-Dynamic to Local-Exec transition.
444 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
445 Output_segment
* tls_segment
,
446 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
447 elfcpp::Elf_types
<32>::Elf_Addr value
,
449 section_size_type view_size
);
451 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
454 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
455 Output_segment
* tls_segment
,
456 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
457 elfcpp::Elf_types
<32>::Elf_Addr value
,
459 section_size_type view_size
);
461 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
464 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
465 Output_segment
* tls_segment
,
466 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
467 elfcpp::Elf_types
<32>::Elf_Addr value
,
469 section_size_type view_size
);
471 // Do a TLS Local-Dynamic to Local-Exec transition.
473 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
474 Output_segment
* tls_segment
,
475 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
476 elfcpp::Elf_types
<32>::Elf_Addr value
,
478 section_size_type view_size
);
480 // Do a TLS Initial-Exec to Local-Exec transition.
482 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
483 Output_segment
* tls_segment
,
484 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
485 elfcpp::Elf_types
<32>::Elf_Addr value
,
487 section_size_type view_size
);
489 // We need to keep track of which type of local dynamic relocation
490 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
491 enum Local_dynamic_type
498 // This is set if we should skip the next reloc, which should be a
499 // PLT32 reloc against ___tls_get_addr.
500 bool skip_call_tls_get_addr_
;
501 // The type of local dynamic relocation we have seen in the section
502 // being relocated, if any.
503 Local_dynamic_type local_dynamic_type_
;
506 // A class which returns the size required for a relocation type,
507 // used while scanning relocs during a relocatable link.
508 class Relocatable_size_for_reloc
512 get_size_for_reloc(unsigned int, Relobj
*);
515 // Adjust TLS relocation type based on the options and whether this
516 // is a local symbol.
517 static tls::Tls_optimization
518 optimize_tls_reloc(bool is_final
, int r_type
);
520 // Get the GOT section, creating it if necessary.
521 Output_data_got
<32, false>*
522 got_section(Symbol_table
*, Layout
*);
524 // Get the GOT PLT section.
526 got_plt_section() const
528 gold_assert(this->got_plt_
!= NULL
);
529 return this->got_plt_
;
532 // Get the GOT section for TLSDESC entries.
533 Output_data_got
<32, false>*
534 got_tlsdesc_section() const
536 gold_assert(this->got_tlsdesc_
!= NULL
);
537 return this->got_tlsdesc_
;
540 // Create the PLT section.
542 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
544 // Create a PLT entry for a global symbol.
546 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
548 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
550 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
551 Sized_relobj
<32, false>* relobj
,
552 unsigned int local_sym_index
);
554 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
556 define_tls_base_symbol(Symbol_table
*, Layout
*);
558 // Create a GOT entry for the TLS module index.
560 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
561 Sized_relobj
<32, false>* object
);
563 // Get the PLT section.
564 Output_data_plt_i386
*
567 gold_assert(this->plt_
!= NULL
);
571 // Get the dynamic reloc section, creating it if necessary.
573 rel_dyn_section(Layout
*);
575 // Get the section to use for TLS_DESC relocations.
577 rel_tls_desc_section(Layout
*) const;
579 // Add a potential copy relocation.
581 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
582 Sized_relobj
<32, false>* object
,
583 unsigned int shndx
, Output_section
* output_section
,
584 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
586 this->copy_relocs_
.copy_reloc(symtab
, layout
,
587 symtab
->get_sized_symbol
<32>(sym
),
588 object
, shndx
, output_section
, reloc
,
589 this->rel_dyn_section(layout
));
592 // Information about this specific target which we pass to the
593 // general Target structure.
594 static const Target::Target_info i386_info
;
596 // The types of GOT entries needed for this platform.
597 // These values are exposed to the ABI in an incremental link.
598 // Do not renumber existing values without changing the version
599 // number of the .gnu_incremental_inputs section.
602 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
603 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
604 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
605 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
606 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
610 Output_data_got
<32, false>* got_
;
612 Output_data_plt_i386
* plt_
;
613 // The GOT PLT section.
614 Output_data_space
* got_plt_
;
615 // The GOT section for TLSDESC relocations.
616 Output_data_got
<32, false>* got_tlsdesc_
;
617 // The _GLOBAL_OFFSET_TABLE_ symbol.
618 Symbol
* global_offset_table_
;
619 // The dynamic reloc section.
620 Reloc_section
* rel_dyn_
;
621 // Relocs saved to avoid a COPY reloc.
622 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
623 // Space for variables copied with a COPY reloc.
624 Output_data_space
* dynbss_
;
625 // Offset of the GOT entry for the TLS module index.
626 unsigned int got_mod_index_offset_
;
627 // True if the _TLS_MODULE_BASE_ symbol has been defined.
628 bool tls_base_symbol_defined_
;
631 const Target::Target_info
Target_i386::i386_info
=
634 false, // is_big_endian
635 elfcpp::EM_386
, // machine_code
636 false, // has_make_symbol
637 false, // has_resolve
638 true, // has_code_fill
639 true, // is_default_stack_executable
641 "/usr/lib/libc.so.1", // dynamic_linker
642 0x08048000, // default_text_segment_address
643 0x1000, // abi_pagesize (overridable by -z max-page-size)
644 0x1000, // common_pagesize (overridable by -z common-page-size)
645 elfcpp::SHN_UNDEF
, // small_common_shndx
646 elfcpp::SHN_UNDEF
, // large_common_shndx
647 0, // small_common_section_flags
648 0, // large_common_section_flags
649 NULL
, // attributes_section
650 NULL
// attributes_vendor
653 // Get the GOT section, creating it if necessary.
655 Output_data_got
<32, false>*
656 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
658 if (this->got_
== NULL
)
660 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
662 this->got_
= new Output_data_got
<32, false>();
664 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
666 | elfcpp::SHF_WRITE
),
667 this->got_
, ORDER_RELRO_LAST
, true);
669 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
670 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
672 | elfcpp::SHF_WRITE
),
673 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
676 // The first three entries are reserved.
677 this->got_plt_
->set_current_data_size(3 * 4);
679 // Those bytes can go into the relro segment.
680 layout
->increase_relro(3 * 4);
682 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
683 this->global_offset_table_
=
684 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
685 Symbol_table::PREDEFINED
,
687 0, 0, elfcpp::STT_OBJECT
,
689 elfcpp::STV_HIDDEN
, 0,
692 // If there are any TLSDESC relocations, they get GOT entries in
693 // .got.plt after the jump slot entries.
694 this->got_tlsdesc_
= new Output_data_got
<32, false>();
695 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
697 | elfcpp::SHF_WRITE
),
699 ORDER_NON_RELRO_FIRST
, false);
705 // Get the dynamic reloc section, creating it if necessary.
707 Target_i386::Reloc_section
*
708 Target_i386::rel_dyn_section(Layout
* layout
)
710 if (this->rel_dyn_
== NULL
)
712 gold_assert(layout
!= NULL
);
713 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
714 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
715 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
716 ORDER_DYNAMIC_RELOCS
, false);
718 return this->rel_dyn_
;
721 // Create the PLT section. The ordinary .got section is an argument,
722 // since we need to refer to the start. We also create our own .got
723 // section just for PLT entries.
725 Output_data_plt_i386::Output_data_plt_i386(Symbol_table
* symtab
,
727 Output_data_space
* got_plt
)
728 : Output_section_data(4), tls_desc_rel_(NULL
), got_plt_(got_plt
), count_(0),
729 global_ifuncs_(), local_ifuncs_()
731 this->rel_
= new Reloc_section(false);
732 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
733 elfcpp::SHF_ALLOC
, this->rel_
,
734 ORDER_DYNAMIC_PLT_RELOCS
, false);
736 if (parameters
->doing_static_link())
738 // A statically linked executable will only have a .rel.plt
739 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
740 // symbols. The library will use these symbols to locate the
741 // IRELATIVE relocs at program startup time.
742 symtab
->define_in_output_data("__rel_iplt_start", NULL
,
743 Symbol_table::PREDEFINED
,
744 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
745 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
747 symtab
->define_in_output_data("__rel_iplt_end", NULL
,
748 Symbol_table::PREDEFINED
,
749 this->rel_
, 0, 0, elfcpp::STT_NOTYPE
,
750 elfcpp::STB_GLOBAL
, elfcpp::STV_HIDDEN
,
756 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
758 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
759 // linker, and so do we.
763 // Add an entry to the PLT.
766 Output_data_plt_i386::add_entry(Symbol
* gsym
)
768 gold_assert(!gsym
->has_plt_offset());
770 // Note that when setting the PLT offset we skip the initial
771 // reserved PLT entry.
772 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
776 section_offset_type got_offset
= this->got_plt_
->current_data_size();
778 // Every PLT entry needs a GOT entry which points back to the PLT
779 // entry (this will be changed by the dynamic linker, normally
780 // lazily when the function is called).
781 this->got_plt_
->set_current_data_size(got_offset
+ 4);
783 // Every PLT entry needs a reloc.
784 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
785 && gsym
->can_use_relative_reloc(false))
787 this->rel_
->add_symbolless_global_addend(gsym
, elfcpp::R_386_IRELATIVE
,
788 this->got_plt_
, got_offset
);
789 struct Global_ifunc gi
;
791 gi
.got_offset
= got_offset
;
792 this->global_ifuncs_
.push_back(gi
);
796 gsym
->set_needs_dynsym_entry();
797 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
801 // Note that we don't need to save the symbol. The contents of the
802 // PLT are independent of which symbols are used. The symbols only
803 // appear in the relocations.
806 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
810 Output_data_plt_i386::add_local_ifunc_entry(Sized_relobj
<32, false>* relobj
,
811 unsigned int local_sym_index
)
813 unsigned int plt_offset
= (this->count_
+ 1) * plt_entry_size
;
816 section_offset_type got_offset
= this->got_plt_
->current_data_size();
818 // Every PLT entry needs a GOT entry which points back to the PLT
820 this->got_plt_
->set_current_data_size(got_offset
+ 4);
822 // Every PLT entry needs a reloc.
823 this->rel_
->add_symbolless_local_addend(relobj
, local_sym_index
,
824 elfcpp::R_386_IRELATIVE
,
825 this->got_plt_
, got_offset
);
827 struct Local_ifunc li
;
829 li
.local_sym_index
= local_sym_index
;
830 li
.got_offset
= got_offset
;
831 this->local_ifuncs_
.push_back(li
);
836 // Return where the TLS_DESC relocations should go, creating it if
837 // necessary. These follow the JUMP_SLOT relocations.
839 Output_data_plt_i386::Reloc_section
*
840 Output_data_plt_i386::rel_tls_desc(Layout
* layout
)
842 if (this->tls_desc_rel_
== NULL
)
844 this->tls_desc_rel_
= new Reloc_section(false);
845 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
846 elfcpp::SHF_ALLOC
, this->tls_desc_rel_
,
847 ORDER_DYNAMIC_PLT_RELOCS
, false);
848 gold_assert(this->tls_desc_rel_
->output_section() ==
849 this->rel_
->output_section());
851 return this->tls_desc_rel_
;
854 // The first entry in the PLT for an executable.
856 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
858 0xff, 0x35, // pushl contents of memory address
859 0, 0, 0, 0, // replaced with address of .got + 4
860 0xff, 0x25, // jmp indirect
861 0, 0, 0, 0, // replaced with address of .got + 8
865 // The first entry in the PLT for a shared object.
867 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
869 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
870 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
874 // Subsequent entries in the PLT for an executable.
876 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
878 0xff, 0x25, // jmp indirect
879 0, 0, 0, 0, // replaced with address of symbol in .got
880 0x68, // pushl immediate
881 0, 0, 0, 0, // replaced with offset into relocation table
882 0xe9, // jmp relative
883 0, 0, 0, 0 // replaced with offset to start of .plt
886 // Subsequent entries in the PLT for a shared object.
888 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
890 0xff, 0xa3, // jmp *offset(%ebx)
891 0, 0, 0, 0, // replaced with offset of symbol in .got
892 0x68, // pushl immediate
893 0, 0, 0, 0, // replaced with offset into relocation table
894 0xe9, // jmp relative
895 0, 0, 0, 0 // replaced with offset to start of .plt
898 // Write out the PLT. This uses the hand-coded instructions above,
899 // and adjusts them as needed. This is all specified by the i386 ELF
900 // Processor Supplement.
903 Output_data_plt_i386::do_write(Output_file
* of
)
905 const off_t offset
= this->offset();
906 const section_size_type oview_size
=
907 convert_to_section_size_type(this->data_size());
908 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
910 const off_t got_file_offset
= this->got_plt_
->offset();
911 const section_size_type got_size
=
912 convert_to_section_size_type(this->got_plt_
->data_size());
913 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
916 unsigned char* pov
= oview
;
918 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
919 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
921 if (parameters
->options().output_is_position_independent())
922 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
925 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
926 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
927 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
929 pov
+= plt_entry_size
;
931 unsigned char* got_pov
= got_view
;
933 memset(got_pov
, 0, 12);
936 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
938 unsigned int plt_offset
= plt_entry_size
;
939 unsigned int plt_rel_offset
= 0;
940 unsigned int got_offset
= 12;
941 const unsigned int count
= this->count_
;
942 for (unsigned int i
= 0;
945 pov
+= plt_entry_size
,
947 plt_offset
+= plt_entry_size
,
948 plt_rel_offset
+= rel_size
,
951 // Set and adjust the PLT entry itself.
953 if (parameters
->options().output_is_position_independent())
955 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
956 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
960 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
961 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
966 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
967 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
968 - (plt_offset
+ plt_entry_size
));
970 // Set the entry in the GOT.
971 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
974 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
975 // the GOT to point to the actual symbol value, rather than point to
976 // the PLT entry. That will let the dynamic linker call the right
977 // function when resolving IRELATIVE relocations.
978 for (std::vector
<Global_ifunc
>::const_iterator p
=
979 this->global_ifuncs_
.begin();
980 p
!= this->global_ifuncs_
.end();
983 const Sized_symbol
<32>* ssym
=
984 static_cast<const Sized_symbol
<32>*>(p
->sym
);
985 elfcpp::Swap
<32, false>::writeval(got_view
+ p
->got_offset
,
989 for (std::vector
<Local_ifunc
>::const_iterator p
=
990 this->local_ifuncs_
.begin();
991 p
!= this->local_ifuncs_
.end();
994 const Symbol_value
<32>* psymval
=
995 p
->object
->local_symbol(p
->local_sym_index
);
996 elfcpp::Swap
<32, false>::writeval(got_view
+ p
->got_offset
,
997 psymval
->value(p
->object
, 0));
1000 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1001 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1003 of
->write_output_view(offset
, oview_size
, oview
);
1004 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1007 // Create the PLT section.
1010 Target_i386::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1012 if (this->plt_
== NULL
)
1014 // Create the GOT sections first.
1015 this->got_section(symtab
, layout
);
1017 this->plt_
= new Output_data_plt_i386(symtab
, layout
, this->got_plt_
);
1018 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1020 | elfcpp::SHF_EXECINSTR
),
1021 this->plt_
, ORDER_PLT
, false);
1023 // Make the sh_info field of .rel.plt point to .plt.
1024 Output_section
* rel_plt_os
= this->plt_
->rel_plt()->output_section();
1025 rel_plt_os
->set_info_section(this->plt_
->output_section());
1029 // Create a PLT entry for a global symbol.
1032 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
1034 if (gsym
->has_plt_offset())
1036 if (this->plt_
== NULL
)
1037 this->make_plt_section(symtab
, layout
);
1038 this->plt_
->add_entry(gsym
);
1041 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1044 Target_i386::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1045 Sized_relobj
<32, false>* relobj
,
1046 unsigned int local_sym_index
)
1048 if (relobj
->local_has_plt_offset(local_sym_index
))
1050 if (this->plt_
== NULL
)
1051 this->make_plt_section(symtab
, layout
);
1052 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(relobj
,
1054 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1057 // Return the number of entries in the PLT.
1060 Target_i386::plt_entry_count() const
1062 if (this->plt_
== NULL
)
1064 return this->plt_
->entry_count();
1067 // Return the offset of the first non-reserved PLT entry.
1070 Target_i386::first_plt_entry_offset() const
1072 return Output_data_plt_i386::first_plt_entry_offset();
1075 // Return the size of each PLT entry.
1078 Target_i386::plt_entry_size() const
1080 return Output_data_plt_i386::get_plt_entry_size();
1083 // Get the section to use for TLS_DESC relocations.
1085 Target_i386::Reloc_section
*
1086 Target_i386::rel_tls_desc_section(Layout
* layout
) const
1088 return this->plt_section()->rel_tls_desc(layout
);
1091 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1094 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1096 if (this->tls_base_symbol_defined_
)
1099 Output_segment
* tls_segment
= layout
->tls_segment();
1100 if (tls_segment
!= NULL
)
1102 bool is_exec
= parameters
->options().output_is_executable();
1103 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1104 Symbol_table::PREDEFINED
,
1108 elfcpp::STV_HIDDEN
, 0,
1110 ? Symbol::SEGMENT_END
1111 : Symbol::SEGMENT_START
),
1114 this->tls_base_symbol_defined_
= true;
1117 // Create a GOT entry for the TLS module index.
1120 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1121 Sized_relobj
<32, false>* object
)
1123 if (this->got_mod_index_offset_
== -1U)
1125 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1126 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1127 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
1128 unsigned int got_offset
= got
->add_constant(0);
1129 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
1131 got
->add_constant(0);
1132 this->got_mod_index_offset_
= got_offset
;
1134 return this->got_mod_index_offset_
;
1137 // Optimize the TLS relocation type based on what we know about the
1138 // symbol. IS_FINAL is true if the final address of this symbol is
1139 // known at link time.
1141 tls::Tls_optimization
1142 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
1144 // If we are generating a shared library, then we can't do anything
1146 if (parameters
->options().shared())
1147 return tls::TLSOPT_NONE
;
1151 case elfcpp::R_386_TLS_GD
:
1152 case elfcpp::R_386_TLS_GOTDESC
:
1153 case elfcpp::R_386_TLS_DESC_CALL
:
1154 // These are General-Dynamic which permits fully general TLS
1155 // access. Since we know that we are generating an executable,
1156 // we can convert this to Initial-Exec. If we also know that
1157 // this is a local symbol, we can further switch to Local-Exec.
1159 return tls::TLSOPT_TO_LE
;
1160 return tls::TLSOPT_TO_IE
;
1162 case elfcpp::R_386_TLS_LDM
:
1163 // This is Local-Dynamic, which refers to a local symbol in the
1164 // dynamic TLS block. Since we know that we generating an
1165 // executable, we can switch to Local-Exec.
1166 return tls::TLSOPT_TO_LE
;
1168 case elfcpp::R_386_TLS_LDO_32
:
1169 // Another type of Local-Dynamic relocation.
1170 return tls::TLSOPT_TO_LE
;
1172 case elfcpp::R_386_TLS_IE
:
1173 case elfcpp::R_386_TLS_GOTIE
:
1174 case elfcpp::R_386_TLS_IE_32
:
1175 // These are Initial-Exec relocs which get the thread offset
1176 // from the GOT. If we know that we are linking against the
1177 // local symbol, we can switch to Local-Exec, which links the
1178 // thread offset into the instruction.
1180 return tls::TLSOPT_TO_LE
;
1181 return tls::TLSOPT_NONE
;
1183 case elfcpp::R_386_TLS_LE
:
1184 case elfcpp::R_386_TLS_LE_32
:
1185 // When we already have Local-Exec, there is nothing further we
1187 return tls::TLSOPT_NONE
;
1194 // Report an unsupported relocation against a local symbol.
1197 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
1198 unsigned int r_type
)
1200 gold_error(_("%s: unsupported reloc %u against local symbol"),
1201 object
->name().c_str(), r_type
);
1204 // Return whether we need to make a PLT entry for a relocation of a
1205 // given type against a STT_GNU_IFUNC symbol.
1208 Target_i386::Scan::reloc_needs_plt_for_ifunc(Sized_relobj
<32, false>* object
,
1209 unsigned int r_type
)
1213 case elfcpp::R_386_NONE
:
1214 case elfcpp::R_386_GNU_VTINHERIT
:
1215 case elfcpp::R_386_GNU_VTENTRY
:
1218 case elfcpp::R_386_32
:
1219 case elfcpp::R_386_16
:
1220 case elfcpp::R_386_8
:
1221 case elfcpp::R_386_PC32
:
1222 case elfcpp::R_386_PC16
:
1223 case elfcpp::R_386_PC8
:
1224 case elfcpp::R_386_PLT32
:
1225 case elfcpp::R_386_GOTOFF
:
1226 case elfcpp::R_386_GOTPC
:
1227 case elfcpp::R_386_GOT32
:
1230 case elfcpp::R_386_COPY
:
1231 case elfcpp::R_386_GLOB_DAT
:
1232 case elfcpp::R_386_JUMP_SLOT
:
1233 case elfcpp::R_386_RELATIVE
:
1234 case elfcpp::R_386_IRELATIVE
:
1235 case elfcpp::R_386_TLS_TPOFF
:
1236 case elfcpp::R_386_TLS_DTPMOD32
:
1237 case elfcpp::R_386_TLS_DTPOFF32
:
1238 case elfcpp::R_386_TLS_TPOFF32
:
1239 case elfcpp::R_386_TLS_DESC
:
1240 // We will give an error later.
1243 case elfcpp::R_386_TLS_GD
:
1244 case elfcpp::R_386_TLS_GOTDESC
:
1245 case elfcpp::R_386_TLS_DESC_CALL
:
1246 case elfcpp::R_386_TLS_LDM
:
1247 case elfcpp::R_386_TLS_LDO_32
:
1248 case elfcpp::R_386_TLS_IE
:
1249 case elfcpp::R_386_TLS_IE_32
:
1250 case elfcpp::R_386_TLS_GOTIE
:
1251 case elfcpp::R_386_TLS_LE
:
1252 case elfcpp::R_386_TLS_LE_32
:
1253 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1254 object
->name().c_str(), r_type
);
1257 case elfcpp::R_386_32PLT
:
1258 case elfcpp::R_386_TLS_GD_32
:
1259 case elfcpp::R_386_TLS_GD_PUSH
:
1260 case elfcpp::R_386_TLS_GD_CALL
:
1261 case elfcpp::R_386_TLS_GD_POP
:
1262 case elfcpp::R_386_TLS_LDM_32
:
1263 case elfcpp::R_386_TLS_LDM_PUSH
:
1264 case elfcpp::R_386_TLS_LDM_CALL
:
1265 case elfcpp::R_386_TLS_LDM_POP
:
1266 case elfcpp::R_386_USED_BY_INTEL_200
:
1268 // We will give an error later.
1273 // Scan a relocation for a local symbol.
1276 Target_i386::Scan::local(Symbol_table
* symtab
,
1278 Target_i386
* target
,
1279 Sized_relobj
<32, false>* object
,
1280 unsigned int data_shndx
,
1281 Output_section
* output_section
,
1282 const elfcpp::Rel
<32, false>& reloc
,
1283 unsigned int r_type
,
1284 const elfcpp::Sym
<32, false>& lsym
)
1286 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1287 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1288 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1290 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1291 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1296 case elfcpp::R_386_NONE
:
1297 case elfcpp::R_386_GNU_VTINHERIT
:
1298 case elfcpp::R_386_GNU_VTENTRY
:
1301 case elfcpp::R_386_32
:
1302 // If building a shared library (or a position-independent
1303 // executable), we need to create a dynamic relocation for
1304 // this location. The relocation applied at link time will
1305 // apply the link-time value, so we flag the location with
1306 // an R_386_RELATIVE relocation so the dynamic loader can
1307 // relocate it easily.
1308 if (parameters
->options().output_is_position_independent())
1310 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1311 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1312 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
1313 output_section
, data_shndx
,
1314 reloc
.get_r_offset());
1318 case elfcpp::R_386_16
:
1319 case elfcpp::R_386_8
:
1320 // If building a shared library (or a position-independent
1321 // executable), we need to create a dynamic relocation for
1322 // this location. Because the addend needs to remain in the
1323 // data section, we need to be careful not to apply this
1324 // relocation statically.
1325 if (parameters
->options().output_is_position_independent())
1327 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1328 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1329 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1330 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1331 data_shndx
, reloc
.get_r_offset());
1334 gold_assert(lsym
.get_st_value() == 0);
1335 unsigned int shndx
= lsym
.get_st_shndx();
1337 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1340 object
->error(_("section symbol %u has bad shndx %u"),
1343 rel_dyn
->add_local_section(object
, shndx
,
1344 r_type
, output_section
,
1345 data_shndx
, reloc
.get_r_offset());
1350 case elfcpp::R_386_PC32
:
1351 case elfcpp::R_386_PC16
:
1352 case elfcpp::R_386_PC8
:
1355 case elfcpp::R_386_PLT32
:
1356 // Since we know this is a local symbol, we can handle this as a
1360 case elfcpp::R_386_GOTOFF
:
1361 case elfcpp::R_386_GOTPC
:
1362 // We need a GOT section.
1363 target
->got_section(symtab
, layout
);
1366 case elfcpp::R_386_GOT32
:
1368 // The symbol requires a GOT entry.
1369 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1370 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1372 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1373 // lets function pointers compare correctly with shared
1374 // libraries. Otherwise we would need an IRELATIVE reloc.
1376 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1377 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1379 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1382 // If we are generating a shared object, we need to add a
1383 // dynamic RELATIVE relocation for this symbol's GOT entry.
1384 if (parameters
->options().output_is_position_independent())
1386 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1387 unsigned int got_offset
=
1388 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1389 rel_dyn
->add_local_relative(object
, r_sym
,
1390 elfcpp::R_386_RELATIVE
,
1397 // These are relocations which should only be seen by the
1398 // dynamic linker, and should never be seen here.
1399 case elfcpp::R_386_COPY
:
1400 case elfcpp::R_386_GLOB_DAT
:
1401 case elfcpp::R_386_JUMP_SLOT
:
1402 case elfcpp::R_386_RELATIVE
:
1403 case elfcpp::R_386_IRELATIVE
:
1404 case elfcpp::R_386_TLS_TPOFF
:
1405 case elfcpp::R_386_TLS_DTPMOD32
:
1406 case elfcpp::R_386_TLS_DTPOFF32
:
1407 case elfcpp::R_386_TLS_TPOFF32
:
1408 case elfcpp::R_386_TLS_DESC
:
1409 gold_error(_("%s: unexpected reloc %u in object file"),
1410 object
->name().c_str(), r_type
);
1413 // These are initial TLS relocs, which are expected when
1415 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1416 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1417 case elfcpp::R_386_TLS_DESC_CALL
:
1418 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1419 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1420 case elfcpp::R_386_TLS_IE
: // Initial-exec
1421 case elfcpp::R_386_TLS_IE_32
:
1422 case elfcpp::R_386_TLS_GOTIE
:
1423 case elfcpp::R_386_TLS_LE
: // Local-exec
1424 case elfcpp::R_386_TLS_LE_32
:
1426 bool output_is_shared
= parameters
->options().shared();
1427 const tls::Tls_optimization optimized_type
1428 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1431 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1432 if (optimized_type
== tls::TLSOPT_NONE
)
1434 // Create a pair of GOT entries for the module index and
1435 // dtv-relative offset.
1436 Output_data_got
<32, false>* got
1437 = target
->got_section(symtab
, layout
);
1438 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1439 unsigned int shndx
= lsym
.get_st_shndx();
1441 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1443 object
->error(_("local symbol %u has bad shndx %u"),
1446 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1448 target
->rel_dyn_section(layout
),
1449 elfcpp::R_386_TLS_DTPMOD32
, 0);
1451 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1452 unsupported_reloc_local(object
, r_type
);
1455 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1456 target
->define_tls_base_symbol(symtab
, layout
);
1457 if (optimized_type
== tls::TLSOPT_NONE
)
1459 // Create a double GOT entry with an R_386_TLS_DESC
1460 // reloc. The R_386_TLS_DESC reloc is resolved
1461 // lazily, so the GOT entry needs to be in an area in
1462 // .got.plt, not .got. Call got_section to make sure
1463 // the section has been created.
1464 target
->got_section(symtab
, layout
);
1465 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1466 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1467 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1469 unsigned int got_offset
= got
->add_constant(0);
1470 // The local symbol value is stored in the second
1472 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_DESC
);
1473 // That set the GOT offset of the local symbol to
1474 // point to the second entry, but we want it to
1475 // point to the first.
1476 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1478 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1479 rt
->add_absolute(elfcpp::R_386_TLS_DESC
, got
, got_offset
);
1482 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1483 unsupported_reloc_local(object
, r_type
);
1486 case elfcpp::R_386_TLS_DESC_CALL
:
1489 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1490 if (optimized_type
== tls::TLSOPT_NONE
)
1492 // Create a GOT entry for the module index.
1493 target
->got_mod_index_entry(symtab
, layout
, object
);
1495 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1496 unsupported_reloc_local(object
, r_type
);
1499 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1502 case elfcpp::R_386_TLS_IE
: // Initial-exec
1503 case elfcpp::R_386_TLS_IE_32
:
1504 case elfcpp::R_386_TLS_GOTIE
:
1505 layout
->set_has_static_tls();
1506 if (optimized_type
== tls::TLSOPT_NONE
)
1508 // For the R_386_TLS_IE relocation, we need to create a
1509 // dynamic relocation when building a shared library.
1510 if (r_type
== elfcpp::R_386_TLS_IE
1511 && parameters
->options().shared())
1513 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1515 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1516 rel_dyn
->add_local_relative(object
, r_sym
,
1517 elfcpp::R_386_RELATIVE
,
1518 output_section
, data_shndx
,
1519 reloc
.get_r_offset());
1521 // Create a GOT entry for the tp-relative offset.
1522 Output_data_got
<32, false>* got
1523 = target
->got_section(symtab
, layout
);
1524 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1525 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1526 ? elfcpp::R_386_TLS_TPOFF32
1527 : elfcpp::R_386_TLS_TPOFF
);
1528 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1529 ? GOT_TYPE_TLS_OFFSET
1530 : GOT_TYPE_TLS_NOFFSET
);
1531 got
->add_local_with_rel(object
, r_sym
, got_type
,
1532 target
->rel_dyn_section(layout
),
1535 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1536 unsupported_reloc_local(object
, r_type
);
1539 case elfcpp::R_386_TLS_LE
: // Local-exec
1540 case elfcpp::R_386_TLS_LE_32
:
1541 layout
->set_has_static_tls();
1542 if (output_is_shared
)
1544 // We need to create a dynamic relocation.
1545 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
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_LE_32
1548 ? elfcpp::R_386_TLS_TPOFF32
1549 : elfcpp::R_386_TLS_TPOFF
);
1550 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1551 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1552 data_shndx
, reloc
.get_r_offset());
1562 case elfcpp::R_386_32PLT
:
1563 case elfcpp::R_386_TLS_GD_32
:
1564 case elfcpp::R_386_TLS_GD_PUSH
:
1565 case elfcpp::R_386_TLS_GD_CALL
:
1566 case elfcpp::R_386_TLS_GD_POP
:
1567 case elfcpp::R_386_TLS_LDM_32
:
1568 case elfcpp::R_386_TLS_LDM_PUSH
:
1569 case elfcpp::R_386_TLS_LDM_CALL
:
1570 case elfcpp::R_386_TLS_LDM_POP
:
1571 case elfcpp::R_386_USED_BY_INTEL_200
:
1573 unsupported_reloc_local(object
, r_type
);
1578 // Report an unsupported relocation against a global symbol.
1581 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1582 unsigned int r_type
,
1585 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1586 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1590 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1594 case elfcpp::R_386_32
:
1595 case elfcpp::R_386_16
:
1596 case elfcpp::R_386_8
:
1597 case elfcpp::R_386_GOTOFF
:
1598 case elfcpp::R_386_GOT32
:
1609 Target_i386::Scan::local_reloc_may_be_function_pointer(
1613 Sized_relobj
<32, false>* ,
1616 const elfcpp::Rel
<32, false>& ,
1617 unsigned int r_type
,
1618 const elfcpp::Sym
<32, false>&)
1620 return possible_function_pointer_reloc(r_type
);
1624 Target_i386::Scan::global_reloc_may_be_function_pointer(
1628 Sized_relobj
<32, false>* ,
1631 const elfcpp::Rel
<32, false>& ,
1632 unsigned int r_type
,
1635 return possible_function_pointer_reloc(r_type
);
1638 // Scan a relocation for a global symbol.
1641 Target_i386::Scan::global(Symbol_table
* symtab
,
1643 Target_i386
* target
,
1644 Sized_relobj
<32, false>* object
,
1645 unsigned int data_shndx
,
1646 Output_section
* output_section
,
1647 const elfcpp::Rel
<32, false>& reloc
,
1648 unsigned int r_type
,
1651 // A STT_GNU_IFUNC symbol may require a PLT entry.
1652 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1653 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1654 target
->make_plt_entry(symtab
, layout
, gsym
);
1658 case elfcpp::R_386_NONE
:
1659 case elfcpp::R_386_GNU_VTINHERIT
:
1660 case elfcpp::R_386_GNU_VTENTRY
:
1663 case elfcpp::R_386_32
:
1664 case elfcpp::R_386_16
:
1665 case elfcpp::R_386_8
:
1667 // Make a PLT entry if necessary.
1668 if (gsym
->needs_plt_entry())
1670 target
->make_plt_entry(symtab
, layout
, gsym
);
1671 // Since this is not a PC-relative relocation, we may be
1672 // taking the address of a function. In that case we need to
1673 // set the entry in the dynamic symbol table to the address of
1675 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1676 gsym
->set_needs_dynsym_value();
1678 // Make a dynamic relocation if necessary.
1679 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1681 if (gsym
->may_need_copy_reloc())
1683 target
->copy_reloc(symtab
, layout
, object
,
1684 data_shndx
, output_section
, gsym
, reloc
);
1686 else if (r_type
== elfcpp::R_386_32
1687 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1688 && gsym
->can_use_relative_reloc(false)
1689 && !gsym
->is_from_dynobj()
1690 && !gsym
->is_undefined()
1691 && !gsym
->is_preemptible())
1693 // Use an IRELATIVE reloc for a locally defined
1694 // STT_GNU_IFUNC symbol. This makes a function
1695 // address in a PIE executable match the address in a
1696 // shared library that it links against.
1697 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1698 rel_dyn
->add_symbolless_global_addend(gsym
,
1699 elfcpp::R_386_IRELATIVE
,
1702 reloc
.get_r_offset());
1704 else if (r_type
== elfcpp::R_386_32
1705 && gsym
->can_use_relative_reloc(false))
1707 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1708 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1709 output_section
, object
,
1710 data_shndx
, reloc
.get_r_offset());
1714 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1715 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1716 data_shndx
, reloc
.get_r_offset());
1722 case elfcpp::R_386_PC32
:
1723 case elfcpp::R_386_PC16
:
1724 case elfcpp::R_386_PC8
:
1726 // Make a PLT entry if necessary.
1727 if (gsym
->needs_plt_entry())
1729 // These relocations are used for function calls only in
1730 // non-PIC code. For a 32-bit relocation in a shared library,
1731 // we'll need a text relocation anyway, so we can skip the
1732 // PLT entry and let the dynamic linker bind the call directly
1733 // to the target. For smaller relocations, we should use a
1734 // PLT entry to ensure that the call can reach.
1735 if (!parameters
->options().shared()
1736 || r_type
!= elfcpp::R_386_PC32
)
1737 target
->make_plt_entry(symtab
, layout
, gsym
);
1739 // Make a dynamic relocation if necessary.
1740 int flags
= Symbol::NON_PIC_REF
;
1741 if (gsym
->is_func())
1742 flags
|= Symbol::FUNCTION_CALL
;
1743 if (gsym
->needs_dynamic_reloc(flags
))
1745 if (gsym
->may_need_copy_reloc())
1747 target
->copy_reloc(symtab
, layout
, object
,
1748 data_shndx
, output_section
, gsym
, reloc
);
1752 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1753 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1754 data_shndx
, reloc
.get_r_offset());
1760 case elfcpp::R_386_GOT32
:
1762 // The symbol requires a GOT entry.
1763 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1764 if (gsym
->final_value_is_known())
1766 // For a STT_GNU_IFUNC symbol we want the PLT address.
1767 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1768 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1770 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1774 // If this symbol is not fully resolved, we need to add a
1775 // GOT entry with a dynamic relocation.
1776 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1777 if (gsym
->is_from_dynobj()
1778 || gsym
->is_undefined()
1779 || gsym
->is_preemptible()
1780 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
1781 && parameters
->options().output_is_position_independent()))
1782 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1783 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1786 // For a STT_GNU_IFUNC symbol we want to write the PLT
1787 // offset into the GOT, so that function pointer
1788 // comparisons work correctly.
1790 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
1791 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1794 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1795 // Tell the dynamic linker to use the PLT address
1796 // when resolving relocations.
1797 if (gsym
->is_from_dynobj()
1798 && !parameters
->options().shared())
1799 gsym
->set_needs_dynsym_value();
1803 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
1804 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1812 case elfcpp::R_386_PLT32
:
1813 // If the symbol is fully resolved, this is just a PC32 reloc.
1814 // Otherwise we need a PLT entry.
1815 if (gsym
->final_value_is_known())
1817 // If building a shared library, we can also skip the PLT entry
1818 // if the symbol is defined in the output file and is protected
1820 if (gsym
->is_defined()
1821 && !gsym
->is_from_dynobj()
1822 && !gsym
->is_preemptible())
1824 target
->make_plt_entry(symtab
, layout
, gsym
);
1827 case elfcpp::R_386_GOTOFF
:
1828 case elfcpp::R_386_GOTPC
:
1829 // We need a GOT section.
1830 target
->got_section(symtab
, layout
);
1833 // These are relocations which should only be seen by the
1834 // dynamic linker, and should never be seen here.
1835 case elfcpp::R_386_COPY
:
1836 case elfcpp::R_386_GLOB_DAT
:
1837 case elfcpp::R_386_JUMP_SLOT
:
1838 case elfcpp::R_386_RELATIVE
:
1839 case elfcpp::R_386_IRELATIVE
:
1840 case elfcpp::R_386_TLS_TPOFF
:
1841 case elfcpp::R_386_TLS_DTPMOD32
:
1842 case elfcpp::R_386_TLS_DTPOFF32
:
1843 case elfcpp::R_386_TLS_TPOFF32
:
1844 case elfcpp::R_386_TLS_DESC
:
1845 gold_error(_("%s: unexpected reloc %u in object file"),
1846 object
->name().c_str(), r_type
);
1849 // These are initial tls relocs, which are expected when
1851 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1852 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1853 case elfcpp::R_386_TLS_DESC_CALL
:
1854 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1855 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1856 case elfcpp::R_386_TLS_IE
: // Initial-exec
1857 case elfcpp::R_386_TLS_IE_32
:
1858 case elfcpp::R_386_TLS_GOTIE
:
1859 case elfcpp::R_386_TLS_LE
: // Local-exec
1860 case elfcpp::R_386_TLS_LE_32
:
1862 const bool is_final
= gsym
->final_value_is_known();
1863 const tls::Tls_optimization optimized_type
1864 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1867 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1868 if (optimized_type
== tls::TLSOPT_NONE
)
1870 // Create a pair of GOT entries for the module index and
1871 // dtv-relative offset.
1872 Output_data_got
<32, false>* got
1873 = target
->got_section(symtab
, layout
);
1874 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1875 target
->rel_dyn_section(layout
),
1876 elfcpp::R_386_TLS_DTPMOD32
,
1877 elfcpp::R_386_TLS_DTPOFF32
);
1879 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1881 // Create a GOT entry for the tp-relative offset.
1882 Output_data_got
<32, false>* got
1883 = target
->got_section(symtab
, layout
);
1884 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1885 target
->rel_dyn_section(layout
),
1886 elfcpp::R_386_TLS_TPOFF
);
1888 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1889 unsupported_reloc_global(object
, r_type
, gsym
);
1892 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1893 target
->define_tls_base_symbol(symtab
, layout
);
1894 if (optimized_type
== tls::TLSOPT_NONE
)
1896 // Create a double GOT entry with an R_386_TLS_DESC
1897 // reloc. The R_386_TLS_DESC reloc is resolved
1898 // lazily, so the GOT entry needs to be in an area in
1899 // .got.plt, not .got. Call got_section to make sure
1900 // the section has been created.
1901 target
->got_section(symtab
, layout
);
1902 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1903 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1904 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
1905 elfcpp::R_386_TLS_DESC
, 0);
1907 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1909 // Create a GOT entry for the tp-relative offset.
1910 Output_data_got
<32, false>* got
1911 = target
->got_section(symtab
, layout
);
1912 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1913 target
->rel_dyn_section(layout
),
1914 elfcpp::R_386_TLS_TPOFF
);
1916 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1917 unsupported_reloc_global(object
, r_type
, gsym
);
1920 case elfcpp::R_386_TLS_DESC_CALL
:
1923 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1924 if (optimized_type
== tls::TLSOPT_NONE
)
1926 // Create a GOT entry for the module index.
1927 target
->got_mod_index_entry(symtab
, layout
, object
);
1929 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1930 unsupported_reloc_global(object
, r_type
, gsym
);
1933 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1936 case elfcpp::R_386_TLS_IE
: // Initial-exec
1937 case elfcpp::R_386_TLS_IE_32
:
1938 case elfcpp::R_386_TLS_GOTIE
:
1939 layout
->set_has_static_tls();
1940 if (optimized_type
== tls::TLSOPT_NONE
)
1942 // For the R_386_TLS_IE relocation, we need to create a
1943 // dynamic relocation when building a shared library.
1944 if (r_type
== elfcpp::R_386_TLS_IE
1945 && parameters
->options().shared())
1947 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1948 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1949 output_section
, object
,
1951 reloc
.get_r_offset());
1953 // Create a GOT entry for the tp-relative offset.
1954 Output_data_got
<32, false>* got
1955 = target
->got_section(symtab
, layout
);
1956 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1957 ? elfcpp::R_386_TLS_TPOFF32
1958 : elfcpp::R_386_TLS_TPOFF
);
1959 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1960 ? GOT_TYPE_TLS_OFFSET
1961 : GOT_TYPE_TLS_NOFFSET
);
1962 got
->add_global_with_rel(gsym
, got_type
,
1963 target
->rel_dyn_section(layout
),
1966 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1967 unsupported_reloc_global(object
, r_type
, gsym
);
1970 case elfcpp::R_386_TLS_LE
: // Local-exec
1971 case elfcpp::R_386_TLS_LE_32
:
1972 layout
->set_has_static_tls();
1973 if (parameters
->options().shared())
1975 // We need to create a dynamic relocation.
1976 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1977 ? elfcpp::R_386_TLS_TPOFF32
1978 : elfcpp::R_386_TLS_TPOFF
);
1979 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1980 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1981 data_shndx
, reloc
.get_r_offset());
1991 case elfcpp::R_386_32PLT
:
1992 case elfcpp::R_386_TLS_GD_32
:
1993 case elfcpp::R_386_TLS_GD_PUSH
:
1994 case elfcpp::R_386_TLS_GD_CALL
:
1995 case elfcpp::R_386_TLS_GD_POP
:
1996 case elfcpp::R_386_TLS_LDM_32
:
1997 case elfcpp::R_386_TLS_LDM_PUSH
:
1998 case elfcpp::R_386_TLS_LDM_CALL
:
1999 case elfcpp::R_386_TLS_LDM_POP
:
2000 case elfcpp::R_386_USED_BY_INTEL_200
:
2002 unsupported_reloc_global(object
, r_type
, gsym
);
2007 // Process relocations for gc.
2010 Target_i386::gc_process_relocs(Symbol_table
* symtab
,
2012 Sized_relobj
<32, false>* object
,
2013 unsigned int data_shndx
,
2015 const unsigned char* prelocs
,
2017 Output_section
* output_section
,
2018 bool needs_special_offset_handling
,
2019 size_t local_symbol_count
,
2020 const unsigned char* plocal_symbols
)
2022 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2024 Target_i386::Relocatable_size_for_reloc
>(
2033 needs_special_offset_handling
,
2038 // Scan relocations for a section.
2041 Target_i386::scan_relocs(Symbol_table
* symtab
,
2043 Sized_relobj
<32, false>* object
,
2044 unsigned int data_shndx
,
2045 unsigned int sh_type
,
2046 const unsigned char* prelocs
,
2048 Output_section
* output_section
,
2049 bool needs_special_offset_handling
,
2050 size_t local_symbol_count
,
2051 const unsigned char* plocal_symbols
)
2053 if (sh_type
== elfcpp::SHT_RELA
)
2055 gold_error(_("%s: unsupported RELA reloc section"),
2056 object
->name().c_str());
2060 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2070 needs_special_offset_handling
,
2075 // Finalize the sections.
2078 Target_i386::do_finalize_sections(
2080 const Input_objects
*,
2081 Symbol_table
* symtab
)
2083 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2085 : this->plt_
->rel_plt());
2086 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
2087 this->rel_dyn_
, true, false);
2089 // Emit any relocs we saved in an attempt to avoid generating COPY
2091 if (this->copy_relocs_
.any_saved_relocs())
2092 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
2094 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2095 // the .got.plt section.
2096 Symbol
* sym
= this->global_offset_table_
;
2099 uint32_t data_size
= this->got_plt_
->current_data_size();
2100 symtab
->get_sized_symbol
<32>(sym
)->set_symsize(data_size
);
2104 // Return whether a direct absolute static relocation needs to be applied.
2105 // In cases where Scan::local() or Scan::global() has created
2106 // a dynamic relocation other than R_386_RELATIVE, the addend
2107 // of the relocation is carried in the data, and we must not
2108 // apply the static relocation.
2111 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2114 Output_section
* output_section
)
2116 // If the output section is not allocated, then we didn't call
2117 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2119 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
2122 // For local symbols, we will have created a non-RELATIVE dynamic
2123 // relocation only if (a) the output is position independent,
2124 // (b) the relocation is absolute (not pc- or segment-relative), and
2125 // (c) the relocation is not 32 bits wide.
2127 return !(parameters
->options().output_is_position_independent()
2128 && (ref_flags
& Symbol::ABSOLUTE_REF
)
2131 // For global symbols, we use the same helper routines used in the
2132 // scan pass. If we did not create a dynamic relocation, or if we
2133 // created a RELATIVE dynamic relocation, we should apply the static
2135 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
2136 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
2137 && gsym
->can_use_relative_reloc(ref_flags
2138 & Symbol::FUNCTION_CALL
);
2139 return !has_dyn
|| is_rel
;
2142 // Perform a relocation.
2145 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
2146 Target_i386
* target
,
2147 Output_section
* output_section
,
2149 const elfcpp::Rel
<32, false>& rel
,
2150 unsigned int r_type
,
2151 const Sized_symbol
<32>* gsym
,
2152 const Symbol_value
<32>* psymval
,
2153 unsigned char* view
,
2154 elfcpp::Elf_types
<32>::Elf_Addr address
,
2155 section_size_type view_size
)
2157 if (this->skip_call_tls_get_addr_
)
2159 if ((r_type
!= elfcpp::R_386_PLT32
2160 && r_type
!= elfcpp::R_386_PC32
)
2162 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
2163 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2164 _("missing expected TLS relocation"));
2167 this->skip_call_tls_get_addr_
= false;
2172 const Sized_relobj
<32, false>* object
= relinfo
->object
;
2174 // Pick the value to use for symbols defined in shared objects.
2175 Symbol_value
<32> symval
;
2177 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2178 && r_type
== elfcpp::R_386_32
2179 && gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
)
2180 && gsym
->can_use_relative_reloc(false)
2181 && !gsym
->is_from_dynobj()
2182 && !gsym
->is_undefined()
2183 && !gsym
->is_preemptible())
2185 // In this case we are generating a R_386_IRELATIVE reloc. We
2186 // want to use the real value of the symbol, not the PLT offset.
2188 else if (gsym
!= NULL
2189 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
2190 || r_type
== elfcpp::R_386_PC16
2191 || r_type
== elfcpp::R_386_PC32
))
2193 symval
.set_output_value(target
->plt_section()->address()
2194 + gsym
->plt_offset());
2197 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2199 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2200 if (object
->local_has_plt_offset(r_sym
))
2202 symval
.set_output_value(target
->plt_section()->address()
2203 + object
->local_plt_offset(r_sym
));
2208 // Get the GOT offset if needed.
2209 // The GOT pointer points to the end of the GOT section.
2210 // We need to subtract the size of the GOT section to get
2211 // the actual offset to use in the relocation.
2212 bool have_got_offset
= false;
2213 unsigned int got_offset
= 0;
2216 case elfcpp::R_386_GOT32
:
2219 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2220 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
2221 - target
->got_size());
2225 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2226 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2227 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2228 - target
->got_size());
2230 have_got_offset
= true;
2239 case elfcpp::R_386_NONE
:
2240 case elfcpp::R_386_GNU_VTINHERIT
:
2241 case elfcpp::R_386_GNU_VTENTRY
:
2244 case elfcpp::R_386_32
:
2245 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
2247 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
2250 case elfcpp::R_386_PC32
:
2252 int ref_flags
= Symbol::NON_PIC_REF
;
2253 if (gsym
!= NULL
&& gsym
->is_func())
2254 ref_flags
|= Symbol::FUNCTION_CALL
;
2255 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
2256 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2260 case elfcpp::R_386_16
:
2261 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
2263 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
2266 case elfcpp::R_386_PC16
:
2268 int ref_flags
= Symbol::NON_PIC_REF
;
2269 if (gsym
!= NULL
&& gsym
->is_func())
2270 ref_flags
|= Symbol::FUNCTION_CALL
;
2271 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
2272 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
2276 case elfcpp::R_386_8
:
2277 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
2279 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
2282 case elfcpp::R_386_PC8
:
2284 int ref_flags
= Symbol::NON_PIC_REF
;
2285 if (gsym
!= NULL
&& gsym
->is_func())
2286 ref_flags
|= Symbol::FUNCTION_CALL
;
2287 if (should_apply_static_reloc(gsym
, ref_flags
, false,
2289 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
2293 case elfcpp::R_386_PLT32
:
2294 gold_assert(gsym
== NULL
2295 || gsym
->has_plt_offset()
2296 || gsym
->final_value_is_known()
2297 || (gsym
->is_defined()
2298 && !gsym
->is_from_dynobj()
2299 && !gsym
->is_preemptible()));
2300 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2303 case elfcpp::R_386_GOT32
:
2304 gold_assert(have_got_offset
);
2305 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2308 case elfcpp::R_386_GOTOFF
:
2310 elfcpp::Elf_types
<32>::Elf_Addr value
;
2311 value
= (psymval
->value(object
, 0)
2312 - target
->got_plt_section()->address());
2313 Relocate_functions
<32, false>::rel32(view
, value
);
2317 case elfcpp::R_386_GOTPC
:
2319 elfcpp::Elf_types
<32>::Elf_Addr value
;
2320 value
= target
->got_plt_section()->address();
2321 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
2325 case elfcpp::R_386_COPY
:
2326 case elfcpp::R_386_GLOB_DAT
:
2327 case elfcpp::R_386_JUMP_SLOT
:
2328 case elfcpp::R_386_RELATIVE
:
2329 case elfcpp::R_386_IRELATIVE
:
2330 // These are outstanding tls relocs, which are unexpected when
2332 case elfcpp::R_386_TLS_TPOFF
:
2333 case elfcpp::R_386_TLS_DTPMOD32
:
2334 case elfcpp::R_386_TLS_DTPOFF32
:
2335 case elfcpp::R_386_TLS_TPOFF32
:
2336 case elfcpp::R_386_TLS_DESC
:
2337 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2338 _("unexpected reloc %u in object file"),
2342 // These are initial tls relocs, which are expected when
2344 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2345 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2346 case elfcpp::R_386_TLS_DESC_CALL
:
2347 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2348 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2349 case elfcpp::R_386_TLS_IE
: // Initial-exec
2350 case elfcpp::R_386_TLS_IE_32
:
2351 case elfcpp::R_386_TLS_GOTIE
:
2352 case elfcpp::R_386_TLS_LE
: // Local-exec
2353 case elfcpp::R_386_TLS_LE_32
:
2354 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
2355 view
, address
, view_size
);
2358 case elfcpp::R_386_32PLT
:
2359 case elfcpp::R_386_TLS_GD_32
:
2360 case elfcpp::R_386_TLS_GD_PUSH
:
2361 case elfcpp::R_386_TLS_GD_CALL
:
2362 case elfcpp::R_386_TLS_GD_POP
:
2363 case elfcpp::R_386_TLS_LDM_32
:
2364 case elfcpp::R_386_TLS_LDM_PUSH
:
2365 case elfcpp::R_386_TLS_LDM_CALL
:
2366 case elfcpp::R_386_TLS_LDM_POP
:
2367 case elfcpp::R_386_USED_BY_INTEL_200
:
2369 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2370 _("unsupported reloc %u"),
2378 // Perform a TLS relocation.
2381 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
2382 Target_i386
* target
,
2384 const elfcpp::Rel
<32, false>& rel
,
2385 unsigned int r_type
,
2386 const Sized_symbol
<32>* gsym
,
2387 const Symbol_value
<32>* psymval
,
2388 unsigned char* view
,
2389 elfcpp::Elf_types
<32>::Elf_Addr
,
2390 section_size_type view_size
)
2392 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2394 const Sized_relobj
<32, false>* object
= relinfo
->object
;
2396 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
2398 const bool is_final
= (gsym
== NULL
2399 ? !parameters
->options().shared()
2400 : gsym
->final_value_is_known());
2401 const tls::Tls_optimization optimized_type
2402 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2405 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2406 if (optimized_type
== tls::TLSOPT_TO_LE
)
2408 gold_assert(tls_segment
!= NULL
);
2409 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2410 rel
, r_type
, value
, view
,
2416 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2417 ? GOT_TYPE_TLS_NOFFSET
2418 : GOT_TYPE_TLS_PAIR
);
2419 unsigned int got_offset
;
2422 gold_assert(gsym
->has_got_offset(got_type
));
2423 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2427 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2428 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2429 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2430 - target
->got_size());
2432 if (optimized_type
== tls::TLSOPT_TO_IE
)
2434 gold_assert(tls_segment
!= NULL
);
2435 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2436 got_offset
, view
, view_size
);
2439 else if (optimized_type
== tls::TLSOPT_NONE
)
2441 // Relocate the field with the offset of the pair of GOT
2443 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2447 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2448 _("unsupported reloc %u"),
2452 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2453 case elfcpp::R_386_TLS_DESC_CALL
:
2454 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2455 if (optimized_type
== tls::TLSOPT_TO_LE
)
2457 gold_assert(tls_segment
!= NULL
);
2458 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2459 rel
, r_type
, value
, view
,
2465 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2466 ? GOT_TYPE_TLS_NOFFSET
2467 : GOT_TYPE_TLS_DESC
);
2468 unsigned int got_offset
= 0;
2469 if (r_type
== elfcpp::R_386_TLS_GOTDESC
2470 && optimized_type
== tls::TLSOPT_NONE
)
2472 // We created GOT entries in the .got.tlsdesc portion of
2473 // the .got.plt section, but the offset stored in the
2474 // symbol is the offset within .got.tlsdesc.
2475 got_offset
= (target
->got_size()
2476 + target
->got_plt_section()->data_size());
2480 gold_assert(gsym
->has_got_offset(got_type
));
2481 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2485 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2486 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2487 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2488 - target
->got_size());
2490 if (optimized_type
== tls::TLSOPT_TO_IE
)
2492 gold_assert(tls_segment
!= NULL
);
2493 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2494 got_offset
, view
, view_size
);
2497 else if (optimized_type
== tls::TLSOPT_NONE
)
2499 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2501 // Relocate the field with the offset of the pair of GOT
2503 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2508 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2509 _("unsupported reloc %u"),
2513 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2514 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
2516 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2517 _("both SUN and GNU model "
2518 "TLS relocations"));
2521 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2522 if (optimized_type
== tls::TLSOPT_TO_LE
)
2524 gold_assert(tls_segment
!= NULL
);
2525 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2526 value
, view
, view_size
);
2529 else if (optimized_type
== tls::TLSOPT_NONE
)
2531 // Relocate the field with the offset of the GOT entry for
2532 // the module index.
2533 unsigned int got_offset
;
2534 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2535 - target
->got_size());
2536 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2539 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2540 _("unsupported reloc %u"),
2544 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2545 if (optimized_type
== tls::TLSOPT_TO_LE
)
2547 // This reloc can appear in debugging sections, in which
2548 // case we must not convert to local-exec. We decide what
2549 // to do based on whether the section is marked as
2550 // containing executable code. That is what the GNU linker
2552 elfcpp::Shdr
<32, false> shdr(relinfo
->data_shdr
);
2553 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
2555 gold_assert(tls_segment
!= NULL
);
2556 value
-= tls_segment
->memsz();
2559 Relocate_functions
<32, false>::rel32(view
, value
);
2562 case elfcpp::R_386_TLS_IE
: // Initial-exec
2563 case elfcpp::R_386_TLS_GOTIE
:
2564 case elfcpp::R_386_TLS_IE_32
:
2565 if (optimized_type
== tls::TLSOPT_TO_LE
)
2567 gold_assert(tls_segment
!= NULL
);
2568 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2569 rel
, r_type
, value
, view
,
2573 else if (optimized_type
== tls::TLSOPT_NONE
)
2575 // Relocate the field with the offset of the GOT entry for
2576 // the tp-relative offset of the symbol.
2577 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2578 ? GOT_TYPE_TLS_OFFSET
2579 : GOT_TYPE_TLS_NOFFSET
);
2580 unsigned int got_offset
;
2583 gold_assert(gsym
->has_got_offset(got_type
));
2584 got_offset
= gsym
->got_offset(got_type
);
2588 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2589 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2590 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2592 // For the R_386_TLS_IE relocation, we need to apply the
2593 // absolute address of the GOT entry.
2594 if (r_type
== elfcpp::R_386_TLS_IE
)
2595 got_offset
+= target
->got_plt_section()->address();
2596 // All GOT offsets are relative to the end of the GOT.
2597 got_offset
-= target
->got_size();
2598 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2601 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2602 _("unsupported reloc %u"),
2606 case elfcpp::R_386_TLS_LE
: // Local-exec
2607 // If we're creating a shared library, a dynamic relocation will
2608 // have been created for this location, so do not apply it now.
2609 if (!parameters
->options().shared())
2611 gold_assert(tls_segment
!= NULL
);
2612 value
-= tls_segment
->memsz();
2613 Relocate_functions
<32, false>::rel32(view
, value
);
2617 case elfcpp::R_386_TLS_LE_32
:
2618 // If we're creating a shared library, a dynamic relocation will
2619 // have been created for this location, so do not apply it now.
2620 if (!parameters
->options().shared())
2622 gold_assert(tls_segment
!= NULL
);
2623 value
= tls_segment
->memsz() - value
;
2624 Relocate_functions
<32, false>::rel32(view
, value
);
2630 // Do a relocation in which we convert a TLS General-Dynamic to a
2634 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2636 Output_segment
* tls_segment
,
2637 const elfcpp::Rel
<32, false>& rel
,
2639 elfcpp::Elf_types
<32>::Elf_Addr value
,
2640 unsigned char* view
,
2641 section_size_type view_size
)
2643 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2644 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2645 // leal foo(%reg),%eax; call ___tls_get_addr
2646 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2648 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2649 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2651 unsigned char op1
= view
[-1];
2652 unsigned char op2
= view
[-2];
2654 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2655 op2
== 0x8d || op2
== 0x04);
2656 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2662 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2663 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2664 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2665 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2666 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2670 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2671 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2672 if (rel
.get_r_offset() + 9 < view_size
2675 // There is a trailing nop. Use the size byte subl.
2676 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2681 // Use the five byte subl.
2682 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2686 value
= tls_segment
->memsz() - value
;
2687 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2689 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2691 this->skip_call_tls_get_addr_
= true;
2694 // Do a relocation in which we convert a TLS General-Dynamic to an
2698 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2701 const elfcpp::Rel
<32, false>& rel
,
2703 elfcpp::Elf_types
<32>::Elf_Addr value
,
2704 unsigned char* view
,
2705 section_size_type view_size
)
2707 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2708 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2710 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2711 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2713 unsigned char op1
= view
[-1];
2714 unsigned char op2
= view
[-2];
2716 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2717 op2
== 0x8d || op2
== 0x04);
2718 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2722 // FIXME: For now, support only the first (SIB) form.
2723 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2727 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2728 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2729 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2730 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2731 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2735 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2736 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2737 if (rel
.get_r_offset() + 9 < view_size
2740 // FIXME: This is not the right instruction sequence.
2741 // There is a trailing nop. Use the size byte subl.
2742 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2747 // FIXME: This is not the right instruction sequence.
2748 // Use the five byte subl.
2749 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2753 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2755 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2757 this->skip_call_tls_get_addr_
= true;
2760 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2761 // General-Dynamic to a Local-Exec.
2764 Target_i386::Relocate::tls_desc_gd_to_le(
2765 const Relocate_info
<32, false>* relinfo
,
2767 Output_segment
* tls_segment
,
2768 const elfcpp::Rel
<32, false>& rel
,
2769 unsigned int r_type
,
2770 elfcpp::Elf_types
<32>::Elf_Addr value
,
2771 unsigned char* view
,
2772 section_size_type view_size
)
2774 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2776 // leal foo@TLSDESC(%ebx), %eax
2777 // ==> leal foo@NTPOFF, %eax
2778 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2779 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2780 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2781 view
[-2] == 0x8d && view
[-1] == 0x83);
2783 value
-= tls_segment
->memsz();
2784 Relocate_functions
<32, false>::rel32(view
, value
);
2788 // call *foo@TLSCALL(%eax)
2790 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2791 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2792 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2793 view
[0] == 0xff && view
[1] == 0x10);
2799 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2800 // General-Dynamic to an Initial-Exec.
2803 Target_i386::Relocate::tls_desc_gd_to_ie(
2804 const Relocate_info
<32, false>* relinfo
,
2807 const elfcpp::Rel
<32, false>& rel
,
2808 unsigned int r_type
,
2809 elfcpp::Elf_types
<32>::Elf_Addr value
,
2810 unsigned char* view
,
2811 section_size_type view_size
)
2813 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2815 // leal foo@TLSDESC(%ebx), %eax
2816 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2817 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2818 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2819 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2820 view
[-2] == 0x8d && view
[-1] == 0x83);
2822 Relocate_functions
<32, false>::rel32(view
, value
);
2826 // call *foo@TLSCALL(%eax)
2828 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2829 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2830 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2831 view
[0] == 0xff && view
[1] == 0x10);
2837 // Do a relocation in which we convert a TLS Local-Dynamic to a
2841 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2844 const elfcpp::Rel
<32, false>& rel
,
2846 elfcpp::Elf_types
<32>::Elf_Addr
,
2847 unsigned char* view
,
2848 section_size_type view_size
)
2850 // leal foo(%reg), %eax; call ___tls_get_addr
2851 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2853 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2854 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2856 // FIXME: Does this test really always pass?
2857 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2858 view
[-2] == 0x8d && view
[-1] == 0x83);
2860 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2862 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2864 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2866 this->skip_call_tls_get_addr_
= true;
2869 // Do a relocation in which we convert a TLS Initial-Exec to a
2873 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2875 Output_segment
* tls_segment
,
2876 const elfcpp::Rel
<32, false>& rel
,
2877 unsigned int r_type
,
2878 elfcpp::Elf_types
<32>::Elf_Addr value
,
2879 unsigned char* view
,
2880 section_size_type view_size
)
2882 // We have to actually change the instructions, which means that we
2883 // need to examine the opcodes to figure out which instruction we
2885 if (r_type
== elfcpp::R_386_TLS_IE
)
2887 // movl %gs:XX,%eax ==> movl $YY,%eax
2888 // movl %gs:XX,%reg ==> movl $YY,%reg
2889 // addl %gs:XX,%reg ==> addl $YY,%reg
2890 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2891 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2893 unsigned char op1
= view
[-1];
2896 // movl XX,%eax ==> movl $YY,%eax
2901 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2903 unsigned char op2
= view
[-2];
2906 // movl XX,%reg ==> movl $YY,%reg
2907 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2908 (op1
& 0xc7) == 0x05);
2910 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2912 else if (op2
== 0x03)
2914 // addl XX,%reg ==> addl $YY,%reg
2915 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2916 (op1
& 0xc7) == 0x05);
2918 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2921 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2926 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2927 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2928 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2929 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2930 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2932 unsigned char op1
= view
[-1];
2933 unsigned char op2
= view
[-2];
2934 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2935 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2938 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2940 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2942 else if (op2
== 0x2b)
2944 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2946 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2948 else if (op2
== 0x03)
2950 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2952 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2955 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2958 value
= tls_segment
->memsz() - value
;
2959 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2962 Relocate_functions
<32, false>::rel32(view
, value
);
2965 // Relocate section data.
2968 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2969 unsigned int sh_type
,
2970 const unsigned char* prelocs
,
2972 Output_section
* output_section
,
2973 bool needs_special_offset_handling
,
2974 unsigned char* view
,
2975 elfcpp::Elf_types
<32>::Elf_Addr address
,
2976 section_size_type view_size
,
2977 const Reloc_symbol_changes
* reloc_symbol_changes
)
2979 gold_assert(sh_type
== elfcpp::SHT_REL
);
2981 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2982 Target_i386::Relocate
>(
2988 needs_special_offset_handling
,
2992 reloc_symbol_changes
);
2995 // Return the size of a relocation while scanning during a relocatable
2999 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3000 unsigned int r_type
,
3005 case elfcpp::R_386_NONE
:
3006 case elfcpp::R_386_GNU_VTINHERIT
:
3007 case elfcpp::R_386_GNU_VTENTRY
:
3008 case elfcpp::R_386_TLS_GD
: // Global-dynamic
3009 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
3010 case elfcpp::R_386_TLS_DESC_CALL
:
3011 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
3012 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
3013 case elfcpp::R_386_TLS_IE
: // Initial-exec
3014 case elfcpp::R_386_TLS_IE_32
:
3015 case elfcpp::R_386_TLS_GOTIE
:
3016 case elfcpp::R_386_TLS_LE
: // Local-exec
3017 case elfcpp::R_386_TLS_LE_32
:
3020 case elfcpp::R_386_32
:
3021 case elfcpp::R_386_PC32
:
3022 case elfcpp::R_386_GOT32
:
3023 case elfcpp::R_386_PLT32
:
3024 case elfcpp::R_386_GOTOFF
:
3025 case elfcpp::R_386_GOTPC
:
3028 case elfcpp::R_386_16
:
3029 case elfcpp::R_386_PC16
:
3032 case elfcpp::R_386_8
:
3033 case elfcpp::R_386_PC8
:
3036 // These are relocations which should only be seen by the
3037 // dynamic linker, and should never be seen here.
3038 case elfcpp::R_386_COPY
:
3039 case elfcpp::R_386_GLOB_DAT
:
3040 case elfcpp::R_386_JUMP_SLOT
:
3041 case elfcpp::R_386_RELATIVE
:
3042 case elfcpp::R_386_IRELATIVE
:
3043 case elfcpp::R_386_TLS_TPOFF
:
3044 case elfcpp::R_386_TLS_DTPMOD32
:
3045 case elfcpp::R_386_TLS_DTPOFF32
:
3046 case elfcpp::R_386_TLS_TPOFF32
:
3047 case elfcpp::R_386_TLS_DESC
:
3048 object
->error(_("unexpected reloc %u in object file"), r_type
);
3051 case elfcpp::R_386_32PLT
:
3052 case elfcpp::R_386_TLS_GD_32
:
3053 case elfcpp::R_386_TLS_GD_PUSH
:
3054 case elfcpp::R_386_TLS_GD_CALL
:
3055 case elfcpp::R_386_TLS_GD_POP
:
3056 case elfcpp::R_386_TLS_LDM_32
:
3057 case elfcpp::R_386_TLS_LDM_PUSH
:
3058 case elfcpp::R_386_TLS_LDM_CALL
:
3059 case elfcpp::R_386_TLS_LDM_POP
:
3060 case elfcpp::R_386_USED_BY_INTEL_200
:
3062 object
->error(_("unsupported reloc %u in object file"), r_type
);
3067 // Scan the relocs during a relocatable link.
3070 Target_i386::scan_relocatable_relocs(Symbol_table
* symtab
,
3072 Sized_relobj
<32, false>* object
,
3073 unsigned int data_shndx
,
3074 unsigned int sh_type
,
3075 const unsigned char* prelocs
,
3077 Output_section
* output_section
,
3078 bool needs_special_offset_handling
,
3079 size_t local_symbol_count
,
3080 const unsigned char* plocal_symbols
,
3081 Relocatable_relocs
* rr
)
3083 gold_assert(sh_type
== elfcpp::SHT_REL
);
3085 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
3086 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3088 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
3089 Scan_relocatable_relocs
>(
3097 needs_special_offset_handling
,
3103 // Relocate a section during a relocatable link.
3106 Target_i386::relocate_for_relocatable(
3107 const Relocate_info
<32, false>* relinfo
,
3108 unsigned int sh_type
,
3109 const unsigned char* prelocs
,
3111 Output_section
* output_section
,
3112 off_t offset_in_output_section
,
3113 const Relocatable_relocs
* rr
,
3114 unsigned char* view
,
3115 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
3116 section_size_type view_size
,
3117 unsigned char* reloc_view
,
3118 section_size_type reloc_view_size
)
3120 gold_assert(sh_type
== elfcpp::SHT_REL
);
3122 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
3127 offset_in_output_section
,
3136 // Return the value to use for a dynamic which requires special
3137 // treatment. This is how we support equality comparisons of function
3138 // pointers across shared library boundaries, as described in the
3139 // processor specific ABI supplement.
3142 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
3144 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3145 return this->plt_section()->address() + gsym
->plt_offset();
3148 // Return a string used to fill a code section with nops to take up
3149 // the specified length.
3152 Target_i386::do_code_fill(section_size_type length
) const
3156 // Build a jmp instruction to skip over the bytes.
3157 unsigned char jmp
[5];
3159 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3160 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3161 + std::string(length
- 5, '\0'));
3164 // Nop sequences of various lengths.
3165 const char nop1
[1] = { 0x90 }; // nop
3166 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3167 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3168 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3169 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3170 0x00 }; // leal 0(%esi,1),%esi
3171 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3173 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3175 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3176 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3177 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3178 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3180 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3181 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3183 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3184 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3186 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3187 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3188 0x00, 0x00, 0x00, 0x00 };
3189 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3190 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3191 0x27, 0x00, 0x00, 0x00,
3193 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3194 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3195 0xbc, 0x27, 0x00, 0x00,
3197 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3198 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3199 0x90, 0x90, 0x90, 0x90,
3202 const char* nops
[16] = {
3204 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3205 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3208 return std::string(nops
[length
], length
);
3211 // Return whether SYM should be treated as a call to a non-split
3212 // function. We don't want that to be true of a call to a
3213 // get_pc_thunk function.
3216 Target_i386::do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const
3218 return (sym
->type() == elfcpp::STT_FUNC
3219 && !is_prefix_of("__i686.get_pc_thunk.", sym
->name()));
3222 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3223 // compiled with -fstack-split. The function calls non-stack-split
3224 // code. We have to change the function so that it always ensures
3225 // that it has enough stack space to run some random function.
3228 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3229 section_offset_type fnoffset
,
3230 section_size_type fnsize
,
3231 unsigned char* view
,
3232 section_size_type view_size
,
3234 std::string
* to
) const
3236 // The function starts with a comparison of the stack pointer and a
3237 // field in the TCB. This is followed by a jump.
3240 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
3243 // We will call __morestack if the carry flag is set after this
3244 // comparison. We turn the comparison into an stc instruction
3246 view
[fnoffset
] = '\xf9';
3247 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
3249 // lea NN(%esp),%ecx
3250 // lea NN(%esp),%edx
3251 else if ((this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
3252 || this->match_view(view
, view_size
, fnoffset
, "\x8d\x94\x24", 3))
3255 // This is loading an offset from the stack pointer for a
3256 // comparison. The offset is negative, so we decrease the
3257 // offset by the amount of space we need for the stack. This
3258 // means we will avoid calling __morestack if there happens to
3259 // be plenty of space on the stack already.
3260 unsigned char* pval
= view
+ fnoffset
+ 3;
3261 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3262 val
-= parameters
->options().split_stack_adjust_size();
3263 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3267 if (!object
->has_no_split_stack())
3268 object
->error(_("failed to match split-stack sequence at "
3269 "section %u offset %0zx"),
3270 shndx
, static_cast<size_t>(fnoffset
));
3274 // We have to change the function so that it calls
3275 // __morestack_non_split instead of __morestack. The former will
3276 // allocate additional stack space.
3277 *from
= "__morestack";
3278 *to
= "__morestack_non_split";
3281 // The selector for i386 object files.
3283 class Target_selector_i386
: public Target_selector_freebsd
3286 Target_selector_i386()
3287 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
3288 "elf32-i386", "elf32-i386-freebsd")
3292 do_instantiate_target()
3293 { return new Target_i386(); }
3296 Target_selector_i386 target_selector_i386
;
3298 } // End anonymous namespace.