1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007 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
)
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
,
99 // Return a string used to fill a code section with nops.
101 do_code_fill(off_t length
);
103 // Return the size of the GOT section.
107 gold_assert(this->got_
!= NULL
);
108 return this->got_
->data_size();
112 // The class which scans relocations.
116 local(const General_options
& options
, Symbol_table
* symtab
,
117 Layout
* layout
, Target_i386
* target
,
118 Sized_relobj
<32, false>* object
,
119 unsigned int data_shndx
,
120 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
121 const elfcpp::Sym
<32, false>& lsym
);
124 global(const General_options
& options
, Symbol_table
* symtab
,
125 Layout
* layout
, Target_i386
* target
,
126 Sized_relobj
<32, false>* object
,
127 unsigned int data_shndx
,
128 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
132 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
135 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
139 // The class which implements relocation.
144 : skip_call_tls_get_addr_(false),
145 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
150 if (this->skip_call_tls_get_addr_
)
152 // FIXME: This needs to specify the location somehow.
153 gold_error(_("missing expected TLS relocation"));
157 // Return whether the static relocation needs to be applied.
159 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
163 // Do a relocation. Return false if the caller should not issue
164 // any warnings about this relocation.
166 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
167 const elfcpp::Rel
<32, false>&,
168 unsigned int r_type
, const Sized_symbol
<32>*,
169 const Symbol_value
<32>*,
170 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
174 // Do a TLS relocation.
176 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
177 const elfcpp::Rel
<32, false>&,
178 unsigned int r_type
, const Sized_symbol
<32>*,
179 const Symbol_value
<32>*,
180 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
182 // Do a TLS General-Dynamic to Local-Exec transition.
184 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
185 Output_segment
* tls_segment
,
186 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
187 elfcpp::Elf_types
<32>::Elf_Addr value
,
191 // Do a TLS Local-Dynamic to Local-Exec transition.
193 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
194 Output_segment
* tls_segment
,
195 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
196 elfcpp::Elf_types
<32>::Elf_Addr value
,
200 // Do a TLS Initial-Exec to Local-Exec transition.
202 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
203 Output_segment
* tls_segment
,
204 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
205 elfcpp::Elf_types
<32>::Elf_Addr value
,
209 // We need to keep track of which type of local dynamic relocation
210 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
211 enum Local_dynamic_type
218 // This is set if we should skip the next reloc, which should be a
219 // PLT32 reloc against ___tls_get_addr.
220 bool skip_call_tls_get_addr_
;
221 // The type of local dynamic relocation we have seen in the section
222 // being relocated, if any.
223 Local_dynamic_type local_dynamic_type_
;
226 // Adjust TLS relocation type based on the options and whether this
227 // is a local symbol.
228 static tls::Tls_optimization
229 optimize_tls_reloc(bool is_final
, int r_type
);
231 // Get the GOT section, creating it if necessary.
232 Output_data_got
<32, false>*
233 got_section(Symbol_table
*, Layout
*);
235 // Get the GOT PLT section.
237 got_plt_section() const
239 gold_assert(this->got_plt_
!= NULL
);
240 return this->got_plt_
;
243 // Create a PLT entry for a global symbol.
245 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
247 // Get the PLT section.
248 const Output_data_plt_i386
*
251 gold_assert(this->plt_
!= NULL
);
255 // Get the dynamic reloc section, creating it if necessary.
257 rel_dyn_section(Layout
*);
259 // Copy a relocation against a global symbol.
261 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
262 Sized_relobj
<32, false>*, unsigned int,
263 Symbol
*, const elfcpp::Rel
<32, false>&);
265 // Information about this specific target which we pass to the
266 // general Target structure.
267 static const Target::Target_info i386_info
;
270 Output_data_got
<32, false>* got_
;
272 Output_data_plt_i386
* plt_
;
273 // The GOT PLT section.
274 Output_data_space
* got_plt_
;
275 // The dynamic reloc section.
276 Reloc_section
* rel_dyn_
;
277 // Relocs saved to avoid a COPY reloc.
278 Copy_relocs
<32, false>* copy_relocs_
;
279 // Space for variables copied with a COPY reloc.
280 Output_data_space
* dynbss_
;
283 const Target::Target_info
Target_i386::i386_info
=
286 false, // is_big_endian
287 elfcpp::EM_386
, // machine_code
288 false, // has_make_symbol
289 false, // has_resolve
290 true, // has_code_fill
291 true, // is_default_stack_executable
292 "/usr/lib/libc.so.1", // dynamic_linker
293 0x08048000, // default_text_segment_address
294 0x1000, // abi_pagesize
295 0x1000 // common_pagesize
298 // Get the GOT section, creating it if necessary.
300 Output_data_got
<32, false>*
301 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
303 if (this->got_
== NULL
)
305 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
307 this->got_
= new Output_data_got
<32, false>();
309 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
310 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
313 // The old GNU linker creates a .got.plt section. We just
314 // create another set of data in the .got section. Note that we
315 // always create a PLT if we create a GOT, although the PLT
317 this->got_plt_
= new Output_data_space(4);
318 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
319 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
322 // The first three entries are reserved.
323 this->got_plt_
->set_space_size(3 * 4);
325 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
326 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
328 0, 0, elfcpp::STT_OBJECT
,
330 elfcpp::STV_HIDDEN
, 0,
337 // Get the dynamic reloc section, creating it if necessary.
339 Target_i386::Reloc_section
*
340 Target_i386::rel_dyn_section(Layout
* layout
)
342 if (this->rel_dyn_
== NULL
)
344 gold_assert(layout
!= NULL
);
345 this->rel_dyn_
= new Reloc_section();
346 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
347 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
349 return this->rel_dyn_
;
352 // A class to handle the PLT data.
354 class Output_data_plt_i386
: public Output_section_data
357 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
359 Output_data_plt_i386(Layout
*, Output_data_space
*);
361 // Add an entry to the PLT.
363 add_entry(Symbol
* gsym
);
365 // Return the .rel.plt section data.
368 { return this->rel_
; }
372 do_adjust_output_section(Output_section
* os
);
375 // The size of an entry in the PLT.
376 static const int plt_entry_size
= 16;
378 // The first entry in the PLT for an executable.
379 static unsigned char exec_first_plt_entry
[plt_entry_size
];
381 // The first entry in the PLT for a shared object.
382 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
384 // Other entries in the PLT for an executable.
385 static unsigned char exec_plt_entry
[plt_entry_size
];
387 // Other entries in the PLT for a shared object.
388 static unsigned char dyn_plt_entry
[plt_entry_size
];
390 // Set the final size.
392 do_set_address(uint64_t, off_t
)
393 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
395 // Write out the PLT data.
397 do_write(Output_file
*);
399 // The reloc section.
401 // The .got.plt section.
402 Output_data_space
* got_plt_
;
403 // The number of PLT entries.
407 // Create the PLT section. The ordinary .got section is an argument,
408 // since we need to refer to the start. We also create our own .got
409 // section just for PLT entries.
411 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
412 Output_data_space
* got_plt
)
413 : Output_section_data(4), got_plt_(got_plt
), count_(0)
415 this->rel_
= new Reloc_section();
416 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
417 elfcpp::SHF_ALLOC
, this->rel_
);
421 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
423 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
424 // linker, and so do we.
428 // Add an entry to the PLT.
431 Output_data_plt_i386::add_entry(Symbol
* gsym
)
433 gold_assert(!gsym
->has_plt_offset());
435 // Note that when setting the PLT offset we skip the initial
436 // reserved PLT entry.
437 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
441 off_t got_offset
= this->got_plt_
->data_size();
443 // Every PLT entry needs a GOT entry which points back to the PLT
444 // entry (this will be changed by the dynamic linker, normally
445 // lazily when the function is called).
446 this->got_plt_
->set_space_size(got_offset
+ 4);
448 // Every PLT entry needs a reloc.
449 gsym
->set_needs_dynsym_entry();
450 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
453 // Note that we don't need to save the symbol. The contents of the
454 // PLT are independent of which symbols are used. The symbols only
455 // appear in the relocations.
458 // The first entry in the PLT for an executable.
460 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
462 0xff, 0x35, // pushl contents of memory address
463 0, 0, 0, 0, // replaced with address of .got + 4
464 0xff, 0x25, // jmp indirect
465 0, 0, 0, 0, // replaced with address of .got + 8
469 // The first entry in the PLT for a shared object.
471 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
473 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
474 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
478 // Subsequent entries in the PLT for an executable.
480 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
482 0xff, 0x25, // jmp indirect
483 0, 0, 0, 0, // replaced with address of symbol in .got
484 0x68, // pushl immediate
485 0, 0, 0, 0, // replaced with offset into relocation table
486 0xe9, // jmp relative
487 0, 0, 0, 0 // replaced with offset to start of .plt
490 // Subsequent entries in the PLT for a shared object.
492 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
494 0xff, 0xa3, // jmp *offset(%ebx)
495 0, 0, 0, 0, // replaced with offset of symbol in .got
496 0x68, // pushl immediate
497 0, 0, 0, 0, // replaced with offset into relocation table
498 0xe9, // jmp relative
499 0, 0, 0, 0 // replaced with offset to start of .plt
502 // Write out the PLT. This uses the hand-coded instructions above,
503 // and adjusts them as needed. This is all specified by the i386 ELF
504 // Processor Supplement.
507 Output_data_plt_i386::do_write(Output_file
* of
)
509 const off_t offset
= this->offset();
510 const off_t oview_size
= this->data_size();
511 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
513 const off_t got_file_offset
= this->got_plt_
->offset();
514 const off_t got_size
= this->got_plt_
->data_size();
515 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
518 unsigned char* pov
= oview
;
520 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
521 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
523 if (parameters
->output_is_shared())
524 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
527 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
528 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
529 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
531 pov
+= plt_entry_size
;
533 unsigned char* got_pov
= got_view
;
535 memset(got_pov
, 0, 12);
538 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
540 unsigned int plt_offset
= plt_entry_size
;
541 unsigned int plt_rel_offset
= 0;
542 unsigned int got_offset
= 12;
543 const unsigned int count
= this->count_
;
544 for (unsigned int i
= 0;
547 pov
+= plt_entry_size
,
549 plt_offset
+= plt_entry_size
,
550 plt_rel_offset
+= rel_size
,
553 // Set and adjust the PLT entry itself.
555 if (parameters
->output_is_shared())
557 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
558 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
562 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
563 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
568 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
569 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
570 - (plt_offset
+ plt_entry_size
));
572 // Set the entry in the GOT.
573 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
576 gold_assert(pov
- oview
== oview_size
);
577 gold_assert(got_pov
- got_view
== got_size
);
579 of
->write_output_view(offset
, oview_size
, oview
);
580 of
->write_output_view(got_file_offset
, got_size
, got_view
);
583 // Create a PLT entry for a global symbol.
586 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
588 if (gsym
->has_plt_offset())
591 if (this->plt_
== NULL
)
593 // Create the GOT sections first.
594 this->got_section(symtab
, layout
);
596 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
597 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
599 | elfcpp::SHF_EXECINSTR
),
603 this->plt_
->add_entry(gsym
);
606 // Handle a relocation against a non-function symbol defined in a
607 // dynamic object. The traditional way to handle this is to generate
608 // a COPY relocation to copy the variable at runtime from the shared
609 // object into the executable's data segment. However, this is
610 // undesirable in general, as if the size of the object changes in the
611 // dynamic object, the executable will no longer work correctly. If
612 // this relocation is in a writable section, then we can create a
613 // dynamic reloc and the dynamic linker will resolve it to the correct
614 // address at runtime. However, we do not want do that if the
615 // relocation is in a read-only section, as it would prevent the
616 // readonly segment from being shared. And if we have to eventually
617 // generate a COPY reloc, then any dynamic relocations will be
618 // useless. So this means that if this is a writable section, we need
619 // to save the relocation until we see whether we have to create a
620 // COPY relocation for this symbol for any other relocation.
623 Target_i386::copy_reloc(const General_options
* options
,
624 Symbol_table
* symtab
,
626 Sized_relobj
<32, false>* object
,
627 unsigned int data_shndx
, Symbol
* gsym
,
628 const elfcpp::Rel
<32, false>& rel
)
630 Sized_symbol
<32>* ssym
;
631 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
634 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
637 // So far we do not need a COPY reloc. Save this relocation.
638 // If it turns out that we never need a COPY reloc for this
639 // symbol, then we will emit the relocation.
640 if (this->copy_relocs_
== NULL
)
641 this->copy_relocs_
= new Copy_relocs
<32, false>();
642 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
646 // Allocate space for this symbol in the .bss section.
648 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
650 // There is no defined way to determine the required alignment
651 // of the symbol. We pick the alignment based on the size. We
652 // set an arbitrary maximum of 256.
654 for (align
= 1; align
< 512; align
<<= 1)
655 if ((symsize
& align
) != 0)
658 if (this->dynbss_
== NULL
)
660 this->dynbss_
= new Output_data_space(align
);
661 layout
->add_output_section_data(".bss",
664 | elfcpp::SHF_WRITE
),
668 Output_data_space
* dynbss
= this->dynbss_
;
670 if (align
> dynbss
->addralign())
671 dynbss
->set_space_alignment(align
);
673 off_t dynbss_size
= dynbss
->data_size();
674 dynbss_size
= align_address(dynbss_size
, align
);
675 off_t offset
= dynbss_size
;
676 dynbss
->set_space_size(dynbss_size
+ symsize
);
678 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
680 // Add the COPY reloc.
681 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
682 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
686 // Optimize the TLS relocation type based on what we know about the
687 // symbol. IS_FINAL is true if the final address of this symbol is
688 // known at link time.
690 tls::Tls_optimization
691 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
693 // If we are generating a shared library, then we can't do anything
695 if (parameters
->output_is_shared())
696 return tls::TLSOPT_NONE
;
700 case elfcpp::R_386_TLS_GD
:
701 case elfcpp::R_386_TLS_GOTDESC
:
702 case elfcpp::R_386_TLS_DESC_CALL
:
703 // These are General-Dynamic which permits fully general TLS
704 // access. Since we know that we are generating an executable,
705 // we can convert this to Initial-Exec. If we also know that
706 // this is a local symbol, we can further switch to Local-Exec.
708 return tls::TLSOPT_TO_LE
;
709 return tls::TLSOPT_TO_IE
;
711 case elfcpp::R_386_TLS_LDM
:
712 // This is Local-Dynamic, which refers to a local symbol in the
713 // dynamic TLS block. Since we know that we generating an
714 // executable, we can switch to Local-Exec.
715 return tls::TLSOPT_TO_LE
;
717 case elfcpp::R_386_TLS_LDO_32
:
718 // Another type of Local-Dynamic relocation.
719 return tls::TLSOPT_TO_LE
;
721 case elfcpp::R_386_TLS_IE
:
722 case elfcpp::R_386_TLS_GOTIE
:
723 case elfcpp::R_386_TLS_IE_32
:
724 // These are Initial-Exec relocs which get the thread offset
725 // from the GOT. If we know that we are linking against the
726 // local symbol, we can switch to Local-Exec, which links the
727 // thread offset into the instruction.
729 return tls::TLSOPT_TO_LE
;
730 return tls::TLSOPT_NONE
;
732 case elfcpp::R_386_TLS_LE
:
733 case elfcpp::R_386_TLS_LE_32
:
734 // When we already have Local-Exec, there is nothing further we
736 return tls::TLSOPT_NONE
;
743 // Report an unsupported relocation against a local symbol.
746 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
749 gold_error(_("%s: unsupported reloc %u against local symbol"),
750 object
->name().c_str(), r_type
);
753 // Scan a relocation for a local symbol.
756 Target_i386::Scan::local(const General_options
&,
757 Symbol_table
* symtab
,
760 Sized_relobj
<32, false>* object
,
761 unsigned int data_shndx
,
762 const elfcpp::Rel
<32, false>& reloc
,
764 const elfcpp::Sym
<32, false>&)
768 case elfcpp::R_386_NONE
:
769 case elfcpp::R_386_GNU_VTINHERIT
:
770 case elfcpp::R_386_GNU_VTENTRY
:
773 case elfcpp::R_386_32
:
774 case elfcpp::R_386_16
:
775 case elfcpp::R_386_8
:
776 // If building a shared library (or a position-independent
777 // executable), we need to create a dynamic relocation for
778 // this location. The relocation applied at link time will
779 // apply the link-time value, so we flag the location with
780 // an R_386_RELATIVE relocation so the dynamic loader can
781 // relocate it easily.
782 if (parameters
->output_is_position_independent())
784 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
785 if (r_type
== elfcpp::R_386_32
)
786 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
787 reloc
.get_r_offset());
790 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
791 rel_dyn
->add_local(object
, r_sym
, r_type
, data_shndx
,
792 reloc
.get_r_offset());
797 case elfcpp::R_386_PC32
:
798 case elfcpp::R_386_PC16
:
799 case elfcpp::R_386_PC8
:
802 case elfcpp::R_386_PLT32
:
803 // Since we know this is a local symbol, we can handle this as a
807 case elfcpp::R_386_GOTOFF
:
808 case elfcpp::R_386_GOTPC
:
809 // We need a GOT section.
810 target
->got_section(symtab
, layout
);
813 case elfcpp::R_386_GOT32
:
815 // The symbol requires a GOT entry.
816 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
817 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
818 if (got
->add_local(object
, r_sym
))
820 // If we are generating a shared object, we need to add a
821 // dynamic RELATIVE relocation for this symbol.
822 if (parameters
->output_is_position_independent())
824 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
825 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
826 data_shndx
, reloc
.get_r_offset());
832 // These are relocations which should only be seen by the
833 // dynamic linker, and should never be seen here.
834 case elfcpp::R_386_COPY
:
835 case elfcpp::R_386_GLOB_DAT
:
836 case elfcpp::R_386_JUMP_SLOT
:
837 case elfcpp::R_386_RELATIVE
:
838 case elfcpp::R_386_TLS_TPOFF
:
839 case elfcpp::R_386_TLS_DTPMOD32
:
840 case elfcpp::R_386_TLS_DTPOFF32
:
841 case elfcpp::R_386_TLS_TPOFF32
:
842 case elfcpp::R_386_TLS_DESC
:
843 gold_error(_("%s: unexpected reloc %u in object file"),
844 object
->name().c_str(), r_type
);
847 // These are initial TLS relocs, which are expected when
849 case elfcpp::R_386_TLS_GD
: // Global-dynamic
850 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
851 case elfcpp::R_386_TLS_DESC_CALL
:
852 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
853 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
854 case elfcpp::R_386_TLS_IE
: // Initial-exec
855 case elfcpp::R_386_TLS_IE_32
:
856 case elfcpp::R_386_TLS_GOTIE
:
857 case elfcpp::R_386_TLS_LE
: // Local-exec
858 case elfcpp::R_386_TLS_LE_32
:
860 bool output_is_shared
= parameters
->output_is_shared();
861 const tls::Tls_optimization optimized_type
862 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
865 case elfcpp::R_386_TLS_GD
: // Global-dynamic
866 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
867 case elfcpp::R_386_TLS_DESC_CALL
:
868 // FIXME: If not relaxing to LE, we need to generate
869 // DTPMOD32 and DTPOFF32 relocs.
870 if (optimized_type
!= tls::TLSOPT_TO_LE
)
871 unsupported_reloc_local(object
, r_type
);
874 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
875 // FIXME: If not relaxing to LE, we need to generate a
877 if (optimized_type
!= tls::TLSOPT_TO_LE
)
878 unsupported_reloc_local(object
, r_type
);
881 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
884 case elfcpp::R_386_TLS_IE
: // Initial-exec
885 case elfcpp::R_386_TLS_IE_32
:
886 case elfcpp::R_386_TLS_GOTIE
:
887 // FIXME: If not relaxing to LE, we need to generate a
888 // TPOFF or TPOFF32 reloc.
889 if (optimized_type
!= tls::TLSOPT_TO_LE
)
890 unsupported_reloc_local(object
, r_type
);
893 case elfcpp::R_386_TLS_LE
: // Local-exec
894 case elfcpp::R_386_TLS_LE_32
:
895 // FIXME: If generating a shared object, we need to copy
896 // this relocation into the object.
897 gold_assert(!output_is_shared
);
906 case elfcpp::R_386_32PLT
:
907 case elfcpp::R_386_TLS_GD_32
:
908 case elfcpp::R_386_TLS_GD_PUSH
:
909 case elfcpp::R_386_TLS_GD_CALL
:
910 case elfcpp::R_386_TLS_GD_POP
:
911 case elfcpp::R_386_TLS_LDM_32
:
912 case elfcpp::R_386_TLS_LDM_PUSH
:
913 case elfcpp::R_386_TLS_LDM_CALL
:
914 case elfcpp::R_386_TLS_LDM_POP
:
915 case elfcpp::R_386_USED_BY_INTEL_200
:
917 unsupported_reloc_local(object
, r_type
);
922 // Report an unsupported relocation against a global symbol.
925 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
929 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
930 object
->name().c_str(), r_type
, gsym
->name());
933 // Scan a relocation for a global symbol.
936 Target_i386::Scan::global(const General_options
& options
,
937 Symbol_table
* symtab
,
940 Sized_relobj
<32, false>* object
,
941 unsigned int data_shndx
,
942 const elfcpp::Rel
<32, false>& reloc
,
948 case elfcpp::R_386_NONE
:
949 case elfcpp::R_386_GNU_VTINHERIT
:
950 case elfcpp::R_386_GNU_VTENTRY
:
953 case elfcpp::R_386_32
:
954 case elfcpp::R_386_PC32
:
955 case elfcpp::R_386_16
:
956 case elfcpp::R_386_PC16
:
957 case elfcpp::R_386_8
:
958 case elfcpp::R_386_PC8
:
960 bool is_pcrel
= (r_type
== elfcpp::R_386_PC32
961 || r_type
== elfcpp::R_386_PC16
962 || r_type
== elfcpp::R_386_PC8
);
964 if (gsym
->is_from_dynobj()
965 || (parameters
->output_is_shared()
966 && gsym
->is_preemptible()))
968 // (a) This symbol is defined in a dynamic object. If it is a
969 // function, we make a PLT entry. Otherwise we need to
970 // either generate a COPY reloc or copy this reloc.
971 // (b) We are building a shared object and this symbol is
972 // preemptible. If it is a function, we make a PLT entry.
973 // Otherwise, we copy the reloc.
974 if (gsym
->type() == elfcpp::STT_FUNC
)
976 target
->make_plt_entry(symtab
, layout
, gsym
);
978 // If this is not a PC relative reference, then we may
979 // be taking the address of the function. In that case
980 // we need to set the entry in the dynamic symbol table
981 // to the address of the PLT entry. We will also need to
982 // create a dynamic relocation.
985 if (gsym
->is_from_dynobj())
986 gsym
->set_needs_dynsym_value();
987 if (parameters
->output_is_position_independent())
989 Reloc_section
* rel_dyn
=
990 target
->rel_dyn_section(layout
);
991 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
992 reloc
.get_r_offset());
996 else if (parameters
->output_is_shared())
998 // We do not make COPY relocs in shared objects.
999 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1000 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
1001 reloc
.get_r_offset());
1004 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
1007 else if (!is_pcrel
&& parameters
->output_is_position_independent())
1009 // This is not a PC-relative reference, so we need to generate
1010 // a dynamic relocation. At this point, we know the symbol
1011 // is not preemptible, so we can use the RELATIVE relocation.
1012 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1013 if (r_type
== elfcpp::R_386_32
)
1014 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
1015 reloc
.get_r_offset());
1017 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
1018 reloc
.get_r_offset());
1023 case elfcpp::R_386_GOT32
:
1025 // The symbol requires a GOT entry.
1026 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1027 if (got
->add_global(gsym
))
1029 // If this symbol is not fully resolved, we need to add a
1030 // dynamic relocation for it.
1031 if (!gsym
->final_value_is_known())
1033 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1034 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
1035 gsym
->got_offset());
1041 case elfcpp::R_386_PLT32
:
1042 // If the symbol is fully resolved, this is just a PC32 reloc.
1043 // Otherwise we need a PLT entry.
1044 if (gsym
->final_value_is_known())
1046 // If building a shared library, we can also skip the PLT entry
1047 // if the symbol is defined in the output file and is protected
1049 if (gsym
->is_defined()
1050 && !gsym
->is_from_dynobj()
1051 && !gsym
->is_preemptible())
1053 target
->make_plt_entry(symtab
, layout
, gsym
);
1056 case elfcpp::R_386_GOTOFF
:
1057 case elfcpp::R_386_GOTPC
:
1058 // We need a GOT section.
1059 target
->got_section(symtab
, layout
);
1062 // These are relocations which should only be seen by the
1063 // dynamic linker, and should never be seen here.
1064 case elfcpp::R_386_COPY
:
1065 case elfcpp::R_386_GLOB_DAT
:
1066 case elfcpp::R_386_JUMP_SLOT
:
1067 case elfcpp::R_386_RELATIVE
:
1068 case elfcpp::R_386_TLS_TPOFF
:
1069 case elfcpp::R_386_TLS_DTPMOD32
:
1070 case elfcpp::R_386_TLS_DTPOFF32
:
1071 case elfcpp::R_386_TLS_TPOFF32
:
1072 case elfcpp::R_386_TLS_DESC
:
1073 gold_error(_("%s: unexpected reloc %u in object file"),
1074 object
->name().c_str(), r_type
);
1077 // These are initial tls relocs, which are expected when
1079 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1080 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1081 case elfcpp::R_386_TLS_DESC_CALL
:
1082 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1083 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1084 case elfcpp::R_386_TLS_IE
: // Initial-exec
1085 case elfcpp::R_386_TLS_IE_32
:
1086 case elfcpp::R_386_TLS_GOTIE
:
1087 case elfcpp::R_386_TLS_LE
: // Local-exec
1088 case elfcpp::R_386_TLS_LE_32
:
1090 const bool is_final
= gsym
->final_value_is_known();
1091 const tls::Tls_optimization optimized_type
1092 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1095 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1096 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1097 case elfcpp::R_386_TLS_DESC_CALL
:
1098 // FIXME: If not relaxing to LE, we need to generate
1099 // DTPMOD32 and DTPOFF32 relocs.
1100 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1101 unsupported_reloc_global(object
, r_type
, gsym
);
1104 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1105 // FIXME: If not relaxing to LE, we need to generate a
1107 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1108 unsupported_reloc_global(object
, r_type
, gsym
);
1111 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1114 case elfcpp::R_386_TLS_IE
: // Initial-exec
1115 case elfcpp::R_386_TLS_IE_32
:
1116 case elfcpp::R_386_TLS_GOTIE
:
1117 // FIXME: If not relaxing to LE, we need to generate a
1118 // TPOFF or TPOFF32 reloc.
1119 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1120 unsupported_reloc_global(object
, r_type
, gsym
);
1123 case elfcpp::R_386_TLS_LE
: // Local-exec
1124 case elfcpp::R_386_TLS_LE_32
:
1125 // FIXME: If generating a shared object, we need to copy
1126 // this relocation into the object.
1127 gold_assert(!parameters
->output_is_shared());
1136 case elfcpp::R_386_32PLT
:
1137 case elfcpp::R_386_TLS_GD_32
:
1138 case elfcpp::R_386_TLS_GD_PUSH
:
1139 case elfcpp::R_386_TLS_GD_CALL
:
1140 case elfcpp::R_386_TLS_GD_POP
:
1141 case elfcpp::R_386_TLS_LDM_32
:
1142 case elfcpp::R_386_TLS_LDM_PUSH
:
1143 case elfcpp::R_386_TLS_LDM_CALL
:
1144 case elfcpp::R_386_TLS_LDM_POP
:
1145 case elfcpp::R_386_USED_BY_INTEL_200
:
1147 unsupported_reloc_global(object
, r_type
, gsym
);
1152 // Scan relocations for a section.
1155 Target_i386::scan_relocs(const General_options
& options
,
1156 Symbol_table
* symtab
,
1158 Sized_relobj
<32, false>* object
,
1159 unsigned int data_shndx
,
1160 unsigned int sh_type
,
1161 const unsigned char* prelocs
,
1163 Output_section
* output_section
,
1164 bool needs_special_offset_handling
,
1165 size_t local_symbol_count
,
1166 const unsigned char* plocal_symbols
)
1168 if (sh_type
== elfcpp::SHT_RELA
)
1170 gold_error(_("%s: unsupported RELA reloc section"),
1171 object
->name().c_str());
1175 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1186 needs_special_offset_handling
,
1191 // Finalize the sections.
1194 Target_i386::do_finalize_sections(Layout
* layout
)
1196 // Fill in some more dynamic tags.
1197 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1200 if (this->got_plt_
!= NULL
)
1201 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1203 if (this->plt_
!= NULL
)
1205 const Output_data
* od
= this->plt_
->rel_plt();
1206 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1207 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1208 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1211 if (this->rel_dyn_
!= NULL
)
1213 const Output_data
* od
= this->rel_dyn_
;
1214 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1215 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1216 odyn
->add_constant(elfcpp::DT_RELENT
,
1217 elfcpp::Elf_sizes
<32>::rel_size
);
1220 if (!parameters
->output_is_shared())
1222 // The value of the DT_DEBUG tag is filled in by the dynamic
1223 // linker at run time, and used by the debugger.
1224 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1228 // Emit any relocs we saved in an attempt to avoid generating COPY
1230 if (this->copy_relocs_
== NULL
)
1232 if (this->copy_relocs_
->any_to_emit())
1234 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1235 this->copy_relocs_
->emit(rel_dyn
);
1237 delete this->copy_relocs_
;
1238 this->copy_relocs_
= NULL
;
1241 // Return whether a direct absolute static relocation needs to be applied.
1242 // In cases where Scan::local() or Scan::global() has created
1243 // a dynamic relocation other than R_386_RELATIVE, the addend
1244 // of the relocation is carried in the data, and we must not
1245 // apply the static relocation.
1248 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1252 // For local symbols, return FALSE if a non-RELATIVE dynamic
1253 // relocation was created; return TRUE otherwise.
1255 return (!parameters
->output_is_position_independent() || is_32bit
);
1257 // For global symbols, mimic the logic in Scan::global()
1258 // to decide whether a non-RELATIVE dynamic relocation was
1260 // FIXME: This is ugly. Try to refactor this logic so it can be
1261 // shared by Scan::global() and Relocate::relocate().
1262 if (gsym
->is_from_dynobj()
1263 || (parameters
->output_is_shared()
1264 && gsym
->is_preemptible()))
1266 if (gsym
->type() == elfcpp::STT_FUNC
)
1268 if (!is_pcrel
&& parameters
->output_is_position_independent())
1274 else if (!is_pcrel
&& parameters
->output_is_position_independent())
1277 // For all other cases, return TRUE
1281 // Perform a relocation.
1284 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1285 Target_i386
* target
,
1287 const elfcpp::Rel
<32, false>& rel
,
1288 unsigned int r_type
,
1289 const Sized_symbol
<32>* gsym
,
1290 const Symbol_value
<32>* psymval
,
1291 unsigned char* view
,
1292 elfcpp::Elf_types
<32>::Elf_Addr address
,
1295 if (this->skip_call_tls_get_addr_
)
1297 if (r_type
!= elfcpp::R_386_PLT32
1299 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1300 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1301 _("missing expected TLS relocation"));
1304 this->skip_call_tls_get_addr_
= false;
1309 // Pick the value to use for symbols defined in shared objects.
1310 Symbol_value
<32> symval
;
1312 && (gsym
->is_from_dynobj()
1313 || (parameters
->output_is_shared()
1314 && gsym
->is_preemptible()))
1315 && gsym
->has_plt_offset())
1317 symval
.set_output_value(target
->plt_section()->address()
1318 + gsym
->plt_offset());
1322 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1324 // Get the GOT offset if needed.
1325 // The GOT pointer points to the end of the GOT section.
1326 // We need to subtract the size of the GOT section to get
1327 // the actual offset to use in the relocation.
1328 bool have_got_offset
= false;
1329 unsigned int got_offset
= 0;
1332 case elfcpp::R_386_GOT32
:
1335 gold_assert(gsym
->has_got_offset());
1336 got_offset
= gsym
->got_offset() - target
->got_size();
1340 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1341 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1343 have_got_offset
= true;
1352 case elfcpp::R_386_NONE
:
1353 case elfcpp::R_386_GNU_VTINHERIT
:
1354 case elfcpp::R_386_GNU_VTENTRY
:
1357 case elfcpp::R_386_32
:
1358 if (should_apply_static_reloc(gsym
, false, true))
1359 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1362 case elfcpp::R_386_PC32
:
1363 if (should_apply_static_reloc(gsym
, true, true))
1364 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1367 case elfcpp::R_386_16
:
1368 if (should_apply_static_reloc(gsym
, false, false))
1369 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1372 case elfcpp::R_386_PC16
:
1373 if (should_apply_static_reloc(gsym
, true, false))
1374 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1377 case elfcpp::R_386_8
:
1378 if (should_apply_static_reloc(gsym
, false, false))
1379 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1382 case elfcpp::R_386_PC8
:
1383 if (should_apply_static_reloc(gsym
, true, false))
1384 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1387 case elfcpp::R_386_PLT32
:
1388 gold_assert(gsym
== NULL
1389 || gsym
->has_plt_offset()
1390 || gsym
->final_value_is_known());
1391 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1394 case elfcpp::R_386_GOT32
:
1395 gold_assert(have_got_offset
);
1396 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1399 case elfcpp::R_386_GOTOFF
:
1401 elfcpp::Elf_types
<32>::Elf_Addr value
;
1402 value
= (psymval
->value(object
, 0)
1403 - target
->got_plt_section()->address());
1404 Relocate_functions
<32, false>::rel32(view
, value
);
1408 case elfcpp::R_386_GOTPC
:
1410 elfcpp::Elf_types
<32>::Elf_Addr value
;
1411 value
= target
->got_plt_section()->address();
1412 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1416 case elfcpp::R_386_COPY
:
1417 case elfcpp::R_386_GLOB_DAT
:
1418 case elfcpp::R_386_JUMP_SLOT
:
1419 case elfcpp::R_386_RELATIVE
:
1420 // These are outstanding tls relocs, which are unexpected when
1422 case elfcpp::R_386_TLS_TPOFF
:
1423 case elfcpp::R_386_TLS_DTPMOD32
:
1424 case elfcpp::R_386_TLS_DTPOFF32
:
1425 case elfcpp::R_386_TLS_TPOFF32
:
1426 case elfcpp::R_386_TLS_DESC
:
1427 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1428 _("unexpected reloc %u in object file"),
1432 // These are initial tls relocs, which are expected when
1434 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1435 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1436 case elfcpp::R_386_TLS_DESC_CALL
:
1437 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1438 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1439 case elfcpp::R_386_TLS_IE
: // Initial-exec
1440 case elfcpp::R_386_TLS_IE_32
:
1441 case elfcpp::R_386_TLS_GOTIE
:
1442 case elfcpp::R_386_TLS_LE
: // Local-exec
1443 case elfcpp::R_386_TLS_LE_32
:
1444 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1445 address
, view_size
);
1448 case elfcpp::R_386_32PLT
:
1449 case elfcpp::R_386_TLS_GD_32
:
1450 case elfcpp::R_386_TLS_GD_PUSH
:
1451 case elfcpp::R_386_TLS_GD_CALL
:
1452 case elfcpp::R_386_TLS_GD_POP
:
1453 case elfcpp::R_386_TLS_LDM_32
:
1454 case elfcpp::R_386_TLS_LDM_PUSH
:
1455 case elfcpp::R_386_TLS_LDM_CALL
:
1456 case elfcpp::R_386_TLS_LDM_POP
:
1457 case elfcpp::R_386_USED_BY_INTEL_200
:
1459 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1460 _("unsupported reloc %u"),
1468 // Perform a TLS relocation.
1471 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1473 const elfcpp::Rel
<32, false>& rel
,
1474 unsigned int r_type
,
1475 const Sized_symbol
<32>* gsym
,
1476 const Symbol_value
<32>* psymval
,
1477 unsigned char* view
,
1478 elfcpp::Elf_types
<32>::Elf_Addr
,
1481 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1482 if (tls_segment
== NULL
)
1484 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1485 _("TLS reloc but no TLS segment"));
1489 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1491 const bool is_final
= (gsym
== NULL
1492 ? !parameters
->output_is_position_independent()
1493 : gsym
->final_value_is_known());
1494 const tls::Tls_optimization optimized_type
1495 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1498 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1499 if (optimized_type
== tls::TLSOPT_TO_LE
)
1501 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1502 rel
, r_type
, value
, view
,
1506 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1507 _("unsupported reloc %u"),
1511 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1512 case elfcpp::R_386_TLS_DESC_CALL
:
1513 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1514 _("unsupported reloc %u"),
1518 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1519 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1521 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1522 _("both SUN and GNU model "
1523 "TLS relocations"));
1526 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1527 if (optimized_type
== tls::TLSOPT_TO_LE
)
1529 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1530 value
, view
, view_size
);
1533 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1534 _("unsupported reloc %u"),
1538 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1539 // This reloc can appear in debugging sections, in which case we
1540 // won't see the TLS_LDM reloc. The local_dynamic_type field
1542 if (optimized_type
!= tls::TLSOPT_TO_LE
1543 || this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1544 value
= value
- tls_segment
->vaddr();
1545 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1546 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1548 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1549 Relocate_functions
<32, false>::rel32(view
, value
);
1552 case elfcpp::R_386_TLS_IE
: // Initial-exec
1553 case elfcpp::R_386_TLS_GOTIE
:
1554 case elfcpp::R_386_TLS_IE_32
:
1555 if (optimized_type
== tls::TLSOPT_TO_LE
)
1557 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1558 rel
, r_type
, value
, view
,
1562 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1563 _("unsupported reloc %u"),
1567 case elfcpp::R_386_TLS_LE
: // Local-exec
1568 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1569 Relocate_functions
<32, false>::rel32(view
, value
);
1572 case elfcpp::R_386_TLS_LE_32
:
1573 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1574 Relocate_functions
<32, false>::rel32(view
, value
);
1579 // Do a relocation in which we convert a TLS General-Dynamic to a
1583 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1585 Output_segment
* tls_segment
,
1586 const elfcpp::Rel
<32, false>& rel
,
1588 elfcpp::Elf_types
<32>::Elf_Addr value
,
1589 unsigned char* view
,
1592 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1593 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1594 // leal foo(%reg),%eax; call ___tls_get_addr
1595 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1597 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1598 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1600 unsigned char op1
= view
[-1];
1601 unsigned char op2
= view
[-2];
1603 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1604 op2
== 0x8d || op2
== 0x04);
1605 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1611 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1612 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1613 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1614 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1615 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1619 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1620 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1621 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1624 // There is a trailing nop. Use the size byte subl.
1625 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1630 // Use the five byte subl.
1631 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1635 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1636 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1638 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1640 this->skip_call_tls_get_addr_
= true;
1643 // Do a relocation in which we convert a TLS Local-Dynamic to a
1647 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1650 const elfcpp::Rel
<32, false>& rel
,
1652 elfcpp::Elf_types
<32>::Elf_Addr
,
1653 unsigned char* view
,
1656 // leal foo(%reg), %eax; call ___tls_get_addr
1657 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1659 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1660 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1662 // FIXME: Does this test really always pass?
1663 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1664 view
[-2] == 0x8d && view
[-1] == 0x83);
1666 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1668 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1670 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1672 this->skip_call_tls_get_addr_
= true;
1675 // Do a relocation in which we convert a TLS Initial-Exec to a
1679 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1681 Output_segment
* tls_segment
,
1682 const elfcpp::Rel
<32, false>& rel
,
1683 unsigned int r_type
,
1684 elfcpp::Elf_types
<32>::Elf_Addr value
,
1685 unsigned char* view
,
1688 // We have to actually change the instructions, which means that we
1689 // need to examine the opcodes to figure out which instruction we
1691 if (r_type
== elfcpp::R_386_TLS_IE
)
1693 // movl %gs:XX,%eax ==> movl $YY,%eax
1694 // movl %gs:XX,%reg ==> movl $YY,%reg
1695 // addl %gs:XX,%reg ==> addl $YY,%reg
1696 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
1697 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1699 unsigned char op1
= view
[-1];
1702 // movl XX,%eax ==> movl $YY,%eax
1707 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1709 unsigned char op2
= view
[-2];
1712 // movl XX,%reg ==> movl $YY,%reg
1713 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1714 (op1
& 0xc7) == 0x05);
1716 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1718 else if (op2
== 0x03)
1720 // addl XX,%reg ==> addl $YY,%reg
1721 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1722 (op1
& 0xc7) == 0x05);
1724 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1727 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1732 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1733 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1734 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1735 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1736 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1738 unsigned char op1
= view
[-1];
1739 unsigned char op2
= view
[-2];
1740 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1741 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1744 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1746 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1748 else if (op2
== 0x2b)
1750 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1752 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1754 else if (op2
== 0x03)
1756 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1758 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1761 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1764 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1765 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1768 Relocate_functions
<32, false>::rel32(view
, value
);
1771 // Relocate section data.
1774 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1775 unsigned int sh_type
,
1776 const unsigned char* prelocs
,
1778 Output_section
* output_section
,
1779 bool needs_special_offset_handling
,
1780 unsigned char* view
,
1781 elfcpp::Elf_types
<32>::Elf_Addr address
,
1784 gold_assert(sh_type
== elfcpp::SHT_REL
);
1786 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1787 Target_i386::Relocate
>(
1793 needs_special_offset_handling
,
1799 // Return the value to use for a dynamic which requires special
1800 // treatment. This is how we support equality comparisons of function
1801 // pointers across shared library boundaries, as described in the
1802 // processor specific ABI supplement.
1805 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1807 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1808 return this->plt_section()->address() + gsym
->plt_offset();
1811 // Return a string used to fill a code section with nops to take up
1812 // the specified length.
1815 Target_i386::do_code_fill(off_t length
)
1819 // Build a jmp instruction to skip over the bytes.
1820 unsigned char jmp
[5];
1822 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1823 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1824 + std::string(length
- 5, '\0'));
1827 // Nop sequences of various lengths.
1828 const char nop1
[1] = { 0x90 }; // nop
1829 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1830 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1831 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1832 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1833 0x00 }; // leal 0(%esi,1),%esi
1834 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1836 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1838 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1839 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1840 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1841 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1843 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1844 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1846 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1847 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1849 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1850 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1851 0x00, 0x00, 0x00, 0x00 };
1852 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1853 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1854 0x27, 0x00, 0x00, 0x00,
1856 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1857 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1858 0xbc, 0x27, 0x00, 0x00,
1860 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1861 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1862 0x90, 0x90, 0x90, 0x90,
1865 const char* nops
[16] = {
1867 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1868 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1871 return std::string(nops
[length
], length
);
1874 // The selector for i386 object files.
1876 class Target_selector_i386
: public Target_selector
1879 Target_selector_i386()
1880 : Target_selector(elfcpp::EM_386
, 32, false)
1884 recognize(int machine
, int osabi
, int abiversion
);
1887 Target_i386
* target_
;
1890 // Recognize an i386 object file when we already know that the machine
1891 // number is EM_386.
1894 Target_selector_i386::recognize(int, int, int)
1896 if (this->target_
== NULL
)
1897 this->target_
= new Target_i386();
1898 return this->target_
;
1901 Target_selector_i386 target_selector_i386
;
1903 } // End anonymous namespace.