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 size_t local_symbol_count
,
74 const unsigned char* plocal_symbols
,
75 Symbol
** global_symbols
);
77 // Finalize the sections.
79 do_finalize_sections(Layout
*);
81 // Return the value to use for a dynamic which requires special
84 do_dynsym_value(const Symbol
*) const;
86 // Relocate a section.
88 relocate_section(const Relocate_info
<32, false>*,
90 const unsigned char* prelocs
,
93 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
96 // Return a string used to fill a code section with nops.
98 do_code_fill(off_t length
);
100 // Return the size of the GOT section.
104 gold_assert(this->got_
!= NULL
);
105 return this->got_
->data_size();
109 // The class which scans relocations.
113 local(const General_options
& options
, Symbol_table
* symtab
,
114 Layout
* layout
, Target_i386
* target
,
115 Sized_relobj
<32, false>* object
,
116 unsigned int data_shndx
,
117 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
118 const elfcpp::Sym
<32, false>& lsym
);
121 global(const General_options
& options
, Symbol_table
* symtab
,
122 Layout
* layout
, Target_i386
* target
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
129 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
132 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
136 // The class which implements relocation.
141 : skip_call_tls_get_addr_(false),
142 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
147 if (this->skip_call_tls_get_addr_
)
149 // FIXME: This needs to specify the location somehow.
150 gold_error(_("missing expected TLS relocation"));
154 // Return whether the static relocation needs to be applied.
156 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
160 // Do a relocation. Return false if the caller should not issue
161 // any warnings about this relocation.
163 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
164 const elfcpp::Rel
<32, false>&,
165 unsigned int r_type
, const Sized_symbol
<32>*,
166 const Symbol_value
<32>*,
167 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
171 // Do a TLS relocation.
173 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
174 const elfcpp::Rel
<32, false>&,
175 unsigned int r_type
, const Sized_symbol
<32>*,
176 const Symbol_value
<32>*,
177 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
179 // Do a TLS General-Dynamic to Local-Exec transition.
181 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
182 Output_segment
* tls_segment
,
183 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
184 elfcpp::Elf_types
<32>::Elf_Addr value
,
188 // Do a TLS Local-Dynamic to Local-Exec transition.
190 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
191 Output_segment
* tls_segment
,
192 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
193 elfcpp::Elf_types
<32>::Elf_Addr value
,
197 // Do a TLS Initial-Exec to Local-Exec transition.
199 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
200 Output_segment
* tls_segment
,
201 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
202 elfcpp::Elf_types
<32>::Elf_Addr value
,
206 // We need to keep track of which type of local dynamic relocation
207 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
208 enum Local_dynamic_type
215 // This is set if we should skip the next reloc, which should be a
216 // PLT32 reloc against ___tls_get_addr.
217 bool skip_call_tls_get_addr_
;
218 // The type of local dynamic relocation we have seen in the section
219 // being relocated, if any.
220 Local_dynamic_type local_dynamic_type_
;
223 // Adjust TLS relocation type based on the options and whether this
224 // is a local symbol.
225 static tls::Tls_optimization
226 optimize_tls_reloc(bool is_final
, int r_type
);
228 // Get the GOT section, creating it if necessary.
229 Output_data_got
<32, false>*
230 got_section(Symbol_table
*, Layout
*);
232 // Get the GOT PLT section.
234 got_plt_section() const
236 gold_assert(this->got_plt_
!= NULL
);
237 return this->got_plt_
;
240 // Create a PLT entry for a global symbol.
242 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
244 // Get the PLT section.
245 const Output_data_plt_i386
*
248 gold_assert(this->plt_
!= NULL
);
252 // Get the dynamic reloc section, creating it if necessary.
254 rel_dyn_section(Layout
*);
256 // Copy a relocation against a global symbol.
258 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
259 Sized_relobj
<32, false>*, unsigned int,
260 Symbol
*, const elfcpp::Rel
<32, false>&);
262 // Information about this specific target which we pass to the
263 // general Target structure.
264 static const Target::Target_info i386_info
;
267 Output_data_got
<32, false>* got_
;
269 Output_data_plt_i386
* plt_
;
270 // The GOT PLT section.
271 Output_data_space
* got_plt_
;
272 // The dynamic reloc section.
273 Reloc_section
* rel_dyn_
;
274 // Relocs saved to avoid a COPY reloc.
275 Copy_relocs
<32, false>* copy_relocs_
;
276 // Space for variables copied with a COPY reloc.
277 Output_data_space
* dynbss_
;
280 const Target::Target_info
Target_i386::i386_info
=
283 false, // is_big_endian
284 elfcpp::EM_386
, // machine_code
285 false, // has_make_symbol
286 false, // has_resolve
287 true, // has_code_fill
288 true, // is_default_stack_executable
289 "/usr/lib/libc.so.1", // dynamic_linker
290 0x08048000, // default_text_segment_address
291 0x1000, // abi_pagesize
292 0x1000 // common_pagesize
295 // Get the GOT section, creating it if necessary.
297 Output_data_got
<32, false>*
298 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
300 if (this->got_
== NULL
)
302 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
304 this->got_
= new Output_data_got
<32, false>();
306 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
307 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
310 // The old GNU linker creates a .got.plt section. We just
311 // create another set of data in the .got section. Note that we
312 // always create a PLT if we create a GOT, although the PLT
314 this->got_plt_
= new Output_data_space(4);
315 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
316 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
319 // The first three entries are reserved.
320 this->got_plt_
->set_space_size(3 * 4);
322 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
323 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
325 0, 0, elfcpp::STT_OBJECT
,
327 elfcpp::STV_HIDDEN
, 0,
334 // Get the dynamic reloc section, creating it if necessary.
336 Target_i386::Reloc_section
*
337 Target_i386::rel_dyn_section(Layout
* layout
)
339 if (this->rel_dyn_
== NULL
)
341 gold_assert(layout
!= NULL
);
342 this->rel_dyn_
= new Reloc_section();
343 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
344 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
346 return this->rel_dyn_
;
349 // A class to handle the PLT data.
351 class Output_data_plt_i386
: public Output_section_data
354 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
356 Output_data_plt_i386(Layout
*, Output_data_space
*);
358 // Add an entry to the PLT.
360 add_entry(Symbol
* gsym
);
362 // Return the .rel.plt section data.
365 { return this->rel_
; }
369 do_adjust_output_section(Output_section
* os
);
372 // The size of an entry in the PLT.
373 static const int plt_entry_size
= 16;
375 // The first entry in the PLT for an executable.
376 static unsigned char exec_first_plt_entry
[plt_entry_size
];
378 // The first entry in the PLT for a shared object.
379 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
381 // Other entries in the PLT for an executable.
382 static unsigned char exec_plt_entry
[plt_entry_size
];
384 // Other entries in the PLT for a shared object.
385 static unsigned char dyn_plt_entry
[plt_entry_size
];
387 // Set the final size.
389 do_set_address(uint64_t, off_t
)
390 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
392 // Write out the PLT data.
394 do_write(Output_file
*);
396 // The reloc section.
398 // The .got.plt section.
399 Output_data_space
* got_plt_
;
400 // The number of PLT entries.
404 // Create the PLT section. The ordinary .got section is an argument,
405 // since we need to refer to the start. We also create our own .got
406 // section just for PLT entries.
408 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
409 Output_data_space
* got_plt
)
410 : Output_section_data(4), got_plt_(got_plt
), count_(0)
412 this->rel_
= new Reloc_section();
413 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
414 elfcpp::SHF_ALLOC
, this->rel_
);
418 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
420 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
421 // linker, and so do we.
425 // Add an entry to the PLT.
428 Output_data_plt_i386::add_entry(Symbol
* gsym
)
430 gold_assert(!gsym
->has_plt_offset());
432 // Note that when setting the PLT offset we skip the initial
433 // reserved PLT entry.
434 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
438 off_t got_offset
= this->got_plt_
->data_size();
440 // Every PLT entry needs a GOT entry which points back to the PLT
441 // entry (this will be changed by the dynamic linker, normally
442 // lazily when the function is called).
443 this->got_plt_
->set_space_size(got_offset
+ 4);
445 // Every PLT entry needs a reloc.
446 gsym
->set_needs_dynsym_entry();
447 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
450 // Note that we don't need to save the symbol. The contents of the
451 // PLT are independent of which symbols are used. The symbols only
452 // appear in the relocations.
455 // The first entry in the PLT for an executable.
457 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
459 0xff, 0x35, // pushl contents of memory address
460 0, 0, 0, 0, // replaced with address of .got + 4
461 0xff, 0x25, // jmp indirect
462 0, 0, 0, 0, // replaced with address of .got + 8
466 // The first entry in the PLT for a shared object.
468 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
470 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
471 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
475 // Subsequent entries in the PLT for an executable.
477 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
479 0xff, 0x25, // jmp indirect
480 0, 0, 0, 0, // replaced with address of symbol in .got
481 0x68, // pushl immediate
482 0, 0, 0, 0, // replaced with offset into relocation table
483 0xe9, // jmp relative
484 0, 0, 0, 0 // replaced with offset to start of .plt
487 // Subsequent entries in the PLT for a shared object.
489 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
491 0xff, 0xa3, // jmp *offset(%ebx)
492 0, 0, 0, 0, // replaced with offset of symbol in .got
493 0x68, // pushl immediate
494 0, 0, 0, 0, // replaced with offset into relocation table
495 0xe9, // jmp relative
496 0, 0, 0, 0 // replaced with offset to start of .plt
499 // Write out the PLT. This uses the hand-coded instructions above,
500 // and adjusts them as needed. This is all specified by the i386 ELF
501 // Processor Supplement.
504 Output_data_plt_i386::do_write(Output_file
* of
)
506 const off_t offset
= this->offset();
507 const off_t oview_size
= this->data_size();
508 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
510 const off_t got_file_offset
= this->got_plt_
->offset();
511 const off_t got_size
= this->got_plt_
->data_size();
512 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
515 unsigned char* pov
= oview
;
517 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
518 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
520 if (parameters
->output_is_shared())
521 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
524 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
525 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
526 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
528 pov
+= plt_entry_size
;
530 unsigned char* got_pov
= got_view
;
532 memset(got_pov
, 0, 12);
535 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
537 unsigned int plt_offset
= plt_entry_size
;
538 unsigned int plt_rel_offset
= 0;
539 unsigned int got_offset
= 12;
540 const unsigned int count
= this->count_
;
541 for (unsigned int i
= 0;
544 pov
+= plt_entry_size
,
546 plt_offset
+= plt_entry_size
,
547 plt_rel_offset
+= rel_size
,
550 // Set and adjust the PLT entry itself.
552 if (parameters
->output_is_shared())
554 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
555 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
559 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
560 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
565 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
566 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
567 - (plt_offset
+ plt_entry_size
));
569 // Set the entry in the GOT.
570 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
573 gold_assert(pov
- oview
== oview_size
);
574 gold_assert(got_pov
- got_view
== got_size
);
576 of
->write_output_view(offset
, oview_size
, oview
);
577 of
->write_output_view(got_file_offset
, got_size
, got_view
);
580 // Create a PLT entry for a global symbol.
583 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
585 if (gsym
->has_plt_offset())
588 if (this->plt_
== NULL
)
590 // Create the GOT sections first.
591 this->got_section(symtab
, layout
);
593 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
594 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
596 | elfcpp::SHF_EXECINSTR
),
600 this->plt_
->add_entry(gsym
);
603 // Handle a relocation against a non-function symbol defined in a
604 // dynamic object. The traditional way to handle this is to generate
605 // a COPY relocation to copy the variable at runtime from the shared
606 // object into the executable's data segment. However, this is
607 // undesirable in general, as if the size of the object changes in the
608 // dynamic object, the executable will no longer work correctly. If
609 // this relocation is in a writable section, then we can create a
610 // dynamic reloc and the dynamic linker will resolve it to the correct
611 // address at runtime. However, we do not want do that if the
612 // relocation is in a read-only section, as it would prevent the
613 // readonly segment from being shared. And if we have to eventually
614 // generate a COPY reloc, then any dynamic relocations will be
615 // useless. So this means that if this is a writable section, we need
616 // to save the relocation until we see whether we have to create a
617 // COPY relocation for this symbol for any other relocation.
620 Target_i386::copy_reloc(const General_options
* options
,
621 Symbol_table
* symtab
,
623 Sized_relobj
<32, false>* object
,
624 unsigned int data_shndx
, Symbol
* gsym
,
625 const elfcpp::Rel
<32, false>& rel
)
627 Sized_symbol
<32>* ssym
;
628 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
631 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
634 // So far we do not need a COPY reloc. Save this relocation.
635 // If it turns out that we never need a COPY reloc for this
636 // symbol, then we will emit the relocation.
637 if (this->copy_relocs_
== NULL
)
638 this->copy_relocs_
= new Copy_relocs
<32, false>();
639 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
643 // Allocate space for this symbol in the .bss section.
645 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
647 // There is no defined way to determine the required alignment
648 // of the symbol. We pick the alignment based on the size. We
649 // set an arbitrary maximum of 256.
651 for (align
= 1; align
< 512; align
<<= 1)
652 if ((symsize
& align
) != 0)
655 if (this->dynbss_
== NULL
)
657 this->dynbss_
= new Output_data_space(align
);
658 layout
->add_output_section_data(".bss",
661 | elfcpp::SHF_WRITE
),
665 Output_data_space
* dynbss
= this->dynbss_
;
667 if (align
> dynbss
->addralign())
668 dynbss
->set_space_alignment(align
);
670 off_t dynbss_size
= dynbss
->data_size();
671 dynbss_size
= align_address(dynbss_size
, align
);
672 off_t offset
= dynbss_size
;
673 dynbss
->set_space_size(dynbss_size
+ symsize
);
675 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
677 // Add the COPY reloc.
678 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
679 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
683 // Optimize the TLS relocation type based on what we know about the
684 // symbol. IS_FINAL is true if the final address of this symbol is
685 // known at link time.
687 tls::Tls_optimization
688 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
690 // If we are generating a shared library, then we can't do anything
692 if (parameters
->output_is_shared())
693 return tls::TLSOPT_NONE
;
697 case elfcpp::R_386_TLS_GD
:
698 case elfcpp::R_386_TLS_GOTDESC
:
699 case elfcpp::R_386_TLS_DESC_CALL
:
700 // These are General-Dynamic which permits fully general TLS
701 // access. Since we know that we are generating an executable,
702 // we can convert this to Initial-Exec. If we also know that
703 // this is a local symbol, we can further switch to Local-Exec.
705 return tls::TLSOPT_TO_LE
;
706 return tls::TLSOPT_TO_IE
;
708 case elfcpp::R_386_TLS_LDM
:
709 // This is Local-Dynamic, which refers to a local symbol in the
710 // dynamic TLS block. Since we know that we generating an
711 // executable, we can switch to Local-Exec.
712 return tls::TLSOPT_TO_LE
;
714 case elfcpp::R_386_TLS_LDO_32
:
715 // Another type of Local-Dynamic relocation.
716 return tls::TLSOPT_TO_LE
;
718 case elfcpp::R_386_TLS_IE
:
719 case elfcpp::R_386_TLS_GOTIE
:
720 case elfcpp::R_386_TLS_IE_32
:
721 // These are Initial-Exec relocs which get the thread offset
722 // from the GOT. If we know that we are linking against the
723 // local symbol, we can switch to Local-Exec, which links the
724 // thread offset into the instruction.
726 return tls::TLSOPT_TO_LE
;
727 return tls::TLSOPT_NONE
;
729 case elfcpp::R_386_TLS_LE
:
730 case elfcpp::R_386_TLS_LE_32
:
731 // When we already have Local-Exec, there is nothing further we
733 return tls::TLSOPT_NONE
;
740 // Report an unsupported relocation against a local symbol.
743 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
746 gold_error(_("%s: unsupported reloc %u against local symbol"),
747 object
->name().c_str(), r_type
);
750 // Scan a relocation for a local symbol.
753 Target_i386::Scan::local(const General_options
&,
754 Symbol_table
* symtab
,
757 Sized_relobj
<32, false>* object
,
758 unsigned int data_shndx
,
759 const elfcpp::Rel
<32, false>& reloc
,
761 const elfcpp::Sym
<32, false>&)
765 case elfcpp::R_386_NONE
:
766 case elfcpp::R_386_GNU_VTINHERIT
:
767 case elfcpp::R_386_GNU_VTENTRY
:
770 case elfcpp::R_386_32
:
771 case elfcpp::R_386_16
:
772 case elfcpp::R_386_8
:
773 // If building a shared library (or a position-independent
774 // executable), we need to create a dynamic relocation for
775 // this location. The relocation applied at link time will
776 // apply the link-time value, so we flag the location with
777 // an R_386_RELATIVE relocation so the dynamic loader can
778 // relocate it easily.
779 if (parameters
->output_is_position_independent())
781 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
782 if (r_type
== elfcpp::R_386_32
)
783 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
784 reloc
.get_r_offset());
787 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
788 rel_dyn
->add_local(object
, r_sym
, r_type
, data_shndx
,
789 reloc
.get_r_offset());
794 case elfcpp::R_386_PC32
:
795 case elfcpp::R_386_PC16
:
796 case elfcpp::R_386_PC8
:
799 case elfcpp::R_386_PLT32
:
800 // Since we know this is a local symbol, we can handle this as a
804 case elfcpp::R_386_GOTOFF
:
805 case elfcpp::R_386_GOTPC
:
806 // We need a GOT section.
807 target
->got_section(symtab
, layout
);
810 case elfcpp::R_386_GOT32
:
812 // The symbol requires a GOT entry.
813 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
814 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
815 if (got
->add_local(object
, r_sym
))
817 // If we are generating a shared object, we need to add a
818 // dynamic RELATIVE relocation for this symbol.
819 if (parameters
->output_is_position_independent())
821 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
822 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
823 data_shndx
, reloc
.get_r_offset());
829 // These are relocations which should only be seen by the
830 // dynamic linker, and should never be seen here.
831 case elfcpp::R_386_COPY
:
832 case elfcpp::R_386_GLOB_DAT
:
833 case elfcpp::R_386_JUMP_SLOT
:
834 case elfcpp::R_386_RELATIVE
:
835 case elfcpp::R_386_TLS_TPOFF
:
836 case elfcpp::R_386_TLS_DTPMOD32
:
837 case elfcpp::R_386_TLS_DTPOFF32
:
838 case elfcpp::R_386_TLS_TPOFF32
:
839 case elfcpp::R_386_TLS_DESC
:
840 gold_error(_("%s: unexpected reloc %u in object file"),
841 object
->name().c_str(), r_type
);
844 // These are initial TLS relocs, which are expected when
846 case elfcpp::R_386_TLS_GD
: // Global-dynamic
847 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
848 case elfcpp::R_386_TLS_DESC_CALL
:
849 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
850 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
851 case elfcpp::R_386_TLS_IE
: // Initial-exec
852 case elfcpp::R_386_TLS_IE_32
:
853 case elfcpp::R_386_TLS_GOTIE
:
854 case elfcpp::R_386_TLS_LE
: // Local-exec
855 case elfcpp::R_386_TLS_LE_32
:
857 bool output_is_shared
= parameters
->output_is_shared();
858 const tls::Tls_optimization optimized_type
859 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
862 case elfcpp::R_386_TLS_GD
: // Global-dynamic
863 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
864 case elfcpp::R_386_TLS_DESC_CALL
:
865 // FIXME: If not relaxing to LE, we need to generate
866 // DTPMOD32 and DTPOFF32 relocs.
867 if (optimized_type
!= tls::TLSOPT_TO_LE
)
868 unsupported_reloc_local(object
, r_type
);
871 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
872 // FIXME: If not relaxing to LE, we need to generate a
874 if (optimized_type
!= tls::TLSOPT_TO_LE
)
875 unsupported_reloc_local(object
, r_type
);
878 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
881 case elfcpp::R_386_TLS_IE
: // Initial-exec
882 case elfcpp::R_386_TLS_IE_32
:
883 case elfcpp::R_386_TLS_GOTIE
:
884 // FIXME: If not relaxing to LE, we need to generate a
885 // TPOFF or TPOFF32 reloc.
886 if (optimized_type
!= tls::TLSOPT_TO_LE
)
887 unsupported_reloc_local(object
, r_type
);
890 case elfcpp::R_386_TLS_LE
: // Local-exec
891 case elfcpp::R_386_TLS_LE_32
:
892 // FIXME: If generating a shared object, we need to copy
893 // this relocation into the object.
894 gold_assert(!output_is_shared
);
903 case elfcpp::R_386_32PLT
:
904 case elfcpp::R_386_TLS_GD_32
:
905 case elfcpp::R_386_TLS_GD_PUSH
:
906 case elfcpp::R_386_TLS_GD_CALL
:
907 case elfcpp::R_386_TLS_GD_POP
:
908 case elfcpp::R_386_TLS_LDM_32
:
909 case elfcpp::R_386_TLS_LDM_PUSH
:
910 case elfcpp::R_386_TLS_LDM_CALL
:
911 case elfcpp::R_386_TLS_LDM_POP
:
912 case elfcpp::R_386_USED_BY_INTEL_200
:
914 unsupported_reloc_local(object
, r_type
);
919 // Report an unsupported relocation against a global symbol.
922 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
926 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
927 object
->name().c_str(), r_type
, gsym
->name());
930 // Scan a relocation for a global symbol.
933 Target_i386::Scan::global(const General_options
& options
,
934 Symbol_table
* symtab
,
937 Sized_relobj
<32, false>* object
,
938 unsigned int data_shndx
,
939 const elfcpp::Rel
<32, false>& reloc
,
945 case elfcpp::R_386_NONE
:
946 case elfcpp::R_386_GNU_VTINHERIT
:
947 case elfcpp::R_386_GNU_VTENTRY
:
950 case elfcpp::R_386_32
:
951 case elfcpp::R_386_PC32
:
952 case elfcpp::R_386_16
:
953 case elfcpp::R_386_PC16
:
954 case elfcpp::R_386_8
:
955 case elfcpp::R_386_PC8
:
957 bool is_pcrel
= (r_type
== elfcpp::R_386_PC32
958 || r_type
== elfcpp::R_386_PC16
959 || r_type
== elfcpp::R_386_PC8
);
961 if (gsym
->is_from_dynobj()
962 || (parameters
->output_is_shared()
963 && gsym
->is_preemptible()))
965 // (a) This symbol is defined in a dynamic object. If it is a
966 // function, we make a PLT entry. Otherwise we need to
967 // either generate a COPY reloc or copy this reloc.
968 // (b) We are building a shared object and this symbol is
969 // preemptible. If it is a function, we make a PLT entry.
970 // Otherwise, we copy the reloc.
971 if (gsym
->type() == elfcpp::STT_FUNC
)
973 target
->make_plt_entry(symtab
, layout
, gsym
);
975 // If this is not a PC relative reference, then we may
976 // be taking the address of the function. In that case
977 // we need to set the entry in the dynamic symbol table
978 // to the address of the PLT entry. We will also need to
979 // create a dynamic relocation.
982 if (gsym
->is_from_dynobj())
983 gsym
->set_needs_dynsym_value();
984 if (parameters
->output_is_position_independent())
986 Reloc_section
* rel_dyn
=
987 target
->rel_dyn_section(layout
);
988 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
989 reloc
.get_r_offset());
993 else if (parameters
->output_is_shared())
995 // We do not make COPY relocs in shared objects.
996 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
997 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
998 reloc
.get_r_offset());
1001 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
1004 else if (!is_pcrel
&& parameters
->output_is_position_independent())
1006 // This is not a PC-relative reference, so we need to generate
1007 // a dynamic relocation. At this point, we know the symbol
1008 // is not preemptible, so we can use the RELATIVE relocation.
1009 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1010 if (r_type
== elfcpp::R_386_32
)
1011 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
1012 reloc
.get_r_offset());
1014 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
1015 reloc
.get_r_offset());
1020 case elfcpp::R_386_GOT32
:
1022 // The symbol requires a GOT entry.
1023 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1024 if (got
->add_global(gsym
))
1026 // If this symbol is not fully resolved, we need to add a
1027 // dynamic relocation for it.
1028 if (!gsym
->final_value_is_known())
1030 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1031 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
1032 gsym
->got_offset());
1038 case elfcpp::R_386_PLT32
:
1039 // If the symbol is fully resolved, this is just a PC32 reloc.
1040 // Otherwise we need a PLT entry.
1041 if (gsym
->final_value_is_known())
1043 // If building a shared library, we can also skip the PLT entry
1044 // if the symbol is defined in the output file and is protected
1046 if (gsym
->is_defined()
1047 && !gsym
->is_from_dynobj()
1048 && !gsym
->is_preemptible())
1050 target
->make_plt_entry(symtab
, layout
, gsym
);
1053 case elfcpp::R_386_GOTOFF
:
1054 case elfcpp::R_386_GOTPC
:
1055 // We need a GOT section.
1056 target
->got_section(symtab
, layout
);
1059 // These are relocations which should only be seen by the
1060 // dynamic linker, and should never be seen here.
1061 case elfcpp::R_386_COPY
:
1062 case elfcpp::R_386_GLOB_DAT
:
1063 case elfcpp::R_386_JUMP_SLOT
:
1064 case elfcpp::R_386_RELATIVE
:
1065 case elfcpp::R_386_TLS_TPOFF
:
1066 case elfcpp::R_386_TLS_DTPMOD32
:
1067 case elfcpp::R_386_TLS_DTPOFF32
:
1068 case elfcpp::R_386_TLS_TPOFF32
:
1069 case elfcpp::R_386_TLS_DESC
:
1070 gold_error(_("%s: unexpected reloc %u in object file"),
1071 object
->name().c_str(), r_type
);
1074 // These are initial tls relocs, which are expected when
1076 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1077 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1078 case elfcpp::R_386_TLS_DESC_CALL
:
1079 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1080 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1081 case elfcpp::R_386_TLS_IE
: // Initial-exec
1082 case elfcpp::R_386_TLS_IE_32
:
1083 case elfcpp::R_386_TLS_GOTIE
:
1084 case elfcpp::R_386_TLS_LE
: // Local-exec
1085 case elfcpp::R_386_TLS_LE_32
:
1087 const bool is_final
= gsym
->final_value_is_known();
1088 const tls::Tls_optimization optimized_type
1089 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1092 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1093 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1094 case elfcpp::R_386_TLS_DESC_CALL
:
1095 // FIXME: If not relaxing to LE, we need to generate
1096 // DTPMOD32 and DTPOFF32 relocs.
1097 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1098 unsupported_reloc_global(object
, r_type
, gsym
);
1101 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1102 // FIXME: If not relaxing to LE, we need to generate a
1104 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1105 unsupported_reloc_global(object
, r_type
, gsym
);
1108 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1111 case elfcpp::R_386_TLS_IE
: // Initial-exec
1112 case elfcpp::R_386_TLS_IE_32
:
1113 case elfcpp::R_386_TLS_GOTIE
:
1114 // FIXME: If not relaxing to LE, we need to generate a
1115 // TPOFF or TPOFF32 reloc.
1116 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1117 unsupported_reloc_global(object
, r_type
, gsym
);
1120 case elfcpp::R_386_TLS_LE
: // Local-exec
1121 case elfcpp::R_386_TLS_LE_32
:
1122 // FIXME: If generating a shared object, we need to copy
1123 // this relocation into the object.
1124 gold_assert(!parameters
->output_is_shared());
1133 case elfcpp::R_386_32PLT
:
1134 case elfcpp::R_386_TLS_GD_32
:
1135 case elfcpp::R_386_TLS_GD_PUSH
:
1136 case elfcpp::R_386_TLS_GD_CALL
:
1137 case elfcpp::R_386_TLS_GD_POP
:
1138 case elfcpp::R_386_TLS_LDM_32
:
1139 case elfcpp::R_386_TLS_LDM_PUSH
:
1140 case elfcpp::R_386_TLS_LDM_CALL
:
1141 case elfcpp::R_386_TLS_LDM_POP
:
1142 case elfcpp::R_386_USED_BY_INTEL_200
:
1144 unsupported_reloc_global(object
, r_type
, gsym
);
1149 // Scan relocations for a section.
1152 Target_i386::scan_relocs(const General_options
& options
,
1153 Symbol_table
* symtab
,
1155 Sized_relobj
<32, false>* object
,
1156 unsigned int data_shndx
,
1157 unsigned int sh_type
,
1158 const unsigned char* prelocs
,
1160 size_t local_symbol_count
,
1161 const unsigned char* plocal_symbols
,
1162 Symbol
** global_symbols
)
1164 if (sh_type
== elfcpp::SHT_RELA
)
1166 gold_error(_("%s: unsupported RELA reloc section"),
1167 object
->name().c_str());
1171 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1186 // Finalize the sections.
1189 Target_i386::do_finalize_sections(Layout
* layout
)
1191 // Fill in some more dynamic tags.
1192 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1195 if (this->got_plt_
!= NULL
)
1196 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1198 if (this->plt_
!= NULL
)
1200 const Output_data
* od
= this->plt_
->rel_plt();
1201 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1202 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1203 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1206 if (this->rel_dyn_
!= NULL
)
1208 const Output_data
* od
= this->rel_dyn_
;
1209 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1210 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1211 odyn
->add_constant(elfcpp::DT_RELENT
,
1212 elfcpp::Elf_sizes
<32>::rel_size
);
1215 if (!parameters
->output_is_shared())
1217 // The value of the DT_DEBUG tag is filled in by the dynamic
1218 // linker at run time, and used by the debugger.
1219 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1223 // Emit any relocs we saved in an attempt to avoid generating COPY
1225 if (this->copy_relocs_
== NULL
)
1227 if (this->copy_relocs_
->any_to_emit())
1229 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1230 this->copy_relocs_
->emit(rel_dyn
);
1232 delete this->copy_relocs_
;
1233 this->copy_relocs_
= NULL
;
1236 // Return whether a direct absolute static relocation needs to be applied.
1237 // In cases where Scan::local() or Scan::global() has created
1238 // a dynamic relocation other than R_386_RELATIVE, the addend
1239 // of the relocation is carried in the data, and we must not
1240 // apply the static relocation.
1243 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1247 // For local symbols, return FALSE if a non-RELATIVE dynamic
1248 // relocation was created; return TRUE otherwise.
1250 return (!parameters
->output_is_position_independent() || is_32bit
);
1252 // For global symbols, mimic the logic in Scan::global()
1253 // to decide whether a non-RELATIVE dynamic relocation was
1255 // FIXME: This is ugly. Try to refactor this logic so it can be
1256 // shared by Scan::global() and Relocate::relocate().
1257 if (gsym
->is_from_dynobj()
1258 || (parameters
->output_is_shared()
1259 && gsym
->is_preemptible()))
1261 if (gsym
->type() == elfcpp::STT_FUNC
)
1263 if (!is_pcrel
&& parameters
->output_is_position_independent())
1269 else if (!is_pcrel
&& parameters
->output_is_position_independent())
1272 // For all other cases, return TRUE
1276 // Perform a relocation.
1279 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1280 Target_i386
* target
,
1282 const elfcpp::Rel
<32, false>& rel
,
1283 unsigned int r_type
,
1284 const Sized_symbol
<32>* gsym
,
1285 const Symbol_value
<32>* psymval
,
1286 unsigned char* view
,
1287 elfcpp::Elf_types
<32>::Elf_Addr address
,
1290 if (this->skip_call_tls_get_addr_
)
1292 if (r_type
!= elfcpp::R_386_PLT32
1294 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1295 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1296 _("missing expected TLS relocation"));
1299 this->skip_call_tls_get_addr_
= false;
1304 // Pick the value to use for symbols defined in shared objects.
1305 Symbol_value
<32> symval
;
1307 && (gsym
->is_from_dynobj()
1308 || (parameters
->output_is_shared()
1309 && gsym
->is_preemptible()))
1310 && gsym
->has_plt_offset())
1312 symval
.set_output_value(target
->plt_section()->address()
1313 + gsym
->plt_offset());
1317 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1319 // Get the GOT offset if needed.
1320 // The GOT pointer points to the end of the GOT section.
1321 // We need to subtract the size of the GOT section to get
1322 // the actual offset to use in the relocation.
1323 bool have_got_offset
= false;
1324 unsigned int got_offset
= 0;
1327 case elfcpp::R_386_GOT32
:
1330 gold_assert(gsym
->has_got_offset());
1331 got_offset
= gsym
->got_offset() - target
->got_size();
1335 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1336 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1338 have_got_offset
= true;
1347 case elfcpp::R_386_NONE
:
1348 case elfcpp::R_386_GNU_VTINHERIT
:
1349 case elfcpp::R_386_GNU_VTENTRY
:
1352 case elfcpp::R_386_32
:
1353 if (should_apply_static_reloc(gsym
, false, true))
1354 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1357 case elfcpp::R_386_PC32
:
1358 if (should_apply_static_reloc(gsym
, true, true))
1359 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1362 case elfcpp::R_386_16
:
1363 if (should_apply_static_reloc(gsym
, false, false))
1364 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1367 case elfcpp::R_386_PC16
:
1368 if (should_apply_static_reloc(gsym
, true, false))
1369 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1372 case elfcpp::R_386_8
:
1373 if (should_apply_static_reloc(gsym
, false, false))
1374 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1377 case elfcpp::R_386_PC8
:
1378 if (should_apply_static_reloc(gsym
, true, false))
1379 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1382 case elfcpp::R_386_PLT32
:
1383 gold_assert(gsym
== NULL
1384 || gsym
->has_plt_offset()
1385 || gsym
->final_value_is_known());
1386 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1389 case elfcpp::R_386_GOT32
:
1390 gold_assert(have_got_offset
);
1391 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1394 case elfcpp::R_386_GOTOFF
:
1396 elfcpp::Elf_types
<32>::Elf_Addr value
;
1397 value
= (psymval
->value(object
, 0)
1398 - target
->got_plt_section()->address());
1399 Relocate_functions
<32, false>::rel32(view
, value
);
1403 case elfcpp::R_386_GOTPC
:
1405 elfcpp::Elf_types
<32>::Elf_Addr value
;
1406 value
= target
->got_plt_section()->address();
1407 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1411 case elfcpp::R_386_COPY
:
1412 case elfcpp::R_386_GLOB_DAT
:
1413 case elfcpp::R_386_JUMP_SLOT
:
1414 case elfcpp::R_386_RELATIVE
:
1415 // These are outstanding tls relocs, which are unexpected when
1417 case elfcpp::R_386_TLS_TPOFF
:
1418 case elfcpp::R_386_TLS_DTPMOD32
:
1419 case elfcpp::R_386_TLS_DTPOFF32
:
1420 case elfcpp::R_386_TLS_TPOFF32
:
1421 case elfcpp::R_386_TLS_DESC
:
1422 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1423 _("unexpected reloc %u in object file"),
1427 // These are initial tls relocs, which are expected when
1429 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1430 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1431 case elfcpp::R_386_TLS_DESC_CALL
:
1432 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1433 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1434 case elfcpp::R_386_TLS_IE
: // Initial-exec
1435 case elfcpp::R_386_TLS_IE_32
:
1436 case elfcpp::R_386_TLS_GOTIE
:
1437 case elfcpp::R_386_TLS_LE
: // Local-exec
1438 case elfcpp::R_386_TLS_LE_32
:
1439 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1440 address
, view_size
);
1443 case elfcpp::R_386_32PLT
:
1444 case elfcpp::R_386_TLS_GD_32
:
1445 case elfcpp::R_386_TLS_GD_PUSH
:
1446 case elfcpp::R_386_TLS_GD_CALL
:
1447 case elfcpp::R_386_TLS_GD_POP
:
1448 case elfcpp::R_386_TLS_LDM_32
:
1449 case elfcpp::R_386_TLS_LDM_PUSH
:
1450 case elfcpp::R_386_TLS_LDM_CALL
:
1451 case elfcpp::R_386_TLS_LDM_POP
:
1452 case elfcpp::R_386_USED_BY_INTEL_200
:
1454 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1455 _("unsupported reloc %u"),
1463 // Perform a TLS relocation.
1466 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1468 const elfcpp::Rel
<32, false>& rel
,
1469 unsigned int r_type
,
1470 const Sized_symbol
<32>* gsym
,
1471 const Symbol_value
<32>* psymval
,
1472 unsigned char* view
,
1473 elfcpp::Elf_types
<32>::Elf_Addr
,
1476 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1477 if (tls_segment
== NULL
)
1479 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1480 _("TLS reloc but no TLS segment"));
1484 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1486 const bool is_final
= (gsym
== NULL
1487 ? !parameters
->output_is_position_independent()
1488 : gsym
->final_value_is_known());
1489 const tls::Tls_optimization optimized_type
1490 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1493 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1494 if (optimized_type
== tls::TLSOPT_TO_LE
)
1496 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1497 rel
, r_type
, value
, view
,
1501 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1502 _("unsupported reloc %u"),
1506 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1507 case elfcpp::R_386_TLS_DESC_CALL
:
1508 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1509 _("unsupported reloc %u"),
1513 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1514 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1516 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1517 _("both SUN and GNU model "
1518 "TLS relocations"));
1521 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1522 if (optimized_type
== tls::TLSOPT_TO_LE
)
1524 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1525 value
, view
, view_size
);
1528 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1529 _("unsupported reloc %u"),
1533 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1534 // This reloc can appear in debugging sections, in which case we
1535 // won't see the TLS_LDM reloc. The local_dynamic_type field
1537 if (optimized_type
!= tls::TLSOPT_TO_LE
1538 || this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1539 value
= value
- tls_segment
->vaddr();
1540 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1541 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1543 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1544 Relocate_functions
<32, false>::rel32(view
, value
);
1547 case elfcpp::R_386_TLS_IE
: // Initial-exec
1548 case elfcpp::R_386_TLS_GOTIE
:
1549 case elfcpp::R_386_TLS_IE_32
:
1550 if (optimized_type
== tls::TLSOPT_TO_LE
)
1552 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1553 rel
, r_type
, value
, view
,
1557 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1558 _("unsupported reloc %u"),
1562 case elfcpp::R_386_TLS_LE
: // Local-exec
1563 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1564 Relocate_functions
<32, false>::rel32(view
, value
);
1567 case elfcpp::R_386_TLS_LE_32
:
1568 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1569 Relocate_functions
<32, false>::rel32(view
, value
);
1574 // Do a relocation in which we convert a TLS General-Dynamic to a
1578 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1580 Output_segment
* tls_segment
,
1581 const elfcpp::Rel
<32, false>& rel
,
1583 elfcpp::Elf_types
<32>::Elf_Addr value
,
1584 unsigned char* view
,
1587 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1588 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1589 // leal foo(%reg),%eax; call ___tls_get_addr
1590 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1592 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1593 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1595 unsigned char op1
= view
[-1];
1596 unsigned char op2
= view
[-2];
1598 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1599 op2
== 0x8d || op2
== 0x04);
1600 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1606 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1607 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1608 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1609 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1610 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1614 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1615 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1616 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1619 // There is a trailing nop. Use the size byte subl.
1620 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1625 // Use the five byte subl.
1626 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1630 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1631 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1633 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1635 this->skip_call_tls_get_addr_
= true;
1638 // Do a relocation in which we convert a TLS Local-Dynamic to a
1642 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1645 const elfcpp::Rel
<32, false>& rel
,
1647 elfcpp::Elf_types
<32>::Elf_Addr
,
1648 unsigned char* view
,
1651 // leal foo(%reg), %eax; call ___tls_get_addr
1652 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1654 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1655 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1657 // FIXME: Does this test really always pass?
1658 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1659 view
[-2] == 0x8d && view
[-1] == 0x83);
1661 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1663 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1665 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1667 this->skip_call_tls_get_addr_
= true;
1670 // Do a relocation in which we convert a TLS Initial-Exec to a
1674 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1676 Output_segment
* tls_segment
,
1677 const elfcpp::Rel
<32, false>& rel
,
1678 unsigned int r_type
,
1679 elfcpp::Elf_types
<32>::Elf_Addr value
,
1680 unsigned char* view
,
1683 // We have to actually change the instructions, which means that we
1684 // need to examine the opcodes to figure out which instruction we
1686 if (r_type
== elfcpp::R_386_TLS_IE
)
1688 // movl %gs:XX,%eax ==> movl $YY,%eax
1689 // movl %gs:XX,%reg ==> movl $YY,%reg
1690 // addl %gs:XX,%reg ==> addl $YY,%reg
1691 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
1692 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1694 unsigned char op1
= view
[-1];
1697 // movl XX,%eax ==> movl $YY,%eax
1702 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1704 unsigned char op2
= view
[-2];
1707 // movl XX,%reg ==> movl $YY,%reg
1708 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1709 (op1
& 0xc7) == 0x05);
1711 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1713 else if (op2
== 0x03)
1715 // addl XX,%reg ==> addl $YY,%reg
1716 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1717 (op1
& 0xc7) == 0x05);
1719 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1722 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1727 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1728 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1729 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1730 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1731 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1733 unsigned char op1
= view
[-1];
1734 unsigned char op2
= view
[-2];
1735 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1736 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1739 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1741 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1743 else if (op2
== 0x2b)
1745 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1747 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1749 else if (op2
== 0x03)
1751 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1753 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1756 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1759 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1760 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1763 Relocate_functions
<32, false>::rel32(view
, value
);
1766 // Relocate section data.
1769 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1770 unsigned int sh_type
,
1771 const unsigned char* prelocs
,
1773 unsigned char* view
,
1774 elfcpp::Elf_types
<32>::Elf_Addr address
,
1777 gold_assert(sh_type
== elfcpp::SHT_REL
);
1779 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1780 Target_i386::Relocate
>(
1790 // Return the value to use for a dynamic which requires special
1791 // treatment. This is how we support equality comparisons of function
1792 // pointers across shared library boundaries, as described in the
1793 // processor specific ABI supplement.
1796 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1798 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1799 return this->plt_section()->address() + gsym
->plt_offset();
1802 // Return a string used to fill a code section with nops to take up
1803 // the specified length.
1806 Target_i386::do_code_fill(off_t length
)
1810 // Build a jmp instruction to skip over the bytes.
1811 unsigned char jmp
[5];
1813 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1814 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1815 + std::string(length
- 5, '\0'));
1818 // Nop sequences of various lengths.
1819 const char nop1
[1] = { 0x90 }; // nop
1820 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1821 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1822 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1823 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1824 0x00 }; // leal 0(%esi,1),%esi
1825 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1827 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1829 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1830 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1831 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1832 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1834 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1835 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1837 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1838 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1840 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1841 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1842 0x00, 0x00, 0x00, 0x00 };
1843 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1844 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1845 0x27, 0x00, 0x00, 0x00,
1847 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1848 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1849 0xbc, 0x27, 0x00, 0x00,
1851 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1852 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1853 0x90, 0x90, 0x90, 0x90,
1856 const char* nops
[16] = {
1858 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1859 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1862 return std::string(nops
[length
], length
);
1865 // The selector for i386 object files.
1867 class Target_selector_i386
: public Target_selector
1870 Target_selector_i386()
1871 : Target_selector(elfcpp::EM_386
, 32, false)
1875 recognize(int machine
, int osabi
, int abiversion
);
1878 Target_i386
* target_
;
1881 // Recognize an i386 object file when we already know that the machine
1882 // number is EM_386.
1885 Target_selector_i386::recognize(int, int, int)
1887 if (this->target_
== NULL
)
1888 this->target_
= new Target_i386();
1889 return this->target_
;
1892 Target_selector_i386 target_selector_i386
;
1894 } // End anonymous namespace.