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
,
219 // Do a relocation. Return false if the caller should not issue
220 // any warnings about this relocation.
222 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
223 const elfcpp::Rel
<32, false>&,
224 unsigned int r_type
, const Sized_symbol
<32>*,
225 const Symbol_value
<32>*,
226 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
230 // Do a TLS relocation.
232 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
233 size_t relnum
, const elfcpp::Rel
<32, false>&,
234 unsigned int r_type
, const Sized_symbol
<32>*,
235 const Symbol_value
<32>*,
236 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
239 // Do a TLS General-Dynamic to Initial-Exec transition.
241 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
242 Output_segment
* tls_segment
,
243 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
244 elfcpp::Elf_types
<32>::Elf_Addr value
,
246 section_size_type view_size
);
248 // Do a TLS General-Dynamic to Local-Exec transition.
250 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
251 Output_segment
* tls_segment
,
252 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
253 elfcpp::Elf_types
<32>::Elf_Addr value
,
255 section_size_type view_size
);
257 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
260 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
261 Output_segment
* tls_segment
,
262 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
263 elfcpp::Elf_types
<32>::Elf_Addr value
,
265 section_size_type view_size
);
267 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
270 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
271 Output_segment
* tls_segment
,
272 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
273 elfcpp::Elf_types
<32>::Elf_Addr value
,
275 section_size_type view_size
);
277 // Do a TLS Local-Dynamic to Local-Exec transition.
279 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
280 Output_segment
* tls_segment
,
281 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
282 elfcpp::Elf_types
<32>::Elf_Addr value
,
284 section_size_type view_size
);
286 // Do a TLS Initial-Exec to Local-Exec transition.
288 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
289 Output_segment
* tls_segment
,
290 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
291 elfcpp::Elf_types
<32>::Elf_Addr value
,
293 section_size_type view_size
);
295 // We need to keep track of which type of local dynamic relocation
296 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
297 enum Local_dynamic_type
304 // This is set if we should skip the next reloc, which should be a
305 // PLT32 reloc against ___tls_get_addr.
306 bool skip_call_tls_get_addr_
;
307 // The type of local dynamic relocation we have seen in the section
308 // being relocated, if any.
309 Local_dynamic_type local_dynamic_type_
;
312 // A class which returns the size required for a relocation type,
313 // used while scanning relocs during a relocatable link.
314 class Relocatable_size_for_reloc
318 get_size_for_reloc(unsigned int, Relobj
*);
321 // Adjust TLS relocation type based on the options and whether this
322 // is a local symbol.
323 static tls::Tls_optimization
324 optimize_tls_reloc(bool is_final
, int r_type
);
326 // Get the GOT section, creating it if necessary.
327 Output_data_got
<32, false>*
328 got_section(Symbol_table
*, Layout
*);
330 // Get the GOT PLT section.
332 got_plt_section() const
334 gold_assert(this->got_plt_
!= NULL
);
335 return this->got_plt_
;
338 // Create a PLT entry for a global symbol.
340 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
342 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
344 define_tls_base_symbol(Symbol_table
*, Layout
*);
346 // Create a GOT entry for the TLS module index.
348 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
349 Sized_relobj
<32, false>* object
);
351 // Get the PLT section.
352 const Output_data_plt_i386
*
355 gold_assert(this->plt_
!= NULL
);
359 // Get the dynamic reloc section, creating it if necessary.
361 rel_dyn_section(Layout
*);
363 // Return true if the symbol may need a COPY relocation.
364 // References from an executable object to non-function symbols
365 // defined in a dynamic object may need a COPY relocation.
367 may_need_copy_reloc(Symbol
* gsym
)
369 return (!parameters
->options().shared()
370 && gsym
->is_from_dynobj()
371 && gsym
->type() != elfcpp::STT_FUNC
);
374 // Add a potential copy relocation.
376 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
377 Sized_relobj
<32, false>* object
,
378 unsigned int shndx
, Output_section
* output_section
,
379 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
381 this->copy_relocs_
.copy_reloc(symtab
, layout
,
382 symtab
->get_sized_symbol
<32>(sym
),
383 object
, shndx
, output_section
, reloc
,
384 this->rel_dyn_section(layout
));
387 // Information about this specific target which we pass to the
388 // general Target structure.
389 static const Target::Target_info i386_info
;
391 // The types of GOT entries needed for this platform.
394 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
395 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
396 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
397 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
398 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
402 Output_data_got
<32, false>* got_
;
404 Output_data_plt_i386
* plt_
;
405 // The GOT PLT section.
406 Output_data_space
* got_plt_
;
407 // The dynamic reloc section.
408 Reloc_section
* rel_dyn_
;
409 // Relocs saved to avoid a COPY reloc.
410 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
411 // Space for variables copied with a COPY reloc.
412 Output_data_space
* dynbss_
;
413 // Offset of the GOT entry for the TLS module index.
414 unsigned int got_mod_index_offset_
;
415 // True if the _TLS_MODULE_BASE_ symbol has been defined.
416 bool tls_base_symbol_defined_
;
419 const Target::Target_info
Target_i386::i386_info
=
422 false, // is_big_endian
423 elfcpp::EM_386
, // machine_code
424 false, // has_make_symbol
425 false, // has_resolve
426 true, // has_code_fill
427 true, // is_default_stack_executable
429 "/usr/lib/libc.so.1", // dynamic_linker
430 0x08048000, // default_text_segment_address
431 0x1000, // abi_pagesize (overridable by -z max-page-size)
432 0x1000 // common_pagesize (overridable by -z common-page-size)
435 // Get the GOT section, creating it if necessary.
437 Output_data_got
<32, false>*
438 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
440 if (this->got_
== NULL
)
442 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
444 this->got_
= new Output_data_got
<32, false>();
447 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
449 | elfcpp::SHF_WRITE
),
453 // The old GNU linker creates a .got.plt section. We just
454 // create another set of data in the .got section. Note that we
455 // always create a PLT if we create a GOT, although the PLT
457 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
458 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
460 | elfcpp::SHF_WRITE
),
464 // The first three entries are reserved.
465 this->got_plt_
->set_current_data_size(3 * 4);
467 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
468 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
470 0, 0, elfcpp::STT_OBJECT
,
472 elfcpp::STV_HIDDEN
, 0,
479 // Get the dynamic reloc section, creating it if necessary.
481 Target_i386::Reloc_section
*
482 Target_i386::rel_dyn_section(Layout
* layout
)
484 if (this->rel_dyn_
== NULL
)
486 gold_assert(layout
!= NULL
);
487 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
488 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
489 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
491 return this->rel_dyn_
;
494 // A class to handle the PLT data.
496 class Output_data_plt_i386
: public Output_section_data
499 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
501 Output_data_plt_i386(Layout
*, Output_data_space
*);
503 // Add an entry to the PLT.
505 add_entry(Symbol
* gsym
);
507 // Return the .rel.plt section data.
510 { return this->rel_
; }
514 do_adjust_output_section(Output_section
* os
);
516 // Write to a map file.
518 do_print_to_mapfile(Mapfile
* mapfile
) const
519 { mapfile
->print_output_data(this, _("** PLT")); }
522 // The size of an entry in the PLT.
523 static const int plt_entry_size
= 16;
525 // The first entry in the PLT for an executable.
526 static unsigned char exec_first_plt_entry
[plt_entry_size
];
528 // The first entry in the PLT for a shared object.
529 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
531 // Other entries in the PLT for an executable.
532 static unsigned char exec_plt_entry
[plt_entry_size
];
534 // Other entries in the PLT for a shared object.
535 static unsigned char dyn_plt_entry
[plt_entry_size
];
537 // Set the final size.
539 set_final_data_size()
540 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
542 // Write out the PLT data.
544 do_write(Output_file
*);
546 // The reloc section.
548 // The .got.plt section.
549 Output_data_space
* got_plt_
;
550 // The number of PLT entries.
554 // Create the PLT section. The ordinary .got section is an argument,
555 // since we need to refer to the start. We also create our own .got
556 // section just for PLT entries.
558 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
559 Output_data_space
* got_plt
)
560 : Output_section_data(4), got_plt_(got_plt
), count_(0)
562 this->rel_
= new Reloc_section(false);
563 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
564 elfcpp::SHF_ALLOC
, this->rel_
);
568 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
570 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
571 // linker, and so do we.
575 // Add an entry to the PLT.
578 Output_data_plt_i386::add_entry(Symbol
* gsym
)
580 gold_assert(!gsym
->has_plt_offset());
582 // Note that when setting the PLT offset we skip the initial
583 // reserved PLT entry.
584 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
588 section_offset_type got_offset
= this->got_plt_
->current_data_size();
590 // Every PLT entry needs a GOT entry which points back to the PLT
591 // entry (this will be changed by the dynamic linker, normally
592 // lazily when the function is called).
593 this->got_plt_
->set_current_data_size(got_offset
+ 4);
595 // Every PLT entry needs a reloc.
596 gsym
->set_needs_dynsym_entry();
597 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
600 // Note that we don't need to save the symbol. The contents of the
601 // PLT are independent of which symbols are used. The symbols only
602 // appear in the relocations.
605 // The first entry in the PLT for an executable.
607 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
609 0xff, 0x35, // pushl contents of memory address
610 0, 0, 0, 0, // replaced with address of .got + 4
611 0xff, 0x25, // jmp indirect
612 0, 0, 0, 0, // replaced with address of .got + 8
616 // The first entry in the PLT for a shared object.
618 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
620 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
621 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
625 // Subsequent entries in the PLT for an executable.
627 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
629 0xff, 0x25, // jmp indirect
630 0, 0, 0, 0, // replaced with address of symbol in .got
631 0x68, // pushl immediate
632 0, 0, 0, 0, // replaced with offset into relocation table
633 0xe9, // jmp relative
634 0, 0, 0, 0 // replaced with offset to start of .plt
637 // Subsequent entries in the PLT for a shared object.
639 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
641 0xff, 0xa3, // jmp *offset(%ebx)
642 0, 0, 0, 0, // replaced with offset of symbol in .got
643 0x68, // pushl immediate
644 0, 0, 0, 0, // replaced with offset into relocation table
645 0xe9, // jmp relative
646 0, 0, 0, 0 // replaced with offset to start of .plt
649 // Write out the PLT. This uses the hand-coded instructions above,
650 // and adjusts them as needed. This is all specified by the i386 ELF
651 // Processor Supplement.
654 Output_data_plt_i386::do_write(Output_file
* of
)
656 const off_t offset
= this->offset();
657 const section_size_type oview_size
=
658 convert_to_section_size_type(this->data_size());
659 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
661 const off_t got_file_offset
= this->got_plt_
->offset();
662 const section_size_type got_size
=
663 convert_to_section_size_type(this->got_plt_
->data_size());
664 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
667 unsigned char* pov
= oview
;
669 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
670 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
672 if (parameters
->options().shared())
673 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
676 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
677 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
678 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
680 pov
+= plt_entry_size
;
682 unsigned char* got_pov
= got_view
;
684 memset(got_pov
, 0, 12);
687 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
689 unsigned int plt_offset
= plt_entry_size
;
690 unsigned int plt_rel_offset
= 0;
691 unsigned int got_offset
= 12;
692 const unsigned int count
= this->count_
;
693 for (unsigned int i
= 0;
696 pov
+= plt_entry_size
,
698 plt_offset
+= plt_entry_size
,
699 plt_rel_offset
+= rel_size
,
702 // Set and adjust the PLT entry itself.
704 if (parameters
->options().shared())
706 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
707 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
711 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
712 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
717 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
718 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
719 - (plt_offset
+ plt_entry_size
));
721 // Set the entry in the GOT.
722 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
725 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
726 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
728 of
->write_output_view(offset
, oview_size
, oview
);
729 of
->write_output_view(got_file_offset
, got_size
, got_view
);
732 // Create a PLT entry for a global symbol.
735 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
737 if (gsym
->has_plt_offset())
740 if (this->plt_
== NULL
)
742 // Create the GOT sections first.
743 this->got_section(symtab
, layout
);
745 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
746 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
748 | elfcpp::SHF_EXECINSTR
),
752 this->plt_
->add_entry(gsym
);
755 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
758 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
760 if (this->tls_base_symbol_defined_
)
763 Output_segment
* tls_segment
= layout
->tls_segment();
764 if (tls_segment
!= NULL
)
766 bool is_exec
= parameters
->options().output_is_executable();
767 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
771 elfcpp::STV_HIDDEN
, 0,
773 ? Symbol::SEGMENT_END
774 : Symbol::SEGMENT_START
),
777 this->tls_base_symbol_defined_
= true;
780 // Create a GOT entry for the TLS module index.
783 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
784 Sized_relobj
<32, false>* object
)
786 if (this->got_mod_index_offset_
== -1U)
788 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
789 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
790 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
791 unsigned int got_offset
= got
->add_constant(0);
792 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
794 got
->add_constant(0);
795 this->got_mod_index_offset_
= got_offset
;
797 return this->got_mod_index_offset_
;
800 // Optimize the TLS relocation type based on what we know about the
801 // symbol. IS_FINAL is true if the final address of this symbol is
802 // known at link time.
804 tls::Tls_optimization
805 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
807 // If we are generating a shared library, then we can't do anything
809 if (parameters
->options().shared())
810 return tls::TLSOPT_NONE
;
814 case elfcpp::R_386_TLS_GD
:
815 case elfcpp::R_386_TLS_GOTDESC
:
816 case elfcpp::R_386_TLS_DESC_CALL
:
817 // These are General-Dynamic which permits fully general TLS
818 // access. Since we know that we are generating an executable,
819 // we can convert this to Initial-Exec. If we also know that
820 // this is a local symbol, we can further switch to Local-Exec.
822 return tls::TLSOPT_TO_LE
;
823 return tls::TLSOPT_TO_IE
;
825 case elfcpp::R_386_TLS_LDM
:
826 // This is Local-Dynamic, which refers to a local symbol in the
827 // dynamic TLS block. Since we know that we generating an
828 // executable, we can switch to Local-Exec.
829 return tls::TLSOPT_TO_LE
;
831 case elfcpp::R_386_TLS_LDO_32
:
832 // Another type of Local-Dynamic relocation.
833 return tls::TLSOPT_TO_LE
;
835 case elfcpp::R_386_TLS_IE
:
836 case elfcpp::R_386_TLS_GOTIE
:
837 case elfcpp::R_386_TLS_IE_32
:
838 // These are Initial-Exec relocs which get the thread offset
839 // from the GOT. If we know that we are linking against the
840 // local symbol, we can switch to Local-Exec, which links the
841 // thread offset into the instruction.
843 return tls::TLSOPT_TO_LE
;
844 return tls::TLSOPT_NONE
;
846 case elfcpp::R_386_TLS_LE
:
847 case elfcpp::R_386_TLS_LE_32
:
848 // When we already have Local-Exec, there is nothing further we
850 return tls::TLSOPT_NONE
;
857 // Report an unsupported relocation against a local symbol.
860 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
863 gold_error(_("%s: unsupported reloc %u against local symbol"),
864 object
->name().c_str(), r_type
);
867 // Scan a relocation for a local symbol.
870 Target_i386::Scan::local(const General_options
&,
871 Symbol_table
* symtab
,
874 Sized_relobj
<32, false>* object
,
875 unsigned int data_shndx
,
876 Output_section
* output_section
,
877 const elfcpp::Rel
<32, false>& reloc
,
879 const elfcpp::Sym
<32, false>& lsym
)
883 case elfcpp::R_386_NONE
:
884 case elfcpp::R_386_GNU_VTINHERIT
:
885 case elfcpp::R_386_GNU_VTENTRY
:
888 case elfcpp::R_386_32
:
889 // If building a shared library (or a position-independent
890 // executable), we need to create a dynamic relocation for
891 // this location. The relocation applied at link time will
892 // apply the link-time value, so we flag the location with
893 // an R_386_RELATIVE relocation so the dynamic loader can
894 // relocate it easily.
895 if (parameters
->options().output_is_position_independent())
897 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
898 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
899 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
900 output_section
, data_shndx
,
901 reloc
.get_r_offset());
905 case elfcpp::R_386_16
:
906 case elfcpp::R_386_8
:
907 // If building a shared library (or a position-independent
908 // executable), we need to create a dynamic relocation for
909 // this location. Because the addend needs to remain in the
910 // data section, we need to be careful not to apply this
911 // relocation statically.
912 if (parameters
->options().output_is_position_independent())
914 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
915 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
916 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
917 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
918 data_shndx
, reloc
.get_r_offset());
921 gold_assert(lsym
.get_st_value() == 0);
922 unsigned int shndx
= lsym
.get_st_shndx();
924 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
927 object
->error(_("section symbol %u has bad shndx %u"),
930 rel_dyn
->add_local_section(object
, shndx
,
931 r_type
, output_section
,
932 data_shndx
, reloc
.get_r_offset());
937 case elfcpp::R_386_PC32
:
938 case elfcpp::R_386_PC16
:
939 case elfcpp::R_386_PC8
:
942 case elfcpp::R_386_PLT32
:
943 // Since we know this is a local symbol, we can handle this as a
947 case elfcpp::R_386_GOTOFF
:
948 case elfcpp::R_386_GOTPC
:
949 // We need a GOT section.
950 target
->got_section(symtab
, layout
);
953 case elfcpp::R_386_GOT32
:
955 // The symbol requires a GOT entry.
956 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
957 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
958 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
960 // If we are generating a shared object, we need to add a
961 // dynamic RELATIVE relocation for this symbol's GOT entry.
962 if (parameters
->options().output_is_position_independent())
964 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
965 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
966 rel_dyn
->add_local_relative(
967 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
968 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
974 // These are relocations which should only be seen by the
975 // dynamic linker, and should never be seen here.
976 case elfcpp::R_386_COPY
:
977 case elfcpp::R_386_GLOB_DAT
:
978 case elfcpp::R_386_JUMP_SLOT
:
979 case elfcpp::R_386_RELATIVE
:
980 case elfcpp::R_386_TLS_TPOFF
:
981 case elfcpp::R_386_TLS_DTPMOD32
:
982 case elfcpp::R_386_TLS_DTPOFF32
:
983 case elfcpp::R_386_TLS_TPOFF32
:
984 case elfcpp::R_386_TLS_DESC
:
985 gold_error(_("%s: unexpected reloc %u in object file"),
986 object
->name().c_str(), r_type
);
989 // These are initial TLS relocs, which are expected when
991 case elfcpp::R_386_TLS_GD
: // Global-dynamic
992 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
993 case elfcpp::R_386_TLS_DESC_CALL
:
994 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
995 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
996 case elfcpp::R_386_TLS_IE
: // Initial-exec
997 case elfcpp::R_386_TLS_IE_32
:
998 case elfcpp::R_386_TLS_GOTIE
:
999 case elfcpp::R_386_TLS_LE
: // Local-exec
1000 case elfcpp::R_386_TLS_LE_32
:
1002 bool output_is_shared
= parameters
->options().shared();
1003 const tls::Tls_optimization optimized_type
1004 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1007 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1008 if (optimized_type
== tls::TLSOPT_NONE
)
1010 // Create a pair of GOT entries for the module index and
1011 // dtv-relative offset.
1012 Output_data_got
<32, false>* got
1013 = target
->got_section(symtab
, layout
);
1014 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1015 unsigned int shndx
= lsym
.get_st_shndx();
1017 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1019 object
->error(_("local symbol %u has bad shndx %u"),
1022 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1024 target
->rel_dyn_section(layout
),
1025 elfcpp::R_386_TLS_DTPMOD32
, 0);
1027 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1028 unsupported_reloc_local(object
, r_type
);
1031 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1032 target
->define_tls_base_symbol(symtab
, layout
);
1033 if (optimized_type
== tls::TLSOPT_NONE
)
1035 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1036 Output_data_got
<32, false>* got
1037 = target
->got_section(symtab
, layout
);
1038 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1039 unsigned int shndx
= lsym
.get_st_shndx();
1041 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1043 object
->error(_("local symbol %u has bad shndx %u"),
1046 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1048 target
->rel_dyn_section(layout
),
1049 elfcpp::R_386_TLS_DESC
, 0);
1051 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1052 unsupported_reloc_local(object
, r_type
);
1055 case elfcpp::R_386_TLS_DESC_CALL
:
1058 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1059 if (optimized_type
== tls::TLSOPT_NONE
)
1061 // Create a GOT entry for the module index.
1062 target
->got_mod_index_entry(symtab
, layout
, object
);
1064 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1065 unsupported_reloc_local(object
, r_type
);
1068 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1071 case elfcpp::R_386_TLS_IE
: // Initial-exec
1072 case elfcpp::R_386_TLS_IE_32
:
1073 case elfcpp::R_386_TLS_GOTIE
:
1074 layout
->set_has_static_tls();
1075 if (optimized_type
== tls::TLSOPT_NONE
)
1077 // For the R_386_TLS_IE relocation, we need to create a
1078 // dynamic relocation when building a shared library.
1079 if (r_type
== elfcpp::R_386_TLS_IE
1080 && parameters
->options().shared())
1082 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1084 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1085 rel_dyn
->add_local_relative(object
, r_sym
,
1086 elfcpp::R_386_RELATIVE
,
1087 output_section
, data_shndx
,
1088 reloc
.get_r_offset());
1090 // Create a GOT entry for the tp-relative offset.
1091 Output_data_got
<32, false>* got
1092 = target
->got_section(symtab
, layout
);
1093 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1094 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1095 ? elfcpp::R_386_TLS_TPOFF32
1096 : elfcpp::R_386_TLS_TPOFF
);
1097 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1098 ? GOT_TYPE_TLS_OFFSET
1099 : GOT_TYPE_TLS_NOFFSET
);
1100 got
->add_local_with_rel(object
, r_sym
, got_type
,
1101 target
->rel_dyn_section(layout
),
1104 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1105 unsupported_reloc_local(object
, r_type
);
1108 case elfcpp::R_386_TLS_LE
: // Local-exec
1109 case elfcpp::R_386_TLS_LE_32
:
1110 layout
->set_has_static_tls();
1111 if (output_is_shared
)
1113 // We need to create a dynamic relocation.
1114 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1115 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1116 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1117 ? elfcpp::R_386_TLS_TPOFF32
1118 : elfcpp::R_386_TLS_TPOFF
);
1119 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1120 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1121 data_shndx
, reloc
.get_r_offset());
1131 case elfcpp::R_386_32PLT
:
1132 case elfcpp::R_386_TLS_GD_32
:
1133 case elfcpp::R_386_TLS_GD_PUSH
:
1134 case elfcpp::R_386_TLS_GD_CALL
:
1135 case elfcpp::R_386_TLS_GD_POP
:
1136 case elfcpp::R_386_TLS_LDM_32
:
1137 case elfcpp::R_386_TLS_LDM_PUSH
:
1138 case elfcpp::R_386_TLS_LDM_CALL
:
1139 case elfcpp::R_386_TLS_LDM_POP
:
1140 case elfcpp::R_386_USED_BY_INTEL_200
:
1142 unsupported_reloc_local(object
, r_type
);
1147 // Report an unsupported relocation against a global symbol.
1150 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1151 unsigned int r_type
,
1154 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1155 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1158 // Scan a relocation for a global symbol.
1161 Target_i386::Scan::global(const General_options
&,
1162 Symbol_table
* symtab
,
1164 Target_i386
* target
,
1165 Sized_relobj
<32, false>* object
,
1166 unsigned int data_shndx
,
1167 Output_section
* output_section
,
1168 const elfcpp::Rel
<32, false>& reloc
,
1169 unsigned int r_type
,
1174 case elfcpp::R_386_NONE
:
1175 case elfcpp::R_386_GNU_VTINHERIT
:
1176 case elfcpp::R_386_GNU_VTENTRY
:
1179 case elfcpp::R_386_32
:
1180 case elfcpp::R_386_16
:
1181 case elfcpp::R_386_8
:
1183 // Make a PLT entry if necessary.
1184 if (gsym
->needs_plt_entry())
1186 target
->make_plt_entry(symtab
, layout
, gsym
);
1187 // Since this is not a PC-relative relocation, we may be
1188 // taking the address of a function. In that case we need to
1189 // set the entry in the dynamic symbol table to the address of
1191 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1192 gsym
->set_needs_dynsym_value();
1194 // Make a dynamic relocation if necessary.
1195 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1197 if (target
->may_need_copy_reloc(gsym
))
1199 target
->copy_reloc(symtab
, layout
, object
,
1200 data_shndx
, output_section
, gsym
, reloc
);
1202 else if (r_type
== elfcpp::R_386_32
1203 && gsym
->can_use_relative_reloc(false))
1205 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1206 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1207 output_section
, object
,
1208 data_shndx
, reloc
.get_r_offset());
1212 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1213 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1214 data_shndx
, reloc
.get_r_offset());
1220 case elfcpp::R_386_PC32
:
1221 case elfcpp::R_386_PC16
:
1222 case elfcpp::R_386_PC8
:
1224 // Make a PLT entry if necessary.
1225 if (gsym
->needs_plt_entry())
1227 // These relocations are used for function calls only in
1228 // non-PIC code. For a 32-bit relocation in a shared library,
1229 // we'll need a text relocation anyway, so we can skip the
1230 // PLT entry and let the dynamic linker bind the call directly
1231 // to the target. For smaller relocations, we should use a
1232 // PLT entry to ensure that the call can reach.
1233 if (!parameters
->options().shared()
1234 || r_type
!= elfcpp::R_386_PC32
)
1235 target
->make_plt_entry(symtab
, layout
, gsym
);
1237 // Make a dynamic relocation if necessary.
1238 int flags
= Symbol::NON_PIC_REF
;
1239 if (gsym
->type() == elfcpp::STT_FUNC
)
1240 flags
|= Symbol::FUNCTION_CALL
;
1241 if (gsym
->needs_dynamic_reloc(flags
))
1243 if (target
->may_need_copy_reloc(gsym
))
1245 target
->copy_reloc(symtab
, layout
, object
,
1246 data_shndx
, output_section
, gsym
, reloc
);
1250 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1251 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1252 data_shndx
, reloc
.get_r_offset());
1258 case elfcpp::R_386_GOT32
:
1260 // The symbol requires a GOT entry.
1261 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1262 if (gsym
->final_value_is_known())
1263 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1266 // If this symbol is not fully resolved, we need to add a
1267 // GOT entry with a dynamic relocation.
1268 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1269 if (gsym
->is_from_dynobj()
1270 || gsym
->is_undefined()
1271 || gsym
->is_preemptible())
1272 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1273 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1276 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1277 rel_dyn
->add_global_relative(
1278 gsym
, elfcpp::R_386_RELATIVE
, got
,
1279 gsym
->got_offset(GOT_TYPE_STANDARD
));
1285 case elfcpp::R_386_PLT32
:
1286 // If the symbol is fully resolved, this is just a PC32 reloc.
1287 // Otherwise we need a PLT entry.
1288 if (gsym
->final_value_is_known())
1290 // If building a shared library, we can also skip the PLT entry
1291 // if the symbol is defined in the output file and is protected
1293 if (gsym
->is_defined()
1294 && !gsym
->is_from_dynobj()
1295 && !gsym
->is_preemptible())
1297 target
->make_plt_entry(symtab
, layout
, gsym
);
1300 case elfcpp::R_386_GOTOFF
:
1301 case elfcpp::R_386_GOTPC
:
1302 // We need a GOT section.
1303 target
->got_section(symtab
, layout
);
1306 // These are relocations which should only be seen by the
1307 // dynamic linker, and should never be seen here.
1308 case elfcpp::R_386_COPY
:
1309 case elfcpp::R_386_GLOB_DAT
:
1310 case elfcpp::R_386_JUMP_SLOT
:
1311 case elfcpp::R_386_RELATIVE
:
1312 case elfcpp::R_386_TLS_TPOFF
:
1313 case elfcpp::R_386_TLS_DTPMOD32
:
1314 case elfcpp::R_386_TLS_DTPOFF32
:
1315 case elfcpp::R_386_TLS_TPOFF32
:
1316 case elfcpp::R_386_TLS_DESC
:
1317 gold_error(_("%s: unexpected reloc %u in object file"),
1318 object
->name().c_str(), r_type
);
1321 // These are initial tls relocs, which are expected when
1323 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1324 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1325 case elfcpp::R_386_TLS_DESC_CALL
:
1326 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1327 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1328 case elfcpp::R_386_TLS_IE
: // Initial-exec
1329 case elfcpp::R_386_TLS_IE_32
:
1330 case elfcpp::R_386_TLS_GOTIE
:
1331 case elfcpp::R_386_TLS_LE
: // Local-exec
1332 case elfcpp::R_386_TLS_LE_32
:
1334 const bool is_final
= gsym
->final_value_is_known();
1335 const tls::Tls_optimization optimized_type
1336 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1339 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1340 if (optimized_type
== tls::TLSOPT_NONE
)
1342 // Create a pair of GOT entries for the module index and
1343 // dtv-relative offset.
1344 Output_data_got
<32, false>* got
1345 = target
->got_section(symtab
, layout
);
1346 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1347 target
->rel_dyn_section(layout
),
1348 elfcpp::R_386_TLS_DTPMOD32
,
1349 elfcpp::R_386_TLS_DTPOFF32
);
1351 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1353 // Create a GOT entry for the tp-relative offset.
1354 Output_data_got
<32, false>* got
1355 = target
->got_section(symtab
, layout
);
1356 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1357 target
->rel_dyn_section(layout
),
1358 elfcpp::R_386_TLS_TPOFF
);
1360 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1361 unsupported_reloc_global(object
, r_type
, gsym
);
1364 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1365 target
->define_tls_base_symbol(symtab
, layout
);
1366 if (optimized_type
== tls::TLSOPT_NONE
)
1368 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1369 Output_data_got
<32, false>* got
1370 = target
->got_section(symtab
, layout
);
1371 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1372 target
->rel_dyn_section(layout
),
1373 elfcpp::R_386_TLS_DESC
, 0);
1375 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1377 // Create a GOT entry for the tp-relative offset.
1378 Output_data_got
<32, false>* got
1379 = target
->got_section(symtab
, layout
);
1380 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1381 target
->rel_dyn_section(layout
),
1382 elfcpp::R_386_TLS_TPOFF
);
1384 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1385 unsupported_reloc_global(object
, r_type
, gsym
);
1388 case elfcpp::R_386_TLS_DESC_CALL
:
1391 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1392 if (optimized_type
== tls::TLSOPT_NONE
)
1394 // Create a GOT entry for the module index.
1395 target
->got_mod_index_entry(symtab
, layout
, object
);
1397 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1398 unsupported_reloc_global(object
, r_type
, gsym
);
1401 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1404 case elfcpp::R_386_TLS_IE
: // Initial-exec
1405 case elfcpp::R_386_TLS_IE_32
:
1406 case elfcpp::R_386_TLS_GOTIE
:
1407 layout
->set_has_static_tls();
1408 if (optimized_type
== tls::TLSOPT_NONE
)
1410 // For the R_386_TLS_IE relocation, we need to create a
1411 // dynamic relocation when building a shared library.
1412 if (r_type
== elfcpp::R_386_TLS_IE
1413 && parameters
->options().shared())
1415 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1416 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1417 output_section
, object
,
1419 reloc
.get_r_offset());
1421 // Create a GOT entry for the tp-relative offset.
1422 Output_data_got
<32, false>* got
1423 = target
->got_section(symtab
, layout
);
1424 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1425 ? elfcpp::R_386_TLS_TPOFF32
1426 : elfcpp::R_386_TLS_TPOFF
);
1427 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1428 ? GOT_TYPE_TLS_OFFSET
1429 : GOT_TYPE_TLS_NOFFSET
);
1430 got
->add_global_with_rel(gsym
, got_type
,
1431 target
->rel_dyn_section(layout
),
1434 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1435 unsupported_reloc_global(object
, r_type
, gsym
);
1438 case elfcpp::R_386_TLS_LE
: // Local-exec
1439 case elfcpp::R_386_TLS_LE_32
:
1440 layout
->set_has_static_tls();
1441 if (parameters
->options().shared())
1443 // We need to create a dynamic relocation.
1444 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1445 ? elfcpp::R_386_TLS_TPOFF32
1446 : elfcpp::R_386_TLS_TPOFF
);
1447 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1448 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1449 data_shndx
, reloc
.get_r_offset());
1459 case elfcpp::R_386_32PLT
:
1460 case elfcpp::R_386_TLS_GD_32
:
1461 case elfcpp::R_386_TLS_GD_PUSH
:
1462 case elfcpp::R_386_TLS_GD_CALL
:
1463 case elfcpp::R_386_TLS_GD_POP
:
1464 case elfcpp::R_386_TLS_LDM_32
:
1465 case elfcpp::R_386_TLS_LDM_PUSH
:
1466 case elfcpp::R_386_TLS_LDM_CALL
:
1467 case elfcpp::R_386_TLS_LDM_POP
:
1468 case elfcpp::R_386_USED_BY_INTEL_200
:
1470 unsupported_reloc_global(object
, r_type
, gsym
);
1475 // Process relocations for gc.
1478 Target_i386::gc_process_relocs(const General_options
& options
,
1479 Symbol_table
* symtab
,
1481 Sized_relobj
<32, false>* object
,
1482 unsigned int data_shndx
,
1484 const unsigned char* prelocs
,
1486 Output_section
* output_section
,
1487 bool needs_special_offset_handling
,
1488 size_t local_symbol_count
,
1489 const unsigned char* plocal_symbols
)
1491 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1502 needs_special_offset_handling
,
1507 // Scan relocations for a section.
1510 Target_i386::scan_relocs(const General_options
& options
,
1511 Symbol_table
* symtab
,
1513 Sized_relobj
<32, false>* object
,
1514 unsigned int data_shndx
,
1515 unsigned int sh_type
,
1516 const unsigned char* prelocs
,
1518 Output_section
* output_section
,
1519 bool needs_special_offset_handling
,
1520 size_t local_symbol_count
,
1521 const unsigned char* plocal_symbols
)
1523 if (sh_type
== elfcpp::SHT_RELA
)
1525 gold_error(_("%s: unsupported RELA reloc section"),
1526 object
->name().c_str());
1530 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1541 needs_special_offset_handling
,
1546 // Finalize the sections.
1549 Target_i386::do_finalize_sections(Layout
* layout
)
1551 // Fill in some more dynamic tags.
1552 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1555 if (this->got_plt_
!= NULL
)
1556 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1558 if (this->plt_
!= NULL
)
1560 const Output_data
* od
= this->plt_
->rel_plt();
1561 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1562 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1563 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1566 if (this->rel_dyn_
!= NULL
)
1568 const Output_data
* od
= this->rel_dyn_
;
1569 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1570 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1571 odyn
->add_constant(elfcpp::DT_RELENT
,
1572 elfcpp::Elf_sizes
<32>::rel_size
);
1575 if (!parameters
->options().shared())
1577 // The value of the DT_DEBUG tag is filled in by the dynamic
1578 // linker at run time, and used by the debugger.
1579 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1583 // Emit any relocs we saved in an attempt to avoid generating COPY
1585 if (this->copy_relocs_
.any_saved_relocs())
1586 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1589 // Return whether a direct absolute static relocation needs to be applied.
1590 // In cases where Scan::local() or Scan::global() has created
1591 // a dynamic relocation other than R_386_RELATIVE, the addend
1592 // of the relocation is carried in the data, and we must not
1593 // apply the static relocation.
1596 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1600 // For local symbols, we will have created a non-RELATIVE dynamic
1601 // relocation only if (a) the output is position independent,
1602 // (b) the relocation is absolute (not pc- or segment-relative), and
1603 // (c) the relocation is not 32 bits wide.
1605 return !(parameters
->options().output_is_position_independent()
1606 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1609 // For global symbols, we use the same helper routines used in the
1610 // scan pass. If we did not create a dynamic relocation, or if we
1611 // created a RELATIVE dynamic relocation, we should apply the static
1613 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1614 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1615 && gsym
->can_use_relative_reloc(ref_flags
1616 & Symbol::FUNCTION_CALL
);
1617 return !has_dyn
|| is_rel
;
1620 // Perform a relocation.
1623 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1624 Target_i386
* target
,
1626 const elfcpp::Rel
<32, false>& rel
,
1627 unsigned int r_type
,
1628 const Sized_symbol
<32>* gsym
,
1629 const Symbol_value
<32>* psymval
,
1630 unsigned char* view
,
1631 elfcpp::Elf_types
<32>::Elf_Addr address
,
1632 section_size_type view_size
)
1634 if (this->skip_call_tls_get_addr_
)
1636 if (r_type
!= elfcpp::R_386_PLT32
1638 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1639 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1640 _("missing expected TLS relocation"));
1643 this->skip_call_tls_get_addr_
= false;
1648 // Pick the value to use for symbols defined in shared objects.
1649 Symbol_value
<32> symval
;
1651 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1652 || r_type
== elfcpp::R_386_PC16
1653 || r_type
== elfcpp::R_386_PC32
))
1655 symval
.set_output_value(target
->plt_section()->address()
1656 + gsym
->plt_offset());
1660 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1662 // Get the GOT offset if needed.
1663 // The GOT pointer points to the end of the GOT section.
1664 // We need to subtract the size of the GOT section to get
1665 // the actual offset to use in the relocation.
1666 bool have_got_offset
= false;
1667 unsigned int got_offset
= 0;
1670 case elfcpp::R_386_GOT32
:
1673 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1674 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1675 - target
->got_size());
1679 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1680 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1681 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1682 - target
->got_size());
1684 have_got_offset
= true;
1693 case elfcpp::R_386_NONE
:
1694 case elfcpp::R_386_GNU_VTINHERIT
:
1695 case elfcpp::R_386_GNU_VTENTRY
:
1698 case elfcpp::R_386_32
:
1699 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true))
1700 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1703 case elfcpp::R_386_PC32
:
1705 int ref_flags
= Symbol::NON_PIC_REF
;
1706 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1707 ref_flags
|= Symbol::FUNCTION_CALL
;
1708 if (should_apply_static_reloc(gsym
, ref_flags
, true))
1709 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1713 case elfcpp::R_386_16
:
1714 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1715 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1718 case elfcpp::R_386_PC16
:
1720 int ref_flags
= Symbol::NON_PIC_REF
;
1721 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1722 ref_flags
|= Symbol::FUNCTION_CALL
;
1723 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1724 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1728 case elfcpp::R_386_8
:
1729 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1730 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1733 case elfcpp::R_386_PC8
:
1735 int ref_flags
= Symbol::NON_PIC_REF
;
1736 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1737 ref_flags
|= Symbol::FUNCTION_CALL
;
1738 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1739 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1743 case elfcpp::R_386_PLT32
:
1744 gold_assert(gsym
== NULL
1745 || gsym
->has_plt_offset()
1746 || gsym
->final_value_is_known()
1747 || (gsym
->is_defined()
1748 && !gsym
->is_from_dynobj()
1749 && !gsym
->is_preemptible()));
1750 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1753 case elfcpp::R_386_GOT32
:
1754 gold_assert(have_got_offset
);
1755 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1758 case elfcpp::R_386_GOTOFF
:
1760 elfcpp::Elf_types
<32>::Elf_Addr value
;
1761 value
= (psymval
->value(object
, 0)
1762 - target
->got_plt_section()->address());
1763 Relocate_functions
<32, false>::rel32(view
, value
);
1767 case elfcpp::R_386_GOTPC
:
1769 elfcpp::Elf_types
<32>::Elf_Addr value
;
1770 value
= target
->got_plt_section()->address();
1771 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1775 case elfcpp::R_386_COPY
:
1776 case elfcpp::R_386_GLOB_DAT
:
1777 case elfcpp::R_386_JUMP_SLOT
:
1778 case elfcpp::R_386_RELATIVE
:
1779 // These are outstanding tls relocs, which are unexpected when
1781 case elfcpp::R_386_TLS_TPOFF
:
1782 case elfcpp::R_386_TLS_DTPMOD32
:
1783 case elfcpp::R_386_TLS_DTPOFF32
:
1784 case elfcpp::R_386_TLS_TPOFF32
:
1785 case elfcpp::R_386_TLS_DESC
:
1786 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1787 _("unexpected reloc %u in object file"),
1791 // These are initial tls relocs, which are expected when
1793 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1794 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1795 case elfcpp::R_386_TLS_DESC_CALL
:
1796 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1797 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1798 case elfcpp::R_386_TLS_IE
: // Initial-exec
1799 case elfcpp::R_386_TLS_IE_32
:
1800 case elfcpp::R_386_TLS_GOTIE
:
1801 case elfcpp::R_386_TLS_LE
: // Local-exec
1802 case elfcpp::R_386_TLS_LE_32
:
1803 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1804 view
, address
, view_size
);
1807 case elfcpp::R_386_32PLT
:
1808 case elfcpp::R_386_TLS_GD_32
:
1809 case elfcpp::R_386_TLS_GD_PUSH
:
1810 case elfcpp::R_386_TLS_GD_CALL
:
1811 case elfcpp::R_386_TLS_GD_POP
:
1812 case elfcpp::R_386_TLS_LDM_32
:
1813 case elfcpp::R_386_TLS_LDM_PUSH
:
1814 case elfcpp::R_386_TLS_LDM_CALL
:
1815 case elfcpp::R_386_TLS_LDM_POP
:
1816 case elfcpp::R_386_USED_BY_INTEL_200
:
1818 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1819 _("unsupported reloc %u"),
1827 // Perform a TLS relocation.
1830 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1831 Target_i386
* target
,
1833 const elfcpp::Rel
<32, false>& rel
,
1834 unsigned int r_type
,
1835 const Sized_symbol
<32>* gsym
,
1836 const Symbol_value
<32>* psymval
,
1837 unsigned char* view
,
1838 elfcpp::Elf_types
<32>::Elf_Addr
,
1839 section_size_type view_size
)
1841 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1843 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1845 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1847 const bool is_final
=
1849 ? !parameters
->options().output_is_position_independent()
1850 : gsym
->final_value_is_known());
1851 const tls::Tls_optimization optimized_type
1852 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1855 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1856 if (optimized_type
== tls::TLSOPT_TO_LE
)
1858 gold_assert(tls_segment
!= NULL
);
1859 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1860 rel
, r_type
, value
, view
,
1866 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1867 ? GOT_TYPE_TLS_NOFFSET
1868 : GOT_TYPE_TLS_PAIR
);
1869 unsigned int got_offset
;
1872 gold_assert(gsym
->has_got_offset(got_type
));
1873 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1877 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1878 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1879 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1880 - target
->got_size());
1882 if (optimized_type
== tls::TLSOPT_TO_IE
)
1884 gold_assert(tls_segment
!= NULL
);
1885 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1886 got_offset
, view
, view_size
);
1889 else if (optimized_type
== tls::TLSOPT_NONE
)
1891 // Relocate the field with the offset of the pair of GOT
1893 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1897 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1898 _("unsupported reloc %u"),
1902 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1903 case elfcpp::R_386_TLS_DESC_CALL
:
1904 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1905 if (optimized_type
== tls::TLSOPT_TO_LE
)
1907 gold_assert(tls_segment
!= NULL
);
1908 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1909 rel
, r_type
, value
, view
,
1915 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1916 ? GOT_TYPE_TLS_NOFFSET
1917 : GOT_TYPE_TLS_DESC
);
1918 unsigned int got_offset
;
1921 gold_assert(gsym
->has_got_offset(got_type
));
1922 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1926 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1927 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1928 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1929 - target
->got_size());
1931 if (optimized_type
== tls::TLSOPT_TO_IE
)
1933 gold_assert(tls_segment
!= NULL
);
1934 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1935 got_offset
, view
, view_size
);
1938 else if (optimized_type
== tls::TLSOPT_NONE
)
1940 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1942 // Relocate the field with the offset of the pair of GOT
1944 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1949 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1950 _("unsupported reloc %u"),
1954 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1955 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1957 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1958 _("both SUN and GNU model "
1959 "TLS relocations"));
1962 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1963 if (optimized_type
== tls::TLSOPT_TO_LE
)
1965 gold_assert(tls_segment
!= NULL
);
1966 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1967 value
, view
, view_size
);
1970 else if (optimized_type
== tls::TLSOPT_NONE
)
1972 // Relocate the field with the offset of the GOT entry for
1973 // the module index.
1974 unsigned int got_offset
;
1975 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1976 - target
->got_size());
1977 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1980 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1981 _("unsupported reloc %u"),
1985 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1986 // This reloc can appear in debugging sections, in which case we
1987 // won't see the TLS_LDM reloc. The local_dynamic_type field
1989 if (optimized_type
== tls::TLSOPT_TO_LE
1990 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
1992 gold_assert(tls_segment
!= NULL
);
1993 value
-= tls_segment
->memsz();
1995 Relocate_functions
<32, false>::rel32(view
, value
);
1998 case elfcpp::R_386_TLS_IE
: // Initial-exec
1999 case elfcpp::R_386_TLS_GOTIE
:
2000 case elfcpp::R_386_TLS_IE_32
:
2001 if (optimized_type
== tls::TLSOPT_TO_LE
)
2003 gold_assert(tls_segment
!= NULL
);
2004 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2005 rel
, r_type
, value
, view
,
2009 else if (optimized_type
== tls::TLSOPT_NONE
)
2011 // Relocate the field with the offset of the GOT entry for
2012 // the tp-relative offset of the symbol.
2013 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2014 ? GOT_TYPE_TLS_OFFSET
2015 : GOT_TYPE_TLS_NOFFSET
);
2016 unsigned int got_offset
;
2019 gold_assert(gsym
->has_got_offset(got_type
));
2020 got_offset
= gsym
->got_offset(got_type
);
2024 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2025 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2026 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2028 // For the R_386_TLS_IE relocation, we need to apply the
2029 // absolute address of the GOT entry.
2030 if (r_type
== elfcpp::R_386_TLS_IE
)
2031 got_offset
+= target
->got_plt_section()->address();
2032 // All GOT offsets are relative to the end of the GOT.
2033 got_offset
-= target
->got_size();
2034 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2037 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2038 _("unsupported reloc %u"),
2042 case elfcpp::R_386_TLS_LE
: // Local-exec
2043 // If we're creating a shared library, a dynamic relocation will
2044 // have been created for this location, so do not apply it now.
2045 if (!parameters
->options().shared())
2047 gold_assert(tls_segment
!= NULL
);
2048 value
-= tls_segment
->memsz();
2049 Relocate_functions
<32, false>::rel32(view
, value
);
2053 case elfcpp::R_386_TLS_LE_32
:
2054 // If we're creating a shared library, a dynamic relocation will
2055 // have been created for this location, so do not apply it now.
2056 if (!parameters
->options().shared())
2058 gold_assert(tls_segment
!= NULL
);
2059 value
= tls_segment
->memsz() - value
;
2060 Relocate_functions
<32, false>::rel32(view
, value
);
2066 // Do a relocation in which we convert a TLS General-Dynamic to a
2070 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2072 Output_segment
* tls_segment
,
2073 const elfcpp::Rel
<32, false>& rel
,
2075 elfcpp::Elf_types
<32>::Elf_Addr value
,
2076 unsigned char* view
,
2077 section_size_type view_size
)
2079 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2080 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2081 // leal foo(%reg),%eax; call ___tls_get_addr
2082 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2084 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2085 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2087 unsigned char op1
= view
[-1];
2088 unsigned char op2
= view
[-2];
2090 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2091 op2
== 0x8d || op2
== 0x04);
2092 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2098 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2099 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2100 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2101 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2102 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2106 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2107 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2108 if (rel
.get_r_offset() + 9 < view_size
2111 // There is a trailing nop. Use the size byte subl.
2112 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2117 // Use the five byte subl.
2118 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2122 value
= tls_segment
->memsz() - value
;
2123 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2125 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2127 this->skip_call_tls_get_addr_
= true;
2130 // Do a relocation in which we convert a TLS General-Dynamic to an
2134 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2137 const elfcpp::Rel
<32, false>& rel
,
2139 elfcpp::Elf_types
<32>::Elf_Addr value
,
2140 unsigned char* view
,
2141 section_size_type view_size
)
2143 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2144 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2146 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2147 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2149 unsigned char op1
= view
[-1];
2150 unsigned char op2
= view
[-2];
2152 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2153 op2
== 0x8d || op2
== 0x04);
2154 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2158 // FIXME: For now, support only the first (SIB) form.
2159 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2163 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2164 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2165 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2166 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2167 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2171 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2172 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2173 if (rel
.get_r_offset() + 9 < view_size
2176 // FIXME: This is not the right instruction sequence.
2177 // There is a trailing nop. Use the size byte subl.
2178 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2183 // FIXME: This is not the right instruction sequence.
2184 // Use the five byte subl.
2185 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2189 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2191 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2193 this->skip_call_tls_get_addr_
= true;
2196 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2197 // General-Dynamic to a Local-Exec.
2200 Target_i386::Relocate::tls_desc_gd_to_le(
2201 const Relocate_info
<32, false>* relinfo
,
2203 Output_segment
* tls_segment
,
2204 const elfcpp::Rel
<32, false>& rel
,
2205 unsigned int r_type
,
2206 elfcpp::Elf_types
<32>::Elf_Addr value
,
2207 unsigned char* view
,
2208 section_size_type view_size
)
2210 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2212 // leal foo@TLSDESC(%ebx), %eax
2213 // ==> leal foo@NTPOFF, %eax
2214 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2215 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2216 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2217 view
[-2] == 0x8d && view
[-1] == 0x83);
2219 value
-= tls_segment
->memsz();
2220 Relocate_functions
<32, false>::rel32(view
, value
);
2224 // call *foo@TLSCALL(%eax)
2226 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2227 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2228 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2229 view
[0] == 0xff && view
[1] == 0x10);
2235 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2236 // General-Dynamic to an Initial-Exec.
2239 Target_i386::Relocate::tls_desc_gd_to_ie(
2240 const Relocate_info
<32, false>* relinfo
,
2243 const elfcpp::Rel
<32, false>& rel
,
2244 unsigned int r_type
,
2245 elfcpp::Elf_types
<32>::Elf_Addr value
,
2246 unsigned char* view
,
2247 section_size_type view_size
)
2249 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2251 // leal foo@TLSDESC(%ebx), %eax
2252 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2253 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2254 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2255 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2256 view
[-2] == 0x8d && view
[-1] == 0x83);
2258 Relocate_functions
<32, false>::rel32(view
, value
);
2262 // call *foo@TLSCALL(%eax)
2264 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2265 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2266 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2267 view
[0] == 0xff && view
[1] == 0x10);
2273 // Do a relocation in which we convert a TLS Local-Dynamic to a
2277 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2280 const elfcpp::Rel
<32, false>& rel
,
2282 elfcpp::Elf_types
<32>::Elf_Addr
,
2283 unsigned char* view
,
2284 section_size_type view_size
)
2286 // leal foo(%reg), %eax; call ___tls_get_addr
2287 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2289 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2290 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2292 // FIXME: Does this test really always pass?
2293 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2294 view
[-2] == 0x8d && view
[-1] == 0x83);
2296 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2298 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2300 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2302 this->skip_call_tls_get_addr_
= true;
2305 // Do a relocation in which we convert a TLS Initial-Exec to a
2309 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2311 Output_segment
* tls_segment
,
2312 const elfcpp::Rel
<32, false>& rel
,
2313 unsigned int r_type
,
2314 elfcpp::Elf_types
<32>::Elf_Addr value
,
2315 unsigned char* view
,
2316 section_size_type view_size
)
2318 // We have to actually change the instructions, which means that we
2319 // need to examine the opcodes to figure out which instruction we
2321 if (r_type
== elfcpp::R_386_TLS_IE
)
2323 // movl %gs:XX,%eax ==> movl $YY,%eax
2324 // movl %gs:XX,%reg ==> movl $YY,%reg
2325 // addl %gs:XX,%reg ==> addl $YY,%reg
2326 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2327 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2329 unsigned char op1
= view
[-1];
2332 // movl XX,%eax ==> movl $YY,%eax
2337 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2339 unsigned char op2
= view
[-2];
2342 // movl XX,%reg ==> movl $YY,%reg
2343 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2344 (op1
& 0xc7) == 0x05);
2346 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2348 else if (op2
== 0x03)
2350 // addl XX,%reg ==> addl $YY,%reg
2351 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2352 (op1
& 0xc7) == 0x05);
2354 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2357 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2362 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2363 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2364 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2365 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2366 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2368 unsigned char op1
= view
[-1];
2369 unsigned char op2
= view
[-2];
2370 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2371 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2374 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2376 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2378 else if (op2
== 0x2b)
2380 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2382 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2384 else if (op2
== 0x03)
2386 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2388 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2391 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2394 value
= tls_segment
->memsz() - value
;
2395 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2398 Relocate_functions
<32, false>::rel32(view
, value
);
2401 // Relocate section data.
2404 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2405 unsigned int sh_type
,
2406 const unsigned char* prelocs
,
2408 Output_section
* output_section
,
2409 bool needs_special_offset_handling
,
2410 unsigned char* view
,
2411 elfcpp::Elf_types
<32>::Elf_Addr address
,
2412 section_size_type view_size
)
2414 gold_assert(sh_type
== elfcpp::SHT_REL
);
2416 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2417 Target_i386::Relocate
>(
2423 needs_special_offset_handling
,
2429 // Return the size of a relocation while scanning during a relocatable
2433 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2434 unsigned int r_type
,
2439 case elfcpp::R_386_NONE
:
2440 case elfcpp::R_386_GNU_VTINHERIT
:
2441 case elfcpp::R_386_GNU_VTENTRY
:
2442 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2443 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2444 case elfcpp::R_386_TLS_DESC_CALL
:
2445 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2446 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2447 case elfcpp::R_386_TLS_IE
: // Initial-exec
2448 case elfcpp::R_386_TLS_IE_32
:
2449 case elfcpp::R_386_TLS_GOTIE
:
2450 case elfcpp::R_386_TLS_LE
: // Local-exec
2451 case elfcpp::R_386_TLS_LE_32
:
2454 case elfcpp::R_386_32
:
2455 case elfcpp::R_386_PC32
:
2456 case elfcpp::R_386_GOT32
:
2457 case elfcpp::R_386_PLT32
:
2458 case elfcpp::R_386_GOTOFF
:
2459 case elfcpp::R_386_GOTPC
:
2462 case elfcpp::R_386_16
:
2463 case elfcpp::R_386_PC16
:
2466 case elfcpp::R_386_8
:
2467 case elfcpp::R_386_PC8
:
2470 // These are relocations which should only be seen by the
2471 // dynamic linker, and should never be seen here.
2472 case elfcpp::R_386_COPY
:
2473 case elfcpp::R_386_GLOB_DAT
:
2474 case elfcpp::R_386_JUMP_SLOT
:
2475 case elfcpp::R_386_RELATIVE
:
2476 case elfcpp::R_386_TLS_TPOFF
:
2477 case elfcpp::R_386_TLS_DTPMOD32
:
2478 case elfcpp::R_386_TLS_DTPOFF32
:
2479 case elfcpp::R_386_TLS_TPOFF32
:
2480 case elfcpp::R_386_TLS_DESC
:
2481 object
->error(_("unexpected reloc %u in object file"), r_type
);
2484 case elfcpp::R_386_32PLT
:
2485 case elfcpp::R_386_TLS_GD_32
:
2486 case elfcpp::R_386_TLS_GD_PUSH
:
2487 case elfcpp::R_386_TLS_GD_CALL
:
2488 case elfcpp::R_386_TLS_GD_POP
:
2489 case elfcpp::R_386_TLS_LDM_32
:
2490 case elfcpp::R_386_TLS_LDM_PUSH
:
2491 case elfcpp::R_386_TLS_LDM_CALL
:
2492 case elfcpp::R_386_TLS_LDM_POP
:
2493 case elfcpp::R_386_USED_BY_INTEL_200
:
2495 object
->error(_("unsupported reloc %u in object file"), r_type
);
2500 // Scan the relocs during a relocatable link.
2503 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2504 Symbol_table
* symtab
,
2506 Sized_relobj
<32, false>* object
,
2507 unsigned int data_shndx
,
2508 unsigned int sh_type
,
2509 const unsigned char* prelocs
,
2511 Output_section
* output_section
,
2512 bool needs_special_offset_handling
,
2513 size_t local_symbol_count
,
2514 const unsigned char* plocal_symbols
,
2515 Relocatable_relocs
* rr
)
2517 gold_assert(sh_type
== elfcpp::SHT_REL
);
2519 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2520 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2522 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2523 Scan_relocatable_relocs
>(
2532 needs_special_offset_handling
,
2538 // Relocate a section during a relocatable link.
2541 Target_i386::relocate_for_relocatable(
2542 const Relocate_info
<32, false>* relinfo
,
2543 unsigned int sh_type
,
2544 const unsigned char* prelocs
,
2546 Output_section
* output_section
,
2547 off_t offset_in_output_section
,
2548 const Relocatable_relocs
* rr
,
2549 unsigned char* view
,
2550 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2551 section_size_type view_size
,
2552 unsigned char* reloc_view
,
2553 section_size_type reloc_view_size
)
2555 gold_assert(sh_type
== elfcpp::SHT_REL
);
2557 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2562 offset_in_output_section
,
2571 // Return the value to use for a dynamic which requires special
2572 // treatment. This is how we support equality comparisons of function
2573 // pointers across shared library boundaries, as described in the
2574 // processor specific ABI supplement.
2577 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2579 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2580 return this->plt_section()->address() + gsym
->plt_offset();
2583 // Return a string used to fill a code section with nops to take up
2584 // the specified length.
2587 Target_i386::do_code_fill(section_size_type length
) const
2591 // Build a jmp instruction to skip over the bytes.
2592 unsigned char jmp
[5];
2594 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2595 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2596 + std::string(length
- 5, '\0'));
2599 // Nop sequences of various lengths.
2600 const char nop1
[1] = { 0x90 }; // nop
2601 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2602 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2603 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2604 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2605 0x00 }; // leal 0(%esi,1),%esi
2606 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2608 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2610 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2611 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2612 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2613 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2615 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2616 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2618 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2619 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2621 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2622 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2623 0x00, 0x00, 0x00, 0x00 };
2624 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2625 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2626 0x27, 0x00, 0x00, 0x00,
2628 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2629 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2630 0xbc, 0x27, 0x00, 0x00,
2632 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2633 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2634 0x90, 0x90, 0x90, 0x90,
2637 const char* nops
[16] = {
2639 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2640 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2643 return std::string(nops
[length
], length
);
2646 // The selector for i386 object files.
2648 class Target_selector_i386
: public Target_selector
2651 Target_selector_i386()
2652 : Target_selector(elfcpp::EM_386
, 32, false, "elf32-i386")
2656 do_instantiate_target()
2657 { return new Target_i386(); }
2660 Target_selector_i386 target_selector_i386
;
2662 } // End anonymous namespace.