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 whether SYM is defined by the ABI.
105 do_is_defined_by_abi(Symbol
* sym
) const
106 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
108 // Return the size of the GOT section.
112 gold_assert(this->got_
!= NULL
);
113 return this->got_
->data_size();
117 // The class which scans relocations.
121 local(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
,
126 const elfcpp::Sym
<32, false>& lsym
);
129 global(const General_options
& options
, Symbol_table
* symtab
,
130 Layout
* layout
, Target_i386
* target
,
131 Sized_relobj
<32, false>* object
,
132 unsigned int data_shndx
,
133 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
137 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
140 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
144 // The class which implements relocation.
149 : skip_call_tls_get_addr_(false),
150 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
155 if (this->skip_call_tls_get_addr_
)
157 // FIXME: This needs to specify the location somehow.
158 gold_error(_("missing expected TLS relocation"));
162 // Return whether the static relocation needs to be applied.
164 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
165 bool is_absolute_ref
,
166 bool is_function_call
,
169 // Do a relocation. Return false if the caller should not issue
170 // any warnings about this relocation.
172 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
173 const elfcpp::Rel
<32, false>&,
174 unsigned int r_type
, const Sized_symbol
<32>*,
175 const Symbol_value
<32>*,
176 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
180 // Do a TLS relocation.
182 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
183 const elfcpp::Rel
<32, false>&,
184 unsigned int r_type
, const Sized_symbol
<32>*,
185 const Symbol_value
<32>*,
186 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
188 // Do a TLS General-Dynamic to Local-Exec transition.
190 tls_gd_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 Local-Dynamic to Local-Exec transition.
199 tls_ld_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 // Do a TLS Initial-Exec to Local-Exec transition.
208 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
209 Output_segment
* tls_segment
,
210 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
211 elfcpp::Elf_types
<32>::Elf_Addr value
,
215 // We need to keep track of which type of local dynamic relocation
216 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
217 enum Local_dynamic_type
224 // This is set if we should skip the next reloc, which should be a
225 // PLT32 reloc against ___tls_get_addr.
226 bool skip_call_tls_get_addr_
;
227 // The type of local dynamic relocation we have seen in the section
228 // being relocated, if any.
229 Local_dynamic_type local_dynamic_type_
;
232 // Adjust TLS relocation type based on the options and whether this
233 // is a local symbol.
234 static tls::Tls_optimization
235 optimize_tls_reloc(bool is_final
, int r_type
);
237 // Get the GOT section, creating it if necessary.
238 Output_data_got
<32, false>*
239 got_section(Symbol_table
*, Layout
*);
241 // Get the GOT PLT section.
243 got_plt_section() const
245 gold_assert(this->got_plt_
!= NULL
);
246 return this->got_plt_
;
249 // Create a PLT entry for a global symbol.
251 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
253 // Get the PLT section.
254 const Output_data_plt_i386
*
257 gold_assert(this->plt_
!= NULL
);
261 // Get the dynamic reloc section, creating it if necessary.
263 rel_dyn_section(Layout
*);
265 // Return true if the symbol may need a COPY relocation.
266 // References from an executable object to non-function symbols
267 // defined in a dynamic object may need a COPY relocation.
269 may_need_copy_reloc(Symbol
* gsym
)
271 return (!parameters
->output_is_shared()
272 && gsym
->is_from_dynobj()
273 && gsym
->type() != elfcpp::STT_FUNC
);
276 // Copy a relocation against a global symbol.
278 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
279 Sized_relobj
<32, false>*, unsigned int,
280 Symbol
*, const elfcpp::Rel
<32, false>&);
282 // Information about this specific target which we pass to the
283 // general Target structure.
284 static const Target::Target_info i386_info
;
287 Output_data_got
<32, false>* got_
;
289 Output_data_plt_i386
* plt_
;
290 // The GOT PLT section.
291 Output_data_space
* got_plt_
;
292 // The dynamic reloc section.
293 Reloc_section
* rel_dyn_
;
294 // Relocs saved to avoid a COPY reloc.
295 Copy_relocs
<32, false>* copy_relocs_
;
296 // Space for variables copied with a COPY reloc.
297 Output_data_space
* dynbss_
;
300 const Target::Target_info
Target_i386::i386_info
=
303 false, // is_big_endian
304 elfcpp::EM_386
, // machine_code
305 false, // has_make_symbol
306 false, // has_resolve
307 true, // has_code_fill
308 true, // is_default_stack_executable
309 "/usr/lib/libc.so.1", // dynamic_linker
310 0x08048000, // default_text_segment_address
311 0x1000, // abi_pagesize
312 0x1000 // common_pagesize
315 // Get the GOT section, creating it if necessary.
317 Output_data_got
<32, false>*
318 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
320 if (this->got_
== NULL
)
322 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
324 this->got_
= new Output_data_got
<32, false>();
326 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
327 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
330 // The old GNU linker creates a .got.plt section. We just
331 // create another set of data in the .got section. Note that we
332 // always create a PLT if we create a GOT, although the PLT
334 this->got_plt_
= new Output_data_space(4);
335 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
336 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
339 // The first three entries are reserved.
340 this->got_plt_
->set_space_size(3 * 4);
342 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
343 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
345 0, 0, elfcpp::STT_OBJECT
,
347 elfcpp::STV_HIDDEN
, 0,
354 // Get the dynamic reloc section, creating it if necessary.
356 Target_i386::Reloc_section
*
357 Target_i386::rel_dyn_section(Layout
* layout
)
359 if (this->rel_dyn_
== NULL
)
361 gold_assert(layout
!= NULL
);
362 this->rel_dyn_
= new Reloc_section();
363 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
364 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
366 return this->rel_dyn_
;
369 // A class to handle the PLT data.
371 class Output_data_plt_i386
: public Output_section_data
374 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
376 Output_data_plt_i386(Layout
*, Output_data_space
*);
378 // Add an entry to the PLT.
380 add_entry(Symbol
* gsym
);
382 // Return the .rel.plt section data.
385 { return this->rel_
; }
389 do_adjust_output_section(Output_section
* os
);
392 // The size of an entry in the PLT.
393 static const int plt_entry_size
= 16;
395 // The first entry in the PLT for an executable.
396 static unsigned char exec_first_plt_entry
[plt_entry_size
];
398 // The first entry in the PLT for a shared object.
399 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
401 // Other entries in the PLT for an executable.
402 static unsigned char exec_plt_entry
[plt_entry_size
];
404 // Other entries in the PLT for a shared object.
405 static unsigned char dyn_plt_entry
[plt_entry_size
];
407 // Set the final size.
409 do_set_address(uint64_t, off_t
)
410 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
412 // Write out the PLT data.
414 do_write(Output_file
*);
416 // The reloc section.
418 // The .got.plt section.
419 Output_data_space
* got_plt_
;
420 // The number of PLT entries.
424 // Create the PLT section. The ordinary .got section is an argument,
425 // since we need to refer to the start. We also create our own .got
426 // section just for PLT entries.
428 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
429 Output_data_space
* got_plt
)
430 : Output_section_data(4), got_plt_(got_plt
), count_(0)
432 this->rel_
= new Reloc_section();
433 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
434 elfcpp::SHF_ALLOC
, this->rel_
);
438 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
440 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
441 // linker, and so do we.
445 // Add an entry to the PLT.
448 Output_data_plt_i386::add_entry(Symbol
* gsym
)
450 gold_assert(!gsym
->has_plt_offset());
452 // Note that when setting the PLT offset we skip the initial
453 // reserved PLT entry.
454 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
458 off_t got_offset
= this->got_plt_
->data_size();
460 // Every PLT entry needs a GOT entry which points back to the PLT
461 // entry (this will be changed by the dynamic linker, normally
462 // lazily when the function is called).
463 this->got_plt_
->set_space_size(got_offset
+ 4);
465 // Every PLT entry needs a reloc.
466 gsym
->set_needs_dynsym_entry();
467 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
470 // Note that we don't need to save the symbol. The contents of the
471 // PLT are independent of which symbols are used. The symbols only
472 // appear in the relocations.
475 // The first entry in the PLT for an executable.
477 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
479 0xff, 0x35, // pushl contents of memory address
480 0, 0, 0, 0, // replaced with address of .got + 4
481 0xff, 0x25, // jmp indirect
482 0, 0, 0, 0, // replaced with address of .got + 8
486 // The first entry in the PLT for a shared object.
488 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
490 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
491 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
495 // Subsequent entries in the PLT for an executable.
497 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
499 0xff, 0x25, // jmp indirect
500 0, 0, 0, 0, // replaced with address of symbol in .got
501 0x68, // pushl immediate
502 0, 0, 0, 0, // replaced with offset into relocation table
503 0xe9, // jmp relative
504 0, 0, 0, 0 // replaced with offset to start of .plt
507 // Subsequent entries in the PLT for a shared object.
509 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
511 0xff, 0xa3, // jmp *offset(%ebx)
512 0, 0, 0, 0, // replaced with offset of symbol in .got
513 0x68, // pushl immediate
514 0, 0, 0, 0, // replaced with offset into relocation table
515 0xe9, // jmp relative
516 0, 0, 0, 0 // replaced with offset to start of .plt
519 // Write out the PLT. This uses the hand-coded instructions above,
520 // and adjusts them as needed. This is all specified by the i386 ELF
521 // Processor Supplement.
524 Output_data_plt_i386::do_write(Output_file
* of
)
526 const off_t offset
= this->offset();
527 const off_t oview_size
= this->data_size();
528 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
530 const off_t got_file_offset
= this->got_plt_
->offset();
531 const off_t got_size
= this->got_plt_
->data_size();
532 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
535 unsigned char* pov
= oview
;
537 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
538 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
540 if (parameters
->output_is_shared())
541 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
544 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
545 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
546 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
548 pov
+= plt_entry_size
;
550 unsigned char* got_pov
= got_view
;
552 memset(got_pov
, 0, 12);
555 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
557 unsigned int plt_offset
= plt_entry_size
;
558 unsigned int plt_rel_offset
= 0;
559 unsigned int got_offset
= 12;
560 const unsigned int count
= this->count_
;
561 for (unsigned int i
= 0;
564 pov
+= plt_entry_size
,
566 plt_offset
+= plt_entry_size
,
567 plt_rel_offset
+= rel_size
,
570 // Set and adjust the PLT entry itself.
572 if (parameters
->output_is_shared())
574 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
575 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
579 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
580 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
585 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
586 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
587 - (plt_offset
+ plt_entry_size
));
589 // Set the entry in the GOT.
590 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
593 gold_assert(pov
- oview
== oview_size
);
594 gold_assert(got_pov
- got_view
== got_size
);
596 of
->write_output_view(offset
, oview_size
, oview
);
597 of
->write_output_view(got_file_offset
, got_size
, got_view
);
600 // Create a PLT entry for a global symbol.
603 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
605 if (gsym
->has_plt_offset())
608 if (this->plt_
== NULL
)
610 // Create the GOT sections first.
611 this->got_section(symtab
, layout
);
613 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
614 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
616 | elfcpp::SHF_EXECINSTR
),
620 this->plt_
->add_entry(gsym
);
623 // Handle a relocation against a non-function symbol defined in a
624 // dynamic object. The traditional way to handle this is to generate
625 // a COPY relocation to copy the variable at runtime from the shared
626 // object into the executable's data segment. However, this is
627 // undesirable in general, as if the size of the object changes in the
628 // dynamic object, the executable will no longer work correctly. If
629 // this relocation is in a writable section, then we can create a
630 // dynamic reloc and the dynamic linker will resolve it to the correct
631 // address at runtime. However, we do not want do that if the
632 // relocation is in a read-only section, as it would prevent the
633 // readonly segment from being shared. And if we have to eventually
634 // generate a COPY reloc, then any dynamic relocations will be
635 // useless. So this means that if this is a writable section, we need
636 // to save the relocation until we see whether we have to create a
637 // COPY relocation for this symbol for any other relocation.
640 Target_i386::copy_reloc(const General_options
* options
,
641 Symbol_table
* symtab
,
643 Sized_relobj
<32, false>* object
,
644 unsigned int data_shndx
, Symbol
* gsym
,
645 const elfcpp::Rel
<32, false>& rel
)
647 Sized_symbol
<32>* ssym
;
648 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
651 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
654 // So far we do not need a COPY reloc. Save this relocation.
655 // If it turns out that we never need a COPY reloc for this
656 // symbol, then we will emit the relocation.
657 if (this->copy_relocs_
== NULL
)
658 this->copy_relocs_
= new Copy_relocs
<32, false>();
659 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
663 // Allocate space for this symbol in the .bss section.
665 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
667 // There is no defined way to determine the required alignment
668 // of the symbol. We pick the alignment based on the size. We
669 // set an arbitrary maximum of 256.
671 for (align
= 1; align
< 512; align
<<= 1)
672 if ((symsize
& align
) != 0)
675 if (this->dynbss_
== NULL
)
677 this->dynbss_
= new Output_data_space(align
);
678 layout
->add_output_section_data(".bss",
681 | elfcpp::SHF_WRITE
),
685 Output_data_space
* dynbss
= this->dynbss_
;
687 if (align
> dynbss
->addralign())
688 dynbss
->set_space_alignment(align
);
690 off_t dynbss_size
= dynbss
->data_size();
691 dynbss_size
= align_address(dynbss_size
, align
);
692 off_t offset
= dynbss_size
;
693 dynbss
->set_space_size(dynbss_size
+ symsize
);
695 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
697 // Add the COPY reloc.
698 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
699 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
703 // Optimize the TLS relocation type based on what we know about the
704 // symbol. IS_FINAL is true if the final address of this symbol is
705 // known at link time.
707 tls::Tls_optimization
708 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
710 // If we are generating a shared library, then we can't do anything
712 if (parameters
->output_is_shared())
713 return tls::TLSOPT_NONE
;
717 case elfcpp::R_386_TLS_GD
:
718 case elfcpp::R_386_TLS_GOTDESC
:
719 case elfcpp::R_386_TLS_DESC_CALL
:
720 // These are General-Dynamic which permits fully general TLS
721 // access. Since we know that we are generating an executable,
722 // we can convert this to Initial-Exec. If we also know that
723 // this is a local symbol, we can further switch to Local-Exec.
725 return tls::TLSOPT_TO_LE
;
726 return tls::TLSOPT_TO_IE
;
728 case elfcpp::R_386_TLS_LDM
:
729 // This is Local-Dynamic, which refers to a local symbol in the
730 // dynamic TLS block. Since we know that we generating an
731 // executable, we can switch to Local-Exec.
732 return tls::TLSOPT_TO_LE
;
734 case elfcpp::R_386_TLS_LDO_32
:
735 // Another type of Local-Dynamic relocation.
736 return tls::TLSOPT_TO_LE
;
738 case elfcpp::R_386_TLS_IE
:
739 case elfcpp::R_386_TLS_GOTIE
:
740 case elfcpp::R_386_TLS_IE_32
:
741 // These are Initial-Exec relocs which get the thread offset
742 // from the GOT. If we know that we are linking against the
743 // local symbol, we can switch to Local-Exec, which links the
744 // thread offset into the instruction.
746 return tls::TLSOPT_TO_LE
;
747 return tls::TLSOPT_NONE
;
749 case elfcpp::R_386_TLS_LE
:
750 case elfcpp::R_386_TLS_LE_32
:
751 // When we already have Local-Exec, there is nothing further we
753 return tls::TLSOPT_NONE
;
760 // Report an unsupported relocation against a local symbol.
763 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
766 gold_error(_("%s: unsupported reloc %u against local symbol"),
767 object
->name().c_str(), r_type
);
770 // Scan a relocation for a local symbol.
773 Target_i386::Scan::local(const General_options
&,
774 Symbol_table
* symtab
,
777 Sized_relobj
<32, false>* object
,
778 unsigned int data_shndx
,
779 const elfcpp::Rel
<32, false>& reloc
,
781 const elfcpp::Sym
<32, false>&)
785 case elfcpp::R_386_NONE
:
786 case elfcpp::R_386_GNU_VTINHERIT
:
787 case elfcpp::R_386_GNU_VTENTRY
:
790 case elfcpp::R_386_32
:
791 // If building a shared library (or a position-independent
792 // executable), we need to create a dynamic relocation for
793 // this location. The relocation applied at link time will
794 // apply the link-time value, so we flag the location with
795 // an R_386_RELATIVE relocation so the dynamic loader can
796 // relocate it easily.
797 if (parameters
->output_is_position_independent())
799 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
800 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
801 reloc
.get_r_offset());
805 case elfcpp::R_386_16
:
806 case elfcpp::R_386_8
:
807 // If building a shared library (or a position-independent
808 // executable), we need to create a dynamic relocation for
809 // this location. Because the addend needs to remain in the
810 // data section, we need to be careful not to apply this
811 // relocation statically.
812 if (parameters
->output_is_position_independent())
814 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
815 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
816 rel_dyn
->add_local(object
, r_sym
, r_type
, data_shndx
,
817 reloc
.get_r_offset());
821 case elfcpp::R_386_PC32
:
822 case elfcpp::R_386_PC16
:
823 case elfcpp::R_386_PC8
:
826 case elfcpp::R_386_PLT32
:
827 // Since we know this is a local symbol, we can handle this as a
831 case elfcpp::R_386_GOTOFF
:
832 case elfcpp::R_386_GOTPC
:
833 // We need a GOT section.
834 target
->got_section(symtab
, layout
);
837 case elfcpp::R_386_GOT32
:
839 // The symbol requires a GOT entry.
840 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
841 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
842 if (got
->add_local(object
, r_sym
))
844 // If we are generating a shared object, we need to add a
845 // dynamic RELATIVE relocation for this symbol.
846 if (parameters
->output_is_position_independent())
848 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
849 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
850 data_shndx
, reloc
.get_r_offset());
856 // These are relocations which should only be seen by the
857 // dynamic linker, and should never be seen here.
858 case elfcpp::R_386_COPY
:
859 case elfcpp::R_386_GLOB_DAT
:
860 case elfcpp::R_386_JUMP_SLOT
:
861 case elfcpp::R_386_RELATIVE
:
862 case elfcpp::R_386_TLS_TPOFF
:
863 case elfcpp::R_386_TLS_DTPMOD32
:
864 case elfcpp::R_386_TLS_DTPOFF32
:
865 case elfcpp::R_386_TLS_TPOFF32
:
866 case elfcpp::R_386_TLS_DESC
:
867 gold_error(_("%s: unexpected reloc %u in object file"),
868 object
->name().c_str(), r_type
);
871 // These are initial TLS relocs, which are expected when
873 case elfcpp::R_386_TLS_GD
: // Global-dynamic
874 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
875 case elfcpp::R_386_TLS_DESC_CALL
:
876 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
877 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
878 case elfcpp::R_386_TLS_IE
: // Initial-exec
879 case elfcpp::R_386_TLS_IE_32
:
880 case elfcpp::R_386_TLS_GOTIE
:
881 case elfcpp::R_386_TLS_LE
: // Local-exec
882 case elfcpp::R_386_TLS_LE_32
:
884 bool output_is_shared
= parameters
->output_is_shared();
885 const tls::Tls_optimization optimized_type
886 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
889 case elfcpp::R_386_TLS_GD
: // Global-dynamic
890 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
891 case elfcpp::R_386_TLS_DESC_CALL
:
892 // FIXME: If not relaxing to LE, we need to generate
893 // DTPMOD32 and DTPOFF32 relocs.
894 if (optimized_type
!= tls::TLSOPT_TO_LE
)
895 unsupported_reloc_local(object
, r_type
);
898 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
899 // FIXME: If not relaxing to LE, we need to generate a
901 if (optimized_type
!= tls::TLSOPT_TO_LE
)
902 unsupported_reloc_local(object
, r_type
);
905 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
908 case elfcpp::R_386_TLS_IE
: // Initial-exec
909 case elfcpp::R_386_TLS_IE_32
:
910 case elfcpp::R_386_TLS_GOTIE
:
911 // FIXME: If not relaxing to LE, we need to generate a
912 // TPOFF or TPOFF32 reloc.
913 if (optimized_type
!= tls::TLSOPT_TO_LE
)
914 unsupported_reloc_local(object
, r_type
);
917 case elfcpp::R_386_TLS_LE
: // Local-exec
918 case elfcpp::R_386_TLS_LE_32
:
919 // FIXME: If generating a shared object, we need to copy
920 // this relocation into the object.
921 gold_assert(!output_is_shared
);
930 case elfcpp::R_386_32PLT
:
931 case elfcpp::R_386_TLS_GD_32
:
932 case elfcpp::R_386_TLS_GD_PUSH
:
933 case elfcpp::R_386_TLS_GD_CALL
:
934 case elfcpp::R_386_TLS_GD_POP
:
935 case elfcpp::R_386_TLS_LDM_32
:
936 case elfcpp::R_386_TLS_LDM_PUSH
:
937 case elfcpp::R_386_TLS_LDM_CALL
:
938 case elfcpp::R_386_TLS_LDM_POP
:
939 case elfcpp::R_386_USED_BY_INTEL_200
:
941 unsupported_reloc_local(object
, r_type
);
946 // Report an unsupported relocation against a global symbol.
949 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
953 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
954 object
->name().c_str(), r_type
, gsym
->name());
957 // Scan a relocation for a global symbol.
960 Target_i386::Scan::global(const General_options
& options
,
961 Symbol_table
* symtab
,
964 Sized_relobj
<32, false>* object
,
965 unsigned int data_shndx
,
966 const elfcpp::Rel
<32, false>& reloc
,
972 case elfcpp::R_386_NONE
:
973 case elfcpp::R_386_GNU_VTINHERIT
:
974 case elfcpp::R_386_GNU_VTENTRY
:
977 case elfcpp::R_386_32
:
978 case elfcpp::R_386_16
:
979 case elfcpp::R_386_8
:
981 // Make a PLT entry if necessary.
982 if (gsym
->needs_plt_entry())
984 target
->make_plt_entry(symtab
, layout
, gsym
);
985 // Since this is not a PC-relative relocation, we may be
986 // taking the address of a function. In that case we need to
987 // set the entry in the dynamic symbol table to the address of
989 if (gsym
->is_from_dynobj())
990 gsym
->set_needs_dynsym_value();
992 // Make a dynamic relocation if necessary.
993 if (gsym
->needs_dynamic_reloc(true, false))
995 if (target
->may_need_copy_reloc(gsym
))
997 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
1000 else if (r_type
== elfcpp::R_386_32
1001 && gsym
->can_use_relative_reloc(false))
1003 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1004 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
1005 reloc
.get_r_offset());
1009 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1010 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
1011 reloc
.get_r_offset());
1017 case elfcpp::R_386_PC32
:
1018 case elfcpp::R_386_PC16
:
1019 case elfcpp::R_386_PC8
:
1021 // Make a PLT entry if necessary.
1022 if (gsym
->needs_plt_entry())
1023 target
->make_plt_entry(symtab
, layout
, gsym
);
1024 // Make a dynamic relocation if necessary.
1025 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1026 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1028 if (target
->may_need_copy_reloc(gsym
))
1030 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
1035 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1036 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
1037 reloc
.get_r_offset());
1043 case elfcpp::R_386_GOT32
:
1045 // The symbol requires a GOT entry.
1046 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1047 if (got
->add_global(gsym
))
1049 // If this symbol is not fully resolved, we need to add a
1050 // dynamic relocation for it.
1051 if (!gsym
->final_value_is_known())
1053 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1054 if (gsym
->is_from_dynobj()
1055 || gsym
->is_preemptible())
1056 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
1057 gsym
->got_offset());
1060 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
1061 got
, gsym
->got_offset());
1062 // Make sure we write the link-time value to the GOT.
1063 gsym
->set_needs_value_in_got();
1070 case elfcpp::R_386_PLT32
:
1071 // If the symbol is fully resolved, this is just a PC32 reloc.
1072 // Otherwise we need a PLT entry.
1073 if (gsym
->final_value_is_known())
1075 // If building a shared library, we can also skip the PLT entry
1076 // if the symbol is defined in the output file and is protected
1078 if (gsym
->is_defined()
1079 && !gsym
->is_from_dynobj()
1080 && !gsym
->is_preemptible())
1082 target
->make_plt_entry(symtab
, layout
, gsym
);
1085 case elfcpp::R_386_GOTOFF
:
1086 case elfcpp::R_386_GOTPC
:
1087 // We need a GOT section.
1088 target
->got_section(symtab
, layout
);
1091 // These are relocations which should only be seen by the
1092 // dynamic linker, and should never be seen here.
1093 case elfcpp::R_386_COPY
:
1094 case elfcpp::R_386_GLOB_DAT
:
1095 case elfcpp::R_386_JUMP_SLOT
:
1096 case elfcpp::R_386_RELATIVE
:
1097 case elfcpp::R_386_TLS_TPOFF
:
1098 case elfcpp::R_386_TLS_DTPMOD32
:
1099 case elfcpp::R_386_TLS_DTPOFF32
:
1100 case elfcpp::R_386_TLS_TPOFF32
:
1101 case elfcpp::R_386_TLS_DESC
:
1102 gold_error(_("%s: unexpected reloc %u in object file"),
1103 object
->name().c_str(), r_type
);
1106 // These are initial tls relocs, which are expected when
1108 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1109 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1110 case elfcpp::R_386_TLS_DESC_CALL
:
1111 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1112 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1113 case elfcpp::R_386_TLS_IE
: // Initial-exec
1114 case elfcpp::R_386_TLS_IE_32
:
1115 case elfcpp::R_386_TLS_GOTIE
:
1116 case elfcpp::R_386_TLS_LE
: // Local-exec
1117 case elfcpp::R_386_TLS_LE_32
:
1119 const bool is_final
= gsym
->final_value_is_known();
1120 const tls::Tls_optimization optimized_type
1121 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1124 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1125 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1126 case elfcpp::R_386_TLS_DESC_CALL
:
1127 // FIXME: If not relaxing to LE, we need to generate
1128 // DTPMOD32 and DTPOFF32 relocs.
1129 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1130 unsupported_reloc_global(object
, r_type
, gsym
);
1133 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1134 // FIXME: If not relaxing to LE, we need to generate a
1136 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1137 unsupported_reloc_global(object
, r_type
, gsym
);
1140 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1143 case elfcpp::R_386_TLS_IE
: // Initial-exec
1144 case elfcpp::R_386_TLS_IE_32
:
1145 case elfcpp::R_386_TLS_GOTIE
:
1146 // FIXME: If not relaxing to LE, we need to generate a
1147 // TPOFF or TPOFF32 reloc.
1148 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1149 unsupported_reloc_global(object
, r_type
, gsym
);
1152 case elfcpp::R_386_TLS_LE
: // Local-exec
1153 case elfcpp::R_386_TLS_LE_32
:
1154 // FIXME: If generating a shared object, we need to copy
1155 // this relocation into the object.
1156 gold_assert(!parameters
->output_is_shared());
1165 case elfcpp::R_386_32PLT
:
1166 case elfcpp::R_386_TLS_GD_32
:
1167 case elfcpp::R_386_TLS_GD_PUSH
:
1168 case elfcpp::R_386_TLS_GD_CALL
:
1169 case elfcpp::R_386_TLS_GD_POP
:
1170 case elfcpp::R_386_TLS_LDM_32
:
1171 case elfcpp::R_386_TLS_LDM_PUSH
:
1172 case elfcpp::R_386_TLS_LDM_CALL
:
1173 case elfcpp::R_386_TLS_LDM_POP
:
1174 case elfcpp::R_386_USED_BY_INTEL_200
:
1176 unsupported_reloc_global(object
, r_type
, gsym
);
1181 // Scan relocations for a section.
1184 Target_i386::scan_relocs(const General_options
& options
,
1185 Symbol_table
* symtab
,
1187 Sized_relobj
<32, false>* object
,
1188 unsigned int data_shndx
,
1189 unsigned int sh_type
,
1190 const unsigned char* prelocs
,
1192 Output_section
* output_section
,
1193 bool needs_special_offset_handling
,
1194 size_t local_symbol_count
,
1195 const unsigned char* plocal_symbols
)
1197 if (sh_type
== elfcpp::SHT_RELA
)
1199 gold_error(_("%s: unsupported RELA reloc section"),
1200 object
->name().c_str());
1204 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1215 needs_special_offset_handling
,
1220 // Finalize the sections.
1223 Target_i386::do_finalize_sections(Layout
* layout
)
1225 // Fill in some more dynamic tags.
1226 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1229 if (this->got_plt_
!= NULL
)
1230 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1232 if (this->plt_
!= NULL
)
1234 const Output_data
* od
= this->plt_
->rel_plt();
1235 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1236 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1237 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1240 if (this->rel_dyn_
!= NULL
)
1242 const Output_data
* od
= this->rel_dyn_
;
1243 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1244 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1245 odyn
->add_constant(elfcpp::DT_RELENT
,
1246 elfcpp::Elf_sizes
<32>::rel_size
);
1249 if (!parameters
->output_is_shared())
1251 // The value of the DT_DEBUG tag is filled in by the dynamic
1252 // linker at run time, and used by the debugger.
1253 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1257 // Emit any relocs we saved in an attempt to avoid generating COPY
1259 if (this->copy_relocs_
== NULL
)
1261 if (this->copy_relocs_
->any_to_emit())
1263 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1264 this->copy_relocs_
->emit(rel_dyn
);
1266 delete this->copy_relocs_
;
1267 this->copy_relocs_
= NULL
;
1270 // Return whether a direct absolute static relocation needs to be applied.
1271 // In cases where Scan::local() or Scan::global() has created
1272 // a dynamic relocation other than R_386_RELATIVE, the addend
1273 // of the relocation is carried in the data, and we must not
1274 // apply the static relocation.
1277 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1278 bool is_absolute_ref
,
1279 bool is_function_call
,
1282 // For local symbols, we will have created a non-RELATIVE dynamic
1283 // relocation only if (a) the output is position independent,
1284 // (b) the relocation is absolute (not pc- or segment-relative), and
1285 // (c) the relocation is not 32 bits wide.
1287 return !(parameters
->output_is_position_independent()
1291 // For global symbols, we use the same helper routines used in the scan pass.
1292 return !(gsym
->needs_dynamic_reloc(is_absolute_ref
, is_function_call
)
1293 && !gsym
->can_use_relative_reloc(is_function_call
));
1296 // Perform a relocation.
1299 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1300 Target_i386
* target
,
1302 const elfcpp::Rel
<32, false>& rel
,
1303 unsigned int r_type
,
1304 const Sized_symbol
<32>* gsym
,
1305 const Symbol_value
<32>* psymval
,
1306 unsigned char* view
,
1307 elfcpp::Elf_types
<32>::Elf_Addr address
,
1310 if (this->skip_call_tls_get_addr_
)
1312 if (r_type
!= elfcpp::R_386_PLT32
1314 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1315 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1316 _("missing expected TLS relocation"));
1319 this->skip_call_tls_get_addr_
= false;
1324 // Pick the value to use for symbols defined in shared objects.
1325 Symbol_value
<32> symval
;
1327 && (gsym
->is_from_dynobj()
1328 || (parameters
->output_is_shared()
1329 && gsym
->is_preemptible()))
1330 && gsym
->has_plt_offset())
1332 symval
.set_output_value(target
->plt_section()->address()
1333 + gsym
->plt_offset());
1337 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1339 // Get the GOT offset if needed.
1340 // The GOT pointer points to the end of the GOT section.
1341 // We need to subtract the size of the GOT section to get
1342 // the actual offset to use in the relocation.
1343 bool have_got_offset
= false;
1344 unsigned int got_offset
= 0;
1347 case elfcpp::R_386_GOT32
:
1350 gold_assert(gsym
->has_got_offset());
1351 got_offset
= gsym
->got_offset() - target
->got_size();
1355 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1356 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1358 have_got_offset
= true;
1367 case elfcpp::R_386_NONE
:
1368 case elfcpp::R_386_GNU_VTINHERIT
:
1369 case elfcpp::R_386_GNU_VTENTRY
:
1372 case elfcpp::R_386_32
:
1373 if (should_apply_static_reloc(gsym
, true, false, true))
1374 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1377 case elfcpp::R_386_PC32
:
1379 bool is_function_call
= (gsym
!= NULL
1380 && gsym
->type() == elfcpp::STT_FUNC
);
1381 if (should_apply_static_reloc(gsym
, false, is_function_call
, true))
1382 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1386 case elfcpp::R_386_16
:
1387 if (should_apply_static_reloc(gsym
, true, false, false))
1388 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1391 case elfcpp::R_386_PC16
:
1393 bool is_function_call
= (gsym
!= NULL
1394 && gsym
->type() == elfcpp::STT_FUNC
);
1395 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1396 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1400 case elfcpp::R_386_8
:
1401 if (should_apply_static_reloc(gsym
, true, false, false))
1402 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1405 case elfcpp::R_386_PC8
:
1407 bool is_function_call
= (gsym
!= NULL
1408 && gsym
->type() == elfcpp::STT_FUNC
);
1409 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1410 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1414 case elfcpp::R_386_PLT32
:
1415 gold_assert(gsym
== NULL
1416 || gsym
->has_plt_offset()
1417 || gsym
->final_value_is_known());
1418 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1421 case elfcpp::R_386_GOT32
:
1422 gold_assert(have_got_offset
);
1423 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1426 case elfcpp::R_386_GOTOFF
:
1428 elfcpp::Elf_types
<32>::Elf_Addr value
;
1429 value
= (psymval
->value(object
, 0)
1430 - target
->got_plt_section()->address());
1431 Relocate_functions
<32, false>::rel32(view
, value
);
1435 case elfcpp::R_386_GOTPC
:
1437 elfcpp::Elf_types
<32>::Elf_Addr value
;
1438 value
= target
->got_plt_section()->address();
1439 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1443 case elfcpp::R_386_COPY
:
1444 case elfcpp::R_386_GLOB_DAT
:
1445 case elfcpp::R_386_JUMP_SLOT
:
1446 case elfcpp::R_386_RELATIVE
:
1447 // These are outstanding tls relocs, which are unexpected when
1449 case elfcpp::R_386_TLS_TPOFF
:
1450 case elfcpp::R_386_TLS_DTPMOD32
:
1451 case elfcpp::R_386_TLS_DTPOFF32
:
1452 case elfcpp::R_386_TLS_TPOFF32
:
1453 case elfcpp::R_386_TLS_DESC
:
1454 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1455 _("unexpected reloc %u in object file"),
1459 // These are initial tls relocs, which are expected when
1461 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1462 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1463 case elfcpp::R_386_TLS_DESC_CALL
:
1464 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1465 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1466 case elfcpp::R_386_TLS_IE
: // Initial-exec
1467 case elfcpp::R_386_TLS_IE_32
:
1468 case elfcpp::R_386_TLS_GOTIE
:
1469 case elfcpp::R_386_TLS_LE
: // Local-exec
1470 case elfcpp::R_386_TLS_LE_32
:
1471 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1472 address
, view_size
);
1475 case elfcpp::R_386_32PLT
:
1476 case elfcpp::R_386_TLS_GD_32
:
1477 case elfcpp::R_386_TLS_GD_PUSH
:
1478 case elfcpp::R_386_TLS_GD_CALL
:
1479 case elfcpp::R_386_TLS_GD_POP
:
1480 case elfcpp::R_386_TLS_LDM_32
:
1481 case elfcpp::R_386_TLS_LDM_PUSH
:
1482 case elfcpp::R_386_TLS_LDM_CALL
:
1483 case elfcpp::R_386_TLS_LDM_POP
:
1484 case elfcpp::R_386_USED_BY_INTEL_200
:
1486 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1487 _("unsupported reloc %u"),
1495 // Perform a TLS relocation.
1498 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1500 const elfcpp::Rel
<32, false>& rel
,
1501 unsigned int r_type
,
1502 const Sized_symbol
<32>* gsym
,
1503 const Symbol_value
<32>* psymval
,
1504 unsigned char* view
,
1505 elfcpp::Elf_types
<32>::Elf_Addr
,
1508 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1509 if (tls_segment
== NULL
)
1511 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1512 _("TLS reloc but no TLS segment"));
1516 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1518 const bool is_final
= (gsym
== NULL
1519 ? !parameters
->output_is_position_independent()
1520 : gsym
->final_value_is_known());
1521 const tls::Tls_optimization optimized_type
1522 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1525 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1526 if (optimized_type
== tls::TLSOPT_TO_LE
)
1528 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1529 rel
, r_type
, value
, view
,
1533 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1534 _("unsupported reloc %u"),
1538 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1539 case elfcpp::R_386_TLS_DESC_CALL
:
1540 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1541 _("unsupported reloc %u"),
1545 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1546 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1548 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1549 _("both SUN and GNU model "
1550 "TLS relocations"));
1553 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1554 if (optimized_type
== tls::TLSOPT_TO_LE
)
1556 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1557 value
, view
, view_size
);
1560 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1561 _("unsupported reloc %u"),
1565 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1566 // This reloc can appear in debugging sections, in which case we
1567 // won't see the TLS_LDM reloc. The local_dynamic_type field
1569 if (optimized_type
!= tls::TLSOPT_TO_LE
1570 || this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1571 value
= value
- tls_segment
->vaddr();
1572 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1573 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1575 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1576 Relocate_functions
<32, false>::rel32(view
, value
);
1579 case elfcpp::R_386_TLS_IE
: // Initial-exec
1580 case elfcpp::R_386_TLS_GOTIE
:
1581 case elfcpp::R_386_TLS_IE_32
:
1582 if (optimized_type
== tls::TLSOPT_TO_LE
)
1584 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1585 rel
, r_type
, value
, view
,
1589 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1590 _("unsupported reloc %u"),
1594 case elfcpp::R_386_TLS_LE
: // Local-exec
1595 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1596 Relocate_functions
<32, false>::rel32(view
, value
);
1599 case elfcpp::R_386_TLS_LE_32
:
1600 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1601 Relocate_functions
<32, false>::rel32(view
, value
);
1606 // Do a relocation in which we convert a TLS General-Dynamic to a
1610 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1612 Output_segment
* tls_segment
,
1613 const elfcpp::Rel
<32, false>& rel
,
1615 elfcpp::Elf_types
<32>::Elf_Addr value
,
1616 unsigned char* view
,
1619 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1620 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1621 // leal foo(%reg),%eax; call ___tls_get_addr
1622 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1624 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1625 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1627 unsigned char op1
= view
[-1];
1628 unsigned char op2
= view
[-2];
1630 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1631 op2
== 0x8d || op2
== 0x04);
1632 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1638 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1639 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1640 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1641 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1642 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1646 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1647 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1648 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1651 // There is a trailing nop. Use the size byte subl.
1652 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1657 // Use the five byte subl.
1658 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1662 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1663 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
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 Local-Dynamic to a
1674 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1677 const elfcpp::Rel
<32, false>& rel
,
1679 elfcpp::Elf_types
<32>::Elf_Addr
,
1680 unsigned char* view
,
1683 // leal foo(%reg), %eax; call ___tls_get_addr
1684 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1686 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1687 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1689 // FIXME: Does this test really always pass?
1690 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1691 view
[-2] == 0x8d && view
[-1] == 0x83);
1693 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1695 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1697 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1699 this->skip_call_tls_get_addr_
= true;
1702 // Do a relocation in which we convert a TLS Initial-Exec to a
1706 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1708 Output_segment
* tls_segment
,
1709 const elfcpp::Rel
<32, false>& rel
,
1710 unsigned int r_type
,
1711 elfcpp::Elf_types
<32>::Elf_Addr value
,
1712 unsigned char* view
,
1715 // We have to actually change the instructions, which means that we
1716 // need to examine the opcodes to figure out which instruction we
1718 if (r_type
== elfcpp::R_386_TLS_IE
)
1720 // movl %gs:XX,%eax ==> movl $YY,%eax
1721 // movl %gs:XX,%reg ==> movl $YY,%reg
1722 // addl %gs:XX,%reg ==> addl $YY,%reg
1723 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
1724 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1726 unsigned char op1
= view
[-1];
1729 // movl XX,%eax ==> movl $YY,%eax
1734 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1736 unsigned char op2
= view
[-2];
1739 // movl XX,%reg ==> movl $YY,%reg
1740 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1741 (op1
& 0xc7) == 0x05);
1743 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1745 else if (op2
== 0x03)
1747 // addl XX,%reg ==> addl $YY,%reg
1748 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1749 (op1
& 0xc7) == 0x05);
1751 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1754 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1759 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1760 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1761 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1762 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1763 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1765 unsigned char op1
= view
[-1];
1766 unsigned char op2
= view
[-2];
1767 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1768 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1771 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1773 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1775 else if (op2
== 0x2b)
1777 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1779 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1781 else if (op2
== 0x03)
1783 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1785 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1788 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1791 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1792 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1795 Relocate_functions
<32, false>::rel32(view
, value
);
1798 // Relocate section data.
1801 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1802 unsigned int sh_type
,
1803 const unsigned char* prelocs
,
1805 Output_section
* output_section
,
1806 bool needs_special_offset_handling
,
1807 unsigned char* view
,
1808 elfcpp::Elf_types
<32>::Elf_Addr address
,
1811 gold_assert(sh_type
== elfcpp::SHT_REL
);
1813 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1814 Target_i386::Relocate
>(
1820 needs_special_offset_handling
,
1826 // Return the value to use for a dynamic which requires special
1827 // treatment. This is how we support equality comparisons of function
1828 // pointers across shared library boundaries, as described in the
1829 // processor specific ABI supplement.
1832 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1834 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1835 return this->plt_section()->address() + gsym
->plt_offset();
1838 // Return a string used to fill a code section with nops to take up
1839 // the specified length.
1842 Target_i386::do_code_fill(off_t length
)
1846 // Build a jmp instruction to skip over the bytes.
1847 unsigned char jmp
[5];
1849 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1850 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1851 + std::string(length
- 5, '\0'));
1854 // Nop sequences of various lengths.
1855 const char nop1
[1] = { 0x90 }; // nop
1856 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1857 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1858 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1859 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1860 0x00 }; // leal 0(%esi,1),%esi
1861 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1863 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1865 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1866 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1867 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1868 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1870 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1871 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1873 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1874 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1876 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1877 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1878 0x00, 0x00, 0x00, 0x00 };
1879 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1880 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1881 0x27, 0x00, 0x00, 0x00,
1883 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1884 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1885 0xbc, 0x27, 0x00, 0x00,
1887 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1888 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1889 0x90, 0x90, 0x90, 0x90,
1892 const char* nops
[16] = {
1894 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1895 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1898 return std::string(nops
[length
], length
);
1901 // The selector for i386 object files.
1903 class Target_selector_i386
: public Target_selector
1906 Target_selector_i386()
1907 : Target_selector(elfcpp::EM_386
, 32, false)
1911 recognize(int machine
, int osabi
, int abiversion
);
1914 Target_i386
* target_
;
1917 // Recognize an i386 object file when we already know that the machine
1918 // number is EM_386.
1921 Target_selector_i386::recognize(int, int, int)
1923 if (this->target_
== NULL
)
1924 this->target_
= new Target_i386();
1925 return this->target_
;
1928 Target_selector_i386 target_selector_i386
;
1930 } // End anonymous namespace.