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"
46 class Output_data_plt_i386
;
48 // The i386 target class.
49 // TLS info comes from
50 // http://people.redhat.com/drepper/tls.pdf
51 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
53 class Target_i386
: public Sized_target
<32, false>
56 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
59 : Sized_target
<32, false>(&i386_info
),
60 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
61 copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
62 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
68 gc_process_relocs(const General_options
& options
,
71 Sized_relobj
<32, false>* object
,
72 unsigned int data_shndx
,
74 const unsigned char* prelocs
,
76 Output_section
* output_section
,
77 bool needs_special_offset_handling
,
78 size_t local_symbol_count
,
79 const unsigned char* plocal_symbols
);
81 // Scan the relocations to look for symbol adjustments.
83 scan_relocs(const General_options
& options
,
86 Sized_relobj
<32, false>* object
,
87 unsigned int data_shndx
,
89 const unsigned char* prelocs
,
91 Output_section
* output_section
,
92 bool needs_special_offset_handling
,
93 size_t local_symbol_count
,
94 const unsigned char* plocal_symbols
);
96 // Finalize the sections.
98 do_finalize_sections(Layout
*);
100 // Return the value to use for a dynamic which requires special
103 do_dynsym_value(const Symbol
*) const;
105 // Relocate a section.
107 relocate_section(const Relocate_info
<32, false>*,
108 unsigned int sh_type
,
109 const unsigned char* prelocs
,
111 Output_section
* output_section
,
112 bool needs_special_offset_handling
,
114 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
115 section_size_type view_size
);
117 // Scan the relocs during a relocatable link.
119 scan_relocatable_relocs(const General_options
& options
,
120 Symbol_table
* symtab
,
122 Sized_relobj
<32, false>* object
,
123 unsigned int data_shndx
,
124 unsigned int sh_type
,
125 const unsigned char* prelocs
,
127 Output_section
* output_section
,
128 bool needs_special_offset_handling
,
129 size_t local_symbol_count
,
130 const unsigned char* plocal_symbols
,
131 Relocatable_relocs
*);
133 // Relocate a section during a relocatable link.
135 relocate_for_relocatable(const Relocate_info
<32, false>*,
136 unsigned int sh_type
,
137 const unsigned char* prelocs
,
139 Output_section
* output_section
,
140 off_t offset_in_output_section
,
141 const Relocatable_relocs
*,
143 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
144 section_size_type view_size
,
145 unsigned char* reloc_view
,
146 section_size_type reloc_view_size
);
148 // Return a string used to fill a code section with nops.
150 do_code_fill(section_size_type length
) const;
152 // Return whether SYM is defined by the ABI.
154 do_is_defined_by_abi(const Symbol
* sym
) const
155 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
157 // Return the size of the GOT section.
161 gold_assert(this->got_
!= NULL
);
162 return this->got_
->data_size();
166 // The class which scans relocations.
170 local(const General_options
& options
, Symbol_table
* symtab
,
171 Layout
* layout
, Target_i386
* target
,
172 Sized_relobj
<32, false>* object
,
173 unsigned int data_shndx
,
174 Output_section
* output_section
,
175 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
176 const elfcpp::Sym
<32, false>& lsym
);
179 global(const General_options
& options
, Symbol_table
* symtab
,
180 Layout
* layout
, Target_i386
* target
,
181 Sized_relobj
<32, false>* object
,
182 unsigned int data_shndx
,
183 Output_section
* output_section
,
184 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
188 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
191 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
195 // The class which implements relocation.
200 : skip_call_tls_get_addr_(false),
201 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
206 if (this->skip_call_tls_get_addr_
)
208 // FIXME: This needs to specify the location somehow.
209 gold_error(_("missing expected TLS relocation"));
213 // Return whether the static relocation needs to be applied.
215 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
218 Output_section
* output_section
);
220 // Do a relocation. Return false if the caller should not issue
221 // any warnings about this relocation.
223 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
224 size_t relnum
, const elfcpp::Rel
<32, false>&,
225 unsigned int r_type
, const Sized_symbol
<32>*,
226 const Symbol_value
<32>*,
227 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
231 // Do a TLS relocation.
233 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
234 size_t relnum
, const elfcpp::Rel
<32, false>&,
235 unsigned int r_type
, const Sized_symbol
<32>*,
236 const Symbol_value
<32>*,
237 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
240 // Do a TLS General-Dynamic to Initial-Exec transition.
242 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
243 Output_segment
* tls_segment
,
244 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
245 elfcpp::Elf_types
<32>::Elf_Addr value
,
247 section_size_type view_size
);
249 // Do a TLS General-Dynamic to Local-Exec transition.
251 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
252 Output_segment
* tls_segment
,
253 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
254 elfcpp::Elf_types
<32>::Elf_Addr value
,
256 section_size_type view_size
);
258 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
261 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
262 Output_segment
* tls_segment
,
263 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
264 elfcpp::Elf_types
<32>::Elf_Addr value
,
266 section_size_type view_size
);
268 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
271 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
272 Output_segment
* tls_segment
,
273 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
274 elfcpp::Elf_types
<32>::Elf_Addr value
,
276 section_size_type view_size
);
278 // Do a TLS Local-Dynamic to Local-Exec transition.
280 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
281 Output_segment
* tls_segment
,
282 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
283 elfcpp::Elf_types
<32>::Elf_Addr value
,
285 section_size_type view_size
);
287 // Do a TLS Initial-Exec to Local-Exec transition.
289 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
290 Output_segment
* tls_segment
,
291 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
292 elfcpp::Elf_types
<32>::Elf_Addr value
,
294 section_size_type view_size
);
296 // We need to keep track of which type of local dynamic relocation
297 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
298 enum Local_dynamic_type
305 // This is set if we should skip the next reloc, which should be a
306 // PLT32 reloc against ___tls_get_addr.
307 bool skip_call_tls_get_addr_
;
308 // The type of local dynamic relocation we have seen in the section
309 // being relocated, if any.
310 Local_dynamic_type local_dynamic_type_
;
313 // A class which returns the size required for a relocation type,
314 // used while scanning relocs during a relocatable link.
315 class Relocatable_size_for_reloc
319 get_size_for_reloc(unsigned int, Relobj
*);
322 // Adjust TLS relocation type based on the options and whether this
323 // is a local symbol.
324 static tls::Tls_optimization
325 optimize_tls_reloc(bool is_final
, int r_type
);
327 // Get the GOT section, creating it if necessary.
328 Output_data_got
<32, false>*
329 got_section(Symbol_table
*, Layout
*);
331 // Get the GOT PLT section.
333 got_plt_section() const
335 gold_assert(this->got_plt_
!= NULL
);
336 return this->got_plt_
;
339 // Create a PLT entry for a global symbol.
341 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
343 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
345 define_tls_base_symbol(Symbol_table
*, Layout
*);
347 // Create a GOT entry for the TLS module index.
349 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
350 Sized_relobj
<32, false>* object
);
352 // Get the PLT section.
353 const Output_data_plt_i386
*
356 gold_assert(this->plt_
!= NULL
);
360 // Get the dynamic reloc section, creating it if necessary.
362 rel_dyn_section(Layout
*);
364 // Return true if the symbol may need a COPY relocation.
365 // References from an executable object to non-function symbols
366 // defined in a dynamic object may need a COPY relocation.
368 may_need_copy_reloc(Symbol
* gsym
)
370 return (!parameters
->options().shared()
371 && gsym
->is_from_dynobj()
372 && gsym
->type() != elfcpp::STT_FUNC
);
375 // Add a potential copy relocation.
377 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
378 Sized_relobj
<32, false>* object
,
379 unsigned int shndx
, Output_section
* output_section
,
380 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
382 this->copy_relocs_
.copy_reloc(symtab
, layout
,
383 symtab
->get_sized_symbol
<32>(sym
),
384 object
, shndx
, output_section
, reloc
,
385 this->rel_dyn_section(layout
));
388 // Information about this specific target which we pass to the
389 // general Target structure.
390 static const Target::Target_info i386_info
;
392 // The types of GOT entries needed for this platform.
395 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
396 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
397 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
398 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
399 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
403 Output_data_got
<32, false>* got_
;
405 Output_data_plt_i386
* plt_
;
406 // The GOT PLT section.
407 Output_data_space
* got_plt_
;
408 // The dynamic reloc section.
409 Reloc_section
* rel_dyn_
;
410 // Relocs saved to avoid a COPY reloc.
411 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
412 // Space for variables copied with a COPY reloc.
413 Output_data_space
* dynbss_
;
414 // Offset of the GOT entry for the TLS module index.
415 unsigned int got_mod_index_offset_
;
416 // True if the _TLS_MODULE_BASE_ symbol has been defined.
417 bool tls_base_symbol_defined_
;
420 const Target::Target_info
Target_i386::i386_info
=
423 false, // is_big_endian
424 elfcpp::EM_386
, // machine_code
425 false, // has_make_symbol
426 false, // has_resolve
427 true, // has_code_fill
428 true, // is_default_stack_executable
430 "/usr/lib/libc.so.1", // dynamic_linker
431 0x08048000, // default_text_segment_address
432 0x1000, // abi_pagesize (overridable by -z max-page-size)
433 0x1000 // common_pagesize (overridable by -z common-page-size)
436 // Get the GOT section, creating it if necessary.
438 Output_data_got
<32, false>*
439 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
441 if (this->got_
== NULL
)
443 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
445 this->got_
= new Output_data_got
<32, false>();
448 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
450 | elfcpp::SHF_WRITE
),
454 // The old GNU linker creates a .got.plt section. We just
455 // create another set of data in the .got section. Note that we
456 // always create a PLT if we create a GOT, although the PLT
458 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
459 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
461 | elfcpp::SHF_WRITE
),
465 // The first three entries are reserved.
466 this->got_plt_
->set_current_data_size(3 * 4);
468 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
469 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
471 0, 0, elfcpp::STT_OBJECT
,
473 elfcpp::STV_HIDDEN
, 0,
480 // Get the dynamic reloc section, creating it if necessary.
482 Target_i386::Reloc_section
*
483 Target_i386::rel_dyn_section(Layout
* layout
)
485 if (this->rel_dyn_
== NULL
)
487 gold_assert(layout
!= NULL
);
488 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
489 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
490 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
492 return this->rel_dyn_
;
495 // A class to handle the PLT data.
497 class Output_data_plt_i386
: public Output_section_data
500 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
502 Output_data_plt_i386(Layout
*, Output_data_space
*);
504 // Add an entry to the PLT.
506 add_entry(Symbol
* gsym
);
508 // Return the .rel.plt section data.
511 { return this->rel_
; }
515 do_adjust_output_section(Output_section
* os
);
517 // Write to a map file.
519 do_print_to_mapfile(Mapfile
* mapfile
) const
520 { mapfile
->print_output_data(this, _("** PLT")); }
523 // The size of an entry in the PLT.
524 static const int plt_entry_size
= 16;
526 // The first entry in the PLT for an executable.
527 static unsigned char exec_first_plt_entry
[plt_entry_size
];
529 // The first entry in the PLT for a shared object.
530 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
532 // Other entries in the PLT for an executable.
533 static unsigned char exec_plt_entry
[plt_entry_size
];
535 // Other entries in the PLT for a shared object.
536 static unsigned char dyn_plt_entry
[plt_entry_size
];
538 // Set the final size.
540 set_final_data_size()
541 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
543 // Write out the PLT data.
545 do_write(Output_file
*);
547 // The reloc section.
549 // The .got.plt section.
550 Output_data_space
* got_plt_
;
551 // The number of PLT entries.
555 // Create the PLT section. The ordinary .got section is an argument,
556 // since we need to refer to the start. We also create our own .got
557 // section just for PLT entries.
559 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
560 Output_data_space
* got_plt
)
561 : Output_section_data(4), got_plt_(got_plt
), count_(0)
563 this->rel_
= new Reloc_section(false);
564 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
565 elfcpp::SHF_ALLOC
, this->rel_
);
569 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
571 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
572 // linker, and so do we.
576 // Add an entry to the PLT.
579 Output_data_plt_i386::add_entry(Symbol
* gsym
)
581 gold_assert(!gsym
->has_plt_offset());
583 // Note that when setting the PLT offset we skip the initial
584 // reserved PLT entry.
585 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
589 section_offset_type got_offset
= this->got_plt_
->current_data_size();
591 // Every PLT entry needs a GOT entry which points back to the PLT
592 // entry (this will be changed by the dynamic linker, normally
593 // lazily when the function is called).
594 this->got_plt_
->set_current_data_size(got_offset
+ 4);
596 // Every PLT entry needs a reloc.
597 gsym
->set_needs_dynsym_entry();
598 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
601 // Note that we don't need to save the symbol. The contents of the
602 // PLT are independent of which symbols are used. The symbols only
603 // appear in the relocations.
606 // The first entry in the PLT for an executable.
608 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
610 0xff, 0x35, // pushl contents of memory address
611 0, 0, 0, 0, // replaced with address of .got + 4
612 0xff, 0x25, // jmp indirect
613 0, 0, 0, 0, // replaced with address of .got + 8
617 // The first entry in the PLT for a shared object.
619 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
621 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
622 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
626 // Subsequent entries in the PLT for an executable.
628 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
630 0xff, 0x25, // jmp indirect
631 0, 0, 0, 0, // replaced with address of symbol in .got
632 0x68, // pushl immediate
633 0, 0, 0, 0, // replaced with offset into relocation table
634 0xe9, // jmp relative
635 0, 0, 0, 0 // replaced with offset to start of .plt
638 // Subsequent entries in the PLT for a shared object.
640 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
642 0xff, 0xa3, // jmp *offset(%ebx)
643 0, 0, 0, 0, // replaced with offset of symbol in .got
644 0x68, // pushl immediate
645 0, 0, 0, 0, // replaced with offset into relocation table
646 0xe9, // jmp relative
647 0, 0, 0, 0 // replaced with offset to start of .plt
650 // Write out the PLT. This uses the hand-coded instructions above,
651 // and adjusts them as needed. This is all specified by the i386 ELF
652 // Processor Supplement.
655 Output_data_plt_i386::do_write(Output_file
* of
)
657 const off_t offset
= this->offset();
658 const section_size_type oview_size
=
659 convert_to_section_size_type(this->data_size());
660 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
662 const off_t got_file_offset
= this->got_plt_
->offset();
663 const section_size_type got_size
=
664 convert_to_section_size_type(this->got_plt_
->data_size());
665 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
668 unsigned char* pov
= oview
;
670 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
671 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
673 if (parameters
->options().shared())
674 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
677 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
678 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
679 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
681 pov
+= plt_entry_size
;
683 unsigned char* got_pov
= got_view
;
685 memset(got_pov
, 0, 12);
688 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
690 unsigned int plt_offset
= plt_entry_size
;
691 unsigned int plt_rel_offset
= 0;
692 unsigned int got_offset
= 12;
693 const unsigned int count
= this->count_
;
694 for (unsigned int i
= 0;
697 pov
+= plt_entry_size
,
699 plt_offset
+= plt_entry_size
,
700 plt_rel_offset
+= rel_size
,
703 // Set and adjust the PLT entry itself.
705 if (parameters
->options().shared())
707 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
708 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
712 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
713 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
718 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
719 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
720 - (plt_offset
+ plt_entry_size
));
722 // Set the entry in the GOT.
723 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
726 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
727 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
729 of
->write_output_view(offset
, oview_size
, oview
);
730 of
->write_output_view(got_file_offset
, got_size
, got_view
);
733 // Create a PLT entry for a global symbol.
736 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
738 if (gsym
->has_plt_offset())
741 if (this->plt_
== NULL
)
743 // Create the GOT sections first.
744 this->got_section(symtab
, layout
);
746 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
747 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
749 | elfcpp::SHF_EXECINSTR
),
753 this->plt_
->add_entry(gsym
);
756 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
759 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
761 if (this->tls_base_symbol_defined_
)
764 Output_segment
* tls_segment
= layout
->tls_segment();
765 if (tls_segment
!= NULL
)
767 bool is_exec
= parameters
->options().output_is_executable();
768 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
772 elfcpp::STV_HIDDEN
, 0,
774 ? Symbol::SEGMENT_END
775 : Symbol::SEGMENT_START
),
778 this->tls_base_symbol_defined_
= true;
781 // Create a GOT entry for the TLS module index.
784 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
785 Sized_relobj
<32, false>* object
)
787 if (this->got_mod_index_offset_
== -1U)
789 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
790 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
791 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
792 unsigned int got_offset
= got
->add_constant(0);
793 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
795 got
->add_constant(0);
796 this->got_mod_index_offset_
= got_offset
;
798 return this->got_mod_index_offset_
;
801 // Optimize the TLS relocation type based on what we know about the
802 // symbol. IS_FINAL is true if the final address of this symbol is
803 // known at link time.
805 tls::Tls_optimization
806 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
808 // If we are generating a shared library, then we can't do anything
810 if (parameters
->options().shared())
811 return tls::TLSOPT_NONE
;
815 case elfcpp::R_386_TLS_GD
:
816 case elfcpp::R_386_TLS_GOTDESC
:
817 case elfcpp::R_386_TLS_DESC_CALL
:
818 // These are General-Dynamic which permits fully general TLS
819 // access. Since we know that we are generating an executable,
820 // we can convert this to Initial-Exec. If we also know that
821 // this is a local symbol, we can further switch to Local-Exec.
823 return tls::TLSOPT_TO_LE
;
824 return tls::TLSOPT_TO_IE
;
826 case elfcpp::R_386_TLS_LDM
:
827 // This is Local-Dynamic, which refers to a local symbol in the
828 // dynamic TLS block. Since we know that we generating an
829 // executable, we can switch to Local-Exec.
830 return tls::TLSOPT_TO_LE
;
832 case elfcpp::R_386_TLS_LDO_32
:
833 // Another type of Local-Dynamic relocation.
834 return tls::TLSOPT_TO_LE
;
836 case elfcpp::R_386_TLS_IE
:
837 case elfcpp::R_386_TLS_GOTIE
:
838 case elfcpp::R_386_TLS_IE_32
:
839 // These are Initial-Exec relocs which get the thread offset
840 // from the GOT. If we know that we are linking against the
841 // local symbol, we can switch to Local-Exec, which links the
842 // thread offset into the instruction.
844 return tls::TLSOPT_TO_LE
;
845 return tls::TLSOPT_NONE
;
847 case elfcpp::R_386_TLS_LE
:
848 case elfcpp::R_386_TLS_LE_32
:
849 // When we already have Local-Exec, there is nothing further we
851 return tls::TLSOPT_NONE
;
858 // Report an unsupported relocation against a local symbol.
861 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
864 gold_error(_("%s: unsupported reloc %u against local symbol"),
865 object
->name().c_str(), r_type
);
868 // Scan a relocation for a local symbol.
871 Target_i386::Scan::local(const General_options
&,
872 Symbol_table
* symtab
,
875 Sized_relobj
<32, false>* object
,
876 unsigned int data_shndx
,
877 Output_section
* output_section
,
878 const elfcpp::Rel
<32, false>& reloc
,
880 const elfcpp::Sym
<32, false>& lsym
)
884 case elfcpp::R_386_NONE
:
885 case elfcpp::R_386_GNU_VTINHERIT
:
886 case elfcpp::R_386_GNU_VTENTRY
:
889 case elfcpp::R_386_32
:
890 // If building a shared library (or a position-independent
891 // executable), we need to create a dynamic relocation for
892 // this location. The relocation applied at link time will
893 // apply the link-time value, so we flag the location with
894 // an R_386_RELATIVE relocation so the dynamic loader can
895 // relocate it easily.
896 if (parameters
->options().output_is_position_independent())
898 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
899 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
900 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
901 output_section
, data_shndx
,
902 reloc
.get_r_offset());
906 case elfcpp::R_386_16
:
907 case elfcpp::R_386_8
:
908 // If building a shared library (or a position-independent
909 // executable), we need to create a dynamic relocation for
910 // this location. Because the addend needs to remain in the
911 // data section, we need to be careful not to apply this
912 // relocation statically.
913 if (parameters
->options().output_is_position_independent())
915 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
916 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
917 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
918 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
919 data_shndx
, reloc
.get_r_offset());
922 gold_assert(lsym
.get_st_value() == 0);
923 unsigned int shndx
= lsym
.get_st_shndx();
925 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
928 object
->error(_("section symbol %u has bad shndx %u"),
931 rel_dyn
->add_local_section(object
, shndx
,
932 r_type
, output_section
,
933 data_shndx
, reloc
.get_r_offset());
938 case elfcpp::R_386_PC32
:
939 case elfcpp::R_386_PC16
:
940 case elfcpp::R_386_PC8
:
943 case elfcpp::R_386_PLT32
:
944 // Since we know this is a local symbol, we can handle this as a
948 case elfcpp::R_386_GOTOFF
:
949 case elfcpp::R_386_GOTPC
:
950 // We need a GOT section.
951 target
->got_section(symtab
, layout
);
954 case elfcpp::R_386_GOT32
:
956 // The symbol requires a GOT entry.
957 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
958 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
959 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
961 // If we are generating a shared object, we need to add a
962 // dynamic RELATIVE relocation for this symbol's GOT entry.
963 if (parameters
->options().output_is_position_independent())
965 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
966 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
967 rel_dyn
->add_local_relative(
968 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
969 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
975 // These are relocations which should only be seen by the
976 // dynamic linker, and should never be seen here.
977 case elfcpp::R_386_COPY
:
978 case elfcpp::R_386_GLOB_DAT
:
979 case elfcpp::R_386_JUMP_SLOT
:
980 case elfcpp::R_386_RELATIVE
:
981 case elfcpp::R_386_TLS_TPOFF
:
982 case elfcpp::R_386_TLS_DTPMOD32
:
983 case elfcpp::R_386_TLS_DTPOFF32
:
984 case elfcpp::R_386_TLS_TPOFF32
:
985 case elfcpp::R_386_TLS_DESC
:
986 gold_error(_("%s: unexpected reloc %u in object file"),
987 object
->name().c_str(), r_type
);
990 // These are initial TLS relocs, which are expected when
992 case elfcpp::R_386_TLS_GD
: // Global-dynamic
993 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
994 case elfcpp::R_386_TLS_DESC_CALL
:
995 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
996 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
997 case elfcpp::R_386_TLS_IE
: // Initial-exec
998 case elfcpp::R_386_TLS_IE_32
:
999 case elfcpp::R_386_TLS_GOTIE
:
1000 case elfcpp::R_386_TLS_LE
: // Local-exec
1001 case elfcpp::R_386_TLS_LE_32
:
1003 bool output_is_shared
= parameters
->options().shared();
1004 const tls::Tls_optimization optimized_type
1005 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1008 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1009 if (optimized_type
== tls::TLSOPT_NONE
)
1011 // Create a pair of GOT entries for the module index and
1012 // dtv-relative offset.
1013 Output_data_got
<32, false>* got
1014 = target
->got_section(symtab
, layout
);
1015 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1016 unsigned int shndx
= lsym
.get_st_shndx();
1018 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1020 object
->error(_("local symbol %u has bad shndx %u"),
1023 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1025 target
->rel_dyn_section(layout
),
1026 elfcpp::R_386_TLS_DTPMOD32
, 0);
1028 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1029 unsupported_reloc_local(object
, r_type
);
1032 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1033 target
->define_tls_base_symbol(symtab
, layout
);
1034 if (optimized_type
== tls::TLSOPT_NONE
)
1036 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1037 Output_data_got
<32, false>* got
1038 = target
->got_section(symtab
, layout
);
1039 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1040 unsigned int shndx
= lsym
.get_st_shndx();
1042 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1044 object
->error(_("local symbol %u has bad shndx %u"),
1047 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1049 target
->rel_dyn_section(layout
),
1050 elfcpp::R_386_TLS_DESC
, 0);
1052 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1053 unsupported_reloc_local(object
, r_type
);
1056 case elfcpp::R_386_TLS_DESC_CALL
:
1059 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1060 if (optimized_type
== tls::TLSOPT_NONE
)
1062 // Create a GOT entry for the module index.
1063 target
->got_mod_index_entry(symtab
, layout
, object
);
1065 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1066 unsupported_reloc_local(object
, r_type
);
1069 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1072 case elfcpp::R_386_TLS_IE
: // Initial-exec
1073 case elfcpp::R_386_TLS_IE_32
:
1074 case elfcpp::R_386_TLS_GOTIE
:
1075 layout
->set_has_static_tls();
1076 if (optimized_type
== tls::TLSOPT_NONE
)
1078 // For the R_386_TLS_IE relocation, we need to create a
1079 // dynamic relocation when building a shared library.
1080 if (r_type
== elfcpp::R_386_TLS_IE
1081 && parameters
->options().shared())
1083 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1085 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1086 rel_dyn
->add_local_relative(object
, r_sym
,
1087 elfcpp::R_386_RELATIVE
,
1088 output_section
, data_shndx
,
1089 reloc
.get_r_offset());
1091 // Create a GOT entry for the tp-relative offset.
1092 Output_data_got
<32, false>* got
1093 = target
->got_section(symtab
, layout
);
1094 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1095 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1096 ? elfcpp::R_386_TLS_TPOFF32
1097 : elfcpp::R_386_TLS_TPOFF
);
1098 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1099 ? GOT_TYPE_TLS_OFFSET
1100 : GOT_TYPE_TLS_NOFFSET
);
1101 got
->add_local_with_rel(object
, r_sym
, got_type
,
1102 target
->rel_dyn_section(layout
),
1105 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1106 unsupported_reloc_local(object
, r_type
);
1109 case elfcpp::R_386_TLS_LE
: // Local-exec
1110 case elfcpp::R_386_TLS_LE_32
:
1111 layout
->set_has_static_tls();
1112 if (output_is_shared
)
1114 // We need to create a dynamic relocation.
1115 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1116 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1117 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1118 ? elfcpp::R_386_TLS_TPOFF32
1119 : elfcpp::R_386_TLS_TPOFF
);
1120 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1121 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1122 data_shndx
, reloc
.get_r_offset());
1132 case elfcpp::R_386_32PLT
:
1133 case elfcpp::R_386_TLS_GD_32
:
1134 case elfcpp::R_386_TLS_GD_PUSH
:
1135 case elfcpp::R_386_TLS_GD_CALL
:
1136 case elfcpp::R_386_TLS_GD_POP
:
1137 case elfcpp::R_386_TLS_LDM_32
:
1138 case elfcpp::R_386_TLS_LDM_PUSH
:
1139 case elfcpp::R_386_TLS_LDM_CALL
:
1140 case elfcpp::R_386_TLS_LDM_POP
:
1141 case elfcpp::R_386_USED_BY_INTEL_200
:
1143 unsupported_reloc_local(object
, r_type
);
1148 // Report an unsupported relocation against a global symbol.
1151 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1152 unsigned int r_type
,
1155 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1156 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1159 // Scan a relocation for a global symbol.
1162 Target_i386::Scan::global(const General_options
&,
1163 Symbol_table
* symtab
,
1165 Target_i386
* target
,
1166 Sized_relobj
<32, false>* object
,
1167 unsigned int data_shndx
,
1168 Output_section
* output_section
,
1169 const elfcpp::Rel
<32, false>& reloc
,
1170 unsigned int r_type
,
1175 case elfcpp::R_386_NONE
:
1176 case elfcpp::R_386_GNU_VTINHERIT
:
1177 case elfcpp::R_386_GNU_VTENTRY
:
1180 case elfcpp::R_386_32
:
1181 case elfcpp::R_386_16
:
1182 case elfcpp::R_386_8
:
1184 // Make a PLT entry if necessary.
1185 if (gsym
->needs_plt_entry())
1187 target
->make_plt_entry(symtab
, layout
, gsym
);
1188 // Since this is not a PC-relative relocation, we may be
1189 // taking the address of a function. In that case we need to
1190 // set the entry in the dynamic symbol table to the address of
1192 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1193 gsym
->set_needs_dynsym_value();
1195 // Make a dynamic relocation if necessary.
1196 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1198 if (target
->may_need_copy_reloc(gsym
))
1200 target
->copy_reloc(symtab
, layout
, object
,
1201 data_shndx
, output_section
, gsym
, reloc
);
1203 else if (r_type
== elfcpp::R_386_32
1204 && gsym
->can_use_relative_reloc(false))
1206 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1207 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1208 output_section
, object
,
1209 data_shndx
, reloc
.get_r_offset());
1213 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1214 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1215 data_shndx
, reloc
.get_r_offset());
1221 case elfcpp::R_386_PC32
:
1222 case elfcpp::R_386_PC16
:
1223 case elfcpp::R_386_PC8
:
1225 // Make a PLT entry if necessary.
1226 if (gsym
->needs_plt_entry())
1228 // These relocations are used for function calls only in
1229 // non-PIC code. For a 32-bit relocation in a shared library,
1230 // we'll need a text relocation anyway, so we can skip the
1231 // PLT entry and let the dynamic linker bind the call directly
1232 // to the target. For smaller relocations, we should use a
1233 // PLT entry to ensure that the call can reach.
1234 if (!parameters
->options().shared()
1235 || r_type
!= elfcpp::R_386_PC32
)
1236 target
->make_plt_entry(symtab
, layout
, gsym
);
1238 // Make a dynamic relocation if necessary.
1239 int flags
= Symbol::NON_PIC_REF
;
1240 if (gsym
->type() == elfcpp::STT_FUNC
)
1241 flags
|= Symbol::FUNCTION_CALL
;
1242 if (gsym
->needs_dynamic_reloc(flags
))
1244 if (target
->may_need_copy_reloc(gsym
))
1246 target
->copy_reloc(symtab
, layout
, object
,
1247 data_shndx
, output_section
, gsym
, reloc
);
1251 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1252 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1253 data_shndx
, reloc
.get_r_offset());
1259 case elfcpp::R_386_GOT32
:
1261 // The symbol requires a GOT entry.
1262 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1263 if (gsym
->final_value_is_known())
1264 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1267 // If this symbol is not fully resolved, we need to add a
1268 // GOT entry with a dynamic relocation.
1269 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1270 if (gsym
->is_from_dynobj()
1271 || gsym
->is_undefined()
1272 || gsym
->is_preemptible())
1273 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1274 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1277 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1278 rel_dyn
->add_global_relative(
1279 gsym
, elfcpp::R_386_RELATIVE
, got
,
1280 gsym
->got_offset(GOT_TYPE_STANDARD
));
1286 case elfcpp::R_386_PLT32
:
1287 // If the symbol is fully resolved, this is just a PC32 reloc.
1288 // Otherwise we need a PLT entry.
1289 if (gsym
->final_value_is_known())
1291 // If building a shared library, we can also skip the PLT entry
1292 // if the symbol is defined in the output file and is protected
1294 if (gsym
->is_defined()
1295 && !gsym
->is_from_dynobj()
1296 && !gsym
->is_preemptible())
1298 target
->make_plt_entry(symtab
, layout
, gsym
);
1301 case elfcpp::R_386_GOTOFF
:
1302 case elfcpp::R_386_GOTPC
:
1303 // We need a GOT section.
1304 target
->got_section(symtab
, layout
);
1307 // These are relocations which should only be seen by the
1308 // dynamic linker, and should never be seen here.
1309 case elfcpp::R_386_COPY
:
1310 case elfcpp::R_386_GLOB_DAT
:
1311 case elfcpp::R_386_JUMP_SLOT
:
1312 case elfcpp::R_386_RELATIVE
:
1313 case elfcpp::R_386_TLS_TPOFF
:
1314 case elfcpp::R_386_TLS_DTPMOD32
:
1315 case elfcpp::R_386_TLS_DTPOFF32
:
1316 case elfcpp::R_386_TLS_TPOFF32
:
1317 case elfcpp::R_386_TLS_DESC
:
1318 gold_error(_("%s: unexpected reloc %u in object file"),
1319 object
->name().c_str(), r_type
);
1322 // These are initial tls relocs, which are expected when
1324 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1325 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1326 case elfcpp::R_386_TLS_DESC_CALL
:
1327 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1328 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1329 case elfcpp::R_386_TLS_IE
: // Initial-exec
1330 case elfcpp::R_386_TLS_IE_32
:
1331 case elfcpp::R_386_TLS_GOTIE
:
1332 case elfcpp::R_386_TLS_LE
: // Local-exec
1333 case elfcpp::R_386_TLS_LE_32
:
1335 const bool is_final
= gsym
->final_value_is_known();
1336 const tls::Tls_optimization optimized_type
1337 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1340 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1341 if (optimized_type
== tls::TLSOPT_NONE
)
1343 // Create a pair of GOT entries for the module index and
1344 // dtv-relative offset.
1345 Output_data_got
<32, false>* got
1346 = target
->got_section(symtab
, layout
);
1347 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1348 target
->rel_dyn_section(layout
),
1349 elfcpp::R_386_TLS_DTPMOD32
,
1350 elfcpp::R_386_TLS_DTPOFF32
);
1352 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1354 // Create a GOT entry for the tp-relative offset.
1355 Output_data_got
<32, false>* got
1356 = target
->got_section(symtab
, layout
);
1357 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1358 target
->rel_dyn_section(layout
),
1359 elfcpp::R_386_TLS_TPOFF
);
1361 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1362 unsupported_reloc_global(object
, r_type
, gsym
);
1365 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1366 target
->define_tls_base_symbol(symtab
, layout
);
1367 if (optimized_type
== tls::TLSOPT_NONE
)
1369 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1370 Output_data_got
<32, false>* got
1371 = target
->got_section(symtab
, layout
);
1372 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1373 target
->rel_dyn_section(layout
),
1374 elfcpp::R_386_TLS_DESC
, 0);
1376 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1378 // Create a GOT entry for the tp-relative offset.
1379 Output_data_got
<32, false>* got
1380 = target
->got_section(symtab
, layout
);
1381 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1382 target
->rel_dyn_section(layout
),
1383 elfcpp::R_386_TLS_TPOFF
);
1385 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1386 unsupported_reloc_global(object
, r_type
, gsym
);
1389 case elfcpp::R_386_TLS_DESC_CALL
:
1392 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1393 if (optimized_type
== tls::TLSOPT_NONE
)
1395 // Create a GOT entry for the module index.
1396 target
->got_mod_index_entry(symtab
, layout
, object
);
1398 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1399 unsupported_reloc_global(object
, r_type
, gsym
);
1402 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1405 case elfcpp::R_386_TLS_IE
: // Initial-exec
1406 case elfcpp::R_386_TLS_IE_32
:
1407 case elfcpp::R_386_TLS_GOTIE
:
1408 layout
->set_has_static_tls();
1409 if (optimized_type
== tls::TLSOPT_NONE
)
1411 // For the R_386_TLS_IE relocation, we need to create a
1412 // dynamic relocation when building a shared library.
1413 if (r_type
== elfcpp::R_386_TLS_IE
1414 && parameters
->options().shared())
1416 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1417 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1418 output_section
, object
,
1420 reloc
.get_r_offset());
1422 // Create a GOT entry for the tp-relative offset.
1423 Output_data_got
<32, false>* got
1424 = target
->got_section(symtab
, layout
);
1425 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1426 ? elfcpp::R_386_TLS_TPOFF32
1427 : elfcpp::R_386_TLS_TPOFF
);
1428 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1429 ? GOT_TYPE_TLS_OFFSET
1430 : GOT_TYPE_TLS_NOFFSET
);
1431 got
->add_global_with_rel(gsym
, got_type
,
1432 target
->rel_dyn_section(layout
),
1435 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1436 unsupported_reloc_global(object
, r_type
, gsym
);
1439 case elfcpp::R_386_TLS_LE
: // Local-exec
1440 case elfcpp::R_386_TLS_LE_32
:
1441 layout
->set_has_static_tls();
1442 if (parameters
->options().shared())
1444 // We need to create a dynamic relocation.
1445 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1446 ? elfcpp::R_386_TLS_TPOFF32
1447 : elfcpp::R_386_TLS_TPOFF
);
1448 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1449 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1450 data_shndx
, reloc
.get_r_offset());
1460 case elfcpp::R_386_32PLT
:
1461 case elfcpp::R_386_TLS_GD_32
:
1462 case elfcpp::R_386_TLS_GD_PUSH
:
1463 case elfcpp::R_386_TLS_GD_CALL
:
1464 case elfcpp::R_386_TLS_GD_POP
:
1465 case elfcpp::R_386_TLS_LDM_32
:
1466 case elfcpp::R_386_TLS_LDM_PUSH
:
1467 case elfcpp::R_386_TLS_LDM_CALL
:
1468 case elfcpp::R_386_TLS_LDM_POP
:
1469 case elfcpp::R_386_USED_BY_INTEL_200
:
1471 unsupported_reloc_global(object
, r_type
, gsym
);
1476 // Process relocations for gc.
1479 Target_i386::gc_process_relocs(const General_options
& options
,
1480 Symbol_table
* symtab
,
1482 Sized_relobj
<32, false>* object
,
1483 unsigned int data_shndx
,
1485 const unsigned char* prelocs
,
1487 Output_section
* output_section
,
1488 bool needs_special_offset_handling
,
1489 size_t local_symbol_count
,
1490 const unsigned char* plocal_symbols
)
1492 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1503 needs_special_offset_handling
,
1508 // Scan relocations for a section.
1511 Target_i386::scan_relocs(const General_options
& options
,
1512 Symbol_table
* symtab
,
1514 Sized_relobj
<32, false>* object
,
1515 unsigned int data_shndx
,
1516 unsigned int sh_type
,
1517 const unsigned char* prelocs
,
1519 Output_section
* output_section
,
1520 bool needs_special_offset_handling
,
1521 size_t local_symbol_count
,
1522 const unsigned char* plocal_symbols
)
1524 if (sh_type
== elfcpp::SHT_RELA
)
1526 gold_error(_("%s: unsupported RELA reloc section"),
1527 object
->name().c_str());
1531 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1542 needs_special_offset_handling
,
1547 // Finalize the sections.
1550 Target_i386::do_finalize_sections(Layout
* layout
)
1552 // Fill in some more dynamic tags.
1553 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1556 if (this->got_plt_
!= NULL
)
1557 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1559 if (this->plt_
!= NULL
)
1561 const Output_data
* od
= this->plt_
->rel_plt();
1562 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1563 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1564 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1567 if (this->rel_dyn_
!= NULL
)
1569 const Output_data
* od
= this->rel_dyn_
;
1570 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1571 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1572 odyn
->add_constant(elfcpp::DT_RELENT
,
1573 elfcpp::Elf_sizes
<32>::rel_size
);
1576 if (!parameters
->options().shared())
1578 // The value of the DT_DEBUG tag is filled in by the dynamic
1579 // linker at run time, and used by the debugger.
1580 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1584 // Emit any relocs we saved in an attempt to avoid generating COPY
1586 if (this->copy_relocs_
.any_saved_relocs())
1587 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1590 // Return whether a direct absolute static relocation needs to be applied.
1591 // In cases where Scan::local() or Scan::global() has created
1592 // a dynamic relocation other than R_386_RELATIVE, the addend
1593 // of the relocation is carried in the data, and we must not
1594 // apply the static relocation.
1597 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1600 Output_section
* output_section
)
1602 // If the output section is not allocated, then we didn't call
1603 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1605 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1608 // For local symbols, we will have created a non-RELATIVE dynamic
1609 // relocation only if (a) the output is position independent,
1610 // (b) the relocation is absolute (not pc- or segment-relative), and
1611 // (c) the relocation is not 32 bits wide.
1613 return !(parameters
->options().output_is_position_independent()
1614 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1617 // For global symbols, we use the same helper routines used in the
1618 // scan pass. If we did not create a dynamic relocation, or if we
1619 // created a RELATIVE dynamic relocation, we should apply the static
1621 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1622 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1623 && gsym
->can_use_relative_reloc(ref_flags
1624 & Symbol::FUNCTION_CALL
);
1625 return !has_dyn
|| is_rel
;
1628 // Perform a relocation.
1631 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1632 Target_i386
* target
,
1633 Output_section
*output_section
,
1635 const elfcpp::Rel
<32, false>& rel
,
1636 unsigned int r_type
,
1637 const Sized_symbol
<32>* gsym
,
1638 const Symbol_value
<32>* psymval
,
1639 unsigned char* view
,
1640 elfcpp::Elf_types
<32>::Elf_Addr address
,
1641 section_size_type view_size
)
1643 if (this->skip_call_tls_get_addr_
)
1645 if ((r_type
!= elfcpp::R_386_PLT32
1646 && r_type
!= elfcpp::R_386_PC32
)
1648 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1649 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1650 _("missing expected TLS relocation"));
1653 this->skip_call_tls_get_addr_
= false;
1658 // Pick the value to use for symbols defined in shared objects.
1659 Symbol_value
<32> symval
;
1661 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1662 || r_type
== elfcpp::R_386_PC16
1663 || r_type
== elfcpp::R_386_PC32
))
1665 symval
.set_output_value(target
->plt_section()->address()
1666 + gsym
->plt_offset());
1670 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1672 // Get the GOT offset if needed.
1673 // The GOT pointer points to the end of the GOT section.
1674 // We need to subtract the size of the GOT section to get
1675 // the actual offset to use in the relocation.
1676 bool have_got_offset
= false;
1677 unsigned int got_offset
= 0;
1680 case elfcpp::R_386_GOT32
:
1683 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1684 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1685 - target
->got_size());
1689 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1690 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1691 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1692 - target
->got_size());
1694 have_got_offset
= true;
1703 case elfcpp::R_386_NONE
:
1704 case elfcpp::R_386_GNU_VTINHERIT
:
1705 case elfcpp::R_386_GNU_VTENTRY
:
1708 case elfcpp::R_386_32
:
1709 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1711 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1714 case elfcpp::R_386_PC32
:
1716 int ref_flags
= Symbol::NON_PIC_REF
;
1717 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1718 ref_flags
|= Symbol::FUNCTION_CALL
;
1719 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1720 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1724 case elfcpp::R_386_16
:
1725 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1727 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1730 case elfcpp::R_386_PC16
:
1732 int ref_flags
= Symbol::NON_PIC_REF
;
1733 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1734 ref_flags
|= Symbol::FUNCTION_CALL
;
1735 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1736 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1740 case elfcpp::R_386_8
:
1741 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1743 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1746 case elfcpp::R_386_PC8
:
1748 int ref_flags
= Symbol::NON_PIC_REF
;
1749 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1750 ref_flags
|= Symbol::FUNCTION_CALL
;
1751 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1753 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1757 case elfcpp::R_386_PLT32
:
1758 gold_assert(gsym
== NULL
1759 || gsym
->has_plt_offset()
1760 || gsym
->final_value_is_known()
1761 || (gsym
->is_defined()
1762 && !gsym
->is_from_dynobj()
1763 && !gsym
->is_preemptible()));
1764 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1767 case elfcpp::R_386_GOT32
:
1768 gold_assert(have_got_offset
);
1769 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1772 case elfcpp::R_386_GOTOFF
:
1774 elfcpp::Elf_types
<32>::Elf_Addr value
;
1775 value
= (psymval
->value(object
, 0)
1776 - target
->got_plt_section()->address());
1777 Relocate_functions
<32, false>::rel32(view
, value
);
1781 case elfcpp::R_386_GOTPC
:
1783 elfcpp::Elf_types
<32>::Elf_Addr value
;
1784 value
= target
->got_plt_section()->address();
1785 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1789 case elfcpp::R_386_COPY
:
1790 case elfcpp::R_386_GLOB_DAT
:
1791 case elfcpp::R_386_JUMP_SLOT
:
1792 case elfcpp::R_386_RELATIVE
:
1793 // These are outstanding tls relocs, which are unexpected when
1795 case elfcpp::R_386_TLS_TPOFF
:
1796 case elfcpp::R_386_TLS_DTPMOD32
:
1797 case elfcpp::R_386_TLS_DTPOFF32
:
1798 case elfcpp::R_386_TLS_TPOFF32
:
1799 case elfcpp::R_386_TLS_DESC
:
1800 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1801 _("unexpected reloc %u in object file"),
1805 // These are initial tls relocs, which are expected when
1807 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1808 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1809 case elfcpp::R_386_TLS_DESC_CALL
:
1810 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1811 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1812 case elfcpp::R_386_TLS_IE
: // Initial-exec
1813 case elfcpp::R_386_TLS_IE_32
:
1814 case elfcpp::R_386_TLS_GOTIE
:
1815 case elfcpp::R_386_TLS_LE
: // Local-exec
1816 case elfcpp::R_386_TLS_LE_32
:
1817 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1818 view
, address
, view_size
);
1821 case elfcpp::R_386_32PLT
:
1822 case elfcpp::R_386_TLS_GD_32
:
1823 case elfcpp::R_386_TLS_GD_PUSH
:
1824 case elfcpp::R_386_TLS_GD_CALL
:
1825 case elfcpp::R_386_TLS_GD_POP
:
1826 case elfcpp::R_386_TLS_LDM_32
:
1827 case elfcpp::R_386_TLS_LDM_PUSH
:
1828 case elfcpp::R_386_TLS_LDM_CALL
:
1829 case elfcpp::R_386_TLS_LDM_POP
:
1830 case elfcpp::R_386_USED_BY_INTEL_200
:
1832 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1833 _("unsupported reloc %u"),
1841 // Perform a TLS relocation.
1844 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1845 Target_i386
* target
,
1847 const elfcpp::Rel
<32, false>& rel
,
1848 unsigned int r_type
,
1849 const Sized_symbol
<32>* gsym
,
1850 const Symbol_value
<32>* psymval
,
1851 unsigned char* view
,
1852 elfcpp::Elf_types
<32>::Elf_Addr
,
1853 section_size_type view_size
)
1855 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1857 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1859 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1861 const bool is_final
=
1863 ? !parameters
->options().output_is_position_independent()
1864 : gsym
->final_value_is_known());
1865 const tls::Tls_optimization optimized_type
1866 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1869 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1870 if (optimized_type
== tls::TLSOPT_TO_LE
)
1872 gold_assert(tls_segment
!= NULL
);
1873 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1874 rel
, r_type
, value
, view
,
1880 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1881 ? GOT_TYPE_TLS_NOFFSET
1882 : GOT_TYPE_TLS_PAIR
);
1883 unsigned int got_offset
;
1886 gold_assert(gsym
->has_got_offset(got_type
));
1887 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1891 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1892 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1893 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1894 - target
->got_size());
1896 if (optimized_type
== tls::TLSOPT_TO_IE
)
1898 gold_assert(tls_segment
!= NULL
);
1899 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1900 got_offset
, view
, view_size
);
1903 else if (optimized_type
== tls::TLSOPT_NONE
)
1905 // Relocate the field with the offset of the pair of GOT
1907 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1911 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1912 _("unsupported reloc %u"),
1916 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1917 case elfcpp::R_386_TLS_DESC_CALL
:
1918 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1919 if (optimized_type
== tls::TLSOPT_TO_LE
)
1921 gold_assert(tls_segment
!= NULL
);
1922 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1923 rel
, r_type
, value
, view
,
1929 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1930 ? GOT_TYPE_TLS_NOFFSET
1931 : GOT_TYPE_TLS_DESC
);
1932 unsigned int got_offset
;
1935 gold_assert(gsym
->has_got_offset(got_type
));
1936 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1940 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1941 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1942 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1943 - target
->got_size());
1945 if (optimized_type
== tls::TLSOPT_TO_IE
)
1947 gold_assert(tls_segment
!= NULL
);
1948 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1949 got_offset
, view
, view_size
);
1952 else if (optimized_type
== tls::TLSOPT_NONE
)
1954 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1956 // Relocate the field with the offset of the pair of GOT
1958 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1963 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1964 _("unsupported reloc %u"),
1968 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1969 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1971 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1972 _("both SUN and GNU model "
1973 "TLS relocations"));
1976 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1977 if (optimized_type
== tls::TLSOPT_TO_LE
)
1979 gold_assert(tls_segment
!= NULL
);
1980 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1981 value
, view
, view_size
);
1984 else if (optimized_type
== tls::TLSOPT_NONE
)
1986 // Relocate the field with the offset of the GOT entry for
1987 // the module index.
1988 unsigned int got_offset
;
1989 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1990 - target
->got_size());
1991 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1994 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1995 _("unsupported reloc %u"),
1999 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2000 // This reloc can appear in debugging sections, in which case we
2001 // won't see the TLS_LDM reloc. The local_dynamic_type field
2003 if (optimized_type
== tls::TLSOPT_TO_LE
2004 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
2006 gold_assert(tls_segment
!= NULL
);
2007 value
-= tls_segment
->memsz();
2009 Relocate_functions
<32, false>::rel32(view
, value
);
2012 case elfcpp::R_386_TLS_IE
: // Initial-exec
2013 case elfcpp::R_386_TLS_GOTIE
:
2014 case elfcpp::R_386_TLS_IE_32
:
2015 if (optimized_type
== tls::TLSOPT_TO_LE
)
2017 gold_assert(tls_segment
!= NULL
);
2018 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2019 rel
, r_type
, value
, view
,
2023 else if (optimized_type
== tls::TLSOPT_NONE
)
2025 // Relocate the field with the offset of the GOT entry for
2026 // the tp-relative offset of the symbol.
2027 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2028 ? GOT_TYPE_TLS_OFFSET
2029 : GOT_TYPE_TLS_NOFFSET
);
2030 unsigned int got_offset
;
2033 gold_assert(gsym
->has_got_offset(got_type
));
2034 got_offset
= gsym
->got_offset(got_type
);
2038 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2039 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2040 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2042 // For the R_386_TLS_IE relocation, we need to apply the
2043 // absolute address of the GOT entry.
2044 if (r_type
== elfcpp::R_386_TLS_IE
)
2045 got_offset
+= target
->got_plt_section()->address();
2046 // All GOT offsets are relative to the end of the GOT.
2047 got_offset
-= target
->got_size();
2048 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2051 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2052 _("unsupported reloc %u"),
2056 case elfcpp::R_386_TLS_LE
: // Local-exec
2057 // If we're creating a shared library, a dynamic relocation will
2058 // have been created for this location, so do not apply it now.
2059 if (!parameters
->options().shared())
2061 gold_assert(tls_segment
!= NULL
);
2062 value
-= tls_segment
->memsz();
2063 Relocate_functions
<32, false>::rel32(view
, value
);
2067 case elfcpp::R_386_TLS_LE_32
:
2068 // If we're creating a shared library, a dynamic relocation will
2069 // have been created for this location, so do not apply it now.
2070 if (!parameters
->options().shared())
2072 gold_assert(tls_segment
!= NULL
);
2073 value
= tls_segment
->memsz() - value
;
2074 Relocate_functions
<32, false>::rel32(view
, value
);
2080 // Do a relocation in which we convert a TLS General-Dynamic to a
2084 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2086 Output_segment
* tls_segment
,
2087 const elfcpp::Rel
<32, false>& rel
,
2089 elfcpp::Elf_types
<32>::Elf_Addr value
,
2090 unsigned char* view
,
2091 section_size_type view_size
)
2093 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2094 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2095 // leal foo(%reg),%eax; call ___tls_get_addr
2096 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2098 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2099 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2101 unsigned char op1
= view
[-1];
2102 unsigned char op2
= view
[-2];
2104 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2105 op2
== 0x8d || op2
== 0x04);
2106 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2112 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2113 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2114 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2115 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2116 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2120 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2121 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2122 if (rel
.get_r_offset() + 9 < view_size
2125 // There is a trailing nop. Use the size byte subl.
2126 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2131 // Use the five byte subl.
2132 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2136 value
= tls_segment
->memsz() - value
;
2137 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2139 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2141 this->skip_call_tls_get_addr_
= true;
2144 // Do a relocation in which we convert a TLS General-Dynamic to an
2148 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2151 const elfcpp::Rel
<32, false>& rel
,
2153 elfcpp::Elf_types
<32>::Elf_Addr value
,
2154 unsigned char* view
,
2155 section_size_type view_size
)
2157 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2158 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2160 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2161 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2163 unsigned char op1
= view
[-1];
2164 unsigned char op2
= view
[-2];
2166 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2167 op2
== 0x8d || op2
== 0x04);
2168 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2172 // FIXME: For now, support only the first (SIB) form.
2173 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2177 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2178 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2179 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2180 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2181 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2185 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2186 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2187 if (rel
.get_r_offset() + 9 < view_size
2190 // FIXME: This is not the right instruction sequence.
2191 // There is a trailing nop. Use the size byte subl.
2192 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2197 // FIXME: This is not the right instruction sequence.
2198 // Use the five byte subl.
2199 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2203 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2205 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2207 this->skip_call_tls_get_addr_
= true;
2210 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2211 // General-Dynamic to a Local-Exec.
2214 Target_i386::Relocate::tls_desc_gd_to_le(
2215 const Relocate_info
<32, false>* relinfo
,
2217 Output_segment
* tls_segment
,
2218 const elfcpp::Rel
<32, false>& rel
,
2219 unsigned int r_type
,
2220 elfcpp::Elf_types
<32>::Elf_Addr value
,
2221 unsigned char* view
,
2222 section_size_type view_size
)
2224 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2226 // leal foo@TLSDESC(%ebx), %eax
2227 // ==> leal foo@NTPOFF, %eax
2228 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2229 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2230 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2231 view
[-2] == 0x8d && view
[-1] == 0x83);
2233 value
-= tls_segment
->memsz();
2234 Relocate_functions
<32, false>::rel32(view
, value
);
2238 // call *foo@TLSCALL(%eax)
2240 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2241 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2242 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2243 view
[0] == 0xff && view
[1] == 0x10);
2249 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2250 // General-Dynamic to an Initial-Exec.
2253 Target_i386::Relocate::tls_desc_gd_to_ie(
2254 const Relocate_info
<32, false>* relinfo
,
2257 const elfcpp::Rel
<32, false>& rel
,
2258 unsigned int r_type
,
2259 elfcpp::Elf_types
<32>::Elf_Addr value
,
2260 unsigned char* view
,
2261 section_size_type view_size
)
2263 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2265 // leal foo@TLSDESC(%ebx), %eax
2266 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2267 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2268 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2269 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2270 view
[-2] == 0x8d && view
[-1] == 0x83);
2272 Relocate_functions
<32, false>::rel32(view
, value
);
2276 // call *foo@TLSCALL(%eax)
2278 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2279 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2280 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2281 view
[0] == 0xff && view
[1] == 0x10);
2287 // Do a relocation in which we convert a TLS Local-Dynamic to a
2291 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2294 const elfcpp::Rel
<32, false>& rel
,
2296 elfcpp::Elf_types
<32>::Elf_Addr
,
2297 unsigned char* view
,
2298 section_size_type view_size
)
2300 // leal foo(%reg), %eax; call ___tls_get_addr
2301 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2303 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2304 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2306 // FIXME: Does this test really always pass?
2307 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2308 view
[-2] == 0x8d && view
[-1] == 0x83);
2310 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2312 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2314 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2316 this->skip_call_tls_get_addr_
= true;
2319 // Do a relocation in which we convert a TLS Initial-Exec to a
2323 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2325 Output_segment
* tls_segment
,
2326 const elfcpp::Rel
<32, false>& rel
,
2327 unsigned int r_type
,
2328 elfcpp::Elf_types
<32>::Elf_Addr value
,
2329 unsigned char* view
,
2330 section_size_type view_size
)
2332 // We have to actually change the instructions, which means that we
2333 // need to examine the opcodes to figure out which instruction we
2335 if (r_type
== elfcpp::R_386_TLS_IE
)
2337 // movl %gs:XX,%eax ==> movl $YY,%eax
2338 // movl %gs:XX,%reg ==> movl $YY,%reg
2339 // addl %gs:XX,%reg ==> addl $YY,%reg
2340 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2341 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2343 unsigned char op1
= view
[-1];
2346 // movl XX,%eax ==> movl $YY,%eax
2351 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2353 unsigned char op2
= view
[-2];
2356 // movl XX,%reg ==> movl $YY,%reg
2357 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2358 (op1
& 0xc7) == 0x05);
2360 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2362 else if (op2
== 0x03)
2364 // addl XX,%reg ==> addl $YY,%reg
2365 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2366 (op1
& 0xc7) == 0x05);
2368 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2371 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2376 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2377 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2378 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2379 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2380 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2382 unsigned char op1
= view
[-1];
2383 unsigned char op2
= view
[-2];
2384 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2385 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2388 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2390 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2392 else if (op2
== 0x2b)
2394 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2396 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2398 else if (op2
== 0x03)
2400 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2402 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2405 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2408 value
= tls_segment
->memsz() - value
;
2409 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2412 Relocate_functions
<32, false>::rel32(view
, value
);
2415 // Relocate section data.
2418 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2419 unsigned int sh_type
,
2420 const unsigned char* prelocs
,
2422 Output_section
* output_section
,
2423 bool needs_special_offset_handling
,
2424 unsigned char* view
,
2425 elfcpp::Elf_types
<32>::Elf_Addr address
,
2426 section_size_type view_size
)
2428 gold_assert(sh_type
== elfcpp::SHT_REL
);
2430 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2431 Target_i386::Relocate
>(
2437 needs_special_offset_handling
,
2443 // Return the size of a relocation while scanning during a relocatable
2447 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2448 unsigned int r_type
,
2453 case elfcpp::R_386_NONE
:
2454 case elfcpp::R_386_GNU_VTINHERIT
:
2455 case elfcpp::R_386_GNU_VTENTRY
:
2456 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2457 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2458 case elfcpp::R_386_TLS_DESC_CALL
:
2459 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2460 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2461 case elfcpp::R_386_TLS_IE
: // Initial-exec
2462 case elfcpp::R_386_TLS_IE_32
:
2463 case elfcpp::R_386_TLS_GOTIE
:
2464 case elfcpp::R_386_TLS_LE
: // Local-exec
2465 case elfcpp::R_386_TLS_LE_32
:
2468 case elfcpp::R_386_32
:
2469 case elfcpp::R_386_PC32
:
2470 case elfcpp::R_386_GOT32
:
2471 case elfcpp::R_386_PLT32
:
2472 case elfcpp::R_386_GOTOFF
:
2473 case elfcpp::R_386_GOTPC
:
2476 case elfcpp::R_386_16
:
2477 case elfcpp::R_386_PC16
:
2480 case elfcpp::R_386_8
:
2481 case elfcpp::R_386_PC8
:
2484 // These are relocations which should only be seen by the
2485 // dynamic linker, and should never be seen here.
2486 case elfcpp::R_386_COPY
:
2487 case elfcpp::R_386_GLOB_DAT
:
2488 case elfcpp::R_386_JUMP_SLOT
:
2489 case elfcpp::R_386_RELATIVE
:
2490 case elfcpp::R_386_TLS_TPOFF
:
2491 case elfcpp::R_386_TLS_DTPMOD32
:
2492 case elfcpp::R_386_TLS_DTPOFF32
:
2493 case elfcpp::R_386_TLS_TPOFF32
:
2494 case elfcpp::R_386_TLS_DESC
:
2495 object
->error(_("unexpected reloc %u in object file"), r_type
);
2498 case elfcpp::R_386_32PLT
:
2499 case elfcpp::R_386_TLS_GD_32
:
2500 case elfcpp::R_386_TLS_GD_PUSH
:
2501 case elfcpp::R_386_TLS_GD_CALL
:
2502 case elfcpp::R_386_TLS_GD_POP
:
2503 case elfcpp::R_386_TLS_LDM_32
:
2504 case elfcpp::R_386_TLS_LDM_PUSH
:
2505 case elfcpp::R_386_TLS_LDM_CALL
:
2506 case elfcpp::R_386_TLS_LDM_POP
:
2507 case elfcpp::R_386_USED_BY_INTEL_200
:
2509 object
->error(_("unsupported reloc %u in object file"), r_type
);
2514 // Scan the relocs during a relocatable link.
2517 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2518 Symbol_table
* symtab
,
2520 Sized_relobj
<32, false>* object
,
2521 unsigned int data_shndx
,
2522 unsigned int sh_type
,
2523 const unsigned char* prelocs
,
2525 Output_section
* output_section
,
2526 bool needs_special_offset_handling
,
2527 size_t local_symbol_count
,
2528 const unsigned char* plocal_symbols
,
2529 Relocatable_relocs
* rr
)
2531 gold_assert(sh_type
== elfcpp::SHT_REL
);
2533 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2534 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2536 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2537 Scan_relocatable_relocs
>(
2546 needs_special_offset_handling
,
2552 // Relocate a section during a relocatable link.
2555 Target_i386::relocate_for_relocatable(
2556 const Relocate_info
<32, false>* relinfo
,
2557 unsigned int sh_type
,
2558 const unsigned char* prelocs
,
2560 Output_section
* output_section
,
2561 off_t offset_in_output_section
,
2562 const Relocatable_relocs
* rr
,
2563 unsigned char* view
,
2564 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2565 section_size_type view_size
,
2566 unsigned char* reloc_view
,
2567 section_size_type reloc_view_size
)
2569 gold_assert(sh_type
== elfcpp::SHT_REL
);
2571 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2576 offset_in_output_section
,
2585 // Return the value to use for a dynamic which requires special
2586 // treatment. This is how we support equality comparisons of function
2587 // pointers across shared library boundaries, as described in the
2588 // processor specific ABI supplement.
2591 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2593 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2594 return this->plt_section()->address() + gsym
->plt_offset();
2597 // Return a string used to fill a code section with nops to take up
2598 // the specified length.
2601 Target_i386::do_code_fill(section_size_type length
) const
2605 // Build a jmp instruction to skip over the bytes.
2606 unsigned char jmp
[5];
2608 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2609 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2610 + std::string(length
- 5, '\0'));
2613 // Nop sequences of various lengths.
2614 const char nop1
[1] = { 0x90 }; // nop
2615 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2616 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2617 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2618 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2619 0x00 }; // leal 0(%esi,1),%esi
2620 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2622 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2624 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2625 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2626 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2627 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2629 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2630 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2632 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2633 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2635 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2636 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2637 0x00, 0x00, 0x00, 0x00 };
2638 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2639 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2640 0x27, 0x00, 0x00, 0x00,
2642 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2643 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2644 0xbc, 0x27, 0x00, 0x00,
2646 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2647 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2648 0x90, 0x90, 0x90, 0x90,
2651 const char* nops
[16] = {
2653 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2654 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2657 return std::string(nops
[length
], length
);
2660 // The selector for i386 object files.
2662 class Target_selector_i386
: public Target_selector
2665 Target_selector_i386()
2666 : Target_selector(elfcpp::EM_386
, 32, false, "elf32-i386")
2670 do_instantiate_target()
2671 { return new Target_i386(); }
2674 Target_selector_i386 target_selector_i386
;
2676 } // End anonymous namespace.