1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_i386
;
47 // The i386 target class.
48 // TLS info comes from
49 // http://people.redhat.com/drepper/tls.pdf
50 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52 class Target_i386
: public Sized_target
<32, false>
55 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
58 : Sized_target
<32, false>(&i386_info
),
59 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
60 copy_relocs_(NULL
), dynbss_(NULL
), got_mod_index_offset_(-1U)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options
& options
,
68 Sized_relobj
<32, false>* object
,
69 unsigned int data_shndx
,
71 const unsigned char* prelocs
,
73 Output_section
* output_section
,
74 bool needs_special_offset_handling
,
75 size_t local_symbol_count
,
76 const unsigned char* plocal_symbols
);
78 // Finalize the sections.
80 do_finalize_sections(Layout
*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol
*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info
<32, false>*,
91 const unsigned char* prelocs
,
93 Output_section
* output_section
,
94 bool needs_special_offset_handling
,
96 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
97 section_size_type view_size
);
99 // Scan the relocs during a relocatable link.
101 scan_relocatable_relocs(const General_options
& options
,
102 Symbol_table
* symtab
,
104 Sized_relobj
<32, false>* object
,
105 unsigned int data_shndx
,
106 unsigned int sh_type
,
107 const unsigned char* prelocs
,
109 Output_section
* output_section
,
110 bool needs_special_offset_handling
,
111 size_t local_symbol_count
,
112 const unsigned char* plocal_symbols
,
113 Relocatable_relocs
*);
115 // Relocate a section during a relocatable link.
117 relocate_for_relocatable(const Relocate_info
<32, false>*,
118 unsigned int sh_type
,
119 const unsigned char* prelocs
,
121 Output_section
* output_section
,
122 off_t offset_in_output_section
,
123 const Relocatable_relocs
*,
125 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
126 section_size_type view_size
,
127 unsigned char* reloc_view
,
128 section_size_type reloc_view_size
);
130 // Return a string used to fill a code section with nops.
132 do_code_fill(section_size_type length
) const;
134 // Return whether SYM is defined by the ABI.
136 do_is_defined_by_abi(Symbol
* sym
) const
137 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
139 // Return the size of the GOT section.
143 gold_assert(this->got_
!= NULL
);
144 return this->got_
->data_size();
148 // The class which scans relocations.
152 local(const General_options
& options
, Symbol_table
* symtab
,
153 Layout
* layout
, Target_i386
* target
,
154 Sized_relobj
<32, false>* object
,
155 unsigned int data_shndx
,
156 Output_section
* output_section
,
157 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
158 const elfcpp::Sym
<32, false>& lsym
);
161 global(const General_options
& options
, Symbol_table
* symtab
,
162 Layout
* layout
, Target_i386
* target
,
163 Sized_relobj
<32, false>* object
,
164 unsigned int data_shndx
,
165 Output_section
* output_section
,
166 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
170 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
173 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
177 // The class which implements relocation.
182 : skip_call_tls_get_addr_(false),
183 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
188 if (this->skip_call_tls_get_addr_
)
190 // FIXME: This needs to specify the location somehow.
191 gold_error(_("missing expected TLS relocation"));
195 // Return whether the static relocation needs to be applied.
197 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
201 // Do a relocation. Return false if the caller should not issue
202 // any warnings about this relocation.
204 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
205 const elfcpp::Rel
<32, false>&,
206 unsigned int r_type
, const Sized_symbol
<32>*,
207 const Symbol_value
<32>*,
208 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
212 // Do a TLS relocation.
214 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
215 size_t relnum
, const elfcpp::Rel
<32, false>&,
216 unsigned int r_type
, const Sized_symbol
<32>*,
217 const Symbol_value
<32>*,
218 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
221 // Do a TLS General-Dynamic to Initial-Exec transition.
223 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
224 Output_segment
* tls_segment
,
225 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
226 elfcpp::Elf_types
<32>::Elf_Addr value
,
228 section_size_type view_size
);
230 // Do a TLS General-Dynamic to Local-Exec transition.
232 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
233 Output_segment
* tls_segment
,
234 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
235 elfcpp::Elf_types
<32>::Elf_Addr value
,
237 section_size_type view_size
);
239 // Do a TLS Local-Dynamic to Local-Exec transition.
241 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
242 Output_segment
* tls_segment
,
243 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
244 elfcpp::Elf_types
<32>::Elf_Addr value
,
246 section_size_type view_size
);
248 // Do a TLS Initial-Exec to Local-Exec transition.
250 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
251 Output_segment
* tls_segment
,
252 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
253 elfcpp::Elf_types
<32>::Elf_Addr value
,
255 section_size_type view_size
);
257 // We need to keep track of which type of local dynamic relocation
258 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
259 enum Local_dynamic_type
266 // This is set if we should skip the next reloc, which should be a
267 // PLT32 reloc against ___tls_get_addr.
268 bool skip_call_tls_get_addr_
;
269 // The type of local dynamic relocation we have seen in the section
270 // being relocated, if any.
271 Local_dynamic_type local_dynamic_type_
;
274 // A class which returns the size required for a relocation type,
275 // used while scanning relocs during a relocatable link.
276 class Relocatable_size_for_reloc
280 get_size_for_reloc(unsigned int, Relobj
*);
283 // Adjust TLS relocation type based on the options and whether this
284 // is a local symbol.
285 static tls::Tls_optimization
286 optimize_tls_reloc(bool is_final
, int r_type
);
288 // Get the GOT section, creating it if necessary.
289 Output_data_got
<32, false>*
290 got_section(Symbol_table
*, Layout
*);
292 // Get the GOT PLT section.
294 got_plt_section() const
296 gold_assert(this->got_plt_
!= NULL
);
297 return this->got_plt_
;
300 // Create a PLT entry for a global symbol.
302 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
304 // Create a GOT entry for the TLS module index.
306 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
307 Sized_relobj
<32, false>* object
);
309 // Get the PLT section.
310 const Output_data_plt_i386
*
313 gold_assert(this->plt_
!= NULL
);
317 // Get the dynamic reloc section, creating it if necessary.
319 rel_dyn_section(Layout
*);
321 // Return true if the symbol may need a COPY relocation.
322 // References from an executable object to non-function symbols
323 // defined in a dynamic object may need a COPY relocation.
325 may_need_copy_reloc(Symbol
* gsym
)
327 return (!parameters
->options().shared()
328 && gsym
->is_from_dynobj()
329 && gsym
->type() != elfcpp::STT_FUNC
);
332 // Copy a relocation against a global symbol.
334 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
335 Sized_relobj
<32, false>*, unsigned int,
336 Output_section
*, Symbol
*, const elfcpp::Rel
<32, false>&);
338 // Information about this specific target which we pass to the
339 // general Target structure.
340 static const Target::Target_info i386_info
;
342 // The types of GOT entries needed for this platform.
345 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
346 GOT_TYPE_TLS_OFFSET
= 1, // GOT entry for TLS offset
347 GOT_TYPE_TLS_PAIR
= 2, // GOT entry for TLS module/offset pair
348 GOT_TYPE_TLS_DESC
= 3 // GOT entry for TLS_DESC pair
352 Output_data_got
<32, false>* got_
;
354 Output_data_plt_i386
* plt_
;
355 // The GOT PLT section.
356 Output_data_space
* got_plt_
;
357 // The dynamic reloc section.
358 Reloc_section
* rel_dyn_
;
359 // Relocs saved to avoid a COPY reloc.
360 Copy_relocs
<32, false>* copy_relocs_
;
361 // Space for variables copied with a COPY reloc.
362 Output_data_space
* dynbss_
;
363 // Offset of the GOT entry for the TLS module index;
364 unsigned int got_mod_index_offset_
;
367 const Target::Target_info
Target_i386::i386_info
=
370 false, // is_big_endian
371 elfcpp::EM_386
, // machine_code
372 false, // has_make_symbol
373 false, // has_resolve
374 true, // has_code_fill
375 true, // is_default_stack_executable
376 "/usr/lib/libc.so.1", // dynamic_linker
377 0x08048000, // default_text_segment_address
378 0x1000, // abi_pagesize (overridable by -z max-page-size)
379 0x1000 // common_pagesize (overridable by -z common-page-size)
382 // Get the GOT section, creating it if necessary.
384 Output_data_got
<32, false>*
385 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
387 if (this->got_
== NULL
)
389 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
391 this->got_
= new Output_data_got
<32, false>();
393 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
394 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
397 // The old GNU linker creates a .got.plt section. We just
398 // create another set of data in the .got section. Note that we
399 // always create a PLT if we create a GOT, although the PLT
401 this->got_plt_
= new Output_data_space(4);
402 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
403 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
406 // The first three entries are reserved.
407 this->got_plt_
->set_current_data_size(3 * 4);
409 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
410 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
412 0, 0, elfcpp::STT_OBJECT
,
414 elfcpp::STV_HIDDEN
, 0,
421 // Get the dynamic reloc section, creating it if necessary.
423 Target_i386::Reloc_section
*
424 Target_i386::rel_dyn_section(Layout
* layout
)
426 if (this->rel_dyn_
== NULL
)
428 gold_assert(layout
!= NULL
);
429 this->rel_dyn_
= new Reloc_section();
430 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
431 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
433 return this->rel_dyn_
;
436 // A class to handle the PLT data.
438 class Output_data_plt_i386
: public Output_section_data
441 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
443 Output_data_plt_i386(Layout
*, Output_data_space
*);
445 // Add an entry to the PLT.
447 add_entry(Symbol
* gsym
);
449 // Return the .rel.plt section data.
452 { return this->rel_
; }
456 do_adjust_output_section(Output_section
* os
);
459 // The size of an entry in the PLT.
460 static const int plt_entry_size
= 16;
462 // The first entry in the PLT for an executable.
463 static unsigned char exec_first_plt_entry
[plt_entry_size
];
465 // The first entry in the PLT for a shared object.
466 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
468 // Other entries in the PLT for an executable.
469 static unsigned char exec_plt_entry
[plt_entry_size
];
471 // Other entries in the PLT for a shared object.
472 static unsigned char dyn_plt_entry
[plt_entry_size
];
474 // Set the final size.
476 set_final_data_size()
477 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
479 // Write out the PLT data.
481 do_write(Output_file
*);
483 // The reloc section.
485 // The .got.plt section.
486 Output_data_space
* got_plt_
;
487 // The number of PLT entries.
491 // Create the PLT section. The ordinary .got section is an argument,
492 // since we need to refer to the start. We also create our own .got
493 // section just for PLT entries.
495 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
496 Output_data_space
* got_plt
)
497 : Output_section_data(4), got_plt_(got_plt
), count_(0)
499 this->rel_
= new Reloc_section();
500 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
501 elfcpp::SHF_ALLOC
, this->rel_
);
505 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
507 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
508 // linker, and so do we.
512 // Add an entry to the PLT.
515 Output_data_plt_i386::add_entry(Symbol
* gsym
)
517 gold_assert(!gsym
->has_plt_offset());
519 // Note that when setting the PLT offset we skip the initial
520 // reserved PLT entry.
521 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
525 section_offset_type got_offset
= this->got_plt_
->current_data_size();
527 // Every PLT entry needs a GOT entry which points back to the PLT
528 // entry (this will be changed by the dynamic linker, normally
529 // lazily when the function is called).
530 this->got_plt_
->set_current_data_size(got_offset
+ 4);
532 // Every PLT entry needs a reloc.
533 gsym
->set_needs_dynsym_entry();
534 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
537 // Note that we don't need to save the symbol. The contents of the
538 // PLT are independent of which symbols are used. The symbols only
539 // appear in the relocations.
542 // The first entry in the PLT for an executable.
544 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
546 0xff, 0x35, // pushl contents of memory address
547 0, 0, 0, 0, // replaced with address of .got + 4
548 0xff, 0x25, // jmp indirect
549 0, 0, 0, 0, // replaced with address of .got + 8
553 // The first entry in the PLT for a shared object.
555 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
557 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
558 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
562 // Subsequent entries in the PLT for an executable.
564 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
566 0xff, 0x25, // jmp indirect
567 0, 0, 0, 0, // replaced with address of symbol in .got
568 0x68, // pushl immediate
569 0, 0, 0, 0, // replaced with offset into relocation table
570 0xe9, // jmp relative
571 0, 0, 0, 0 // replaced with offset to start of .plt
574 // Subsequent entries in the PLT for a shared object.
576 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
578 0xff, 0xa3, // jmp *offset(%ebx)
579 0, 0, 0, 0, // replaced with offset of symbol in .got
580 0x68, // pushl immediate
581 0, 0, 0, 0, // replaced with offset into relocation table
582 0xe9, // jmp relative
583 0, 0, 0, 0 // replaced with offset to start of .plt
586 // Write out the PLT. This uses the hand-coded instructions above,
587 // and adjusts them as needed. This is all specified by the i386 ELF
588 // Processor Supplement.
591 Output_data_plt_i386::do_write(Output_file
* of
)
593 const off_t offset
= this->offset();
594 const section_size_type oview_size
=
595 convert_to_section_size_type(this->data_size());
596 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
598 const off_t got_file_offset
= this->got_plt_
->offset();
599 const section_size_type got_size
=
600 convert_to_section_size_type(this->got_plt_
->data_size());
601 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
604 unsigned char* pov
= oview
;
606 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
607 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
609 if (parameters
->options().shared())
610 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
613 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
614 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
615 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
617 pov
+= plt_entry_size
;
619 unsigned char* got_pov
= got_view
;
621 memset(got_pov
, 0, 12);
624 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
626 unsigned int plt_offset
= plt_entry_size
;
627 unsigned int plt_rel_offset
= 0;
628 unsigned int got_offset
= 12;
629 const unsigned int count
= this->count_
;
630 for (unsigned int i
= 0;
633 pov
+= plt_entry_size
,
635 plt_offset
+= plt_entry_size
,
636 plt_rel_offset
+= rel_size
,
639 // Set and adjust the PLT entry itself.
641 if (parameters
->options().shared())
643 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
644 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
648 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
649 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
654 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
655 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
656 - (plt_offset
+ plt_entry_size
));
658 // Set the entry in the GOT.
659 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
662 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
663 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
665 of
->write_output_view(offset
, oview_size
, oview
);
666 of
->write_output_view(got_file_offset
, got_size
, got_view
);
669 // Create a PLT entry for a global symbol.
672 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
674 if (gsym
->has_plt_offset())
677 if (this->plt_
== NULL
)
679 // Create the GOT sections first.
680 this->got_section(symtab
, layout
);
682 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
683 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
685 | elfcpp::SHF_EXECINSTR
),
689 this->plt_
->add_entry(gsym
);
692 // Create a GOT entry for the TLS module index.
695 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
696 Sized_relobj
<32, false>* object
)
698 if (this->got_mod_index_offset_
== -1U)
700 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
701 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
702 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
703 unsigned int got_offset
= got
->add_constant(0);
704 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
706 got
->add_constant(0);
707 this->got_mod_index_offset_
= got_offset
;
709 return this->got_mod_index_offset_
;
712 // Handle a relocation against a non-function symbol defined in a
713 // dynamic object. The traditional way to handle this is to generate
714 // a COPY relocation to copy the variable at runtime from the shared
715 // object into the executable's data segment. However, this is
716 // undesirable in general, as if the size of the object changes in the
717 // dynamic object, the executable will no longer work correctly. If
718 // this relocation is in a writable section, then we can create a
719 // dynamic reloc and the dynamic linker will resolve it to the correct
720 // address at runtime. However, we do not want do that if the
721 // relocation is in a read-only section, as it would prevent the
722 // readonly segment from being shared. And if we have to eventually
723 // generate a COPY reloc, then any dynamic relocations will be
724 // useless. So this means that if this is a writable section, we need
725 // to save the relocation until we see whether we have to create a
726 // COPY relocation for this symbol for any other relocation.
729 Target_i386::copy_reloc(const General_options
* options
,
730 Symbol_table
* symtab
,
732 Sized_relobj
<32, false>* object
,
733 unsigned int data_shndx
,
734 Output_section
* output_section
,
736 const elfcpp::Rel
<32, false>& rel
)
738 Sized_symbol
<32>* ssym
= symtab
->get_sized_symbol
<32>(gsym
);
740 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
743 // So far we do not need a COPY reloc. Save this relocation.
744 // If it turns out that we never need a COPY reloc for this
745 // symbol, then we will emit the relocation.
746 if (this->copy_relocs_
== NULL
)
747 this->copy_relocs_
= new Copy_relocs
<32, false>();
748 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rel
);
752 // Allocate space for this symbol in the .bss section.
754 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
756 // There is no defined way to determine the required alignment
757 // of the symbol. We pick the alignment based on the size. We
758 // set an arbitrary maximum of 256.
760 for (align
= 1; align
< 512; align
<<= 1)
761 if ((symsize
& align
) != 0)
764 if (this->dynbss_
== NULL
)
766 this->dynbss_
= new Output_data_space(align
);
767 layout
->add_output_section_data(".bss",
770 | elfcpp::SHF_WRITE
),
774 Output_data_space
* dynbss
= this->dynbss_
;
776 if (align
> dynbss
->addralign())
777 dynbss
->set_space_alignment(align
);
779 section_size_type dynbss_size
=
780 convert_to_section_size_type(dynbss
->current_data_size());
781 dynbss_size
= align_address(dynbss_size
, align
);
782 section_size_type offset
= dynbss_size
;
783 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
785 symtab
->define_with_copy_reloc(ssym
, dynbss
, offset
);
787 // Add the COPY reloc.
788 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
789 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
793 // Optimize the TLS relocation type based on what we know about the
794 // symbol. IS_FINAL is true if the final address of this symbol is
795 // known at link time.
797 tls::Tls_optimization
798 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
800 // If we are generating a shared library, then we can't do anything
802 if (parameters
->options().shared())
803 return tls::TLSOPT_NONE
;
807 case elfcpp::R_386_TLS_GD
:
808 case elfcpp::R_386_TLS_GOTDESC
:
809 case elfcpp::R_386_TLS_DESC_CALL
:
810 // These are General-Dynamic which permits fully general TLS
811 // access. Since we know that we are generating an executable,
812 // we can convert this to Initial-Exec. If we also know that
813 // this is a local symbol, we can further switch to Local-Exec.
815 return tls::TLSOPT_TO_LE
;
816 return tls::TLSOPT_TO_IE
;
818 case elfcpp::R_386_TLS_LDM
:
819 // This is Local-Dynamic, which refers to a local symbol in the
820 // dynamic TLS block. Since we know that we generating an
821 // executable, we can switch to Local-Exec.
822 return tls::TLSOPT_TO_LE
;
824 case elfcpp::R_386_TLS_LDO_32
:
825 // Another type of Local-Dynamic relocation.
826 return tls::TLSOPT_TO_LE
;
828 case elfcpp::R_386_TLS_IE
:
829 case elfcpp::R_386_TLS_GOTIE
:
830 case elfcpp::R_386_TLS_IE_32
:
831 // These are Initial-Exec relocs which get the thread offset
832 // from the GOT. If we know that we are linking against the
833 // local symbol, we can switch to Local-Exec, which links the
834 // thread offset into the instruction.
836 return tls::TLSOPT_TO_LE
;
837 return tls::TLSOPT_NONE
;
839 case elfcpp::R_386_TLS_LE
:
840 case elfcpp::R_386_TLS_LE_32
:
841 // When we already have Local-Exec, there is nothing further we
843 return tls::TLSOPT_NONE
;
850 // Report an unsupported relocation against a local symbol.
853 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
856 gold_error(_("%s: unsupported reloc %u against local symbol"),
857 object
->name().c_str(), r_type
);
860 // Scan a relocation for a local symbol.
863 Target_i386::Scan::local(const General_options
&,
864 Symbol_table
* symtab
,
867 Sized_relobj
<32, false>* object
,
868 unsigned int data_shndx
,
869 Output_section
* output_section
,
870 const elfcpp::Rel
<32, false>& reloc
,
872 const elfcpp::Sym
<32, false>& lsym
)
876 case elfcpp::R_386_NONE
:
877 case elfcpp::R_386_GNU_VTINHERIT
:
878 case elfcpp::R_386_GNU_VTENTRY
:
881 case elfcpp::R_386_32
:
882 // If building a shared library (or a position-independent
883 // executable), we need to create a dynamic relocation for
884 // this location. The relocation applied at link time will
885 // apply the link-time value, so we flag the location with
886 // an R_386_RELATIVE relocation so the dynamic loader can
887 // relocate it easily.
888 if (parameters
->options().output_is_position_independent())
890 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
891 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
892 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
893 output_section
, data_shndx
,
894 reloc
.get_r_offset());
898 case elfcpp::R_386_16
:
899 case elfcpp::R_386_8
:
900 // If building a shared library (or a position-independent
901 // executable), we need to create a dynamic relocation for
902 // this location. Because the addend needs to remain in the
903 // data section, we need to be careful not to apply this
904 // relocation statically.
905 if (parameters
->options().output_is_position_independent())
907 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
908 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
910 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
911 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
912 data_shndx
, reloc
.get_r_offset());
916 gold_assert(lsym
.get_st_value() == 0);
917 rel_dyn
->add_local_section(object
, lsym
.get_st_shndx(),
918 r_type
, output_section
,
919 data_shndx
, reloc
.get_r_offset());
924 case elfcpp::R_386_PC32
:
925 case elfcpp::R_386_PC16
:
926 case elfcpp::R_386_PC8
:
929 case elfcpp::R_386_PLT32
:
930 // Since we know this is a local symbol, we can handle this as a
934 case elfcpp::R_386_GOTOFF
:
935 case elfcpp::R_386_GOTPC
:
936 // We need a GOT section.
937 target
->got_section(symtab
, layout
);
940 case elfcpp::R_386_GOT32
:
942 // The symbol requires a GOT entry.
943 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
944 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
945 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
947 // If we are generating a shared object, we need to add a
948 // dynamic RELATIVE relocation for this symbol's GOT entry.
949 if (parameters
->options().output_is_position_independent())
951 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
952 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
953 rel_dyn
->add_local_relative(
954 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
955 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
961 // These are relocations which should only be seen by the
962 // dynamic linker, and should never be seen here.
963 case elfcpp::R_386_COPY
:
964 case elfcpp::R_386_GLOB_DAT
:
965 case elfcpp::R_386_JUMP_SLOT
:
966 case elfcpp::R_386_RELATIVE
:
967 case elfcpp::R_386_TLS_TPOFF
:
968 case elfcpp::R_386_TLS_DTPMOD32
:
969 case elfcpp::R_386_TLS_DTPOFF32
:
970 case elfcpp::R_386_TLS_TPOFF32
:
971 case elfcpp::R_386_TLS_DESC
:
972 gold_error(_("%s: unexpected reloc %u in object file"),
973 object
->name().c_str(), r_type
);
976 // These are initial TLS relocs, which are expected when
978 case elfcpp::R_386_TLS_GD
: // Global-dynamic
979 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
980 case elfcpp::R_386_TLS_DESC_CALL
:
981 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
982 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
983 case elfcpp::R_386_TLS_IE
: // Initial-exec
984 case elfcpp::R_386_TLS_IE_32
:
985 case elfcpp::R_386_TLS_GOTIE
:
986 case elfcpp::R_386_TLS_LE
: // Local-exec
987 case elfcpp::R_386_TLS_LE_32
:
989 bool output_is_shared
= parameters
->options().shared();
990 const tls::Tls_optimization optimized_type
991 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
994 case elfcpp::R_386_TLS_GD
: // Global-dynamic
995 if (optimized_type
== tls::TLSOPT_NONE
)
997 // Create a pair of GOT entries for the module index and
998 // dtv-relative offset.
999 Output_data_got
<32, false>* got
1000 = target
->got_section(symtab
, layout
);
1001 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1002 got
->add_local_pair_with_rel(object
, r_sym
,
1003 lsym
.get_st_shndx(),
1005 target
->rel_dyn_section(layout
),
1006 elfcpp::R_386_TLS_DTPMOD32
, 0);
1008 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1009 unsupported_reloc_local(object
, r_type
);
1012 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1013 case elfcpp::R_386_TLS_DESC_CALL
:
1014 // FIXME: If not relaxing to LE, we need to generate
1015 // a GOT entry with an R_386_TLS_DESC reloc.
1016 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1017 unsupported_reloc_local(object
, r_type
);
1020 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1021 if (optimized_type
== tls::TLSOPT_NONE
)
1023 // Create a GOT entry for the module index.
1024 target
->got_mod_index_entry(symtab
, layout
, object
);
1026 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1027 unsupported_reloc_local(object
, r_type
);
1030 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1033 case elfcpp::R_386_TLS_IE
: // Initial-exec
1034 case elfcpp::R_386_TLS_IE_32
:
1035 case elfcpp::R_386_TLS_GOTIE
:
1036 layout
->set_has_static_tls();
1037 if (optimized_type
== tls::TLSOPT_NONE
)
1039 // For the R_386_TLS_IE relocation, we need to create a
1040 // dynamic relocation when building a shared library.
1041 if (r_type
== elfcpp::R_386_TLS_IE
1042 && parameters
->options().shared())
1044 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1046 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1047 rel_dyn
->add_local_relative(object
, r_sym
,
1048 elfcpp::R_386_RELATIVE
,
1049 output_section
, data_shndx
,
1050 reloc
.get_r_offset());
1052 // Create a GOT entry for the tp-relative offset.
1053 Output_data_got
<32, false>* got
1054 = target
->got_section(symtab
, layout
);
1055 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1056 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1057 ? elfcpp::R_386_TLS_TPOFF32
1058 : elfcpp::R_386_TLS_TPOFF
);
1059 got
->add_local_with_rel(object
, r_sym
, GOT_TYPE_TLS_OFFSET
,
1060 target
->rel_dyn_section(layout
),
1063 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1064 unsupported_reloc_local(object
, r_type
);
1067 case elfcpp::R_386_TLS_LE
: // Local-exec
1068 case elfcpp::R_386_TLS_LE_32
:
1069 layout
->set_has_static_tls();
1070 if (output_is_shared
)
1072 // We need to create a dynamic relocation.
1073 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1074 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1075 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1076 ? elfcpp::R_386_TLS_TPOFF32
1077 : elfcpp::R_386_TLS_TPOFF
);
1078 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1079 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1080 data_shndx
, reloc
.get_r_offset());
1090 case elfcpp::R_386_32PLT
:
1091 case elfcpp::R_386_TLS_GD_32
:
1092 case elfcpp::R_386_TLS_GD_PUSH
:
1093 case elfcpp::R_386_TLS_GD_CALL
:
1094 case elfcpp::R_386_TLS_GD_POP
:
1095 case elfcpp::R_386_TLS_LDM_32
:
1096 case elfcpp::R_386_TLS_LDM_PUSH
:
1097 case elfcpp::R_386_TLS_LDM_CALL
:
1098 case elfcpp::R_386_TLS_LDM_POP
:
1099 case elfcpp::R_386_USED_BY_INTEL_200
:
1101 unsupported_reloc_local(object
, r_type
);
1106 // Report an unsupported relocation against a global symbol.
1109 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1110 unsigned int r_type
,
1113 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1114 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1117 // Scan a relocation for a global symbol.
1120 Target_i386::Scan::global(const General_options
& options
,
1121 Symbol_table
* symtab
,
1123 Target_i386
* target
,
1124 Sized_relobj
<32, false>* object
,
1125 unsigned int data_shndx
,
1126 Output_section
* output_section
,
1127 const elfcpp::Rel
<32, false>& reloc
,
1128 unsigned int r_type
,
1133 case elfcpp::R_386_NONE
:
1134 case elfcpp::R_386_GNU_VTINHERIT
:
1135 case elfcpp::R_386_GNU_VTENTRY
:
1138 case elfcpp::R_386_32
:
1139 case elfcpp::R_386_16
:
1140 case elfcpp::R_386_8
:
1142 // Make a PLT entry if necessary.
1143 if (gsym
->needs_plt_entry())
1145 target
->make_plt_entry(symtab
, layout
, gsym
);
1146 // Since this is not a PC-relative relocation, we may be
1147 // taking the address of a function. In that case we need to
1148 // set the entry in the dynamic symbol table to the address of
1150 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1151 gsym
->set_needs_dynsym_value();
1153 // Make a dynamic relocation if necessary.
1154 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1156 if (target
->may_need_copy_reloc(gsym
))
1158 target
->copy_reloc(&options
, symtab
, layout
, object
,
1159 data_shndx
, output_section
, gsym
, reloc
);
1161 else if (r_type
== elfcpp::R_386_32
1162 && gsym
->can_use_relative_reloc(false))
1164 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1165 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1166 output_section
, object
,
1167 data_shndx
, reloc
.get_r_offset());
1171 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1172 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1173 data_shndx
, reloc
.get_r_offset());
1179 case elfcpp::R_386_PC32
:
1180 case elfcpp::R_386_PC16
:
1181 case elfcpp::R_386_PC8
:
1183 // Make a PLT entry if necessary.
1184 if (gsym
->needs_plt_entry())
1186 // These relocations are used for function calls only in
1187 // non-PIC code. For a 32-bit relocation in a shared library,
1188 // we'll need a text relocation anyway, so we can skip the
1189 // PLT entry and let the dynamic linker bind the call directly
1190 // to the target. For smaller relocations, we should use a
1191 // PLT entry to ensure that the call can reach.
1192 if (!parameters
->options().shared()
1193 || r_type
!= elfcpp::R_386_PC32
)
1194 target
->make_plt_entry(symtab
, layout
, gsym
);
1196 // Make a dynamic relocation if necessary.
1197 int flags
= Symbol::NON_PIC_REF
;
1198 if (gsym
->type() == elfcpp::STT_FUNC
)
1199 flags
|= Symbol::FUNCTION_CALL
;
1200 if (gsym
->needs_dynamic_reloc(flags
))
1202 if (target
->may_need_copy_reloc(gsym
))
1204 target
->copy_reloc(&options
, symtab
, layout
, object
,
1205 data_shndx
, output_section
, gsym
, reloc
);
1209 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1210 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1211 data_shndx
, reloc
.get_r_offset());
1217 case elfcpp::R_386_GOT32
:
1219 // The symbol requires a GOT entry.
1220 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1221 if (gsym
->final_value_is_known())
1222 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1225 // If this symbol is not fully resolved, we need to add a
1226 // GOT entry with a dynamic relocation.
1227 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1228 if (gsym
->is_from_dynobj()
1229 || gsym
->is_undefined()
1230 || gsym
->is_preemptible())
1231 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1232 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1235 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1236 rel_dyn
->add_global_relative(
1237 gsym
, elfcpp::R_386_RELATIVE
, got
,
1238 gsym
->got_offset(GOT_TYPE_STANDARD
));
1244 case elfcpp::R_386_PLT32
:
1245 // If the symbol is fully resolved, this is just a PC32 reloc.
1246 // Otherwise we need a PLT entry.
1247 if (gsym
->final_value_is_known())
1249 // If building a shared library, we can also skip the PLT entry
1250 // if the symbol is defined in the output file and is protected
1252 if (gsym
->is_defined()
1253 && !gsym
->is_from_dynobj()
1254 && !gsym
->is_preemptible())
1256 target
->make_plt_entry(symtab
, layout
, gsym
);
1259 case elfcpp::R_386_GOTOFF
:
1260 case elfcpp::R_386_GOTPC
:
1261 // We need a GOT section.
1262 target
->got_section(symtab
, layout
);
1265 // These are relocations which should only be seen by the
1266 // dynamic linker, and should never be seen here.
1267 case elfcpp::R_386_COPY
:
1268 case elfcpp::R_386_GLOB_DAT
:
1269 case elfcpp::R_386_JUMP_SLOT
:
1270 case elfcpp::R_386_RELATIVE
:
1271 case elfcpp::R_386_TLS_TPOFF
:
1272 case elfcpp::R_386_TLS_DTPMOD32
:
1273 case elfcpp::R_386_TLS_DTPOFF32
:
1274 case elfcpp::R_386_TLS_TPOFF32
:
1275 case elfcpp::R_386_TLS_DESC
:
1276 gold_error(_("%s: unexpected reloc %u in object file"),
1277 object
->name().c_str(), r_type
);
1280 // These are initial tls relocs, which are expected when
1282 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1283 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1284 case elfcpp::R_386_TLS_DESC_CALL
:
1285 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1286 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1287 case elfcpp::R_386_TLS_IE
: // Initial-exec
1288 case elfcpp::R_386_TLS_IE_32
:
1289 case elfcpp::R_386_TLS_GOTIE
:
1290 case elfcpp::R_386_TLS_LE
: // Local-exec
1291 case elfcpp::R_386_TLS_LE_32
:
1293 const bool is_final
= gsym
->final_value_is_known();
1294 const tls::Tls_optimization optimized_type
1295 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1298 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1299 if (optimized_type
== tls::TLSOPT_NONE
)
1301 // Create a pair of GOT entries for the module index and
1302 // dtv-relative offset.
1303 Output_data_got
<32, false>* got
1304 = target
->got_section(symtab
, layout
);
1305 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1306 target
->rel_dyn_section(layout
),
1307 elfcpp::R_386_TLS_DTPMOD32
,
1308 elfcpp::R_386_TLS_DTPOFF32
);
1310 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1312 // Create a GOT entry for the tp-relative offset.
1313 Output_data_got
<32, false>* got
1314 = target
->got_section(symtab
, layout
);
1315 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
1316 target
->rel_dyn_section(layout
),
1317 elfcpp::R_386_TLS_TPOFF32
);
1319 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1320 unsupported_reloc_global(object
, r_type
, gsym
);
1323 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1324 case elfcpp::R_386_TLS_DESC_CALL
:
1325 // FIXME: If not relaxing to LE, we need to generate
1326 // a GOT entry with an R_386_TLS_DESC reloc.
1327 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1328 unsupported_reloc_global(object
, r_type
, gsym
);
1329 unsupported_reloc_global(object
, r_type
, gsym
);
1332 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1333 if (optimized_type
== tls::TLSOPT_NONE
)
1335 // Create a GOT entry for the module index.
1336 target
->got_mod_index_entry(symtab
, layout
, object
);
1338 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1339 unsupported_reloc_global(object
, r_type
, gsym
);
1342 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1345 case elfcpp::R_386_TLS_IE
: // Initial-exec
1346 case elfcpp::R_386_TLS_IE_32
:
1347 case elfcpp::R_386_TLS_GOTIE
:
1348 layout
->set_has_static_tls();
1349 if (optimized_type
== tls::TLSOPT_NONE
)
1351 // For the R_386_TLS_IE relocation, we need to create a
1352 // dynamic relocation when building a shared library.
1353 if (r_type
== elfcpp::R_386_TLS_IE
1354 && parameters
->options().shared())
1356 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1357 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1358 output_section
, object
,
1360 reloc
.get_r_offset());
1362 // Create a GOT entry for the tp-relative offset.
1363 Output_data_got
<32, false>* got
1364 = target
->got_section(symtab
, layout
);
1365 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1366 ? elfcpp::R_386_TLS_TPOFF32
1367 : elfcpp::R_386_TLS_TPOFF
);
1368 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_OFFSET
,
1369 target
->rel_dyn_section(layout
),
1372 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1373 unsupported_reloc_global(object
, r_type
, gsym
);
1376 case elfcpp::R_386_TLS_LE
: // Local-exec
1377 case elfcpp::R_386_TLS_LE_32
:
1378 layout
->set_has_static_tls();
1379 if (parameters
->options().shared())
1381 // We need to create a dynamic relocation.
1382 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1383 ? elfcpp::R_386_TLS_TPOFF32
1384 : elfcpp::R_386_TLS_TPOFF
);
1385 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1386 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1387 data_shndx
, reloc
.get_r_offset());
1397 case elfcpp::R_386_32PLT
:
1398 case elfcpp::R_386_TLS_GD_32
:
1399 case elfcpp::R_386_TLS_GD_PUSH
:
1400 case elfcpp::R_386_TLS_GD_CALL
:
1401 case elfcpp::R_386_TLS_GD_POP
:
1402 case elfcpp::R_386_TLS_LDM_32
:
1403 case elfcpp::R_386_TLS_LDM_PUSH
:
1404 case elfcpp::R_386_TLS_LDM_CALL
:
1405 case elfcpp::R_386_TLS_LDM_POP
:
1406 case elfcpp::R_386_USED_BY_INTEL_200
:
1408 unsupported_reloc_global(object
, r_type
, gsym
);
1413 // Scan relocations for a section.
1416 Target_i386::scan_relocs(const General_options
& options
,
1417 Symbol_table
* symtab
,
1419 Sized_relobj
<32, false>* object
,
1420 unsigned int data_shndx
,
1421 unsigned int sh_type
,
1422 const unsigned char* prelocs
,
1424 Output_section
* output_section
,
1425 bool needs_special_offset_handling
,
1426 size_t local_symbol_count
,
1427 const unsigned char* plocal_symbols
)
1429 if (sh_type
== elfcpp::SHT_RELA
)
1431 gold_error(_("%s: unsupported RELA reloc section"),
1432 object
->name().c_str());
1436 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1447 needs_special_offset_handling
,
1452 // Finalize the sections.
1455 Target_i386::do_finalize_sections(Layout
* layout
)
1457 // Fill in some more dynamic tags.
1458 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1461 if (this->got_plt_
!= NULL
)
1462 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1464 if (this->plt_
!= NULL
)
1466 const Output_data
* od
= this->plt_
->rel_plt();
1467 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1468 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1469 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1472 if (this->rel_dyn_
!= NULL
)
1474 const Output_data
* od
= this->rel_dyn_
;
1475 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1476 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1477 odyn
->add_constant(elfcpp::DT_RELENT
,
1478 elfcpp::Elf_sizes
<32>::rel_size
);
1481 if (!parameters
->options().shared())
1483 // The value of the DT_DEBUG tag is filled in by the dynamic
1484 // linker at run time, and used by the debugger.
1485 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1489 // Emit any relocs we saved in an attempt to avoid generating COPY
1491 if (this->copy_relocs_
== NULL
)
1493 if (this->copy_relocs_
->any_to_emit())
1495 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1496 this->copy_relocs_
->emit(rel_dyn
);
1498 delete this->copy_relocs_
;
1499 this->copy_relocs_
= NULL
;
1502 // Return whether a direct absolute static relocation needs to be applied.
1503 // In cases where Scan::local() or Scan::global() has created
1504 // a dynamic relocation other than R_386_RELATIVE, the addend
1505 // of the relocation is carried in the data, and we must not
1506 // apply the static relocation.
1509 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1513 // For local symbols, we will have created a non-RELATIVE dynamic
1514 // relocation only if (a) the output is position independent,
1515 // (b) the relocation is absolute (not pc- or segment-relative), and
1516 // (c) the relocation is not 32 bits wide.
1518 return !(parameters
->options().output_is_position_independent()
1519 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1522 // For global symbols, we use the same helper routines used in the
1523 // scan pass. If we did not create a dynamic relocation, or if we
1524 // created a RELATIVE dynamic relocation, we should apply the static
1526 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1527 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1528 && gsym
->can_use_relative_reloc(ref_flags
1529 & Symbol::FUNCTION_CALL
);
1530 return !has_dyn
|| is_rel
;
1533 // Perform a relocation.
1536 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1537 Target_i386
* target
,
1539 const elfcpp::Rel
<32, false>& rel
,
1540 unsigned int r_type
,
1541 const Sized_symbol
<32>* gsym
,
1542 const Symbol_value
<32>* psymval
,
1543 unsigned char* view
,
1544 elfcpp::Elf_types
<32>::Elf_Addr address
,
1545 section_size_type view_size
)
1547 if (this->skip_call_tls_get_addr_
)
1549 if (r_type
!= elfcpp::R_386_PLT32
1551 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1552 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1553 _("missing expected TLS relocation"));
1556 this->skip_call_tls_get_addr_
= false;
1561 // Pick the value to use for symbols defined in shared objects.
1562 Symbol_value
<32> symval
;
1563 bool is_nonpic
= (r_type
== elfcpp::R_386_PC8
1564 || r_type
== elfcpp::R_386_PC16
1565 || r_type
== elfcpp::R_386_PC32
);
1567 && (gsym
->is_from_dynobj()
1568 || (parameters
->options().shared()
1569 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1570 && gsym
->has_plt_offset()
1571 && (!is_nonpic
|| !parameters
->options().shared()))
1573 symval
.set_output_value(target
->plt_section()->address()
1574 + gsym
->plt_offset());
1578 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1580 // Get the GOT offset if needed.
1581 // The GOT pointer points to the end of the GOT section.
1582 // We need to subtract the size of the GOT section to get
1583 // the actual offset to use in the relocation.
1584 bool have_got_offset
= false;
1585 unsigned int got_offset
= 0;
1588 case elfcpp::R_386_GOT32
:
1591 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1592 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1593 - target
->got_size());
1597 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1598 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1599 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1600 - target
->got_size());
1602 have_got_offset
= true;
1611 case elfcpp::R_386_NONE
:
1612 case elfcpp::R_386_GNU_VTINHERIT
:
1613 case elfcpp::R_386_GNU_VTENTRY
:
1616 case elfcpp::R_386_32
:
1617 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true))
1618 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1621 case elfcpp::R_386_PC32
:
1623 int ref_flags
= Symbol::NON_PIC_REF
;
1624 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1625 ref_flags
|= Symbol::FUNCTION_CALL
;
1626 if (should_apply_static_reloc(gsym
, ref_flags
, true))
1627 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1631 case elfcpp::R_386_16
:
1632 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1633 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1636 case elfcpp::R_386_PC16
:
1638 int ref_flags
= Symbol::NON_PIC_REF
;
1639 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1640 ref_flags
|= Symbol::FUNCTION_CALL
;
1641 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1642 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1646 case elfcpp::R_386_8
:
1647 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1648 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1651 case elfcpp::R_386_PC8
:
1653 int ref_flags
= Symbol::NON_PIC_REF
;
1654 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1655 ref_flags
|= Symbol::FUNCTION_CALL
;
1656 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1657 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1661 case elfcpp::R_386_PLT32
:
1662 gold_assert(gsym
== NULL
1663 || gsym
->has_plt_offset()
1664 || gsym
->final_value_is_known()
1665 || (gsym
->is_defined()
1666 && !gsym
->is_from_dynobj()
1667 && !gsym
->is_preemptible()));
1668 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1671 case elfcpp::R_386_GOT32
:
1672 gold_assert(have_got_offset
);
1673 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1676 case elfcpp::R_386_GOTOFF
:
1678 elfcpp::Elf_types
<32>::Elf_Addr value
;
1679 value
= (psymval
->value(object
, 0)
1680 - target
->got_plt_section()->address());
1681 Relocate_functions
<32, false>::rel32(view
, value
);
1685 case elfcpp::R_386_GOTPC
:
1687 elfcpp::Elf_types
<32>::Elf_Addr value
;
1688 value
= target
->got_plt_section()->address();
1689 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1693 case elfcpp::R_386_COPY
:
1694 case elfcpp::R_386_GLOB_DAT
:
1695 case elfcpp::R_386_JUMP_SLOT
:
1696 case elfcpp::R_386_RELATIVE
:
1697 // These are outstanding tls relocs, which are unexpected when
1699 case elfcpp::R_386_TLS_TPOFF
:
1700 case elfcpp::R_386_TLS_DTPMOD32
:
1701 case elfcpp::R_386_TLS_DTPOFF32
:
1702 case elfcpp::R_386_TLS_TPOFF32
:
1703 case elfcpp::R_386_TLS_DESC
:
1704 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1705 _("unexpected reloc %u in object file"),
1709 // These are initial tls relocs, which are expected when
1711 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1712 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1713 case elfcpp::R_386_TLS_DESC_CALL
:
1714 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1715 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1716 case elfcpp::R_386_TLS_IE
: // Initial-exec
1717 case elfcpp::R_386_TLS_IE_32
:
1718 case elfcpp::R_386_TLS_GOTIE
:
1719 case elfcpp::R_386_TLS_LE
: // Local-exec
1720 case elfcpp::R_386_TLS_LE_32
:
1721 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1722 view
, address
, view_size
);
1725 case elfcpp::R_386_32PLT
:
1726 case elfcpp::R_386_TLS_GD_32
:
1727 case elfcpp::R_386_TLS_GD_PUSH
:
1728 case elfcpp::R_386_TLS_GD_CALL
:
1729 case elfcpp::R_386_TLS_GD_POP
:
1730 case elfcpp::R_386_TLS_LDM_32
:
1731 case elfcpp::R_386_TLS_LDM_PUSH
:
1732 case elfcpp::R_386_TLS_LDM_CALL
:
1733 case elfcpp::R_386_TLS_LDM_POP
:
1734 case elfcpp::R_386_USED_BY_INTEL_200
:
1736 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1737 _("unsupported reloc %u"),
1745 // Perform a TLS relocation.
1748 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1749 Target_i386
* target
,
1751 const elfcpp::Rel
<32, false>& rel
,
1752 unsigned int r_type
,
1753 const Sized_symbol
<32>* gsym
,
1754 const Symbol_value
<32>* psymval
,
1755 unsigned char* view
,
1756 elfcpp::Elf_types
<32>::Elf_Addr
,
1757 section_size_type view_size
)
1759 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1761 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1763 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1765 const bool is_final
=
1767 ? !parameters
->options().output_is_position_independent()
1768 : gsym
->final_value_is_known());
1769 const tls::Tls_optimization optimized_type
1770 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1773 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1774 if (optimized_type
== tls::TLSOPT_TO_LE
)
1776 gold_assert(tls_segment
!= NULL
);
1777 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1778 rel
, r_type
, value
, view
,
1784 unsigned int got_offset
;
1787 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_PAIR
));
1788 got_offset
= (gsym
->got_offset(GOT_TYPE_TLS_PAIR
)
1789 - target
->got_size());
1793 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1794 gold_assert(object
->local_has_got_offset(r_sym
,
1795 GOT_TYPE_TLS_PAIR
));
1796 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_TLS_PAIR
)
1797 - target
->got_size());
1799 if (optimized_type
== tls::TLSOPT_TO_IE
)
1801 gold_assert(tls_segment
!= NULL
);
1802 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1803 got_offset
, view
, view_size
);
1806 else if (optimized_type
== tls::TLSOPT_NONE
)
1808 // Relocate the field with the offset of the pair of GOT
1810 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1814 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1815 _("unsupported reloc %u"),
1819 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1820 case elfcpp::R_386_TLS_DESC_CALL
:
1821 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1822 _("unsupported reloc %u"),
1826 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1827 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1829 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1830 _("both SUN and GNU model "
1831 "TLS relocations"));
1834 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1835 if (optimized_type
== tls::TLSOPT_TO_LE
)
1837 gold_assert(tls_segment
!= NULL
);
1838 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1839 value
, view
, view_size
);
1842 else if (optimized_type
== tls::TLSOPT_NONE
)
1844 // Relocate the field with the offset of the GOT entry for
1845 // the module index.
1846 unsigned int got_offset
;
1847 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1848 - target
->got_size());
1849 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1852 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1853 _("unsupported reloc %u"),
1857 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1858 // This reloc can appear in debugging sections, in which case we
1859 // won't see the TLS_LDM reloc. The local_dynamic_type field
1861 if (optimized_type
== tls::TLSOPT_TO_LE
)
1863 gold_assert(tls_segment
!= NULL
);
1864 value
-= tls_segment
->memsz();
1866 Relocate_functions
<32, false>::rel32(view
, value
);
1869 case elfcpp::R_386_TLS_IE
: // Initial-exec
1870 case elfcpp::R_386_TLS_GOTIE
:
1871 case elfcpp::R_386_TLS_IE_32
:
1872 if (optimized_type
== tls::TLSOPT_TO_LE
)
1874 gold_assert(tls_segment
!= NULL
);
1875 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1876 rel
, r_type
, value
, view
,
1880 else if (optimized_type
== tls::TLSOPT_NONE
)
1882 // Relocate the field with the offset of the GOT entry for
1883 // the tp-relative offset of the symbol.
1884 unsigned int got_offset
;
1887 gold_assert(gsym
->has_got_offset(GOT_TYPE_TLS_OFFSET
));
1888 got_offset
= gsym
->got_offset(GOT_TYPE_TLS_OFFSET
);
1892 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1893 gold_assert(object
->local_has_got_offset(r_sym
,
1894 GOT_TYPE_TLS_OFFSET
));
1895 got_offset
= object
->local_got_offset(r_sym
,
1896 GOT_TYPE_TLS_OFFSET
);
1898 // For the R_386_TLS_IE relocation, we need to apply the
1899 // absolute address of the GOT entry.
1900 if (r_type
== elfcpp::R_386_TLS_IE
)
1901 got_offset
+= target
->got_plt_section()->address();
1902 // All GOT offsets are relative to the end of the GOT.
1903 got_offset
-= target
->got_size();
1904 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1907 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1908 _("unsupported reloc %u"),
1912 case elfcpp::R_386_TLS_LE
: // Local-exec
1913 // If we're creating a shared library, a dynamic relocation will
1914 // have been created for this location, so do not apply it now.
1915 if (!parameters
->options().shared())
1917 gold_assert(tls_segment
!= NULL
);
1918 value
-= tls_segment
->memsz();
1919 Relocate_functions
<32, false>::rel32(view
, value
);
1923 case elfcpp::R_386_TLS_LE_32
:
1924 // If we're creating a shared library, a dynamic relocation will
1925 // have been created for this location, so do not apply it now.
1926 if (!parameters
->options().shared())
1928 gold_assert(tls_segment
!= NULL
);
1929 value
= tls_segment
->memsz() - value
;
1930 Relocate_functions
<32, false>::rel32(view
, value
);
1936 // Do a relocation in which we convert a TLS General-Dynamic to a
1940 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1942 Output_segment
* tls_segment
,
1943 const elfcpp::Rel
<32, false>& rel
,
1945 elfcpp::Elf_types
<32>::Elf_Addr value
,
1946 unsigned char* view
,
1947 section_size_type view_size
)
1949 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1950 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1951 // leal foo(%reg),%eax; call ___tls_get_addr
1952 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1954 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1955 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1957 unsigned char op1
= view
[-1];
1958 unsigned char op2
= view
[-2];
1960 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1961 op2
== 0x8d || op2
== 0x04);
1962 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1968 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1969 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1970 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1971 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1972 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1976 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1977 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1978 if (rel
.get_r_offset() + 9 < view_size
1981 // There is a trailing nop. Use the size byte subl.
1982 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1987 // Use the five byte subl.
1988 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1992 value
= tls_segment
->memsz() - value
;
1993 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1995 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1997 this->skip_call_tls_get_addr_
= true;
2000 // Do a relocation in which we convert a TLS General-Dynamic to an
2004 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2006 Output_segment
* tls_segment
,
2007 const elfcpp::Rel
<32, false>& rel
,
2009 elfcpp::Elf_types
<32>::Elf_Addr value
,
2010 unsigned char* view
,
2011 section_size_type view_size
)
2013 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2014 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2016 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2017 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2019 unsigned char op1
= view
[-1];
2020 unsigned char op2
= view
[-2];
2022 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2023 op2
== 0x8d || op2
== 0x04);
2024 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2028 // FIXME: For now, support only one form.
2029 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2030 op1
== 0x8d && op2
== 0x04);
2034 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2035 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2036 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2037 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2038 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2042 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2043 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2044 if (rel
.get_r_offset() + 9 < view_size
2047 // FIXME: This is not the right instruction sequence.
2048 // There is a trailing nop. Use the size byte subl.
2049 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2054 // FIXME: This is not the right instruction sequence.
2055 // Use the five byte subl.
2056 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2060 value
= tls_segment
->memsz() - value
;
2061 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2063 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2065 this->skip_call_tls_get_addr_
= true;
2068 // Do a relocation in which we convert a TLS Local-Dynamic to a
2072 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2075 const elfcpp::Rel
<32, false>& rel
,
2077 elfcpp::Elf_types
<32>::Elf_Addr
,
2078 unsigned char* view
,
2079 section_size_type view_size
)
2081 // leal foo(%reg), %eax; call ___tls_get_addr
2082 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2084 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2085 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2087 // FIXME: Does this test really always pass?
2088 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2089 view
[-2] == 0x8d && view
[-1] == 0x83);
2091 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2093 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2095 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2097 this->skip_call_tls_get_addr_
= true;
2100 // Do a relocation in which we convert a TLS Initial-Exec to a
2104 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2106 Output_segment
* tls_segment
,
2107 const elfcpp::Rel
<32, false>& rel
,
2108 unsigned int r_type
,
2109 elfcpp::Elf_types
<32>::Elf_Addr value
,
2110 unsigned char* view
,
2111 section_size_type view_size
)
2113 // We have to actually change the instructions, which means that we
2114 // need to examine the opcodes to figure out which instruction we
2116 if (r_type
== elfcpp::R_386_TLS_IE
)
2118 // movl %gs:XX,%eax ==> movl $YY,%eax
2119 // movl %gs:XX,%reg ==> movl $YY,%reg
2120 // addl %gs:XX,%reg ==> addl $YY,%reg
2121 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2122 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2124 unsigned char op1
= view
[-1];
2127 // movl XX,%eax ==> movl $YY,%eax
2132 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2134 unsigned char op2
= view
[-2];
2137 // movl XX,%reg ==> movl $YY,%reg
2138 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2139 (op1
& 0xc7) == 0x05);
2141 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2143 else if (op2
== 0x03)
2145 // addl XX,%reg ==> addl $YY,%reg
2146 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2147 (op1
& 0xc7) == 0x05);
2149 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2152 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2157 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2158 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2159 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2160 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2161 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2163 unsigned char op1
= view
[-1];
2164 unsigned char op2
= view
[-2];
2165 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2166 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2169 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2171 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2173 else if (op2
== 0x2b)
2175 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2177 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2179 else if (op2
== 0x03)
2181 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2183 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2186 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2189 value
= tls_segment
->memsz() - value
;
2190 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2193 Relocate_functions
<32, false>::rel32(view
, value
);
2196 // Relocate section data.
2199 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2200 unsigned int sh_type
,
2201 const unsigned char* prelocs
,
2203 Output_section
* output_section
,
2204 bool needs_special_offset_handling
,
2205 unsigned char* view
,
2206 elfcpp::Elf_types
<32>::Elf_Addr address
,
2207 section_size_type view_size
)
2209 gold_assert(sh_type
== elfcpp::SHT_REL
);
2211 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2212 Target_i386::Relocate
>(
2218 needs_special_offset_handling
,
2224 // Return the size of a relocation while scanning during a relocatable
2228 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2229 unsigned int r_type
,
2234 case elfcpp::R_386_NONE
:
2235 case elfcpp::R_386_GNU_VTINHERIT
:
2236 case elfcpp::R_386_GNU_VTENTRY
:
2237 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2238 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2239 case elfcpp::R_386_TLS_DESC_CALL
:
2240 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2241 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2242 case elfcpp::R_386_TLS_IE
: // Initial-exec
2243 case elfcpp::R_386_TLS_IE_32
:
2244 case elfcpp::R_386_TLS_GOTIE
:
2245 case elfcpp::R_386_TLS_LE
: // Local-exec
2246 case elfcpp::R_386_TLS_LE_32
:
2249 case elfcpp::R_386_32
:
2250 case elfcpp::R_386_PC32
:
2251 case elfcpp::R_386_GOT32
:
2252 case elfcpp::R_386_PLT32
:
2253 case elfcpp::R_386_GOTOFF
:
2254 case elfcpp::R_386_GOTPC
:
2257 case elfcpp::R_386_16
:
2258 case elfcpp::R_386_PC16
:
2261 case elfcpp::R_386_8
:
2262 case elfcpp::R_386_PC8
:
2265 // These are relocations which should only be seen by the
2266 // dynamic linker, and should never be seen here.
2267 case elfcpp::R_386_COPY
:
2268 case elfcpp::R_386_GLOB_DAT
:
2269 case elfcpp::R_386_JUMP_SLOT
:
2270 case elfcpp::R_386_RELATIVE
:
2271 case elfcpp::R_386_TLS_TPOFF
:
2272 case elfcpp::R_386_TLS_DTPMOD32
:
2273 case elfcpp::R_386_TLS_DTPOFF32
:
2274 case elfcpp::R_386_TLS_TPOFF32
:
2275 case elfcpp::R_386_TLS_DESC
:
2276 object
->error(_("unexpected reloc %u in object file"), r_type
);
2279 case elfcpp::R_386_32PLT
:
2280 case elfcpp::R_386_TLS_GD_32
:
2281 case elfcpp::R_386_TLS_GD_PUSH
:
2282 case elfcpp::R_386_TLS_GD_CALL
:
2283 case elfcpp::R_386_TLS_GD_POP
:
2284 case elfcpp::R_386_TLS_LDM_32
:
2285 case elfcpp::R_386_TLS_LDM_PUSH
:
2286 case elfcpp::R_386_TLS_LDM_CALL
:
2287 case elfcpp::R_386_TLS_LDM_POP
:
2288 case elfcpp::R_386_USED_BY_INTEL_200
:
2290 object
->error(_("unsupported reloc %u in object file"), r_type
);
2295 // Scan the relocs during a relocatable link.
2298 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2299 Symbol_table
* symtab
,
2301 Sized_relobj
<32, false>* object
,
2302 unsigned int data_shndx
,
2303 unsigned int sh_type
,
2304 const unsigned char* prelocs
,
2306 Output_section
* output_section
,
2307 bool needs_special_offset_handling
,
2308 size_t local_symbol_count
,
2309 const unsigned char* plocal_symbols
,
2310 Relocatable_relocs
* rr
)
2312 gold_assert(sh_type
== elfcpp::SHT_REL
);
2314 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2315 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2317 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2318 Scan_relocatable_relocs
>(
2327 needs_special_offset_handling
,
2333 // Relocate a section during a relocatable link.
2336 Target_i386::relocate_for_relocatable(
2337 const Relocate_info
<32, false>* relinfo
,
2338 unsigned int sh_type
,
2339 const unsigned char* prelocs
,
2341 Output_section
* output_section
,
2342 off_t offset_in_output_section
,
2343 const Relocatable_relocs
* rr
,
2344 unsigned char* view
,
2345 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2346 section_size_type view_size
,
2347 unsigned char* reloc_view
,
2348 section_size_type reloc_view_size
)
2350 gold_assert(sh_type
== elfcpp::SHT_REL
);
2352 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2357 offset_in_output_section
,
2366 // Return the value to use for a dynamic which requires special
2367 // treatment. This is how we support equality comparisons of function
2368 // pointers across shared library boundaries, as described in the
2369 // processor specific ABI supplement.
2372 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2374 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2375 return this->plt_section()->address() + gsym
->plt_offset();
2378 // Return a string used to fill a code section with nops to take up
2379 // the specified length.
2382 Target_i386::do_code_fill(section_size_type length
) const
2386 // Build a jmp instruction to skip over the bytes.
2387 unsigned char jmp
[5];
2389 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2390 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2391 + std::string(length
- 5, '\0'));
2394 // Nop sequences of various lengths.
2395 const char nop1
[1] = { 0x90 }; // nop
2396 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2397 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2398 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2399 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2400 0x00 }; // leal 0(%esi,1),%esi
2401 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2403 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2405 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2406 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2407 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2408 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2410 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2411 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2413 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2414 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2416 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2417 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2418 0x00, 0x00, 0x00, 0x00 };
2419 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2420 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2421 0x27, 0x00, 0x00, 0x00,
2423 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2424 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2425 0xbc, 0x27, 0x00, 0x00,
2427 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2428 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2429 0x90, 0x90, 0x90, 0x90,
2432 const char* nops
[16] = {
2434 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2435 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2438 return std::string(nops
[length
], length
);
2441 // The selector for i386 object files.
2443 class Target_selector_i386
: public Target_selector
2446 Target_selector_i386()
2447 : Target_selector(elfcpp::EM_386
, 32, false, "elf32-i386")
2451 do_instantiate_target()
2452 { return new Target_i386(); }
2455 Target_selector_i386 target_selector_i386
;
2457 } // End anonymous namespace.