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 Output_section
* output_section
,
126 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
127 const elfcpp::Sym
<32, false>& lsym
);
130 global(const General_options
& options
, Symbol_table
* symtab
,
131 Layout
* layout
, Target_i386
* target
,
132 Sized_relobj
<32, false>* object
,
133 unsigned int data_shndx
,
134 Output_section
* output_section
,
135 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
139 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
142 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
146 // The class which implements relocation.
151 : skip_call_tls_get_addr_(false),
152 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
157 if (this->skip_call_tls_get_addr_
)
159 // FIXME: This needs to specify the location somehow.
160 gold_error(_("missing expected TLS relocation"));
164 // Return whether the static relocation needs to be applied.
166 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
167 bool is_absolute_ref
,
168 bool is_function_call
,
171 // Do a relocation. Return false if the caller should not issue
172 // any warnings about this relocation.
174 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
175 const elfcpp::Rel
<32, false>&,
176 unsigned int r_type
, const Sized_symbol
<32>*,
177 const Symbol_value
<32>*,
178 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
182 // Do a TLS relocation.
184 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
185 size_t relnum
, const elfcpp::Rel
<32, false>&,
186 unsigned int r_type
, const Sized_symbol
<32>*,
187 const Symbol_value
<32>*,
188 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
190 // Do a TLS General-Dynamic to Initial-Exec transition.
192 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
193 Output_segment
* tls_segment
,
194 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
195 elfcpp::Elf_types
<32>::Elf_Addr value
,
199 // Do a TLS General-Dynamic to Local-Exec transition.
201 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
202 Output_segment
* tls_segment
,
203 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
204 elfcpp::Elf_types
<32>::Elf_Addr value
,
208 // Do a TLS Local-Dynamic to Local-Exec transition.
210 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
211 Output_segment
* tls_segment
,
212 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
213 elfcpp::Elf_types
<32>::Elf_Addr value
,
217 // Do a TLS Initial-Exec to Local-Exec transition.
219 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
220 Output_segment
* tls_segment
,
221 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
222 elfcpp::Elf_types
<32>::Elf_Addr value
,
226 // We need to keep track of which type of local dynamic relocation
227 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
228 enum Local_dynamic_type
235 // This is set if we should skip the next reloc, which should be a
236 // PLT32 reloc against ___tls_get_addr.
237 bool skip_call_tls_get_addr_
;
238 // The type of local dynamic relocation we have seen in the section
239 // being relocated, if any.
240 Local_dynamic_type local_dynamic_type_
;
243 // Adjust TLS relocation type based on the options and whether this
244 // is a local symbol.
245 static tls::Tls_optimization
246 optimize_tls_reloc(bool is_final
, int r_type
);
248 // Get the GOT section, creating it if necessary.
249 Output_data_got
<32, false>*
250 got_section(Symbol_table
*, Layout
*);
252 // Get the GOT PLT section.
254 got_plt_section() const
256 gold_assert(this->got_plt_
!= NULL
);
257 return this->got_plt_
;
260 // Create a PLT entry for a global symbol.
262 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
264 // Get the PLT section.
265 const Output_data_plt_i386
*
268 gold_assert(this->plt_
!= NULL
);
272 // Get the dynamic reloc section, creating it if necessary.
274 rel_dyn_section(Layout
*);
276 // Return true if the symbol may need a COPY relocation.
277 // References from an executable object to non-function symbols
278 // defined in a dynamic object may need a COPY relocation.
280 may_need_copy_reloc(Symbol
* gsym
)
282 return (!parameters
->output_is_shared()
283 && gsym
->is_from_dynobj()
284 && gsym
->type() != elfcpp::STT_FUNC
);
287 // Copy a relocation against a global symbol.
289 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
290 Sized_relobj
<32, false>*, unsigned int,
291 Output_section
*, Symbol
*, const elfcpp::Rel
<32, false>&);
293 // Information about this specific target which we pass to the
294 // general Target structure.
295 static const Target::Target_info i386_info
;
298 Output_data_got
<32, false>* got_
;
300 Output_data_plt_i386
* plt_
;
301 // The GOT PLT section.
302 Output_data_space
* got_plt_
;
303 // The dynamic reloc section.
304 Reloc_section
* rel_dyn_
;
305 // Relocs saved to avoid a COPY reloc.
306 Copy_relocs
<32, false>* copy_relocs_
;
307 // Space for variables copied with a COPY reloc.
308 Output_data_space
* dynbss_
;
311 const Target::Target_info
Target_i386::i386_info
=
314 false, // is_big_endian
315 elfcpp::EM_386
, // machine_code
316 false, // has_make_symbol
317 false, // has_resolve
318 true, // has_code_fill
319 true, // is_default_stack_executable
320 "/usr/lib/libc.so.1", // dynamic_linker
321 0x08048000, // default_text_segment_address
322 0x1000, // abi_pagesize
323 0x1000 // common_pagesize
326 // Get the GOT section, creating it if necessary.
328 Output_data_got
<32, false>*
329 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
331 if (this->got_
== NULL
)
333 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
335 this->got_
= new Output_data_got
<32, false>();
337 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
338 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
341 // The old GNU linker creates a .got.plt section. We just
342 // create another set of data in the .got section. Note that we
343 // always create a PLT if we create a GOT, although the PLT
345 this->got_plt_
= new Output_data_space(4);
346 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
347 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
350 // The first three entries are reserved.
351 this->got_plt_
->set_current_data_size(3 * 4);
353 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
354 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
356 0, 0, elfcpp::STT_OBJECT
,
358 elfcpp::STV_HIDDEN
, 0,
365 // Get the dynamic reloc section, creating it if necessary.
367 Target_i386::Reloc_section
*
368 Target_i386::rel_dyn_section(Layout
* layout
)
370 if (this->rel_dyn_
== NULL
)
372 gold_assert(layout
!= NULL
);
373 this->rel_dyn_
= new Reloc_section();
374 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
375 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
377 return this->rel_dyn_
;
380 // A class to handle the PLT data.
382 class Output_data_plt_i386
: public Output_section_data
385 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
387 Output_data_plt_i386(Layout
*, Output_data_space
*);
389 // Add an entry to the PLT.
391 add_entry(Symbol
* gsym
);
393 // Return the .rel.plt section data.
396 { return this->rel_
; }
400 do_adjust_output_section(Output_section
* os
);
403 // The size of an entry in the PLT.
404 static const int plt_entry_size
= 16;
406 // The first entry in the PLT for an executable.
407 static unsigned char exec_first_plt_entry
[plt_entry_size
];
409 // The first entry in the PLT for a shared object.
410 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
412 // Other entries in the PLT for an executable.
413 static unsigned char exec_plt_entry
[plt_entry_size
];
415 // Other entries in the PLT for a shared object.
416 static unsigned char dyn_plt_entry
[plt_entry_size
];
418 // Set the final size.
420 set_final_data_size()
421 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
423 // Write out the PLT data.
425 do_write(Output_file
*);
427 // The reloc section.
429 // The .got.plt section.
430 Output_data_space
* got_plt_
;
431 // The number of PLT entries.
435 // Create the PLT section. The ordinary .got section is an argument,
436 // since we need to refer to the start. We also create our own .got
437 // section just for PLT entries.
439 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
440 Output_data_space
* got_plt
)
441 : Output_section_data(4), got_plt_(got_plt
), count_(0)
443 this->rel_
= new Reloc_section();
444 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
445 elfcpp::SHF_ALLOC
, this->rel_
);
449 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
451 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
452 // linker, and so do we.
456 // Add an entry to the PLT.
459 Output_data_plt_i386::add_entry(Symbol
* gsym
)
461 gold_assert(!gsym
->has_plt_offset());
463 // Note that when setting the PLT offset we skip the initial
464 // reserved PLT entry.
465 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
469 off_t got_offset
= this->got_plt_
->current_data_size();
471 // Every PLT entry needs a GOT entry which points back to the PLT
472 // entry (this will be changed by the dynamic linker, normally
473 // lazily when the function is called).
474 this->got_plt_
->set_current_data_size(got_offset
+ 4);
476 // Every PLT entry needs a reloc.
477 gsym
->set_needs_dynsym_entry();
478 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
481 // Note that we don't need to save the symbol. The contents of the
482 // PLT are independent of which symbols are used. The symbols only
483 // appear in the relocations.
486 // The first entry in the PLT for an executable.
488 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
490 0xff, 0x35, // pushl contents of memory address
491 0, 0, 0, 0, // replaced with address of .got + 4
492 0xff, 0x25, // jmp indirect
493 0, 0, 0, 0, // replaced with address of .got + 8
497 // The first entry in the PLT for a shared object.
499 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
501 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
502 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
506 // Subsequent entries in the PLT for an executable.
508 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
510 0xff, 0x25, // jmp indirect
511 0, 0, 0, 0, // replaced with address of symbol in .got
512 0x68, // pushl immediate
513 0, 0, 0, 0, // replaced with offset into relocation table
514 0xe9, // jmp relative
515 0, 0, 0, 0 // replaced with offset to start of .plt
518 // Subsequent entries in the PLT for a shared object.
520 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
522 0xff, 0xa3, // jmp *offset(%ebx)
523 0, 0, 0, 0, // replaced with offset of symbol in .got
524 0x68, // pushl immediate
525 0, 0, 0, 0, // replaced with offset into relocation table
526 0xe9, // jmp relative
527 0, 0, 0, 0 // replaced with offset to start of .plt
530 // Write out the PLT. This uses the hand-coded instructions above,
531 // and adjusts them as needed. This is all specified by the i386 ELF
532 // Processor Supplement.
535 Output_data_plt_i386::do_write(Output_file
* of
)
537 const off_t offset
= this->offset();
538 const off_t oview_size
= this->data_size();
539 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
541 const off_t got_file_offset
= this->got_plt_
->offset();
542 const off_t got_size
= this->got_plt_
->data_size();
543 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
546 unsigned char* pov
= oview
;
548 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
549 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
551 if (parameters
->output_is_shared())
552 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
555 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
556 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
557 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
559 pov
+= plt_entry_size
;
561 unsigned char* got_pov
= got_view
;
563 memset(got_pov
, 0, 12);
566 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
568 unsigned int plt_offset
= plt_entry_size
;
569 unsigned int plt_rel_offset
= 0;
570 unsigned int got_offset
= 12;
571 const unsigned int count
= this->count_
;
572 for (unsigned int i
= 0;
575 pov
+= plt_entry_size
,
577 plt_offset
+= plt_entry_size
,
578 plt_rel_offset
+= rel_size
,
581 // Set and adjust the PLT entry itself.
583 if (parameters
->output_is_shared())
585 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
586 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
590 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
591 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
596 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
597 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
598 - (plt_offset
+ plt_entry_size
));
600 // Set the entry in the GOT.
601 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
604 gold_assert(pov
- oview
== oview_size
);
605 gold_assert(got_pov
- got_view
== got_size
);
607 of
->write_output_view(offset
, oview_size
, oview
);
608 of
->write_output_view(got_file_offset
, got_size
, got_view
);
611 // Create a PLT entry for a global symbol.
614 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
616 if (gsym
->has_plt_offset())
619 if (this->plt_
== NULL
)
621 // Create the GOT sections first.
622 this->got_section(symtab
, layout
);
624 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
625 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
627 | elfcpp::SHF_EXECINSTR
),
631 this->plt_
->add_entry(gsym
);
634 // Handle a relocation against a non-function symbol defined in a
635 // dynamic object. The traditional way to handle this is to generate
636 // a COPY relocation to copy the variable at runtime from the shared
637 // object into the executable's data segment. However, this is
638 // undesirable in general, as if the size of the object changes in the
639 // dynamic object, the executable will no longer work correctly. If
640 // this relocation is in a writable section, then we can create a
641 // dynamic reloc and the dynamic linker will resolve it to the correct
642 // address at runtime. However, we do not want do that if the
643 // relocation is in a read-only section, as it would prevent the
644 // readonly segment from being shared. And if we have to eventually
645 // generate a COPY reloc, then any dynamic relocations will be
646 // useless. So this means that if this is a writable section, we need
647 // to save the relocation until we see whether we have to create a
648 // COPY relocation for this symbol for any other relocation.
651 Target_i386::copy_reloc(const General_options
* options
,
652 Symbol_table
* symtab
,
654 Sized_relobj
<32, false>* object
,
655 unsigned int data_shndx
,
656 Output_section
* output_section
,
658 const elfcpp::Rel
<32, false>& rel
)
660 Sized_symbol
<32>* ssym
;
661 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
664 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
667 // So far we do not need a COPY reloc. Save this relocation.
668 // If it turns out that we never need a COPY reloc for this
669 // symbol, then we will emit the relocation.
670 if (this->copy_relocs_
== NULL
)
671 this->copy_relocs_
= new Copy_relocs
<32, false>();
672 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rel
);
676 // Allocate space for this symbol in the .bss section.
678 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
680 // There is no defined way to determine the required alignment
681 // of the symbol. We pick the alignment based on the size. We
682 // set an arbitrary maximum of 256.
684 for (align
= 1; align
< 512; align
<<= 1)
685 if ((symsize
& align
) != 0)
688 if (this->dynbss_
== NULL
)
690 this->dynbss_
= new Output_data_space(align
);
691 layout
->add_output_section_data(".bss",
694 | elfcpp::SHF_WRITE
),
698 Output_data_space
* dynbss
= this->dynbss_
;
700 if (align
> dynbss
->addralign())
701 dynbss
->set_space_alignment(align
);
703 off_t dynbss_size
= dynbss
->current_data_size();
704 dynbss_size
= align_address(dynbss_size
, align
);
705 off_t offset
= dynbss_size
;
706 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
708 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
710 // Add the COPY reloc.
711 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
712 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
716 // Optimize the TLS relocation type based on what we know about the
717 // symbol. IS_FINAL is true if the final address of this symbol is
718 // known at link time.
720 tls::Tls_optimization
721 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
723 // If we are generating a shared library, then we can't do anything
725 if (parameters
->output_is_shared())
726 return tls::TLSOPT_NONE
;
730 case elfcpp::R_386_TLS_GD
:
731 case elfcpp::R_386_TLS_GOTDESC
:
732 case elfcpp::R_386_TLS_DESC_CALL
:
733 // These are General-Dynamic which permits fully general TLS
734 // access. Since we know that we are generating an executable,
735 // we can convert this to Initial-Exec. If we also know that
736 // this is a local symbol, we can further switch to Local-Exec.
738 return tls::TLSOPT_TO_LE
;
739 return tls::TLSOPT_TO_IE
;
741 case elfcpp::R_386_TLS_LDM
:
742 // This is Local-Dynamic, which refers to a local symbol in the
743 // dynamic TLS block. Since we know that we generating an
744 // executable, we can switch to Local-Exec.
745 return tls::TLSOPT_TO_LE
;
747 case elfcpp::R_386_TLS_LDO_32
:
748 // Another type of Local-Dynamic relocation.
749 return tls::TLSOPT_TO_LE
;
751 case elfcpp::R_386_TLS_IE
:
752 case elfcpp::R_386_TLS_GOTIE
:
753 case elfcpp::R_386_TLS_IE_32
:
754 // These are Initial-Exec relocs which get the thread offset
755 // from the GOT. If we know that we are linking against the
756 // local symbol, we can switch to Local-Exec, which links the
757 // thread offset into the instruction.
759 return tls::TLSOPT_TO_LE
;
760 return tls::TLSOPT_NONE
;
762 case elfcpp::R_386_TLS_LE
:
763 case elfcpp::R_386_TLS_LE_32
:
764 // When we already have Local-Exec, there is nothing further we
766 return tls::TLSOPT_NONE
;
773 // Report an unsupported relocation against a local symbol.
776 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
779 gold_error(_("%s: unsupported reloc %u against local symbol"),
780 object
->name().c_str(), r_type
);
783 // Scan a relocation for a local symbol.
786 Target_i386::Scan::local(const General_options
&,
787 Symbol_table
* symtab
,
790 Sized_relobj
<32, false>* object
,
791 unsigned int data_shndx
,
792 Output_section
* output_section
,
793 const elfcpp::Rel
<32, false>& reloc
,
795 const elfcpp::Sym
<32, false>& lsym
)
799 case elfcpp::R_386_NONE
:
800 case elfcpp::R_386_GNU_VTINHERIT
:
801 case elfcpp::R_386_GNU_VTENTRY
:
804 case elfcpp::R_386_32
:
805 // If building a shared library (or a position-independent
806 // executable), we need to create a dynamic relocation for
807 // this location. The relocation applied at link time will
808 // apply the link-time value, so we flag the location with
809 // an R_386_RELATIVE relocation so the dynamic loader can
810 // relocate it easily.
811 if (parameters
->output_is_position_independent())
813 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
814 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, output_section
,
815 data_shndx
, reloc
.get_r_offset());
819 case elfcpp::R_386_16
:
820 case elfcpp::R_386_8
:
821 // If building a shared library (or a position-independent
822 // executable), we need to create a dynamic relocation for
823 // this location. Because the addend needs to remain in the
824 // data section, we need to be careful not to apply this
825 // relocation statically.
826 if (parameters
->output_is_position_independent())
828 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
829 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
830 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
, data_shndx
,
831 reloc
.get_r_offset());
835 case elfcpp::R_386_PC32
:
836 case elfcpp::R_386_PC16
:
837 case elfcpp::R_386_PC8
:
840 case elfcpp::R_386_PLT32
:
841 // Since we know this is a local symbol, we can handle this as a
845 case elfcpp::R_386_GOTOFF
:
846 case elfcpp::R_386_GOTPC
:
847 // We need a GOT section.
848 target
->got_section(symtab
, layout
);
851 case elfcpp::R_386_GOT32
:
853 // The symbol requires a GOT entry.
854 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
855 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
856 if (got
->add_local(object
, r_sym
))
858 // If we are generating a shared object, we need to add a
859 // dynamic RELATIVE relocation for this symbol's GOT entry.
860 if (parameters
->output_is_position_independent())
862 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
863 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
864 got
, object
->local_got_offset(r_sym
));
870 // These are relocations which should only be seen by the
871 // dynamic linker, and should never be seen here.
872 case elfcpp::R_386_COPY
:
873 case elfcpp::R_386_GLOB_DAT
:
874 case elfcpp::R_386_JUMP_SLOT
:
875 case elfcpp::R_386_RELATIVE
:
876 case elfcpp::R_386_TLS_TPOFF
:
877 case elfcpp::R_386_TLS_DTPMOD32
:
878 case elfcpp::R_386_TLS_DTPOFF32
:
879 case elfcpp::R_386_TLS_TPOFF32
:
880 case elfcpp::R_386_TLS_DESC
:
881 gold_error(_("%s: unexpected reloc %u in object file"),
882 object
->name().c_str(), r_type
);
885 // These are initial TLS relocs, which are expected when
887 case elfcpp::R_386_TLS_GD
: // Global-dynamic
888 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
889 case elfcpp::R_386_TLS_DESC_CALL
:
890 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
891 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
892 case elfcpp::R_386_TLS_IE
: // Initial-exec
893 case elfcpp::R_386_TLS_IE_32
:
894 case elfcpp::R_386_TLS_GOTIE
:
895 case elfcpp::R_386_TLS_LE
: // Local-exec
896 case elfcpp::R_386_TLS_LE_32
:
898 bool output_is_shared
= parameters
->output_is_shared();
899 const tls::Tls_optimization optimized_type
900 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
903 case elfcpp::R_386_TLS_GD
: // Global-dynamic
904 if (optimized_type
== tls::TLSOPT_NONE
)
906 // Create a pair of GOT entries for the module index and
907 // dtv-relative offset.
908 Output_data_got
<32, false>* got
909 = target
->got_section(symtab
, layout
);
910 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
911 got
->add_local_tls_with_rel(object
, r_sym
,
912 lsym
.get_st_shndx(), true,
913 target
->rel_dyn_section(layout
),
914 elfcpp::R_386_TLS_DTPMOD32
);
916 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
917 unsupported_reloc_local(object
, r_type
);
920 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
921 case elfcpp::R_386_TLS_DESC_CALL
:
922 // FIXME: If not relaxing to LE, we need to generate
923 // a GOT entry with an R_386_TLS_DESC reloc.
924 if (optimized_type
!= tls::TLSOPT_TO_LE
)
925 unsupported_reloc_local(object
, r_type
);
928 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
929 if (optimized_type
== tls::TLSOPT_NONE
)
931 // Create a GOT entry for the module index.
932 Output_data_got
<32, false>* got
933 = target
->got_section(symtab
, layout
);
934 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
935 got
->add_local_tls_with_rel(object
, r_sym
,
936 lsym
.get_st_shndx(), false,
937 target
->rel_dyn_section(layout
),
938 elfcpp::R_386_TLS_DTPMOD32
);
940 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
941 unsupported_reloc_local(object
, r_type
);
944 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
947 case elfcpp::R_386_TLS_IE
: // Initial-exec
948 case elfcpp::R_386_TLS_IE_32
:
949 case elfcpp::R_386_TLS_GOTIE
:
950 if (optimized_type
== tls::TLSOPT_NONE
)
952 // For the R_386_TLS_IE relocation, we need to create a
953 // dynamic relocation when building a shared library.
954 if (r_type
== elfcpp::R_386_TLS_IE
955 && parameters
->output_is_shared())
957 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
958 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
959 output_section
, data_shndx
,
960 reloc
.get_r_offset());
962 // Create a GOT entry for the tp-relative offset.
963 Output_data_got
<32, false>* got
964 = target
->got_section(symtab
, layout
);
965 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
966 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
967 ? elfcpp::R_386_TLS_TPOFF32
968 : elfcpp::R_386_TLS_TPOFF
);
969 got
->add_local_with_rel(object
, r_sym
,
970 target
->rel_dyn_section(layout
),
973 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
974 unsupported_reloc_local(object
, r_type
);
977 case elfcpp::R_386_TLS_LE
: // Local-exec
978 case elfcpp::R_386_TLS_LE_32
:
979 if (output_is_shared
)
981 // We need to create a dynamic relocation.
982 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
983 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
984 ? elfcpp::R_386_TLS_TPOFF32
985 : elfcpp::R_386_TLS_TPOFF
);
986 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
987 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
988 data_shndx
, reloc
.get_r_offset());
998 case elfcpp::R_386_32PLT
:
999 case elfcpp::R_386_TLS_GD_32
:
1000 case elfcpp::R_386_TLS_GD_PUSH
:
1001 case elfcpp::R_386_TLS_GD_CALL
:
1002 case elfcpp::R_386_TLS_GD_POP
:
1003 case elfcpp::R_386_TLS_LDM_32
:
1004 case elfcpp::R_386_TLS_LDM_PUSH
:
1005 case elfcpp::R_386_TLS_LDM_CALL
:
1006 case elfcpp::R_386_TLS_LDM_POP
:
1007 case elfcpp::R_386_USED_BY_INTEL_200
:
1009 unsupported_reloc_local(object
, r_type
);
1014 // Report an unsupported relocation against a global symbol.
1017 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1018 unsigned int r_type
,
1021 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1022 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1025 // Scan a relocation for a global symbol.
1028 Target_i386::Scan::global(const General_options
& options
,
1029 Symbol_table
* symtab
,
1031 Target_i386
* target
,
1032 Sized_relobj
<32, false>* object
,
1033 unsigned int data_shndx
,
1034 Output_section
* output_section
,
1035 const elfcpp::Rel
<32, false>& reloc
,
1036 unsigned int r_type
,
1041 case elfcpp::R_386_NONE
:
1042 case elfcpp::R_386_GNU_VTINHERIT
:
1043 case elfcpp::R_386_GNU_VTENTRY
:
1046 case elfcpp::R_386_32
:
1047 case elfcpp::R_386_16
:
1048 case elfcpp::R_386_8
:
1050 // Make a PLT entry if necessary.
1051 if (gsym
->needs_plt_entry())
1053 target
->make_plt_entry(symtab
, layout
, gsym
);
1054 // Since this is not a PC-relative relocation, we may be
1055 // taking the address of a function. In that case we need to
1056 // set the entry in the dynamic symbol table to the address of
1058 if (gsym
->is_from_dynobj())
1059 gsym
->set_needs_dynsym_value();
1061 // Make a dynamic relocation if necessary.
1062 if (gsym
->needs_dynamic_reloc(true, false))
1064 if (target
->may_need_copy_reloc(gsym
))
1066 target
->copy_reloc(&options
, symtab
, layout
, object
,
1067 data_shndx
, output_section
, gsym
, reloc
);
1069 else if (r_type
== elfcpp::R_386_32
1070 && gsym
->can_use_relative_reloc(false))
1072 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1073 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
1074 output_section
, data_shndx
,
1075 reloc
.get_r_offset());
1079 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1080 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1081 data_shndx
, reloc
.get_r_offset());
1087 case elfcpp::R_386_PC32
:
1088 case elfcpp::R_386_PC16
:
1089 case elfcpp::R_386_PC8
:
1091 // Make a PLT entry if necessary.
1092 if (gsym
->needs_plt_entry())
1094 // These relocations are used for function calls only in
1095 // non-PIC code. For a 32-bit relocation in a shared library,
1096 // we'll need a text relocation anyway, so we can skip the
1097 // PLT entry and let the dynamic linker bind the call directly
1098 // to the target. For smaller relocations, we should use a
1099 // PLT entry to ensure that the call can reach.
1100 if (!parameters
->output_is_shared()
1101 || r_type
!= elfcpp::R_386_PC32
)
1102 target
->make_plt_entry(symtab
, layout
, gsym
);
1104 // Make a dynamic relocation if necessary.
1105 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1106 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1108 if (target
->may_need_copy_reloc(gsym
))
1110 target
->copy_reloc(&options
, symtab
, layout
, object
,
1111 data_shndx
, output_section
, gsym
, reloc
);
1115 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1116 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1117 data_shndx
, reloc
.get_r_offset());
1123 case elfcpp::R_386_GOT32
:
1125 // The symbol requires a GOT entry.
1126 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1127 if (gsym
->final_value_is_known())
1128 got
->add_global(gsym
);
1131 // If this symbol is not fully resolved, we need to add a
1132 // GOT entry with a dynamic relocation.
1133 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1134 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1135 got
->add_global_with_rel(gsym
, rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1138 if (got
->add_global(gsym
))
1140 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
1141 got
, gsym
->got_offset());
1142 // Make sure we write the link-time value to the GOT.
1143 gsym
->set_needs_value_in_got();
1150 case elfcpp::R_386_PLT32
:
1151 // If the symbol is fully resolved, this is just a PC32 reloc.
1152 // Otherwise we need a PLT entry.
1153 if (gsym
->final_value_is_known())
1155 // If building a shared library, we can also skip the PLT entry
1156 // if the symbol is defined in the output file and is protected
1158 if (gsym
->is_defined()
1159 && !gsym
->is_from_dynobj()
1160 && !gsym
->is_preemptible())
1162 target
->make_plt_entry(symtab
, layout
, gsym
);
1165 case elfcpp::R_386_GOTOFF
:
1166 case elfcpp::R_386_GOTPC
:
1167 // We need a GOT section.
1168 target
->got_section(symtab
, layout
);
1171 // These are relocations which should only be seen by the
1172 // dynamic linker, and should never be seen here.
1173 case elfcpp::R_386_COPY
:
1174 case elfcpp::R_386_GLOB_DAT
:
1175 case elfcpp::R_386_JUMP_SLOT
:
1176 case elfcpp::R_386_RELATIVE
:
1177 case elfcpp::R_386_TLS_TPOFF
:
1178 case elfcpp::R_386_TLS_DTPMOD32
:
1179 case elfcpp::R_386_TLS_DTPOFF32
:
1180 case elfcpp::R_386_TLS_TPOFF32
:
1181 case elfcpp::R_386_TLS_DESC
:
1182 gold_error(_("%s: unexpected reloc %u in object file"),
1183 object
->name().c_str(), r_type
);
1186 // These are initial tls relocs, which are expected when
1188 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1189 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1190 case elfcpp::R_386_TLS_DESC_CALL
:
1191 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1192 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1193 case elfcpp::R_386_TLS_IE
: // Initial-exec
1194 case elfcpp::R_386_TLS_IE_32
:
1195 case elfcpp::R_386_TLS_GOTIE
:
1196 case elfcpp::R_386_TLS_LE
: // Local-exec
1197 case elfcpp::R_386_TLS_LE_32
:
1199 const bool is_final
= gsym
->final_value_is_known();
1200 const tls::Tls_optimization optimized_type
1201 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1204 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1205 if (optimized_type
== tls::TLSOPT_NONE
)
1207 // Create a pair of GOT entries for the module index and
1208 // dtv-relative offset.
1209 Output_data_got
<32, false>* got
1210 = target
->got_section(symtab
, layout
);
1211 got
->add_global_tls_with_rel(gsym
,
1212 target
->rel_dyn_section(layout
),
1213 elfcpp::R_386_TLS_DTPMOD32
,
1214 elfcpp::R_386_TLS_DTPOFF32
);
1216 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1218 // Create a GOT entry for the tp-relative offset.
1219 Output_data_got
<32, false>* got
1220 = target
->got_section(symtab
, layout
);
1221 got
->add_global_with_rel(gsym
, target
->rel_dyn_section(layout
),
1222 elfcpp::R_386_TLS_TPOFF32
);
1224 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1225 unsupported_reloc_global(object
, r_type
, gsym
);
1228 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1229 case elfcpp::R_386_TLS_DESC_CALL
:
1230 // FIXME: If not relaxing to LE, we need to generate
1231 // a GOT entry with an R_386_TLS_DESC reloc.
1232 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1233 unsupported_reloc_global(object
, r_type
, gsym
);
1234 unsupported_reloc_global(object
, r_type
, gsym
);
1237 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1238 // FIXME: If not relaxing to LE, we need to generate a
1240 if (optimized_type
== tls::TLSOPT_NONE
)
1242 // Create a GOT entry for the module index.
1243 Output_data_got
<32, false>* got
1244 = target
->got_section(symtab
, layout
);
1245 got
->add_global_tls_with_rel(gsym
,
1246 target
->rel_dyn_section(layout
),
1247 elfcpp::R_386_TLS_DTPMOD32
);
1249 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1250 unsupported_reloc_global(object
, r_type
, gsym
);
1253 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1256 case elfcpp::R_386_TLS_IE
: // Initial-exec
1257 case elfcpp::R_386_TLS_IE_32
:
1258 case elfcpp::R_386_TLS_GOTIE
:
1259 if (optimized_type
== tls::TLSOPT_NONE
)
1261 // For the R_386_TLS_IE relocation, we need to create a
1262 // dynamic relocation when building a shared library.
1263 if (r_type
== elfcpp::R_386_TLS_IE
1264 && parameters
->output_is_shared())
1266 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1267 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
1268 output_section
, data_shndx
,
1269 reloc
.get_r_offset());
1271 // Create a GOT entry for the tp-relative offset.
1272 Output_data_got
<32, false>* got
1273 = target
->got_section(symtab
, layout
);
1274 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1275 ? elfcpp::R_386_TLS_TPOFF32
1276 : elfcpp::R_386_TLS_TPOFF
);
1277 got
->add_global_with_rel(gsym
,
1278 target
->rel_dyn_section(layout
),
1281 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1282 unsupported_reloc_global(object
, r_type
, gsym
);
1285 case elfcpp::R_386_TLS_LE
: // Local-exec
1286 case elfcpp::R_386_TLS_LE_32
:
1287 if (parameters
->output_is_shared())
1289 // We need to create a dynamic relocation.
1290 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1291 ? elfcpp::R_386_TLS_TPOFF32
1292 : elfcpp::R_386_TLS_TPOFF
);
1293 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1294 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1295 data_shndx
, reloc
.get_r_offset());
1305 case elfcpp::R_386_32PLT
:
1306 case elfcpp::R_386_TLS_GD_32
:
1307 case elfcpp::R_386_TLS_GD_PUSH
:
1308 case elfcpp::R_386_TLS_GD_CALL
:
1309 case elfcpp::R_386_TLS_GD_POP
:
1310 case elfcpp::R_386_TLS_LDM_32
:
1311 case elfcpp::R_386_TLS_LDM_PUSH
:
1312 case elfcpp::R_386_TLS_LDM_CALL
:
1313 case elfcpp::R_386_TLS_LDM_POP
:
1314 case elfcpp::R_386_USED_BY_INTEL_200
:
1316 unsupported_reloc_global(object
, r_type
, gsym
);
1321 // Scan relocations for a section.
1324 Target_i386::scan_relocs(const General_options
& options
,
1325 Symbol_table
* symtab
,
1327 Sized_relobj
<32, false>* object
,
1328 unsigned int data_shndx
,
1329 unsigned int sh_type
,
1330 const unsigned char* prelocs
,
1332 Output_section
* output_section
,
1333 bool needs_special_offset_handling
,
1334 size_t local_symbol_count
,
1335 const unsigned char* plocal_symbols
)
1337 if (sh_type
== elfcpp::SHT_RELA
)
1339 gold_error(_("%s: unsupported RELA reloc section"),
1340 object
->name().c_str());
1344 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1355 needs_special_offset_handling
,
1360 // Finalize the sections.
1363 Target_i386::do_finalize_sections(Layout
* layout
)
1365 // Fill in some more dynamic tags.
1366 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1369 if (this->got_plt_
!= NULL
)
1370 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1372 if (this->plt_
!= NULL
)
1374 const Output_data
* od
= this->plt_
->rel_plt();
1375 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1376 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1377 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1380 if (this->rel_dyn_
!= NULL
)
1382 const Output_data
* od
= this->rel_dyn_
;
1383 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1384 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1385 odyn
->add_constant(elfcpp::DT_RELENT
,
1386 elfcpp::Elf_sizes
<32>::rel_size
);
1389 if (!parameters
->output_is_shared())
1391 // The value of the DT_DEBUG tag is filled in by the dynamic
1392 // linker at run time, and used by the debugger.
1393 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1397 // Emit any relocs we saved in an attempt to avoid generating COPY
1399 if (this->copy_relocs_
== NULL
)
1401 if (this->copy_relocs_
->any_to_emit())
1403 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1404 this->copy_relocs_
->emit(rel_dyn
);
1406 delete this->copy_relocs_
;
1407 this->copy_relocs_
= NULL
;
1410 // Return whether a direct absolute static relocation needs to be applied.
1411 // In cases where Scan::local() or Scan::global() has created
1412 // a dynamic relocation other than R_386_RELATIVE, the addend
1413 // of the relocation is carried in the data, and we must not
1414 // apply the static relocation.
1417 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1418 bool is_absolute_ref
,
1419 bool is_function_call
,
1422 // For local symbols, we will have created a non-RELATIVE dynamic
1423 // relocation only if (a) the output is position independent,
1424 // (b) the relocation is absolute (not pc- or segment-relative), and
1425 // (c) the relocation is not 32 bits wide.
1427 return !(parameters
->output_is_position_independent()
1431 // For global symbols, we use the same helper routines used in the scan pass.
1432 return !(gsym
->needs_dynamic_reloc(is_absolute_ref
, is_function_call
)
1433 && !gsym
->can_use_relative_reloc(is_function_call
));
1436 // Perform a relocation.
1439 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1440 Target_i386
* target
,
1442 const elfcpp::Rel
<32, false>& rel
,
1443 unsigned int r_type
,
1444 const Sized_symbol
<32>* gsym
,
1445 const Symbol_value
<32>* psymval
,
1446 unsigned char* view
,
1447 elfcpp::Elf_types
<32>::Elf_Addr address
,
1450 if (this->skip_call_tls_get_addr_
)
1452 if (r_type
!= elfcpp::R_386_PLT32
1454 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1455 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1456 _("missing expected TLS relocation"));
1459 this->skip_call_tls_get_addr_
= false;
1464 // Pick the value to use for symbols defined in shared objects.
1465 Symbol_value
<32> symval
;
1467 && (gsym
->is_from_dynobj()
1468 || (parameters
->output_is_shared()
1469 && gsym
->is_preemptible()))
1470 && gsym
->has_plt_offset())
1472 symval
.set_output_value(target
->plt_section()->address()
1473 + gsym
->plt_offset());
1477 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1479 // Get the GOT offset if needed.
1480 // The GOT pointer points to the end of the GOT section.
1481 // We need to subtract the size of the GOT section to get
1482 // the actual offset to use in the relocation.
1483 bool have_got_offset
= false;
1484 unsigned int got_offset
= 0;
1487 case elfcpp::R_386_GOT32
:
1490 gold_assert(gsym
->has_got_offset());
1491 got_offset
= gsym
->got_offset() - target
->got_size();
1495 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1496 gold_assert(object
->local_has_got_offset(r_sym
));
1497 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1499 have_got_offset
= true;
1508 case elfcpp::R_386_NONE
:
1509 case elfcpp::R_386_GNU_VTINHERIT
:
1510 case elfcpp::R_386_GNU_VTENTRY
:
1513 case elfcpp::R_386_32
:
1514 if (should_apply_static_reloc(gsym
, true, false, true))
1515 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1518 case elfcpp::R_386_PC32
:
1520 bool is_function_call
= (gsym
!= NULL
1521 && gsym
->type() == elfcpp::STT_FUNC
);
1522 if (should_apply_static_reloc(gsym
, false, is_function_call
, true))
1523 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1527 case elfcpp::R_386_16
:
1528 if (should_apply_static_reloc(gsym
, true, false, false))
1529 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1532 case elfcpp::R_386_PC16
:
1534 bool is_function_call
= (gsym
!= NULL
1535 && gsym
->type() == elfcpp::STT_FUNC
);
1536 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1537 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1541 case elfcpp::R_386_8
:
1542 if (should_apply_static_reloc(gsym
, true, false, false))
1543 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1546 case elfcpp::R_386_PC8
:
1548 bool is_function_call
= (gsym
!= NULL
1549 && gsym
->type() == elfcpp::STT_FUNC
);
1550 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1551 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1555 case elfcpp::R_386_PLT32
:
1556 gold_assert(gsym
== NULL
1557 || gsym
->has_plt_offset()
1558 || gsym
->final_value_is_known());
1559 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1562 case elfcpp::R_386_GOT32
:
1563 gold_assert(have_got_offset
);
1564 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1567 case elfcpp::R_386_GOTOFF
:
1569 elfcpp::Elf_types
<32>::Elf_Addr value
;
1570 value
= (psymval
->value(object
, 0)
1571 - target
->got_plt_section()->address());
1572 Relocate_functions
<32, false>::rel32(view
, value
);
1576 case elfcpp::R_386_GOTPC
:
1578 elfcpp::Elf_types
<32>::Elf_Addr value
;
1579 value
= target
->got_plt_section()->address();
1580 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1584 case elfcpp::R_386_COPY
:
1585 case elfcpp::R_386_GLOB_DAT
:
1586 case elfcpp::R_386_JUMP_SLOT
:
1587 case elfcpp::R_386_RELATIVE
:
1588 // These are outstanding tls relocs, which are unexpected when
1590 case elfcpp::R_386_TLS_TPOFF
:
1591 case elfcpp::R_386_TLS_DTPMOD32
:
1592 case elfcpp::R_386_TLS_DTPOFF32
:
1593 case elfcpp::R_386_TLS_TPOFF32
:
1594 case elfcpp::R_386_TLS_DESC
:
1595 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1596 _("unexpected reloc %u in object file"),
1600 // These are initial tls relocs, which are expected when
1602 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1603 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1604 case elfcpp::R_386_TLS_DESC_CALL
:
1605 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1606 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1607 case elfcpp::R_386_TLS_IE
: // Initial-exec
1608 case elfcpp::R_386_TLS_IE_32
:
1609 case elfcpp::R_386_TLS_GOTIE
:
1610 case elfcpp::R_386_TLS_LE
: // Local-exec
1611 case elfcpp::R_386_TLS_LE_32
:
1612 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1613 view
, address
, view_size
);
1616 case elfcpp::R_386_32PLT
:
1617 case elfcpp::R_386_TLS_GD_32
:
1618 case elfcpp::R_386_TLS_GD_PUSH
:
1619 case elfcpp::R_386_TLS_GD_CALL
:
1620 case elfcpp::R_386_TLS_GD_POP
:
1621 case elfcpp::R_386_TLS_LDM_32
:
1622 case elfcpp::R_386_TLS_LDM_PUSH
:
1623 case elfcpp::R_386_TLS_LDM_CALL
:
1624 case elfcpp::R_386_TLS_LDM_POP
:
1625 case elfcpp::R_386_USED_BY_INTEL_200
:
1627 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1628 _("unsupported reloc %u"),
1636 // Perform a TLS relocation.
1639 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1640 Target_i386
* target
,
1642 const elfcpp::Rel
<32, false>& rel
,
1643 unsigned int r_type
,
1644 const Sized_symbol
<32>* gsym
,
1645 const Symbol_value
<32>* psymval
,
1646 unsigned char* view
,
1647 elfcpp::Elf_types
<32>::Elf_Addr
,
1650 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1652 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1654 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1656 const bool is_final
= (gsym
== NULL
1657 ? !parameters
->output_is_position_independent()
1658 : gsym
->final_value_is_known());
1659 const tls::Tls_optimization optimized_type
1660 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1663 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1664 if (optimized_type
== tls::TLSOPT_TO_LE
)
1666 gold_assert(tls_segment
!= NULL
);
1667 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1668 rel
, r_type
, value
, view
,
1674 unsigned int got_offset
;
1677 gold_assert(gsym
->has_tls_got_offset(true));
1678 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1682 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1683 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1684 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1685 - target
->got_size());
1687 if (optimized_type
== tls::TLSOPT_TO_IE
)
1689 gold_assert(tls_segment
!= NULL
);
1690 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1691 got_offset
, view
, view_size
);
1694 else if (optimized_type
== tls::TLSOPT_NONE
)
1696 // Relocate the field with the offset of the pair of GOT
1698 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1702 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1703 _("unsupported reloc %u"),
1707 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1708 case elfcpp::R_386_TLS_DESC_CALL
:
1709 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1710 _("unsupported reloc %u"),
1714 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1715 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1717 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1718 _("both SUN and GNU model "
1719 "TLS relocations"));
1722 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1723 if (optimized_type
== tls::TLSOPT_TO_LE
)
1725 gold_assert(tls_segment
!= NULL
);
1726 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1727 value
, view
, view_size
);
1730 else if (optimized_type
== tls::TLSOPT_NONE
)
1732 // Relocate the field with the offset of the GOT entry for
1733 // the module index.
1734 unsigned int got_offset
;
1737 gold_assert(gsym
->has_tls_got_offset(false));
1738 got_offset
= gsym
->tls_got_offset(false) - target
->got_size();
1742 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1743 gold_assert(object
->local_has_tls_got_offset(r_sym
, false));
1744 got_offset
= (object
->local_tls_got_offset(r_sym
, false)
1745 - target
->got_size());
1747 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1750 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1751 _("unsupported reloc %u"),
1755 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1756 // This reloc can appear in debugging sections, in which case we
1757 // won't see the TLS_LDM reloc. The local_dynamic_type field
1759 gold_assert(tls_segment
!= NULL
);
1760 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1761 value
-= tls_segment
->memsz();
1762 else if (optimized_type
== tls::TLSOPT_TO_LE
1763 && this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
1764 value
= tls_segment
->memsz() - value
;
1765 Relocate_functions
<32, false>::rel32(view
, value
);
1768 case elfcpp::R_386_TLS_IE
: // Initial-exec
1769 case elfcpp::R_386_TLS_GOTIE
:
1770 case elfcpp::R_386_TLS_IE_32
:
1771 if (optimized_type
== tls::TLSOPT_TO_LE
)
1773 gold_assert(tls_segment
!= NULL
);
1774 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1775 rel
, r_type
, value
, view
,
1779 else if (optimized_type
== tls::TLSOPT_NONE
)
1781 // Relocate the field with the offset of the GOT entry for
1782 // the tp-relative offset of the symbol.
1783 unsigned int got_offset
;
1786 gold_assert(gsym
->has_got_offset());
1787 got_offset
= gsym
->got_offset();
1791 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1792 gold_assert(object
->local_has_got_offset(r_sym
));
1793 got_offset
= object
->local_got_offset(r_sym
);
1795 // For the R_386_TLS_IE relocation, we need to apply the
1796 // absolute address of the GOT entry.
1797 if (r_type
== elfcpp::R_386_TLS_IE
)
1798 got_offset
+= target
->got_plt_section()->address();
1799 // All GOT offsets are relative to the end of the GOT.
1800 got_offset
-= target
->got_size();
1801 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1804 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1805 _("unsupported reloc %u"),
1809 case elfcpp::R_386_TLS_LE
: // Local-exec
1810 // If we're creating a shared library, a dynamic relocation will
1811 // have been created for this location, so do not apply it now.
1812 if (!parameters
->output_is_shared())
1814 gold_assert(tls_segment
!= NULL
);
1815 value
-= tls_segment
->memsz();
1816 Relocate_functions
<32, false>::rel32(view
, value
);
1820 case elfcpp::R_386_TLS_LE_32
:
1821 // If we're creating a shared library, a dynamic relocation will
1822 // have been created for this location, so do not apply it now.
1823 if (!parameters
->output_is_shared())
1825 gold_assert(tls_segment
!= NULL
);
1826 value
= tls_segment
->memsz() - value
;
1827 Relocate_functions
<32, false>::rel32(view
, value
);
1833 // Do a relocation in which we convert a TLS General-Dynamic to a
1837 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1839 Output_segment
* tls_segment
,
1840 const elfcpp::Rel
<32, false>& rel
,
1842 elfcpp::Elf_types
<32>::Elf_Addr value
,
1843 unsigned char* view
,
1846 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1847 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1848 // leal foo(%reg),%eax; call ___tls_get_addr
1849 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1851 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1852 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1854 unsigned char op1
= view
[-1];
1855 unsigned char op2
= view
[-2];
1857 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1858 op2
== 0x8d || op2
== 0x04);
1859 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1865 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1866 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1867 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1868 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1869 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1873 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1874 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1875 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1878 // There is a trailing nop. Use the size byte subl.
1879 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1884 // Use the five byte subl.
1885 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1889 value
= tls_segment
->memsz() - value
;
1890 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1892 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1894 this->skip_call_tls_get_addr_
= true;
1897 // Do a relocation in which we convert a TLS General-Dynamic to an
1901 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
1903 Output_segment
* tls_segment
,
1904 const elfcpp::Rel
<32, false>& rel
,
1906 elfcpp::Elf_types
<32>::Elf_Addr value
,
1907 unsigned char* view
,
1910 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
1911 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
1913 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1914 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1916 unsigned char op1
= view
[-1];
1917 unsigned char op2
= view
[-2];
1919 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1920 op2
== 0x8d || op2
== 0x04);
1921 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1925 // FIXME: For now, support only one form.
1926 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1927 op1
== 0x8d && op2
== 0x04);
1931 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1932 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1933 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1934 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1935 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
1939 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1940 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1941 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1944 // FIXME: This is not the right instruction sequence.
1945 // There is a trailing nop. Use the size byte subl.
1946 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1951 // FIXME: This is not the right instruction sequence.
1952 // Use the five byte subl.
1953 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1957 value
= tls_segment
->memsz() - value
;
1958 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1960 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1962 this->skip_call_tls_get_addr_
= true;
1965 // Do a relocation in which we convert a TLS Local-Dynamic to a
1969 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1972 const elfcpp::Rel
<32, false>& rel
,
1974 elfcpp::Elf_types
<32>::Elf_Addr
,
1975 unsigned char* view
,
1978 // leal foo(%reg), %eax; call ___tls_get_addr
1979 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1981 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1982 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1984 // FIXME: Does this test really always pass?
1985 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1986 view
[-2] == 0x8d && view
[-1] == 0x83);
1988 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1990 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1992 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1994 this->skip_call_tls_get_addr_
= true;
1997 // Do a relocation in which we convert a TLS Initial-Exec to a
2001 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2003 Output_segment
* tls_segment
,
2004 const elfcpp::Rel
<32, false>& rel
,
2005 unsigned int r_type
,
2006 elfcpp::Elf_types
<32>::Elf_Addr value
,
2007 unsigned char* view
,
2010 // We have to actually change the instructions, which means that we
2011 // need to examine the opcodes to figure out which instruction we
2013 if (r_type
== elfcpp::R_386_TLS_IE
)
2015 // movl %gs:XX,%eax ==> movl $YY,%eax
2016 // movl %gs:XX,%reg ==> movl $YY,%reg
2017 // addl %gs:XX,%reg ==> addl $YY,%reg
2018 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2019 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2021 unsigned char op1
= view
[-1];
2024 // movl XX,%eax ==> movl $YY,%eax
2029 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2031 unsigned char op2
= view
[-2];
2034 // movl XX,%reg ==> movl $YY,%reg
2035 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2036 (op1
& 0xc7) == 0x05);
2038 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2040 else if (op2
== 0x03)
2042 // addl XX,%reg ==> addl $YY,%reg
2043 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2044 (op1
& 0xc7) == 0x05);
2046 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2049 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2054 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2055 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2056 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2057 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2058 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2060 unsigned char op1
= view
[-1];
2061 unsigned char op2
= view
[-2];
2062 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2063 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2066 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2068 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2070 else if (op2
== 0x2b)
2072 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2074 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2076 else if (op2
== 0x03)
2078 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2080 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2083 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2086 value
= tls_segment
->memsz() - value
;
2087 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2090 Relocate_functions
<32, false>::rel32(view
, value
);
2093 // Relocate section data.
2096 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2097 unsigned int sh_type
,
2098 const unsigned char* prelocs
,
2100 Output_section
* output_section
,
2101 bool needs_special_offset_handling
,
2102 unsigned char* view
,
2103 elfcpp::Elf_types
<32>::Elf_Addr address
,
2106 gold_assert(sh_type
== elfcpp::SHT_REL
);
2108 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2109 Target_i386::Relocate
>(
2115 needs_special_offset_handling
,
2121 // Return the value to use for a dynamic which requires special
2122 // treatment. This is how we support equality comparisons of function
2123 // pointers across shared library boundaries, as described in the
2124 // processor specific ABI supplement.
2127 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2129 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2130 return this->plt_section()->address() + gsym
->plt_offset();
2133 // Return a string used to fill a code section with nops to take up
2134 // the specified length.
2137 Target_i386::do_code_fill(off_t length
)
2141 // Build a jmp instruction to skip over the bytes.
2142 unsigned char jmp
[5];
2144 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2145 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2146 + std::string(length
- 5, '\0'));
2149 // Nop sequences of various lengths.
2150 const char nop1
[1] = { 0x90 }; // nop
2151 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2152 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2153 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2154 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2155 0x00 }; // leal 0(%esi,1),%esi
2156 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2158 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2160 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2161 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2162 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2163 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2165 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2166 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2168 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2169 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2171 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2172 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2173 0x00, 0x00, 0x00, 0x00 };
2174 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2175 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2176 0x27, 0x00, 0x00, 0x00,
2178 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2179 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2180 0xbc, 0x27, 0x00, 0x00,
2182 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2183 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2184 0x90, 0x90, 0x90, 0x90,
2187 const char* nops
[16] = {
2189 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2190 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2193 return std::string(nops
[length
], length
);
2196 // The selector for i386 object files.
2198 class Target_selector_i386
: public Target_selector
2201 Target_selector_i386()
2202 : Target_selector(elfcpp::EM_386
, 32, false)
2206 recognize(int machine
, int osabi
, int abiversion
);
2209 Target_i386
* target_
;
2212 // Recognize an i386 object file when we already know that the machine
2213 // number is EM_386.
2216 Target_selector_i386::recognize(int, int, int)
2218 if (this->target_
== NULL
)
2219 this->target_
= new Target_i386();
2220 return this->target_
;
2223 Target_selector_i386 target_selector_i386
;
2225 } // End anonymous namespace.