1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
47 class Output_data_plt_i386
;
49 // The i386 target class.
50 // TLS info comes from
51 // http://people.redhat.com/drepper/tls.pdf
52 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
54 class Target_i386
: public Target_freebsd
<32, false>
57 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
60 : Target_freebsd
<32, false>(&i386_info
),
61 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
62 copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
63 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
66 // Process the relocations to determine unreferenced sections for
67 // garbage collection.
69 gc_process_relocs(const General_options
& options
,
72 Sized_relobj
<32, false>* object
,
73 unsigned int data_shndx
,
75 const unsigned char* prelocs
,
77 Output_section
* output_section
,
78 bool needs_special_offset_handling
,
79 size_t local_symbol_count
,
80 const unsigned char* plocal_symbols
);
82 // Scan the relocations to look for symbol adjustments.
84 scan_relocs(const General_options
& options
,
87 Sized_relobj
<32, false>* object
,
88 unsigned int data_shndx
,
90 const unsigned char* prelocs
,
92 Output_section
* output_section
,
93 bool needs_special_offset_handling
,
94 size_t local_symbol_count
,
95 const unsigned char* plocal_symbols
);
97 // Finalize the sections.
99 do_finalize_sections(Layout
*);
101 // Return the value to use for a dynamic which requires special
104 do_dynsym_value(const Symbol
*) const;
106 // Relocate a section.
108 relocate_section(const Relocate_info
<32, false>*,
109 unsigned int sh_type
,
110 const unsigned char* prelocs
,
112 Output_section
* output_section
,
113 bool needs_special_offset_handling
,
115 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
116 section_size_type view_size
);
118 // Scan the relocs during a relocatable link.
120 scan_relocatable_relocs(const General_options
& options
,
121 Symbol_table
* symtab
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 unsigned int sh_type
,
126 const unsigned char* prelocs
,
128 Output_section
* output_section
,
129 bool needs_special_offset_handling
,
130 size_t local_symbol_count
,
131 const unsigned char* plocal_symbols
,
132 Relocatable_relocs
*);
134 // Relocate a section during a relocatable link.
136 relocate_for_relocatable(const Relocate_info
<32, false>*,
137 unsigned int sh_type
,
138 const unsigned char* prelocs
,
140 Output_section
* output_section
,
141 off_t offset_in_output_section
,
142 const Relocatable_relocs
*,
144 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
145 section_size_type view_size
,
146 unsigned char* reloc_view
,
147 section_size_type reloc_view_size
);
149 // Return a string used to fill a code section with nops.
151 do_code_fill(section_size_type length
) const;
153 // Return whether SYM is defined by the ABI.
155 do_is_defined_by_abi(const Symbol
* sym
) const
156 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
158 // Return whether a symbol name implies a local label. The UnixWare
159 // 2.1 cc generates temporary symbols that start with .X, so we
160 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
161 // If so, we should move the .X recognition into
162 // Target::do_is_local_label_name.
164 do_is_local_label_name(const char* name
) const
166 if (name
[0] == '.' && name
[1] == 'X')
168 return Target::do_is_local_label_name(name
);
171 // Return the size of the GOT section.
175 gold_assert(this->got_
!= NULL
);
176 return this->got_
->data_size();
180 // The class which scans relocations.
184 local(const General_options
& options
, Symbol_table
* symtab
,
185 Layout
* layout
, Target_i386
* target
,
186 Sized_relobj
<32, false>* object
,
187 unsigned int data_shndx
,
188 Output_section
* output_section
,
189 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
190 const elfcpp::Sym
<32, false>& lsym
);
193 global(const General_options
& options
, Symbol_table
* symtab
,
194 Layout
* layout
, Target_i386
* target
,
195 Sized_relobj
<32, false>* object
,
196 unsigned int data_shndx
,
197 Output_section
* output_section
,
198 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
202 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
205 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
209 // The class which implements relocation.
214 : skip_call_tls_get_addr_(false),
215 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
220 if (this->skip_call_tls_get_addr_
)
222 // FIXME: This needs to specify the location somehow.
223 gold_error(_("missing expected TLS relocation"));
227 // Return whether the static relocation needs to be applied.
229 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
232 Output_section
* output_section
);
234 // Do a relocation. Return false if the caller should not issue
235 // any warnings about this relocation.
237 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
238 size_t relnum
, const elfcpp::Rel
<32, false>&,
239 unsigned int r_type
, const Sized_symbol
<32>*,
240 const Symbol_value
<32>*,
241 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
245 // Do a TLS relocation.
247 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
248 size_t relnum
, const elfcpp::Rel
<32, false>&,
249 unsigned int r_type
, const Sized_symbol
<32>*,
250 const Symbol_value
<32>*,
251 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
254 // Do a TLS General-Dynamic to Initial-Exec transition.
256 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
257 Output_segment
* tls_segment
,
258 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
259 elfcpp::Elf_types
<32>::Elf_Addr value
,
261 section_size_type view_size
);
263 // Do a TLS General-Dynamic to Local-Exec transition.
265 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
266 Output_segment
* tls_segment
,
267 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
268 elfcpp::Elf_types
<32>::Elf_Addr value
,
270 section_size_type view_size
);
272 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
275 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
276 Output_segment
* tls_segment
,
277 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
278 elfcpp::Elf_types
<32>::Elf_Addr value
,
280 section_size_type view_size
);
282 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
285 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
286 Output_segment
* tls_segment
,
287 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
288 elfcpp::Elf_types
<32>::Elf_Addr value
,
290 section_size_type view_size
);
292 // Do a TLS Local-Dynamic to Local-Exec transition.
294 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
295 Output_segment
* tls_segment
,
296 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
297 elfcpp::Elf_types
<32>::Elf_Addr value
,
299 section_size_type view_size
);
301 // Do a TLS Initial-Exec to Local-Exec transition.
303 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
304 Output_segment
* tls_segment
,
305 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
306 elfcpp::Elf_types
<32>::Elf_Addr value
,
308 section_size_type view_size
);
310 // We need to keep track of which type of local dynamic relocation
311 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
312 enum Local_dynamic_type
319 // This is set if we should skip the next reloc, which should be a
320 // PLT32 reloc against ___tls_get_addr.
321 bool skip_call_tls_get_addr_
;
322 // The type of local dynamic relocation we have seen in the section
323 // being relocated, if any.
324 Local_dynamic_type local_dynamic_type_
;
327 // A class which returns the size required for a relocation type,
328 // used while scanning relocs during a relocatable link.
329 class Relocatable_size_for_reloc
333 get_size_for_reloc(unsigned int, Relobj
*);
336 // Adjust TLS relocation type based on the options and whether this
337 // is a local symbol.
338 static tls::Tls_optimization
339 optimize_tls_reloc(bool is_final
, int r_type
);
341 // Get the GOT section, creating it if necessary.
342 Output_data_got
<32, false>*
343 got_section(Symbol_table
*, Layout
*);
345 // Get the GOT PLT section.
347 got_plt_section() const
349 gold_assert(this->got_plt_
!= NULL
);
350 return this->got_plt_
;
353 // Create a PLT entry for a global symbol.
355 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
357 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
359 define_tls_base_symbol(Symbol_table
*, Layout
*);
361 // Create a GOT entry for the TLS module index.
363 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
364 Sized_relobj
<32, false>* object
);
366 // Get the PLT section.
367 const Output_data_plt_i386
*
370 gold_assert(this->plt_
!= NULL
);
374 // Get the dynamic reloc section, creating it if necessary.
376 rel_dyn_section(Layout
*);
378 // Return true if the symbol may need a COPY relocation.
379 // References from an executable object to non-function symbols
380 // defined in a dynamic object may need a COPY relocation.
382 may_need_copy_reloc(Symbol
* gsym
)
384 return (!parameters
->options().shared()
385 && gsym
->is_from_dynobj()
386 && gsym
->type() != elfcpp::STT_FUNC
);
389 // Add a potential copy relocation.
391 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
392 Sized_relobj
<32, false>* object
,
393 unsigned int shndx
, Output_section
* output_section
,
394 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
396 this->copy_relocs_
.copy_reloc(symtab
, layout
,
397 symtab
->get_sized_symbol
<32>(sym
),
398 object
, shndx
, output_section
, reloc
,
399 this->rel_dyn_section(layout
));
402 // Information about this specific target which we pass to the
403 // general Target structure.
404 static const Target::Target_info i386_info
;
406 // The types of GOT entries needed for this platform.
409 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
410 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
411 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
412 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
413 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
417 Output_data_got
<32, false>* got_
;
419 Output_data_plt_i386
* plt_
;
420 // The GOT PLT section.
421 Output_data_space
* got_plt_
;
422 // The dynamic reloc section.
423 Reloc_section
* rel_dyn_
;
424 // Relocs saved to avoid a COPY reloc.
425 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
426 // Space for variables copied with a COPY reloc.
427 Output_data_space
* dynbss_
;
428 // Offset of the GOT entry for the TLS module index.
429 unsigned int got_mod_index_offset_
;
430 // True if the _TLS_MODULE_BASE_ symbol has been defined.
431 bool tls_base_symbol_defined_
;
434 const Target::Target_info
Target_i386::i386_info
=
437 false, // is_big_endian
438 elfcpp::EM_386
, // machine_code
439 false, // has_make_symbol
440 false, // has_resolve
441 true, // has_code_fill
442 true, // is_default_stack_executable
444 "/usr/lib/libc.so.1", // dynamic_linker
445 0x08048000, // default_text_segment_address
446 0x1000, // abi_pagesize (overridable by -z max-page-size)
447 0x1000, // common_pagesize (overridable by -z common-page-size)
448 elfcpp::SHN_UNDEF
, // small_common_shndx
449 elfcpp::SHN_UNDEF
, // large_common_shndx
450 0, // small_common_section_flags
451 0 // large_common_section_flags
454 // Get the GOT section, creating it if necessary.
456 Output_data_got
<32, false>*
457 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
459 if (this->got_
== NULL
)
461 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
463 this->got_
= new Output_data_got
<32, false>();
466 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
468 | elfcpp::SHF_WRITE
),
472 // The old GNU linker creates a .got.plt section. We just
473 // create another set of data in the .got section. Note that we
474 // always create a PLT if we create a GOT, although the PLT
476 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
477 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
479 | elfcpp::SHF_WRITE
),
483 // The first three entries are reserved.
484 this->got_plt_
->set_current_data_size(3 * 4);
486 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
487 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
489 0, 0, elfcpp::STT_OBJECT
,
491 elfcpp::STV_HIDDEN
, 0,
498 // Get the dynamic reloc section, creating it if necessary.
500 Target_i386::Reloc_section
*
501 Target_i386::rel_dyn_section(Layout
* layout
)
503 if (this->rel_dyn_
== NULL
)
505 gold_assert(layout
!= NULL
);
506 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
507 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
508 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
510 return this->rel_dyn_
;
513 // A class to handle the PLT data.
515 class Output_data_plt_i386
: public Output_section_data
518 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
520 Output_data_plt_i386(Layout
*, Output_data_space
*);
522 // Add an entry to the PLT.
524 add_entry(Symbol
* gsym
);
526 // Return the .rel.plt section data.
529 { return this->rel_
; }
533 do_adjust_output_section(Output_section
* os
);
535 // Write to a map file.
537 do_print_to_mapfile(Mapfile
* mapfile
) const
538 { mapfile
->print_output_data(this, _("** PLT")); }
541 // The size of an entry in the PLT.
542 static const int plt_entry_size
= 16;
544 // The first entry in the PLT for an executable.
545 static unsigned char exec_first_plt_entry
[plt_entry_size
];
547 // The first entry in the PLT for a shared object.
548 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
550 // Other entries in the PLT for an executable.
551 static unsigned char exec_plt_entry
[plt_entry_size
];
553 // Other entries in the PLT for a shared object.
554 static unsigned char dyn_plt_entry
[plt_entry_size
];
556 // Set the final size.
558 set_final_data_size()
559 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
561 // Write out the PLT data.
563 do_write(Output_file
*);
565 // The reloc section.
567 // The .got.plt section.
568 Output_data_space
* got_plt_
;
569 // The number of PLT entries.
573 // Create the PLT section. The ordinary .got section is an argument,
574 // since we need to refer to the start. We also create our own .got
575 // section just for PLT entries.
577 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
578 Output_data_space
* got_plt
)
579 : Output_section_data(4), got_plt_(got_plt
), count_(0)
581 this->rel_
= new Reloc_section(false);
582 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
583 elfcpp::SHF_ALLOC
, this->rel_
);
587 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
589 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
590 // linker, and so do we.
594 // Add an entry to the PLT.
597 Output_data_plt_i386::add_entry(Symbol
* gsym
)
599 gold_assert(!gsym
->has_plt_offset());
601 // Note that when setting the PLT offset we skip the initial
602 // reserved PLT entry.
603 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
607 section_offset_type got_offset
= this->got_plt_
->current_data_size();
609 // Every PLT entry needs a GOT entry which points back to the PLT
610 // entry (this will be changed by the dynamic linker, normally
611 // lazily when the function is called).
612 this->got_plt_
->set_current_data_size(got_offset
+ 4);
614 // Every PLT entry needs a reloc.
615 gsym
->set_needs_dynsym_entry();
616 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
619 // Note that we don't need to save the symbol. The contents of the
620 // PLT are independent of which symbols are used. The symbols only
621 // appear in the relocations.
624 // The first entry in the PLT for an executable.
626 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
628 0xff, 0x35, // pushl contents of memory address
629 0, 0, 0, 0, // replaced with address of .got + 4
630 0xff, 0x25, // jmp indirect
631 0, 0, 0, 0, // replaced with address of .got + 8
635 // The first entry in the PLT for a shared object.
637 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
639 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
640 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
644 // Subsequent entries in the PLT for an executable.
646 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
648 0xff, 0x25, // jmp indirect
649 0, 0, 0, 0, // replaced with address of symbol in .got
650 0x68, // pushl immediate
651 0, 0, 0, 0, // replaced with offset into relocation table
652 0xe9, // jmp relative
653 0, 0, 0, 0 // replaced with offset to start of .plt
656 // Subsequent entries in the PLT for a shared object.
658 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
660 0xff, 0xa3, // jmp *offset(%ebx)
661 0, 0, 0, 0, // replaced with offset of symbol in .got
662 0x68, // pushl immediate
663 0, 0, 0, 0, // replaced with offset into relocation table
664 0xe9, // jmp relative
665 0, 0, 0, 0 // replaced with offset to start of .plt
668 // Write out the PLT. This uses the hand-coded instructions above,
669 // and adjusts them as needed. This is all specified by the i386 ELF
670 // Processor Supplement.
673 Output_data_plt_i386::do_write(Output_file
* of
)
675 const off_t offset
= this->offset();
676 const section_size_type oview_size
=
677 convert_to_section_size_type(this->data_size());
678 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
680 const off_t got_file_offset
= this->got_plt_
->offset();
681 const section_size_type got_size
=
682 convert_to_section_size_type(this->got_plt_
->data_size());
683 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
686 unsigned char* pov
= oview
;
688 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
689 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
691 if (parameters
->options().shared())
692 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
695 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
696 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
697 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
699 pov
+= plt_entry_size
;
701 unsigned char* got_pov
= got_view
;
703 memset(got_pov
, 0, 12);
706 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
708 unsigned int plt_offset
= plt_entry_size
;
709 unsigned int plt_rel_offset
= 0;
710 unsigned int got_offset
= 12;
711 const unsigned int count
= this->count_
;
712 for (unsigned int i
= 0;
715 pov
+= plt_entry_size
,
717 plt_offset
+= plt_entry_size
,
718 plt_rel_offset
+= rel_size
,
721 // Set and adjust the PLT entry itself.
723 if (parameters
->options().shared())
725 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
726 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
730 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
731 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
736 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
737 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
738 - (plt_offset
+ plt_entry_size
));
740 // Set the entry in the GOT.
741 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
744 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
745 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
747 of
->write_output_view(offset
, oview_size
, oview
);
748 of
->write_output_view(got_file_offset
, got_size
, got_view
);
751 // Create a PLT entry for a global symbol.
754 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
756 if (gsym
->has_plt_offset())
759 if (this->plt_
== NULL
)
761 // Create the GOT sections first.
762 this->got_section(symtab
, layout
);
764 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
765 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
767 | elfcpp::SHF_EXECINSTR
),
771 this->plt_
->add_entry(gsym
);
774 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
777 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
779 if (this->tls_base_symbol_defined_
)
782 Output_segment
* tls_segment
= layout
->tls_segment();
783 if (tls_segment
!= NULL
)
785 bool is_exec
= parameters
->options().output_is_executable();
786 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
790 elfcpp::STV_HIDDEN
, 0,
792 ? Symbol::SEGMENT_END
793 : Symbol::SEGMENT_START
),
796 this->tls_base_symbol_defined_
= true;
799 // Create a GOT entry for the TLS module index.
802 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
803 Sized_relobj
<32, false>* object
)
805 if (this->got_mod_index_offset_
== -1U)
807 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
808 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
809 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
810 unsigned int got_offset
= got
->add_constant(0);
811 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
813 got
->add_constant(0);
814 this->got_mod_index_offset_
= got_offset
;
816 return this->got_mod_index_offset_
;
819 // Optimize the TLS relocation type based on what we know about the
820 // symbol. IS_FINAL is true if the final address of this symbol is
821 // known at link time.
823 tls::Tls_optimization
824 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
826 // If we are generating a shared library, then we can't do anything
828 if (parameters
->options().shared())
829 return tls::TLSOPT_NONE
;
833 case elfcpp::R_386_TLS_GD
:
834 case elfcpp::R_386_TLS_GOTDESC
:
835 case elfcpp::R_386_TLS_DESC_CALL
:
836 // These are General-Dynamic which permits fully general TLS
837 // access. Since we know that we are generating an executable,
838 // we can convert this to Initial-Exec. If we also know that
839 // this is a local symbol, we can further switch to Local-Exec.
841 return tls::TLSOPT_TO_LE
;
842 return tls::TLSOPT_TO_IE
;
844 case elfcpp::R_386_TLS_LDM
:
845 // This is Local-Dynamic, which refers to a local symbol in the
846 // dynamic TLS block. Since we know that we generating an
847 // executable, we can switch to Local-Exec.
848 return tls::TLSOPT_TO_LE
;
850 case elfcpp::R_386_TLS_LDO_32
:
851 // Another type of Local-Dynamic relocation.
852 return tls::TLSOPT_TO_LE
;
854 case elfcpp::R_386_TLS_IE
:
855 case elfcpp::R_386_TLS_GOTIE
:
856 case elfcpp::R_386_TLS_IE_32
:
857 // These are Initial-Exec relocs which get the thread offset
858 // from the GOT. If we know that we are linking against the
859 // local symbol, we can switch to Local-Exec, which links the
860 // thread offset into the instruction.
862 return tls::TLSOPT_TO_LE
;
863 return tls::TLSOPT_NONE
;
865 case elfcpp::R_386_TLS_LE
:
866 case elfcpp::R_386_TLS_LE_32
:
867 // When we already have Local-Exec, there is nothing further we
869 return tls::TLSOPT_NONE
;
876 // Report an unsupported relocation against a local symbol.
879 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
882 gold_error(_("%s: unsupported reloc %u against local symbol"),
883 object
->name().c_str(), r_type
);
886 // Scan a relocation for a local symbol.
889 Target_i386::Scan::local(const General_options
&,
890 Symbol_table
* symtab
,
893 Sized_relobj
<32, false>* object
,
894 unsigned int data_shndx
,
895 Output_section
* output_section
,
896 const elfcpp::Rel
<32, false>& reloc
,
898 const elfcpp::Sym
<32, false>& lsym
)
902 case elfcpp::R_386_NONE
:
903 case elfcpp::R_386_GNU_VTINHERIT
:
904 case elfcpp::R_386_GNU_VTENTRY
:
907 case elfcpp::R_386_32
:
908 // If building a shared library (or a position-independent
909 // executable), we need to create a dynamic relocation for
910 // this location. The relocation applied at link time will
911 // apply the link-time value, so we flag the location with
912 // an R_386_RELATIVE relocation so the dynamic loader can
913 // relocate it easily.
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 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
919 output_section
, data_shndx
,
920 reloc
.get_r_offset());
924 case elfcpp::R_386_16
:
925 case elfcpp::R_386_8
:
926 // If building a shared library (or a position-independent
927 // executable), we need to create a dynamic relocation for
928 // this location. Because the addend needs to remain in the
929 // data section, we need to be careful not to apply this
930 // relocation statically.
931 if (parameters
->options().output_is_position_independent())
933 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
934 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
935 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
936 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
937 data_shndx
, reloc
.get_r_offset());
940 gold_assert(lsym
.get_st_value() == 0);
941 unsigned int shndx
= lsym
.get_st_shndx();
943 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
946 object
->error(_("section symbol %u has bad shndx %u"),
949 rel_dyn
->add_local_section(object
, shndx
,
950 r_type
, output_section
,
951 data_shndx
, reloc
.get_r_offset());
956 case elfcpp::R_386_PC32
:
957 case elfcpp::R_386_PC16
:
958 case elfcpp::R_386_PC8
:
961 case elfcpp::R_386_PLT32
:
962 // Since we know this is a local symbol, we can handle this as a
966 case elfcpp::R_386_GOTOFF
:
967 case elfcpp::R_386_GOTPC
:
968 // We need a GOT section.
969 target
->got_section(symtab
, layout
);
972 case elfcpp::R_386_GOT32
:
974 // The symbol requires a GOT entry.
975 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
976 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
977 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
979 // If we are generating a shared object, we need to add a
980 // dynamic RELATIVE relocation for this symbol's GOT entry.
981 if (parameters
->options().output_is_position_independent())
983 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
984 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
985 rel_dyn
->add_local_relative(
986 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
987 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
993 // These are relocations which should only be seen by the
994 // dynamic linker, and should never be seen here.
995 case elfcpp::R_386_COPY
:
996 case elfcpp::R_386_GLOB_DAT
:
997 case elfcpp::R_386_JUMP_SLOT
:
998 case elfcpp::R_386_RELATIVE
:
999 case elfcpp::R_386_TLS_TPOFF
:
1000 case elfcpp::R_386_TLS_DTPMOD32
:
1001 case elfcpp::R_386_TLS_DTPOFF32
:
1002 case elfcpp::R_386_TLS_TPOFF32
:
1003 case elfcpp::R_386_TLS_DESC
:
1004 gold_error(_("%s: unexpected reloc %u in object file"),
1005 object
->name().c_str(), r_type
);
1008 // These are initial TLS relocs, which are expected when
1010 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1011 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1012 case elfcpp::R_386_TLS_DESC_CALL
:
1013 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1014 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1015 case elfcpp::R_386_TLS_IE
: // Initial-exec
1016 case elfcpp::R_386_TLS_IE_32
:
1017 case elfcpp::R_386_TLS_GOTIE
:
1018 case elfcpp::R_386_TLS_LE
: // Local-exec
1019 case elfcpp::R_386_TLS_LE_32
:
1021 bool output_is_shared
= parameters
->options().shared();
1022 const tls::Tls_optimization optimized_type
1023 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1026 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1027 if (optimized_type
== tls::TLSOPT_NONE
)
1029 // Create a pair of GOT entries for the module index and
1030 // dtv-relative offset.
1031 Output_data_got
<32, false>* got
1032 = target
->got_section(symtab
, layout
);
1033 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1034 unsigned int shndx
= lsym
.get_st_shndx();
1036 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1038 object
->error(_("local symbol %u has bad shndx %u"),
1041 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1043 target
->rel_dyn_section(layout
),
1044 elfcpp::R_386_TLS_DTPMOD32
, 0);
1046 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1047 unsupported_reloc_local(object
, r_type
);
1050 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1051 target
->define_tls_base_symbol(symtab
, layout
);
1052 if (optimized_type
== tls::TLSOPT_NONE
)
1054 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1055 Output_data_got
<32, false>* got
1056 = target
->got_section(symtab
, layout
);
1057 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1058 unsigned int shndx
= lsym
.get_st_shndx();
1060 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1062 object
->error(_("local symbol %u has bad shndx %u"),
1065 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1067 target
->rel_dyn_section(layout
),
1068 elfcpp::R_386_TLS_DESC
, 0);
1070 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1071 unsupported_reloc_local(object
, r_type
);
1074 case elfcpp::R_386_TLS_DESC_CALL
:
1077 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1078 if (optimized_type
== tls::TLSOPT_NONE
)
1080 // Create a GOT entry for the module index.
1081 target
->got_mod_index_entry(symtab
, layout
, object
);
1083 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1084 unsupported_reloc_local(object
, r_type
);
1087 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1090 case elfcpp::R_386_TLS_IE
: // Initial-exec
1091 case elfcpp::R_386_TLS_IE_32
:
1092 case elfcpp::R_386_TLS_GOTIE
:
1093 layout
->set_has_static_tls();
1094 if (optimized_type
== tls::TLSOPT_NONE
)
1096 // For the R_386_TLS_IE relocation, we need to create a
1097 // dynamic relocation when building a shared library.
1098 if (r_type
== elfcpp::R_386_TLS_IE
1099 && parameters
->options().shared())
1101 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1103 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1104 rel_dyn
->add_local_relative(object
, r_sym
,
1105 elfcpp::R_386_RELATIVE
,
1106 output_section
, data_shndx
,
1107 reloc
.get_r_offset());
1109 // Create a GOT entry for the tp-relative offset.
1110 Output_data_got
<32, false>* got
1111 = target
->got_section(symtab
, layout
);
1112 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1113 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1114 ? elfcpp::R_386_TLS_TPOFF32
1115 : elfcpp::R_386_TLS_TPOFF
);
1116 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1117 ? GOT_TYPE_TLS_OFFSET
1118 : GOT_TYPE_TLS_NOFFSET
);
1119 got
->add_local_with_rel(object
, r_sym
, got_type
,
1120 target
->rel_dyn_section(layout
),
1123 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1124 unsupported_reloc_local(object
, r_type
);
1127 case elfcpp::R_386_TLS_LE
: // Local-exec
1128 case elfcpp::R_386_TLS_LE_32
:
1129 layout
->set_has_static_tls();
1130 if (output_is_shared
)
1132 // We need to create a dynamic relocation.
1133 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1134 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1135 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1136 ? elfcpp::R_386_TLS_TPOFF32
1137 : elfcpp::R_386_TLS_TPOFF
);
1138 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1139 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1140 data_shndx
, reloc
.get_r_offset());
1150 case elfcpp::R_386_32PLT
:
1151 case elfcpp::R_386_TLS_GD_32
:
1152 case elfcpp::R_386_TLS_GD_PUSH
:
1153 case elfcpp::R_386_TLS_GD_CALL
:
1154 case elfcpp::R_386_TLS_GD_POP
:
1155 case elfcpp::R_386_TLS_LDM_32
:
1156 case elfcpp::R_386_TLS_LDM_PUSH
:
1157 case elfcpp::R_386_TLS_LDM_CALL
:
1158 case elfcpp::R_386_TLS_LDM_POP
:
1159 case elfcpp::R_386_USED_BY_INTEL_200
:
1161 unsupported_reloc_local(object
, r_type
);
1166 // Report an unsupported relocation against a global symbol.
1169 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1170 unsigned int r_type
,
1173 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1174 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1177 // Scan a relocation for a global symbol.
1180 Target_i386::Scan::global(const General_options
&,
1181 Symbol_table
* symtab
,
1183 Target_i386
* target
,
1184 Sized_relobj
<32, false>* object
,
1185 unsigned int data_shndx
,
1186 Output_section
* output_section
,
1187 const elfcpp::Rel
<32, false>& reloc
,
1188 unsigned int r_type
,
1193 case elfcpp::R_386_NONE
:
1194 case elfcpp::R_386_GNU_VTINHERIT
:
1195 case elfcpp::R_386_GNU_VTENTRY
:
1198 case elfcpp::R_386_32
:
1199 case elfcpp::R_386_16
:
1200 case elfcpp::R_386_8
:
1202 // Make a PLT entry if necessary.
1203 if (gsym
->needs_plt_entry())
1205 target
->make_plt_entry(symtab
, layout
, gsym
);
1206 // Since this is not a PC-relative relocation, we may be
1207 // taking the address of a function. In that case we need to
1208 // set the entry in the dynamic symbol table to the address of
1210 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1211 gsym
->set_needs_dynsym_value();
1213 // Make a dynamic relocation if necessary.
1214 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1216 if (target
->may_need_copy_reloc(gsym
))
1218 target
->copy_reloc(symtab
, layout
, object
,
1219 data_shndx
, output_section
, gsym
, reloc
);
1221 else if (r_type
== elfcpp::R_386_32
1222 && gsym
->can_use_relative_reloc(false))
1224 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1225 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1226 output_section
, object
,
1227 data_shndx
, reloc
.get_r_offset());
1231 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1232 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1233 data_shndx
, reloc
.get_r_offset());
1239 case elfcpp::R_386_PC32
:
1240 case elfcpp::R_386_PC16
:
1241 case elfcpp::R_386_PC8
:
1243 // Make a PLT entry if necessary.
1244 if (gsym
->needs_plt_entry())
1246 // These relocations are used for function calls only in
1247 // non-PIC code. For a 32-bit relocation in a shared library,
1248 // we'll need a text relocation anyway, so we can skip the
1249 // PLT entry and let the dynamic linker bind the call directly
1250 // to the target. For smaller relocations, we should use a
1251 // PLT entry to ensure that the call can reach.
1252 if (!parameters
->options().shared()
1253 || r_type
!= elfcpp::R_386_PC32
)
1254 target
->make_plt_entry(symtab
, layout
, gsym
);
1256 // Make a dynamic relocation if necessary.
1257 int flags
= Symbol::NON_PIC_REF
;
1258 if (gsym
->type() == elfcpp::STT_FUNC
)
1259 flags
|= Symbol::FUNCTION_CALL
;
1260 if (gsym
->needs_dynamic_reloc(flags
))
1262 if (target
->may_need_copy_reloc(gsym
))
1264 target
->copy_reloc(symtab
, layout
, object
,
1265 data_shndx
, output_section
, gsym
, reloc
);
1269 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1270 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1271 data_shndx
, reloc
.get_r_offset());
1277 case elfcpp::R_386_GOT32
:
1279 // The symbol requires a GOT entry.
1280 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1281 if (gsym
->final_value_is_known())
1282 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1285 // If this symbol is not fully resolved, we need to add a
1286 // GOT entry with a dynamic relocation.
1287 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1288 if (gsym
->is_from_dynobj()
1289 || gsym
->is_undefined()
1290 || gsym
->is_preemptible())
1291 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1292 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1295 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1296 rel_dyn
->add_global_relative(
1297 gsym
, elfcpp::R_386_RELATIVE
, got
,
1298 gsym
->got_offset(GOT_TYPE_STANDARD
));
1304 case elfcpp::R_386_PLT32
:
1305 // If the symbol is fully resolved, this is just a PC32 reloc.
1306 // Otherwise we need a PLT entry.
1307 if (gsym
->final_value_is_known())
1309 // If building a shared library, we can also skip the PLT entry
1310 // if the symbol is defined in the output file and is protected
1312 if (gsym
->is_defined()
1313 && !gsym
->is_from_dynobj()
1314 && !gsym
->is_preemptible())
1316 target
->make_plt_entry(symtab
, layout
, gsym
);
1319 case elfcpp::R_386_GOTOFF
:
1320 case elfcpp::R_386_GOTPC
:
1321 // We need a GOT section.
1322 target
->got_section(symtab
, layout
);
1325 // These are relocations which should only be seen by the
1326 // dynamic linker, and should never be seen here.
1327 case elfcpp::R_386_COPY
:
1328 case elfcpp::R_386_GLOB_DAT
:
1329 case elfcpp::R_386_JUMP_SLOT
:
1330 case elfcpp::R_386_RELATIVE
:
1331 case elfcpp::R_386_TLS_TPOFF
:
1332 case elfcpp::R_386_TLS_DTPMOD32
:
1333 case elfcpp::R_386_TLS_DTPOFF32
:
1334 case elfcpp::R_386_TLS_TPOFF32
:
1335 case elfcpp::R_386_TLS_DESC
:
1336 gold_error(_("%s: unexpected reloc %u in object file"),
1337 object
->name().c_str(), r_type
);
1340 // These are initial tls relocs, which are expected when
1342 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1343 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1344 case elfcpp::R_386_TLS_DESC_CALL
:
1345 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1346 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1347 case elfcpp::R_386_TLS_IE
: // Initial-exec
1348 case elfcpp::R_386_TLS_IE_32
:
1349 case elfcpp::R_386_TLS_GOTIE
:
1350 case elfcpp::R_386_TLS_LE
: // Local-exec
1351 case elfcpp::R_386_TLS_LE_32
:
1353 const bool is_final
= gsym
->final_value_is_known();
1354 const tls::Tls_optimization optimized_type
1355 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1358 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1359 if (optimized_type
== tls::TLSOPT_NONE
)
1361 // Create a pair of GOT entries for the module index and
1362 // dtv-relative offset.
1363 Output_data_got
<32, false>* got
1364 = target
->got_section(symtab
, layout
);
1365 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1366 target
->rel_dyn_section(layout
),
1367 elfcpp::R_386_TLS_DTPMOD32
,
1368 elfcpp::R_386_TLS_DTPOFF32
);
1370 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1372 // Create a GOT entry for the tp-relative offset.
1373 Output_data_got
<32, false>* got
1374 = target
->got_section(symtab
, layout
);
1375 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1376 target
->rel_dyn_section(layout
),
1377 elfcpp::R_386_TLS_TPOFF
);
1379 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1380 unsupported_reloc_global(object
, r_type
, gsym
);
1383 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1384 target
->define_tls_base_symbol(symtab
, layout
);
1385 if (optimized_type
== tls::TLSOPT_NONE
)
1387 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1388 Output_data_got
<32, false>* got
1389 = target
->got_section(symtab
, layout
);
1390 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1391 target
->rel_dyn_section(layout
),
1392 elfcpp::R_386_TLS_DESC
, 0);
1394 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1396 // Create a GOT entry for the tp-relative offset.
1397 Output_data_got
<32, false>* got
1398 = target
->got_section(symtab
, layout
);
1399 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1400 target
->rel_dyn_section(layout
),
1401 elfcpp::R_386_TLS_TPOFF
);
1403 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1404 unsupported_reloc_global(object
, r_type
, gsym
);
1407 case elfcpp::R_386_TLS_DESC_CALL
:
1410 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1411 if (optimized_type
== tls::TLSOPT_NONE
)
1413 // Create a GOT entry for the module index.
1414 target
->got_mod_index_entry(symtab
, layout
, object
);
1416 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1417 unsupported_reloc_global(object
, r_type
, gsym
);
1420 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1423 case elfcpp::R_386_TLS_IE
: // Initial-exec
1424 case elfcpp::R_386_TLS_IE_32
:
1425 case elfcpp::R_386_TLS_GOTIE
:
1426 layout
->set_has_static_tls();
1427 if (optimized_type
== tls::TLSOPT_NONE
)
1429 // For the R_386_TLS_IE relocation, we need to create a
1430 // dynamic relocation when building a shared library.
1431 if (r_type
== elfcpp::R_386_TLS_IE
1432 && parameters
->options().shared())
1434 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1435 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1436 output_section
, object
,
1438 reloc
.get_r_offset());
1440 // Create a GOT entry for the tp-relative offset.
1441 Output_data_got
<32, false>* got
1442 = target
->got_section(symtab
, layout
);
1443 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1444 ? elfcpp::R_386_TLS_TPOFF32
1445 : elfcpp::R_386_TLS_TPOFF
);
1446 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1447 ? GOT_TYPE_TLS_OFFSET
1448 : GOT_TYPE_TLS_NOFFSET
);
1449 got
->add_global_with_rel(gsym
, got_type
,
1450 target
->rel_dyn_section(layout
),
1453 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1454 unsupported_reloc_global(object
, r_type
, gsym
);
1457 case elfcpp::R_386_TLS_LE
: // Local-exec
1458 case elfcpp::R_386_TLS_LE_32
:
1459 layout
->set_has_static_tls();
1460 if (parameters
->options().shared())
1462 // We need to create a dynamic relocation.
1463 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1464 ? elfcpp::R_386_TLS_TPOFF32
1465 : elfcpp::R_386_TLS_TPOFF
);
1466 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1467 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1468 data_shndx
, reloc
.get_r_offset());
1478 case elfcpp::R_386_32PLT
:
1479 case elfcpp::R_386_TLS_GD_32
:
1480 case elfcpp::R_386_TLS_GD_PUSH
:
1481 case elfcpp::R_386_TLS_GD_CALL
:
1482 case elfcpp::R_386_TLS_GD_POP
:
1483 case elfcpp::R_386_TLS_LDM_32
:
1484 case elfcpp::R_386_TLS_LDM_PUSH
:
1485 case elfcpp::R_386_TLS_LDM_CALL
:
1486 case elfcpp::R_386_TLS_LDM_POP
:
1487 case elfcpp::R_386_USED_BY_INTEL_200
:
1489 unsupported_reloc_global(object
, r_type
, gsym
);
1494 // Process relocations for gc.
1497 Target_i386::gc_process_relocs(const General_options
& options
,
1498 Symbol_table
* symtab
,
1500 Sized_relobj
<32, false>* object
,
1501 unsigned int data_shndx
,
1503 const unsigned char* prelocs
,
1505 Output_section
* output_section
,
1506 bool needs_special_offset_handling
,
1507 size_t local_symbol_count
,
1508 const unsigned char* plocal_symbols
)
1510 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1521 needs_special_offset_handling
,
1526 // Scan relocations for a section.
1529 Target_i386::scan_relocs(const General_options
& options
,
1530 Symbol_table
* symtab
,
1532 Sized_relobj
<32, false>* object
,
1533 unsigned int data_shndx
,
1534 unsigned int sh_type
,
1535 const unsigned char* prelocs
,
1537 Output_section
* output_section
,
1538 bool needs_special_offset_handling
,
1539 size_t local_symbol_count
,
1540 const unsigned char* plocal_symbols
)
1542 if (sh_type
== elfcpp::SHT_RELA
)
1544 gold_error(_("%s: unsupported RELA reloc section"),
1545 object
->name().c_str());
1549 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1560 needs_special_offset_handling
,
1565 // Finalize the sections.
1568 Target_i386::do_finalize_sections(Layout
* layout
)
1570 // Fill in some more dynamic tags.
1571 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1574 if (this->got_plt_
!= NULL
)
1575 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1577 if (this->plt_
!= NULL
)
1579 const Output_data
* od
= this->plt_
->rel_plt();
1580 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1581 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1582 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1585 if (this->rel_dyn_
!= NULL
)
1587 const Output_data
* od
= this->rel_dyn_
;
1588 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1589 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1590 odyn
->add_constant(elfcpp::DT_RELENT
,
1591 elfcpp::Elf_sizes
<32>::rel_size
);
1594 if (!parameters
->options().shared())
1596 // The value of the DT_DEBUG tag is filled in by the dynamic
1597 // linker at run time, and used by the debugger.
1598 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1602 // Emit any relocs we saved in an attempt to avoid generating COPY
1604 if (this->copy_relocs_
.any_saved_relocs())
1605 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1608 // Return whether a direct absolute static relocation needs to be applied.
1609 // In cases where Scan::local() or Scan::global() has created
1610 // a dynamic relocation other than R_386_RELATIVE, the addend
1611 // of the relocation is carried in the data, and we must not
1612 // apply the static relocation.
1615 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1618 Output_section
* output_section
)
1620 // If the output section is not allocated, then we didn't call
1621 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1623 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1626 // For local symbols, we will have created a non-RELATIVE dynamic
1627 // relocation only if (a) the output is position independent,
1628 // (b) the relocation is absolute (not pc- or segment-relative), and
1629 // (c) the relocation is not 32 bits wide.
1631 return !(parameters
->options().output_is_position_independent()
1632 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1635 // For global symbols, we use the same helper routines used in the
1636 // scan pass. If we did not create a dynamic relocation, or if we
1637 // created a RELATIVE dynamic relocation, we should apply the static
1639 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1640 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1641 && gsym
->can_use_relative_reloc(ref_flags
1642 & Symbol::FUNCTION_CALL
);
1643 return !has_dyn
|| is_rel
;
1646 // Perform a relocation.
1649 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1650 Target_i386
* target
,
1651 Output_section
*output_section
,
1653 const elfcpp::Rel
<32, false>& rel
,
1654 unsigned int r_type
,
1655 const Sized_symbol
<32>* gsym
,
1656 const Symbol_value
<32>* psymval
,
1657 unsigned char* view
,
1658 elfcpp::Elf_types
<32>::Elf_Addr address
,
1659 section_size_type view_size
)
1661 if (this->skip_call_tls_get_addr_
)
1663 if ((r_type
!= elfcpp::R_386_PLT32
1664 && r_type
!= elfcpp::R_386_PC32
)
1666 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1667 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1668 _("missing expected TLS relocation"));
1671 this->skip_call_tls_get_addr_
= false;
1676 // Pick the value to use for symbols defined in shared objects.
1677 Symbol_value
<32> symval
;
1679 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1680 || r_type
== elfcpp::R_386_PC16
1681 || r_type
== elfcpp::R_386_PC32
))
1683 symval
.set_output_value(target
->plt_section()->address()
1684 + gsym
->plt_offset());
1688 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1690 // Get the GOT offset if needed.
1691 // The GOT pointer points to the end of the GOT section.
1692 // We need to subtract the size of the GOT section to get
1693 // the actual offset to use in the relocation.
1694 bool have_got_offset
= false;
1695 unsigned int got_offset
= 0;
1698 case elfcpp::R_386_GOT32
:
1701 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1702 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1703 - target
->got_size());
1707 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1708 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1709 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1710 - target
->got_size());
1712 have_got_offset
= true;
1721 case elfcpp::R_386_NONE
:
1722 case elfcpp::R_386_GNU_VTINHERIT
:
1723 case elfcpp::R_386_GNU_VTENTRY
:
1726 case elfcpp::R_386_32
:
1727 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1729 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1732 case elfcpp::R_386_PC32
:
1734 int ref_flags
= Symbol::NON_PIC_REF
;
1735 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1736 ref_flags
|= Symbol::FUNCTION_CALL
;
1737 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1738 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1742 case elfcpp::R_386_16
:
1743 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1745 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1748 case elfcpp::R_386_PC16
:
1750 int ref_flags
= Symbol::NON_PIC_REF
;
1751 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1752 ref_flags
|= Symbol::FUNCTION_CALL
;
1753 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1754 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1758 case elfcpp::R_386_8
:
1759 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1761 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1764 case elfcpp::R_386_PC8
:
1766 int ref_flags
= Symbol::NON_PIC_REF
;
1767 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1768 ref_flags
|= Symbol::FUNCTION_CALL
;
1769 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1771 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1775 case elfcpp::R_386_PLT32
:
1776 gold_assert(gsym
== NULL
1777 || gsym
->has_plt_offset()
1778 || gsym
->final_value_is_known()
1779 || (gsym
->is_defined()
1780 && !gsym
->is_from_dynobj()
1781 && !gsym
->is_preemptible()));
1782 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1785 case elfcpp::R_386_GOT32
:
1786 gold_assert(have_got_offset
);
1787 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1790 case elfcpp::R_386_GOTOFF
:
1792 elfcpp::Elf_types
<32>::Elf_Addr value
;
1793 value
= (psymval
->value(object
, 0)
1794 - target
->got_plt_section()->address());
1795 Relocate_functions
<32, false>::rel32(view
, value
);
1799 case elfcpp::R_386_GOTPC
:
1801 elfcpp::Elf_types
<32>::Elf_Addr value
;
1802 value
= target
->got_plt_section()->address();
1803 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1807 case elfcpp::R_386_COPY
:
1808 case elfcpp::R_386_GLOB_DAT
:
1809 case elfcpp::R_386_JUMP_SLOT
:
1810 case elfcpp::R_386_RELATIVE
:
1811 // These are outstanding tls relocs, which are unexpected when
1813 case elfcpp::R_386_TLS_TPOFF
:
1814 case elfcpp::R_386_TLS_DTPMOD32
:
1815 case elfcpp::R_386_TLS_DTPOFF32
:
1816 case elfcpp::R_386_TLS_TPOFF32
:
1817 case elfcpp::R_386_TLS_DESC
:
1818 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1819 _("unexpected reloc %u in object file"),
1823 // These are initial tls relocs, which are expected when
1825 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1826 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1827 case elfcpp::R_386_TLS_DESC_CALL
:
1828 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1829 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1830 case elfcpp::R_386_TLS_IE
: // Initial-exec
1831 case elfcpp::R_386_TLS_IE_32
:
1832 case elfcpp::R_386_TLS_GOTIE
:
1833 case elfcpp::R_386_TLS_LE
: // Local-exec
1834 case elfcpp::R_386_TLS_LE_32
:
1835 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1836 view
, address
, view_size
);
1839 case elfcpp::R_386_32PLT
:
1840 case elfcpp::R_386_TLS_GD_32
:
1841 case elfcpp::R_386_TLS_GD_PUSH
:
1842 case elfcpp::R_386_TLS_GD_CALL
:
1843 case elfcpp::R_386_TLS_GD_POP
:
1844 case elfcpp::R_386_TLS_LDM_32
:
1845 case elfcpp::R_386_TLS_LDM_PUSH
:
1846 case elfcpp::R_386_TLS_LDM_CALL
:
1847 case elfcpp::R_386_TLS_LDM_POP
:
1848 case elfcpp::R_386_USED_BY_INTEL_200
:
1850 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1851 _("unsupported reloc %u"),
1859 // Perform a TLS relocation.
1862 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1863 Target_i386
* target
,
1865 const elfcpp::Rel
<32, false>& rel
,
1866 unsigned int r_type
,
1867 const Sized_symbol
<32>* gsym
,
1868 const Symbol_value
<32>* psymval
,
1869 unsigned char* view
,
1870 elfcpp::Elf_types
<32>::Elf_Addr
,
1871 section_size_type view_size
)
1873 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1875 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1877 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1879 const bool is_final
=
1881 ? !parameters
->options().output_is_position_independent()
1882 : gsym
->final_value_is_known());
1883 const tls::Tls_optimization optimized_type
1884 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1887 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1888 if (optimized_type
== tls::TLSOPT_TO_LE
)
1890 gold_assert(tls_segment
!= NULL
);
1891 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1892 rel
, r_type
, value
, view
,
1898 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1899 ? GOT_TYPE_TLS_NOFFSET
1900 : GOT_TYPE_TLS_PAIR
);
1901 unsigned int got_offset
;
1904 gold_assert(gsym
->has_got_offset(got_type
));
1905 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1909 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1910 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1911 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1912 - target
->got_size());
1914 if (optimized_type
== tls::TLSOPT_TO_IE
)
1916 gold_assert(tls_segment
!= NULL
);
1917 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1918 got_offset
, view
, view_size
);
1921 else if (optimized_type
== tls::TLSOPT_NONE
)
1923 // Relocate the field with the offset of the pair of GOT
1925 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1929 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1930 _("unsupported reloc %u"),
1934 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1935 case elfcpp::R_386_TLS_DESC_CALL
:
1936 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1937 if (optimized_type
== tls::TLSOPT_TO_LE
)
1939 gold_assert(tls_segment
!= NULL
);
1940 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1941 rel
, r_type
, value
, view
,
1947 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1948 ? GOT_TYPE_TLS_NOFFSET
1949 : GOT_TYPE_TLS_DESC
);
1950 unsigned int got_offset
;
1953 gold_assert(gsym
->has_got_offset(got_type
));
1954 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1958 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1959 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1960 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1961 - target
->got_size());
1963 if (optimized_type
== tls::TLSOPT_TO_IE
)
1965 gold_assert(tls_segment
!= NULL
);
1966 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1967 got_offset
, view
, view_size
);
1970 else if (optimized_type
== tls::TLSOPT_NONE
)
1972 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1974 // Relocate the field with the offset of the pair of GOT
1976 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1981 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1982 _("unsupported reloc %u"),
1986 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1987 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1989 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1990 _("both SUN and GNU model "
1991 "TLS relocations"));
1994 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1995 if (optimized_type
== tls::TLSOPT_TO_LE
)
1997 gold_assert(tls_segment
!= NULL
);
1998 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1999 value
, view
, view_size
);
2002 else if (optimized_type
== tls::TLSOPT_NONE
)
2004 // Relocate the field with the offset of the GOT entry for
2005 // the module index.
2006 unsigned int got_offset
;
2007 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2008 - target
->got_size());
2009 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2012 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2013 _("unsupported reloc %u"),
2017 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2018 // This reloc can appear in debugging sections, in which case we
2019 // won't see the TLS_LDM reloc. The local_dynamic_type field
2021 if (optimized_type
== tls::TLSOPT_TO_LE
2022 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
2024 gold_assert(tls_segment
!= NULL
);
2025 value
-= tls_segment
->memsz();
2027 Relocate_functions
<32, false>::rel32(view
, value
);
2030 case elfcpp::R_386_TLS_IE
: // Initial-exec
2031 case elfcpp::R_386_TLS_GOTIE
:
2032 case elfcpp::R_386_TLS_IE_32
:
2033 if (optimized_type
== tls::TLSOPT_TO_LE
)
2035 gold_assert(tls_segment
!= NULL
);
2036 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2037 rel
, r_type
, value
, view
,
2041 else if (optimized_type
== tls::TLSOPT_NONE
)
2043 // Relocate the field with the offset of the GOT entry for
2044 // the tp-relative offset of the symbol.
2045 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2046 ? GOT_TYPE_TLS_OFFSET
2047 : GOT_TYPE_TLS_NOFFSET
);
2048 unsigned int got_offset
;
2051 gold_assert(gsym
->has_got_offset(got_type
));
2052 got_offset
= gsym
->got_offset(got_type
);
2056 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2057 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2058 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2060 // For the R_386_TLS_IE relocation, we need to apply the
2061 // absolute address of the GOT entry.
2062 if (r_type
== elfcpp::R_386_TLS_IE
)
2063 got_offset
+= target
->got_plt_section()->address();
2064 // All GOT offsets are relative to the end of the GOT.
2065 got_offset
-= target
->got_size();
2066 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2069 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2070 _("unsupported reloc %u"),
2074 case elfcpp::R_386_TLS_LE
: // Local-exec
2075 // If we're creating a shared library, a dynamic relocation will
2076 // have been created for this location, so do not apply it now.
2077 if (!parameters
->options().shared())
2079 gold_assert(tls_segment
!= NULL
);
2080 value
-= tls_segment
->memsz();
2081 Relocate_functions
<32, false>::rel32(view
, value
);
2085 case elfcpp::R_386_TLS_LE_32
:
2086 // If we're creating a shared library, a dynamic relocation will
2087 // have been created for this location, so do not apply it now.
2088 if (!parameters
->options().shared())
2090 gold_assert(tls_segment
!= NULL
);
2091 value
= tls_segment
->memsz() - value
;
2092 Relocate_functions
<32, false>::rel32(view
, value
);
2098 // Do a relocation in which we convert a TLS General-Dynamic to a
2102 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2104 Output_segment
* tls_segment
,
2105 const elfcpp::Rel
<32, false>& rel
,
2107 elfcpp::Elf_types
<32>::Elf_Addr value
,
2108 unsigned char* view
,
2109 section_size_type view_size
)
2111 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2112 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2113 // leal foo(%reg),%eax; call ___tls_get_addr
2114 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2116 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2117 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2119 unsigned char op1
= view
[-1];
2120 unsigned char op2
= view
[-2];
2122 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2123 op2
== 0x8d || op2
== 0x04);
2124 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2130 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2131 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2132 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2133 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2134 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2138 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2139 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2140 if (rel
.get_r_offset() + 9 < view_size
2143 // There is a trailing nop. Use the size byte subl.
2144 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2149 // Use the five byte subl.
2150 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2154 value
= tls_segment
->memsz() - value
;
2155 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2157 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2159 this->skip_call_tls_get_addr_
= true;
2162 // Do a relocation in which we convert a TLS General-Dynamic to an
2166 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2169 const elfcpp::Rel
<32, false>& rel
,
2171 elfcpp::Elf_types
<32>::Elf_Addr value
,
2172 unsigned char* view
,
2173 section_size_type view_size
)
2175 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2176 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2178 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2179 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2181 unsigned char op1
= view
[-1];
2182 unsigned char op2
= view
[-2];
2184 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2185 op2
== 0x8d || op2
== 0x04);
2186 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2190 // FIXME: For now, support only the first (SIB) form.
2191 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2195 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2196 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2197 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2198 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2199 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2203 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2204 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2205 if (rel
.get_r_offset() + 9 < view_size
2208 // FIXME: This is not the right instruction sequence.
2209 // There is a trailing nop. Use the size byte subl.
2210 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2215 // FIXME: This is not the right instruction sequence.
2216 // Use the five byte subl.
2217 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2221 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2223 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2225 this->skip_call_tls_get_addr_
= true;
2228 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2229 // General-Dynamic to a Local-Exec.
2232 Target_i386::Relocate::tls_desc_gd_to_le(
2233 const Relocate_info
<32, false>* relinfo
,
2235 Output_segment
* tls_segment
,
2236 const elfcpp::Rel
<32, false>& rel
,
2237 unsigned int r_type
,
2238 elfcpp::Elf_types
<32>::Elf_Addr value
,
2239 unsigned char* view
,
2240 section_size_type view_size
)
2242 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2244 // leal foo@TLSDESC(%ebx), %eax
2245 // ==> leal foo@NTPOFF, %eax
2246 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2247 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2248 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2249 view
[-2] == 0x8d && view
[-1] == 0x83);
2251 value
-= tls_segment
->memsz();
2252 Relocate_functions
<32, false>::rel32(view
, value
);
2256 // call *foo@TLSCALL(%eax)
2258 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2259 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2260 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2261 view
[0] == 0xff && view
[1] == 0x10);
2267 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2268 // General-Dynamic to an Initial-Exec.
2271 Target_i386::Relocate::tls_desc_gd_to_ie(
2272 const Relocate_info
<32, false>* relinfo
,
2275 const elfcpp::Rel
<32, false>& rel
,
2276 unsigned int r_type
,
2277 elfcpp::Elf_types
<32>::Elf_Addr value
,
2278 unsigned char* view
,
2279 section_size_type view_size
)
2281 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2283 // leal foo@TLSDESC(%ebx), %eax
2284 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2285 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2286 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2287 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2288 view
[-2] == 0x8d && view
[-1] == 0x83);
2290 Relocate_functions
<32, false>::rel32(view
, value
);
2294 // call *foo@TLSCALL(%eax)
2296 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2297 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2298 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2299 view
[0] == 0xff && view
[1] == 0x10);
2305 // Do a relocation in which we convert a TLS Local-Dynamic to a
2309 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2312 const elfcpp::Rel
<32, false>& rel
,
2314 elfcpp::Elf_types
<32>::Elf_Addr
,
2315 unsigned char* view
,
2316 section_size_type view_size
)
2318 // leal foo(%reg), %eax; call ___tls_get_addr
2319 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2321 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2322 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2324 // FIXME: Does this test really always pass?
2325 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2326 view
[-2] == 0x8d && view
[-1] == 0x83);
2328 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2330 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2332 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2334 this->skip_call_tls_get_addr_
= true;
2337 // Do a relocation in which we convert a TLS Initial-Exec to a
2341 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2343 Output_segment
* tls_segment
,
2344 const elfcpp::Rel
<32, false>& rel
,
2345 unsigned int r_type
,
2346 elfcpp::Elf_types
<32>::Elf_Addr value
,
2347 unsigned char* view
,
2348 section_size_type view_size
)
2350 // We have to actually change the instructions, which means that we
2351 // need to examine the opcodes to figure out which instruction we
2353 if (r_type
== elfcpp::R_386_TLS_IE
)
2355 // movl %gs:XX,%eax ==> movl $YY,%eax
2356 // movl %gs:XX,%reg ==> movl $YY,%reg
2357 // addl %gs:XX,%reg ==> addl $YY,%reg
2358 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2359 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2361 unsigned char op1
= view
[-1];
2364 // movl XX,%eax ==> movl $YY,%eax
2369 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2371 unsigned char op2
= view
[-2];
2374 // movl XX,%reg ==> movl $YY,%reg
2375 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2376 (op1
& 0xc7) == 0x05);
2378 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2380 else if (op2
== 0x03)
2382 // addl XX,%reg ==> addl $YY,%reg
2383 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2384 (op1
& 0xc7) == 0x05);
2386 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2389 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2394 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2395 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2396 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2397 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2398 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2400 unsigned char op1
= view
[-1];
2401 unsigned char op2
= view
[-2];
2402 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2403 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2406 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2408 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2410 else if (op2
== 0x2b)
2412 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2414 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2416 else if (op2
== 0x03)
2418 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2420 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2423 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2426 value
= tls_segment
->memsz() - value
;
2427 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2430 Relocate_functions
<32, false>::rel32(view
, value
);
2433 // Relocate section data.
2436 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2437 unsigned int sh_type
,
2438 const unsigned char* prelocs
,
2440 Output_section
* output_section
,
2441 bool needs_special_offset_handling
,
2442 unsigned char* view
,
2443 elfcpp::Elf_types
<32>::Elf_Addr address
,
2444 section_size_type view_size
)
2446 gold_assert(sh_type
== elfcpp::SHT_REL
);
2448 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2449 Target_i386::Relocate
>(
2455 needs_special_offset_handling
,
2461 // Return the size of a relocation while scanning during a relocatable
2465 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2466 unsigned int r_type
,
2471 case elfcpp::R_386_NONE
:
2472 case elfcpp::R_386_GNU_VTINHERIT
:
2473 case elfcpp::R_386_GNU_VTENTRY
:
2474 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2475 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2476 case elfcpp::R_386_TLS_DESC_CALL
:
2477 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2478 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2479 case elfcpp::R_386_TLS_IE
: // Initial-exec
2480 case elfcpp::R_386_TLS_IE_32
:
2481 case elfcpp::R_386_TLS_GOTIE
:
2482 case elfcpp::R_386_TLS_LE
: // Local-exec
2483 case elfcpp::R_386_TLS_LE_32
:
2486 case elfcpp::R_386_32
:
2487 case elfcpp::R_386_PC32
:
2488 case elfcpp::R_386_GOT32
:
2489 case elfcpp::R_386_PLT32
:
2490 case elfcpp::R_386_GOTOFF
:
2491 case elfcpp::R_386_GOTPC
:
2494 case elfcpp::R_386_16
:
2495 case elfcpp::R_386_PC16
:
2498 case elfcpp::R_386_8
:
2499 case elfcpp::R_386_PC8
:
2502 // These are relocations which should only be seen by the
2503 // dynamic linker, and should never be seen here.
2504 case elfcpp::R_386_COPY
:
2505 case elfcpp::R_386_GLOB_DAT
:
2506 case elfcpp::R_386_JUMP_SLOT
:
2507 case elfcpp::R_386_RELATIVE
:
2508 case elfcpp::R_386_TLS_TPOFF
:
2509 case elfcpp::R_386_TLS_DTPMOD32
:
2510 case elfcpp::R_386_TLS_DTPOFF32
:
2511 case elfcpp::R_386_TLS_TPOFF32
:
2512 case elfcpp::R_386_TLS_DESC
:
2513 object
->error(_("unexpected reloc %u in object file"), r_type
);
2516 case elfcpp::R_386_32PLT
:
2517 case elfcpp::R_386_TLS_GD_32
:
2518 case elfcpp::R_386_TLS_GD_PUSH
:
2519 case elfcpp::R_386_TLS_GD_CALL
:
2520 case elfcpp::R_386_TLS_GD_POP
:
2521 case elfcpp::R_386_TLS_LDM_32
:
2522 case elfcpp::R_386_TLS_LDM_PUSH
:
2523 case elfcpp::R_386_TLS_LDM_CALL
:
2524 case elfcpp::R_386_TLS_LDM_POP
:
2525 case elfcpp::R_386_USED_BY_INTEL_200
:
2527 object
->error(_("unsupported reloc %u in object file"), r_type
);
2532 // Scan the relocs during a relocatable link.
2535 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2536 Symbol_table
* symtab
,
2538 Sized_relobj
<32, false>* object
,
2539 unsigned int data_shndx
,
2540 unsigned int sh_type
,
2541 const unsigned char* prelocs
,
2543 Output_section
* output_section
,
2544 bool needs_special_offset_handling
,
2545 size_t local_symbol_count
,
2546 const unsigned char* plocal_symbols
,
2547 Relocatable_relocs
* rr
)
2549 gold_assert(sh_type
== elfcpp::SHT_REL
);
2551 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2552 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2554 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2555 Scan_relocatable_relocs
>(
2564 needs_special_offset_handling
,
2570 // Relocate a section during a relocatable link.
2573 Target_i386::relocate_for_relocatable(
2574 const Relocate_info
<32, false>* relinfo
,
2575 unsigned int sh_type
,
2576 const unsigned char* prelocs
,
2578 Output_section
* output_section
,
2579 off_t offset_in_output_section
,
2580 const Relocatable_relocs
* rr
,
2581 unsigned char* view
,
2582 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2583 section_size_type view_size
,
2584 unsigned char* reloc_view
,
2585 section_size_type reloc_view_size
)
2587 gold_assert(sh_type
== elfcpp::SHT_REL
);
2589 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2594 offset_in_output_section
,
2603 // Return the value to use for a dynamic which requires special
2604 // treatment. This is how we support equality comparisons of function
2605 // pointers across shared library boundaries, as described in the
2606 // processor specific ABI supplement.
2609 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2611 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2612 return this->plt_section()->address() + gsym
->plt_offset();
2615 // Return a string used to fill a code section with nops to take up
2616 // the specified length.
2619 Target_i386::do_code_fill(section_size_type length
) const
2623 // Build a jmp instruction to skip over the bytes.
2624 unsigned char jmp
[5];
2626 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2627 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2628 + std::string(length
- 5, '\0'));
2631 // Nop sequences of various lengths.
2632 const char nop1
[1] = { 0x90 }; // nop
2633 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2634 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2635 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2636 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2637 0x00 }; // leal 0(%esi,1),%esi
2638 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2640 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2642 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2643 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2644 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2645 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2647 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2648 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2650 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2651 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2653 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2654 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2655 0x00, 0x00, 0x00, 0x00 };
2656 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2657 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2658 0x27, 0x00, 0x00, 0x00,
2660 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2661 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2662 0xbc, 0x27, 0x00, 0x00,
2664 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2665 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2666 0x90, 0x90, 0x90, 0x90,
2669 const char* nops
[16] = {
2671 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2672 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2675 return std::string(nops
[length
], length
);
2678 // The selector for i386 object files.
2680 class Target_selector_i386
: public Target_selector_freebsd
2683 Target_selector_i386()
2684 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
2685 "elf32-i386", "elf32-i386-freebsd")
2689 do_instantiate_target()
2690 { return new Target_i386(); }
2693 Target_selector_i386 target_selector_i386
;
2695 } // End anonymous namespace.