1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008 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 // Scan the relocations to look for symbol adjustments.
67 scan_relocs(const General_options
& options
,
70 Sized_relobj
<32, false>* object
,
71 unsigned int data_shndx
,
73 const unsigned char* prelocs
,
75 Output_section
* output_section
,
76 bool needs_special_offset_handling
,
77 size_t local_symbol_count
,
78 const unsigned char* plocal_symbols
);
80 // Finalize the sections.
82 do_finalize_sections(Layout
*);
84 // Return the value to use for a dynamic which requires special
87 do_dynsym_value(const Symbol
*) const;
89 // Relocate a section.
91 relocate_section(const Relocate_info
<32, false>*,
93 const unsigned char* prelocs
,
95 Output_section
* output_section
,
96 bool needs_special_offset_handling
,
98 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
99 section_size_type view_size
);
101 // Scan the relocs during a relocatable link.
103 scan_relocatable_relocs(const General_options
& options
,
104 Symbol_table
* symtab
,
106 Sized_relobj
<32, false>* object
,
107 unsigned int data_shndx
,
108 unsigned int sh_type
,
109 const unsigned char* prelocs
,
111 Output_section
* output_section
,
112 bool needs_special_offset_handling
,
113 size_t local_symbol_count
,
114 const unsigned char* plocal_symbols
,
115 Relocatable_relocs
*);
117 // Relocate a section during a relocatable link.
119 relocate_for_relocatable(const Relocate_info
<32, false>*,
120 unsigned int sh_type
,
121 const unsigned char* prelocs
,
123 Output_section
* output_section
,
124 off_t offset_in_output_section
,
125 const Relocatable_relocs
*,
127 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
128 section_size_type view_size
,
129 unsigned char* reloc_view
,
130 section_size_type reloc_view_size
);
132 // Return a string used to fill a code section with nops.
134 do_code_fill(section_size_type length
) const;
136 // Return whether SYM is defined by the ABI.
138 do_is_defined_by_abi(Symbol
* sym
) const
139 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
141 // Return the size of the GOT section.
145 gold_assert(this->got_
!= NULL
);
146 return this->got_
->data_size();
150 // The class which scans relocations.
154 local(const General_options
& options
, Symbol_table
* symtab
,
155 Layout
* layout
, Target_i386
* target
,
156 Sized_relobj
<32, false>* object
,
157 unsigned int data_shndx
,
158 Output_section
* output_section
,
159 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
160 const elfcpp::Sym
<32, false>& lsym
);
163 global(const General_options
& options
, Symbol_table
* symtab
,
164 Layout
* layout
, Target_i386
* target
,
165 Sized_relobj
<32, false>* object
,
166 unsigned int data_shndx
,
167 Output_section
* output_section
,
168 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
172 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
175 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
179 // The class which implements relocation.
184 : skip_call_tls_get_addr_(false),
185 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
190 if (this->skip_call_tls_get_addr_
)
192 // FIXME: This needs to specify the location somehow.
193 gold_error(_("missing expected TLS relocation"));
197 // Return whether the static relocation needs to be applied.
199 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
203 // Do a relocation. Return false if the caller should not issue
204 // any warnings about this relocation.
206 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
207 const elfcpp::Rel
<32, false>&,
208 unsigned int r_type
, const Sized_symbol
<32>*,
209 const Symbol_value
<32>*,
210 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
214 // Do a TLS relocation.
216 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
217 size_t relnum
, const elfcpp::Rel
<32, false>&,
218 unsigned int r_type
, const Sized_symbol
<32>*,
219 const Symbol_value
<32>*,
220 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
223 // Do a TLS General-Dynamic to Initial-Exec transition.
225 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
226 Output_segment
* tls_segment
,
227 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
228 elfcpp::Elf_types
<32>::Elf_Addr value
,
230 section_size_type view_size
);
232 // Do a TLS General-Dynamic to Local-Exec transition.
234 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
235 Output_segment
* tls_segment
,
236 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
237 elfcpp::Elf_types
<32>::Elf_Addr value
,
239 section_size_type view_size
);
241 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
244 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
245 Output_segment
* tls_segment
,
246 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
247 elfcpp::Elf_types
<32>::Elf_Addr value
,
249 section_size_type view_size
);
251 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
254 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
255 Output_segment
* tls_segment
,
256 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
257 elfcpp::Elf_types
<32>::Elf_Addr value
,
259 section_size_type view_size
);
261 // Do a TLS Local-Dynamic to Local-Exec transition.
263 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
264 Output_segment
* tls_segment
,
265 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
266 elfcpp::Elf_types
<32>::Elf_Addr value
,
268 section_size_type view_size
);
270 // Do a TLS Initial-Exec to Local-Exec transition.
272 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
273 Output_segment
* tls_segment
,
274 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
275 elfcpp::Elf_types
<32>::Elf_Addr value
,
277 section_size_type view_size
);
279 // We need to keep track of which type of local dynamic relocation
280 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
281 enum Local_dynamic_type
288 // This is set if we should skip the next reloc, which should be a
289 // PLT32 reloc against ___tls_get_addr.
290 bool skip_call_tls_get_addr_
;
291 // The type of local dynamic relocation we have seen in the section
292 // being relocated, if any.
293 Local_dynamic_type local_dynamic_type_
;
296 // A class which returns the size required for a relocation type,
297 // used while scanning relocs during a relocatable link.
298 class Relocatable_size_for_reloc
302 get_size_for_reloc(unsigned int, Relobj
*);
305 // Adjust TLS relocation type based on the options and whether this
306 // is a local symbol.
307 static tls::Tls_optimization
308 optimize_tls_reloc(bool is_final
, int r_type
);
310 // Get the GOT section, creating it if necessary.
311 Output_data_got
<32, false>*
312 got_section(Symbol_table
*, Layout
*);
314 // Get the GOT PLT section.
316 got_plt_section() const
318 gold_assert(this->got_plt_
!= NULL
);
319 return this->got_plt_
;
322 // Create a PLT entry for a global symbol.
324 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
326 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
328 define_tls_base_symbol(Symbol_table
*, Layout
*);
330 // Create a GOT entry for the TLS module index.
332 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
333 Sized_relobj
<32, false>* object
);
335 // Get the PLT section.
336 const Output_data_plt_i386
*
339 gold_assert(this->plt_
!= NULL
);
343 // Get the dynamic reloc section, creating it if necessary.
345 rel_dyn_section(Layout
*);
347 // Return true if the symbol may need a COPY relocation.
348 // References from an executable object to non-function symbols
349 // defined in a dynamic object may need a COPY relocation.
351 may_need_copy_reloc(Symbol
* gsym
)
353 return (!parameters
->options().shared()
354 && gsym
->is_from_dynobj()
355 && gsym
->type() != elfcpp::STT_FUNC
);
358 // Add a potential copy relocation.
360 copy_reloc(Symbol_table
* symtab
, Layout
* layout
, Relobj
* object
,
361 unsigned int shndx
, Output_section
* output_section
,
362 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
364 this->copy_relocs_
.copy_reloc(symtab
, layout
,
365 symtab
->get_sized_symbol
<32>(sym
),
366 object
, shndx
, output_section
, reloc
,
367 this->rel_dyn_section(layout
));
370 // Information about this specific target which we pass to the
371 // general Target structure.
372 static const Target::Target_info i386_info
;
374 // The types of GOT entries needed for this platform.
377 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
378 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
379 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
380 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
381 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
385 Output_data_got
<32, false>* got_
;
387 Output_data_plt_i386
* plt_
;
388 // The GOT PLT section.
389 Output_data_space
* got_plt_
;
390 // The dynamic reloc section.
391 Reloc_section
* rel_dyn_
;
392 // Relocs saved to avoid a COPY reloc.
393 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
394 // Space for variables copied with a COPY reloc.
395 Output_data_space
* dynbss_
;
396 // Offset of the GOT entry for the TLS module index.
397 unsigned int got_mod_index_offset_
;
398 // True if the _TLS_MODULE_BASE_ symbol has been defined.
399 bool tls_base_symbol_defined_
;
402 const Target::Target_info
Target_i386::i386_info
=
405 false, // is_big_endian
406 elfcpp::EM_386
, // machine_code
407 false, // has_make_symbol
408 false, // has_resolve
409 true, // has_code_fill
410 true, // is_default_stack_executable
412 "/usr/lib/libc.so.1", // dynamic_linker
413 0x08048000, // default_text_segment_address
414 0x1000, // abi_pagesize (overridable by -z max-page-size)
415 0x1000 // common_pagesize (overridable by -z common-page-size)
418 // Get the GOT section, creating it if necessary.
420 Output_data_got
<32, false>*
421 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
423 if (this->got_
== NULL
)
425 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
427 this->got_
= new Output_data_got
<32, false>();
430 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
432 | elfcpp::SHF_WRITE
),
436 // The old GNU linker creates a .got.plt section. We just
437 // create another set of data in the .got section. Note that we
438 // always create a PLT if we create a GOT, although the PLT
440 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
441 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
443 | elfcpp::SHF_WRITE
),
447 // The first three entries are reserved.
448 this->got_plt_
->set_current_data_size(3 * 4);
450 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
451 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
453 0, 0, elfcpp::STT_OBJECT
,
455 elfcpp::STV_HIDDEN
, 0,
462 // Get the dynamic reloc section, creating it if necessary.
464 Target_i386::Reloc_section
*
465 Target_i386::rel_dyn_section(Layout
* layout
)
467 if (this->rel_dyn_
== NULL
)
469 gold_assert(layout
!= NULL
);
470 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
471 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
472 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
474 return this->rel_dyn_
;
477 // A class to handle the PLT data.
479 class Output_data_plt_i386
: public Output_section_data
482 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
484 Output_data_plt_i386(Layout
*, Output_data_space
*);
486 // Add an entry to the PLT.
488 add_entry(Symbol
* gsym
);
490 // Return the .rel.plt section data.
493 { return this->rel_
; }
497 do_adjust_output_section(Output_section
* os
);
499 // Write to a map file.
501 do_print_to_mapfile(Mapfile
* mapfile
) const
502 { mapfile
->print_output_data(this, _("** PLT")); }
505 // The size of an entry in the PLT.
506 static const int plt_entry_size
= 16;
508 // The first entry in the PLT for an executable.
509 static unsigned char exec_first_plt_entry
[plt_entry_size
];
511 // The first entry in the PLT for a shared object.
512 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
514 // Other entries in the PLT for an executable.
515 static unsigned char exec_plt_entry
[plt_entry_size
];
517 // Other entries in the PLT for a shared object.
518 static unsigned char dyn_plt_entry
[plt_entry_size
];
520 // Set the final size.
522 set_final_data_size()
523 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
525 // Write out the PLT data.
527 do_write(Output_file
*);
529 // The reloc section.
531 // The .got.plt section.
532 Output_data_space
* got_plt_
;
533 // The number of PLT entries.
537 // Create the PLT section. The ordinary .got section is an argument,
538 // since we need to refer to the start. We also create our own .got
539 // section just for PLT entries.
541 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
542 Output_data_space
* got_plt
)
543 : Output_section_data(4), got_plt_(got_plt
), count_(0)
545 this->rel_
= new Reloc_section(false);
546 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
547 elfcpp::SHF_ALLOC
, this->rel_
);
551 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
553 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
554 // linker, and so do we.
558 // Add an entry to the PLT.
561 Output_data_plt_i386::add_entry(Symbol
* gsym
)
563 gold_assert(!gsym
->has_plt_offset());
565 // Note that when setting the PLT offset we skip the initial
566 // reserved PLT entry.
567 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
571 section_offset_type got_offset
= this->got_plt_
->current_data_size();
573 // Every PLT entry needs a GOT entry which points back to the PLT
574 // entry (this will be changed by the dynamic linker, normally
575 // lazily when the function is called).
576 this->got_plt_
->set_current_data_size(got_offset
+ 4);
578 // Every PLT entry needs a reloc.
579 gsym
->set_needs_dynsym_entry();
580 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
583 // Note that we don't need to save the symbol. The contents of the
584 // PLT are independent of which symbols are used. The symbols only
585 // appear in the relocations.
588 // The first entry in the PLT for an executable.
590 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
592 0xff, 0x35, // pushl contents of memory address
593 0, 0, 0, 0, // replaced with address of .got + 4
594 0xff, 0x25, // jmp indirect
595 0, 0, 0, 0, // replaced with address of .got + 8
599 // The first entry in the PLT for a shared object.
601 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
603 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
604 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
608 // Subsequent entries in the PLT for an executable.
610 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
612 0xff, 0x25, // jmp indirect
613 0, 0, 0, 0, // replaced with address of symbol in .got
614 0x68, // pushl immediate
615 0, 0, 0, 0, // replaced with offset into relocation table
616 0xe9, // jmp relative
617 0, 0, 0, 0 // replaced with offset to start of .plt
620 // Subsequent entries in the PLT for a shared object.
622 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
624 0xff, 0xa3, // jmp *offset(%ebx)
625 0, 0, 0, 0, // replaced with offset of symbol in .got
626 0x68, // pushl immediate
627 0, 0, 0, 0, // replaced with offset into relocation table
628 0xe9, // jmp relative
629 0, 0, 0, 0 // replaced with offset to start of .plt
632 // Write out the PLT. This uses the hand-coded instructions above,
633 // and adjusts them as needed. This is all specified by the i386 ELF
634 // Processor Supplement.
637 Output_data_plt_i386::do_write(Output_file
* of
)
639 const off_t offset
= this->offset();
640 const section_size_type oview_size
=
641 convert_to_section_size_type(this->data_size());
642 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
644 const off_t got_file_offset
= this->got_plt_
->offset();
645 const section_size_type got_size
=
646 convert_to_section_size_type(this->got_plt_
->data_size());
647 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
650 unsigned char* pov
= oview
;
652 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
653 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
655 if (parameters
->options().shared())
656 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
659 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
660 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
661 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
663 pov
+= plt_entry_size
;
665 unsigned char* got_pov
= got_view
;
667 memset(got_pov
, 0, 12);
670 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
672 unsigned int plt_offset
= plt_entry_size
;
673 unsigned int plt_rel_offset
= 0;
674 unsigned int got_offset
= 12;
675 const unsigned int count
= this->count_
;
676 for (unsigned int i
= 0;
679 pov
+= plt_entry_size
,
681 plt_offset
+= plt_entry_size
,
682 plt_rel_offset
+= rel_size
,
685 // Set and adjust the PLT entry itself.
687 if (parameters
->options().shared())
689 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
690 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
694 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
695 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
700 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
701 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
702 - (plt_offset
+ plt_entry_size
));
704 // Set the entry in the GOT.
705 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
708 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
709 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
711 of
->write_output_view(offset
, oview_size
, oview
);
712 of
->write_output_view(got_file_offset
, got_size
, got_view
);
715 // Create a PLT entry for a global symbol.
718 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
720 if (gsym
->has_plt_offset())
723 if (this->plt_
== NULL
)
725 // Create the GOT sections first.
726 this->got_section(symtab
, layout
);
728 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
729 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
731 | elfcpp::SHF_EXECINSTR
),
735 this->plt_
->add_entry(gsym
);
738 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
741 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
743 if (this->tls_base_symbol_defined_
)
746 Output_segment
* tls_segment
= layout
->tls_segment();
747 if (tls_segment
!= NULL
)
749 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
753 elfcpp::STV_HIDDEN
, 0,
754 Symbol::SEGMENT_END
, true);
756 this->tls_base_symbol_defined_
= true;
759 // Create a GOT entry for the TLS module index.
762 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
763 Sized_relobj
<32, false>* object
)
765 if (this->got_mod_index_offset_
== -1U)
767 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
768 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
769 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
770 unsigned int got_offset
= got
->add_constant(0);
771 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
773 got
->add_constant(0);
774 this->got_mod_index_offset_
= got_offset
;
776 return this->got_mod_index_offset_
;
779 // Optimize the TLS relocation type based on what we know about the
780 // symbol. IS_FINAL is true if the final address of this symbol is
781 // known at link time.
783 tls::Tls_optimization
784 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
786 // If we are generating a shared library, then we can't do anything
788 if (parameters
->options().shared())
789 return tls::TLSOPT_NONE
;
793 case elfcpp::R_386_TLS_GD
:
794 case elfcpp::R_386_TLS_GOTDESC
:
795 case elfcpp::R_386_TLS_DESC_CALL
:
796 // These are General-Dynamic which permits fully general TLS
797 // access. Since we know that we are generating an executable,
798 // we can convert this to Initial-Exec. If we also know that
799 // this is a local symbol, we can further switch to Local-Exec.
801 return tls::TLSOPT_TO_LE
;
802 return tls::TLSOPT_TO_IE
;
804 case elfcpp::R_386_TLS_LDM
:
805 // This is Local-Dynamic, which refers to a local symbol in the
806 // dynamic TLS block. Since we know that we generating an
807 // executable, we can switch to Local-Exec.
808 return tls::TLSOPT_TO_LE
;
810 case elfcpp::R_386_TLS_LDO_32
:
811 // Another type of Local-Dynamic relocation.
812 return tls::TLSOPT_TO_LE
;
814 case elfcpp::R_386_TLS_IE
:
815 case elfcpp::R_386_TLS_GOTIE
:
816 case elfcpp::R_386_TLS_IE_32
:
817 // These are Initial-Exec relocs which get the thread offset
818 // from the GOT. If we know that we are linking against the
819 // local symbol, we can switch to Local-Exec, which links the
820 // thread offset into the instruction.
822 return tls::TLSOPT_TO_LE
;
823 return tls::TLSOPT_NONE
;
825 case elfcpp::R_386_TLS_LE
:
826 case elfcpp::R_386_TLS_LE_32
:
827 // When we already have Local-Exec, there is nothing further we
829 return tls::TLSOPT_NONE
;
836 // Report an unsupported relocation against a local symbol.
839 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
842 gold_error(_("%s: unsupported reloc %u against local symbol"),
843 object
->name().c_str(), r_type
);
846 // Scan a relocation for a local symbol.
849 Target_i386::Scan::local(const General_options
&,
850 Symbol_table
* symtab
,
853 Sized_relobj
<32, false>* object
,
854 unsigned int data_shndx
,
855 Output_section
* output_section
,
856 const elfcpp::Rel
<32, false>& reloc
,
858 const elfcpp::Sym
<32, false>& lsym
)
862 case elfcpp::R_386_NONE
:
863 case elfcpp::R_386_GNU_VTINHERIT
:
864 case elfcpp::R_386_GNU_VTENTRY
:
867 case elfcpp::R_386_32
:
868 // If building a shared library (or a position-independent
869 // executable), we need to create a dynamic relocation for
870 // this location. The relocation applied at link time will
871 // apply the link-time value, so we flag the location with
872 // an R_386_RELATIVE relocation so the dynamic loader can
873 // relocate it easily.
874 if (parameters
->options().output_is_position_independent())
876 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
877 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
878 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
879 output_section
, data_shndx
,
880 reloc
.get_r_offset());
884 case elfcpp::R_386_16
:
885 case elfcpp::R_386_8
:
886 // If building a shared library (or a position-independent
887 // executable), we need to create a dynamic relocation for
888 // this location. Because the addend needs to remain in the
889 // data section, we need to be careful not to apply this
890 // relocation statically.
891 if (parameters
->options().output_is_position_independent())
893 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
894 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
895 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
896 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
897 data_shndx
, reloc
.get_r_offset());
900 gold_assert(lsym
.get_st_value() == 0);
901 unsigned int shndx
= lsym
.get_st_shndx();
903 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
906 object
->error(_("section symbol %u has bad shndx %u"),
909 rel_dyn
->add_local_section(object
, shndx
,
910 r_type
, output_section
,
911 data_shndx
, reloc
.get_r_offset());
916 case elfcpp::R_386_PC32
:
917 case elfcpp::R_386_PC16
:
918 case elfcpp::R_386_PC8
:
921 case elfcpp::R_386_PLT32
:
922 // Since we know this is a local symbol, we can handle this as a
926 case elfcpp::R_386_GOTOFF
:
927 case elfcpp::R_386_GOTPC
:
928 // We need a GOT section.
929 target
->got_section(symtab
, layout
);
932 case elfcpp::R_386_GOT32
:
934 // The symbol requires a GOT entry.
935 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
936 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
937 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
939 // If we are generating a shared object, we need to add a
940 // dynamic RELATIVE relocation for this symbol's GOT entry.
941 if (parameters
->options().output_is_position_independent())
943 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
944 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
945 rel_dyn
->add_local_relative(
946 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
947 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
953 // These are relocations which should only be seen by the
954 // dynamic linker, and should never be seen here.
955 case elfcpp::R_386_COPY
:
956 case elfcpp::R_386_GLOB_DAT
:
957 case elfcpp::R_386_JUMP_SLOT
:
958 case elfcpp::R_386_RELATIVE
:
959 case elfcpp::R_386_TLS_TPOFF
:
960 case elfcpp::R_386_TLS_DTPMOD32
:
961 case elfcpp::R_386_TLS_DTPOFF32
:
962 case elfcpp::R_386_TLS_TPOFF32
:
963 case elfcpp::R_386_TLS_DESC
:
964 gold_error(_("%s: unexpected reloc %u in object file"),
965 object
->name().c_str(), r_type
);
968 // These are initial TLS relocs, which are expected when
970 case elfcpp::R_386_TLS_GD
: // Global-dynamic
971 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
972 case elfcpp::R_386_TLS_DESC_CALL
:
973 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
974 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
975 case elfcpp::R_386_TLS_IE
: // Initial-exec
976 case elfcpp::R_386_TLS_IE_32
:
977 case elfcpp::R_386_TLS_GOTIE
:
978 case elfcpp::R_386_TLS_LE
: // Local-exec
979 case elfcpp::R_386_TLS_LE_32
:
981 bool output_is_shared
= parameters
->options().shared();
982 const tls::Tls_optimization optimized_type
983 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
986 case elfcpp::R_386_TLS_GD
: // Global-dynamic
987 if (optimized_type
== tls::TLSOPT_NONE
)
989 // Create a pair of GOT entries for the module index and
990 // dtv-relative offset.
991 Output_data_got
<32, false>* got
992 = target
->got_section(symtab
, layout
);
993 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
994 unsigned int shndx
= lsym
.get_st_shndx();
996 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
998 object
->error(_("local symbol %u has bad shndx %u"),
1001 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1003 target
->rel_dyn_section(layout
),
1004 elfcpp::R_386_TLS_DTPMOD32
, 0);
1006 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1007 unsupported_reloc_local(object
, r_type
);
1010 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1011 target
->define_tls_base_symbol(symtab
, layout
);
1012 if (optimized_type
== tls::TLSOPT_NONE
)
1014 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1015 Output_data_got
<32, false>* got
1016 = target
->got_section(symtab
, layout
);
1017 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1018 unsigned int shndx
= lsym
.get_st_shndx();
1020 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1022 object
->error(_("local symbol %u has bad shndx %u"),
1025 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1027 target
->rel_dyn_section(layout
),
1028 elfcpp::R_386_TLS_DESC
, 0);
1030 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1031 unsupported_reloc_local(object
, r_type
);
1034 case elfcpp::R_386_TLS_DESC_CALL
:
1037 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1038 if (optimized_type
== tls::TLSOPT_NONE
)
1040 // Create a GOT entry for the module index.
1041 target
->got_mod_index_entry(symtab
, layout
, object
);
1043 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1044 unsupported_reloc_local(object
, r_type
);
1047 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1050 case elfcpp::R_386_TLS_IE
: // Initial-exec
1051 case elfcpp::R_386_TLS_IE_32
:
1052 case elfcpp::R_386_TLS_GOTIE
:
1053 layout
->set_has_static_tls();
1054 if (optimized_type
== tls::TLSOPT_NONE
)
1056 // For the R_386_TLS_IE relocation, we need to create a
1057 // dynamic relocation when building a shared library.
1058 if (r_type
== elfcpp::R_386_TLS_IE
1059 && parameters
->options().shared())
1061 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1063 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1064 rel_dyn
->add_local_relative(object
, r_sym
,
1065 elfcpp::R_386_RELATIVE
,
1066 output_section
, data_shndx
,
1067 reloc
.get_r_offset());
1069 // Create a GOT entry for the tp-relative offset.
1070 Output_data_got
<32, false>* got
1071 = target
->got_section(symtab
, layout
);
1072 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1073 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1074 ? elfcpp::R_386_TLS_TPOFF32
1075 : elfcpp::R_386_TLS_TPOFF
);
1076 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1077 ? GOT_TYPE_TLS_OFFSET
1078 : GOT_TYPE_TLS_NOFFSET
);
1079 got
->add_local_with_rel(object
, r_sym
, got_type
,
1080 target
->rel_dyn_section(layout
),
1083 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1084 unsupported_reloc_local(object
, r_type
);
1087 case elfcpp::R_386_TLS_LE
: // Local-exec
1088 case elfcpp::R_386_TLS_LE_32
:
1089 layout
->set_has_static_tls();
1090 if (output_is_shared
)
1092 // We need to create a dynamic relocation.
1093 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
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_LE_32
1096 ? elfcpp::R_386_TLS_TPOFF32
1097 : elfcpp::R_386_TLS_TPOFF
);
1098 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1099 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1100 data_shndx
, reloc
.get_r_offset());
1110 case elfcpp::R_386_32PLT
:
1111 case elfcpp::R_386_TLS_GD_32
:
1112 case elfcpp::R_386_TLS_GD_PUSH
:
1113 case elfcpp::R_386_TLS_GD_CALL
:
1114 case elfcpp::R_386_TLS_GD_POP
:
1115 case elfcpp::R_386_TLS_LDM_32
:
1116 case elfcpp::R_386_TLS_LDM_PUSH
:
1117 case elfcpp::R_386_TLS_LDM_CALL
:
1118 case elfcpp::R_386_TLS_LDM_POP
:
1119 case elfcpp::R_386_USED_BY_INTEL_200
:
1121 unsupported_reloc_local(object
, r_type
);
1126 // Report an unsupported relocation against a global symbol.
1129 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1130 unsigned int r_type
,
1133 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1134 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1137 // Scan a relocation for a global symbol.
1140 Target_i386::Scan::global(const General_options
&,
1141 Symbol_table
* symtab
,
1143 Target_i386
* target
,
1144 Sized_relobj
<32, false>* object
,
1145 unsigned int data_shndx
,
1146 Output_section
* output_section
,
1147 const elfcpp::Rel
<32, false>& reloc
,
1148 unsigned int r_type
,
1153 case elfcpp::R_386_NONE
:
1154 case elfcpp::R_386_GNU_VTINHERIT
:
1155 case elfcpp::R_386_GNU_VTENTRY
:
1158 case elfcpp::R_386_32
:
1159 case elfcpp::R_386_16
:
1160 case elfcpp::R_386_8
:
1162 // Make a PLT entry if necessary.
1163 if (gsym
->needs_plt_entry())
1165 target
->make_plt_entry(symtab
, layout
, gsym
);
1166 // Since this is not a PC-relative relocation, we may be
1167 // taking the address of a function. In that case we need to
1168 // set the entry in the dynamic symbol table to the address of
1170 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1171 gsym
->set_needs_dynsym_value();
1173 // Make a dynamic relocation if necessary.
1174 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1176 if (target
->may_need_copy_reloc(gsym
))
1178 target
->copy_reloc(symtab
, layout
, object
,
1179 data_shndx
, output_section
, gsym
, reloc
);
1181 else if (r_type
== elfcpp::R_386_32
1182 && gsym
->can_use_relative_reloc(false))
1184 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1185 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1186 output_section
, object
,
1187 data_shndx
, reloc
.get_r_offset());
1191 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1192 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1193 data_shndx
, reloc
.get_r_offset());
1199 case elfcpp::R_386_PC32
:
1200 case elfcpp::R_386_PC16
:
1201 case elfcpp::R_386_PC8
:
1203 // Make a PLT entry if necessary.
1204 if (gsym
->needs_plt_entry())
1206 // These relocations are used for function calls only in
1207 // non-PIC code. For a 32-bit relocation in a shared library,
1208 // we'll need a text relocation anyway, so we can skip the
1209 // PLT entry and let the dynamic linker bind the call directly
1210 // to the target. For smaller relocations, we should use a
1211 // PLT entry to ensure that the call can reach.
1212 if (!parameters
->options().shared()
1213 || r_type
!= elfcpp::R_386_PC32
)
1214 target
->make_plt_entry(symtab
, layout
, gsym
);
1216 // Make a dynamic relocation if necessary.
1217 int flags
= Symbol::NON_PIC_REF
;
1218 if (gsym
->type() == elfcpp::STT_FUNC
)
1219 flags
|= Symbol::FUNCTION_CALL
;
1220 if (gsym
->needs_dynamic_reloc(flags
))
1222 if (target
->may_need_copy_reloc(gsym
))
1224 target
->copy_reloc(symtab
, layout
, object
,
1225 data_shndx
, output_section
, gsym
, reloc
);
1229 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1230 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1231 data_shndx
, reloc
.get_r_offset());
1237 case elfcpp::R_386_GOT32
:
1239 // The symbol requires a GOT entry.
1240 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1241 if (gsym
->final_value_is_known())
1242 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1245 // If this symbol is not fully resolved, we need to add a
1246 // GOT entry with a dynamic relocation.
1247 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1248 if (gsym
->is_from_dynobj()
1249 || gsym
->is_undefined()
1250 || gsym
->is_preemptible())
1251 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1252 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1255 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1256 rel_dyn
->add_global_relative(
1257 gsym
, elfcpp::R_386_RELATIVE
, got
,
1258 gsym
->got_offset(GOT_TYPE_STANDARD
));
1264 case elfcpp::R_386_PLT32
:
1265 // If the symbol is fully resolved, this is just a PC32 reloc.
1266 // Otherwise we need a PLT entry.
1267 if (gsym
->final_value_is_known())
1269 // If building a shared library, we can also skip the PLT entry
1270 // if the symbol is defined in the output file and is protected
1272 if (gsym
->is_defined()
1273 && !gsym
->is_from_dynobj()
1274 && !gsym
->is_preemptible())
1276 target
->make_plt_entry(symtab
, layout
, gsym
);
1279 case elfcpp::R_386_GOTOFF
:
1280 case elfcpp::R_386_GOTPC
:
1281 // We need a GOT section.
1282 target
->got_section(symtab
, layout
);
1285 // These are relocations which should only be seen by the
1286 // dynamic linker, and should never be seen here.
1287 case elfcpp::R_386_COPY
:
1288 case elfcpp::R_386_GLOB_DAT
:
1289 case elfcpp::R_386_JUMP_SLOT
:
1290 case elfcpp::R_386_RELATIVE
:
1291 case elfcpp::R_386_TLS_TPOFF
:
1292 case elfcpp::R_386_TLS_DTPMOD32
:
1293 case elfcpp::R_386_TLS_DTPOFF32
:
1294 case elfcpp::R_386_TLS_TPOFF32
:
1295 case elfcpp::R_386_TLS_DESC
:
1296 gold_error(_("%s: unexpected reloc %u in object file"),
1297 object
->name().c_str(), r_type
);
1300 // These are initial tls relocs, which are expected when
1302 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1303 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1304 case elfcpp::R_386_TLS_DESC_CALL
:
1305 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1306 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1307 case elfcpp::R_386_TLS_IE
: // Initial-exec
1308 case elfcpp::R_386_TLS_IE_32
:
1309 case elfcpp::R_386_TLS_GOTIE
:
1310 case elfcpp::R_386_TLS_LE
: // Local-exec
1311 case elfcpp::R_386_TLS_LE_32
:
1313 const bool is_final
= gsym
->final_value_is_known();
1314 const tls::Tls_optimization optimized_type
1315 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1318 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1319 if (optimized_type
== tls::TLSOPT_NONE
)
1321 // Create a pair of GOT entries for the module index and
1322 // dtv-relative offset.
1323 Output_data_got
<32, false>* got
1324 = target
->got_section(symtab
, layout
);
1325 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1326 target
->rel_dyn_section(layout
),
1327 elfcpp::R_386_TLS_DTPMOD32
,
1328 elfcpp::R_386_TLS_DTPOFF32
);
1330 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1332 // Create a GOT entry for the tp-relative offset.
1333 Output_data_got
<32, false>* got
1334 = target
->got_section(symtab
, layout
);
1335 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1336 target
->rel_dyn_section(layout
),
1337 elfcpp::R_386_TLS_TPOFF
);
1339 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1340 unsupported_reloc_global(object
, r_type
, gsym
);
1343 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1344 target
->define_tls_base_symbol(symtab
, layout
);
1345 if (optimized_type
== tls::TLSOPT_NONE
)
1347 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1348 Output_data_got
<32, false>* got
1349 = target
->got_section(symtab
, layout
);
1350 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1351 target
->rel_dyn_section(layout
),
1352 elfcpp::R_386_TLS_DESC
, 0);
1354 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1356 // Create a GOT entry for the tp-relative offset.
1357 Output_data_got
<32, false>* got
1358 = target
->got_section(symtab
, layout
);
1359 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1360 target
->rel_dyn_section(layout
),
1361 elfcpp::R_386_TLS_TPOFF
);
1363 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1364 unsupported_reloc_global(object
, r_type
, gsym
);
1367 case elfcpp::R_386_TLS_DESC_CALL
:
1370 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1371 if (optimized_type
== tls::TLSOPT_NONE
)
1373 // Create a GOT entry for the module index.
1374 target
->got_mod_index_entry(symtab
, layout
, object
);
1376 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1377 unsupported_reloc_global(object
, r_type
, gsym
);
1380 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1383 case elfcpp::R_386_TLS_IE
: // Initial-exec
1384 case elfcpp::R_386_TLS_IE_32
:
1385 case elfcpp::R_386_TLS_GOTIE
:
1386 layout
->set_has_static_tls();
1387 if (optimized_type
== tls::TLSOPT_NONE
)
1389 // For the R_386_TLS_IE relocation, we need to create a
1390 // dynamic relocation when building a shared library.
1391 if (r_type
== elfcpp::R_386_TLS_IE
1392 && parameters
->options().shared())
1394 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1395 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1396 output_section
, object
,
1398 reloc
.get_r_offset());
1400 // Create a GOT entry for the tp-relative offset.
1401 Output_data_got
<32, false>* got
1402 = target
->got_section(symtab
, layout
);
1403 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1404 ? elfcpp::R_386_TLS_TPOFF32
1405 : elfcpp::R_386_TLS_TPOFF
);
1406 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1407 ? GOT_TYPE_TLS_OFFSET
1408 : GOT_TYPE_TLS_NOFFSET
);
1409 got
->add_global_with_rel(gsym
, got_type
,
1410 target
->rel_dyn_section(layout
),
1413 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1414 unsupported_reloc_global(object
, r_type
, gsym
);
1417 case elfcpp::R_386_TLS_LE
: // Local-exec
1418 case elfcpp::R_386_TLS_LE_32
:
1419 layout
->set_has_static_tls();
1420 if (parameters
->options().shared())
1422 // We need to create a dynamic relocation.
1423 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1424 ? elfcpp::R_386_TLS_TPOFF32
1425 : elfcpp::R_386_TLS_TPOFF
);
1426 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1427 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1428 data_shndx
, reloc
.get_r_offset());
1438 case elfcpp::R_386_32PLT
:
1439 case elfcpp::R_386_TLS_GD_32
:
1440 case elfcpp::R_386_TLS_GD_PUSH
:
1441 case elfcpp::R_386_TLS_GD_CALL
:
1442 case elfcpp::R_386_TLS_GD_POP
:
1443 case elfcpp::R_386_TLS_LDM_32
:
1444 case elfcpp::R_386_TLS_LDM_PUSH
:
1445 case elfcpp::R_386_TLS_LDM_CALL
:
1446 case elfcpp::R_386_TLS_LDM_POP
:
1447 case elfcpp::R_386_USED_BY_INTEL_200
:
1449 unsupported_reloc_global(object
, r_type
, gsym
);
1454 // Scan relocations for a section.
1457 Target_i386::scan_relocs(const General_options
& options
,
1458 Symbol_table
* symtab
,
1460 Sized_relobj
<32, false>* object
,
1461 unsigned int data_shndx
,
1462 unsigned int sh_type
,
1463 const unsigned char* prelocs
,
1465 Output_section
* output_section
,
1466 bool needs_special_offset_handling
,
1467 size_t local_symbol_count
,
1468 const unsigned char* plocal_symbols
)
1470 if (sh_type
== elfcpp::SHT_RELA
)
1472 gold_error(_("%s: unsupported RELA reloc section"),
1473 object
->name().c_str());
1477 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1488 needs_special_offset_handling
,
1493 // Finalize the sections.
1496 Target_i386::do_finalize_sections(Layout
* layout
)
1498 // Fill in some more dynamic tags.
1499 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1502 if (this->got_plt_
!= NULL
)
1503 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1505 if (this->plt_
!= NULL
)
1507 const Output_data
* od
= this->plt_
->rel_plt();
1508 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1509 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1510 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1513 if (this->rel_dyn_
!= NULL
)
1515 const Output_data
* od
= this->rel_dyn_
;
1516 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1517 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1518 odyn
->add_constant(elfcpp::DT_RELENT
,
1519 elfcpp::Elf_sizes
<32>::rel_size
);
1522 if (!parameters
->options().shared())
1524 // The value of the DT_DEBUG tag is filled in by the dynamic
1525 // linker at run time, and used by the debugger.
1526 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1530 // Emit any relocs we saved in an attempt to avoid generating COPY
1532 if (this->copy_relocs_
.any_saved_relocs())
1533 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1536 // Return whether a direct absolute static relocation needs to be applied.
1537 // In cases where Scan::local() or Scan::global() has created
1538 // a dynamic relocation other than R_386_RELATIVE, the addend
1539 // of the relocation is carried in the data, and we must not
1540 // apply the static relocation.
1543 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1547 // For local symbols, we will have created a non-RELATIVE dynamic
1548 // relocation only if (a) the output is position independent,
1549 // (b) the relocation is absolute (not pc- or segment-relative), and
1550 // (c) the relocation is not 32 bits wide.
1552 return !(parameters
->options().output_is_position_independent()
1553 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1556 // For global symbols, we use the same helper routines used in the
1557 // scan pass. If we did not create a dynamic relocation, or if we
1558 // created a RELATIVE dynamic relocation, we should apply the static
1560 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1561 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1562 && gsym
->can_use_relative_reloc(ref_flags
1563 & Symbol::FUNCTION_CALL
);
1564 return !has_dyn
|| is_rel
;
1567 // Perform a relocation.
1570 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1571 Target_i386
* target
,
1573 const elfcpp::Rel
<32, false>& rel
,
1574 unsigned int r_type
,
1575 const Sized_symbol
<32>* gsym
,
1576 const Symbol_value
<32>* psymval
,
1577 unsigned char* view
,
1578 elfcpp::Elf_types
<32>::Elf_Addr address
,
1579 section_size_type view_size
)
1581 if (this->skip_call_tls_get_addr_
)
1583 if (r_type
!= elfcpp::R_386_PLT32
1585 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1586 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1587 _("missing expected TLS relocation"));
1590 this->skip_call_tls_get_addr_
= false;
1595 // Pick the value to use for symbols defined in shared objects.
1596 Symbol_value
<32> symval
;
1597 bool is_nonpic
= (r_type
== elfcpp::R_386_PC8
1598 || r_type
== elfcpp::R_386_PC16
1599 || r_type
== elfcpp::R_386_PC32
);
1601 && (gsym
->is_from_dynobj()
1602 || (parameters
->options().shared()
1603 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1604 && gsym
->has_plt_offset()
1605 && (!is_nonpic
|| !parameters
->options().shared()))
1607 symval
.set_output_value(target
->plt_section()->address()
1608 + gsym
->plt_offset());
1612 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1614 // Get the GOT offset if needed.
1615 // The GOT pointer points to the end of the GOT section.
1616 // We need to subtract the size of the GOT section to get
1617 // the actual offset to use in the relocation.
1618 bool have_got_offset
= false;
1619 unsigned int got_offset
= 0;
1622 case elfcpp::R_386_GOT32
:
1625 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1626 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1627 - target
->got_size());
1631 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1632 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1633 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1634 - target
->got_size());
1636 have_got_offset
= true;
1645 case elfcpp::R_386_NONE
:
1646 case elfcpp::R_386_GNU_VTINHERIT
:
1647 case elfcpp::R_386_GNU_VTENTRY
:
1650 case elfcpp::R_386_32
:
1651 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true))
1652 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1655 case elfcpp::R_386_PC32
:
1657 int ref_flags
= Symbol::NON_PIC_REF
;
1658 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1659 ref_flags
|= Symbol::FUNCTION_CALL
;
1660 if (should_apply_static_reloc(gsym
, ref_flags
, true))
1661 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1665 case elfcpp::R_386_16
:
1666 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1667 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1670 case elfcpp::R_386_PC16
:
1672 int ref_flags
= Symbol::NON_PIC_REF
;
1673 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1674 ref_flags
|= Symbol::FUNCTION_CALL
;
1675 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1676 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1680 case elfcpp::R_386_8
:
1681 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1682 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1685 case elfcpp::R_386_PC8
:
1687 int ref_flags
= Symbol::NON_PIC_REF
;
1688 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1689 ref_flags
|= Symbol::FUNCTION_CALL
;
1690 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1691 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1695 case elfcpp::R_386_PLT32
:
1696 gold_assert(gsym
== NULL
1697 || gsym
->has_plt_offset()
1698 || gsym
->final_value_is_known()
1699 || (gsym
->is_defined()
1700 && !gsym
->is_from_dynobj()
1701 && !gsym
->is_preemptible()));
1702 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1705 case elfcpp::R_386_GOT32
:
1706 gold_assert(have_got_offset
);
1707 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1710 case elfcpp::R_386_GOTOFF
:
1712 elfcpp::Elf_types
<32>::Elf_Addr value
;
1713 value
= (psymval
->value(object
, 0)
1714 - target
->got_plt_section()->address());
1715 Relocate_functions
<32, false>::rel32(view
, value
);
1719 case elfcpp::R_386_GOTPC
:
1721 elfcpp::Elf_types
<32>::Elf_Addr value
;
1722 value
= target
->got_plt_section()->address();
1723 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1727 case elfcpp::R_386_COPY
:
1728 case elfcpp::R_386_GLOB_DAT
:
1729 case elfcpp::R_386_JUMP_SLOT
:
1730 case elfcpp::R_386_RELATIVE
:
1731 // These are outstanding tls relocs, which are unexpected when
1733 case elfcpp::R_386_TLS_TPOFF
:
1734 case elfcpp::R_386_TLS_DTPMOD32
:
1735 case elfcpp::R_386_TLS_DTPOFF32
:
1736 case elfcpp::R_386_TLS_TPOFF32
:
1737 case elfcpp::R_386_TLS_DESC
:
1738 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1739 _("unexpected reloc %u in object file"),
1743 // These are initial tls relocs, which are expected when
1745 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1746 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1747 case elfcpp::R_386_TLS_DESC_CALL
:
1748 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1749 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1750 case elfcpp::R_386_TLS_IE
: // Initial-exec
1751 case elfcpp::R_386_TLS_IE_32
:
1752 case elfcpp::R_386_TLS_GOTIE
:
1753 case elfcpp::R_386_TLS_LE
: // Local-exec
1754 case elfcpp::R_386_TLS_LE_32
:
1755 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1756 view
, address
, view_size
);
1759 case elfcpp::R_386_32PLT
:
1760 case elfcpp::R_386_TLS_GD_32
:
1761 case elfcpp::R_386_TLS_GD_PUSH
:
1762 case elfcpp::R_386_TLS_GD_CALL
:
1763 case elfcpp::R_386_TLS_GD_POP
:
1764 case elfcpp::R_386_TLS_LDM_32
:
1765 case elfcpp::R_386_TLS_LDM_PUSH
:
1766 case elfcpp::R_386_TLS_LDM_CALL
:
1767 case elfcpp::R_386_TLS_LDM_POP
:
1768 case elfcpp::R_386_USED_BY_INTEL_200
:
1770 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1771 _("unsupported reloc %u"),
1779 // Perform a TLS relocation.
1782 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1783 Target_i386
* target
,
1785 const elfcpp::Rel
<32, false>& rel
,
1786 unsigned int r_type
,
1787 const Sized_symbol
<32>* gsym
,
1788 const Symbol_value
<32>* psymval
,
1789 unsigned char* view
,
1790 elfcpp::Elf_types
<32>::Elf_Addr
,
1791 section_size_type view_size
)
1793 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1795 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1797 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1799 const bool is_final
=
1801 ? !parameters
->options().output_is_position_independent()
1802 : gsym
->final_value_is_known());
1803 const tls::Tls_optimization optimized_type
1804 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1807 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1808 if (optimized_type
== tls::TLSOPT_TO_LE
)
1810 gold_assert(tls_segment
!= NULL
);
1811 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1812 rel
, r_type
, value
, view
,
1818 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1819 ? GOT_TYPE_TLS_NOFFSET
1820 : GOT_TYPE_TLS_PAIR
);
1821 unsigned int got_offset
;
1824 gold_assert(gsym
->has_got_offset(got_type
));
1825 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1829 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1830 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1831 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1832 - target
->got_size());
1834 if (optimized_type
== tls::TLSOPT_TO_IE
)
1836 gold_assert(tls_segment
!= NULL
);
1837 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1838 got_offset
, view
, view_size
);
1841 else if (optimized_type
== tls::TLSOPT_NONE
)
1843 // Relocate the field with the offset of the pair of GOT
1845 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1849 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1850 _("unsupported reloc %u"),
1854 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1855 case elfcpp::R_386_TLS_DESC_CALL
:
1856 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1857 if (optimized_type
== tls::TLSOPT_TO_LE
)
1859 gold_assert(tls_segment
!= NULL
);
1860 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1861 rel
, r_type
, value
, view
,
1867 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1868 ? GOT_TYPE_TLS_NOFFSET
1869 : GOT_TYPE_TLS_DESC
);
1870 unsigned int got_offset
;
1873 gold_assert(gsym
->has_got_offset(got_type
));
1874 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1878 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1879 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1880 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1881 - target
->got_size());
1883 if (optimized_type
== tls::TLSOPT_TO_IE
)
1885 gold_assert(tls_segment
!= NULL
);
1886 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1887 got_offset
, view
, view_size
);
1890 else if (optimized_type
== tls::TLSOPT_NONE
)
1892 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1894 // Relocate the field with the offset of the pair of GOT
1896 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1901 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1902 _("unsupported reloc %u"),
1906 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1907 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1909 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1910 _("both SUN and GNU model "
1911 "TLS relocations"));
1914 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1915 if (optimized_type
== tls::TLSOPT_TO_LE
)
1917 gold_assert(tls_segment
!= NULL
);
1918 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1919 value
, view
, view_size
);
1922 else if (optimized_type
== tls::TLSOPT_NONE
)
1924 // Relocate the field with the offset of the GOT entry for
1925 // the module index.
1926 unsigned int got_offset
;
1927 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1928 - target
->got_size());
1929 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1932 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1933 _("unsupported reloc %u"),
1937 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1938 // This reloc can appear in debugging sections, in which case we
1939 // won't see the TLS_LDM reloc. The local_dynamic_type field
1941 if (optimized_type
== tls::TLSOPT_TO_LE
1942 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
1944 gold_assert(tls_segment
!= NULL
);
1945 value
-= tls_segment
->memsz();
1947 Relocate_functions
<32, false>::rel32(view
, value
);
1950 case elfcpp::R_386_TLS_IE
: // Initial-exec
1951 case elfcpp::R_386_TLS_GOTIE
:
1952 case elfcpp::R_386_TLS_IE_32
:
1953 if (optimized_type
== tls::TLSOPT_TO_LE
)
1955 gold_assert(tls_segment
!= NULL
);
1956 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1957 rel
, r_type
, value
, view
,
1961 else if (optimized_type
== tls::TLSOPT_NONE
)
1963 // Relocate the field with the offset of the GOT entry for
1964 // the tp-relative offset of the symbol.
1965 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1966 ? GOT_TYPE_TLS_OFFSET
1967 : GOT_TYPE_TLS_NOFFSET
);
1968 unsigned int got_offset
;
1971 gold_assert(gsym
->has_got_offset(got_type
));
1972 got_offset
= gsym
->got_offset(got_type
);
1976 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1977 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1978 got_offset
= object
->local_got_offset(r_sym
, got_type
);
1980 // For the R_386_TLS_IE relocation, we need to apply the
1981 // absolute address of the GOT entry.
1982 if (r_type
== elfcpp::R_386_TLS_IE
)
1983 got_offset
+= target
->got_plt_section()->address();
1984 // All GOT offsets are relative to the end of the GOT.
1985 got_offset
-= target
->got_size();
1986 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1989 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1990 _("unsupported reloc %u"),
1994 case elfcpp::R_386_TLS_LE
: // Local-exec
1995 // If we're creating a shared library, a dynamic relocation will
1996 // have been created for this location, so do not apply it now.
1997 if (!parameters
->options().shared())
1999 gold_assert(tls_segment
!= NULL
);
2000 value
-= tls_segment
->memsz();
2001 Relocate_functions
<32, false>::rel32(view
, value
);
2005 case elfcpp::R_386_TLS_LE_32
:
2006 // If we're creating a shared library, a dynamic relocation will
2007 // have been created for this location, so do not apply it now.
2008 if (!parameters
->options().shared())
2010 gold_assert(tls_segment
!= NULL
);
2011 value
= tls_segment
->memsz() - value
;
2012 Relocate_functions
<32, false>::rel32(view
, value
);
2018 // Do a relocation in which we convert a TLS General-Dynamic to a
2022 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2024 Output_segment
* tls_segment
,
2025 const elfcpp::Rel
<32, false>& rel
,
2027 elfcpp::Elf_types
<32>::Elf_Addr value
,
2028 unsigned char* view
,
2029 section_size_type view_size
)
2031 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2032 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2033 // leal foo(%reg),%eax; call ___tls_get_addr
2034 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2036 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2037 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2039 unsigned char op1
= view
[-1];
2040 unsigned char op2
= view
[-2];
2042 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2043 op2
== 0x8d || op2
== 0x04);
2044 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2050 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2051 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2052 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2053 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2054 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2058 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2059 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2060 if (rel
.get_r_offset() + 9 < view_size
2063 // There is a trailing nop. Use the size byte subl.
2064 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2069 // Use the five byte subl.
2070 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2074 value
= tls_segment
->memsz() - value
;
2075 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2077 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2079 this->skip_call_tls_get_addr_
= true;
2082 // Do a relocation in which we convert a TLS General-Dynamic to an
2086 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2089 const elfcpp::Rel
<32, false>& rel
,
2091 elfcpp::Elf_types
<32>::Elf_Addr value
,
2092 unsigned char* view
,
2093 section_size_type view_size
)
2095 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2096 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%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);
2110 // FIXME: For now, support only the first (SIB) form.
2111 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2115 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2116 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2117 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2118 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2119 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2123 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2124 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2125 if (rel
.get_r_offset() + 9 < view_size
2128 // FIXME: This is not the right instruction sequence.
2129 // There is a trailing nop. Use the size byte subl.
2130 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2135 // FIXME: This is not the right instruction sequence.
2136 // Use the five byte subl.
2137 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2141 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2143 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2145 this->skip_call_tls_get_addr_
= true;
2148 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2149 // General-Dynamic to a Local-Exec.
2152 Target_i386::Relocate::tls_desc_gd_to_le(
2153 const Relocate_info
<32, false>* relinfo
,
2155 Output_segment
* tls_segment
,
2156 const elfcpp::Rel
<32, false>& rel
,
2157 unsigned int r_type
,
2158 elfcpp::Elf_types
<32>::Elf_Addr value
,
2159 unsigned char* view
,
2160 section_size_type view_size
)
2162 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2164 // leal foo@TLSDESC(%ebx), %eax
2165 // ==> leal foo@NTPOFF, %eax
2166 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2167 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2168 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2169 view
[-2] == 0x8d && view
[-1] == 0x83);
2171 value
-= tls_segment
->memsz();
2172 Relocate_functions
<32, false>::rel32(view
, value
);
2176 // call *foo@TLSCALL(%eax)
2178 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2179 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2180 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2181 view
[0] == 0xff && view
[1] == 0x10);
2187 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2188 // General-Dynamic to an Initial-Exec.
2191 Target_i386::Relocate::tls_desc_gd_to_ie(
2192 const Relocate_info
<32, false>* relinfo
,
2195 const elfcpp::Rel
<32, false>& rel
,
2196 unsigned int r_type
,
2197 elfcpp::Elf_types
<32>::Elf_Addr value
,
2198 unsigned char* view
,
2199 section_size_type view_size
)
2201 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2203 // leal foo@TLSDESC(%ebx), %eax
2204 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2205 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2206 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2207 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2208 view
[-2] == 0x8d && view
[-1] == 0x83);
2210 Relocate_functions
<32, false>::rel32(view
, value
);
2214 // call *foo@TLSCALL(%eax)
2216 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2217 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2218 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2219 view
[0] == 0xff && view
[1] == 0x10);
2225 // Do a relocation in which we convert a TLS Local-Dynamic to a
2229 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2232 const elfcpp::Rel
<32, false>& rel
,
2234 elfcpp::Elf_types
<32>::Elf_Addr
,
2235 unsigned char* view
,
2236 section_size_type view_size
)
2238 // leal foo(%reg), %eax; call ___tls_get_addr
2239 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2241 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2242 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2244 // FIXME: Does this test really always pass?
2245 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2246 view
[-2] == 0x8d && view
[-1] == 0x83);
2248 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2250 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2252 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2254 this->skip_call_tls_get_addr_
= true;
2257 // Do a relocation in which we convert a TLS Initial-Exec to a
2261 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2263 Output_segment
* tls_segment
,
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 // We have to actually change the instructions, which means that we
2271 // need to examine the opcodes to figure out which instruction we
2273 if (r_type
== elfcpp::R_386_TLS_IE
)
2275 // movl %gs:XX,%eax ==> movl $YY,%eax
2276 // movl %gs:XX,%reg ==> movl $YY,%reg
2277 // addl %gs:XX,%reg ==> addl $YY,%reg
2278 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2279 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2281 unsigned char op1
= view
[-1];
2284 // movl XX,%eax ==> movl $YY,%eax
2289 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2291 unsigned char op2
= view
[-2];
2294 // movl XX,%reg ==> movl $YY,%reg
2295 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2296 (op1
& 0xc7) == 0x05);
2298 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2300 else if (op2
== 0x03)
2302 // addl XX,%reg ==> addl $YY,%reg
2303 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2304 (op1
& 0xc7) == 0x05);
2306 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2309 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2314 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2315 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2316 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2317 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2318 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2320 unsigned char op1
= view
[-1];
2321 unsigned char op2
= view
[-2];
2322 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2323 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2326 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2328 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2330 else if (op2
== 0x2b)
2332 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2334 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2336 else if (op2
== 0x03)
2338 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2340 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2343 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2346 value
= tls_segment
->memsz() - value
;
2347 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2350 Relocate_functions
<32, false>::rel32(view
, value
);
2353 // Relocate section data.
2356 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2357 unsigned int sh_type
,
2358 const unsigned char* prelocs
,
2360 Output_section
* output_section
,
2361 bool needs_special_offset_handling
,
2362 unsigned char* view
,
2363 elfcpp::Elf_types
<32>::Elf_Addr address
,
2364 section_size_type view_size
)
2366 gold_assert(sh_type
== elfcpp::SHT_REL
);
2368 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2369 Target_i386::Relocate
>(
2375 needs_special_offset_handling
,
2381 // Return the size of a relocation while scanning during a relocatable
2385 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2386 unsigned int r_type
,
2391 case elfcpp::R_386_NONE
:
2392 case elfcpp::R_386_GNU_VTINHERIT
:
2393 case elfcpp::R_386_GNU_VTENTRY
:
2394 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2395 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2396 case elfcpp::R_386_TLS_DESC_CALL
:
2397 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2398 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2399 case elfcpp::R_386_TLS_IE
: // Initial-exec
2400 case elfcpp::R_386_TLS_IE_32
:
2401 case elfcpp::R_386_TLS_GOTIE
:
2402 case elfcpp::R_386_TLS_LE
: // Local-exec
2403 case elfcpp::R_386_TLS_LE_32
:
2406 case elfcpp::R_386_32
:
2407 case elfcpp::R_386_PC32
:
2408 case elfcpp::R_386_GOT32
:
2409 case elfcpp::R_386_PLT32
:
2410 case elfcpp::R_386_GOTOFF
:
2411 case elfcpp::R_386_GOTPC
:
2414 case elfcpp::R_386_16
:
2415 case elfcpp::R_386_PC16
:
2418 case elfcpp::R_386_8
:
2419 case elfcpp::R_386_PC8
:
2422 // These are relocations which should only be seen by the
2423 // dynamic linker, and should never be seen here.
2424 case elfcpp::R_386_COPY
:
2425 case elfcpp::R_386_GLOB_DAT
:
2426 case elfcpp::R_386_JUMP_SLOT
:
2427 case elfcpp::R_386_RELATIVE
:
2428 case elfcpp::R_386_TLS_TPOFF
:
2429 case elfcpp::R_386_TLS_DTPMOD32
:
2430 case elfcpp::R_386_TLS_DTPOFF32
:
2431 case elfcpp::R_386_TLS_TPOFF32
:
2432 case elfcpp::R_386_TLS_DESC
:
2433 object
->error(_("unexpected reloc %u in object file"), r_type
);
2436 case elfcpp::R_386_32PLT
:
2437 case elfcpp::R_386_TLS_GD_32
:
2438 case elfcpp::R_386_TLS_GD_PUSH
:
2439 case elfcpp::R_386_TLS_GD_CALL
:
2440 case elfcpp::R_386_TLS_GD_POP
:
2441 case elfcpp::R_386_TLS_LDM_32
:
2442 case elfcpp::R_386_TLS_LDM_PUSH
:
2443 case elfcpp::R_386_TLS_LDM_CALL
:
2444 case elfcpp::R_386_TLS_LDM_POP
:
2445 case elfcpp::R_386_USED_BY_INTEL_200
:
2447 object
->error(_("unsupported reloc %u in object file"), r_type
);
2452 // Scan the relocs during a relocatable link.
2455 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2456 Symbol_table
* symtab
,
2458 Sized_relobj
<32, false>* object
,
2459 unsigned int data_shndx
,
2460 unsigned int sh_type
,
2461 const unsigned char* prelocs
,
2463 Output_section
* output_section
,
2464 bool needs_special_offset_handling
,
2465 size_t local_symbol_count
,
2466 const unsigned char* plocal_symbols
,
2467 Relocatable_relocs
* rr
)
2469 gold_assert(sh_type
== elfcpp::SHT_REL
);
2471 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2472 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2474 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2475 Scan_relocatable_relocs
>(
2484 needs_special_offset_handling
,
2490 // Relocate a section during a relocatable link.
2493 Target_i386::relocate_for_relocatable(
2494 const Relocate_info
<32, false>* relinfo
,
2495 unsigned int sh_type
,
2496 const unsigned char* prelocs
,
2498 Output_section
* output_section
,
2499 off_t offset_in_output_section
,
2500 const Relocatable_relocs
* rr
,
2501 unsigned char* view
,
2502 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2503 section_size_type view_size
,
2504 unsigned char* reloc_view
,
2505 section_size_type reloc_view_size
)
2507 gold_assert(sh_type
== elfcpp::SHT_REL
);
2509 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2514 offset_in_output_section
,
2523 // Return the value to use for a dynamic which requires special
2524 // treatment. This is how we support equality comparisons of function
2525 // pointers across shared library boundaries, as described in the
2526 // processor specific ABI supplement.
2529 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2531 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2532 return this->plt_section()->address() + gsym
->plt_offset();
2535 // Return a string used to fill a code section with nops to take up
2536 // the specified length.
2539 Target_i386::do_code_fill(section_size_type length
) const
2543 // Build a jmp instruction to skip over the bytes.
2544 unsigned char jmp
[5];
2546 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2547 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2548 + std::string(length
- 5, '\0'));
2551 // Nop sequences of various lengths.
2552 const char nop1
[1] = { 0x90 }; // nop
2553 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2554 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2555 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2556 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2557 0x00 }; // leal 0(%esi,1),%esi
2558 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2560 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2562 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2563 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2564 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2565 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2567 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2568 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2570 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2571 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2573 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2574 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2575 0x00, 0x00, 0x00, 0x00 };
2576 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2577 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2578 0x27, 0x00, 0x00, 0x00,
2580 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2581 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2582 0xbc, 0x27, 0x00, 0x00,
2584 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2585 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2586 0x90, 0x90, 0x90, 0x90,
2589 const char* nops
[16] = {
2591 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2592 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2595 return std::string(nops
[length
], length
);
2598 // The selector for i386 object files.
2600 class Target_selector_i386
: public Target_selector
2603 Target_selector_i386()
2604 : Target_selector(elfcpp::EM_386
, 32, false, "elf32-i386")
2608 do_instantiate_target()
2609 { return new Target_i386(); }
2612 Target_selector_i386 target_selector_i386
;
2614 } // End anonymous namespace.