1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
29 #include "parameters.h"
36 #include "copy-relocs.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
49 // A class to handle the PLT data.
51 class Output_data_plt_i386
: public Output_section_data
54 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
56 Output_data_plt_i386(Layout
*, Output_data_space
*, Output_data_space
*);
58 // Add an entry to the PLT.
60 add_entry(Symbol_table
*, Layout
*, Symbol
* gsym
);
62 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
64 add_local_ifunc_entry(Symbol_table
*, Layout
*,
65 Sized_relobj_file
<32, false>* relobj
,
66 unsigned int local_sym_index
);
68 // Return the .rel.plt section data.
71 { return this->rel_
; }
73 // Return where the TLS_DESC relocations should go.
75 rel_tls_desc(Layout
*);
77 // Return where the IRELATIVE relocations should go.
79 rel_irelative(Symbol_table
*, Layout
*);
81 // Return whether we created a section for IRELATIVE relocations.
83 has_irelative_section() const
84 { return this->irelative_rel_
!= NULL
; }
86 // Return the number of PLT entries.
89 { return this->count_
+ this->irelative_count_
; }
91 // Return the offset of the first non-reserved PLT entry.
93 first_plt_entry_offset()
94 { return plt_entry_size
; }
96 // Return the size of a PLT entry.
99 { return plt_entry_size
; }
101 // Return the PLT address to use for a global symbol.
103 address_for_global(const Symbol
*);
105 // Return the PLT address to use for a local symbol.
107 address_for_local(const Relobj
*, unsigned int symndx
);
111 do_adjust_output_section(Output_section
* os
);
113 // Write to a map file.
115 do_print_to_mapfile(Mapfile
* mapfile
) const
116 { mapfile
->print_output_data(this, _("** PLT")); }
119 // The size of an entry in the PLT.
120 static const int plt_entry_size
= 16;
122 // The first entry in the PLT for an executable.
123 static const unsigned char exec_first_plt_entry
[plt_entry_size
];
125 // The first entry in the PLT for a shared object.
126 static const unsigned char dyn_first_plt_entry
[plt_entry_size
];
128 // Other entries in the PLT for an executable.
129 static const unsigned char exec_plt_entry
[plt_entry_size
];
131 // Other entries in the PLT for a shared object.
132 static const unsigned char dyn_plt_entry
[plt_entry_size
];
134 // The .eh_frame unwind information for the PLT.
135 static const int plt_eh_frame_cie_size
= 16;
136 static const int plt_eh_frame_fde_size
= 32;
137 static const unsigned char plt_eh_frame_cie
[plt_eh_frame_cie_size
];
138 static const unsigned char plt_eh_frame_fde
[plt_eh_frame_fde_size
];
140 // Set the final size.
142 set_final_data_size()
144 this->set_data_size((this->count_
+ this->irelative_count_
+ 1)
148 // Write out the PLT data.
150 do_write(Output_file
*);
152 // We keep a list of global STT_GNU_IFUNC symbols, each with its
153 // offset in the GOT.
157 unsigned int got_offset
;
160 // We keep a list of local STT_GNU_IFUNC symbols, each with its
161 // offset in the GOT.
164 Sized_relobj_file
<32, false>* object
;
165 unsigned int local_sym_index
;
166 unsigned int got_offset
;
169 // The reloc section.
171 // The TLS_DESC relocations, if necessary. These must follow the
172 // regular PLT relocs.
173 Reloc_section
* tls_desc_rel_
;
174 // The IRELATIVE relocations, if necessary. These must follow the
175 // regular relocatoins and the TLS_DESC relocations.
176 Reloc_section
* irelative_rel_
;
177 // The .got.plt section.
178 Output_data_space
* got_plt_
;
179 // The part of the .got.plt section used for IRELATIVE relocs.
180 Output_data_space
* got_irelative_
;
181 // The number of PLT entries.
183 // Number of PLT entries with R_386_IRELATIVE relocs. These follow
184 // the regular PLT entries.
185 unsigned int irelative_count_
;
186 // Global STT_GNU_IFUNC symbols.
187 std::vector
<Global_ifunc
> global_ifuncs_
;
188 // Local STT_GNU_IFUNC symbols.
189 std::vector
<Local_ifunc
> local_ifuncs_
;
192 // The i386 target class.
193 // TLS info comes from
194 // http://people.redhat.com/drepper/tls.pdf
195 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
197 class Target_i386
: public Sized_target
<32, false>
200 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
203 : Sized_target
<32, false>(&i386_info
),
204 got_(NULL
), plt_(NULL
), got_plt_(NULL
), got_irelative_(NULL
),
205 got_tlsdesc_(NULL
), global_offset_table_(NULL
), rel_dyn_(NULL
),
206 rel_irelative_(NULL
), copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
207 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
210 // Process the relocations to determine unreferenced sections for
211 // garbage collection.
213 gc_process_relocs(Symbol_table
* symtab
,
215 Sized_relobj_file
<32, false>* object
,
216 unsigned int data_shndx
,
217 unsigned int sh_type
,
218 const unsigned char* prelocs
,
220 Output_section
* output_section
,
221 bool needs_special_offset_handling
,
222 size_t local_symbol_count
,
223 const unsigned char* plocal_symbols
);
225 // Scan the relocations to look for symbol adjustments.
227 scan_relocs(Symbol_table
* symtab
,
229 Sized_relobj_file
<32, false>* object
,
230 unsigned int data_shndx
,
231 unsigned int sh_type
,
232 const unsigned char* prelocs
,
234 Output_section
* output_section
,
235 bool needs_special_offset_handling
,
236 size_t local_symbol_count
,
237 const unsigned char* plocal_symbols
);
239 // Finalize the sections.
241 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
243 // Return the value to use for a dynamic which requires special
246 do_dynsym_value(const Symbol
*) const;
248 // Relocate a section.
250 relocate_section(const Relocate_info
<32, false>*,
251 unsigned int sh_type
,
252 const unsigned char* prelocs
,
254 Output_section
* output_section
,
255 bool needs_special_offset_handling
,
257 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
258 section_size_type view_size
,
259 const Reloc_symbol_changes
*);
261 // Scan the relocs during a relocatable link.
263 scan_relocatable_relocs(Symbol_table
* symtab
,
265 Sized_relobj_file
<32, false>* object
,
266 unsigned int data_shndx
,
267 unsigned int sh_type
,
268 const unsigned char* prelocs
,
270 Output_section
* output_section
,
271 bool needs_special_offset_handling
,
272 size_t local_symbol_count
,
273 const unsigned char* plocal_symbols
,
274 Relocatable_relocs
*);
276 // Relocate a section during a relocatable link.
278 relocate_for_relocatable(const Relocate_info
<32, false>*,
279 unsigned int sh_type
,
280 const unsigned char* prelocs
,
282 Output_section
* output_section
,
283 off_t offset_in_output_section
,
284 const Relocatable_relocs
*,
286 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
287 section_size_type view_size
,
288 unsigned char* reloc_view
,
289 section_size_type reloc_view_size
);
291 // Return a string used to fill a code section with nops.
293 do_code_fill(section_size_type length
) const;
295 // Return whether SYM is defined by the ABI.
297 do_is_defined_by_abi(const Symbol
* sym
) const
298 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
300 // Return whether a symbol name implies a local label. The UnixWare
301 // 2.1 cc generates temporary symbols that start with .X, so we
302 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
303 // If so, we should move the .X recognition into
304 // Target::do_is_local_label_name.
306 do_is_local_label_name(const char* name
) const
308 if (name
[0] == '.' && name
[1] == 'X')
310 return Target::do_is_local_label_name(name
);
313 // Return the PLT address to use for a global symbol.
315 do_plt_address_for_global(const Symbol
* gsym
) const
316 { return this->plt_section()->address_for_global(gsym
); }
319 do_plt_address_for_local(const Relobj
* relobj
, unsigned int symndx
) const
320 { return this->plt_section()->address_for_local(relobj
, symndx
); }
322 // We can tell whether we take the address of a function.
324 do_can_check_for_function_pointers() const
327 // Return the base for a DW_EH_PE_datarel encoding.
329 do_ehframe_datarel_base() const;
331 // Return whether SYM is call to a non-split function.
333 do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const;
335 // Adjust -fsplit-stack code which calls non-split-stack code.
337 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
338 section_offset_type fnoffset
, section_size_type fnsize
,
339 unsigned char* view
, section_size_type view_size
,
340 std::string
* from
, std::string
* to
) const;
342 // Return the size of the GOT section.
346 gold_assert(this->got_
!= NULL
);
347 return this->got_
->data_size();
350 // Return the number of entries in the GOT.
352 got_entry_count() const
354 if (this->got_
== NULL
)
356 return this->got_size() / 4;
359 // Return the number of entries in the PLT.
361 plt_entry_count() const;
363 // Return the offset of the first non-reserved PLT entry.
365 first_plt_entry_offset() const;
367 // Return the size of each PLT entry.
369 plt_entry_size() const;
372 // The class which scans relocations.
377 get_reference_flags(unsigned int r_type
);
380 local(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
381 Sized_relobj_file
<32, false>* object
,
382 unsigned int data_shndx
,
383 Output_section
* output_section
,
384 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
385 const elfcpp::Sym
<32, false>& lsym
);
388 global(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
389 Sized_relobj_file
<32, false>* object
,
390 unsigned int data_shndx
,
391 Output_section
* output_section
,
392 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
396 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
398 Sized_relobj_file
<32, false>* object
,
399 unsigned int data_shndx
,
400 Output_section
* output_section
,
401 const elfcpp::Rel
<32, false>& reloc
,
403 const elfcpp::Sym
<32, false>& lsym
);
406 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
408 Sized_relobj_file
<32, false>* object
,
409 unsigned int data_shndx
,
410 Output_section
* output_section
,
411 const elfcpp::Rel
<32, false>& reloc
,
416 possible_function_pointer_reloc(unsigned int r_type
);
419 reloc_needs_plt_for_ifunc(Sized_relobj_file
<32, false>*,
420 unsigned int r_type
);
423 unsupported_reloc_local(Sized_relobj_file
<32, false>*, unsigned int r_type
);
426 unsupported_reloc_global(Sized_relobj_file
<32, false>*, unsigned int r_type
,
430 // The class which implements relocation.
435 : skip_call_tls_get_addr_(false),
436 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
441 if (this->skip_call_tls_get_addr_
)
443 // FIXME: This needs to specify the location somehow.
444 gold_error(_("missing expected TLS relocation"));
448 // Return whether the static relocation needs to be applied.
450 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
453 Output_section
* output_section
);
455 // Do a relocation. Return false if the caller should not issue
456 // any warnings about this relocation.
458 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
459 size_t relnum
, const elfcpp::Rel
<32, false>&,
460 unsigned int r_type
, const Sized_symbol
<32>*,
461 const Symbol_value
<32>*,
462 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
466 // Do a TLS relocation.
468 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
469 size_t relnum
, const elfcpp::Rel
<32, false>&,
470 unsigned int r_type
, const Sized_symbol
<32>*,
471 const Symbol_value
<32>*,
472 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
475 // Do a TLS General-Dynamic to Initial-Exec transition.
477 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
478 Output_segment
* tls_segment
,
479 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
480 elfcpp::Elf_types
<32>::Elf_Addr value
,
482 section_size_type view_size
);
484 // Do a TLS General-Dynamic to Local-Exec transition.
486 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
487 Output_segment
* tls_segment
,
488 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
489 elfcpp::Elf_types
<32>::Elf_Addr value
,
491 section_size_type view_size
);
493 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
496 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
497 Output_segment
* tls_segment
,
498 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
499 elfcpp::Elf_types
<32>::Elf_Addr value
,
501 section_size_type view_size
);
503 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
506 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
507 Output_segment
* tls_segment
,
508 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
509 elfcpp::Elf_types
<32>::Elf_Addr value
,
511 section_size_type view_size
);
513 // Do a TLS Local-Dynamic to Local-Exec transition.
515 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
516 Output_segment
* tls_segment
,
517 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
518 elfcpp::Elf_types
<32>::Elf_Addr value
,
520 section_size_type view_size
);
522 // Do a TLS Initial-Exec to Local-Exec transition.
524 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
525 Output_segment
* tls_segment
,
526 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
527 elfcpp::Elf_types
<32>::Elf_Addr value
,
529 section_size_type view_size
);
531 // We need to keep track of which type of local dynamic relocation
532 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
533 enum Local_dynamic_type
540 // This is set if we should skip the next reloc, which should be a
541 // PLT32 reloc against ___tls_get_addr.
542 bool skip_call_tls_get_addr_
;
543 // The type of local dynamic relocation we have seen in the section
544 // being relocated, if any.
545 Local_dynamic_type local_dynamic_type_
;
548 // A class which returns the size required for a relocation type,
549 // used while scanning relocs during a relocatable link.
550 class Relocatable_size_for_reloc
554 get_size_for_reloc(unsigned int, Relobj
*);
557 // Adjust TLS relocation type based on the options and whether this
558 // is a local symbol.
559 static tls::Tls_optimization
560 optimize_tls_reloc(bool is_final
, int r_type
);
562 // Get the GOT section, creating it if necessary.
563 Output_data_got
<32, false>*
564 got_section(Symbol_table
*, Layout
*);
566 // Get the GOT PLT section.
568 got_plt_section() const
570 gold_assert(this->got_plt_
!= NULL
);
571 return this->got_plt_
;
574 // Get the GOT section for TLSDESC entries.
575 Output_data_got
<32, false>*
576 got_tlsdesc_section() const
578 gold_assert(this->got_tlsdesc_
!= NULL
);
579 return this->got_tlsdesc_
;
582 // Create the PLT section.
584 make_plt_section(Symbol_table
* symtab
, Layout
* layout
);
586 // Create a PLT entry for a global symbol.
588 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
590 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
592 make_local_ifunc_plt_entry(Symbol_table
*, Layout
*,
593 Sized_relobj_file
<32, false>* relobj
,
594 unsigned int local_sym_index
);
596 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
598 define_tls_base_symbol(Symbol_table
*, Layout
*);
600 // Create a GOT entry for the TLS module index.
602 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
603 Sized_relobj_file
<32, false>* object
);
605 // Get the PLT section.
606 Output_data_plt_i386
*
609 gold_assert(this->plt_
!= NULL
);
613 // Get the dynamic reloc section, creating it if necessary.
615 rel_dyn_section(Layout
*);
617 // Get the section to use for TLS_DESC relocations.
619 rel_tls_desc_section(Layout
*) const;
621 // Get the section to use for IRELATIVE relocations.
623 rel_irelative_section(Layout
*);
625 // Add a potential copy relocation.
627 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
628 Sized_relobj_file
<32, false>* object
,
629 unsigned int shndx
, Output_section
* output_section
,
630 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
632 this->copy_relocs_
.copy_reloc(symtab
, layout
,
633 symtab
->get_sized_symbol
<32>(sym
),
634 object
, shndx
, output_section
, reloc
,
635 this->rel_dyn_section(layout
));
638 // Information about this specific target which we pass to the
639 // general Target structure.
640 static const Target::Target_info i386_info
;
642 // The types of GOT entries needed for this platform.
643 // These values are exposed to the ABI in an incremental link.
644 // Do not renumber existing values without changing the version
645 // number of the .gnu_incremental_inputs section.
648 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
649 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
650 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
651 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
652 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
656 Output_data_got
<32, false>* got_
;
658 Output_data_plt_i386
* plt_
;
659 // The GOT PLT section.
660 Output_data_space
* got_plt_
;
661 // The GOT section for IRELATIVE relocations.
662 Output_data_space
* got_irelative_
;
663 // The GOT section for TLSDESC relocations.
664 Output_data_got
<32, false>* got_tlsdesc_
;
665 // The _GLOBAL_OFFSET_TABLE_ symbol.
666 Symbol
* global_offset_table_
;
667 // The dynamic reloc section.
668 Reloc_section
* rel_dyn_
;
669 // The section to use for IRELATIVE relocs.
670 Reloc_section
* rel_irelative_
;
671 // Relocs saved to avoid a COPY reloc.
672 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
673 // Space for variables copied with a COPY reloc.
674 Output_data_space
* dynbss_
;
675 // Offset of the GOT entry for the TLS module index.
676 unsigned int got_mod_index_offset_
;
677 // True if the _TLS_MODULE_BASE_ symbol has been defined.
678 bool tls_base_symbol_defined_
;
681 const Target::Target_info
Target_i386::i386_info
=
684 false, // is_big_endian
685 elfcpp::EM_386
, // machine_code
686 false, // has_make_symbol
687 false, // has_resolve
688 true, // has_code_fill
689 true, // is_default_stack_executable
690 true, // can_icf_inline_merge_sections
692 "/usr/lib/libc.so.1", // dynamic_linker
693 0x08048000, // default_text_segment_address
694 0x1000, // abi_pagesize (overridable by -z max-page-size)
695 0x1000, // common_pagesize (overridable by -z common-page-size)
696 elfcpp::SHN_UNDEF
, // small_common_shndx
697 elfcpp::SHN_UNDEF
, // large_common_shndx
698 0, // small_common_section_flags
699 0, // large_common_section_flags
700 NULL
, // attributes_section
701 NULL
// attributes_vendor
704 // Get the GOT section, creating it if necessary.
706 Output_data_got
<32, false>*
707 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
709 if (this->got_
== NULL
)
711 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
713 this->got_
= new Output_data_got
<32, false>();
715 // When using -z now, we can treat .got.plt as a relro section.
716 // Without -z now, it is modified after program startup by lazy
718 bool is_got_plt_relro
= parameters
->options().now();
719 Output_section_order got_order
= (is_got_plt_relro
722 Output_section_order got_plt_order
= (is_got_plt_relro
724 : ORDER_NON_RELRO_FIRST
);
726 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
728 | elfcpp::SHF_WRITE
),
729 this->got_
, got_order
, true);
731 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
732 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
734 | elfcpp::SHF_WRITE
),
735 this->got_plt_
, got_plt_order
,
738 // The first three entries are reserved.
739 this->got_plt_
->set_current_data_size(3 * 4);
741 if (!is_got_plt_relro
)
743 // Those bytes can go into the relro segment.
744 layout
->increase_relro(3 * 4);
747 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
748 this->global_offset_table_
=
749 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
750 Symbol_table::PREDEFINED
,
752 0, 0, elfcpp::STT_OBJECT
,
754 elfcpp::STV_HIDDEN
, 0,
757 // If there are any IRELATIVE relocations, they get GOT entries
758 // in .got.plt after the jump slot relocations.
759 this->got_irelative_
= new Output_data_space(4, "** GOT IRELATIVE PLT");
760 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
762 | elfcpp::SHF_WRITE
),
763 this->got_irelative_
,
764 got_plt_order
, is_got_plt_relro
);
766 // If there are any TLSDESC relocations, they get GOT entries in
767 // .got.plt after the jump slot entries.
768 this->got_tlsdesc_
= new Output_data_got
<32, false>();
769 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
771 | elfcpp::SHF_WRITE
),
773 got_plt_order
, is_got_plt_relro
);
779 // Get the dynamic reloc section, creating it if necessary.
781 Target_i386::Reloc_section
*
782 Target_i386::rel_dyn_section(Layout
* layout
)
784 if (this->rel_dyn_
== NULL
)
786 gold_assert(layout
!= NULL
);
787 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
788 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
789 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
790 ORDER_DYNAMIC_RELOCS
, false);
792 return this->rel_dyn_
;
795 // Get the section to use for IRELATIVE relocs, creating it if
796 // necessary. These go in .rel.dyn, but only after all other dynamic
797 // relocations. They need to follow the other dynamic relocations so
798 // that they can refer to global variables initialized by those
801 Target_i386::Reloc_section
*
802 Target_i386::rel_irelative_section(Layout
* layout
)
804 if (this->rel_irelative_
== NULL
)
806 // Make sure we have already create the dynamic reloc section.
807 this->rel_dyn_section(layout
);
808 this->rel_irelative_
= new Reloc_section(false);
809 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
810 elfcpp::SHF_ALLOC
, this->rel_irelative_
,
811 ORDER_DYNAMIC_RELOCS
, false);
812 gold_assert(this->rel_dyn_
->output_section()
813 == this->rel_irelative_
->output_section());
815 return this->rel_irelative_
;
818 // Create the PLT section. The ordinary .got section is an argument,
819 // since we need to refer to the start. We also create our own .got
820 // section just for PLT entries.
822 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
823 Output_data_space
* got_plt
,
824 Output_data_space
* got_irelative
)
825 : Output_section_data(16), tls_desc_rel_(NULL
), irelative_rel_(NULL
),
826 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(0),
827 irelative_count_(0), global_ifuncs_(), local_ifuncs_()
829 this->rel_
= new Reloc_section(false);
830 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
831 elfcpp::SHF_ALLOC
, this->rel_
,
832 ORDER_DYNAMIC_PLT_RELOCS
, false);
834 // Add unwind information if requested.
835 if (parameters
->options().ld_generated_unwind_info())
836 layout
->add_eh_frame_for_plt(this, plt_eh_frame_cie
, plt_eh_frame_cie_size
,
837 plt_eh_frame_fde
, plt_eh_frame_fde_size
);
841 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
843 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
844 // linker, and so do we.
848 // Add an entry to the PLT.
851 Output_data_plt_i386::add_entry(Symbol_table
* symtab
, Layout
* layout
,
854 gold_assert(!gsym
->has_plt_offset());
856 // Every PLT entry needs a reloc.
857 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
858 && gsym
->can_use_relative_reloc(false))
860 gsym
->set_plt_offset(this->irelative_count_
* plt_entry_size
);
861 ++this->irelative_count_
;
862 section_offset_type got_offset
=
863 this->got_irelative_
->current_data_size();
864 this->got_irelative_
->set_current_data_size(got_offset
+ 4);
865 Reloc_section
* rel
= this->rel_irelative(symtab
, layout
);
866 rel
->add_symbolless_global_addend(gsym
, elfcpp::R_386_IRELATIVE
,
867 this->got_irelative_
, got_offset
);
868 struct Global_ifunc gi
;
870 gi
.got_offset
= got_offset
;
871 this->global_ifuncs_
.push_back(gi
);
875 // When setting the PLT offset we skip the initial reserved PLT
877 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
881 section_offset_type got_offset
= this->got_plt_
->current_data_size();
883 // Every PLT entry needs a GOT entry which points back to the
884 // PLT entry (this will be changed by the dynamic linker,
885 // normally lazily when the function is called).
886 this->got_plt_
->set_current_data_size(got_offset
+ 4);
888 gsym
->set_needs_dynsym_entry();
889 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
893 // Note that we don't need to save the symbol. The contents of the
894 // PLT are independent of which symbols are used. The symbols only
895 // appear in the relocations.
898 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
902 Output_data_plt_i386::add_local_ifunc_entry(
903 Symbol_table
* symtab
,
905 Sized_relobj_file
<32, false>* relobj
,
906 unsigned int local_sym_index
)
908 unsigned int plt_offset
= this->irelative_count_
* plt_entry_size
;
909 ++this->irelative_count_
;
911 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
913 // Every PLT entry needs a GOT entry which points back to the PLT
915 this->got_irelative_
->set_current_data_size(got_offset
+ 4);
917 // Every PLT entry needs a reloc.
918 Reloc_section
* rel
= this->rel_irelative(symtab
, layout
);
919 rel
->add_symbolless_local_addend(relobj
, local_sym_index
,
920 elfcpp::R_386_IRELATIVE
,
921 this->got_irelative_
, got_offset
);
923 struct Local_ifunc li
;
925 li
.local_sym_index
= local_sym_index
;
926 li
.got_offset
= got_offset
;
927 this->local_ifuncs_
.push_back(li
);
932 // Return where the TLS_DESC relocations should go, creating it if
933 // necessary. These follow the JUMP_SLOT relocations.
935 Output_data_plt_i386::Reloc_section
*
936 Output_data_plt_i386::rel_tls_desc(Layout
* layout
)
938 if (this->tls_desc_rel_
== NULL
)
940 this->tls_desc_rel_
= new Reloc_section(false);
941 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
942 elfcpp::SHF_ALLOC
, this->tls_desc_rel_
,
943 ORDER_DYNAMIC_PLT_RELOCS
, false);
944 gold_assert(this->tls_desc_rel_
->output_section()
945 == this->rel_
->output_section());
947 return this->tls_desc_rel_
;
950 // Return where the IRELATIVE relocations should go in the PLT. These
951 // follow the JUMP_SLOT and TLS_DESC relocations.
953 Output_data_plt_i386::Reloc_section
*
954 Output_data_plt_i386::rel_irelative(Symbol_table
* symtab
, Layout
* layout
)
956 if (this->irelative_rel_
== NULL
)
958 // Make sure we have a place for the TLS_DESC relocations, in
959 // case we see any later on.
960 this->rel_tls_desc(layout
);
961 this->irelative_rel_
= new Reloc_section(false);
962 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
963 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
964 ORDER_DYNAMIC_PLT_RELOCS
, false);
965 gold_assert(this->irelative_rel_
->output_section()
966 == this->rel_
->output_section());
968 if (parameters
->doing_static_link())
970 // A statically linked executable will only have a .rel.plt
971 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
972 // symbols. The library will use these symbols to locate
973 // the IRELATIVE relocs at program startup time.
974 symtab
->define_in_output_data("__rel_iplt_start", NULL
,
975 Symbol_table::PREDEFINED
,
976 this->irelative_rel_
, 0, 0,
977 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
978 elfcpp::STV_HIDDEN
, 0, false, true);
979 symtab
->define_in_output_data("__rel_iplt_end", NULL
,
980 Symbol_table::PREDEFINED
,
981 this->irelative_rel_
, 0, 0,
982 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
983 elfcpp::STV_HIDDEN
, 0, true, true);
986 return this->irelative_rel_
;
989 // Return the PLT address to use for a global symbol.
992 Output_data_plt_i386::address_for_global(const Symbol
* gsym
)
995 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
996 && gsym
->can_use_relative_reloc(false))
997 offset
= (this->count_
+ 1) * plt_entry_size
;
998 return this->address() + offset
;
1001 // Return the PLT address to use for a local symbol. These are always
1002 // IRELATIVE relocs.
1005 Output_data_plt_i386::address_for_local(const Relobj
*, unsigned int)
1007 return this->address() + (this->count_
+ 1) * plt_entry_size
;
1010 // The first entry in the PLT for an executable.
1012 const unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
1014 0xff, 0x35, // pushl contents of memory address
1015 0, 0, 0, 0, // replaced with address of .got + 4
1016 0xff, 0x25, // jmp indirect
1017 0, 0, 0, 0, // replaced with address of .got + 8
1018 0, 0, 0, 0 // unused
1021 // The first entry in the PLT for a shared object.
1023 const unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
1025 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1026 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1027 0, 0, 0, 0 // unused
1030 // Subsequent entries in the PLT for an executable.
1032 const unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
1034 0xff, 0x25, // jmp indirect
1035 0, 0, 0, 0, // replaced with address of symbol in .got
1036 0x68, // pushl immediate
1037 0, 0, 0, 0, // replaced with offset into relocation table
1038 0xe9, // jmp relative
1039 0, 0, 0, 0 // replaced with offset to start of .plt
1042 // Subsequent entries in the PLT for a shared object.
1044 const unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
1046 0xff, 0xa3, // jmp *offset(%ebx)
1047 0, 0, 0, 0, // replaced with offset of symbol in .got
1048 0x68, // pushl immediate
1049 0, 0, 0, 0, // replaced with offset into relocation table
1050 0xe9, // jmp relative
1051 0, 0, 0, 0 // replaced with offset to start of .plt
1054 // The .eh_frame unwind information for the PLT.
1057 Output_data_plt_i386::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1060 'z', // Augmentation: augmentation size included.
1061 'R', // Augmentation: FDE encoding included.
1062 '\0', // End of augmentation string.
1063 1, // Code alignment factor.
1064 0x7c, // Data alignment factor.
1065 8, // Return address column.
1066 1, // Augmentation size.
1067 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1068 | elfcpp::DW_EH_PE_sdata4
),
1069 elfcpp::DW_CFA_def_cfa
, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1070 elfcpp::DW_CFA_offset
+ 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1071 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1076 Output_data_plt_i386::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1078 0, 0, 0, 0, // Replaced with offset to .plt.
1079 0, 0, 0, 0, // Replaced with size of .plt.
1080 0, // Augmentation size.
1081 elfcpp::DW_CFA_def_cfa_offset
, 8, // DW_CFA_def_cfa_offset: 8.
1082 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1083 elfcpp::DW_CFA_def_cfa_offset
, 12, // DW_CFA_def_cfa_offset: 12.
1084 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1085 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1086 11, // Block length.
1087 elfcpp::DW_OP_breg4
, 4, // Push %esp + 4.
1088 elfcpp::DW_OP_breg8
, 0, // Push %eip.
1089 elfcpp::DW_OP_lit15
, // Push 0xf.
1090 elfcpp::DW_OP_and
, // & (%eip & 0xf).
1091 elfcpp::DW_OP_lit11
, // Push 0xb.
1092 elfcpp::DW_OP_ge
, // >= ((%eip & 0xf) >= 0xb)
1093 elfcpp::DW_OP_lit2
, // Push 2.
1094 elfcpp::DW_OP_shl
, // << (((%eip & 0xf) >= 0xb) << 2)
1095 elfcpp::DW_OP_plus
, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1096 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1102 // Write out the PLT. This uses the hand-coded instructions above,
1103 // and adjusts them as needed. This is all specified by the i386 ELF
1104 // Processor Supplement.
1107 Output_data_plt_i386::do_write(Output_file
* of
)
1109 const off_t offset
= this->offset();
1110 const section_size_type oview_size
=
1111 convert_to_section_size_type(this->data_size());
1112 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1114 const off_t got_file_offset
= this->got_plt_
->offset();
1115 gold_assert(parameters
->incremental_update()
1116 || (got_file_offset
+ this->got_plt_
->data_size()
1117 == this->got_irelative_
->offset()));
1118 const section_size_type got_size
=
1119 convert_to_section_size_type(this->got_plt_
->data_size()
1120 + this->got_irelative_
->data_size());
1121 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1124 unsigned char* pov
= oview
;
1126 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
1127 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
1129 if (parameters
->options().output_is_position_independent())
1130 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
1133 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
1134 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
1135 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
1137 pov
+= plt_entry_size
;
1139 unsigned char* got_pov
= got_view
;
1141 memset(got_pov
, 0, 12);
1144 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
1146 unsigned int plt_offset
= plt_entry_size
;
1147 unsigned int plt_rel_offset
= 0;
1148 unsigned int got_offset
= 12;
1149 const unsigned int count
= this->count_
+ this->irelative_count_
;
1150 for (unsigned int i
= 0;
1153 pov
+= plt_entry_size
,
1155 plt_offset
+= plt_entry_size
,
1156 plt_rel_offset
+= rel_size
,
1159 // Set and adjust the PLT entry itself.
1161 if (parameters
->options().output_is_position_independent())
1163 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
1164 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
1168 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
1169 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1174 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
1175 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1176 - (plt_offset
+ plt_entry_size
));
1178 // Set the entry in the GOT.
1179 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1182 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1183 // the GOT to point to the actual symbol value, rather than point to
1184 // the PLT entry. That will let the dynamic linker call the right
1185 // function when resolving IRELATIVE relocations.
1186 unsigned char* got_irelative_view
= got_view
+ this->got_plt_
->data_size();
1187 for (std::vector
<Global_ifunc
>::const_iterator p
=
1188 this->global_ifuncs_
.begin();
1189 p
!= this->global_ifuncs_
.end();
1192 const Sized_symbol
<32>* ssym
=
1193 static_cast<const Sized_symbol
<32>*>(p
->sym
);
1194 elfcpp::Swap
<32, false>::writeval(got_irelative_view
+ p
->got_offset
,
1198 for (std::vector
<Local_ifunc
>::const_iterator p
=
1199 this->local_ifuncs_
.begin();
1200 p
!= this->local_ifuncs_
.end();
1203 const Symbol_value
<32>* psymval
=
1204 p
->object
->local_symbol(p
->local_sym_index
);
1205 elfcpp::Swap
<32, false>::writeval(got_irelative_view
+ p
->got_offset
,
1206 psymval
->value(p
->object
, 0));
1209 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1210 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1212 of
->write_output_view(offset
, oview_size
, oview
);
1213 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1216 // Create the PLT section.
1219 Target_i386::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1221 if (this->plt_
== NULL
)
1223 // Create the GOT sections first.
1224 this->got_section(symtab
, layout
);
1226 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
,
1227 this->got_irelative_
);
1228 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1230 | elfcpp::SHF_EXECINSTR
),
1231 this->plt_
, ORDER_PLT
, false);
1233 // Make the sh_info field of .rel.plt point to .plt.
1234 Output_section
* rel_plt_os
= this->plt_
->rel_plt()->output_section();
1235 rel_plt_os
->set_info_section(this->plt_
->output_section());
1239 // Create a PLT entry for a global symbol.
1242 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
1244 if (gsym
->has_plt_offset())
1246 if (this->plt_
== NULL
)
1247 this->make_plt_section(symtab
, layout
);
1248 this->plt_
->add_entry(symtab
, layout
, gsym
);
1251 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1254 Target_i386::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1255 Sized_relobj_file
<32, false>* relobj
,
1256 unsigned int local_sym_index
)
1258 if (relobj
->local_has_plt_offset(local_sym_index
))
1260 if (this->plt_
== NULL
)
1261 this->make_plt_section(symtab
, layout
);
1262 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1265 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1268 // Return the number of entries in the PLT.
1271 Target_i386::plt_entry_count() const
1273 if (this->plt_
== NULL
)
1275 return this->plt_
->entry_count();
1278 // Return the offset of the first non-reserved PLT entry.
1281 Target_i386::first_plt_entry_offset() const
1283 return Output_data_plt_i386::first_plt_entry_offset();
1286 // Return the size of each PLT entry.
1289 Target_i386::plt_entry_size() const
1291 return Output_data_plt_i386::get_plt_entry_size();
1294 // Get the section to use for TLS_DESC relocations.
1296 Target_i386::Reloc_section
*
1297 Target_i386::rel_tls_desc_section(Layout
* layout
) const
1299 return this->plt_section()->rel_tls_desc(layout
);
1302 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1305 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1307 if (this->tls_base_symbol_defined_
)
1310 Output_segment
* tls_segment
= layout
->tls_segment();
1311 if (tls_segment
!= NULL
)
1313 bool is_exec
= parameters
->options().output_is_executable();
1314 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1315 Symbol_table::PREDEFINED
,
1319 elfcpp::STV_HIDDEN
, 0,
1321 ? Symbol::SEGMENT_END
1322 : Symbol::SEGMENT_START
),
1325 this->tls_base_symbol_defined_
= true;
1328 // Create a GOT entry for the TLS module index.
1331 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1332 Sized_relobj_file
<32, false>* object
)
1334 if (this->got_mod_index_offset_
== -1U)
1336 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1337 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1338 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
1339 unsigned int got_offset
= got
->add_constant(0);
1340 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
1342 got
->add_constant(0);
1343 this->got_mod_index_offset_
= got_offset
;
1345 return this->got_mod_index_offset_
;
1348 // Optimize the TLS relocation type based on what we know about the
1349 // symbol. IS_FINAL is true if the final address of this symbol is
1350 // known at link time.
1352 tls::Tls_optimization
1353 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
1355 // If we are generating a shared library, then we can't do anything
1357 if (parameters
->options().shared())
1358 return tls::TLSOPT_NONE
;
1362 case elfcpp::R_386_TLS_GD
:
1363 case elfcpp::R_386_TLS_GOTDESC
:
1364 case elfcpp::R_386_TLS_DESC_CALL
:
1365 // These are General-Dynamic which permits fully general TLS
1366 // access. Since we know that we are generating an executable,
1367 // we can convert this to Initial-Exec. If we also know that
1368 // this is a local symbol, we can further switch to Local-Exec.
1370 return tls::TLSOPT_TO_LE
;
1371 return tls::TLSOPT_TO_IE
;
1373 case elfcpp::R_386_TLS_LDM
:
1374 // This is Local-Dynamic, which refers to a local symbol in the
1375 // dynamic TLS block. Since we know that we generating an
1376 // executable, we can switch to Local-Exec.
1377 return tls::TLSOPT_TO_LE
;
1379 case elfcpp::R_386_TLS_LDO_32
:
1380 // Another type of Local-Dynamic relocation.
1381 return tls::TLSOPT_TO_LE
;
1383 case elfcpp::R_386_TLS_IE
:
1384 case elfcpp::R_386_TLS_GOTIE
:
1385 case elfcpp::R_386_TLS_IE_32
:
1386 // These are Initial-Exec relocs which get the thread offset
1387 // from the GOT. If we know that we are linking against the
1388 // local symbol, we can switch to Local-Exec, which links the
1389 // thread offset into the instruction.
1391 return tls::TLSOPT_TO_LE
;
1392 return tls::TLSOPT_NONE
;
1394 case elfcpp::R_386_TLS_LE
:
1395 case elfcpp::R_386_TLS_LE_32
:
1396 // When we already have Local-Exec, there is nothing further we
1398 return tls::TLSOPT_NONE
;
1405 // Get the Reference_flags for a particular relocation.
1408 Target_i386::Scan::get_reference_flags(unsigned int r_type
)
1412 case elfcpp::R_386_NONE
:
1413 case elfcpp::R_386_GNU_VTINHERIT
:
1414 case elfcpp::R_386_GNU_VTENTRY
:
1415 case elfcpp::R_386_GOTPC
:
1416 // No symbol reference.
1419 case elfcpp::R_386_32
:
1420 case elfcpp::R_386_16
:
1421 case elfcpp::R_386_8
:
1422 return Symbol::ABSOLUTE_REF
;
1424 case elfcpp::R_386_PC32
:
1425 case elfcpp::R_386_PC16
:
1426 case elfcpp::R_386_PC8
:
1427 case elfcpp::R_386_GOTOFF
:
1428 return Symbol::RELATIVE_REF
;
1430 case elfcpp::R_386_PLT32
:
1431 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1433 case elfcpp::R_386_GOT32
:
1435 return Symbol::ABSOLUTE_REF
;
1437 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1438 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1439 case elfcpp::R_386_TLS_DESC_CALL
:
1440 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1441 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1442 case elfcpp::R_386_TLS_IE
: // Initial-exec
1443 case elfcpp::R_386_TLS_IE_32
:
1444 case elfcpp::R_386_TLS_GOTIE
:
1445 case elfcpp::R_386_TLS_LE
: // Local-exec
1446 case elfcpp::R_386_TLS_LE_32
:
1447 return Symbol::TLS_REF
;
1449 case elfcpp::R_386_COPY
:
1450 case elfcpp::R_386_GLOB_DAT
:
1451 case elfcpp::R_386_JUMP_SLOT
:
1452 case elfcpp::R_386_RELATIVE
:
1453 case elfcpp::R_386_IRELATIVE
:
1454 case elfcpp::R_386_TLS_TPOFF
:
1455 case elfcpp::R_386_TLS_DTPMOD32
:
1456 case elfcpp::R_386_TLS_DTPOFF32
:
1457 case elfcpp::R_386_TLS_TPOFF32
:
1458 case elfcpp::R_386_TLS_DESC
:
1459 case elfcpp::R_386_32PLT
:
1460 case elfcpp::R_386_TLS_GD_32
:
1461 case elfcpp::R_386_TLS_GD_PUSH
:
1462 case elfcpp::R_386_TLS_GD_CALL
:
1463 case elfcpp::R_386_TLS_GD_POP
:
1464 case elfcpp::R_386_TLS_LDM_32
:
1465 case elfcpp::R_386_TLS_LDM_PUSH
:
1466 case elfcpp::R_386_TLS_LDM_CALL
:
1467 case elfcpp::R_386_TLS_LDM_POP
:
1468 case elfcpp::R_386_USED_BY_INTEL_200
:
1470 // Not expected. We will give an error later.
1475 // Report an unsupported relocation against a local symbol.
1478 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file
<32, false>* object
,
1479 unsigned int r_type
)
1481 gold_error(_("%s: unsupported reloc %u against local symbol"),
1482 object
->name().c_str(), r_type
);
1485 // Return whether we need to make a PLT entry for a relocation of a
1486 // given type against a STT_GNU_IFUNC symbol.
1489 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1490 Sized_relobj_file
<32, false>* object
,
1491 unsigned int r_type
)
1493 int flags
= Scan::get_reference_flags(r_type
);
1494 if (flags
& Symbol::TLS_REF
)
1495 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1496 object
->name().c_str(), r_type
);
1500 // Scan a relocation for a local symbol.
1503 Target_i386::Scan::local(Symbol_table
* symtab
,
1505 Target_i386
* target
,
1506 Sized_relobj_file
<32, false>* object
,
1507 unsigned int data_shndx
,
1508 Output_section
* output_section
,
1509 const elfcpp::Rel
<32, false>& reloc
,
1510 unsigned int r_type
,
1511 const elfcpp::Sym
<32, false>& lsym
)
1513 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1514 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1515 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1517 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1518 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1523 case elfcpp::R_386_NONE
:
1524 case elfcpp::R_386_GNU_VTINHERIT
:
1525 case elfcpp::R_386_GNU_VTENTRY
:
1528 case elfcpp::R_386_32
:
1529 // If building a shared library (or a position-independent
1530 // executable), we need to create a dynamic relocation for
1531 // this location. The relocation applied at link time will
1532 // apply the link-time value, so we flag the location with
1533 // an R_386_RELATIVE relocation so the dynamic loader can
1534 // relocate it easily.
1535 if (parameters
->options().output_is_position_independent())
1537 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1538 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1539 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
1540 output_section
, data_shndx
,
1541 reloc
.get_r_offset());
1545 case elfcpp::R_386_16
:
1546 case elfcpp::R_386_8
:
1547 // If building a shared library (or a position-independent
1548 // executable), we need to create a dynamic relocation for
1549 // this location. Because the addend needs to remain in the
1550 // data section, we need to be careful not to apply this
1551 // relocation statically.
1552 if (parameters
->options().output_is_position_independent())
1554 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1555 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1556 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1557 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1558 data_shndx
, reloc
.get_r_offset());
1561 gold_assert(lsym
.get_st_value() == 0);
1562 unsigned int shndx
= lsym
.get_st_shndx();
1564 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1567 object
->error(_("section symbol %u has bad shndx %u"),
1570 rel_dyn
->add_local_section(object
, shndx
,
1571 r_type
, output_section
,
1572 data_shndx
, reloc
.get_r_offset());
1577 case elfcpp::R_386_PC32
:
1578 case elfcpp::R_386_PC16
:
1579 case elfcpp::R_386_PC8
:
1582 case elfcpp::R_386_PLT32
:
1583 // Since we know this is a local symbol, we can handle this as a
1587 case elfcpp::R_386_GOTOFF
:
1588 case elfcpp::R_386_GOTPC
:
1589 // We need a GOT section.
1590 target
->got_section(symtab
, layout
);
1593 case elfcpp::R_386_GOT32
:
1595 // The symbol requires a GOT entry.
1596 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1597 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1599 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1600 // lets function pointers compare correctly with shared
1601 // libraries. Otherwise we would need an IRELATIVE reloc.
1603 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1604 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1606 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1609 // If we are generating a shared object, we need to add a
1610 // dynamic RELATIVE relocation for this symbol's GOT entry.
1611 if (parameters
->options().output_is_position_independent())
1613 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1614 unsigned int got_offset
=
1615 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1616 rel_dyn
->add_local_relative(object
, r_sym
,
1617 elfcpp::R_386_RELATIVE
,
1624 // These are relocations which should only be seen by the
1625 // dynamic linker, and should never be seen here.
1626 case elfcpp::R_386_COPY
:
1627 case elfcpp::R_386_GLOB_DAT
:
1628 case elfcpp::R_386_JUMP_SLOT
:
1629 case elfcpp::R_386_RELATIVE
:
1630 case elfcpp::R_386_IRELATIVE
:
1631 case elfcpp::R_386_TLS_TPOFF
:
1632 case elfcpp::R_386_TLS_DTPMOD32
:
1633 case elfcpp::R_386_TLS_DTPOFF32
:
1634 case elfcpp::R_386_TLS_TPOFF32
:
1635 case elfcpp::R_386_TLS_DESC
:
1636 gold_error(_("%s: unexpected reloc %u in object file"),
1637 object
->name().c_str(), r_type
);
1640 // These are initial TLS relocs, which are expected when
1642 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1643 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1644 case elfcpp::R_386_TLS_DESC_CALL
:
1645 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1646 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1647 case elfcpp::R_386_TLS_IE
: // Initial-exec
1648 case elfcpp::R_386_TLS_IE_32
:
1649 case elfcpp::R_386_TLS_GOTIE
:
1650 case elfcpp::R_386_TLS_LE
: // Local-exec
1651 case elfcpp::R_386_TLS_LE_32
:
1653 bool output_is_shared
= parameters
->options().shared();
1654 const tls::Tls_optimization optimized_type
1655 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1658 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1659 if (optimized_type
== tls::TLSOPT_NONE
)
1661 // Create a pair of GOT entries for the module index and
1662 // dtv-relative offset.
1663 Output_data_got
<32, false>* got
1664 = target
->got_section(symtab
, layout
);
1665 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1666 unsigned int shndx
= lsym
.get_st_shndx();
1668 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1670 object
->error(_("local symbol %u has bad shndx %u"),
1673 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1675 target
->rel_dyn_section(layout
),
1676 elfcpp::R_386_TLS_DTPMOD32
, 0);
1678 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1679 unsupported_reloc_local(object
, r_type
);
1682 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1683 target
->define_tls_base_symbol(symtab
, layout
);
1684 if (optimized_type
== tls::TLSOPT_NONE
)
1686 // Create a double GOT entry with an R_386_TLS_DESC
1687 // reloc. The R_386_TLS_DESC reloc is resolved
1688 // lazily, so the GOT entry needs to be in an area in
1689 // .got.plt, not .got. Call got_section to make sure
1690 // the section has been created.
1691 target
->got_section(symtab
, layout
);
1692 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1693 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1694 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1696 unsigned int got_offset
= got
->add_constant(0);
1697 // The local symbol value is stored in the second
1699 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_DESC
);
1700 // That set the GOT offset of the local symbol to
1701 // point to the second entry, but we want it to
1702 // point to the first.
1703 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1705 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1706 rt
->add_absolute(elfcpp::R_386_TLS_DESC
, got
, got_offset
);
1709 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1710 unsupported_reloc_local(object
, r_type
);
1713 case elfcpp::R_386_TLS_DESC_CALL
:
1716 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1717 if (optimized_type
== tls::TLSOPT_NONE
)
1719 // Create a GOT entry for the module index.
1720 target
->got_mod_index_entry(symtab
, layout
, object
);
1722 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1723 unsupported_reloc_local(object
, r_type
);
1726 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1729 case elfcpp::R_386_TLS_IE
: // Initial-exec
1730 case elfcpp::R_386_TLS_IE_32
:
1731 case elfcpp::R_386_TLS_GOTIE
:
1732 layout
->set_has_static_tls();
1733 if (optimized_type
== tls::TLSOPT_NONE
)
1735 // For the R_386_TLS_IE relocation, we need to create a
1736 // dynamic relocation when building a shared library.
1737 if (r_type
== elfcpp::R_386_TLS_IE
1738 && parameters
->options().shared())
1740 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1742 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1743 rel_dyn
->add_local_relative(object
, r_sym
,
1744 elfcpp::R_386_RELATIVE
,
1745 output_section
, data_shndx
,
1746 reloc
.get_r_offset());
1748 // Create a GOT entry for the tp-relative offset.
1749 Output_data_got
<32, false>* got
1750 = target
->got_section(symtab
, layout
);
1751 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1752 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1753 ? elfcpp::R_386_TLS_TPOFF32
1754 : elfcpp::R_386_TLS_TPOFF
);
1755 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1756 ? GOT_TYPE_TLS_OFFSET
1757 : GOT_TYPE_TLS_NOFFSET
);
1758 got
->add_local_with_rel(object
, r_sym
, got_type
,
1759 target
->rel_dyn_section(layout
),
1762 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1763 unsupported_reloc_local(object
, r_type
);
1766 case elfcpp::R_386_TLS_LE
: // Local-exec
1767 case elfcpp::R_386_TLS_LE_32
:
1768 layout
->set_has_static_tls();
1769 if (output_is_shared
)
1771 // We need to create a dynamic relocation.
1772 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1773 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1774 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1775 ? elfcpp::R_386_TLS_TPOFF32
1776 : elfcpp::R_386_TLS_TPOFF
);
1777 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1778 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1779 data_shndx
, reloc
.get_r_offset());
1789 case elfcpp::R_386_32PLT
:
1790 case elfcpp::R_386_TLS_GD_32
:
1791 case elfcpp::R_386_TLS_GD_PUSH
:
1792 case elfcpp::R_386_TLS_GD_CALL
:
1793 case elfcpp::R_386_TLS_GD_POP
:
1794 case elfcpp::R_386_TLS_LDM_32
:
1795 case elfcpp::R_386_TLS_LDM_PUSH
:
1796 case elfcpp::R_386_TLS_LDM_CALL
:
1797 case elfcpp::R_386_TLS_LDM_POP
:
1798 case elfcpp::R_386_USED_BY_INTEL_200
:
1800 unsupported_reloc_local(object
, r_type
);
1805 // Report an unsupported relocation against a global symbol.
1808 Target_i386::Scan::unsupported_reloc_global(
1809 Sized_relobj_file
<32, false>* object
,
1810 unsigned int r_type
,
1813 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1814 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1818 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1822 case elfcpp::R_386_32
:
1823 case elfcpp::R_386_16
:
1824 case elfcpp::R_386_8
:
1825 case elfcpp::R_386_GOTOFF
:
1826 case elfcpp::R_386_GOT32
:
1837 Target_i386::Scan::local_reloc_may_be_function_pointer(
1841 Sized_relobj_file
<32, false>* ,
1844 const elfcpp::Rel
<32, false>& ,
1845 unsigned int r_type
,
1846 const elfcpp::Sym
<32, false>&)
1848 return possible_function_pointer_reloc(r_type
);
1852 Target_i386::Scan::global_reloc_may_be_function_pointer(
1856 Sized_relobj_file
<32, false>* ,
1859 const elfcpp::Rel
<32, false>& ,
1860 unsigned int r_type
,
1863 return possible_function_pointer_reloc(r_type
);
1866 // Scan a relocation for a global symbol.
1869 Target_i386::Scan::global(Symbol_table
* symtab
,
1871 Target_i386
* target
,
1872 Sized_relobj_file
<32, false>* object
,
1873 unsigned int data_shndx
,
1874 Output_section
* output_section
,
1875 const elfcpp::Rel
<32, false>& reloc
,
1876 unsigned int r_type
,
1879 // A STT_GNU_IFUNC symbol may require a PLT entry.
1880 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1881 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1882 target
->make_plt_entry(symtab
, layout
, gsym
);
1886 case elfcpp::R_386_NONE
:
1887 case elfcpp::R_386_GNU_VTINHERIT
:
1888 case elfcpp::R_386_GNU_VTENTRY
:
1891 case elfcpp::R_386_32
:
1892 case elfcpp::R_386_16
:
1893 case elfcpp::R_386_8
:
1895 // Make a PLT entry if necessary.
1896 if (gsym
->needs_plt_entry())
1898 target
->make_plt_entry(symtab
, layout
, gsym
);
1899 // Since this is not a PC-relative relocation, we may be
1900 // taking the address of a function. In that case we need to
1901 // set the entry in the dynamic symbol table to the address of
1903 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1904 gsym
->set_needs_dynsym_value();
1906 // Make a dynamic relocation if necessary.
1907 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1909 if (gsym
->may_need_copy_reloc())
1911 target
->copy_reloc(symtab
, layout
, object
,
1912 data_shndx
, output_section
, gsym
, reloc
);
1914 else if (r_type
== elfcpp::R_386_32
1915 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1916 && gsym
->can_use_relative_reloc(false)
1917 && !gsym
->is_from_dynobj()
1918 && !gsym
->is_undefined()
1919 && !gsym
->is_preemptible())
1921 // Use an IRELATIVE reloc for a locally defined
1922 // STT_GNU_IFUNC symbol. This makes a function
1923 // address in a PIE executable match the address in a
1924 // shared library that it links against.
1925 Reloc_section
* rel_dyn
= target
->rel_irelative_section(layout
);
1926 rel_dyn
->add_symbolless_global_addend(gsym
,
1927 elfcpp::R_386_IRELATIVE
,
1930 reloc
.get_r_offset());
1932 else if (r_type
== elfcpp::R_386_32
1933 && gsym
->can_use_relative_reloc(false))
1935 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1936 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1937 output_section
, object
,
1938 data_shndx
, reloc
.get_r_offset());
1942 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1943 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1944 data_shndx
, reloc
.get_r_offset());
1950 case elfcpp::R_386_PC32
:
1951 case elfcpp::R_386_PC16
:
1952 case elfcpp::R_386_PC8
:
1954 // Make a PLT entry if necessary.
1955 if (gsym
->needs_plt_entry())
1957 // These relocations are used for function calls only in
1958 // non-PIC code. For a 32-bit relocation in a shared library,
1959 // we'll need a text relocation anyway, so we can skip the
1960 // PLT entry and let the dynamic linker bind the call directly
1961 // to the target. For smaller relocations, we should use a
1962 // PLT entry to ensure that the call can reach.
1963 if (!parameters
->options().shared()
1964 || r_type
!= elfcpp::R_386_PC32
)
1965 target
->make_plt_entry(symtab
, layout
, gsym
);
1967 // Make a dynamic relocation if necessary.
1968 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1970 if (gsym
->may_need_copy_reloc())
1972 target
->copy_reloc(symtab
, layout
, object
,
1973 data_shndx
, output_section
, gsym
, reloc
);
1977 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1978 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1979 data_shndx
, reloc
.get_r_offset());
1985 case elfcpp::R_386_GOT32
:
1987 // The symbol requires a GOT entry.
1988 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1989 if (gsym
->final_value_is_known())
1991 // For a STT_GNU_IFUNC symbol we want the PLT address.
1992 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1993 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1995 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1999 // If this symbol is not fully resolved, we need to add a
2000 // GOT entry with a dynamic relocation.
2001 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2003 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2005 // 1) The symbol may be defined in some other module.
2007 // 2) We are building a shared library and this is a
2008 // protected symbol; using GLOB_DAT means that the dynamic
2009 // linker can use the address of the PLT in the main
2010 // executable when appropriate so that function address
2011 // comparisons work.
2013 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2014 // code, again so that function address comparisons work.
2015 if (gsym
->is_from_dynobj()
2016 || gsym
->is_undefined()
2017 || gsym
->is_preemptible()
2018 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2019 && parameters
->options().shared())
2020 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2021 && parameters
->options().output_is_position_independent()))
2022 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
2023 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
2026 // For a STT_GNU_IFUNC symbol we want to write the PLT
2027 // offset into the GOT, so that function pointer
2028 // comparisons work correctly.
2030 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2031 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2034 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2035 // Tell the dynamic linker to use the PLT address
2036 // when resolving relocations.
2037 if (gsym
->is_from_dynobj()
2038 && !parameters
->options().shared())
2039 gsym
->set_needs_dynsym_value();
2043 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2044 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
2052 case elfcpp::R_386_PLT32
:
2053 // If the symbol is fully resolved, this is just a PC32 reloc.
2054 // Otherwise we need a PLT entry.
2055 if (gsym
->final_value_is_known())
2057 // If building a shared library, we can also skip the PLT entry
2058 // if the symbol is defined in the output file and is protected
2060 if (gsym
->is_defined()
2061 && !gsym
->is_from_dynobj()
2062 && !gsym
->is_preemptible())
2064 target
->make_plt_entry(symtab
, layout
, gsym
);
2067 case elfcpp::R_386_GOTOFF
:
2068 case elfcpp::R_386_GOTPC
:
2069 // We need a GOT section.
2070 target
->got_section(symtab
, layout
);
2073 // These are relocations which should only be seen by the
2074 // dynamic linker, and should never be seen here.
2075 case elfcpp::R_386_COPY
:
2076 case elfcpp::R_386_GLOB_DAT
:
2077 case elfcpp::R_386_JUMP_SLOT
:
2078 case elfcpp::R_386_RELATIVE
:
2079 case elfcpp::R_386_IRELATIVE
:
2080 case elfcpp::R_386_TLS_TPOFF
:
2081 case elfcpp::R_386_TLS_DTPMOD32
:
2082 case elfcpp::R_386_TLS_DTPOFF32
:
2083 case elfcpp::R_386_TLS_TPOFF32
:
2084 case elfcpp::R_386_TLS_DESC
:
2085 gold_error(_("%s: unexpected reloc %u in object file"),
2086 object
->name().c_str(), r_type
);
2089 // These are initial tls relocs, which are expected when
2091 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2092 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2093 case elfcpp::R_386_TLS_DESC_CALL
:
2094 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2095 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2096 case elfcpp::R_386_TLS_IE
: // Initial-exec
2097 case elfcpp::R_386_TLS_IE_32
:
2098 case elfcpp::R_386_TLS_GOTIE
:
2099 case elfcpp::R_386_TLS_LE
: // Local-exec
2100 case elfcpp::R_386_TLS_LE_32
:
2102 const bool is_final
= gsym
->final_value_is_known();
2103 const tls::Tls_optimization optimized_type
2104 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2107 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2108 if (optimized_type
== tls::TLSOPT_NONE
)
2110 // Create a pair of GOT entries for the module index and
2111 // dtv-relative offset.
2112 Output_data_got
<32, false>* got
2113 = target
->got_section(symtab
, layout
);
2114 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2115 target
->rel_dyn_section(layout
),
2116 elfcpp::R_386_TLS_DTPMOD32
,
2117 elfcpp::R_386_TLS_DTPOFF32
);
2119 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2121 // Create a GOT entry for the tp-relative offset.
2122 Output_data_got
<32, false>* got
2123 = target
->got_section(symtab
, layout
);
2124 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
2125 target
->rel_dyn_section(layout
),
2126 elfcpp::R_386_TLS_TPOFF
);
2128 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2129 unsupported_reloc_global(object
, r_type
, gsym
);
2132 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
2133 target
->define_tls_base_symbol(symtab
, layout
);
2134 if (optimized_type
== tls::TLSOPT_NONE
)
2136 // Create a double GOT entry with an R_386_TLS_DESC
2137 // reloc. The R_386_TLS_DESC reloc is resolved
2138 // lazily, so the GOT entry needs to be in an area in
2139 // .got.plt, not .got. Call got_section to make sure
2140 // the section has been created.
2141 target
->got_section(symtab
, layout
);
2142 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
2143 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
2144 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2145 elfcpp::R_386_TLS_DESC
, 0);
2147 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2149 // Create a GOT entry for the tp-relative offset.
2150 Output_data_got
<32, false>* got
2151 = target
->got_section(symtab
, layout
);
2152 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
2153 target
->rel_dyn_section(layout
),
2154 elfcpp::R_386_TLS_TPOFF
);
2156 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2157 unsupported_reloc_global(object
, r_type
, gsym
);
2160 case elfcpp::R_386_TLS_DESC_CALL
:
2163 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2164 if (optimized_type
== tls::TLSOPT_NONE
)
2166 // Create a GOT entry for the module index.
2167 target
->got_mod_index_entry(symtab
, layout
, object
);
2169 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2170 unsupported_reloc_global(object
, r_type
, gsym
);
2173 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2176 case elfcpp::R_386_TLS_IE
: // Initial-exec
2177 case elfcpp::R_386_TLS_IE_32
:
2178 case elfcpp::R_386_TLS_GOTIE
:
2179 layout
->set_has_static_tls();
2180 if (optimized_type
== tls::TLSOPT_NONE
)
2182 // For the R_386_TLS_IE relocation, we need to create a
2183 // dynamic relocation when building a shared library.
2184 if (r_type
== elfcpp::R_386_TLS_IE
2185 && parameters
->options().shared())
2187 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2188 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
2189 output_section
, object
,
2191 reloc
.get_r_offset());
2193 // Create a GOT entry for the tp-relative offset.
2194 Output_data_got
<32, false>* got
2195 = target
->got_section(symtab
, layout
);
2196 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2197 ? elfcpp::R_386_TLS_TPOFF32
2198 : elfcpp::R_386_TLS_TPOFF
);
2199 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2200 ? GOT_TYPE_TLS_OFFSET
2201 : GOT_TYPE_TLS_NOFFSET
);
2202 got
->add_global_with_rel(gsym
, got_type
,
2203 target
->rel_dyn_section(layout
),
2206 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2207 unsupported_reloc_global(object
, r_type
, gsym
);
2210 case elfcpp::R_386_TLS_LE
: // Local-exec
2211 case elfcpp::R_386_TLS_LE_32
:
2212 layout
->set_has_static_tls();
2213 if (parameters
->options().shared())
2215 // We need to create a dynamic relocation.
2216 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
2217 ? elfcpp::R_386_TLS_TPOFF32
2218 : elfcpp::R_386_TLS_TPOFF
);
2219 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2220 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
2221 data_shndx
, reloc
.get_r_offset());
2231 case elfcpp::R_386_32PLT
:
2232 case elfcpp::R_386_TLS_GD_32
:
2233 case elfcpp::R_386_TLS_GD_PUSH
:
2234 case elfcpp::R_386_TLS_GD_CALL
:
2235 case elfcpp::R_386_TLS_GD_POP
:
2236 case elfcpp::R_386_TLS_LDM_32
:
2237 case elfcpp::R_386_TLS_LDM_PUSH
:
2238 case elfcpp::R_386_TLS_LDM_CALL
:
2239 case elfcpp::R_386_TLS_LDM_POP
:
2240 case elfcpp::R_386_USED_BY_INTEL_200
:
2242 unsupported_reloc_global(object
, r_type
, gsym
);
2247 // Process relocations for gc.
2250 Target_i386::gc_process_relocs(Symbol_table
* symtab
,
2252 Sized_relobj_file
<32, false>* object
,
2253 unsigned int data_shndx
,
2255 const unsigned char* prelocs
,
2257 Output_section
* output_section
,
2258 bool needs_special_offset_handling
,
2259 size_t local_symbol_count
,
2260 const unsigned char* plocal_symbols
)
2262 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2264 Target_i386::Relocatable_size_for_reloc
>(
2273 needs_special_offset_handling
,
2278 // Scan relocations for a section.
2281 Target_i386::scan_relocs(Symbol_table
* symtab
,
2283 Sized_relobj_file
<32, false>* object
,
2284 unsigned int data_shndx
,
2285 unsigned int sh_type
,
2286 const unsigned char* prelocs
,
2288 Output_section
* output_section
,
2289 bool needs_special_offset_handling
,
2290 size_t local_symbol_count
,
2291 const unsigned char* plocal_symbols
)
2293 if (sh_type
== elfcpp::SHT_RELA
)
2295 gold_error(_("%s: unsupported RELA reloc section"),
2296 object
->name().c_str());
2300 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2310 needs_special_offset_handling
,
2315 // Finalize the sections.
2318 Target_i386::do_finalize_sections(
2320 const Input_objects
*,
2321 Symbol_table
* symtab
)
2323 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2325 : this->plt_
->rel_plt());
2326 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
2327 this->rel_dyn_
, true, false);
2329 // Emit any relocs we saved in an attempt to avoid generating COPY
2331 if (this->copy_relocs_
.any_saved_relocs())
2332 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
2334 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2335 // the .got.plt section.
2336 Symbol
* sym
= this->global_offset_table_
;
2339 uint32_t data_size
= this->got_plt_
->current_data_size();
2340 symtab
->get_sized_symbol
<32>(sym
)->set_symsize(data_size
);
2343 if (parameters
->doing_static_link()
2344 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
2346 // If linking statically, make sure that the __rel_iplt symbols
2347 // were defined if necessary, even if we didn't create a PLT.
2348 static const Define_symbol_in_segment syms
[] =
2351 "__rel_iplt_start", // name
2352 elfcpp::PT_LOAD
, // segment_type
2353 elfcpp::PF_W
, // segment_flags_set
2354 elfcpp::PF(0), // segment_flags_clear
2357 elfcpp::STT_NOTYPE
, // type
2358 elfcpp::STB_GLOBAL
, // binding
2359 elfcpp::STV_HIDDEN
, // visibility
2361 Symbol::SEGMENT_START
, // offset_from_base
2365 "__rel_iplt_end", // name
2366 elfcpp::PT_LOAD
, // segment_type
2367 elfcpp::PF_W
, // segment_flags_set
2368 elfcpp::PF(0), // segment_flags_clear
2371 elfcpp::STT_NOTYPE
, // type
2372 elfcpp::STB_GLOBAL
, // binding
2373 elfcpp::STV_HIDDEN
, // visibility
2375 Symbol::SEGMENT_START
, // offset_from_base
2380 symtab
->define_symbols(layout
, 2, syms
,
2381 layout
->script_options()->saw_sections_clause());
2385 // Return whether a direct absolute static relocation needs to be applied.
2386 // In cases where Scan::local() or Scan::global() has created
2387 // a dynamic relocation other than R_386_RELATIVE, the addend
2388 // of the relocation is carried in the data, and we must not
2389 // apply the static relocation.
2392 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2393 unsigned int r_type
,
2395 Output_section
* output_section
)
2397 // If the output section is not allocated, then we didn't call
2398 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2400 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
2403 int ref_flags
= Scan::get_reference_flags(r_type
);
2405 // For local symbols, we will have created a non-RELATIVE dynamic
2406 // relocation only if (a) the output is position independent,
2407 // (b) the relocation is absolute (not pc- or segment-relative), and
2408 // (c) the relocation is not 32 bits wide.
2410 return !(parameters
->options().output_is_position_independent()
2411 && (ref_flags
& Symbol::ABSOLUTE_REF
)
2414 // For global symbols, we use the same helper routines used in the
2415 // scan pass. If we did not create a dynamic relocation, or if we
2416 // created a RELATIVE dynamic relocation, we should apply the static
2418 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
2419 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
2420 && gsym
->can_use_relative_reloc(ref_flags
2421 & Symbol::FUNCTION_CALL
);
2422 return !has_dyn
|| is_rel
;
2425 // Perform a relocation.
2428 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
2429 Target_i386
* target
,
2430 Output_section
* output_section
,
2432 const elfcpp::Rel
<32, false>& rel
,
2433 unsigned int r_type
,
2434 const Sized_symbol
<32>* gsym
,
2435 const Symbol_value
<32>* psymval
,
2436 unsigned char* view
,
2437 elfcpp::Elf_types
<32>::Elf_Addr address
,
2438 section_size_type view_size
)
2440 if (this->skip_call_tls_get_addr_
)
2442 if ((r_type
!= elfcpp::R_386_PLT32
2443 && r_type
!= elfcpp::R_386_PC32
)
2445 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
2446 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2447 _("missing expected TLS relocation"));
2450 this->skip_call_tls_get_addr_
= false;
2455 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2457 // Pick the value to use for symbols defined in shared objects.
2458 Symbol_value
<32> symval
;
2460 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2461 && r_type
== elfcpp::R_386_32
2462 && gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
))
2463 && gsym
->can_use_relative_reloc(false)
2464 && !gsym
->is_from_dynobj()
2465 && !gsym
->is_undefined()
2466 && !gsym
->is_preemptible())
2468 // In this case we are generating a R_386_IRELATIVE reloc. We
2469 // want to use the real value of the symbol, not the PLT offset.
2471 else if (gsym
!= NULL
2472 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2474 symval
.set_output_value(target
->plt_address_for_global(gsym
)
2475 + gsym
->plt_offset());
2478 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2480 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2481 if (object
->local_has_plt_offset(r_sym
))
2483 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
)
2484 + object
->local_plt_offset(r_sym
));
2489 // Get the GOT offset if needed.
2490 // The GOT pointer points to the end of the GOT section.
2491 // We need to subtract the size of the GOT section to get
2492 // the actual offset to use in the relocation.
2493 bool have_got_offset
= false;
2494 unsigned int got_offset
= 0;
2497 case elfcpp::R_386_GOT32
:
2500 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2501 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
2502 - target
->got_size());
2506 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2507 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2508 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2509 - target
->got_size());
2511 have_got_offset
= true;
2520 case elfcpp::R_386_NONE
:
2521 case elfcpp::R_386_GNU_VTINHERIT
:
2522 case elfcpp::R_386_GNU_VTENTRY
:
2525 case elfcpp::R_386_32
:
2526 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2527 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
2530 case elfcpp::R_386_PC32
:
2531 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2532 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2535 case elfcpp::R_386_16
:
2536 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2537 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
2540 case elfcpp::R_386_PC16
:
2541 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2542 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
2545 case elfcpp::R_386_8
:
2546 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2547 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
2550 case elfcpp::R_386_PC8
:
2551 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2552 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
2555 case elfcpp::R_386_PLT32
:
2556 gold_assert(gsym
== NULL
2557 || gsym
->has_plt_offset()
2558 || gsym
->final_value_is_known()
2559 || (gsym
->is_defined()
2560 && !gsym
->is_from_dynobj()
2561 && !gsym
->is_preemptible()));
2562 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2565 case elfcpp::R_386_GOT32
:
2566 gold_assert(have_got_offset
);
2567 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2570 case elfcpp::R_386_GOTOFF
:
2572 elfcpp::Elf_types
<32>::Elf_Addr value
;
2573 value
= (psymval
->value(object
, 0)
2574 - target
->got_plt_section()->address());
2575 Relocate_functions
<32, false>::rel32(view
, value
);
2579 case elfcpp::R_386_GOTPC
:
2581 elfcpp::Elf_types
<32>::Elf_Addr value
;
2582 value
= target
->got_plt_section()->address();
2583 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
2587 case elfcpp::R_386_COPY
:
2588 case elfcpp::R_386_GLOB_DAT
:
2589 case elfcpp::R_386_JUMP_SLOT
:
2590 case elfcpp::R_386_RELATIVE
:
2591 case elfcpp::R_386_IRELATIVE
:
2592 // These are outstanding tls relocs, which are unexpected when
2594 case elfcpp::R_386_TLS_TPOFF
:
2595 case elfcpp::R_386_TLS_DTPMOD32
:
2596 case elfcpp::R_386_TLS_DTPOFF32
:
2597 case elfcpp::R_386_TLS_TPOFF32
:
2598 case elfcpp::R_386_TLS_DESC
:
2599 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2600 _("unexpected reloc %u in object file"),
2604 // These are initial tls relocs, which are expected when
2606 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2607 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2608 case elfcpp::R_386_TLS_DESC_CALL
:
2609 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2610 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2611 case elfcpp::R_386_TLS_IE
: // Initial-exec
2612 case elfcpp::R_386_TLS_IE_32
:
2613 case elfcpp::R_386_TLS_GOTIE
:
2614 case elfcpp::R_386_TLS_LE
: // Local-exec
2615 case elfcpp::R_386_TLS_LE_32
:
2616 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
2617 view
, address
, view_size
);
2620 case elfcpp::R_386_32PLT
:
2621 case elfcpp::R_386_TLS_GD_32
:
2622 case elfcpp::R_386_TLS_GD_PUSH
:
2623 case elfcpp::R_386_TLS_GD_CALL
:
2624 case elfcpp::R_386_TLS_GD_POP
:
2625 case elfcpp::R_386_TLS_LDM_32
:
2626 case elfcpp::R_386_TLS_LDM_PUSH
:
2627 case elfcpp::R_386_TLS_LDM_CALL
:
2628 case elfcpp::R_386_TLS_LDM_POP
:
2629 case elfcpp::R_386_USED_BY_INTEL_200
:
2631 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2632 _("unsupported reloc %u"),
2640 // Perform a TLS relocation.
2643 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
2644 Target_i386
* target
,
2646 const elfcpp::Rel
<32, false>& rel
,
2647 unsigned int r_type
,
2648 const Sized_symbol
<32>* gsym
,
2649 const Symbol_value
<32>* psymval
,
2650 unsigned char* view
,
2651 elfcpp::Elf_types
<32>::Elf_Addr
,
2652 section_size_type view_size
)
2654 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2656 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2658 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
2660 const bool is_final
= (gsym
== NULL
2661 ? !parameters
->options().shared()
2662 : gsym
->final_value_is_known());
2663 const tls::Tls_optimization optimized_type
2664 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2667 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2668 if (optimized_type
== tls::TLSOPT_TO_LE
)
2670 if (tls_segment
== NULL
)
2672 gold_assert(parameters
->errors()->error_count() > 0
2673 || issue_undefined_symbol_error(gsym
));
2676 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2677 rel
, r_type
, value
, view
,
2683 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2684 ? GOT_TYPE_TLS_NOFFSET
2685 : GOT_TYPE_TLS_PAIR
);
2686 unsigned int got_offset
;
2689 gold_assert(gsym
->has_got_offset(got_type
));
2690 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2694 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2695 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2696 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2697 - target
->got_size());
2699 if (optimized_type
== tls::TLSOPT_TO_IE
)
2701 if (tls_segment
== NULL
)
2703 gold_assert(parameters
->errors()->error_count() > 0
2704 || issue_undefined_symbol_error(gsym
));
2707 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2708 got_offset
, view
, view_size
);
2711 else if (optimized_type
== tls::TLSOPT_NONE
)
2713 // Relocate the field with the offset of the pair of GOT
2715 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2719 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2720 _("unsupported reloc %u"),
2724 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2725 case elfcpp::R_386_TLS_DESC_CALL
:
2726 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2727 if (optimized_type
== tls::TLSOPT_TO_LE
)
2729 if (tls_segment
== NULL
)
2731 gold_assert(parameters
->errors()->error_count() > 0
2732 || issue_undefined_symbol_error(gsym
));
2735 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2736 rel
, r_type
, value
, view
,
2742 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2743 ? GOT_TYPE_TLS_NOFFSET
2744 : GOT_TYPE_TLS_DESC
);
2745 unsigned int got_offset
= 0;
2746 if (r_type
== elfcpp::R_386_TLS_GOTDESC
2747 && optimized_type
== tls::TLSOPT_NONE
)
2749 // We created GOT entries in the .got.tlsdesc portion of
2750 // the .got.plt section, but the offset stored in the
2751 // symbol is the offset within .got.tlsdesc.
2752 got_offset
= (target
->got_size()
2753 + target
->got_plt_section()->data_size());
2757 gold_assert(gsym
->has_got_offset(got_type
));
2758 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2762 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2763 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2764 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2765 - target
->got_size());
2767 if (optimized_type
== tls::TLSOPT_TO_IE
)
2769 if (tls_segment
== NULL
)
2771 gold_assert(parameters
->errors()->error_count() > 0
2772 || issue_undefined_symbol_error(gsym
));
2775 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2776 got_offset
, view
, view_size
);
2779 else if (optimized_type
== tls::TLSOPT_NONE
)
2781 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2783 // Relocate the field with the offset of the pair of GOT
2785 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2790 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2791 _("unsupported reloc %u"),
2795 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2796 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
2798 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2799 _("both SUN and GNU model "
2800 "TLS relocations"));
2803 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2804 if (optimized_type
== tls::TLSOPT_TO_LE
)
2806 if (tls_segment
== NULL
)
2808 gold_assert(parameters
->errors()->error_count() > 0
2809 || issue_undefined_symbol_error(gsym
));
2812 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2813 value
, view
, view_size
);
2816 else if (optimized_type
== tls::TLSOPT_NONE
)
2818 // Relocate the field with the offset of the GOT entry for
2819 // the module index.
2820 unsigned int got_offset
;
2821 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2822 - target
->got_size());
2823 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2826 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2827 _("unsupported reloc %u"),
2831 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2832 if (optimized_type
== tls::TLSOPT_TO_LE
)
2834 // This reloc can appear in debugging sections, in which
2835 // case we must not convert to local-exec. We decide what
2836 // to do based on whether the section is marked as
2837 // containing executable code. That is what the GNU linker
2839 elfcpp::Shdr
<32, false> shdr(relinfo
->data_shdr
);
2840 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
2842 if (tls_segment
== NULL
)
2844 gold_assert(parameters
->errors()->error_count() > 0
2845 || issue_undefined_symbol_error(gsym
));
2848 value
-= tls_segment
->memsz();
2851 Relocate_functions
<32, false>::rel32(view
, value
);
2854 case elfcpp::R_386_TLS_IE
: // Initial-exec
2855 case elfcpp::R_386_TLS_GOTIE
:
2856 case elfcpp::R_386_TLS_IE_32
:
2857 if (optimized_type
== tls::TLSOPT_TO_LE
)
2859 if (tls_segment
== NULL
)
2861 gold_assert(parameters
->errors()->error_count() > 0
2862 || issue_undefined_symbol_error(gsym
));
2865 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2866 rel
, r_type
, value
, view
,
2870 else if (optimized_type
== tls::TLSOPT_NONE
)
2872 // Relocate the field with the offset of the GOT entry for
2873 // the tp-relative offset of the symbol.
2874 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2875 ? GOT_TYPE_TLS_OFFSET
2876 : GOT_TYPE_TLS_NOFFSET
);
2877 unsigned int got_offset
;
2880 gold_assert(gsym
->has_got_offset(got_type
));
2881 got_offset
= gsym
->got_offset(got_type
);
2885 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2886 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2887 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2889 // For the R_386_TLS_IE relocation, we need to apply the
2890 // absolute address of the GOT entry.
2891 if (r_type
== elfcpp::R_386_TLS_IE
)
2892 got_offset
+= target
->got_plt_section()->address();
2893 // All GOT offsets are relative to the end of the GOT.
2894 got_offset
-= target
->got_size();
2895 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2898 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2899 _("unsupported reloc %u"),
2903 case elfcpp::R_386_TLS_LE
: // Local-exec
2904 // If we're creating a shared library, a dynamic relocation will
2905 // have been created for this location, so do not apply it now.
2906 if (!parameters
->options().shared())
2908 if (tls_segment
== NULL
)
2910 gold_assert(parameters
->errors()->error_count() > 0
2911 || issue_undefined_symbol_error(gsym
));
2914 value
-= tls_segment
->memsz();
2915 Relocate_functions
<32, false>::rel32(view
, value
);
2919 case elfcpp::R_386_TLS_LE_32
:
2920 // If we're creating a shared library, a dynamic relocation will
2921 // have been created for this location, so do not apply it now.
2922 if (!parameters
->options().shared())
2924 if (tls_segment
== NULL
)
2926 gold_assert(parameters
->errors()->error_count() > 0
2927 || issue_undefined_symbol_error(gsym
));
2930 value
= tls_segment
->memsz() - value
;
2931 Relocate_functions
<32, false>::rel32(view
, value
);
2937 // Do a relocation in which we convert a TLS General-Dynamic to a
2941 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2943 Output_segment
* tls_segment
,
2944 const elfcpp::Rel
<32, false>& rel
,
2946 elfcpp::Elf_types
<32>::Elf_Addr value
,
2947 unsigned char* view
,
2948 section_size_type view_size
)
2950 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2951 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2952 // leal foo(%reg),%eax; call ___tls_get_addr
2953 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2955 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2956 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2958 unsigned char op1
= view
[-1];
2959 unsigned char op2
= view
[-2];
2961 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2962 op2
== 0x8d || op2
== 0x04);
2963 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2969 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2970 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2971 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2972 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2973 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2977 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2978 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2979 if (rel
.get_r_offset() + 9 < view_size
2982 // There is a trailing nop. Use the size byte subl.
2983 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2988 // Use the five byte subl.
2989 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2993 value
= tls_segment
->memsz() - value
;
2994 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2996 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2998 this->skip_call_tls_get_addr_
= true;
3001 // Do a relocation in which we convert a TLS General-Dynamic to an
3005 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
3008 const elfcpp::Rel
<32, false>& rel
,
3010 elfcpp::Elf_types
<32>::Elf_Addr value
,
3011 unsigned char* view
,
3012 section_size_type view_size
)
3014 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3015 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3017 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3018 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
3020 unsigned char op1
= view
[-1];
3021 unsigned char op2
= view
[-2];
3023 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3024 op2
== 0x8d || op2
== 0x04);
3025 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
3029 // FIXME: For now, support only the first (SIB) form.
3030 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
3034 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
3035 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
3036 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3037 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
3038 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3042 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3043 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
3044 if (rel
.get_r_offset() + 9 < view_size
3047 // FIXME: This is not the right instruction sequence.
3048 // There is a trailing nop. Use the size byte subl.
3049 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3054 // FIXME: This is not the right instruction sequence.
3055 // Use the five byte subl.
3056 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3060 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
3062 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3064 this->skip_call_tls_get_addr_
= true;
3067 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3068 // General-Dynamic to a Local-Exec.
3071 Target_i386::Relocate::tls_desc_gd_to_le(
3072 const Relocate_info
<32, false>* relinfo
,
3074 Output_segment
* tls_segment
,
3075 const elfcpp::Rel
<32, false>& rel
,
3076 unsigned int r_type
,
3077 elfcpp::Elf_types
<32>::Elf_Addr value
,
3078 unsigned char* view
,
3079 section_size_type view_size
)
3081 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
3083 // leal foo@TLSDESC(%ebx), %eax
3084 // ==> leal foo@NTPOFF, %eax
3085 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3086 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3087 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3088 view
[-2] == 0x8d && view
[-1] == 0x83);
3090 value
-= tls_segment
->memsz();
3091 Relocate_functions
<32, false>::rel32(view
, value
);
3095 // call *foo@TLSCALL(%eax)
3097 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
3098 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
3099 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3100 view
[0] == 0xff && view
[1] == 0x10);
3106 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3107 // General-Dynamic to an Initial-Exec.
3110 Target_i386::Relocate::tls_desc_gd_to_ie(
3111 const Relocate_info
<32, false>* relinfo
,
3114 const elfcpp::Rel
<32, false>& rel
,
3115 unsigned int r_type
,
3116 elfcpp::Elf_types
<32>::Elf_Addr value
,
3117 unsigned char* view
,
3118 section_size_type view_size
)
3120 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
3122 // leal foo@TLSDESC(%ebx), %eax
3123 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3124 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3125 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3126 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3127 view
[-2] == 0x8d && view
[-1] == 0x83);
3129 Relocate_functions
<32, false>::rel32(view
, value
);
3133 // call *foo@TLSCALL(%eax)
3135 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
3136 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
3137 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3138 view
[0] == 0xff && view
[1] == 0x10);
3144 // Do a relocation in which we convert a TLS Local-Dynamic to a
3148 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
3151 const elfcpp::Rel
<32, false>& rel
,
3153 elfcpp::Elf_types
<32>::Elf_Addr
,
3154 unsigned char* view
,
3155 section_size_type view_size
)
3157 // leal foo(%reg), %eax; call ___tls_get_addr
3158 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3160 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3161 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
3163 // FIXME: Does this test really always pass?
3164 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3165 view
[-2] == 0x8d && view
[-1] == 0x83);
3167 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
3169 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3171 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3173 this->skip_call_tls_get_addr_
= true;
3176 // Do a relocation in which we convert a TLS Initial-Exec to a
3180 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
3182 Output_segment
* tls_segment
,
3183 const elfcpp::Rel
<32, false>& rel
,
3184 unsigned int r_type
,
3185 elfcpp::Elf_types
<32>::Elf_Addr value
,
3186 unsigned char* view
,
3187 section_size_type view_size
)
3189 // We have to actually change the instructions, which means that we
3190 // need to examine the opcodes to figure out which instruction we
3192 if (r_type
== elfcpp::R_386_TLS_IE
)
3194 // movl %gs:XX,%eax ==> movl $YY,%eax
3195 // movl %gs:XX,%reg ==> movl $YY,%reg
3196 // addl %gs:XX,%reg ==> addl $YY,%reg
3197 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
3198 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3200 unsigned char op1
= view
[-1];
3203 // movl XX,%eax ==> movl $YY,%eax
3208 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3210 unsigned char op2
= view
[-2];
3213 // movl XX,%reg ==> movl $YY,%reg
3214 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3215 (op1
& 0xc7) == 0x05);
3217 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3219 else if (op2
== 0x03)
3221 // addl XX,%reg ==> addl $YY,%reg
3222 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3223 (op1
& 0xc7) == 0x05);
3225 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3228 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
3233 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3234 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3235 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3236 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3237 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3239 unsigned char op1
= view
[-1];
3240 unsigned char op2
= view
[-2];
3241 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3242 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
3245 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3247 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3249 else if (op2
== 0x2b)
3251 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3253 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
3255 else if (op2
== 0x03)
3257 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3259 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3262 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
3265 value
= tls_segment
->memsz() - value
;
3266 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
3269 Relocate_functions
<32, false>::rel32(view
, value
);
3272 // Relocate section data.
3275 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
3276 unsigned int sh_type
,
3277 const unsigned char* prelocs
,
3279 Output_section
* output_section
,
3280 bool needs_special_offset_handling
,
3281 unsigned char* view
,
3282 elfcpp::Elf_types
<32>::Elf_Addr address
,
3283 section_size_type view_size
,
3284 const Reloc_symbol_changes
* reloc_symbol_changes
)
3286 gold_assert(sh_type
== elfcpp::SHT_REL
);
3288 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
3289 Target_i386::Relocate
>(
3295 needs_special_offset_handling
,
3299 reloc_symbol_changes
);
3302 // Return the size of a relocation while scanning during a relocatable
3306 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3307 unsigned int r_type
,
3312 case elfcpp::R_386_NONE
:
3313 case elfcpp::R_386_GNU_VTINHERIT
:
3314 case elfcpp::R_386_GNU_VTENTRY
:
3315 case elfcpp::R_386_TLS_GD
: // Global-dynamic
3316 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
3317 case elfcpp::R_386_TLS_DESC_CALL
:
3318 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
3319 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
3320 case elfcpp::R_386_TLS_IE
: // Initial-exec
3321 case elfcpp::R_386_TLS_IE_32
:
3322 case elfcpp::R_386_TLS_GOTIE
:
3323 case elfcpp::R_386_TLS_LE
: // Local-exec
3324 case elfcpp::R_386_TLS_LE_32
:
3327 case elfcpp::R_386_32
:
3328 case elfcpp::R_386_PC32
:
3329 case elfcpp::R_386_GOT32
:
3330 case elfcpp::R_386_PLT32
:
3331 case elfcpp::R_386_GOTOFF
:
3332 case elfcpp::R_386_GOTPC
:
3335 case elfcpp::R_386_16
:
3336 case elfcpp::R_386_PC16
:
3339 case elfcpp::R_386_8
:
3340 case elfcpp::R_386_PC8
:
3343 // These are relocations which should only be seen by the
3344 // dynamic linker, and should never be seen here.
3345 case elfcpp::R_386_COPY
:
3346 case elfcpp::R_386_GLOB_DAT
:
3347 case elfcpp::R_386_JUMP_SLOT
:
3348 case elfcpp::R_386_RELATIVE
:
3349 case elfcpp::R_386_IRELATIVE
:
3350 case elfcpp::R_386_TLS_TPOFF
:
3351 case elfcpp::R_386_TLS_DTPMOD32
:
3352 case elfcpp::R_386_TLS_DTPOFF32
:
3353 case elfcpp::R_386_TLS_TPOFF32
:
3354 case elfcpp::R_386_TLS_DESC
:
3355 object
->error(_("unexpected reloc %u in object file"), r_type
);
3358 case elfcpp::R_386_32PLT
:
3359 case elfcpp::R_386_TLS_GD_32
:
3360 case elfcpp::R_386_TLS_GD_PUSH
:
3361 case elfcpp::R_386_TLS_GD_CALL
:
3362 case elfcpp::R_386_TLS_GD_POP
:
3363 case elfcpp::R_386_TLS_LDM_32
:
3364 case elfcpp::R_386_TLS_LDM_PUSH
:
3365 case elfcpp::R_386_TLS_LDM_CALL
:
3366 case elfcpp::R_386_TLS_LDM_POP
:
3367 case elfcpp::R_386_USED_BY_INTEL_200
:
3369 object
->error(_("unsupported reloc %u in object file"), r_type
);
3374 // Scan the relocs during a relocatable link.
3377 Target_i386::scan_relocatable_relocs(Symbol_table
* symtab
,
3379 Sized_relobj_file
<32, false>* object
,
3380 unsigned int data_shndx
,
3381 unsigned int sh_type
,
3382 const unsigned char* prelocs
,
3384 Output_section
* output_section
,
3385 bool needs_special_offset_handling
,
3386 size_t local_symbol_count
,
3387 const unsigned char* plocal_symbols
,
3388 Relocatable_relocs
* rr
)
3390 gold_assert(sh_type
== elfcpp::SHT_REL
);
3392 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
3393 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3395 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
3396 Scan_relocatable_relocs
>(
3404 needs_special_offset_handling
,
3410 // Relocate a section during a relocatable link.
3413 Target_i386::relocate_for_relocatable(
3414 const Relocate_info
<32, false>* relinfo
,
3415 unsigned int sh_type
,
3416 const unsigned char* prelocs
,
3418 Output_section
* output_section
,
3419 off_t offset_in_output_section
,
3420 const Relocatable_relocs
* rr
,
3421 unsigned char* view
,
3422 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
3423 section_size_type view_size
,
3424 unsigned char* reloc_view
,
3425 section_size_type reloc_view_size
)
3427 gold_assert(sh_type
== elfcpp::SHT_REL
);
3429 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
3434 offset_in_output_section
,
3443 // Return the value to use for a dynamic which requires special
3444 // treatment. This is how we support equality comparisons of function
3445 // pointers across shared library boundaries, as described in the
3446 // processor specific ABI supplement.
3449 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
3451 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3452 return this->plt_address_for_global(gsym
) + gsym
->plt_offset();
3455 // Return a string used to fill a code section with nops to take up
3456 // the specified length.
3459 Target_i386::do_code_fill(section_size_type length
) const
3463 // Build a jmp instruction to skip over the bytes.
3464 unsigned char jmp
[5];
3466 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3467 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3468 + std::string(length
- 5, '\0'));
3471 // Nop sequences of various lengths.
3472 const char nop1
[1] = { 0x90 }; // nop
3473 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3474 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3475 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3476 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3477 0x00 }; // leal 0(%esi,1),%esi
3478 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3480 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3482 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3483 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3484 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3485 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3487 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3488 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3490 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3491 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3493 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3494 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3495 0x00, 0x00, 0x00, 0x00 };
3496 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3497 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3498 0x27, 0x00, 0x00, 0x00,
3500 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3501 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3502 0xbc, 0x27, 0x00, 0x00,
3504 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3505 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3506 0x90, 0x90, 0x90, 0x90,
3509 const char* nops
[16] = {
3511 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3512 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3515 return std::string(nops
[length
], length
);
3518 // Return the value to use for the base of a DW_EH_PE_datarel offset
3519 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3520 // assembler can not write out the difference between two labels in
3521 // different sections, so instead of using a pc-relative value they
3522 // use an offset from the GOT.
3525 Target_i386::do_ehframe_datarel_base() const
3527 gold_assert(this->global_offset_table_
!= NULL
);
3528 Symbol
* sym
= this->global_offset_table_
;
3529 Sized_symbol
<32>* ssym
= static_cast<Sized_symbol
<32>*>(sym
);
3530 return ssym
->value();
3533 // Return whether SYM should be treated as a call to a non-split
3534 // function. We don't want that to be true of a call to a
3535 // get_pc_thunk function.
3538 Target_i386::do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const
3540 return (sym
->type() == elfcpp::STT_FUNC
3541 && !is_prefix_of("__i686.get_pc_thunk.", sym
->name()));
3544 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3545 // compiled with -fsplit-stack. The function calls non-split-stack
3546 // code. We have to change the function so that it always ensures
3547 // that it has enough stack space to run some random function.
3550 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3551 section_offset_type fnoffset
,
3552 section_size_type fnsize
,
3553 unsigned char* view
,
3554 section_size_type view_size
,
3556 std::string
* to
) const
3558 // The function starts with a comparison of the stack pointer and a
3559 // field in the TCB. This is followed by a jump.
3562 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
3565 // We will call __morestack if the carry flag is set after this
3566 // comparison. We turn the comparison into an stc instruction
3568 view
[fnoffset
] = '\xf9';
3569 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
3571 // lea NN(%esp),%ecx
3572 // lea NN(%esp),%edx
3573 else if ((this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
3574 || this->match_view(view
, view_size
, fnoffset
, "\x8d\x94\x24", 3))
3577 // This is loading an offset from the stack pointer for a
3578 // comparison. The offset is negative, so we decrease the
3579 // offset by the amount of space we need for the stack. This
3580 // means we will avoid calling __morestack if there happens to
3581 // be plenty of space on the stack already.
3582 unsigned char* pval
= view
+ fnoffset
+ 3;
3583 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3584 val
-= parameters
->options().split_stack_adjust_size();
3585 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3589 if (!object
->has_no_split_stack())
3590 object
->error(_("failed to match split-stack sequence at "
3591 "section %u offset %0zx"),
3592 shndx
, static_cast<size_t>(fnoffset
));
3596 // We have to change the function so that it calls
3597 // __morestack_non_split instead of __morestack. The former will
3598 // allocate additional stack space.
3599 *from
= "__morestack";
3600 *to
= "__morestack_non_split";
3603 // The selector for i386 object files.
3605 class Target_selector_i386
: public Target_selector_freebsd
3608 Target_selector_i386()
3609 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
3610 "elf32-i386", "elf32-i386-freebsd",
3615 do_instantiate_target()
3616 { return new Target_i386(); }
3619 Target_selector_i386 target_selector_i386
;
3621 } // End anonymous namespace.