1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_i386
;
49 // The i386 target class.
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_i386
: public Target_freebsd
<32, false>
57 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
60 : Target_freebsd
<32, false>(&i386_info
),
61 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
62 copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
63 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
66 // Process the relocations to determine unreferenced sections for
67 // garbage collection.
69 gc_process_relocs(const General_options
& options
,
72 Sized_relobj
<32, false>* object
,
73 unsigned int data_shndx
,
75 const unsigned char* prelocs
,
77 Output_section
* output_section
,
78 bool needs_special_offset_handling
,
79 size_t local_symbol_count
,
80 const unsigned char* plocal_symbols
);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(const General_options
& options
,
87 Sized_relobj
<32, false>* object
,
88 unsigned int data_shndx
,
90 const unsigned char* prelocs
,
92 Output_section
* output_section
,
93 bool needs_special_offset_handling
,
94 size_t local_symbol_count
,
95 const unsigned char* plocal_symbols
);
97 // Finalize the sections.
99 do_finalize_sections(Layout
*);
101 // Return the value to use for a dynamic which requires special
104 do_dynsym_value(const Symbol
*) const;
106 // Relocate a section.
108 relocate_section(const Relocate_info
<32, false>*,
109 unsigned int sh_type
,
110 const unsigned char* prelocs
,
112 Output_section
* output_section
,
113 bool needs_special_offset_handling
,
115 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
116 section_size_type view_size
);
118 // Scan the relocs during a relocatable link.
120 scan_relocatable_relocs(const General_options
& options
,
121 Symbol_table
* symtab
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 unsigned int sh_type
,
126 const unsigned char* prelocs
,
128 Output_section
* output_section
,
129 bool needs_special_offset_handling
,
130 size_t local_symbol_count
,
131 const unsigned char* plocal_symbols
,
132 Relocatable_relocs
*);
134 // Relocate a section during a relocatable link.
136 relocate_for_relocatable(const Relocate_info
<32, false>*,
137 unsigned int sh_type
,
138 const unsigned char* prelocs
,
140 Output_section
* output_section
,
141 off_t offset_in_output_section
,
142 const Relocatable_relocs
*,
144 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
145 section_size_type view_size
,
146 unsigned char* reloc_view
,
147 section_size_type reloc_view_size
);
149 // Return a string used to fill a code section with nops.
151 do_code_fill(section_size_type length
) const;
153 // Return whether SYM is defined by the ABI.
155 do_is_defined_by_abi(const Symbol
* sym
) const
156 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
158 // Return whether a symbol name implies a local label. The UnixWare
159 // 2.1 cc generates temporary symbols that start with .X, so we
160 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
161 // If so, we should move the .X recognition into
162 // Target::do_is_local_label_name.
164 do_is_local_label_name(const char* name
) const
166 if (name
[0] == '.' && name
[1] == 'X')
168 return Target::do_is_local_label_name(name
);
171 // Return the size of the GOT section.
175 gold_assert(this->got_
!= NULL
);
176 return this->got_
->data_size();
180 // The class which scans relocations.
184 local(const General_options
& options
, Symbol_table
* symtab
,
185 Layout
* layout
, Target_i386
* target
,
186 Sized_relobj
<32, false>* object
,
187 unsigned int data_shndx
,
188 Output_section
* output_section
,
189 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
190 const elfcpp::Sym
<32, false>& lsym
);
193 global(const General_options
& options
, Symbol_table
* symtab
,
194 Layout
* layout
, Target_i386
* target
,
195 Sized_relobj
<32, false>* object
,
196 unsigned int data_shndx
,
197 Output_section
* output_section
,
198 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
202 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
205 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
209 // The class which implements relocation.
214 : skip_call_tls_get_addr_(false),
215 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
220 if (this->skip_call_tls_get_addr_
)
222 // FIXME: This needs to specify the location somehow.
223 gold_error(_("missing expected TLS relocation"));
227 // Return whether the static relocation needs to be applied.
229 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
232 Output_section
* output_section
);
234 // Do a relocation. Return false if the caller should not issue
235 // any warnings about this relocation.
237 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
238 size_t relnum
, const elfcpp::Rel
<32, false>&,
239 unsigned int r_type
, const Sized_symbol
<32>*,
240 const Symbol_value
<32>*,
241 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
245 // Do a TLS relocation.
247 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
248 size_t relnum
, const elfcpp::Rel
<32, false>&,
249 unsigned int r_type
, const Sized_symbol
<32>*,
250 const Symbol_value
<32>*,
251 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
254 // Do a TLS General-Dynamic to Initial-Exec transition.
256 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
257 Output_segment
* tls_segment
,
258 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
259 elfcpp::Elf_types
<32>::Elf_Addr value
,
261 section_size_type view_size
);
263 // Do a TLS General-Dynamic to Local-Exec transition.
265 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
266 Output_segment
* tls_segment
,
267 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
268 elfcpp::Elf_types
<32>::Elf_Addr value
,
270 section_size_type view_size
);
272 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
275 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
276 Output_segment
* tls_segment
,
277 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
278 elfcpp::Elf_types
<32>::Elf_Addr value
,
280 section_size_type view_size
);
282 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
285 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
286 Output_segment
* tls_segment
,
287 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
288 elfcpp::Elf_types
<32>::Elf_Addr value
,
290 section_size_type view_size
);
292 // Do a TLS Local-Dynamic to Local-Exec transition.
294 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
295 Output_segment
* tls_segment
,
296 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
297 elfcpp::Elf_types
<32>::Elf_Addr value
,
299 section_size_type view_size
);
301 // Do a TLS Initial-Exec to Local-Exec transition.
303 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
304 Output_segment
* tls_segment
,
305 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
306 elfcpp::Elf_types
<32>::Elf_Addr value
,
308 section_size_type view_size
);
310 // We need to keep track of which type of local dynamic relocation
311 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
312 enum Local_dynamic_type
319 // This is set if we should skip the next reloc, which should be a
320 // PLT32 reloc against ___tls_get_addr.
321 bool skip_call_tls_get_addr_
;
322 // The type of local dynamic relocation we have seen in the section
323 // being relocated, if any.
324 Local_dynamic_type local_dynamic_type_
;
327 // A class which returns the size required for a relocation type,
328 // used while scanning relocs during a relocatable link.
329 class Relocatable_size_for_reloc
333 get_size_for_reloc(unsigned int, Relobj
*);
336 // Adjust TLS relocation type based on the options and whether this
337 // is a local symbol.
338 static tls::Tls_optimization
339 optimize_tls_reloc(bool is_final
, int r_type
);
341 // Get the GOT section, creating it if necessary.
342 Output_data_got
<32, false>*
343 got_section(Symbol_table
*, Layout
*);
345 // Get the GOT PLT section.
347 got_plt_section() const
349 gold_assert(this->got_plt_
!= NULL
);
350 return this->got_plt_
;
353 // Create a PLT entry for a global symbol.
355 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
357 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
359 define_tls_base_symbol(Symbol_table
*, Layout
*);
361 // Create a GOT entry for the TLS module index.
363 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
364 Sized_relobj
<32, false>* object
);
366 // Get the PLT section.
367 const Output_data_plt_i386
*
370 gold_assert(this->plt_
!= NULL
);
374 // Get the dynamic reloc section, creating it if necessary.
376 rel_dyn_section(Layout
*);
378 // Add a potential copy relocation.
380 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
381 Sized_relobj
<32, false>* object
,
382 unsigned int shndx
, Output_section
* output_section
,
383 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
385 this->copy_relocs_
.copy_reloc(symtab
, layout
,
386 symtab
->get_sized_symbol
<32>(sym
),
387 object
, shndx
, output_section
, reloc
,
388 this->rel_dyn_section(layout
));
391 // Information about this specific target which we pass to the
392 // general Target structure.
393 static const Target::Target_info i386_info
;
395 // The types of GOT entries needed for this platform.
398 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
399 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
400 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
401 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
402 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
406 Output_data_got
<32, false>* got_
;
408 Output_data_plt_i386
* plt_
;
409 // The GOT PLT section.
410 Output_data_space
* got_plt_
;
411 // The dynamic reloc section.
412 Reloc_section
* rel_dyn_
;
413 // Relocs saved to avoid a COPY reloc.
414 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
415 // Space for variables copied with a COPY reloc.
416 Output_data_space
* dynbss_
;
417 // Offset of the GOT entry for the TLS module index.
418 unsigned int got_mod_index_offset_
;
419 // True if the _TLS_MODULE_BASE_ symbol has been defined.
420 bool tls_base_symbol_defined_
;
423 const Target::Target_info
Target_i386::i386_info
=
426 false, // is_big_endian
427 elfcpp::EM_386
, // machine_code
428 false, // has_make_symbol
429 false, // has_resolve
430 true, // has_code_fill
431 true, // is_default_stack_executable
433 "/usr/lib/libc.so.1", // dynamic_linker
434 0x08048000, // default_text_segment_address
435 0x1000, // abi_pagesize (overridable by -z max-page-size)
436 0x1000, // common_pagesize (overridable by -z common-page-size)
437 elfcpp::SHN_UNDEF
, // small_common_shndx
438 elfcpp::SHN_UNDEF
, // large_common_shndx
439 0, // small_common_section_flags
440 0 // large_common_section_flags
443 // Get the GOT section, creating it if necessary.
445 Output_data_got
<32, false>*
446 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
448 if (this->got_
== NULL
)
450 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
452 this->got_
= new Output_data_got
<32, false>();
455 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
457 | elfcpp::SHF_WRITE
),
461 // The old GNU linker creates a .got.plt section. We just
462 // create another set of data in the .got section. Note that we
463 // always create a PLT if we create a GOT, although the PLT
465 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
466 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
468 | elfcpp::SHF_WRITE
),
472 // The first three entries are reserved.
473 this->got_plt_
->set_current_data_size(3 * 4);
475 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
476 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
478 0, 0, elfcpp::STT_OBJECT
,
480 elfcpp::STV_HIDDEN
, 0,
487 // Get the dynamic reloc section, creating it if necessary.
489 Target_i386::Reloc_section
*
490 Target_i386::rel_dyn_section(Layout
* layout
)
492 if (this->rel_dyn_
== NULL
)
494 gold_assert(layout
!= NULL
);
495 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
496 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
497 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
499 return this->rel_dyn_
;
502 // A class to handle the PLT data.
504 class Output_data_plt_i386
: public Output_section_data
507 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
509 Output_data_plt_i386(Layout
*, Output_data_space
*);
511 // Add an entry to the PLT.
513 add_entry(Symbol
* gsym
);
515 // Return the .rel.plt section data.
518 { return this->rel_
; }
522 do_adjust_output_section(Output_section
* os
);
524 // Write to a map file.
526 do_print_to_mapfile(Mapfile
* mapfile
) const
527 { mapfile
->print_output_data(this, _("** PLT")); }
530 // The size of an entry in the PLT.
531 static const int plt_entry_size
= 16;
533 // The first entry in the PLT for an executable.
534 static unsigned char exec_first_plt_entry
[plt_entry_size
];
536 // The first entry in the PLT for a shared object.
537 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
539 // Other entries in the PLT for an executable.
540 static unsigned char exec_plt_entry
[plt_entry_size
];
542 // Other entries in the PLT for a shared object.
543 static unsigned char dyn_plt_entry
[plt_entry_size
];
545 // Set the final size.
547 set_final_data_size()
548 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
550 // Write out the PLT data.
552 do_write(Output_file
*);
554 // The reloc section.
556 // The .got.plt section.
557 Output_data_space
* got_plt_
;
558 // The number of PLT entries.
562 // Create the PLT section. The ordinary .got section is an argument,
563 // since we need to refer to the start. We also create our own .got
564 // section just for PLT entries.
566 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
567 Output_data_space
* got_plt
)
568 : Output_section_data(4), got_plt_(got_plt
), count_(0)
570 this->rel_
= new Reloc_section(false);
571 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
572 elfcpp::SHF_ALLOC
, this->rel_
);
576 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
578 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
579 // linker, and so do we.
583 // Add an entry to the PLT.
586 Output_data_plt_i386::add_entry(Symbol
* gsym
)
588 gold_assert(!gsym
->has_plt_offset());
590 // Note that when setting the PLT offset we skip the initial
591 // reserved PLT entry.
592 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
596 section_offset_type got_offset
= this->got_plt_
->current_data_size();
598 // Every PLT entry needs a GOT entry which points back to the PLT
599 // entry (this will be changed by the dynamic linker, normally
600 // lazily when the function is called).
601 this->got_plt_
->set_current_data_size(got_offset
+ 4);
603 // Every PLT entry needs a reloc.
604 gsym
->set_needs_dynsym_entry();
605 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
608 // Note that we don't need to save the symbol. The contents of the
609 // PLT are independent of which symbols are used. The symbols only
610 // appear in the relocations.
613 // The first entry in the PLT for an executable.
615 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
617 0xff, 0x35, // pushl contents of memory address
618 0, 0, 0, 0, // replaced with address of .got + 4
619 0xff, 0x25, // jmp indirect
620 0, 0, 0, 0, // replaced with address of .got + 8
624 // The first entry in the PLT for a shared object.
626 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
628 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
629 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
633 // Subsequent entries in the PLT for an executable.
635 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
637 0xff, 0x25, // jmp indirect
638 0, 0, 0, 0, // replaced with address of symbol in .got
639 0x68, // pushl immediate
640 0, 0, 0, 0, // replaced with offset into relocation table
641 0xe9, // jmp relative
642 0, 0, 0, 0 // replaced with offset to start of .plt
645 // Subsequent entries in the PLT for a shared object.
647 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
649 0xff, 0xa3, // jmp *offset(%ebx)
650 0, 0, 0, 0, // replaced with offset of symbol in .got
651 0x68, // pushl immediate
652 0, 0, 0, 0, // replaced with offset into relocation table
653 0xe9, // jmp relative
654 0, 0, 0, 0 // replaced with offset to start of .plt
657 // Write out the PLT. This uses the hand-coded instructions above,
658 // and adjusts them as needed. This is all specified by the i386 ELF
659 // Processor Supplement.
662 Output_data_plt_i386::do_write(Output_file
* of
)
664 const off_t offset
= this->offset();
665 const section_size_type oview_size
=
666 convert_to_section_size_type(this->data_size());
667 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
669 const off_t got_file_offset
= this->got_plt_
->offset();
670 const section_size_type got_size
=
671 convert_to_section_size_type(this->got_plt_
->data_size());
672 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
675 unsigned char* pov
= oview
;
677 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
678 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
680 if (parameters
->options().shared())
681 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
684 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
685 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
686 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
688 pov
+= plt_entry_size
;
690 unsigned char* got_pov
= got_view
;
692 memset(got_pov
, 0, 12);
695 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
697 unsigned int plt_offset
= plt_entry_size
;
698 unsigned int plt_rel_offset
= 0;
699 unsigned int got_offset
= 12;
700 const unsigned int count
= this->count_
;
701 for (unsigned int i
= 0;
704 pov
+= plt_entry_size
,
706 plt_offset
+= plt_entry_size
,
707 plt_rel_offset
+= rel_size
,
710 // Set and adjust the PLT entry itself.
712 if (parameters
->options().shared())
714 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
715 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
719 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
720 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
725 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
726 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
727 - (plt_offset
+ plt_entry_size
));
729 // Set the entry in the GOT.
730 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
733 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
734 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
736 of
->write_output_view(offset
, oview_size
, oview
);
737 of
->write_output_view(got_file_offset
, got_size
, got_view
);
740 // Create a PLT entry for a global symbol.
743 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
745 if (gsym
->has_plt_offset())
748 if (this->plt_
== NULL
)
750 // Create the GOT sections first.
751 this->got_section(symtab
, layout
);
753 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
754 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
756 | elfcpp::SHF_EXECINSTR
),
760 this->plt_
->add_entry(gsym
);
763 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
766 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
768 if (this->tls_base_symbol_defined_
)
771 Output_segment
* tls_segment
= layout
->tls_segment();
772 if (tls_segment
!= NULL
)
774 bool is_exec
= parameters
->options().output_is_executable();
775 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
779 elfcpp::STV_HIDDEN
, 0,
781 ? Symbol::SEGMENT_END
782 : Symbol::SEGMENT_START
),
785 this->tls_base_symbol_defined_
= true;
788 // Create a GOT entry for the TLS module index.
791 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
792 Sized_relobj
<32, false>* object
)
794 if (this->got_mod_index_offset_
== -1U)
796 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
797 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
798 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
799 unsigned int got_offset
= got
->add_constant(0);
800 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
802 got
->add_constant(0);
803 this->got_mod_index_offset_
= got_offset
;
805 return this->got_mod_index_offset_
;
808 // Optimize the TLS relocation type based on what we know about the
809 // symbol. IS_FINAL is true if the final address of this symbol is
810 // known at link time.
812 tls::Tls_optimization
813 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
815 // If we are generating a shared library, then we can't do anything
817 if (parameters
->options().shared())
818 return tls::TLSOPT_NONE
;
822 case elfcpp::R_386_TLS_GD
:
823 case elfcpp::R_386_TLS_GOTDESC
:
824 case elfcpp::R_386_TLS_DESC_CALL
:
825 // These are General-Dynamic which permits fully general TLS
826 // access. Since we know that we are generating an executable,
827 // we can convert this to Initial-Exec. If we also know that
828 // this is a local symbol, we can further switch to Local-Exec.
830 return tls::TLSOPT_TO_LE
;
831 return tls::TLSOPT_TO_IE
;
833 case elfcpp::R_386_TLS_LDM
:
834 // This is Local-Dynamic, which refers to a local symbol in the
835 // dynamic TLS block. Since we know that we generating an
836 // executable, we can switch to Local-Exec.
837 return tls::TLSOPT_TO_LE
;
839 case elfcpp::R_386_TLS_LDO_32
:
840 // Another type of Local-Dynamic relocation.
841 return tls::TLSOPT_TO_LE
;
843 case elfcpp::R_386_TLS_IE
:
844 case elfcpp::R_386_TLS_GOTIE
:
845 case elfcpp::R_386_TLS_IE_32
:
846 // These are Initial-Exec relocs which get the thread offset
847 // from the GOT. If we know that we are linking against the
848 // local symbol, we can switch to Local-Exec, which links the
849 // thread offset into the instruction.
851 return tls::TLSOPT_TO_LE
;
852 return tls::TLSOPT_NONE
;
854 case elfcpp::R_386_TLS_LE
:
855 case elfcpp::R_386_TLS_LE_32
:
856 // When we already have Local-Exec, there is nothing further we
858 return tls::TLSOPT_NONE
;
865 // Report an unsupported relocation against a local symbol.
868 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
871 gold_error(_("%s: unsupported reloc %u against local symbol"),
872 object
->name().c_str(), r_type
);
875 // Scan a relocation for a local symbol.
878 Target_i386::Scan::local(const General_options
&,
879 Symbol_table
* symtab
,
882 Sized_relobj
<32, false>* object
,
883 unsigned int data_shndx
,
884 Output_section
* output_section
,
885 const elfcpp::Rel
<32, false>& reloc
,
887 const elfcpp::Sym
<32, false>& lsym
)
891 case elfcpp::R_386_NONE
:
892 case elfcpp::R_386_GNU_VTINHERIT
:
893 case elfcpp::R_386_GNU_VTENTRY
:
896 case elfcpp::R_386_32
:
897 // If building a shared library (or a position-independent
898 // executable), we need to create a dynamic relocation for
899 // this location. The relocation applied at link time will
900 // apply the link-time value, so we flag the location with
901 // an R_386_RELATIVE relocation so the dynamic loader can
902 // relocate it easily.
903 if (parameters
->options().output_is_position_independent())
905 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
906 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
907 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
908 output_section
, data_shndx
,
909 reloc
.get_r_offset());
913 case elfcpp::R_386_16
:
914 case elfcpp::R_386_8
:
915 // If building a shared library (or a position-independent
916 // executable), we need to create a dynamic relocation for
917 // this location. Because the addend needs to remain in the
918 // data section, we need to be careful not to apply this
919 // relocation statically.
920 if (parameters
->options().output_is_position_independent())
922 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
923 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
924 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
925 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
926 data_shndx
, reloc
.get_r_offset());
929 gold_assert(lsym
.get_st_value() == 0);
930 unsigned int shndx
= lsym
.get_st_shndx();
932 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
935 object
->error(_("section symbol %u has bad shndx %u"),
938 rel_dyn
->add_local_section(object
, shndx
,
939 r_type
, output_section
,
940 data_shndx
, reloc
.get_r_offset());
945 case elfcpp::R_386_PC32
:
946 case elfcpp::R_386_PC16
:
947 case elfcpp::R_386_PC8
:
950 case elfcpp::R_386_PLT32
:
951 // Since we know this is a local symbol, we can handle this as a
955 case elfcpp::R_386_GOTOFF
:
956 case elfcpp::R_386_GOTPC
:
957 // We need a GOT section.
958 target
->got_section(symtab
, layout
);
961 case elfcpp::R_386_GOT32
:
963 // The symbol requires a GOT entry.
964 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
965 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
966 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
968 // If we are generating a shared object, we need to add a
969 // dynamic RELATIVE relocation for this symbol's GOT entry.
970 if (parameters
->options().output_is_position_independent())
972 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
973 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
974 rel_dyn
->add_local_relative(
975 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
976 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
982 // These are relocations which should only be seen by the
983 // dynamic linker, and should never be seen here.
984 case elfcpp::R_386_COPY
:
985 case elfcpp::R_386_GLOB_DAT
:
986 case elfcpp::R_386_JUMP_SLOT
:
987 case elfcpp::R_386_RELATIVE
:
988 case elfcpp::R_386_TLS_TPOFF
:
989 case elfcpp::R_386_TLS_DTPMOD32
:
990 case elfcpp::R_386_TLS_DTPOFF32
:
991 case elfcpp::R_386_TLS_TPOFF32
:
992 case elfcpp::R_386_TLS_DESC
:
993 gold_error(_("%s: unexpected reloc %u in object file"),
994 object
->name().c_str(), r_type
);
997 // These are initial TLS relocs, which are expected when
999 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1000 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1001 case elfcpp::R_386_TLS_DESC_CALL
:
1002 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1003 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1004 case elfcpp::R_386_TLS_IE
: // Initial-exec
1005 case elfcpp::R_386_TLS_IE_32
:
1006 case elfcpp::R_386_TLS_GOTIE
:
1007 case elfcpp::R_386_TLS_LE
: // Local-exec
1008 case elfcpp::R_386_TLS_LE_32
:
1010 bool output_is_shared
= parameters
->options().shared();
1011 const tls::Tls_optimization optimized_type
1012 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1015 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1016 if (optimized_type
== tls::TLSOPT_NONE
)
1018 // Create a pair of GOT entries for the module index and
1019 // dtv-relative offset.
1020 Output_data_got
<32, false>* got
1021 = target
->got_section(symtab
, layout
);
1022 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1023 unsigned int shndx
= lsym
.get_st_shndx();
1025 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1027 object
->error(_("local symbol %u has bad shndx %u"),
1030 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1032 target
->rel_dyn_section(layout
),
1033 elfcpp::R_386_TLS_DTPMOD32
, 0);
1035 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1036 unsupported_reloc_local(object
, r_type
);
1039 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1040 target
->define_tls_base_symbol(symtab
, layout
);
1041 if (optimized_type
== tls::TLSOPT_NONE
)
1043 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1044 Output_data_got
<32, false>* got
1045 = target
->got_section(symtab
, layout
);
1046 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1047 unsigned int shndx
= lsym
.get_st_shndx();
1049 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1051 object
->error(_("local symbol %u has bad shndx %u"),
1054 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1056 target
->rel_dyn_section(layout
),
1057 elfcpp::R_386_TLS_DESC
, 0);
1059 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1060 unsupported_reloc_local(object
, r_type
);
1063 case elfcpp::R_386_TLS_DESC_CALL
:
1066 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1067 if (optimized_type
== tls::TLSOPT_NONE
)
1069 // Create a GOT entry for the module index.
1070 target
->got_mod_index_entry(symtab
, layout
, object
);
1072 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1073 unsupported_reloc_local(object
, r_type
);
1076 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1079 case elfcpp::R_386_TLS_IE
: // Initial-exec
1080 case elfcpp::R_386_TLS_IE_32
:
1081 case elfcpp::R_386_TLS_GOTIE
:
1082 layout
->set_has_static_tls();
1083 if (optimized_type
== tls::TLSOPT_NONE
)
1085 // For the R_386_TLS_IE relocation, we need to create a
1086 // dynamic relocation when building a shared library.
1087 if (r_type
== elfcpp::R_386_TLS_IE
1088 && parameters
->options().shared())
1090 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1092 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1093 rel_dyn
->add_local_relative(object
, r_sym
,
1094 elfcpp::R_386_RELATIVE
,
1095 output_section
, data_shndx
,
1096 reloc
.get_r_offset());
1098 // Create a GOT entry for the tp-relative offset.
1099 Output_data_got
<32, false>* got
1100 = target
->got_section(symtab
, layout
);
1101 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1102 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1103 ? elfcpp::R_386_TLS_TPOFF32
1104 : elfcpp::R_386_TLS_TPOFF
);
1105 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1106 ? GOT_TYPE_TLS_OFFSET
1107 : GOT_TYPE_TLS_NOFFSET
);
1108 got
->add_local_with_rel(object
, r_sym
, got_type
,
1109 target
->rel_dyn_section(layout
),
1112 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1113 unsupported_reloc_local(object
, r_type
);
1116 case elfcpp::R_386_TLS_LE
: // Local-exec
1117 case elfcpp::R_386_TLS_LE_32
:
1118 layout
->set_has_static_tls();
1119 if (output_is_shared
)
1121 // We need to create a dynamic relocation.
1122 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1123 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1124 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1125 ? elfcpp::R_386_TLS_TPOFF32
1126 : elfcpp::R_386_TLS_TPOFF
);
1127 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1128 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1129 data_shndx
, reloc
.get_r_offset());
1139 case elfcpp::R_386_32PLT
:
1140 case elfcpp::R_386_TLS_GD_32
:
1141 case elfcpp::R_386_TLS_GD_PUSH
:
1142 case elfcpp::R_386_TLS_GD_CALL
:
1143 case elfcpp::R_386_TLS_GD_POP
:
1144 case elfcpp::R_386_TLS_LDM_32
:
1145 case elfcpp::R_386_TLS_LDM_PUSH
:
1146 case elfcpp::R_386_TLS_LDM_CALL
:
1147 case elfcpp::R_386_TLS_LDM_POP
:
1148 case elfcpp::R_386_USED_BY_INTEL_200
:
1150 unsupported_reloc_local(object
, r_type
);
1155 // Report an unsupported relocation against a global symbol.
1158 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1159 unsigned int r_type
,
1162 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1163 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1166 // Scan a relocation for a global symbol.
1169 Target_i386::Scan::global(const General_options
&,
1170 Symbol_table
* symtab
,
1172 Target_i386
* target
,
1173 Sized_relobj
<32, false>* object
,
1174 unsigned int data_shndx
,
1175 Output_section
* output_section
,
1176 const elfcpp::Rel
<32, false>& reloc
,
1177 unsigned int r_type
,
1182 case elfcpp::R_386_NONE
:
1183 case elfcpp::R_386_GNU_VTINHERIT
:
1184 case elfcpp::R_386_GNU_VTENTRY
:
1187 case elfcpp::R_386_32
:
1188 case elfcpp::R_386_16
:
1189 case elfcpp::R_386_8
:
1191 // Make a PLT entry if necessary.
1192 if (gsym
->needs_plt_entry())
1194 target
->make_plt_entry(symtab
, layout
, gsym
);
1195 // Since this is not a PC-relative relocation, we may be
1196 // taking the address of a function. In that case we need to
1197 // set the entry in the dynamic symbol table to the address of
1199 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1200 gsym
->set_needs_dynsym_value();
1202 // Make a dynamic relocation if necessary.
1203 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1205 if (gsym
->may_need_copy_reloc())
1207 target
->copy_reloc(symtab
, layout
, object
,
1208 data_shndx
, output_section
, gsym
, reloc
);
1210 else if (r_type
== elfcpp::R_386_32
1211 && gsym
->can_use_relative_reloc(false))
1213 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1214 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1215 output_section
, object
,
1216 data_shndx
, reloc
.get_r_offset());
1220 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1221 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1222 data_shndx
, reloc
.get_r_offset());
1228 case elfcpp::R_386_PC32
:
1229 case elfcpp::R_386_PC16
:
1230 case elfcpp::R_386_PC8
:
1232 // Make a PLT entry if necessary.
1233 if (gsym
->needs_plt_entry())
1235 // These relocations are used for function calls only in
1236 // non-PIC code. For a 32-bit relocation in a shared library,
1237 // we'll need a text relocation anyway, so we can skip the
1238 // PLT entry and let the dynamic linker bind the call directly
1239 // to the target. For smaller relocations, we should use a
1240 // PLT entry to ensure that the call can reach.
1241 if (!parameters
->options().shared()
1242 || r_type
!= elfcpp::R_386_PC32
)
1243 target
->make_plt_entry(symtab
, layout
, gsym
);
1245 // Make a dynamic relocation if necessary.
1246 int flags
= Symbol::NON_PIC_REF
;
1247 if (gsym
->type() == elfcpp::STT_FUNC
)
1248 flags
|= Symbol::FUNCTION_CALL
;
1249 if (gsym
->needs_dynamic_reloc(flags
))
1251 if (gsym
->may_need_copy_reloc())
1253 target
->copy_reloc(symtab
, layout
, object
,
1254 data_shndx
, output_section
, gsym
, reloc
);
1258 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1259 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1260 data_shndx
, reloc
.get_r_offset());
1266 case elfcpp::R_386_GOT32
:
1268 // The symbol requires a GOT entry.
1269 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1270 if (gsym
->final_value_is_known())
1271 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1274 // If this symbol is not fully resolved, we need to add a
1275 // GOT entry with a dynamic relocation.
1276 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1277 if (gsym
->is_from_dynobj()
1278 || gsym
->is_undefined()
1279 || gsym
->is_preemptible())
1280 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1281 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1284 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1285 rel_dyn
->add_global_relative(
1286 gsym
, elfcpp::R_386_RELATIVE
, got
,
1287 gsym
->got_offset(GOT_TYPE_STANDARD
));
1293 case elfcpp::R_386_PLT32
:
1294 // If the symbol is fully resolved, this is just a PC32 reloc.
1295 // Otherwise we need a PLT entry.
1296 if (gsym
->final_value_is_known())
1298 // If building a shared library, we can also skip the PLT entry
1299 // if the symbol is defined in the output file and is protected
1301 if (gsym
->is_defined()
1302 && !gsym
->is_from_dynobj()
1303 && !gsym
->is_preemptible())
1305 target
->make_plt_entry(symtab
, layout
, gsym
);
1308 case elfcpp::R_386_GOTOFF
:
1309 case elfcpp::R_386_GOTPC
:
1310 // We need a GOT section.
1311 target
->got_section(symtab
, layout
);
1314 // These are relocations which should only be seen by the
1315 // dynamic linker, and should never be seen here.
1316 case elfcpp::R_386_COPY
:
1317 case elfcpp::R_386_GLOB_DAT
:
1318 case elfcpp::R_386_JUMP_SLOT
:
1319 case elfcpp::R_386_RELATIVE
:
1320 case elfcpp::R_386_TLS_TPOFF
:
1321 case elfcpp::R_386_TLS_DTPMOD32
:
1322 case elfcpp::R_386_TLS_DTPOFF32
:
1323 case elfcpp::R_386_TLS_TPOFF32
:
1324 case elfcpp::R_386_TLS_DESC
:
1325 gold_error(_("%s: unexpected reloc %u in object file"),
1326 object
->name().c_str(), r_type
);
1329 // These are initial tls relocs, which are expected when
1331 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1332 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1333 case elfcpp::R_386_TLS_DESC_CALL
:
1334 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1335 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1336 case elfcpp::R_386_TLS_IE
: // Initial-exec
1337 case elfcpp::R_386_TLS_IE_32
:
1338 case elfcpp::R_386_TLS_GOTIE
:
1339 case elfcpp::R_386_TLS_LE
: // Local-exec
1340 case elfcpp::R_386_TLS_LE_32
:
1342 const bool is_final
= gsym
->final_value_is_known();
1343 const tls::Tls_optimization optimized_type
1344 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1347 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1348 if (optimized_type
== tls::TLSOPT_NONE
)
1350 // Create a pair of GOT entries for the module index and
1351 // dtv-relative offset.
1352 Output_data_got
<32, false>* got
1353 = target
->got_section(symtab
, layout
);
1354 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1355 target
->rel_dyn_section(layout
),
1356 elfcpp::R_386_TLS_DTPMOD32
,
1357 elfcpp::R_386_TLS_DTPOFF32
);
1359 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1361 // Create a GOT entry for the tp-relative offset.
1362 Output_data_got
<32, false>* got
1363 = target
->got_section(symtab
, layout
);
1364 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1365 target
->rel_dyn_section(layout
),
1366 elfcpp::R_386_TLS_TPOFF
);
1368 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1369 unsupported_reloc_global(object
, r_type
, gsym
);
1372 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1373 target
->define_tls_base_symbol(symtab
, layout
);
1374 if (optimized_type
== tls::TLSOPT_NONE
)
1376 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1377 Output_data_got
<32, false>* got
1378 = target
->got_section(symtab
, layout
);
1379 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1380 target
->rel_dyn_section(layout
),
1381 elfcpp::R_386_TLS_DESC
, 0);
1383 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1385 // Create a GOT entry for the tp-relative offset.
1386 Output_data_got
<32, false>* got
1387 = target
->got_section(symtab
, layout
);
1388 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1389 target
->rel_dyn_section(layout
),
1390 elfcpp::R_386_TLS_TPOFF
);
1392 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1393 unsupported_reloc_global(object
, r_type
, gsym
);
1396 case elfcpp::R_386_TLS_DESC_CALL
:
1399 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1400 if (optimized_type
== tls::TLSOPT_NONE
)
1402 // Create a GOT entry for the module index.
1403 target
->got_mod_index_entry(symtab
, layout
, object
);
1405 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1406 unsupported_reloc_global(object
, r_type
, gsym
);
1409 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1412 case elfcpp::R_386_TLS_IE
: // Initial-exec
1413 case elfcpp::R_386_TLS_IE_32
:
1414 case elfcpp::R_386_TLS_GOTIE
:
1415 layout
->set_has_static_tls();
1416 if (optimized_type
== tls::TLSOPT_NONE
)
1418 // For the R_386_TLS_IE relocation, we need to create a
1419 // dynamic relocation when building a shared library.
1420 if (r_type
== elfcpp::R_386_TLS_IE
1421 && parameters
->options().shared())
1423 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1424 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1425 output_section
, object
,
1427 reloc
.get_r_offset());
1429 // Create a GOT entry for the tp-relative offset.
1430 Output_data_got
<32, false>* got
1431 = target
->got_section(symtab
, layout
);
1432 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1433 ? elfcpp::R_386_TLS_TPOFF32
1434 : elfcpp::R_386_TLS_TPOFF
);
1435 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1436 ? GOT_TYPE_TLS_OFFSET
1437 : GOT_TYPE_TLS_NOFFSET
);
1438 got
->add_global_with_rel(gsym
, got_type
,
1439 target
->rel_dyn_section(layout
),
1442 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1443 unsupported_reloc_global(object
, r_type
, gsym
);
1446 case elfcpp::R_386_TLS_LE
: // Local-exec
1447 case elfcpp::R_386_TLS_LE_32
:
1448 layout
->set_has_static_tls();
1449 if (parameters
->options().shared())
1451 // We need to create a dynamic relocation.
1452 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1453 ? elfcpp::R_386_TLS_TPOFF32
1454 : elfcpp::R_386_TLS_TPOFF
);
1455 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1456 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1457 data_shndx
, reloc
.get_r_offset());
1467 case elfcpp::R_386_32PLT
:
1468 case elfcpp::R_386_TLS_GD_32
:
1469 case elfcpp::R_386_TLS_GD_PUSH
:
1470 case elfcpp::R_386_TLS_GD_CALL
:
1471 case elfcpp::R_386_TLS_GD_POP
:
1472 case elfcpp::R_386_TLS_LDM_32
:
1473 case elfcpp::R_386_TLS_LDM_PUSH
:
1474 case elfcpp::R_386_TLS_LDM_CALL
:
1475 case elfcpp::R_386_TLS_LDM_POP
:
1476 case elfcpp::R_386_USED_BY_INTEL_200
:
1478 unsupported_reloc_global(object
, r_type
, gsym
);
1483 // Process relocations for gc.
1486 Target_i386::gc_process_relocs(const General_options
& options
,
1487 Symbol_table
* symtab
,
1489 Sized_relobj
<32, false>* object
,
1490 unsigned int data_shndx
,
1492 const unsigned char* prelocs
,
1494 Output_section
* output_section
,
1495 bool needs_special_offset_handling
,
1496 size_t local_symbol_count
,
1497 const unsigned char* plocal_symbols
)
1499 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1510 needs_special_offset_handling
,
1515 // Scan relocations for a section.
1518 Target_i386::scan_relocs(const General_options
& options
,
1519 Symbol_table
* symtab
,
1521 Sized_relobj
<32, false>* object
,
1522 unsigned int data_shndx
,
1523 unsigned int sh_type
,
1524 const unsigned char* prelocs
,
1526 Output_section
* output_section
,
1527 bool needs_special_offset_handling
,
1528 size_t local_symbol_count
,
1529 const unsigned char* plocal_symbols
)
1531 if (sh_type
== elfcpp::SHT_RELA
)
1533 gold_error(_("%s: unsupported RELA reloc section"),
1534 object
->name().c_str());
1538 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1549 needs_special_offset_handling
,
1554 // Finalize the sections.
1557 Target_i386::do_finalize_sections(Layout
* layout
)
1559 // Fill in some more dynamic tags.
1560 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1563 if (this->got_plt_
!= NULL
)
1564 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1566 if (this->plt_
!= NULL
)
1568 const Output_data
* od
= this->plt_
->rel_plt();
1569 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1570 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1571 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1574 if (this->rel_dyn_
!= NULL
)
1576 const Output_data
* od
= this->rel_dyn_
;
1577 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1578 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1579 odyn
->add_constant(elfcpp::DT_RELENT
,
1580 elfcpp::Elf_sizes
<32>::rel_size
);
1583 if (!parameters
->options().shared())
1585 // The value of the DT_DEBUG tag is filled in by the dynamic
1586 // linker at run time, and used by the debugger.
1587 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1591 // Emit any relocs we saved in an attempt to avoid generating COPY
1593 if (this->copy_relocs_
.any_saved_relocs())
1594 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1597 // Return whether a direct absolute static relocation needs to be applied.
1598 // In cases where Scan::local() or Scan::global() has created
1599 // a dynamic relocation other than R_386_RELATIVE, the addend
1600 // of the relocation is carried in the data, and we must not
1601 // apply the static relocation.
1604 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1607 Output_section
* output_section
)
1609 // If the output section is not allocated, then we didn't call
1610 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1612 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1615 // For local symbols, we will have created a non-RELATIVE dynamic
1616 // relocation only if (a) the output is position independent,
1617 // (b) the relocation is absolute (not pc- or segment-relative), and
1618 // (c) the relocation is not 32 bits wide.
1620 return !(parameters
->options().output_is_position_independent()
1621 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1624 // For global symbols, we use the same helper routines used in the
1625 // scan pass. If we did not create a dynamic relocation, or if we
1626 // created a RELATIVE dynamic relocation, we should apply the static
1628 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1629 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1630 && gsym
->can_use_relative_reloc(ref_flags
1631 & Symbol::FUNCTION_CALL
);
1632 return !has_dyn
|| is_rel
;
1635 // Perform a relocation.
1638 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1639 Target_i386
* target
,
1640 Output_section
*output_section
,
1642 const elfcpp::Rel
<32, false>& rel
,
1643 unsigned int r_type
,
1644 const Sized_symbol
<32>* gsym
,
1645 const Symbol_value
<32>* psymval
,
1646 unsigned char* view
,
1647 elfcpp::Elf_types
<32>::Elf_Addr address
,
1648 section_size_type view_size
)
1650 if (this->skip_call_tls_get_addr_
)
1652 if ((r_type
!= elfcpp::R_386_PLT32
1653 && r_type
!= elfcpp::R_386_PC32
)
1655 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1656 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1657 _("missing expected TLS relocation"));
1660 this->skip_call_tls_get_addr_
= false;
1665 // Pick the value to use for symbols defined in shared objects.
1666 Symbol_value
<32> symval
;
1668 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1669 || r_type
== elfcpp::R_386_PC16
1670 || r_type
== elfcpp::R_386_PC32
))
1672 symval
.set_output_value(target
->plt_section()->address()
1673 + gsym
->plt_offset());
1677 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1679 // Get the GOT offset if needed.
1680 // The GOT pointer points to the end of the GOT section.
1681 // We need to subtract the size of the GOT section to get
1682 // the actual offset to use in the relocation.
1683 bool have_got_offset
= false;
1684 unsigned int got_offset
= 0;
1687 case elfcpp::R_386_GOT32
:
1690 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1691 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1692 - target
->got_size());
1696 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1697 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1698 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1699 - target
->got_size());
1701 have_got_offset
= true;
1710 case elfcpp::R_386_NONE
:
1711 case elfcpp::R_386_GNU_VTINHERIT
:
1712 case elfcpp::R_386_GNU_VTENTRY
:
1715 case elfcpp::R_386_32
:
1716 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1718 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1721 case elfcpp::R_386_PC32
:
1723 int ref_flags
= Symbol::NON_PIC_REF
;
1724 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1725 ref_flags
|= Symbol::FUNCTION_CALL
;
1726 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1727 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1731 case elfcpp::R_386_16
:
1732 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1734 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1737 case elfcpp::R_386_PC16
:
1739 int ref_flags
= Symbol::NON_PIC_REF
;
1740 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1741 ref_flags
|= Symbol::FUNCTION_CALL
;
1742 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1743 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1747 case elfcpp::R_386_8
:
1748 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1750 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1753 case elfcpp::R_386_PC8
:
1755 int ref_flags
= Symbol::NON_PIC_REF
;
1756 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1757 ref_flags
|= Symbol::FUNCTION_CALL
;
1758 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1760 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1764 case elfcpp::R_386_PLT32
:
1765 gold_assert(gsym
== NULL
1766 || gsym
->has_plt_offset()
1767 || gsym
->final_value_is_known()
1768 || (gsym
->is_defined()
1769 && !gsym
->is_from_dynobj()
1770 && !gsym
->is_preemptible()));
1771 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1774 case elfcpp::R_386_GOT32
:
1775 gold_assert(have_got_offset
);
1776 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1779 case elfcpp::R_386_GOTOFF
:
1781 elfcpp::Elf_types
<32>::Elf_Addr value
;
1782 value
= (psymval
->value(object
, 0)
1783 - target
->got_plt_section()->address());
1784 Relocate_functions
<32, false>::rel32(view
, value
);
1788 case elfcpp::R_386_GOTPC
:
1790 elfcpp::Elf_types
<32>::Elf_Addr value
;
1791 value
= target
->got_plt_section()->address();
1792 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1796 case elfcpp::R_386_COPY
:
1797 case elfcpp::R_386_GLOB_DAT
:
1798 case elfcpp::R_386_JUMP_SLOT
:
1799 case elfcpp::R_386_RELATIVE
:
1800 // These are outstanding tls relocs, which are unexpected when
1802 case elfcpp::R_386_TLS_TPOFF
:
1803 case elfcpp::R_386_TLS_DTPMOD32
:
1804 case elfcpp::R_386_TLS_DTPOFF32
:
1805 case elfcpp::R_386_TLS_TPOFF32
:
1806 case elfcpp::R_386_TLS_DESC
:
1807 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1808 _("unexpected reloc %u in object file"),
1812 // These are initial tls relocs, which are expected when
1814 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1815 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1816 case elfcpp::R_386_TLS_DESC_CALL
:
1817 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1818 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1819 case elfcpp::R_386_TLS_IE
: // Initial-exec
1820 case elfcpp::R_386_TLS_IE_32
:
1821 case elfcpp::R_386_TLS_GOTIE
:
1822 case elfcpp::R_386_TLS_LE
: // Local-exec
1823 case elfcpp::R_386_TLS_LE_32
:
1824 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1825 view
, address
, view_size
);
1828 case elfcpp::R_386_32PLT
:
1829 case elfcpp::R_386_TLS_GD_32
:
1830 case elfcpp::R_386_TLS_GD_PUSH
:
1831 case elfcpp::R_386_TLS_GD_CALL
:
1832 case elfcpp::R_386_TLS_GD_POP
:
1833 case elfcpp::R_386_TLS_LDM_32
:
1834 case elfcpp::R_386_TLS_LDM_PUSH
:
1835 case elfcpp::R_386_TLS_LDM_CALL
:
1836 case elfcpp::R_386_TLS_LDM_POP
:
1837 case elfcpp::R_386_USED_BY_INTEL_200
:
1839 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1840 _("unsupported reloc %u"),
1848 // Perform a TLS relocation.
1851 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1852 Target_i386
* target
,
1854 const elfcpp::Rel
<32, false>& rel
,
1855 unsigned int r_type
,
1856 const Sized_symbol
<32>* gsym
,
1857 const Symbol_value
<32>* psymval
,
1858 unsigned char* view
,
1859 elfcpp::Elf_types
<32>::Elf_Addr
,
1860 section_size_type view_size
)
1862 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1864 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1866 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1868 const bool is_final
=
1870 ? !parameters
->options().output_is_position_independent()
1871 : gsym
->final_value_is_known());
1872 const tls::Tls_optimization optimized_type
1873 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1876 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1877 if (optimized_type
== tls::TLSOPT_TO_LE
)
1879 gold_assert(tls_segment
!= NULL
);
1880 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1881 rel
, r_type
, value
, view
,
1887 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1888 ? GOT_TYPE_TLS_NOFFSET
1889 : GOT_TYPE_TLS_PAIR
);
1890 unsigned int got_offset
;
1893 gold_assert(gsym
->has_got_offset(got_type
));
1894 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1898 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1899 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1900 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1901 - target
->got_size());
1903 if (optimized_type
== tls::TLSOPT_TO_IE
)
1905 gold_assert(tls_segment
!= NULL
);
1906 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1907 got_offset
, view
, view_size
);
1910 else if (optimized_type
== tls::TLSOPT_NONE
)
1912 // Relocate the field with the offset of the pair of GOT
1914 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1918 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1919 _("unsupported reloc %u"),
1923 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1924 case elfcpp::R_386_TLS_DESC_CALL
:
1925 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1926 if (optimized_type
== tls::TLSOPT_TO_LE
)
1928 gold_assert(tls_segment
!= NULL
);
1929 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1930 rel
, r_type
, value
, view
,
1936 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1937 ? GOT_TYPE_TLS_NOFFSET
1938 : GOT_TYPE_TLS_DESC
);
1939 unsigned int got_offset
;
1942 gold_assert(gsym
->has_got_offset(got_type
));
1943 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1947 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1948 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1949 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1950 - target
->got_size());
1952 if (optimized_type
== tls::TLSOPT_TO_IE
)
1954 gold_assert(tls_segment
!= NULL
);
1955 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1956 got_offset
, view
, view_size
);
1959 else if (optimized_type
== tls::TLSOPT_NONE
)
1961 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1963 // Relocate the field with the offset of the pair of GOT
1965 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1970 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1971 _("unsupported reloc %u"),
1975 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1976 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1978 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1979 _("both SUN and GNU model "
1980 "TLS relocations"));
1983 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1984 if (optimized_type
== tls::TLSOPT_TO_LE
)
1986 gold_assert(tls_segment
!= NULL
);
1987 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1988 value
, view
, view_size
);
1991 else if (optimized_type
== tls::TLSOPT_NONE
)
1993 // Relocate the field with the offset of the GOT entry for
1994 // the module index.
1995 unsigned int got_offset
;
1996 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1997 - target
->got_size());
1998 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2001 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2002 _("unsupported reloc %u"),
2006 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2007 // This reloc can appear in debugging sections, in which case we
2008 // won't see the TLS_LDM reloc. The local_dynamic_type field
2010 if (optimized_type
== tls::TLSOPT_TO_LE
2011 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
2013 gold_assert(tls_segment
!= NULL
);
2014 value
-= tls_segment
->memsz();
2016 Relocate_functions
<32, false>::rel32(view
, value
);
2019 case elfcpp::R_386_TLS_IE
: // Initial-exec
2020 case elfcpp::R_386_TLS_GOTIE
:
2021 case elfcpp::R_386_TLS_IE_32
:
2022 if (optimized_type
== tls::TLSOPT_TO_LE
)
2024 gold_assert(tls_segment
!= NULL
);
2025 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2026 rel
, r_type
, value
, view
,
2030 else if (optimized_type
== tls::TLSOPT_NONE
)
2032 // Relocate the field with the offset of the GOT entry for
2033 // the tp-relative offset of the symbol.
2034 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2035 ? GOT_TYPE_TLS_OFFSET
2036 : GOT_TYPE_TLS_NOFFSET
);
2037 unsigned int got_offset
;
2040 gold_assert(gsym
->has_got_offset(got_type
));
2041 got_offset
= gsym
->got_offset(got_type
);
2045 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2046 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2047 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2049 // For the R_386_TLS_IE relocation, we need to apply the
2050 // absolute address of the GOT entry.
2051 if (r_type
== elfcpp::R_386_TLS_IE
)
2052 got_offset
+= target
->got_plt_section()->address();
2053 // All GOT offsets are relative to the end of the GOT.
2054 got_offset
-= target
->got_size();
2055 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2058 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2059 _("unsupported reloc %u"),
2063 case elfcpp::R_386_TLS_LE
: // Local-exec
2064 // If we're creating a shared library, a dynamic relocation will
2065 // have been created for this location, so do not apply it now.
2066 if (!parameters
->options().shared())
2068 gold_assert(tls_segment
!= NULL
);
2069 value
-= tls_segment
->memsz();
2070 Relocate_functions
<32, false>::rel32(view
, value
);
2074 case elfcpp::R_386_TLS_LE_32
:
2075 // If we're creating a shared library, a dynamic relocation will
2076 // have been created for this location, so do not apply it now.
2077 if (!parameters
->options().shared())
2079 gold_assert(tls_segment
!= NULL
);
2080 value
= tls_segment
->memsz() - value
;
2081 Relocate_functions
<32, false>::rel32(view
, value
);
2087 // Do a relocation in which we convert a TLS General-Dynamic to a
2091 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2093 Output_segment
* tls_segment
,
2094 const elfcpp::Rel
<32, false>& rel
,
2096 elfcpp::Elf_types
<32>::Elf_Addr value
,
2097 unsigned char* view
,
2098 section_size_type view_size
)
2100 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2101 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2102 // leal foo(%reg),%eax; call ___tls_get_addr
2103 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2105 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2106 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2108 unsigned char op1
= view
[-1];
2109 unsigned char op2
= view
[-2];
2111 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2112 op2
== 0x8d || op2
== 0x04);
2113 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2119 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2120 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2121 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2122 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2123 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2127 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2128 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2129 if (rel
.get_r_offset() + 9 < view_size
2132 // There is a trailing nop. Use the size byte subl.
2133 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2138 // Use the five byte subl.
2139 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2143 value
= tls_segment
->memsz() - value
;
2144 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2146 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2148 this->skip_call_tls_get_addr_
= true;
2151 // Do a relocation in which we convert a TLS General-Dynamic to an
2155 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2158 const elfcpp::Rel
<32, false>& rel
,
2160 elfcpp::Elf_types
<32>::Elf_Addr value
,
2161 unsigned char* view
,
2162 section_size_type view_size
)
2164 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2165 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2167 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2168 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2170 unsigned char op1
= view
[-1];
2171 unsigned char op2
= view
[-2];
2173 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2174 op2
== 0x8d || op2
== 0x04);
2175 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2179 // FIXME: For now, support only the first (SIB) form.
2180 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2184 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2185 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2186 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2187 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2188 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2192 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2193 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2194 if (rel
.get_r_offset() + 9 < view_size
2197 // FIXME: This is not the right instruction sequence.
2198 // There is a trailing nop. Use the size byte subl.
2199 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2204 // FIXME: This is not the right instruction sequence.
2205 // Use the five byte subl.
2206 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2210 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2212 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2214 this->skip_call_tls_get_addr_
= true;
2217 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2218 // General-Dynamic to a Local-Exec.
2221 Target_i386::Relocate::tls_desc_gd_to_le(
2222 const Relocate_info
<32, false>* relinfo
,
2224 Output_segment
* tls_segment
,
2225 const elfcpp::Rel
<32, false>& rel
,
2226 unsigned int r_type
,
2227 elfcpp::Elf_types
<32>::Elf_Addr value
,
2228 unsigned char* view
,
2229 section_size_type view_size
)
2231 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2233 // leal foo@TLSDESC(%ebx), %eax
2234 // ==> leal foo@NTPOFF, %eax
2235 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2236 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2237 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2238 view
[-2] == 0x8d && view
[-1] == 0x83);
2240 value
-= tls_segment
->memsz();
2241 Relocate_functions
<32, false>::rel32(view
, value
);
2245 // call *foo@TLSCALL(%eax)
2247 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2248 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2249 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2250 view
[0] == 0xff && view
[1] == 0x10);
2256 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2257 // General-Dynamic to an Initial-Exec.
2260 Target_i386::Relocate::tls_desc_gd_to_ie(
2261 const Relocate_info
<32, false>* relinfo
,
2264 const elfcpp::Rel
<32, false>& rel
,
2265 unsigned int r_type
,
2266 elfcpp::Elf_types
<32>::Elf_Addr value
,
2267 unsigned char* view
,
2268 section_size_type view_size
)
2270 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2272 // leal foo@TLSDESC(%ebx), %eax
2273 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2274 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2275 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2276 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2277 view
[-2] == 0x8d && view
[-1] == 0x83);
2279 Relocate_functions
<32, false>::rel32(view
, value
);
2283 // call *foo@TLSCALL(%eax)
2285 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2286 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2287 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2288 view
[0] == 0xff && view
[1] == 0x10);
2294 // Do a relocation in which we convert a TLS Local-Dynamic to a
2298 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2301 const elfcpp::Rel
<32, false>& rel
,
2303 elfcpp::Elf_types
<32>::Elf_Addr
,
2304 unsigned char* view
,
2305 section_size_type view_size
)
2307 // leal foo(%reg), %eax; call ___tls_get_addr
2308 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2310 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2311 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2313 // FIXME: Does this test really always pass?
2314 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2315 view
[-2] == 0x8d && view
[-1] == 0x83);
2317 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2319 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2321 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2323 this->skip_call_tls_get_addr_
= true;
2326 // Do a relocation in which we convert a TLS Initial-Exec to a
2330 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2332 Output_segment
* tls_segment
,
2333 const elfcpp::Rel
<32, false>& rel
,
2334 unsigned int r_type
,
2335 elfcpp::Elf_types
<32>::Elf_Addr value
,
2336 unsigned char* view
,
2337 section_size_type view_size
)
2339 // We have to actually change the instructions, which means that we
2340 // need to examine the opcodes to figure out which instruction we
2342 if (r_type
== elfcpp::R_386_TLS_IE
)
2344 // movl %gs:XX,%eax ==> movl $YY,%eax
2345 // movl %gs:XX,%reg ==> movl $YY,%reg
2346 // addl %gs:XX,%reg ==> addl $YY,%reg
2347 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2348 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2350 unsigned char op1
= view
[-1];
2353 // movl XX,%eax ==> movl $YY,%eax
2358 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2360 unsigned char op2
= view
[-2];
2363 // movl XX,%reg ==> movl $YY,%reg
2364 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2365 (op1
& 0xc7) == 0x05);
2367 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2369 else if (op2
== 0x03)
2371 // addl XX,%reg ==> addl $YY,%reg
2372 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2373 (op1
& 0xc7) == 0x05);
2375 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2378 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2383 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2384 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2385 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2386 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2387 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2389 unsigned char op1
= view
[-1];
2390 unsigned char op2
= view
[-2];
2391 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2392 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2395 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2397 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2399 else if (op2
== 0x2b)
2401 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2403 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2405 else if (op2
== 0x03)
2407 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2409 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2412 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2415 value
= tls_segment
->memsz() - value
;
2416 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2419 Relocate_functions
<32, false>::rel32(view
, value
);
2422 // Relocate section data.
2425 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2426 unsigned int sh_type
,
2427 const unsigned char* prelocs
,
2429 Output_section
* output_section
,
2430 bool needs_special_offset_handling
,
2431 unsigned char* view
,
2432 elfcpp::Elf_types
<32>::Elf_Addr address
,
2433 section_size_type view_size
)
2435 gold_assert(sh_type
== elfcpp::SHT_REL
);
2437 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2438 Target_i386::Relocate
>(
2444 needs_special_offset_handling
,
2450 // Return the size of a relocation while scanning during a relocatable
2454 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2455 unsigned int r_type
,
2460 case elfcpp::R_386_NONE
:
2461 case elfcpp::R_386_GNU_VTINHERIT
:
2462 case elfcpp::R_386_GNU_VTENTRY
:
2463 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2464 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2465 case elfcpp::R_386_TLS_DESC_CALL
:
2466 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2467 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2468 case elfcpp::R_386_TLS_IE
: // Initial-exec
2469 case elfcpp::R_386_TLS_IE_32
:
2470 case elfcpp::R_386_TLS_GOTIE
:
2471 case elfcpp::R_386_TLS_LE
: // Local-exec
2472 case elfcpp::R_386_TLS_LE_32
:
2475 case elfcpp::R_386_32
:
2476 case elfcpp::R_386_PC32
:
2477 case elfcpp::R_386_GOT32
:
2478 case elfcpp::R_386_PLT32
:
2479 case elfcpp::R_386_GOTOFF
:
2480 case elfcpp::R_386_GOTPC
:
2483 case elfcpp::R_386_16
:
2484 case elfcpp::R_386_PC16
:
2487 case elfcpp::R_386_8
:
2488 case elfcpp::R_386_PC8
:
2491 // These are relocations which should only be seen by the
2492 // dynamic linker, and should never be seen here.
2493 case elfcpp::R_386_COPY
:
2494 case elfcpp::R_386_GLOB_DAT
:
2495 case elfcpp::R_386_JUMP_SLOT
:
2496 case elfcpp::R_386_RELATIVE
:
2497 case elfcpp::R_386_TLS_TPOFF
:
2498 case elfcpp::R_386_TLS_DTPMOD32
:
2499 case elfcpp::R_386_TLS_DTPOFF32
:
2500 case elfcpp::R_386_TLS_TPOFF32
:
2501 case elfcpp::R_386_TLS_DESC
:
2502 object
->error(_("unexpected reloc %u in object file"), r_type
);
2505 case elfcpp::R_386_32PLT
:
2506 case elfcpp::R_386_TLS_GD_32
:
2507 case elfcpp::R_386_TLS_GD_PUSH
:
2508 case elfcpp::R_386_TLS_GD_CALL
:
2509 case elfcpp::R_386_TLS_GD_POP
:
2510 case elfcpp::R_386_TLS_LDM_32
:
2511 case elfcpp::R_386_TLS_LDM_PUSH
:
2512 case elfcpp::R_386_TLS_LDM_CALL
:
2513 case elfcpp::R_386_TLS_LDM_POP
:
2514 case elfcpp::R_386_USED_BY_INTEL_200
:
2516 object
->error(_("unsupported reloc %u in object file"), r_type
);
2521 // Scan the relocs during a relocatable link.
2524 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2525 Symbol_table
* symtab
,
2527 Sized_relobj
<32, false>* object
,
2528 unsigned int data_shndx
,
2529 unsigned int sh_type
,
2530 const unsigned char* prelocs
,
2532 Output_section
* output_section
,
2533 bool needs_special_offset_handling
,
2534 size_t local_symbol_count
,
2535 const unsigned char* plocal_symbols
,
2536 Relocatable_relocs
* rr
)
2538 gold_assert(sh_type
== elfcpp::SHT_REL
);
2540 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2541 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2543 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2544 Scan_relocatable_relocs
>(
2553 needs_special_offset_handling
,
2559 // Relocate a section during a relocatable link.
2562 Target_i386::relocate_for_relocatable(
2563 const Relocate_info
<32, false>* relinfo
,
2564 unsigned int sh_type
,
2565 const unsigned char* prelocs
,
2567 Output_section
* output_section
,
2568 off_t offset_in_output_section
,
2569 const Relocatable_relocs
* rr
,
2570 unsigned char* view
,
2571 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2572 section_size_type view_size
,
2573 unsigned char* reloc_view
,
2574 section_size_type reloc_view_size
)
2576 gold_assert(sh_type
== elfcpp::SHT_REL
);
2578 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2583 offset_in_output_section
,
2592 // Return the value to use for a dynamic which requires special
2593 // treatment. This is how we support equality comparisons of function
2594 // pointers across shared library boundaries, as described in the
2595 // processor specific ABI supplement.
2598 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2600 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2601 return this->plt_section()->address() + gsym
->plt_offset();
2604 // Return a string used to fill a code section with nops to take up
2605 // the specified length.
2608 Target_i386::do_code_fill(section_size_type length
) const
2612 // Build a jmp instruction to skip over the bytes.
2613 unsigned char jmp
[5];
2615 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2616 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2617 + std::string(length
- 5, '\0'));
2620 // Nop sequences of various lengths.
2621 const char nop1
[1] = { 0x90 }; // nop
2622 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2623 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2624 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2625 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2626 0x00 }; // leal 0(%esi,1),%esi
2627 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2629 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2631 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2632 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2633 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2634 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2636 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2637 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2639 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2640 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2642 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2643 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2644 0x00, 0x00, 0x00, 0x00 };
2645 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2646 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2647 0x27, 0x00, 0x00, 0x00,
2649 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2650 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2651 0xbc, 0x27, 0x00, 0x00,
2653 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2654 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2655 0x90, 0x90, 0x90, 0x90,
2658 const char* nops
[16] = {
2660 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2661 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2664 return std::string(nops
[length
], length
);
2667 // The selector for i386 object files.
2669 class Target_selector_i386
: public Target_selector_freebsd
2672 Target_selector_i386()
2673 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
2674 "elf32-i386", "elf32-i386-freebsd")
2678 do_instantiate_target()
2679 { return new Target_i386(); }
2682 Target_selector_i386 target_selector_i386
;
2684 } // End anonymous namespace.