1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_i386
;
49 // The i386 target class.
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_i386
: public Target_freebsd
<32, false>
57 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
60 : Target_freebsd
<32, false>(&i386_info
),
61 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
62 copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
63 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
66 // Process the relocations to determine unreferenced sections for
67 // garbage collection.
69 gc_process_relocs(const General_options
& options
,
72 Sized_relobj
<32, false>* object
,
73 unsigned int data_shndx
,
75 const unsigned char* prelocs
,
77 Output_section
* output_section
,
78 bool needs_special_offset_handling
,
79 size_t local_symbol_count
,
80 const unsigned char* plocal_symbols
);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(const General_options
& options
,
87 Sized_relobj
<32, false>* object
,
88 unsigned int data_shndx
,
90 const unsigned char* prelocs
,
92 Output_section
* output_section
,
93 bool needs_special_offset_handling
,
94 size_t local_symbol_count
,
95 const unsigned char* plocal_symbols
);
97 // Finalize the sections.
99 do_finalize_sections(Layout
*);
101 // Return the value to use for a dynamic which requires special
104 do_dynsym_value(const Symbol
*) const;
106 // Relocate a section.
108 relocate_section(const Relocate_info
<32, false>*,
109 unsigned int sh_type
,
110 const unsigned char* prelocs
,
112 Output_section
* output_section
,
113 bool needs_special_offset_handling
,
115 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
116 section_size_type view_size
);
118 // Scan the relocs during a relocatable link.
120 scan_relocatable_relocs(const General_options
& options
,
121 Symbol_table
* symtab
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 unsigned int sh_type
,
126 const unsigned char* prelocs
,
128 Output_section
* output_section
,
129 bool needs_special_offset_handling
,
130 size_t local_symbol_count
,
131 const unsigned char* plocal_symbols
,
132 Relocatable_relocs
*);
134 // Relocate a section during a relocatable link.
136 relocate_for_relocatable(const Relocate_info
<32, false>*,
137 unsigned int sh_type
,
138 const unsigned char* prelocs
,
140 Output_section
* output_section
,
141 off_t offset_in_output_section
,
142 const Relocatable_relocs
*,
144 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
145 section_size_type view_size
,
146 unsigned char* reloc_view
,
147 section_size_type reloc_view_size
);
149 // Return a string used to fill a code section with nops.
151 do_code_fill(section_size_type length
) const;
153 // Return whether SYM is defined by the ABI.
155 do_is_defined_by_abi(const Symbol
* sym
) const
156 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
158 // Return the size of the GOT section.
162 gold_assert(this->got_
!= NULL
);
163 return this->got_
->data_size();
167 // The class which scans relocations.
171 local(const General_options
& options
, Symbol_table
* symtab
,
172 Layout
* layout
, Target_i386
* target
,
173 Sized_relobj
<32, false>* object
,
174 unsigned int data_shndx
,
175 Output_section
* output_section
,
176 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
177 const elfcpp::Sym
<32, false>& lsym
);
180 global(const General_options
& options
, Symbol_table
* symtab
,
181 Layout
* layout
, Target_i386
* target
,
182 Sized_relobj
<32, false>* object
,
183 unsigned int data_shndx
,
184 Output_section
* output_section
,
185 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
189 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
192 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
196 // The class which implements relocation.
201 : skip_call_tls_get_addr_(false),
202 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
207 if (this->skip_call_tls_get_addr_
)
209 // FIXME: This needs to specify the location somehow.
210 gold_error(_("missing expected TLS relocation"));
214 // Return whether the static relocation needs to be applied.
216 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
219 Output_section
* output_section
);
221 // Do a relocation. Return false if the caller should not issue
222 // any warnings about this relocation.
224 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
225 size_t relnum
, const elfcpp::Rel
<32, false>&,
226 unsigned int r_type
, const Sized_symbol
<32>*,
227 const Symbol_value
<32>*,
228 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
232 // Do a TLS relocation.
234 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
235 size_t relnum
, const elfcpp::Rel
<32, false>&,
236 unsigned int r_type
, const Sized_symbol
<32>*,
237 const Symbol_value
<32>*,
238 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
241 // Do a TLS General-Dynamic to Initial-Exec transition.
243 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
244 Output_segment
* tls_segment
,
245 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
246 elfcpp::Elf_types
<32>::Elf_Addr value
,
248 section_size_type view_size
);
250 // Do a TLS General-Dynamic to Local-Exec transition.
252 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
253 Output_segment
* tls_segment
,
254 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
255 elfcpp::Elf_types
<32>::Elf_Addr value
,
257 section_size_type view_size
);
259 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
262 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
263 Output_segment
* tls_segment
,
264 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
265 elfcpp::Elf_types
<32>::Elf_Addr value
,
267 section_size_type view_size
);
269 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
272 tls_desc_gd_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 // Do a TLS Local-Dynamic to Local-Exec transition.
281 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
282 Output_segment
* tls_segment
,
283 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
284 elfcpp::Elf_types
<32>::Elf_Addr value
,
286 section_size_type view_size
);
288 // Do a TLS Initial-Exec to Local-Exec transition.
290 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
291 Output_segment
* tls_segment
,
292 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
293 elfcpp::Elf_types
<32>::Elf_Addr value
,
295 section_size_type view_size
);
297 // We need to keep track of which type of local dynamic relocation
298 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
299 enum Local_dynamic_type
306 // This is set if we should skip the next reloc, which should be a
307 // PLT32 reloc against ___tls_get_addr.
308 bool skip_call_tls_get_addr_
;
309 // The type of local dynamic relocation we have seen in the section
310 // being relocated, if any.
311 Local_dynamic_type local_dynamic_type_
;
314 // A class which returns the size required for a relocation type,
315 // used while scanning relocs during a relocatable link.
316 class Relocatable_size_for_reloc
320 get_size_for_reloc(unsigned int, Relobj
*);
323 // Adjust TLS relocation type based on the options and whether this
324 // is a local symbol.
325 static tls::Tls_optimization
326 optimize_tls_reloc(bool is_final
, int r_type
);
328 // Get the GOT section, creating it if necessary.
329 Output_data_got
<32, false>*
330 got_section(Symbol_table
*, Layout
*);
332 // Get the GOT PLT section.
334 got_plt_section() const
336 gold_assert(this->got_plt_
!= NULL
);
337 return this->got_plt_
;
340 // Create a PLT entry for a global symbol.
342 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
344 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
346 define_tls_base_symbol(Symbol_table
*, Layout
*);
348 // Create a GOT entry for the TLS module index.
350 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
351 Sized_relobj
<32, false>* object
);
353 // Get the PLT section.
354 const Output_data_plt_i386
*
357 gold_assert(this->plt_
!= NULL
);
361 // Get the dynamic reloc section, creating it if necessary.
363 rel_dyn_section(Layout
*);
365 // Return true if the symbol may need a COPY relocation.
366 // References from an executable object to non-function symbols
367 // defined in a dynamic object may need a COPY relocation.
369 may_need_copy_reloc(Symbol
* gsym
)
371 return (!parameters
->options().shared()
372 && gsym
->is_from_dynobj()
373 && gsym
->type() != elfcpp::STT_FUNC
);
376 // Add a potential copy relocation.
378 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
379 Sized_relobj
<32, false>* object
,
380 unsigned int shndx
, Output_section
* output_section
,
381 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
383 this->copy_relocs_
.copy_reloc(symtab
, layout
,
384 symtab
->get_sized_symbol
<32>(sym
),
385 object
, shndx
, output_section
, reloc
,
386 this->rel_dyn_section(layout
));
389 // Information about this specific target which we pass to the
390 // general Target structure.
391 static const Target::Target_info i386_info
;
393 // The types of GOT entries needed for this platform.
396 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
397 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
398 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
399 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
400 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
404 Output_data_got
<32, false>* got_
;
406 Output_data_plt_i386
* plt_
;
407 // The GOT PLT section.
408 Output_data_space
* got_plt_
;
409 // The dynamic reloc section.
410 Reloc_section
* rel_dyn_
;
411 // Relocs saved to avoid a COPY reloc.
412 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
413 // Space for variables copied with a COPY reloc.
414 Output_data_space
* dynbss_
;
415 // Offset of the GOT entry for the TLS module index.
416 unsigned int got_mod_index_offset_
;
417 // True if the _TLS_MODULE_BASE_ symbol has been defined.
418 bool tls_base_symbol_defined_
;
421 const Target::Target_info
Target_i386::i386_info
=
424 false, // is_big_endian
425 elfcpp::EM_386
, // machine_code
426 false, // has_make_symbol
427 false, // has_resolve
428 true, // has_code_fill
429 true, // is_default_stack_executable
431 "/usr/lib/libc.so.1", // dynamic_linker
432 0x08048000, // default_text_segment_address
433 0x1000, // abi_pagesize (overridable by -z max-page-size)
434 0x1000 // common_pagesize (overridable by -z common-page-size)
437 // Get the GOT section, creating it if necessary.
439 Output_data_got
<32, false>*
440 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
442 if (this->got_
== NULL
)
444 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
446 this->got_
= new Output_data_got
<32, false>();
449 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
451 | elfcpp::SHF_WRITE
),
455 // The old GNU linker creates a .got.plt section. We just
456 // create another set of data in the .got section. Note that we
457 // always create a PLT if we create a GOT, although the PLT
459 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
460 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
462 | elfcpp::SHF_WRITE
),
466 // The first three entries are reserved.
467 this->got_plt_
->set_current_data_size(3 * 4);
469 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
470 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
472 0, 0, elfcpp::STT_OBJECT
,
474 elfcpp::STV_HIDDEN
, 0,
481 // Get the dynamic reloc section, creating it if necessary.
483 Target_i386::Reloc_section
*
484 Target_i386::rel_dyn_section(Layout
* layout
)
486 if (this->rel_dyn_
== NULL
)
488 gold_assert(layout
!= NULL
);
489 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
490 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
491 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
493 return this->rel_dyn_
;
496 // A class to handle the PLT data.
498 class Output_data_plt_i386
: public Output_section_data
501 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
503 Output_data_plt_i386(Layout
*, Output_data_space
*);
505 // Add an entry to the PLT.
507 add_entry(Symbol
* gsym
);
509 // Return the .rel.plt section data.
512 { return this->rel_
; }
516 do_adjust_output_section(Output_section
* os
);
518 // Write to a map file.
520 do_print_to_mapfile(Mapfile
* mapfile
) const
521 { mapfile
->print_output_data(this, _("** PLT")); }
524 // The size of an entry in the PLT.
525 static const int plt_entry_size
= 16;
527 // The first entry in the PLT for an executable.
528 static unsigned char exec_first_plt_entry
[plt_entry_size
];
530 // The first entry in the PLT for a shared object.
531 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
533 // Other entries in the PLT for an executable.
534 static unsigned char exec_plt_entry
[plt_entry_size
];
536 // Other entries in the PLT for a shared object.
537 static unsigned char dyn_plt_entry
[plt_entry_size
];
539 // Set the final size.
541 set_final_data_size()
542 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
544 // Write out the PLT data.
546 do_write(Output_file
*);
548 // The reloc section.
550 // The .got.plt section.
551 Output_data_space
* got_plt_
;
552 // The number of PLT entries.
556 // Create the PLT section. The ordinary .got section is an argument,
557 // since we need to refer to the start. We also create our own .got
558 // section just for PLT entries.
560 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
561 Output_data_space
* got_plt
)
562 : Output_section_data(4), got_plt_(got_plt
), count_(0)
564 this->rel_
= new Reloc_section(false);
565 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
566 elfcpp::SHF_ALLOC
, this->rel_
);
570 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
572 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
573 // linker, and so do we.
577 // Add an entry to the PLT.
580 Output_data_plt_i386::add_entry(Symbol
* gsym
)
582 gold_assert(!gsym
->has_plt_offset());
584 // Note that when setting the PLT offset we skip the initial
585 // reserved PLT entry.
586 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
590 section_offset_type got_offset
= this->got_plt_
->current_data_size();
592 // Every PLT entry needs a GOT entry which points back to the PLT
593 // entry (this will be changed by the dynamic linker, normally
594 // lazily when the function is called).
595 this->got_plt_
->set_current_data_size(got_offset
+ 4);
597 // Every PLT entry needs a reloc.
598 gsym
->set_needs_dynsym_entry();
599 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
602 // Note that we don't need to save the symbol. The contents of the
603 // PLT are independent of which symbols are used. The symbols only
604 // appear in the relocations.
607 // The first entry in the PLT for an executable.
609 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
611 0xff, 0x35, // pushl contents of memory address
612 0, 0, 0, 0, // replaced with address of .got + 4
613 0xff, 0x25, // jmp indirect
614 0, 0, 0, 0, // replaced with address of .got + 8
618 // The first entry in the PLT for a shared object.
620 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
622 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
623 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
627 // Subsequent entries in the PLT for an executable.
629 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
631 0xff, 0x25, // jmp indirect
632 0, 0, 0, 0, // replaced with address of symbol in .got
633 0x68, // pushl immediate
634 0, 0, 0, 0, // replaced with offset into relocation table
635 0xe9, // jmp relative
636 0, 0, 0, 0 // replaced with offset to start of .plt
639 // Subsequent entries in the PLT for a shared object.
641 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
643 0xff, 0xa3, // jmp *offset(%ebx)
644 0, 0, 0, 0, // replaced with offset of symbol in .got
645 0x68, // pushl immediate
646 0, 0, 0, 0, // replaced with offset into relocation table
647 0xe9, // jmp relative
648 0, 0, 0, 0 // replaced with offset to start of .plt
651 // Write out the PLT. This uses the hand-coded instructions above,
652 // and adjusts them as needed. This is all specified by the i386 ELF
653 // Processor Supplement.
656 Output_data_plt_i386::do_write(Output_file
* of
)
658 const off_t offset
= this->offset();
659 const section_size_type oview_size
=
660 convert_to_section_size_type(this->data_size());
661 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
663 const off_t got_file_offset
= this->got_plt_
->offset();
664 const section_size_type got_size
=
665 convert_to_section_size_type(this->got_plt_
->data_size());
666 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
669 unsigned char* pov
= oview
;
671 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
672 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
674 if (parameters
->options().shared())
675 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
678 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
679 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
680 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
682 pov
+= plt_entry_size
;
684 unsigned char* got_pov
= got_view
;
686 memset(got_pov
, 0, 12);
689 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
691 unsigned int plt_offset
= plt_entry_size
;
692 unsigned int plt_rel_offset
= 0;
693 unsigned int got_offset
= 12;
694 const unsigned int count
= this->count_
;
695 for (unsigned int i
= 0;
698 pov
+= plt_entry_size
,
700 plt_offset
+= plt_entry_size
,
701 plt_rel_offset
+= rel_size
,
704 // Set and adjust the PLT entry itself.
706 if (parameters
->options().shared())
708 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
709 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
713 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
714 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
719 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
720 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
721 - (plt_offset
+ plt_entry_size
));
723 // Set the entry in the GOT.
724 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
727 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
728 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
730 of
->write_output_view(offset
, oview_size
, oview
);
731 of
->write_output_view(got_file_offset
, got_size
, got_view
);
734 // Create a PLT entry for a global symbol.
737 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
739 if (gsym
->has_plt_offset())
742 if (this->plt_
== NULL
)
744 // Create the GOT sections first.
745 this->got_section(symtab
, layout
);
747 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
748 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
750 | elfcpp::SHF_EXECINSTR
),
754 this->plt_
->add_entry(gsym
);
757 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
760 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
762 if (this->tls_base_symbol_defined_
)
765 Output_segment
* tls_segment
= layout
->tls_segment();
766 if (tls_segment
!= NULL
)
768 bool is_exec
= parameters
->options().output_is_executable();
769 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
773 elfcpp::STV_HIDDEN
, 0,
775 ? Symbol::SEGMENT_END
776 : Symbol::SEGMENT_START
),
779 this->tls_base_symbol_defined_
= true;
782 // Create a GOT entry for the TLS module index.
785 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
786 Sized_relobj
<32, false>* object
)
788 if (this->got_mod_index_offset_
== -1U)
790 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
791 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
792 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
793 unsigned int got_offset
= got
->add_constant(0);
794 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
796 got
->add_constant(0);
797 this->got_mod_index_offset_
= got_offset
;
799 return this->got_mod_index_offset_
;
802 // Optimize the TLS relocation type based on what we know about the
803 // symbol. IS_FINAL is true if the final address of this symbol is
804 // known at link time.
806 tls::Tls_optimization
807 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
809 // If we are generating a shared library, then we can't do anything
811 if (parameters
->options().shared())
812 return tls::TLSOPT_NONE
;
816 case elfcpp::R_386_TLS_GD
:
817 case elfcpp::R_386_TLS_GOTDESC
:
818 case elfcpp::R_386_TLS_DESC_CALL
:
819 // These are General-Dynamic which permits fully general TLS
820 // access. Since we know that we are generating an executable,
821 // we can convert this to Initial-Exec. If we also know that
822 // this is a local symbol, we can further switch to Local-Exec.
824 return tls::TLSOPT_TO_LE
;
825 return tls::TLSOPT_TO_IE
;
827 case elfcpp::R_386_TLS_LDM
:
828 // This is Local-Dynamic, which refers to a local symbol in the
829 // dynamic TLS block. Since we know that we generating an
830 // executable, we can switch to Local-Exec.
831 return tls::TLSOPT_TO_LE
;
833 case elfcpp::R_386_TLS_LDO_32
:
834 // Another type of Local-Dynamic relocation.
835 return tls::TLSOPT_TO_LE
;
837 case elfcpp::R_386_TLS_IE
:
838 case elfcpp::R_386_TLS_GOTIE
:
839 case elfcpp::R_386_TLS_IE_32
:
840 // These are Initial-Exec relocs which get the thread offset
841 // from the GOT. If we know that we are linking against the
842 // local symbol, we can switch to Local-Exec, which links the
843 // thread offset into the instruction.
845 return tls::TLSOPT_TO_LE
;
846 return tls::TLSOPT_NONE
;
848 case elfcpp::R_386_TLS_LE
:
849 case elfcpp::R_386_TLS_LE_32
:
850 // When we already have Local-Exec, there is nothing further we
852 return tls::TLSOPT_NONE
;
859 // Report an unsupported relocation against a local symbol.
862 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
865 gold_error(_("%s: unsupported reloc %u against local symbol"),
866 object
->name().c_str(), r_type
);
869 // Scan a relocation for a local symbol.
872 Target_i386::Scan::local(const General_options
&,
873 Symbol_table
* symtab
,
876 Sized_relobj
<32, false>* object
,
877 unsigned int data_shndx
,
878 Output_section
* output_section
,
879 const elfcpp::Rel
<32, false>& reloc
,
881 const elfcpp::Sym
<32, false>& lsym
)
885 case elfcpp::R_386_NONE
:
886 case elfcpp::R_386_GNU_VTINHERIT
:
887 case elfcpp::R_386_GNU_VTENTRY
:
890 case elfcpp::R_386_32
:
891 // If building a shared library (or a position-independent
892 // executable), we need to create a dynamic relocation for
893 // this location. The relocation applied at link time will
894 // apply the link-time value, so we flag the location with
895 // an R_386_RELATIVE relocation so the dynamic loader can
896 // relocate it easily.
897 if (parameters
->options().output_is_position_independent())
899 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
900 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
901 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
902 output_section
, data_shndx
,
903 reloc
.get_r_offset());
907 case elfcpp::R_386_16
:
908 case elfcpp::R_386_8
:
909 // If building a shared library (or a position-independent
910 // executable), we need to create a dynamic relocation for
911 // this location. Because the addend needs to remain in the
912 // data section, we need to be careful not to apply this
913 // relocation statically.
914 if (parameters
->options().output_is_position_independent())
916 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
917 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
918 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
919 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
920 data_shndx
, reloc
.get_r_offset());
923 gold_assert(lsym
.get_st_value() == 0);
924 unsigned int shndx
= lsym
.get_st_shndx();
926 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
929 object
->error(_("section symbol %u has bad shndx %u"),
932 rel_dyn
->add_local_section(object
, shndx
,
933 r_type
, output_section
,
934 data_shndx
, reloc
.get_r_offset());
939 case elfcpp::R_386_PC32
:
940 case elfcpp::R_386_PC16
:
941 case elfcpp::R_386_PC8
:
944 case elfcpp::R_386_PLT32
:
945 // Since we know this is a local symbol, we can handle this as a
949 case elfcpp::R_386_GOTOFF
:
950 case elfcpp::R_386_GOTPC
:
951 // We need a GOT section.
952 target
->got_section(symtab
, layout
);
955 case elfcpp::R_386_GOT32
:
957 // The symbol requires a GOT entry.
958 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
959 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
960 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
962 // If we are generating a shared object, we need to add a
963 // dynamic RELATIVE relocation for this symbol's GOT entry.
964 if (parameters
->options().output_is_position_independent())
966 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
967 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
968 rel_dyn
->add_local_relative(
969 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
970 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
976 // These are relocations which should only be seen by the
977 // dynamic linker, and should never be seen here.
978 case elfcpp::R_386_COPY
:
979 case elfcpp::R_386_GLOB_DAT
:
980 case elfcpp::R_386_JUMP_SLOT
:
981 case elfcpp::R_386_RELATIVE
:
982 case elfcpp::R_386_TLS_TPOFF
:
983 case elfcpp::R_386_TLS_DTPMOD32
:
984 case elfcpp::R_386_TLS_DTPOFF32
:
985 case elfcpp::R_386_TLS_TPOFF32
:
986 case elfcpp::R_386_TLS_DESC
:
987 gold_error(_("%s: unexpected reloc %u in object file"),
988 object
->name().c_str(), r_type
);
991 // These are initial TLS relocs, which are expected when
993 case elfcpp::R_386_TLS_GD
: // Global-dynamic
994 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
995 case elfcpp::R_386_TLS_DESC_CALL
:
996 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
997 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
998 case elfcpp::R_386_TLS_IE
: // Initial-exec
999 case elfcpp::R_386_TLS_IE_32
:
1000 case elfcpp::R_386_TLS_GOTIE
:
1001 case elfcpp::R_386_TLS_LE
: // Local-exec
1002 case elfcpp::R_386_TLS_LE_32
:
1004 bool output_is_shared
= parameters
->options().shared();
1005 const tls::Tls_optimization optimized_type
1006 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1009 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1010 if (optimized_type
== tls::TLSOPT_NONE
)
1012 // Create a pair of GOT entries for the module index and
1013 // dtv-relative offset.
1014 Output_data_got
<32, false>* got
1015 = target
->got_section(symtab
, layout
);
1016 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1017 unsigned int shndx
= lsym
.get_st_shndx();
1019 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1021 object
->error(_("local symbol %u has bad shndx %u"),
1024 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1026 target
->rel_dyn_section(layout
),
1027 elfcpp::R_386_TLS_DTPMOD32
, 0);
1029 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1030 unsupported_reloc_local(object
, r_type
);
1033 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1034 target
->define_tls_base_symbol(symtab
, layout
);
1035 if (optimized_type
== tls::TLSOPT_NONE
)
1037 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1038 Output_data_got
<32, false>* got
1039 = target
->got_section(symtab
, layout
);
1040 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1041 unsigned int shndx
= lsym
.get_st_shndx();
1043 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1045 object
->error(_("local symbol %u has bad shndx %u"),
1048 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1050 target
->rel_dyn_section(layout
),
1051 elfcpp::R_386_TLS_DESC
, 0);
1053 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1054 unsupported_reloc_local(object
, r_type
);
1057 case elfcpp::R_386_TLS_DESC_CALL
:
1060 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1061 if (optimized_type
== tls::TLSOPT_NONE
)
1063 // Create a GOT entry for the module index.
1064 target
->got_mod_index_entry(symtab
, layout
, object
);
1066 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1067 unsupported_reloc_local(object
, r_type
);
1070 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1073 case elfcpp::R_386_TLS_IE
: // Initial-exec
1074 case elfcpp::R_386_TLS_IE_32
:
1075 case elfcpp::R_386_TLS_GOTIE
:
1076 layout
->set_has_static_tls();
1077 if (optimized_type
== tls::TLSOPT_NONE
)
1079 // For the R_386_TLS_IE relocation, we need to create a
1080 // dynamic relocation when building a shared library.
1081 if (r_type
== elfcpp::R_386_TLS_IE
1082 && parameters
->options().shared())
1084 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1086 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1087 rel_dyn
->add_local_relative(object
, r_sym
,
1088 elfcpp::R_386_RELATIVE
,
1089 output_section
, data_shndx
,
1090 reloc
.get_r_offset());
1092 // Create a GOT entry for the tp-relative offset.
1093 Output_data_got
<32, false>* got
1094 = target
->got_section(symtab
, layout
);
1095 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1096 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1097 ? elfcpp::R_386_TLS_TPOFF32
1098 : elfcpp::R_386_TLS_TPOFF
);
1099 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1100 ? GOT_TYPE_TLS_OFFSET
1101 : GOT_TYPE_TLS_NOFFSET
);
1102 got
->add_local_with_rel(object
, r_sym
, got_type
,
1103 target
->rel_dyn_section(layout
),
1106 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1107 unsupported_reloc_local(object
, r_type
);
1110 case elfcpp::R_386_TLS_LE
: // Local-exec
1111 case elfcpp::R_386_TLS_LE_32
:
1112 layout
->set_has_static_tls();
1113 if (output_is_shared
)
1115 // We need to create a dynamic relocation.
1116 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1117 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1118 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1119 ? elfcpp::R_386_TLS_TPOFF32
1120 : elfcpp::R_386_TLS_TPOFF
);
1121 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1122 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1123 data_shndx
, reloc
.get_r_offset());
1133 case elfcpp::R_386_32PLT
:
1134 case elfcpp::R_386_TLS_GD_32
:
1135 case elfcpp::R_386_TLS_GD_PUSH
:
1136 case elfcpp::R_386_TLS_GD_CALL
:
1137 case elfcpp::R_386_TLS_GD_POP
:
1138 case elfcpp::R_386_TLS_LDM_32
:
1139 case elfcpp::R_386_TLS_LDM_PUSH
:
1140 case elfcpp::R_386_TLS_LDM_CALL
:
1141 case elfcpp::R_386_TLS_LDM_POP
:
1142 case elfcpp::R_386_USED_BY_INTEL_200
:
1144 unsupported_reloc_local(object
, r_type
);
1149 // Report an unsupported relocation against a global symbol.
1152 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1153 unsigned int r_type
,
1156 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1157 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1160 // Scan a relocation for a global symbol.
1163 Target_i386::Scan::global(const General_options
&,
1164 Symbol_table
* symtab
,
1166 Target_i386
* target
,
1167 Sized_relobj
<32, false>* object
,
1168 unsigned int data_shndx
,
1169 Output_section
* output_section
,
1170 const elfcpp::Rel
<32, false>& reloc
,
1171 unsigned int r_type
,
1176 case elfcpp::R_386_NONE
:
1177 case elfcpp::R_386_GNU_VTINHERIT
:
1178 case elfcpp::R_386_GNU_VTENTRY
:
1181 case elfcpp::R_386_32
:
1182 case elfcpp::R_386_16
:
1183 case elfcpp::R_386_8
:
1185 // Make a PLT entry if necessary.
1186 if (gsym
->needs_plt_entry())
1188 target
->make_plt_entry(symtab
, layout
, gsym
);
1189 // Since this is not a PC-relative relocation, we may be
1190 // taking the address of a function. In that case we need to
1191 // set the entry in the dynamic symbol table to the address of
1193 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1194 gsym
->set_needs_dynsym_value();
1196 // Make a dynamic relocation if necessary.
1197 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1199 if (target
->may_need_copy_reloc(gsym
))
1201 target
->copy_reloc(symtab
, layout
, object
,
1202 data_shndx
, output_section
, gsym
, reloc
);
1204 else if (r_type
== elfcpp::R_386_32
1205 && gsym
->can_use_relative_reloc(false))
1207 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1208 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1209 output_section
, object
,
1210 data_shndx
, reloc
.get_r_offset());
1214 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1215 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1216 data_shndx
, reloc
.get_r_offset());
1222 case elfcpp::R_386_PC32
:
1223 case elfcpp::R_386_PC16
:
1224 case elfcpp::R_386_PC8
:
1226 // Make a PLT entry if necessary.
1227 if (gsym
->needs_plt_entry())
1229 // These relocations are used for function calls only in
1230 // non-PIC code. For a 32-bit relocation in a shared library,
1231 // we'll need a text relocation anyway, so we can skip the
1232 // PLT entry and let the dynamic linker bind the call directly
1233 // to the target. For smaller relocations, we should use a
1234 // PLT entry to ensure that the call can reach.
1235 if (!parameters
->options().shared()
1236 || r_type
!= elfcpp::R_386_PC32
)
1237 target
->make_plt_entry(symtab
, layout
, gsym
);
1239 // Make a dynamic relocation if necessary.
1240 int flags
= Symbol::NON_PIC_REF
;
1241 if (gsym
->type() == elfcpp::STT_FUNC
)
1242 flags
|= Symbol::FUNCTION_CALL
;
1243 if (gsym
->needs_dynamic_reloc(flags
))
1245 if (target
->may_need_copy_reloc(gsym
))
1247 target
->copy_reloc(symtab
, layout
, object
,
1248 data_shndx
, output_section
, gsym
, reloc
);
1252 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1253 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1254 data_shndx
, reloc
.get_r_offset());
1260 case elfcpp::R_386_GOT32
:
1262 // The symbol requires a GOT entry.
1263 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1264 if (gsym
->final_value_is_known())
1265 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1268 // If this symbol is not fully resolved, we need to add a
1269 // GOT entry with a dynamic relocation.
1270 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1271 if (gsym
->is_from_dynobj()
1272 || gsym
->is_undefined()
1273 || gsym
->is_preemptible())
1274 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1275 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1278 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1279 rel_dyn
->add_global_relative(
1280 gsym
, elfcpp::R_386_RELATIVE
, got
,
1281 gsym
->got_offset(GOT_TYPE_STANDARD
));
1287 case elfcpp::R_386_PLT32
:
1288 // If the symbol is fully resolved, this is just a PC32 reloc.
1289 // Otherwise we need a PLT entry.
1290 if (gsym
->final_value_is_known())
1292 // If building a shared library, we can also skip the PLT entry
1293 // if the symbol is defined in the output file and is protected
1295 if (gsym
->is_defined()
1296 && !gsym
->is_from_dynobj()
1297 && !gsym
->is_preemptible())
1299 target
->make_plt_entry(symtab
, layout
, gsym
);
1302 case elfcpp::R_386_GOTOFF
:
1303 case elfcpp::R_386_GOTPC
:
1304 // We need a GOT section.
1305 target
->got_section(symtab
, layout
);
1308 // These are relocations which should only be seen by the
1309 // dynamic linker, and should never be seen here.
1310 case elfcpp::R_386_COPY
:
1311 case elfcpp::R_386_GLOB_DAT
:
1312 case elfcpp::R_386_JUMP_SLOT
:
1313 case elfcpp::R_386_RELATIVE
:
1314 case elfcpp::R_386_TLS_TPOFF
:
1315 case elfcpp::R_386_TLS_DTPMOD32
:
1316 case elfcpp::R_386_TLS_DTPOFF32
:
1317 case elfcpp::R_386_TLS_TPOFF32
:
1318 case elfcpp::R_386_TLS_DESC
:
1319 gold_error(_("%s: unexpected reloc %u in object file"),
1320 object
->name().c_str(), r_type
);
1323 // These are initial tls relocs, which are expected when
1325 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1326 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1327 case elfcpp::R_386_TLS_DESC_CALL
:
1328 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1329 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1330 case elfcpp::R_386_TLS_IE
: // Initial-exec
1331 case elfcpp::R_386_TLS_IE_32
:
1332 case elfcpp::R_386_TLS_GOTIE
:
1333 case elfcpp::R_386_TLS_LE
: // Local-exec
1334 case elfcpp::R_386_TLS_LE_32
:
1336 const bool is_final
= gsym
->final_value_is_known();
1337 const tls::Tls_optimization optimized_type
1338 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1341 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1342 if (optimized_type
== tls::TLSOPT_NONE
)
1344 // Create a pair of GOT entries for the module index and
1345 // dtv-relative offset.
1346 Output_data_got
<32, false>* got
1347 = target
->got_section(symtab
, layout
);
1348 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1349 target
->rel_dyn_section(layout
),
1350 elfcpp::R_386_TLS_DTPMOD32
,
1351 elfcpp::R_386_TLS_DTPOFF32
);
1353 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1355 // Create a GOT entry for the tp-relative offset.
1356 Output_data_got
<32, false>* got
1357 = target
->got_section(symtab
, layout
);
1358 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1359 target
->rel_dyn_section(layout
),
1360 elfcpp::R_386_TLS_TPOFF
);
1362 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1363 unsupported_reloc_global(object
, r_type
, gsym
);
1366 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1367 target
->define_tls_base_symbol(symtab
, layout
);
1368 if (optimized_type
== tls::TLSOPT_NONE
)
1370 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1371 Output_data_got
<32, false>* got
1372 = target
->got_section(symtab
, layout
);
1373 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1374 target
->rel_dyn_section(layout
),
1375 elfcpp::R_386_TLS_DESC
, 0);
1377 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1379 // Create a GOT entry for the tp-relative offset.
1380 Output_data_got
<32, false>* got
1381 = target
->got_section(symtab
, layout
);
1382 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1383 target
->rel_dyn_section(layout
),
1384 elfcpp::R_386_TLS_TPOFF
);
1386 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1387 unsupported_reloc_global(object
, r_type
, gsym
);
1390 case elfcpp::R_386_TLS_DESC_CALL
:
1393 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1394 if (optimized_type
== tls::TLSOPT_NONE
)
1396 // Create a GOT entry for the module index.
1397 target
->got_mod_index_entry(symtab
, layout
, object
);
1399 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1400 unsupported_reloc_global(object
, r_type
, gsym
);
1403 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1406 case elfcpp::R_386_TLS_IE
: // Initial-exec
1407 case elfcpp::R_386_TLS_IE_32
:
1408 case elfcpp::R_386_TLS_GOTIE
:
1409 layout
->set_has_static_tls();
1410 if (optimized_type
== tls::TLSOPT_NONE
)
1412 // For the R_386_TLS_IE relocation, we need to create a
1413 // dynamic relocation when building a shared library.
1414 if (r_type
== elfcpp::R_386_TLS_IE
1415 && parameters
->options().shared())
1417 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1418 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1419 output_section
, object
,
1421 reloc
.get_r_offset());
1423 // Create a GOT entry for the tp-relative offset.
1424 Output_data_got
<32, false>* got
1425 = target
->got_section(symtab
, layout
);
1426 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1427 ? elfcpp::R_386_TLS_TPOFF32
1428 : elfcpp::R_386_TLS_TPOFF
);
1429 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1430 ? GOT_TYPE_TLS_OFFSET
1431 : GOT_TYPE_TLS_NOFFSET
);
1432 got
->add_global_with_rel(gsym
, got_type
,
1433 target
->rel_dyn_section(layout
),
1436 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1437 unsupported_reloc_global(object
, r_type
, gsym
);
1440 case elfcpp::R_386_TLS_LE
: // Local-exec
1441 case elfcpp::R_386_TLS_LE_32
:
1442 layout
->set_has_static_tls();
1443 if (parameters
->options().shared())
1445 // We need to create a dynamic relocation.
1446 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1447 ? elfcpp::R_386_TLS_TPOFF32
1448 : elfcpp::R_386_TLS_TPOFF
);
1449 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1450 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1451 data_shndx
, reloc
.get_r_offset());
1461 case elfcpp::R_386_32PLT
:
1462 case elfcpp::R_386_TLS_GD_32
:
1463 case elfcpp::R_386_TLS_GD_PUSH
:
1464 case elfcpp::R_386_TLS_GD_CALL
:
1465 case elfcpp::R_386_TLS_GD_POP
:
1466 case elfcpp::R_386_TLS_LDM_32
:
1467 case elfcpp::R_386_TLS_LDM_PUSH
:
1468 case elfcpp::R_386_TLS_LDM_CALL
:
1469 case elfcpp::R_386_TLS_LDM_POP
:
1470 case elfcpp::R_386_USED_BY_INTEL_200
:
1472 unsupported_reloc_global(object
, r_type
, gsym
);
1477 // Process relocations for gc.
1480 Target_i386::gc_process_relocs(const General_options
& options
,
1481 Symbol_table
* symtab
,
1483 Sized_relobj
<32, false>* object
,
1484 unsigned int data_shndx
,
1486 const unsigned char* prelocs
,
1488 Output_section
* output_section
,
1489 bool needs_special_offset_handling
,
1490 size_t local_symbol_count
,
1491 const unsigned char* plocal_symbols
)
1493 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1504 needs_special_offset_handling
,
1509 // Scan relocations for a section.
1512 Target_i386::scan_relocs(const General_options
& options
,
1513 Symbol_table
* symtab
,
1515 Sized_relobj
<32, false>* object
,
1516 unsigned int data_shndx
,
1517 unsigned int sh_type
,
1518 const unsigned char* prelocs
,
1520 Output_section
* output_section
,
1521 bool needs_special_offset_handling
,
1522 size_t local_symbol_count
,
1523 const unsigned char* plocal_symbols
)
1525 if (sh_type
== elfcpp::SHT_RELA
)
1527 gold_error(_("%s: unsupported RELA reloc section"),
1528 object
->name().c_str());
1532 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1543 needs_special_offset_handling
,
1548 // Finalize the sections.
1551 Target_i386::do_finalize_sections(Layout
* layout
)
1553 // Fill in some more dynamic tags.
1554 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1557 if (this->got_plt_
!= NULL
)
1558 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1560 if (this->plt_
!= NULL
)
1562 const Output_data
* od
= this->plt_
->rel_plt();
1563 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1564 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1565 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1568 if (this->rel_dyn_
!= NULL
)
1570 const Output_data
* od
= this->rel_dyn_
;
1571 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1572 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1573 odyn
->add_constant(elfcpp::DT_RELENT
,
1574 elfcpp::Elf_sizes
<32>::rel_size
);
1577 if (!parameters
->options().shared())
1579 // The value of the DT_DEBUG tag is filled in by the dynamic
1580 // linker at run time, and used by the debugger.
1581 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1585 // Emit any relocs we saved in an attempt to avoid generating COPY
1587 if (this->copy_relocs_
.any_saved_relocs())
1588 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1591 // Return whether a direct absolute static relocation needs to be applied.
1592 // In cases where Scan::local() or Scan::global() has created
1593 // a dynamic relocation other than R_386_RELATIVE, the addend
1594 // of the relocation is carried in the data, and we must not
1595 // apply the static relocation.
1598 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1601 Output_section
* output_section
)
1603 // If the output section is not allocated, then we didn't call
1604 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1606 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1609 // For local symbols, we will have created a non-RELATIVE dynamic
1610 // relocation only if (a) the output is position independent,
1611 // (b) the relocation is absolute (not pc- or segment-relative), and
1612 // (c) the relocation is not 32 bits wide.
1614 return !(parameters
->options().output_is_position_independent()
1615 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1618 // For global symbols, we use the same helper routines used in the
1619 // scan pass. If we did not create a dynamic relocation, or if we
1620 // created a RELATIVE dynamic relocation, we should apply the static
1622 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1623 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1624 && gsym
->can_use_relative_reloc(ref_flags
1625 & Symbol::FUNCTION_CALL
);
1626 return !has_dyn
|| is_rel
;
1629 // Perform a relocation.
1632 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1633 Target_i386
* target
,
1634 Output_section
*output_section
,
1636 const elfcpp::Rel
<32, false>& rel
,
1637 unsigned int r_type
,
1638 const Sized_symbol
<32>* gsym
,
1639 const Symbol_value
<32>* psymval
,
1640 unsigned char* view
,
1641 elfcpp::Elf_types
<32>::Elf_Addr address
,
1642 section_size_type view_size
)
1644 if (this->skip_call_tls_get_addr_
)
1646 if ((r_type
!= elfcpp::R_386_PLT32
1647 && r_type
!= elfcpp::R_386_PC32
)
1649 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1650 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1651 _("missing expected TLS relocation"));
1654 this->skip_call_tls_get_addr_
= false;
1659 // Pick the value to use for symbols defined in shared objects.
1660 Symbol_value
<32> symval
;
1662 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1663 || r_type
== elfcpp::R_386_PC16
1664 || r_type
== elfcpp::R_386_PC32
))
1666 symval
.set_output_value(target
->plt_section()->address()
1667 + gsym
->plt_offset());
1671 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1673 // Get the GOT offset if needed.
1674 // The GOT pointer points to the end of the GOT section.
1675 // We need to subtract the size of the GOT section to get
1676 // the actual offset to use in the relocation.
1677 bool have_got_offset
= false;
1678 unsigned int got_offset
= 0;
1681 case elfcpp::R_386_GOT32
:
1684 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1685 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1686 - target
->got_size());
1690 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1691 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1692 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1693 - target
->got_size());
1695 have_got_offset
= true;
1704 case elfcpp::R_386_NONE
:
1705 case elfcpp::R_386_GNU_VTINHERIT
:
1706 case elfcpp::R_386_GNU_VTENTRY
:
1709 case elfcpp::R_386_32
:
1710 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1712 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1715 case elfcpp::R_386_PC32
:
1717 int ref_flags
= Symbol::NON_PIC_REF
;
1718 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1719 ref_flags
|= Symbol::FUNCTION_CALL
;
1720 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1721 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1725 case elfcpp::R_386_16
:
1726 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1728 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1731 case elfcpp::R_386_PC16
:
1733 int ref_flags
= Symbol::NON_PIC_REF
;
1734 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1735 ref_flags
|= Symbol::FUNCTION_CALL
;
1736 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1737 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1741 case elfcpp::R_386_8
:
1742 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1744 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1747 case elfcpp::R_386_PC8
:
1749 int ref_flags
= Symbol::NON_PIC_REF
;
1750 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1751 ref_flags
|= Symbol::FUNCTION_CALL
;
1752 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1754 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1758 case elfcpp::R_386_PLT32
:
1759 gold_assert(gsym
== NULL
1760 || gsym
->has_plt_offset()
1761 || gsym
->final_value_is_known()
1762 || (gsym
->is_defined()
1763 && !gsym
->is_from_dynobj()
1764 && !gsym
->is_preemptible()));
1765 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1768 case elfcpp::R_386_GOT32
:
1769 gold_assert(have_got_offset
);
1770 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1773 case elfcpp::R_386_GOTOFF
:
1775 elfcpp::Elf_types
<32>::Elf_Addr value
;
1776 value
= (psymval
->value(object
, 0)
1777 - target
->got_plt_section()->address());
1778 Relocate_functions
<32, false>::rel32(view
, value
);
1782 case elfcpp::R_386_GOTPC
:
1784 elfcpp::Elf_types
<32>::Elf_Addr value
;
1785 value
= target
->got_plt_section()->address();
1786 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1790 case elfcpp::R_386_COPY
:
1791 case elfcpp::R_386_GLOB_DAT
:
1792 case elfcpp::R_386_JUMP_SLOT
:
1793 case elfcpp::R_386_RELATIVE
:
1794 // These are outstanding tls relocs, which are unexpected when
1796 case elfcpp::R_386_TLS_TPOFF
:
1797 case elfcpp::R_386_TLS_DTPMOD32
:
1798 case elfcpp::R_386_TLS_DTPOFF32
:
1799 case elfcpp::R_386_TLS_TPOFF32
:
1800 case elfcpp::R_386_TLS_DESC
:
1801 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1802 _("unexpected reloc %u in object file"),
1806 // These are initial tls relocs, which are expected when
1808 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1809 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1810 case elfcpp::R_386_TLS_DESC_CALL
:
1811 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1812 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1813 case elfcpp::R_386_TLS_IE
: // Initial-exec
1814 case elfcpp::R_386_TLS_IE_32
:
1815 case elfcpp::R_386_TLS_GOTIE
:
1816 case elfcpp::R_386_TLS_LE
: // Local-exec
1817 case elfcpp::R_386_TLS_LE_32
:
1818 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1819 view
, address
, view_size
);
1822 case elfcpp::R_386_32PLT
:
1823 case elfcpp::R_386_TLS_GD_32
:
1824 case elfcpp::R_386_TLS_GD_PUSH
:
1825 case elfcpp::R_386_TLS_GD_CALL
:
1826 case elfcpp::R_386_TLS_GD_POP
:
1827 case elfcpp::R_386_TLS_LDM_32
:
1828 case elfcpp::R_386_TLS_LDM_PUSH
:
1829 case elfcpp::R_386_TLS_LDM_CALL
:
1830 case elfcpp::R_386_TLS_LDM_POP
:
1831 case elfcpp::R_386_USED_BY_INTEL_200
:
1833 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1834 _("unsupported reloc %u"),
1842 // Perform a TLS relocation.
1845 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1846 Target_i386
* target
,
1848 const elfcpp::Rel
<32, false>& rel
,
1849 unsigned int r_type
,
1850 const Sized_symbol
<32>* gsym
,
1851 const Symbol_value
<32>* psymval
,
1852 unsigned char* view
,
1853 elfcpp::Elf_types
<32>::Elf_Addr
,
1854 section_size_type view_size
)
1856 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1858 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1860 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1862 const bool is_final
=
1864 ? !parameters
->options().output_is_position_independent()
1865 : gsym
->final_value_is_known());
1866 const tls::Tls_optimization optimized_type
1867 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1870 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1871 if (optimized_type
== tls::TLSOPT_TO_LE
)
1873 gold_assert(tls_segment
!= NULL
);
1874 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1875 rel
, r_type
, value
, view
,
1881 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1882 ? GOT_TYPE_TLS_NOFFSET
1883 : GOT_TYPE_TLS_PAIR
);
1884 unsigned int got_offset
;
1887 gold_assert(gsym
->has_got_offset(got_type
));
1888 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1892 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1893 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1894 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1895 - target
->got_size());
1897 if (optimized_type
== tls::TLSOPT_TO_IE
)
1899 gold_assert(tls_segment
!= NULL
);
1900 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1901 got_offset
, view
, view_size
);
1904 else if (optimized_type
== tls::TLSOPT_NONE
)
1906 // Relocate the field with the offset of the pair of GOT
1908 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1912 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1913 _("unsupported reloc %u"),
1917 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1918 case elfcpp::R_386_TLS_DESC_CALL
:
1919 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1920 if (optimized_type
== tls::TLSOPT_TO_LE
)
1922 gold_assert(tls_segment
!= NULL
);
1923 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1924 rel
, r_type
, value
, view
,
1930 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1931 ? GOT_TYPE_TLS_NOFFSET
1932 : GOT_TYPE_TLS_DESC
);
1933 unsigned int got_offset
;
1936 gold_assert(gsym
->has_got_offset(got_type
));
1937 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1941 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1942 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1943 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1944 - target
->got_size());
1946 if (optimized_type
== tls::TLSOPT_TO_IE
)
1948 gold_assert(tls_segment
!= NULL
);
1949 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1950 got_offset
, view
, view_size
);
1953 else if (optimized_type
== tls::TLSOPT_NONE
)
1955 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1957 // Relocate the field with the offset of the pair of GOT
1959 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1964 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1965 _("unsupported reloc %u"),
1969 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1970 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1972 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1973 _("both SUN and GNU model "
1974 "TLS relocations"));
1977 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1978 if (optimized_type
== tls::TLSOPT_TO_LE
)
1980 gold_assert(tls_segment
!= NULL
);
1981 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1982 value
, view
, view_size
);
1985 else if (optimized_type
== tls::TLSOPT_NONE
)
1987 // Relocate the field with the offset of the GOT entry for
1988 // the module index.
1989 unsigned int got_offset
;
1990 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1991 - target
->got_size());
1992 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1995 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1996 _("unsupported reloc %u"),
2000 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2001 // This reloc can appear in debugging sections, in which case we
2002 // won't see the TLS_LDM reloc. The local_dynamic_type field
2004 if (optimized_type
== tls::TLSOPT_TO_LE
2005 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
2007 gold_assert(tls_segment
!= NULL
);
2008 value
-= tls_segment
->memsz();
2010 Relocate_functions
<32, false>::rel32(view
, value
);
2013 case elfcpp::R_386_TLS_IE
: // Initial-exec
2014 case elfcpp::R_386_TLS_GOTIE
:
2015 case elfcpp::R_386_TLS_IE_32
:
2016 if (optimized_type
== tls::TLSOPT_TO_LE
)
2018 gold_assert(tls_segment
!= NULL
);
2019 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2020 rel
, r_type
, value
, view
,
2024 else if (optimized_type
== tls::TLSOPT_NONE
)
2026 // Relocate the field with the offset of the GOT entry for
2027 // the tp-relative offset of the symbol.
2028 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2029 ? GOT_TYPE_TLS_OFFSET
2030 : GOT_TYPE_TLS_NOFFSET
);
2031 unsigned int got_offset
;
2034 gold_assert(gsym
->has_got_offset(got_type
));
2035 got_offset
= gsym
->got_offset(got_type
);
2039 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2040 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2041 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2043 // For the R_386_TLS_IE relocation, we need to apply the
2044 // absolute address of the GOT entry.
2045 if (r_type
== elfcpp::R_386_TLS_IE
)
2046 got_offset
+= target
->got_plt_section()->address();
2047 // All GOT offsets are relative to the end of the GOT.
2048 got_offset
-= target
->got_size();
2049 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2052 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2053 _("unsupported reloc %u"),
2057 case elfcpp::R_386_TLS_LE
: // Local-exec
2058 // If we're creating a shared library, a dynamic relocation will
2059 // have been created for this location, so do not apply it now.
2060 if (!parameters
->options().shared())
2062 gold_assert(tls_segment
!= NULL
);
2063 value
-= tls_segment
->memsz();
2064 Relocate_functions
<32, false>::rel32(view
, value
);
2068 case elfcpp::R_386_TLS_LE_32
:
2069 // If we're creating a shared library, a dynamic relocation will
2070 // have been created for this location, so do not apply it now.
2071 if (!parameters
->options().shared())
2073 gold_assert(tls_segment
!= NULL
);
2074 value
= tls_segment
->memsz() - value
;
2075 Relocate_functions
<32, false>::rel32(view
, value
);
2081 // Do a relocation in which we convert a TLS General-Dynamic to a
2085 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2087 Output_segment
* tls_segment
,
2088 const elfcpp::Rel
<32, false>& rel
,
2090 elfcpp::Elf_types
<32>::Elf_Addr value
,
2091 unsigned char* view
,
2092 section_size_type view_size
)
2094 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2095 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2096 // leal foo(%reg),%eax; call ___tls_get_addr
2097 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2099 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2100 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2102 unsigned char op1
= view
[-1];
2103 unsigned char op2
= view
[-2];
2105 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2106 op2
== 0x8d || op2
== 0x04);
2107 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2113 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2114 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2115 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2116 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2117 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2121 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2122 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2123 if (rel
.get_r_offset() + 9 < view_size
2126 // There is a trailing nop. Use the size byte subl.
2127 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2132 // Use the five byte subl.
2133 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2137 value
= tls_segment
->memsz() - value
;
2138 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2140 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2142 this->skip_call_tls_get_addr_
= true;
2145 // Do a relocation in which we convert a TLS General-Dynamic to an
2149 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2152 const elfcpp::Rel
<32, false>& rel
,
2154 elfcpp::Elf_types
<32>::Elf_Addr value
,
2155 unsigned char* view
,
2156 section_size_type view_size
)
2158 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2159 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2161 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2162 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2164 unsigned char op1
= view
[-1];
2165 unsigned char op2
= view
[-2];
2167 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2168 op2
== 0x8d || op2
== 0x04);
2169 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2173 // FIXME: For now, support only the first (SIB) form.
2174 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2178 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2179 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2180 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2181 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2182 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2186 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2187 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2188 if (rel
.get_r_offset() + 9 < view_size
2191 // FIXME: This is not the right instruction sequence.
2192 // There is a trailing nop. Use the size byte subl.
2193 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2198 // FIXME: This is not the right instruction sequence.
2199 // Use the five byte subl.
2200 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2204 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2206 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2208 this->skip_call_tls_get_addr_
= true;
2211 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2212 // General-Dynamic to a Local-Exec.
2215 Target_i386::Relocate::tls_desc_gd_to_le(
2216 const Relocate_info
<32, false>* relinfo
,
2218 Output_segment
* tls_segment
,
2219 const elfcpp::Rel
<32, false>& rel
,
2220 unsigned int r_type
,
2221 elfcpp::Elf_types
<32>::Elf_Addr value
,
2222 unsigned char* view
,
2223 section_size_type view_size
)
2225 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2227 // leal foo@TLSDESC(%ebx), %eax
2228 // ==> leal foo@NTPOFF, %eax
2229 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2230 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2231 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2232 view
[-2] == 0x8d && view
[-1] == 0x83);
2234 value
-= tls_segment
->memsz();
2235 Relocate_functions
<32, false>::rel32(view
, value
);
2239 // call *foo@TLSCALL(%eax)
2241 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2242 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2243 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2244 view
[0] == 0xff && view
[1] == 0x10);
2250 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2251 // General-Dynamic to an Initial-Exec.
2254 Target_i386::Relocate::tls_desc_gd_to_ie(
2255 const Relocate_info
<32, false>* relinfo
,
2258 const elfcpp::Rel
<32, false>& rel
,
2259 unsigned int r_type
,
2260 elfcpp::Elf_types
<32>::Elf_Addr value
,
2261 unsigned char* view
,
2262 section_size_type view_size
)
2264 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2266 // leal foo@TLSDESC(%ebx), %eax
2267 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2268 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2269 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2270 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2271 view
[-2] == 0x8d && view
[-1] == 0x83);
2273 Relocate_functions
<32, false>::rel32(view
, value
);
2277 // call *foo@TLSCALL(%eax)
2279 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2280 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2281 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2282 view
[0] == 0xff && view
[1] == 0x10);
2288 // Do a relocation in which we convert a TLS Local-Dynamic to a
2292 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2295 const elfcpp::Rel
<32, false>& rel
,
2297 elfcpp::Elf_types
<32>::Elf_Addr
,
2298 unsigned char* view
,
2299 section_size_type view_size
)
2301 // leal foo(%reg), %eax; call ___tls_get_addr
2302 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2304 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2305 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2307 // FIXME: Does this test really always pass?
2308 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2309 view
[-2] == 0x8d && view
[-1] == 0x83);
2311 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2313 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2315 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2317 this->skip_call_tls_get_addr_
= true;
2320 // Do a relocation in which we convert a TLS Initial-Exec to a
2324 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2326 Output_segment
* tls_segment
,
2327 const elfcpp::Rel
<32, false>& rel
,
2328 unsigned int r_type
,
2329 elfcpp::Elf_types
<32>::Elf_Addr value
,
2330 unsigned char* view
,
2331 section_size_type view_size
)
2333 // We have to actually change the instructions, which means that we
2334 // need to examine the opcodes to figure out which instruction we
2336 if (r_type
== elfcpp::R_386_TLS_IE
)
2338 // movl %gs:XX,%eax ==> movl $YY,%eax
2339 // movl %gs:XX,%reg ==> movl $YY,%reg
2340 // addl %gs:XX,%reg ==> addl $YY,%reg
2341 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2342 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2344 unsigned char op1
= view
[-1];
2347 // movl XX,%eax ==> movl $YY,%eax
2352 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2354 unsigned char op2
= view
[-2];
2357 // movl XX,%reg ==> movl $YY,%reg
2358 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2359 (op1
& 0xc7) == 0x05);
2361 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2363 else if (op2
== 0x03)
2365 // addl XX,%reg ==> addl $YY,%reg
2366 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2367 (op1
& 0xc7) == 0x05);
2369 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2372 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2377 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2378 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2379 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2380 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2381 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2383 unsigned char op1
= view
[-1];
2384 unsigned char op2
= view
[-2];
2385 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2386 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2389 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2391 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2393 else if (op2
== 0x2b)
2395 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2397 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2399 else if (op2
== 0x03)
2401 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2403 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2406 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2409 value
= tls_segment
->memsz() - value
;
2410 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2413 Relocate_functions
<32, false>::rel32(view
, value
);
2416 // Relocate section data.
2419 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2420 unsigned int sh_type
,
2421 const unsigned char* prelocs
,
2423 Output_section
* output_section
,
2424 bool needs_special_offset_handling
,
2425 unsigned char* view
,
2426 elfcpp::Elf_types
<32>::Elf_Addr address
,
2427 section_size_type view_size
)
2429 gold_assert(sh_type
== elfcpp::SHT_REL
);
2431 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2432 Target_i386::Relocate
>(
2438 needs_special_offset_handling
,
2444 // Return the size of a relocation while scanning during a relocatable
2448 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2449 unsigned int r_type
,
2454 case elfcpp::R_386_NONE
:
2455 case elfcpp::R_386_GNU_VTINHERIT
:
2456 case elfcpp::R_386_GNU_VTENTRY
:
2457 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2458 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2459 case elfcpp::R_386_TLS_DESC_CALL
:
2460 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2461 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2462 case elfcpp::R_386_TLS_IE
: // Initial-exec
2463 case elfcpp::R_386_TLS_IE_32
:
2464 case elfcpp::R_386_TLS_GOTIE
:
2465 case elfcpp::R_386_TLS_LE
: // Local-exec
2466 case elfcpp::R_386_TLS_LE_32
:
2469 case elfcpp::R_386_32
:
2470 case elfcpp::R_386_PC32
:
2471 case elfcpp::R_386_GOT32
:
2472 case elfcpp::R_386_PLT32
:
2473 case elfcpp::R_386_GOTOFF
:
2474 case elfcpp::R_386_GOTPC
:
2477 case elfcpp::R_386_16
:
2478 case elfcpp::R_386_PC16
:
2481 case elfcpp::R_386_8
:
2482 case elfcpp::R_386_PC8
:
2485 // These are relocations which should only be seen by the
2486 // dynamic linker, and should never be seen here.
2487 case elfcpp::R_386_COPY
:
2488 case elfcpp::R_386_GLOB_DAT
:
2489 case elfcpp::R_386_JUMP_SLOT
:
2490 case elfcpp::R_386_RELATIVE
:
2491 case elfcpp::R_386_TLS_TPOFF
:
2492 case elfcpp::R_386_TLS_DTPMOD32
:
2493 case elfcpp::R_386_TLS_DTPOFF32
:
2494 case elfcpp::R_386_TLS_TPOFF32
:
2495 case elfcpp::R_386_TLS_DESC
:
2496 object
->error(_("unexpected reloc %u in object file"), r_type
);
2499 case elfcpp::R_386_32PLT
:
2500 case elfcpp::R_386_TLS_GD_32
:
2501 case elfcpp::R_386_TLS_GD_PUSH
:
2502 case elfcpp::R_386_TLS_GD_CALL
:
2503 case elfcpp::R_386_TLS_GD_POP
:
2504 case elfcpp::R_386_TLS_LDM_32
:
2505 case elfcpp::R_386_TLS_LDM_PUSH
:
2506 case elfcpp::R_386_TLS_LDM_CALL
:
2507 case elfcpp::R_386_TLS_LDM_POP
:
2508 case elfcpp::R_386_USED_BY_INTEL_200
:
2510 object
->error(_("unsupported reloc %u in object file"), r_type
);
2515 // Scan the relocs during a relocatable link.
2518 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2519 Symbol_table
* symtab
,
2521 Sized_relobj
<32, false>* object
,
2522 unsigned int data_shndx
,
2523 unsigned int sh_type
,
2524 const unsigned char* prelocs
,
2526 Output_section
* output_section
,
2527 bool needs_special_offset_handling
,
2528 size_t local_symbol_count
,
2529 const unsigned char* plocal_symbols
,
2530 Relocatable_relocs
* rr
)
2532 gold_assert(sh_type
== elfcpp::SHT_REL
);
2534 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2535 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2537 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2538 Scan_relocatable_relocs
>(
2547 needs_special_offset_handling
,
2553 // Relocate a section during a relocatable link.
2556 Target_i386::relocate_for_relocatable(
2557 const Relocate_info
<32, false>* relinfo
,
2558 unsigned int sh_type
,
2559 const unsigned char* prelocs
,
2561 Output_section
* output_section
,
2562 off_t offset_in_output_section
,
2563 const Relocatable_relocs
* rr
,
2564 unsigned char* view
,
2565 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2566 section_size_type view_size
,
2567 unsigned char* reloc_view
,
2568 section_size_type reloc_view_size
)
2570 gold_assert(sh_type
== elfcpp::SHT_REL
);
2572 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2577 offset_in_output_section
,
2586 // Return the value to use for a dynamic which requires special
2587 // treatment. This is how we support equality comparisons of function
2588 // pointers across shared library boundaries, as described in the
2589 // processor specific ABI supplement.
2592 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2594 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2595 return this->plt_section()->address() + gsym
->plt_offset();
2598 // Return a string used to fill a code section with nops to take up
2599 // the specified length.
2602 Target_i386::do_code_fill(section_size_type length
) const
2606 // Build a jmp instruction to skip over the bytes.
2607 unsigned char jmp
[5];
2609 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2610 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2611 + std::string(length
- 5, '\0'));
2614 // Nop sequences of various lengths.
2615 const char nop1
[1] = { 0x90 }; // nop
2616 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2617 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2618 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2619 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2620 0x00 }; // leal 0(%esi,1),%esi
2621 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2623 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2625 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2626 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2627 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2628 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2630 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2631 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2633 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2634 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2636 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2637 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2638 0x00, 0x00, 0x00, 0x00 };
2639 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2640 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2641 0x27, 0x00, 0x00, 0x00,
2643 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2644 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2645 0xbc, 0x27, 0x00, 0x00,
2647 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2648 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2649 0x90, 0x90, 0x90, 0x90,
2652 const char* nops
[16] = {
2654 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2655 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2658 return std::string(nops
[length
], length
);
2661 // The selector for i386 object files.
2663 class Target_selector_i386
: public Target_selector_freebsd
2666 Target_selector_i386()
2667 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
2668 "elf32-i386", "elf32-i386-freebsd")
2672 do_instantiate_target()
2673 { return new Target_i386(); }
2676 Target_selector_i386 target_selector_i386
;
2678 } // End anonymous namespace.