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 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
717 | elfcpp::SHF_WRITE
),
718 this->got_
, ORDER_RELRO_LAST
, true);
720 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
721 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
723 | elfcpp::SHF_WRITE
),
724 this->got_plt_
, ORDER_NON_RELRO_FIRST
,
727 // The first three entries are reserved.
728 this->got_plt_
->set_current_data_size(3 * 4);
730 // Those bytes can go into the relro segment.
731 layout
->increase_relro(3 * 4);
733 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
734 this->global_offset_table_
=
735 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
736 Symbol_table::PREDEFINED
,
738 0, 0, elfcpp::STT_OBJECT
,
740 elfcpp::STV_HIDDEN
, 0,
743 // If there are any IRELATIVE relocations, they get GOT entries
744 // in .got.plt after the jump slot relocations.
745 this->got_irelative_
= new Output_data_space(4, "** GOT IRELATIVE PLT");
746 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
748 | elfcpp::SHF_WRITE
),
749 this->got_irelative_
,
750 ORDER_NON_RELRO_FIRST
, false);
752 // If there are any TLSDESC relocations, they get GOT entries in
753 // .got.plt after the jump slot entries.
754 this->got_tlsdesc_
= new Output_data_got
<32, false>();
755 layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
757 | elfcpp::SHF_WRITE
),
759 ORDER_NON_RELRO_FIRST
, false);
765 // Get the dynamic reloc section, creating it if necessary.
767 Target_i386::Reloc_section
*
768 Target_i386::rel_dyn_section(Layout
* layout
)
770 if (this->rel_dyn_
== NULL
)
772 gold_assert(layout
!= NULL
);
773 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
774 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
775 elfcpp::SHF_ALLOC
, this->rel_dyn_
,
776 ORDER_DYNAMIC_RELOCS
, false);
778 return this->rel_dyn_
;
781 // Get the section to use for IRELATIVE relocs, creating it if
782 // necessary. These go in .rel.dyn, but only after all other dynamic
783 // relocations. They need to follow the other dynamic relocations so
784 // that they can refer to global variables initialized by those
787 Target_i386::Reloc_section
*
788 Target_i386::rel_irelative_section(Layout
* layout
)
790 if (this->rel_irelative_
== NULL
)
792 // Make sure we have already create the dynamic reloc section.
793 this->rel_dyn_section(layout
);
794 this->rel_irelative_
= new Reloc_section(false);
795 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
796 elfcpp::SHF_ALLOC
, this->rel_irelative_
,
797 ORDER_DYNAMIC_RELOCS
, false);
798 gold_assert(this->rel_dyn_
->output_section()
799 == this->rel_irelative_
->output_section());
801 return this->rel_irelative_
;
804 // Create the PLT section. The ordinary .got section is an argument,
805 // since we need to refer to the start. We also create our own .got
806 // section just for PLT entries.
808 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
809 Output_data_space
* got_plt
,
810 Output_data_space
* got_irelative
)
811 : Output_section_data(16), tls_desc_rel_(NULL
), irelative_rel_(NULL
),
812 got_plt_(got_plt
), got_irelative_(got_irelative
), count_(0),
813 irelative_count_(0), global_ifuncs_(), local_ifuncs_()
815 this->rel_
= new Reloc_section(false);
816 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
817 elfcpp::SHF_ALLOC
, this->rel_
,
818 ORDER_DYNAMIC_PLT_RELOCS
, false);
820 // Add unwind information if requested.
821 if (parameters
->options().ld_generated_unwind_info())
822 layout
->add_eh_frame_for_plt(this, plt_eh_frame_cie
, plt_eh_frame_cie_size
,
823 plt_eh_frame_fde
, plt_eh_frame_fde_size
);
827 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
829 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
830 // linker, and so do we.
834 // Add an entry to the PLT.
837 Output_data_plt_i386::add_entry(Symbol_table
* symtab
, Layout
* layout
,
840 gold_assert(!gsym
->has_plt_offset());
842 // Every PLT entry needs a reloc.
843 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
844 && gsym
->can_use_relative_reloc(false))
846 gsym
->set_plt_offset(this->irelative_count_
* plt_entry_size
);
847 ++this->irelative_count_
;
848 section_offset_type got_offset
=
849 this->got_irelative_
->current_data_size();
850 this->got_irelative_
->set_current_data_size(got_offset
+ 4);
851 Reloc_section
* rel
= this->rel_irelative(symtab
, layout
);
852 rel
->add_symbolless_global_addend(gsym
, elfcpp::R_386_IRELATIVE
,
853 this->got_irelative_
, got_offset
);
854 struct Global_ifunc gi
;
856 gi
.got_offset
= got_offset
;
857 this->global_ifuncs_
.push_back(gi
);
861 // When setting the PLT offset we skip the initial reserved PLT
863 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
867 section_offset_type got_offset
= this->got_plt_
->current_data_size();
869 // Every PLT entry needs a GOT entry which points back to the
870 // PLT entry (this will be changed by the dynamic linker,
871 // normally lazily when the function is called).
872 this->got_plt_
->set_current_data_size(got_offset
+ 4);
874 gsym
->set_needs_dynsym_entry();
875 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
879 // Note that we don't need to save the symbol. The contents of the
880 // PLT are independent of which symbols are used. The symbols only
881 // appear in the relocations.
884 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
888 Output_data_plt_i386::add_local_ifunc_entry(
889 Symbol_table
* symtab
,
891 Sized_relobj_file
<32, false>* relobj
,
892 unsigned int local_sym_index
)
894 unsigned int plt_offset
= this->irelative_count_
* plt_entry_size
;
895 ++this->irelative_count_
;
897 section_offset_type got_offset
= this->got_irelative_
->current_data_size();
899 // Every PLT entry needs a GOT entry which points back to the PLT
901 this->got_irelative_
->set_current_data_size(got_offset
+ 4);
903 // Every PLT entry needs a reloc.
904 Reloc_section
* rel
= this->rel_irelative(symtab
, layout
);
905 rel
->add_symbolless_local_addend(relobj
, local_sym_index
,
906 elfcpp::R_386_IRELATIVE
,
907 this->got_irelative_
, got_offset
);
909 struct Local_ifunc li
;
911 li
.local_sym_index
= local_sym_index
;
912 li
.got_offset
= got_offset
;
913 this->local_ifuncs_
.push_back(li
);
918 // Return where the TLS_DESC relocations should go, creating it if
919 // necessary. These follow the JUMP_SLOT relocations.
921 Output_data_plt_i386::Reloc_section
*
922 Output_data_plt_i386::rel_tls_desc(Layout
* layout
)
924 if (this->tls_desc_rel_
== NULL
)
926 this->tls_desc_rel_
= new Reloc_section(false);
927 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
928 elfcpp::SHF_ALLOC
, this->tls_desc_rel_
,
929 ORDER_DYNAMIC_PLT_RELOCS
, false);
930 gold_assert(this->tls_desc_rel_
->output_section()
931 == this->rel_
->output_section());
933 return this->tls_desc_rel_
;
936 // Return where the IRELATIVE relocations should go in the PLT. These
937 // follow the JUMP_SLOT and TLS_DESC relocations.
939 Output_data_plt_i386::Reloc_section
*
940 Output_data_plt_i386::rel_irelative(Symbol_table
* symtab
, Layout
* layout
)
942 if (this->irelative_rel_
== NULL
)
944 // Make sure we have a place for the TLS_DESC relocations, in
945 // case we see any later on.
946 this->rel_tls_desc(layout
);
947 this->irelative_rel_
= new Reloc_section(false);
948 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
949 elfcpp::SHF_ALLOC
, this->irelative_rel_
,
950 ORDER_DYNAMIC_PLT_RELOCS
, false);
951 gold_assert(this->irelative_rel_
->output_section()
952 == this->rel_
->output_section());
954 if (parameters
->doing_static_link())
956 // A statically linked executable will only have a .rel.plt
957 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
958 // symbols. The library will use these symbols to locate
959 // the IRELATIVE relocs at program startup time.
960 symtab
->define_in_output_data("__rel_iplt_start", NULL
,
961 Symbol_table::PREDEFINED
,
962 this->irelative_rel_
, 0, 0,
963 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
964 elfcpp::STV_HIDDEN
, 0, false, true);
965 symtab
->define_in_output_data("__rel_iplt_end", NULL
,
966 Symbol_table::PREDEFINED
,
967 this->irelative_rel_
, 0, 0,
968 elfcpp::STT_NOTYPE
, elfcpp::STB_GLOBAL
,
969 elfcpp::STV_HIDDEN
, 0, true, true);
972 return this->irelative_rel_
;
975 // Return the PLT address to use for a global symbol.
978 Output_data_plt_i386::address_for_global(const Symbol
* gsym
)
981 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
982 && gsym
->can_use_relative_reloc(false))
983 offset
= (this->count_
+ 1) * plt_entry_size
;
984 return this->address() + offset
;
987 // Return the PLT address to use for a local symbol. These are always
991 Output_data_plt_i386::address_for_local(const Relobj
*, unsigned int)
993 return this->address() + (this->count_
+ 1) * plt_entry_size
;
996 // The first entry in the PLT for an executable.
998 const unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
1000 0xff, 0x35, // pushl contents of memory address
1001 0, 0, 0, 0, // replaced with address of .got + 4
1002 0xff, 0x25, // jmp indirect
1003 0, 0, 0, 0, // replaced with address of .got + 8
1004 0, 0, 0, 0 // unused
1007 // The first entry in the PLT for a shared object.
1009 const unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
1011 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1012 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1013 0, 0, 0, 0 // unused
1016 // Subsequent entries in the PLT for an executable.
1018 const unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
1020 0xff, 0x25, // jmp indirect
1021 0, 0, 0, 0, // replaced with address of symbol in .got
1022 0x68, // pushl immediate
1023 0, 0, 0, 0, // replaced with offset into relocation table
1024 0xe9, // jmp relative
1025 0, 0, 0, 0 // replaced with offset to start of .plt
1028 // Subsequent entries in the PLT for a shared object.
1030 const unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
1032 0xff, 0xa3, // jmp *offset(%ebx)
1033 0, 0, 0, 0, // replaced with offset of symbol in .got
1034 0x68, // pushl immediate
1035 0, 0, 0, 0, // replaced with offset into relocation table
1036 0xe9, // jmp relative
1037 0, 0, 0, 0 // replaced with offset to start of .plt
1040 // The .eh_frame unwind information for the PLT.
1043 Output_data_plt_i386::plt_eh_frame_cie
[plt_eh_frame_cie_size
] =
1046 'z', // Augmentation: augmentation size included.
1047 'R', // Augmentation: FDE encoding included.
1048 '\0', // End of augmentation string.
1049 1, // Code alignment factor.
1050 0x7c, // Data alignment factor.
1051 8, // Return address column.
1052 1, // Augmentation size.
1053 (elfcpp::DW_EH_PE_pcrel
// FDE encoding.
1054 | elfcpp::DW_EH_PE_sdata4
),
1055 elfcpp::DW_CFA_def_cfa
, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1056 elfcpp::DW_CFA_offset
+ 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1057 elfcpp::DW_CFA_nop
, // Align to 16 bytes.
1062 Output_data_plt_i386::plt_eh_frame_fde
[plt_eh_frame_fde_size
] =
1064 0, 0, 0, 0, // Replaced with offset to .plt.
1065 0, 0, 0, 0, // Replaced with size of .plt.
1066 0, // Augmentation size.
1067 elfcpp::DW_CFA_def_cfa_offset
, 8, // DW_CFA_def_cfa_offset: 8.
1068 elfcpp::DW_CFA_advance_loc
+ 6, // Advance 6 to __PLT__ + 6.
1069 elfcpp::DW_CFA_def_cfa_offset
, 12, // DW_CFA_def_cfa_offset: 12.
1070 elfcpp::DW_CFA_advance_loc
+ 10, // Advance 10 to __PLT__ + 16.
1071 elfcpp::DW_CFA_def_cfa_expression
, // DW_CFA_def_cfa_expression.
1072 11, // Block length.
1073 elfcpp::DW_OP_breg4
, 4, // Push %esp + 4.
1074 elfcpp::DW_OP_breg8
, 0, // Push %eip.
1075 elfcpp::DW_OP_lit15
, // Push 0xf.
1076 elfcpp::DW_OP_and
, // & (%eip & 0xf).
1077 elfcpp::DW_OP_lit11
, // Push 0xb.
1078 elfcpp::DW_OP_ge
, // >= ((%eip & 0xf) >= 0xb)
1079 elfcpp::DW_OP_lit2
, // Push 2.
1080 elfcpp::DW_OP_shl
, // << (((%eip & 0xf) >= 0xb) << 2)
1081 elfcpp::DW_OP_plus
, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1082 elfcpp::DW_CFA_nop
, // Align to 32 bytes.
1088 // Write out the PLT. This uses the hand-coded instructions above,
1089 // and adjusts them as needed. This is all specified by the i386 ELF
1090 // Processor Supplement.
1093 Output_data_plt_i386::do_write(Output_file
* of
)
1095 const off_t offset
= this->offset();
1096 const section_size_type oview_size
=
1097 convert_to_section_size_type(this->data_size());
1098 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
1100 const off_t got_file_offset
= this->got_plt_
->offset();
1101 gold_assert(parameters
->incremental_update()
1102 || (got_file_offset
+ this->got_plt_
->data_size()
1103 == this->got_irelative_
->offset()));
1104 const section_size_type got_size
=
1105 convert_to_section_size_type(this->got_plt_
->data_size()
1106 + this->got_irelative_
->data_size());
1107 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
1110 unsigned char* pov
= oview
;
1112 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
1113 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
1115 if (parameters
->options().output_is_position_independent())
1116 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
1119 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
1120 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
1121 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
1123 pov
+= plt_entry_size
;
1125 unsigned char* got_pov
= got_view
;
1127 memset(got_pov
, 0, 12);
1130 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
1132 unsigned int plt_offset
= plt_entry_size
;
1133 unsigned int plt_rel_offset
= 0;
1134 unsigned int got_offset
= 12;
1135 const unsigned int count
= this->count_
+ this->irelative_count_
;
1136 for (unsigned int i
= 0;
1139 pov
+= plt_entry_size
,
1141 plt_offset
+= plt_entry_size
,
1142 plt_rel_offset
+= rel_size
,
1145 // Set and adjust the PLT entry itself.
1147 if (parameters
->options().output_is_position_independent())
1149 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
1150 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
1154 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
1155 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
1160 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
1161 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
1162 - (plt_offset
+ plt_entry_size
));
1164 // Set the entry in the GOT.
1165 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
1168 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1169 // the GOT to point to the actual symbol value, rather than point to
1170 // the PLT entry. That will let the dynamic linker call the right
1171 // function when resolving IRELATIVE relocations.
1172 unsigned char* got_irelative_view
= got_view
+ this->got_plt_
->data_size();
1173 for (std::vector
<Global_ifunc
>::const_iterator p
=
1174 this->global_ifuncs_
.begin();
1175 p
!= this->global_ifuncs_
.end();
1178 const Sized_symbol
<32>* ssym
=
1179 static_cast<const Sized_symbol
<32>*>(p
->sym
);
1180 elfcpp::Swap
<32, false>::writeval(got_irelative_view
+ p
->got_offset
,
1184 for (std::vector
<Local_ifunc
>::const_iterator p
=
1185 this->local_ifuncs_
.begin();
1186 p
!= this->local_ifuncs_
.end();
1189 const Symbol_value
<32>* psymval
=
1190 p
->object
->local_symbol(p
->local_sym_index
);
1191 elfcpp::Swap
<32, false>::writeval(got_irelative_view
+ p
->got_offset
,
1192 psymval
->value(p
->object
, 0));
1195 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
1196 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
1198 of
->write_output_view(offset
, oview_size
, oview
);
1199 of
->write_output_view(got_file_offset
, got_size
, got_view
);
1202 // Create the PLT section.
1205 Target_i386::make_plt_section(Symbol_table
* symtab
, Layout
* layout
)
1207 if (this->plt_
== NULL
)
1209 // Create the GOT sections first.
1210 this->got_section(symtab
, layout
);
1212 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
,
1213 this->got_irelative_
);
1214 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
1216 | elfcpp::SHF_EXECINSTR
),
1217 this->plt_
, ORDER_PLT
, false);
1219 // Make the sh_info field of .rel.plt point to .plt.
1220 Output_section
* rel_plt_os
= this->plt_
->rel_plt()->output_section();
1221 rel_plt_os
->set_info_section(this->plt_
->output_section());
1225 // Create a PLT entry for a global symbol.
1228 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
1230 if (gsym
->has_plt_offset())
1232 if (this->plt_
== NULL
)
1233 this->make_plt_section(symtab
, layout
);
1234 this->plt_
->add_entry(symtab
, layout
, gsym
);
1237 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1240 Target_i386::make_local_ifunc_plt_entry(Symbol_table
* symtab
, Layout
* layout
,
1241 Sized_relobj_file
<32, false>* relobj
,
1242 unsigned int local_sym_index
)
1244 if (relobj
->local_has_plt_offset(local_sym_index
))
1246 if (this->plt_
== NULL
)
1247 this->make_plt_section(symtab
, layout
);
1248 unsigned int plt_offset
= this->plt_
->add_local_ifunc_entry(symtab
, layout
,
1251 relobj
->set_local_plt_offset(local_sym_index
, plt_offset
);
1254 // Return the number of entries in the PLT.
1257 Target_i386::plt_entry_count() const
1259 if (this->plt_
== NULL
)
1261 return this->plt_
->entry_count();
1264 // Return the offset of the first non-reserved PLT entry.
1267 Target_i386::first_plt_entry_offset() const
1269 return Output_data_plt_i386::first_plt_entry_offset();
1272 // Return the size of each PLT entry.
1275 Target_i386::plt_entry_size() const
1277 return Output_data_plt_i386::get_plt_entry_size();
1280 // Get the section to use for TLS_DESC relocations.
1282 Target_i386::Reloc_section
*
1283 Target_i386::rel_tls_desc_section(Layout
* layout
) const
1285 return this->plt_section()->rel_tls_desc(layout
);
1288 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1291 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
1293 if (this->tls_base_symbol_defined_
)
1296 Output_segment
* tls_segment
= layout
->tls_segment();
1297 if (tls_segment
!= NULL
)
1299 bool is_exec
= parameters
->options().output_is_executable();
1300 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
1301 Symbol_table::PREDEFINED
,
1305 elfcpp::STV_HIDDEN
, 0,
1307 ? Symbol::SEGMENT_END
1308 : Symbol::SEGMENT_START
),
1311 this->tls_base_symbol_defined_
= true;
1314 // Create a GOT entry for the TLS module index.
1317 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
1318 Sized_relobj_file
<32, false>* object
)
1320 if (this->got_mod_index_offset_
== -1U)
1322 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
1323 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1324 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
1325 unsigned int got_offset
= got
->add_constant(0);
1326 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
1328 got
->add_constant(0);
1329 this->got_mod_index_offset_
= got_offset
;
1331 return this->got_mod_index_offset_
;
1334 // Optimize the TLS relocation type based on what we know about the
1335 // symbol. IS_FINAL is true if the final address of this symbol is
1336 // known at link time.
1338 tls::Tls_optimization
1339 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
1341 // If we are generating a shared library, then we can't do anything
1343 if (parameters
->options().shared())
1344 return tls::TLSOPT_NONE
;
1348 case elfcpp::R_386_TLS_GD
:
1349 case elfcpp::R_386_TLS_GOTDESC
:
1350 case elfcpp::R_386_TLS_DESC_CALL
:
1351 // These are General-Dynamic which permits fully general TLS
1352 // access. Since we know that we are generating an executable,
1353 // we can convert this to Initial-Exec. If we also know that
1354 // this is a local symbol, we can further switch to Local-Exec.
1356 return tls::TLSOPT_TO_LE
;
1357 return tls::TLSOPT_TO_IE
;
1359 case elfcpp::R_386_TLS_LDM
:
1360 // This is Local-Dynamic, which refers to a local symbol in the
1361 // dynamic TLS block. Since we know that we generating an
1362 // executable, we can switch to Local-Exec.
1363 return tls::TLSOPT_TO_LE
;
1365 case elfcpp::R_386_TLS_LDO_32
:
1366 // Another type of Local-Dynamic relocation.
1367 return tls::TLSOPT_TO_LE
;
1369 case elfcpp::R_386_TLS_IE
:
1370 case elfcpp::R_386_TLS_GOTIE
:
1371 case elfcpp::R_386_TLS_IE_32
:
1372 // These are Initial-Exec relocs which get the thread offset
1373 // from the GOT. If we know that we are linking against the
1374 // local symbol, we can switch to Local-Exec, which links the
1375 // thread offset into the instruction.
1377 return tls::TLSOPT_TO_LE
;
1378 return tls::TLSOPT_NONE
;
1380 case elfcpp::R_386_TLS_LE
:
1381 case elfcpp::R_386_TLS_LE_32
:
1382 // When we already have Local-Exec, there is nothing further we
1384 return tls::TLSOPT_NONE
;
1391 // Get the Reference_flags for a particular relocation.
1394 Target_i386::Scan::get_reference_flags(unsigned int r_type
)
1398 case elfcpp::R_386_NONE
:
1399 case elfcpp::R_386_GNU_VTINHERIT
:
1400 case elfcpp::R_386_GNU_VTENTRY
:
1401 case elfcpp::R_386_GOTPC
:
1402 // No symbol reference.
1405 case elfcpp::R_386_32
:
1406 case elfcpp::R_386_16
:
1407 case elfcpp::R_386_8
:
1408 return Symbol::ABSOLUTE_REF
;
1410 case elfcpp::R_386_PC32
:
1411 case elfcpp::R_386_PC16
:
1412 case elfcpp::R_386_PC8
:
1413 case elfcpp::R_386_GOTOFF
:
1414 return Symbol::RELATIVE_REF
;
1416 case elfcpp::R_386_PLT32
:
1417 return Symbol::FUNCTION_CALL
| Symbol::RELATIVE_REF
;
1419 case elfcpp::R_386_GOT32
:
1421 return Symbol::ABSOLUTE_REF
;
1423 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1424 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1425 case elfcpp::R_386_TLS_DESC_CALL
:
1426 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1427 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1428 case elfcpp::R_386_TLS_IE
: // Initial-exec
1429 case elfcpp::R_386_TLS_IE_32
:
1430 case elfcpp::R_386_TLS_GOTIE
:
1431 case elfcpp::R_386_TLS_LE
: // Local-exec
1432 case elfcpp::R_386_TLS_LE_32
:
1433 return Symbol::TLS_REF
;
1435 case elfcpp::R_386_COPY
:
1436 case elfcpp::R_386_GLOB_DAT
:
1437 case elfcpp::R_386_JUMP_SLOT
:
1438 case elfcpp::R_386_RELATIVE
:
1439 case elfcpp::R_386_IRELATIVE
:
1440 case elfcpp::R_386_TLS_TPOFF
:
1441 case elfcpp::R_386_TLS_DTPMOD32
:
1442 case elfcpp::R_386_TLS_DTPOFF32
:
1443 case elfcpp::R_386_TLS_TPOFF32
:
1444 case elfcpp::R_386_TLS_DESC
:
1445 case elfcpp::R_386_32PLT
:
1446 case elfcpp::R_386_TLS_GD_32
:
1447 case elfcpp::R_386_TLS_GD_PUSH
:
1448 case elfcpp::R_386_TLS_GD_CALL
:
1449 case elfcpp::R_386_TLS_GD_POP
:
1450 case elfcpp::R_386_TLS_LDM_32
:
1451 case elfcpp::R_386_TLS_LDM_PUSH
:
1452 case elfcpp::R_386_TLS_LDM_CALL
:
1453 case elfcpp::R_386_TLS_LDM_POP
:
1454 case elfcpp::R_386_USED_BY_INTEL_200
:
1456 // Not expected. We will give an error later.
1461 // Report an unsupported relocation against a local symbol.
1464 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file
<32, false>* object
,
1465 unsigned int r_type
)
1467 gold_error(_("%s: unsupported reloc %u against local symbol"),
1468 object
->name().c_str(), r_type
);
1471 // Return whether we need to make a PLT entry for a relocation of a
1472 // given type against a STT_GNU_IFUNC symbol.
1475 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1476 Sized_relobj_file
<32, false>* object
,
1477 unsigned int r_type
)
1479 int flags
= Scan::get_reference_flags(r_type
);
1480 if (flags
& Symbol::TLS_REF
)
1481 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1482 object
->name().c_str(), r_type
);
1486 // Scan a relocation for a local symbol.
1489 Target_i386::Scan::local(Symbol_table
* symtab
,
1491 Target_i386
* target
,
1492 Sized_relobj_file
<32, false>* object
,
1493 unsigned int data_shndx
,
1494 Output_section
* output_section
,
1495 const elfcpp::Rel
<32, false>& reloc
,
1496 unsigned int r_type
,
1497 const elfcpp::Sym
<32, false>& lsym
)
1499 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1500 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
1501 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1503 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1504 target
->make_local_ifunc_plt_entry(symtab
, layout
, object
, r_sym
);
1509 case elfcpp::R_386_NONE
:
1510 case elfcpp::R_386_GNU_VTINHERIT
:
1511 case elfcpp::R_386_GNU_VTENTRY
:
1514 case elfcpp::R_386_32
:
1515 // If building a shared library (or a position-independent
1516 // executable), we need to create a dynamic relocation for
1517 // this location. The relocation applied at link time will
1518 // apply the link-time value, so we flag the location with
1519 // an R_386_RELATIVE relocation so the dynamic loader can
1520 // relocate it easily.
1521 if (parameters
->options().output_is_position_independent())
1523 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1524 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1525 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
1526 output_section
, data_shndx
,
1527 reloc
.get_r_offset());
1531 case elfcpp::R_386_16
:
1532 case elfcpp::R_386_8
:
1533 // If building a shared library (or a position-independent
1534 // executable), we need to create a dynamic relocation for
1535 // this location. Because the addend needs to remain in the
1536 // data section, we need to be careful not to apply this
1537 // relocation statically.
1538 if (parameters
->options().output_is_position_independent())
1540 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1541 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1542 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1543 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1544 data_shndx
, reloc
.get_r_offset());
1547 gold_assert(lsym
.get_st_value() == 0);
1548 unsigned int shndx
= lsym
.get_st_shndx();
1550 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1553 object
->error(_("section symbol %u has bad shndx %u"),
1556 rel_dyn
->add_local_section(object
, shndx
,
1557 r_type
, output_section
,
1558 data_shndx
, reloc
.get_r_offset());
1563 case elfcpp::R_386_PC32
:
1564 case elfcpp::R_386_PC16
:
1565 case elfcpp::R_386_PC8
:
1568 case elfcpp::R_386_PLT32
:
1569 // Since we know this is a local symbol, we can handle this as a
1573 case elfcpp::R_386_GOTOFF
:
1574 case elfcpp::R_386_GOTPC
:
1575 // We need a GOT section.
1576 target
->got_section(symtab
, layout
);
1579 case elfcpp::R_386_GOT32
:
1581 // The symbol requires a GOT entry.
1582 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1583 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1585 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1586 // lets function pointers compare correctly with shared
1587 // libraries. Otherwise we would need an IRELATIVE reloc.
1589 if (lsym
.get_st_type() == elfcpp::STT_GNU_IFUNC
)
1590 is_new
= got
->add_local_plt(object
, r_sym
, GOT_TYPE_STANDARD
);
1592 is_new
= got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
);
1595 // If we are generating a shared object, we need to add a
1596 // dynamic RELATIVE relocation for this symbol's GOT entry.
1597 if (parameters
->options().output_is_position_independent())
1599 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1600 unsigned int got_offset
=
1601 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
);
1602 rel_dyn
->add_local_relative(object
, r_sym
,
1603 elfcpp::R_386_RELATIVE
,
1610 // These are relocations which should only be seen by the
1611 // dynamic linker, and should never be seen here.
1612 case elfcpp::R_386_COPY
:
1613 case elfcpp::R_386_GLOB_DAT
:
1614 case elfcpp::R_386_JUMP_SLOT
:
1615 case elfcpp::R_386_RELATIVE
:
1616 case elfcpp::R_386_IRELATIVE
:
1617 case elfcpp::R_386_TLS_TPOFF
:
1618 case elfcpp::R_386_TLS_DTPMOD32
:
1619 case elfcpp::R_386_TLS_DTPOFF32
:
1620 case elfcpp::R_386_TLS_TPOFF32
:
1621 case elfcpp::R_386_TLS_DESC
:
1622 gold_error(_("%s: unexpected reloc %u in object file"),
1623 object
->name().c_str(), r_type
);
1626 // These are initial TLS relocs, which are expected when
1628 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1629 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1630 case elfcpp::R_386_TLS_DESC_CALL
:
1631 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1632 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1633 case elfcpp::R_386_TLS_IE
: // Initial-exec
1634 case elfcpp::R_386_TLS_IE_32
:
1635 case elfcpp::R_386_TLS_GOTIE
:
1636 case elfcpp::R_386_TLS_LE
: // Local-exec
1637 case elfcpp::R_386_TLS_LE_32
:
1639 bool output_is_shared
= parameters
->options().shared();
1640 const tls::Tls_optimization optimized_type
1641 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1644 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1645 if (optimized_type
== tls::TLSOPT_NONE
)
1647 // Create a pair of GOT entries for the module index and
1648 // dtv-relative offset.
1649 Output_data_got
<32, false>* got
1650 = target
->got_section(symtab
, layout
);
1651 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1652 unsigned int shndx
= lsym
.get_st_shndx();
1654 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1656 object
->error(_("local symbol %u has bad shndx %u"),
1659 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1661 target
->rel_dyn_section(layout
),
1662 elfcpp::R_386_TLS_DTPMOD32
, 0);
1664 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1665 unsupported_reloc_local(object
, r_type
);
1668 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1669 target
->define_tls_base_symbol(symtab
, layout
);
1670 if (optimized_type
== tls::TLSOPT_NONE
)
1672 // Create a double GOT entry with an R_386_TLS_DESC
1673 // reloc. The R_386_TLS_DESC reloc is resolved
1674 // lazily, so the GOT entry needs to be in an area in
1675 // .got.plt, not .got. Call got_section to make sure
1676 // the section has been created.
1677 target
->got_section(symtab
, layout
);
1678 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
1679 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1680 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1682 unsigned int got_offset
= got
->add_constant(0);
1683 // The local symbol value is stored in the second
1685 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_DESC
);
1686 // That set the GOT offset of the local symbol to
1687 // point to the second entry, but we want it to
1688 // point to the first.
1689 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1691 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1692 rt
->add_absolute(elfcpp::R_386_TLS_DESC
, got
, got_offset
);
1695 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1696 unsupported_reloc_local(object
, r_type
);
1699 case elfcpp::R_386_TLS_DESC_CALL
:
1702 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1703 if (optimized_type
== tls::TLSOPT_NONE
)
1705 // Create a GOT entry for the module index.
1706 target
->got_mod_index_entry(symtab
, layout
, object
);
1708 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1709 unsupported_reloc_local(object
, r_type
);
1712 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1715 case elfcpp::R_386_TLS_IE
: // Initial-exec
1716 case elfcpp::R_386_TLS_IE_32
:
1717 case elfcpp::R_386_TLS_GOTIE
:
1718 layout
->set_has_static_tls();
1719 if (optimized_type
== tls::TLSOPT_NONE
)
1721 // For the R_386_TLS_IE relocation, we need to create a
1722 // dynamic relocation when building a shared library.
1723 if (r_type
== elfcpp::R_386_TLS_IE
1724 && parameters
->options().shared())
1726 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1728 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1729 rel_dyn
->add_local_relative(object
, r_sym
,
1730 elfcpp::R_386_RELATIVE
,
1731 output_section
, data_shndx
,
1732 reloc
.get_r_offset());
1734 // Create a GOT entry for the tp-relative offset.
1735 Output_data_got
<32, false>* got
1736 = target
->got_section(symtab
, layout
);
1737 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1738 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1739 ? elfcpp::R_386_TLS_TPOFF32
1740 : elfcpp::R_386_TLS_TPOFF
);
1741 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1742 ? GOT_TYPE_TLS_OFFSET
1743 : GOT_TYPE_TLS_NOFFSET
);
1744 got
->add_local_with_rel(object
, r_sym
, got_type
,
1745 target
->rel_dyn_section(layout
),
1748 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1749 unsupported_reloc_local(object
, r_type
);
1752 case elfcpp::R_386_TLS_LE
: // Local-exec
1753 case elfcpp::R_386_TLS_LE_32
:
1754 layout
->set_has_static_tls();
1755 if (output_is_shared
)
1757 // We need to create a dynamic relocation.
1758 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1759 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1760 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1761 ? elfcpp::R_386_TLS_TPOFF32
1762 : elfcpp::R_386_TLS_TPOFF
);
1763 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1764 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1765 data_shndx
, reloc
.get_r_offset());
1775 case elfcpp::R_386_32PLT
:
1776 case elfcpp::R_386_TLS_GD_32
:
1777 case elfcpp::R_386_TLS_GD_PUSH
:
1778 case elfcpp::R_386_TLS_GD_CALL
:
1779 case elfcpp::R_386_TLS_GD_POP
:
1780 case elfcpp::R_386_TLS_LDM_32
:
1781 case elfcpp::R_386_TLS_LDM_PUSH
:
1782 case elfcpp::R_386_TLS_LDM_CALL
:
1783 case elfcpp::R_386_TLS_LDM_POP
:
1784 case elfcpp::R_386_USED_BY_INTEL_200
:
1786 unsupported_reloc_local(object
, r_type
);
1791 // Report an unsupported relocation against a global symbol.
1794 Target_i386::Scan::unsupported_reloc_global(
1795 Sized_relobj_file
<32, false>* object
,
1796 unsigned int r_type
,
1799 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1800 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1804 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1808 case elfcpp::R_386_32
:
1809 case elfcpp::R_386_16
:
1810 case elfcpp::R_386_8
:
1811 case elfcpp::R_386_GOTOFF
:
1812 case elfcpp::R_386_GOT32
:
1823 Target_i386::Scan::local_reloc_may_be_function_pointer(
1827 Sized_relobj_file
<32, false>* ,
1830 const elfcpp::Rel
<32, false>& ,
1831 unsigned int r_type
,
1832 const elfcpp::Sym
<32, false>&)
1834 return possible_function_pointer_reloc(r_type
);
1838 Target_i386::Scan::global_reloc_may_be_function_pointer(
1842 Sized_relobj_file
<32, false>* ,
1845 const elfcpp::Rel
<32, false>& ,
1846 unsigned int r_type
,
1849 return possible_function_pointer_reloc(r_type
);
1852 // Scan a relocation for a global symbol.
1855 Target_i386::Scan::global(Symbol_table
* symtab
,
1857 Target_i386
* target
,
1858 Sized_relobj_file
<32, false>* object
,
1859 unsigned int data_shndx
,
1860 Output_section
* output_section
,
1861 const elfcpp::Rel
<32, false>& reloc
,
1862 unsigned int r_type
,
1865 // A STT_GNU_IFUNC symbol may require a PLT entry.
1866 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
1867 && this->reloc_needs_plt_for_ifunc(object
, r_type
))
1868 target
->make_plt_entry(symtab
, layout
, gsym
);
1872 case elfcpp::R_386_NONE
:
1873 case elfcpp::R_386_GNU_VTINHERIT
:
1874 case elfcpp::R_386_GNU_VTENTRY
:
1877 case elfcpp::R_386_32
:
1878 case elfcpp::R_386_16
:
1879 case elfcpp::R_386_8
:
1881 // Make a PLT entry if necessary.
1882 if (gsym
->needs_plt_entry())
1884 target
->make_plt_entry(symtab
, layout
, gsym
);
1885 // Since this is not a PC-relative relocation, we may be
1886 // taking the address of a function. In that case we need to
1887 // set the entry in the dynamic symbol table to the address of
1889 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1890 gsym
->set_needs_dynsym_value();
1892 // Make a dynamic relocation if necessary.
1893 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1895 if (gsym
->may_need_copy_reloc())
1897 target
->copy_reloc(symtab
, layout
, object
,
1898 data_shndx
, output_section
, gsym
, reloc
);
1900 else if (r_type
== elfcpp::R_386_32
1901 && gsym
->type() == elfcpp::STT_GNU_IFUNC
1902 && gsym
->can_use_relative_reloc(false)
1903 && !gsym
->is_from_dynobj()
1904 && !gsym
->is_undefined()
1905 && !gsym
->is_preemptible())
1907 // Use an IRELATIVE reloc for a locally defined
1908 // STT_GNU_IFUNC symbol. This makes a function
1909 // address in a PIE executable match the address in a
1910 // shared library that it links against.
1911 Reloc_section
* rel_dyn
= target
->rel_irelative_section(layout
);
1912 rel_dyn
->add_symbolless_global_addend(gsym
,
1913 elfcpp::R_386_IRELATIVE
,
1916 reloc
.get_r_offset());
1918 else if (r_type
== elfcpp::R_386_32
1919 && gsym
->can_use_relative_reloc(false))
1921 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1922 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1923 output_section
, object
,
1924 data_shndx
, reloc
.get_r_offset());
1928 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1929 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1930 data_shndx
, reloc
.get_r_offset());
1936 case elfcpp::R_386_PC32
:
1937 case elfcpp::R_386_PC16
:
1938 case elfcpp::R_386_PC8
:
1940 // Make a PLT entry if necessary.
1941 if (gsym
->needs_plt_entry())
1943 // These relocations are used for function calls only in
1944 // non-PIC code. For a 32-bit relocation in a shared library,
1945 // we'll need a text relocation anyway, so we can skip the
1946 // PLT entry and let the dynamic linker bind the call directly
1947 // to the target. For smaller relocations, we should use a
1948 // PLT entry to ensure that the call can reach.
1949 if (!parameters
->options().shared()
1950 || r_type
!= elfcpp::R_386_PC32
)
1951 target
->make_plt_entry(symtab
, layout
, gsym
);
1953 // Make a dynamic relocation if necessary.
1954 if (gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
)))
1956 if (gsym
->may_need_copy_reloc())
1958 target
->copy_reloc(symtab
, layout
, object
,
1959 data_shndx
, output_section
, gsym
, reloc
);
1963 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1964 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1965 data_shndx
, reloc
.get_r_offset());
1971 case elfcpp::R_386_GOT32
:
1973 // The symbol requires a GOT entry.
1974 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1975 if (gsym
->final_value_is_known())
1977 // For a STT_GNU_IFUNC symbol we want the PLT address.
1978 if (gsym
->type() == elfcpp::STT_GNU_IFUNC
)
1979 got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
1981 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1985 // If this symbol is not fully resolved, we need to add a
1986 // GOT entry with a dynamic relocation.
1987 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1989 // Use a GLOB_DAT rather than a RELATIVE reloc if:
1991 // 1) The symbol may be defined in some other module.
1993 // 2) We are building a shared library and this is a
1994 // protected symbol; using GLOB_DAT means that the dynamic
1995 // linker can use the address of the PLT in the main
1996 // executable when appropriate so that function address
1997 // comparisons work.
1999 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2000 // code, again so that function address comparisons work.
2001 if (gsym
->is_from_dynobj()
2002 || gsym
->is_undefined()
2003 || gsym
->is_preemptible()
2004 || (gsym
->visibility() == elfcpp::STV_PROTECTED
2005 && parameters
->options().shared())
2006 || (gsym
->type() == elfcpp::STT_GNU_IFUNC
2007 && parameters
->options().output_is_position_independent()))
2008 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
2009 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
2012 // For a STT_GNU_IFUNC symbol we want to write the PLT
2013 // offset into the GOT, so that function pointer
2014 // comparisons work correctly.
2016 if (gsym
->type() != elfcpp::STT_GNU_IFUNC
)
2017 is_new
= got
->add_global(gsym
, GOT_TYPE_STANDARD
);
2020 is_new
= got
->add_global_plt(gsym
, GOT_TYPE_STANDARD
);
2021 // Tell the dynamic linker to use the PLT address
2022 // when resolving relocations.
2023 if (gsym
->is_from_dynobj()
2024 && !parameters
->options().shared())
2025 gsym
->set_needs_dynsym_value();
2029 unsigned int got_off
= gsym
->got_offset(GOT_TYPE_STANDARD
);
2030 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
2038 case elfcpp::R_386_PLT32
:
2039 // If the symbol is fully resolved, this is just a PC32 reloc.
2040 // Otherwise we need a PLT entry.
2041 if (gsym
->final_value_is_known())
2043 // If building a shared library, we can also skip the PLT entry
2044 // if the symbol is defined in the output file and is protected
2046 if (gsym
->is_defined()
2047 && !gsym
->is_from_dynobj()
2048 && !gsym
->is_preemptible())
2050 target
->make_plt_entry(symtab
, layout
, gsym
);
2053 case elfcpp::R_386_GOTOFF
:
2054 case elfcpp::R_386_GOTPC
:
2055 // We need a GOT section.
2056 target
->got_section(symtab
, layout
);
2059 // These are relocations which should only be seen by the
2060 // dynamic linker, and should never be seen here.
2061 case elfcpp::R_386_COPY
:
2062 case elfcpp::R_386_GLOB_DAT
:
2063 case elfcpp::R_386_JUMP_SLOT
:
2064 case elfcpp::R_386_RELATIVE
:
2065 case elfcpp::R_386_IRELATIVE
:
2066 case elfcpp::R_386_TLS_TPOFF
:
2067 case elfcpp::R_386_TLS_DTPMOD32
:
2068 case elfcpp::R_386_TLS_DTPOFF32
:
2069 case elfcpp::R_386_TLS_TPOFF32
:
2070 case elfcpp::R_386_TLS_DESC
:
2071 gold_error(_("%s: unexpected reloc %u in object file"),
2072 object
->name().c_str(), r_type
);
2075 // These are initial tls relocs, which are expected when
2077 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2078 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2079 case elfcpp::R_386_TLS_DESC_CALL
:
2080 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2081 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2082 case elfcpp::R_386_TLS_IE
: // Initial-exec
2083 case elfcpp::R_386_TLS_IE_32
:
2084 case elfcpp::R_386_TLS_GOTIE
:
2085 case elfcpp::R_386_TLS_LE
: // Local-exec
2086 case elfcpp::R_386_TLS_LE_32
:
2088 const bool is_final
= gsym
->final_value_is_known();
2089 const tls::Tls_optimization optimized_type
2090 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2093 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2094 if (optimized_type
== tls::TLSOPT_NONE
)
2096 // Create a pair of GOT entries for the module index and
2097 // dtv-relative offset.
2098 Output_data_got
<32, false>* got
2099 = target
->got_section(symtab
, layout
);
2100 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
2101 target
->rel_dyn_section(layout
),
2102 elfcpp::R_386_TLS_DTPMOD32
,
2103 elfcpp::R_386_TLS_DTPOFF32
);
2105 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2107 // Create a GOT entry for the tp-relative offset.
2108 Output_data_got
<32, false>* got
2109 = target
->got_section(symtab
, layout
);
2110 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
2111 target
->rel_dyn_section(layout
),
2112 elfcpp::R_386_TLS_TPOFF
);
2114 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2115 unsupported_reloc_global(object
, r_type
, gsym
);
2118 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
2119 target
->define_tls_base_symbol(symtab
, layout
);
2120 if (optimized_type
== tls::TLSOPT_NONE
)
2122 // Create a double GOT entry with an R_386_TLS_DESC
2123 // reloc. The R_386_TLS_DESC reloc is resolved
2124 // lazily, so the GOT entry needs to be in an area in
2125 // .got.plt, not .got. Call got_section to make sure
2126 // the section has been created.
2127 target
->got_section(symtab
, layout
);
2128 Output_data_got
<32, false>* got
= target
->got_tlsdesc_section();
2129 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
2130 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
2131 elfcpp::R_386_TLS_DESC
, 0);
2133 else if (optimized_type
== tls::TLSOPT_TO_IE
)
2135 // Create a GOT entry for the tp-relative offset.
2136 Output_data_got
<32, false>* got
2137 = target
->got_section(symtab
, layout
);
2138 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
2139 target
->rel_dyn_section(layout
),
2140 elfcpp::R_386_TLS_TPOFF
);
2142 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2143 unsupported_reloc_global(object
, r_type
, gsym
);
2146 case elfcpp::R_386_TLS_DESC_CALL
:
2149 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2150 if (optimized_type
== tls::TLSOPT_NONE
)
2152 // Create a GOT entry for the module index.
2153 target
->got_mod_index_entry(symtab
, layout
, object
);
2155 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2156 unsupported_reloc_global(object
, r_type
, gsym
);
2159 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2162 case elfcpp::R_386_TLS_IE
: // Initial-exec
2163 case elfcpp::R_386_TLS_IE_32
:
2164 case elfcpp::R_386_TLS_GOTIE
:
2165 layout
->set_has_static_tls();
2166 if (optimized_type
== tls::TLSOPT_NONE
)
2168 // For the R_386_TLS_IE relocation, we need to create a
2169 // dynamic relocation when building a shared library.
2170 if (r_type
== elfcpp::R_386_TLS_IE
2171 && parameters
->options().shared())
2173 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2174 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
2175 output_section
, object
,
2177 reloc
.get_r_offset());
2179 // Create a GOT entry for the tp-relative offset.
2180 Output_data_got
<32, false>* got
2181 = target
->got_section(symtab
, layout
);
2182 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2183 ? elfcpp::R_386_TLS_TPOFF32
2184 : elfcpp::R_386_TLS_TPOFF
);
2185 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2186 ? GOT_TYPE_TLS_OFFSET
2187 : GOT_TYPE_TLS_NOFFSET
);
2188 got
->add_global_with_rel(gsym
, got_type
,
2189 target
->rel_dyn_section(layout
),
2192 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
2193 unsupported_reloc_global(object
, r_type
, gsym
);
2196 case elfcpp::R_386_TLS_LE
: // Local-exec
2197 case elfcpp::R_386_TLS_LE_32
:
2198 layout
->set_has_static_tls();
2199 if (parameters
->options().shared())
2201 // We need to create a dynamic relocation.
2202 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
2203 ? elfcpp::R_386_TLS_TPOFF32
2204 : elfcpp::R_386_TLS_TPOFF
);
2205 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
2206 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
2207 data_shndx
, reloc
.get_r_offset());
2217 case elfcpp::R_386_32PLT
:
2218 case elfcpp::R_386_TLS_GD_32
:
2219 case elfcpp::R_386_TLS_GD_PUSH
:
2220 case elfcpp::R_386_TLS_GD_CALL
:
2221 case elfcpp::R_386_TLS_GD_POP
:
2222 case elfcpp::R_386_TLS_LDM_32
:
2223 case elfcpp::R_386_TLS_LDM_PUSH
:
2224 case elfcpp::R_386_TLS_LDM_CALL
:
2225 case elfcpp::R_386_TLS_LDM_POP
:
2226 case elfcpp::R_386_USED_BY_INTEL_200
:
2228 unsupported_reloc_global(object
, r_type
, gsym
);
2233 // Process relocations for gc.
2236 Target_i386::gc_process_relocs(Symbol_table
* symtab
,
2238 Sized_relobj_file
<32, false>* object
,
2239 unsigned int data_shndx
,
2241 const unsigned char* prelocs
,
2243 Output_section
* output_section
,
2244 bool needs_special_offset_handling
,
2245 size_t local_symbol_count
,
2246 const unsigned char* plocal_symbols
)
2248 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2250 Target_i386::Relocatable_size_for_reloc
>(
2259 needs_special_offset_handling
,
2264 // Scan relocations for a section.
2267 Target_i386::scan_relocs(Symbol_table
* symtab
,
2269 Sized_relobj_file
<32, false>* object
,
2270 unsigned int data_shndx
,
2271 unsigned int sh_type
,
2272 const unsigned char* prelocs
,
2274 Output_section
* output_section
,
2275 bool needs_special_offset_handling
,
2276 size_t local_symbol_count
,
2277 const unsigned char* plocal_symbols
)
2279 if (sh_type
== elfcpp::SHT_RELA
)
2281 gold_error(_("%s: unsupported RELA reloc section"),
2282 object
->name().c_str());
2286 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
2296 needs_special_offset_handling
,
2301 // Finalize the sections.
2304 Target_i386::do_finalize_sections(
2306 const Input_objects
*,
2307 Symbol_table
* symtab
)
2309 const Reloc_section
* rel_plt
= (this->plt_
== NULL
2311 : this->plt_
->rel_plt());
2312 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
2313 this->rel_dyn_
, true, false);
2315 // Emit any relocs we saved in an attempt to avoid generating COPY
2317 if (this->copy_relocs_
.any_saved_relocs())
2318 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
2320 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2321 // the .got.plt section.
2322 Symbol
* sym
= this->global_offset_table_
;
2325 uint32_t data_size
= this->got_plt_
->current_data_size();
2326 symtab
->get_sized_symbol
<32>(sym
)->set_symsize(data_size
);
2329 if (parameters
->doing_static_link()
2330 && (this->plt_
== NULL
|| !this->plt_
->has_irelative_section()))
2332 // If linking statically, make sure that the __rel_iplt symbols
2333 // were defined if necessary, even if we didn't create a PLT.
2334 static const Define_symbol_in_segment syms
[] =
2337 "__rel_iplt_start", // name
2338 elfcpp::PT_LOAD
, // segment_type
2339 elfcpp::PF_W
, // segment_flags_set
2340 elfcpp::PF(0), // segment_flags_clear
2343 elfcpp::STT_NOTYPE
, // type
2344 elfcpp::STB_GLOBAL
, // binding
2345 elfcpp::STV_HIDDEN
, // visibility
2347 Symbol::SEGMENT_START
, // offset_from_base
2351 "__rel_iplt_end", // 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
2366 symtab
->define_symbols(layout
, 2, syms
,
2367 layout
->script_options()->saw_sections_clause());
2371 // Return whether a direct absolute static relocation needs to be applied.
2372 // In cases where Scan::local() or Scan::global() has created
2373 // a dynamic relocation other than R_386_RELATIVE, the addend
2374 // of the relocation is carried in the data, and we must not
2375 // apply the static relocation.
2378 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
2379 unsigned int r_type
,
2381 Output_section
* output_section
)
2383 // If the output section is not allocated, then we didn't call
2384 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2386 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
2389 int ref_flags
= Scan::get_reference_flags(r_type
);
2391 // For local symbols, we will have created a non-RELATIVE dynamic
2392 // relocation only if (a) the output is position independent,
2393 // (b) the relocation is absolute (not pc- or segment-relative), and
2394 // (c) the relocation is not 32 bits wide.
2396 return !(parameters
->options().output_is_position_independent()
2397 && (ref_flags
& Symbol::ABSOLUTE_REF
)
2400 // For global symbols, we use the same helper routines used in the
2401 // scan pass. If we did not create a dynamic relocation, or if we
2402 // created a RELATIVE dynamic relocation, we should apply the static
2404 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
2405 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
2406 && gsym
->can_use_relative_reloc(ref_flags
2407 & Symbol::FUNCTION_CALL
);
2408 return !has_dyn
|| is_rel
;
2411 // Perform a relocation.
2414 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
2415 Target_i386
* target
,
2416 Output_section
* output_section
,
2418 const elfcpp::Rel
<32, false>& rel
,
2419 unsigned int r_type
,
2420 const Sized_symbol
<32>* gsym
,
2421 const Symbol_value
<32>* psymval
,
2422 unsigned char* view
,
2423 elfcpp::Elf_types
<32>::Elf_Addr address
,
2424 section_size_type view_size
)
2426 if (this->skip_call_tls_get_addr_
)
2428 if ((r_type
!= elfcpp::R_386_PLT32
2429 && r_type
!= elfcpp::R_386_PC32
)
2431 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
2432 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2433 _("missing expected TLS relocation"));
2436 this->skip_call_tls_get_addr_
= false;
2441 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2443 // Pick the value to use for symbols defined in shared objects.
2444 Symbol_value
<32> symval
;
2446 && gsym
->type() == elfcpp::STT_GNU_IFUNC
2447 && r_type
== elfcpp::R_386_32
2448 && gsym
->needs_dynamic_reloc(Scan::get_reference_flags(r_type
))
2449 && gsym
->can_use_relative_reloc(false)
2450 && !gsym
->is_from_dynobj()
2451 && !gsym
->is_undefined()
2452 && !gsym
->is_preemptible())
2454 // In this case we are generating a R_386_IRELATIVE reloc. We
2455 // want to use the real value of the symbol, not the PLT offset.
2457 else if (gsym
!= NULL
2458 && gsym
->use_plt_offset(Scan::get_reference_flags(r_type
)))
2460 symval
.set_output_value(target
->plt_address_for_global(gsym
)
2461 + gsym
->plt_offset());
2464 else if (gsym
== NULL
&& psymval
->is_ifunc_symbol())
2466 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2467 if (object
->local_has_plt_offset(r_sym
))
2469 symval
.set_output_value(target
->plt_address_for_local(object
, r_sym
)
2470 + object
->local_plt_offset(r_sym
));
2475 // Get the GOT offset if needed.
2476 // The GOT pointer points to the end of the GOT section.
2477 // We need to subtract the size of the GOT section to get
2478 // the actual offset to use in the relocation.
2479 bool have_got_offset
= false;
2480 unsigned int got_offset
= 0;
2483 case elfcpp::R_386_GOT32
:
2486 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
2487 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
2488 - target
->got_size());
2492 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2493 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
2494 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
2495 - target
->got_size());
2497 have_got_offset
= true;
2506 case elfcpp::R_386_NONE
:
2507 case elfcpp::R_386_GNU_VTINHERIT
:
2508 case elfcpp::R_386_GNU_VTENTRY
:
2511 case elfcpp::R_386_32
:
2512 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2513 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
2516 case elfcpp::R_386_PC32
:
2517 if (should_apply_static_reloc(gsym
, r_type
, true, output_section
))
2518 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2521 case elfcpp::R_386_16
:
2522 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2523 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
2526 case elfcpp::R_386_PC16
:
2527 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2528 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
2531 case elfcpp::R_386_8
:
2532 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2533 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
2536 case elfcpp::R_386_PC8
:
2537 if (should_apply_static_reloc(gsym
, r_type
, false, output_section
))
2538 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
2541 case elfcpp::R_386_PLT32
:
2542 gold_assert(gsym
== NULL
2543 || gsym
->has_plt_offset()
2544 || gsym
->final_value_is_known()
2545 || (gsym
->is_defined()
2546 && !gsym
->is_from_dynobj()
2547 && !gsym
->is_preemptible()));
2548 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
2551 case elfcpp::R_386_GOT32
:
2552 gold_assert(have_got_offset
);
2553 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2556 case elfcpp::R_386_GOTOFF
:
2558 elfcpp::Elf_types
<32>::Elf_Addr value
;
2559 value
= (psymval
->value(object
, 0)
2560 - target
->got_plt_section()->address());
2561 Relocate_functions
<32, false>::rel32(view
, value
);
2565 case elfcpp::R_386_GOTPC
:
2567 elfcpp::Elf_types
<32>::Elf_Addr value
;
2568 value
= target
->got_plt_section()->address();
2569 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
2573 case elfcpp::R_386_COPY
:
2574 case elfcpp::R_386_GLOB_DAT
:
2575 case elfcpp::R_386_JUMP_SLOT
:
2576 case elfcpp::R_386_RELATIVE
:
2577 case elfcpp::R_386_IRELATIVE
:
2578 // These are outstanding tls relocs, which are unexpected when
2580 case elfcpp::R_386_TLS_TPOFF
:
2581 case elfcpp::R_386_TLS_DTPMOD32
:
2582 case elfcpp::R_386_TLS_DTPOFF32
:
2583 case elfcpp::R_386_TLS_TPOFF32
:
2584 case elfcpp::R_386_TLS_DESC
:
2585 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2586 _("unexpected reloc %u in object file"),
2590 // These are initial tls relocs, which are expected when
2592 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2593 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2594 case elfcpp::R_386_TLS_DESC_CALL
:
2595 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2596 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2597 case elfcpp::R_386_TLS_IE
: // Initial-exec
2598 case elfcpp::R_386_TLS_IE_32
:
2599 case elfcpp::R_386_TLS_GOTIE
:
2600 case elfcpp::R_386_TLS_LE
: // Local-exec
2601 case elfcpp::R_386_TLS_LE_32
:
2602 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
2603 view
, address
, view_size
);
2606 case elfcpp::R_386_32PLT
:
2607 case elfcpp::R_386_TLS_GD_32
:
2608 case elfcpp::R_386_TLS_GD_PUSH
:
2609 case elfcpp::R_386_TLS_GD_CALL
:
2610 case elfcpp::R_386_TLS_GD_POP
:
2611 case elfcpp::R_386_TLS_LDM_32
:
2612 case elfcpp::R_386_TLS_LDM_PUSH
:
2613 case elfcpp::R_386_TLS_LDM_CALL
:
2614 case elfcpp::R_386_TLS_LDM_POP
:
2615 case elfcpp::R_386_USED_BY_INTEL_200
:
2617 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2618 _("unsupported reloc %u"),
2626 // Perform a TLS relocation.
2629 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
2630 Target_i386
* target
,
2632 const elfcpp::Rel
<32, false>& rel
,
2633 unsigned int r_type
,
2634 const Sized_symbol
<32>* gsym
,
2635 const Symbol_value
<32>* psymval
,
2636 unsigned char* view
,
2637 elfcpp::Elf_types
<32>::Elf_Addr
,
2638 section_size_type view_size
)
2640 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2642 const Sized_relobj_file
<32, false>* object
= relinfo
->object
;
2644 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
2646 const bool is_final
= (gsym
== NULL
2647 ? !parameters
->options().shared()
2648 : gsym
->final_value_is_known());
2649 const tls::Tls_optimization optimized_type
2650 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
2653 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2654 if (optimized_type
== tls::TLSOPT_TO_LE
)
2656 if (tls_segment
== NULL
)
2658 gold_assert(parameters
->errors()->error_count() > 0
2659 || issue_undefined_symbol_error(gsym
));
2662 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
2663 rel
, r_type
, value
, view
,
2669 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2670 ? GOT_TYPE_TLS_NOFFSET
2671 : GOT_TYPE_TLS_PAIR
);
2672 unsigned int got_offset
;
2675 gold_assert(gsym
->has_got_offset(got_type
));
2676 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2680 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2681 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2682 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2683 - target
->got_size());
2685 if (optimized_type
== tls::TLSOPT_TO_IE
)
2687 if (tls_segment
== NULL
)
2689 gold_assert(parameters
->errors()->error_count() > 0
2690 || issue_undefined_symbol_error(gsym
));
2693 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2694 got_offset
, view
, view_size
);
2697 else if (optimized_type
== tls::TLSOPT_NONE
)
2699 // Relocate the field with the offset of the pair of GOT
2701 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2705 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2706 _("unsupported reloc %u"),
2710 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2711 case elfcpp::R_386_TLS_DESC_CALL
:
2712 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2713 if (optimized_type
== tls::TLSOPT_TO_LE
)
2715 if (tls_segment
== NULL
)
2717 gold_assert(parameters
->errors()->error_count() > 0
2718 || issue_undefined_symbol_error(gsym
));
2721 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2722 rel
, r_type
, value
, view
,
2728 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2729 ? GOT_TYPE_TLS_NOFFSET
2730 : GOT_TYPE_TLS_DESC
);
2731 unsigned int got_offset
= 0;
2732 if (r_type
== elfcpp::R_386_TLS_GOTDESC
2733 && optimized_type
== tls::TLSOPT_NONE
)
2735 // We created GOT entries in the .got.tlsdesc portion of
2736 // the .got.plt section, but the offset stored in the
2737 // symbol is the offset within .got.tlsdesc.
2738 got_offset
= (target
->got_size()
2739 + target
->got_plt_section()->data_size());
2743 gold_assert(gsym
->has_got_offset(got_type
));
2744 got_offset
+= gsym
->got_offset(got_type
) - target
->got_size();
2748 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2749 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2750 got_offset
+= (object
->local_got_offset(r_sym
, got_type
)
2751 - target
->got_size());
2753 if (optimized_type
== tls::TLSOPT_TO_IE
)
2755 if (tls_segment
== NULL
)
2757 gold_assert(parameters
->errors()->error_count() > 0
2758 || issue_undefined_symbol_error(gsym
));
2761 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2762 got_offset
, view
, view_size
);
2765 else if (optimized_type
== tls::TLSOPT_NONE
)
2767 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2769 // Relocate the field with the offset of the pair of GOT
2771 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2776 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2777 _("unsupported reloc %u"),
2781 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2782 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
2784 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2785 _("both SUN and GNU model "
2786 "TLS relocations"));
2789 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2790 if (optimized_type
== tls::TLSOPT_TO_LE
)
2792 if (tls_segment
== NULL
)
2794 gold_assert(parameters
->errors()->error_count() > 0
2795 || issue_undefined_symbol_error(gsym
));
2798 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2799 value
, view
, view_size
);
2802 else if (optimized_type
== tls::TLSOPT_NONE
)
2804 // Relocate the field with the offset of the GOT entry for
2805 // the module index.
2806 unsigned int got_offset
;
2807 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2808 - target
->got_size());
2809 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2812 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2813 _("unsupported reloc %u"),
2817 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2818 if (optimized_type
== tls::TLSOPT_TO_LE
)
2820 // This reloc can appear in debugging sections, in which
2821 // case we must not convert to local-exec. We decide what
2822 // to do based on whether the section is marked as
2823 // containing executable code. That is what the GNU linker
2825 elfcpp::Shdr
<32, false> shdr(relinfo
->data_shdr
);
2826 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
2828 if (tls_segment
== NULL
)
2830 gold_assert(parameters
->errors()->error_count() > 0
2831 || issue_undefined_symbol_error(gsym
));
2834 value
-= tls_segment
->memsz();
2837 Relocate_functions
<32, false>::rel32(view
, value
);
2840 case elfcpp::R_386_TLS_IE
: // Initial-exec
2841 case elfcpp::R_386_TLS_GOTIE
:
2842 case elfcpp::R_386_TLS_IE_32
:
2843 if (optimized_type
== tls::TLSOPT_TO_LE
)
2845 if (tls_segment
== NULL
)
2847 gold_assert(parameters
->errors()->error_count() > 0
2848 || issue_undefined_symbol_error(gsym
));
2851 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2852 rel
, r_type
, value
, view
,
2856 else if (optimized_type
== tls::TLSOPT_NONE
)
2858 // Relocate the field with the offset of the GOT entry for
2859 // the tp-relative offset of the symbol.
2860 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2861 ? GOT_TYPE_TLS_OFFSET
2862 : GOT_TYPE_TLS_NOFFSET
);
2863 unsigned int got_offset
;
2866 gold_assert(gsym
->has_got_offset(got_type
));
2867 got_offset
= gsym
->got_offset(got_type
);
2871 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2872 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2873 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2875 // For the R_386_TLS_IE relocation, we need to apply the
2876 // absolute address of the GOT entry.
2877 if (r_type
== elfcpp::R_386_TLS_IE
)
2878 got_offset
+= target
->got_plt_section()->address();
2879 // All GOT offsets are relative to the end of the GOT.
2880 got_offset
-= target
->got_size();
2881 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2884 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2885 _("unsupported reloc %u"),
2889 case elfcpp::R_386_TLS_LE
: // Local-exec
2890 // If we're creating a shared library, a dynamic relocation will
2891 // have been created for this location, so do not apply it now.
2892 if (!parameters
->options().shared())
2894 if (tls_segment
== NULL
)
2896 gold_assert(parameters
->errors()->error_count() > 0
2897 || issue_undefined_symbol_error(gsym
));
2900 value
-= tls_segment
->memsz();
2901 Relocate_functions
<32, false>::rel32(view
, value
);
2905 case elfcpp::R_386_TLS_LE_32
:
2906 // If we're creating a shared library, a dynamic relocation will
2907 // have been created for this location, so do not apply it now.
2908 if (!parameters
->options().shared())
2910 if (tls_segment
== NULL
)
2912 gold_assert(parameters
->errors()->error_count() > 0
2913 || issue_undefined_symbol_error(gsym
));
2916 value
= tls_segment
->memsz() - value
;
2917 Relocate_functions
<32, false>::rel32(view
, value
);
2923 // Do a relocation in which we convert a TLS General-Dynamic to a
2927 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2929 Output_segment
* tls_segment
,
2930 const elfcpp::Rel
<32, false>& rel
,
2932 elfcpp::Elf_types
<32>::Elf_Addr value
,
2933 unsigned char* view
,
2934 section_size_type view_size
)
2936 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2937 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2938 // leal foo(%reg),%eax; call ___tls_get_addr
2939 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2941 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2942 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2944 unsigned char op1
= view
[-1];
2945 unsigned char op2
= view
[-2];
2947 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2948 op2
== 0x8d || op2
== 0x04);
2949 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2955 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2956 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2957 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2958 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2959 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2963 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2964 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2965 if (rel
.get_r_offset() + 9 < view_size
2968 // There is a trailing nop. Use the size byte subl.
2969 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2974 // Use the five byte subl.
2975 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2979 value
= tls_segment
->memsz() - value
;
2980 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2982 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2984 this->skip_call_tls_get_addr_
= true;
2987 // Do a relocation in which we convert a TLS General-Dynamic to an
2991 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2994 const elfcpp::Rel
<32, false>& rel
,
2996 elfcpp::Elf_types
<32>::Elf_Addr value
,
2997 unsigned char* view
,
2998 section_size_type view_size
)
3000 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3001 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3003 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3004 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
3006 unsigned char op1
= view
[-1];
3007 unsigned char op2
= view
[-2];
3009 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3010 op2
== 0x8d || op2
== 0x04);
3011 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
3015 // FIXME: For now, support only the first (SIB) form.
3016 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
3020 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
3021 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
3022 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3023 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
3024 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3028 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3029 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
3030 if (rel
.get_r_offset() + 9 < view_size
3033 // FIXME: This is not the right instruction sequence.
3034 // There is a trailing nop. Use the size byte subl.
3035 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3040 // FIXME: This is not the right instruction sequence.
3041 // Use the five byte subl.
3042 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3046 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
3048 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3050 this->skip_call_tls_get_addr_
= true;
3053 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3054 // General-Dynamic to a Local-Exec.
3057 Target_i386::Relocate::tls_desc_gd_to_le(
3058 const Relocate_info
<32, false>* relinfo
,
3060 Output_segment
* tls_segment
,
3061 const elfcpp::Rel
<32, false>& rel
,
3062 unsigned int r_type
,
3063 elfcpp::Elf_types
<32>::Elf_Addr value
,
3064 unsigned char* view
,
3065 section_size_type view_size
)
3067 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
3069 // leal foo@TLSDESC(%ebx), %eax
3070 // ==> leal foo@NTPOFF, %eax
3071 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3072 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3073 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3074 view
[-2] == 0x8d && view
[-1] == 0x83);
3076 value
-= tls_segment
->memsz();
3077 Relocate_functions
<32, false>::rel32(view
, value
);
3081 // call *foo@TLSCALL(%eax)
3083 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
3084 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
3085 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3086 view
[0] == 0xff && view
[1] == 0x10);
3092 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3093 // General-Dynamic to an Initial-Exec.
3096 Target_i386::Relocate::tls_desc_gd_to_ie(
3097 const Relocate_info
<32, false>* relinfo
,
3100 const elfcpp::Rel
<32, false>& rel
,
3101 unsigned int r_type
,
3102 elfcpp::Elf_types
<32>::Elf_Addr value
,
3103 unsigned char* view
,
3104 section_size_type view_size
)
3106 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
3108 // leal foo@TLSDESC(%ebx), %eax
3109 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3110 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3111 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3112 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3113 view
[-2] == 0x8d && view
[-1] == 0x83);
3115 Relocate_functions
<32, false>::rel32(view
, value
);
3119 // call *foo@TLSCALL(%eax)
3121 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
3122 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
3123 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3124 view
[0] == 0xff && view
[1] == 0x10);
3130 // Do a relocation in which we convert a TLS Local-Dynamic to a
3134 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
3137 const elfcpp::Rel
<32, false>& rel
,
3139 elfcpp::Elf_types
<32>::Elf_Addr
,
3140 unsigned char* view
,
3141 section_size_type view_size
)
3143 // leal foo(%reg), %eax; call ___tls_get_addr
3144 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3146 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3147 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
3149 // FIXME: Does this test really always pass?
3150 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3151 view
[-2] == 0x8d && view
[-1] == 0x83);
3153 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
3155 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3157 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3159 this->skip_call_tls_get_addr_
= true;
3162 // Do a relocation in which we convert a TLS Initial-Exec to a
3166 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
3168 Output_segment
* tls_segment
,
3169 const elfcpp::Rel
<32, false>& rel
,
3170 unsigned int r_type
,
3171 elfcpp::Elf_types
<32>::Elf_Addr value
,
3172 unsigned char* view
,
3173 section_size_type view_size
)
3175 // We have to actually change the instructions, which means that we
3176 // need to examine the opcodes to figure out which instruction we
3178 if (r_type
== elfcpp::R_386_TLS_IE
)
3180 // movl %gs:XX,%eax ==> movl $YY,%eax
3181 // movl %gs:XX,%reg ==> movl $YY,%reg
3182 // addl %gs:XX,%reg ==> addl $YY,%reg
3183 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
3184 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3186 unsigned char op1
= view
[-1];
3189 // movl XX,%eax ==> movl $YY,%eax
3194 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3196 unsigned char op2
= view
[-2];
3199 // movl XX,%reg ==> movl $YY,%reg
3200 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3201 (op1
& 0xc7) == 0x05);
3203 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3205 else if (op2
== 0x03)
3207 // addl XX,%reg ==> addl $YY,%reg
3208 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3209 (op1
& 0xc7) == 0x05);
3211 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3214 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
3219 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3220 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3221 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3222 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
3223 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
3225 unsigned char op1
= view
[-1];
3226 unsigned char op2
= view
[-2];
3227 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
3228 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
3231 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3233 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3235 else if (op2
== 0x2b)
3237 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3239 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
3241 else if (op2
== 0x03)
3243 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3245 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
3248 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
3251 value
= tls_segment
->memsz() - value
;
3252 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
3255 Relocate_functions
<32, false>::rel32(view
, value
);
3258 // Relocate section data.
3261 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
3262 unsigned int sh_type
,
3263 const unsigned char* prelocs
,
3265 Output_section
* output_section
,
3266 bool needs_special_offset_handling
,
3267 unsigned char* view
,
3268 elfcpp::Elf_types
<32>::Elf_Addr address
,
3269 section_size_type view_size
,
3270 const Reloc_symbol_changes
* reloc_symbol_changes
)
3272 gold_assert(sh_type
== elfcpp::SHT_REL
);
3274 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
3275 Target_i386::Relocate
>(
3281 needs_special_offset_handling
,
3285 reloc_symbol_changes
);
3288 // Return the size of a relocation while scanning during a relocatable
3292 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3293 unsigned int r_type
,
3298 case elfcpp::R_386_NONE
:
3299 case elfcpp::R_386_GNU_VTINHERIT
:
3300 case elfcpp::R_386_GNU_VTENTRY
:
3301 case elfcpp::R_386_TLS_GD
: // Global-dynamic
3302 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
3303 case elfcpp::R_386_TLS_DESC_CALL
:
3304 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
3305 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
3306 case elfcpp::R_386_TLS_IE
: // Initial-exec
3307 case elfcpp::R_386_TLS_IE_32
:
3308 case elfcpp::R_386_TLS_GOTIE
:
3309 case elfcpp::R_386_TLS_LE
: // Local-exec
3310 case elfcpp::R_386_TLS_LE_32
:
3313 case elfcpp::R_386_32
:
3314 case elfcpp::R_386_PC32
:
3315 case elfcpp::R_386_GOT32
:
3316 case elfcpp::R_386_PLT32
:
3317 case elfcpp::R_386_GOTOFF
:
3318 case elfcpp::R_386_GOTPC
:
3321 case elfcpp::R_386_16
:
3322 case elfcpp::R_386_PC16
:
3325 case elfcpp::R_386_8
:
3326 case elfcpp::R_386_PC8
:
3329 // These are relocations which should only be seen by the
3330 // dynamic linker, and should never be seen here.
3331 case elfcpp::R_386_COPY
:
3332 case elfcpp::R_386_GLOB_DAT
:
3333 case elfcpp::R_386_JUMP_SLOT
:
3334 case elfcpp::R_386_RELATIVE
:
3335 case elfcpp::R_386_IRELATIVE
:
3336 case elfcpp::R_386_TLS_TPOFF
:
3337 case elfcpp::R_386_TLS_DTPMOD32
:
3338 case elfcpp::R_386_TLS_DTPOFF32
:
3339 case elfcpp::R_386_TLS_TPOFF32
:
3340 case elfcpp::R_386_TLS_DESC
:
3341 object
->error(_("unexpected reloc %u in object file"), r_type
);
3344 case elfcpp::R_386_32PLT
:
3345 case elfcpp::R_386_TLS_GD_32
:
3346 case elfcpp::R_386_TLS_GD_PUSH
:
3347 case elfcpp::R_386_TLS_GD_CALL
:
3348 case elfcpp::R_386_TLS_GD_POP
:
3349 case elfcpp::R_386_TLS_LDM_32
:
3350 case elfcpp::R_386_TLS_LDM_PUSH
:
3351 case elfcpp::R_386_TLS_LDM_CALL
:
3352 case elfcpp::R_386_TLS_LDM_POP
:
3353 case elfcpp::R_386_USED_BY_INTEL_200
:
3355 object
->error(_("unsupported reloc %u in object file"), r_type
);
3360 // Scan the relocs during a relocatable link.
3363 Target_i386::scan_relocatable_relocs(Symbol_table
* symtab
,
3365 Sized_relobj_file
<32, false>* object
,
3366 unsigned int data_shndx
,
3367 unsigned int sh_type
,
3368 const unsigned char* prelocs
,
3370 Output_section
* output_section
,
3371 bool needs_special_offset_handling
,
3372 size_t local_symbol_count
,
3373 const unsigned char* plocal_symbols
,
3374 Relocatable_relocs
* rr
)
3376 gold_assert(sh_type
== elfcpp::SHT_REL
);
3378 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
3379 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
3381 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
3382 Scan_relocatable_relocs
>(
3390 needs_special_offset_handling
,
3396 // Relocate a section during a relocatable link.
3399 Target_i386::relocate_for_relocatable(
3400 const Relocate_info
<32, false>* relinfo
,
3401 unsigned int sh_type
,
3402 const unsigned char* prelocs
,
3404 Output_section
* output_section
,
3405 off_t offset_in_output_section
,
3406 const Relocatable_relocs
* rr
,
3407 unsigned char* view
,
3408 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
3409 section_size_type view_size
,
3410 unsigned char* reloc_view
,
3411 section_size_type reloc_view_size
)
3413 gold_assert(sh_type
== elfcpp::SHT_REL
);
3415 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
3420 offset_in_output_section
,
3429 // Return the value to use for a dynamic which requires special
3430 // treatment. This is how we support equality comparisons of function
3431 // pointers across shared library boundaries, as described in the
3432 // processor specific ABI supplement.
3435 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
3437 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
3438 return this->plt_address_for_global(gsym
) + gsym
->plt_offset();
3441 // Return a string used to fill a code section with nops to take up
3442 // the specified length.
3445 Target_i386::do_code_fill(section_size_type length
) const
3449 // Build a jmp instruction to skip over the bytes.
3450 unsigned char jmp
[5];
3452 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
3453 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
3454 + std::string(length
- 5, '\0'));
3457 // Nop sequences of various lengths.
3458 const char nop1
[1] = { 0x90 }; // nop
3459 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
3460 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3461 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3462 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3463 0x00 }; // leal 0(%esi,1),%esi
3464 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3466 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3468 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3469 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3470 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3471 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3473 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3474 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3476 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3477 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3479 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3480 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3481 0x00, 0x00, 0x00, 0x00 };
3482 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3483 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3484 0x27, 0x00, 0x00, 0x00,
3486 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3487 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3488 0xbc, 0x27, 0x00, 0x00,
3490 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3491 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3492 0x90, 0x90, 0x90, 0x90,
3495 const char* nops
[16] = {
3497 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
3498 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
3501 return std::string(nops
[length
], length
);
3504 // Return the value to use for the base of a DW_EH_PE_datarel offset
3505 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3506 // assembler can not write out the difference between two labels in
3507 // different sections, so instead of using a pc-relative value they
3508 // use an offset from the GOT.
3511 Target_i386::do_ehframe_datarel_base() const
3513 gold_assert(this->global_offset_table_
!= NULL
);
3514 Symbol
* sym
= this->global_offset_table_
;
3515 Sized_symbol
<32>* ssym
= static_cast<Sized_symbol
<32>*>(sym
);
3516 return ssym
->value();
3519 // Return whether SYM should be treated as a call to a non-split
3520 // function. We don't want that to be true of a call to a
3521 // get_pc_thunk function.
3524 Target_i386::do_is_call_to_non_split(const Symbol
* sym
, unsigned int) const
3526 return (sym
->type() == elfcpp::STT_FUNC
3527 && !is_prefix_of("__i686.get_pc_thunk.", sym
->name()));
3530 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3531 // compiled with -fsplit-stack. The function calls non-split-stack
3532 // code. We have to change the function so that it always ensures
3533 // that it has enough stack space to run some random function.
3536 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
3537 section_offset_type fnoffset
,
3538 section_size_type fnsize
,
3539 unsigned char* view
,
3540 section_size_type view_size
,
3542 std::string
* to
) const
3544 // The function starts with a comparison of the stack pointer and a
3545 // field in the TCB. This is followed by a jump.
3548 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
3551 // We will call __morestack if the carry flag is set after this
3552 // comparison. We turn the comparison into an stc instruction
3554 view
[fnoffset
] = '\xf9';
3555 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
3557 // lea NN(%esp),%ecx
3558 // lea NN(%esp),%edx
3559 else if ((this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
3560 || this->match_view(view
, view_size
, fnoffset
, "\x8d\x94\x24", 3))
3563 // This is loading an offset from the stack pointer for a
3564 // comparison. The offset is negative, so we decrease the
3565 // offset by the amount of space we need for the stack. This
3566 // means we will avoid calling __morestack if there happens to
3567 // be plenty of space on the stack already.
3568 unsigned char* pval
= view
+ fnoffset
+ 3;
3569 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
3570 val
-= parameters
->options().split_stack_adjust_size();
3571 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
3575 if (!object
->has_no_split_stack())
3576 object
->error(_("failed to match split-stack sequence at "
3577 "section %u offset %0zx"),
3578 shndx
, static_cast<size_t>(fnoffset
));
3582 // We have to change the function so that it calls
3583 // __morestack_non_split instead of __morestack. The former will
3584 // allocate additional stack space.
3585 *from
= "__morestack";
3586 *to
= "__morestack_non_split";
3589 // The selector for i386 object files.
3591 class Target_selector_i386
: public Target_selector_freebsd
3594 Target_selector_i386()
3595 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
3596 "elf32-i386", "elf32-i386-freebsd",
3601 do_instantiate_target()
3602 { return new Target_i386(); }
3605 Target_selector_i386 target_selector_i386
;
3607 } // End anonymous namespace.