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
), got_mod_index_offset_(-1U)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options
& options
,
68 Sized_relobj
<32, false>* object
,
69 unsigned int data_shndx
,
71 const unsigned char* prelocs
,
73 Output_section
* output_section
,
74 bool needs_special_offset_handling
,
75 size_t local_symbol_count
,
76 const unsigned char* plocal_symbols
);
78 // Finalize the sections.
80 do_finalize_sections(Layout
*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol
*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info
<32, false>*,
91 const unsigned char* prelocs
,
93 Output_section
* output_section
,
94 bool needs_special_offset_handling
,
96 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
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 // Create a GOT entry for the TLS module index.
266 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
267 Sized_relobj
<32, false>* object
);
269 // Get the PLT section.
270 const Output_data_plt_i386
*
273 gold_assert(this->plt_
!= NULL
);
277 // Get the dynamic reloc section, creating it if necessary.
279 rel_dyn_section(Layout
*);
281 // Return true if the symbol may need a COPY relocation.
282 // References from an executable object to non-function symbols
283 // defined in a dynamic object may need a COPY relocation.
285 may_need_copy_reloc(Symbol
* gsym
)
287 return (!parameters
->output_is_shared()
288 && gsym
->is_from_dynobj()
289 && gsym
->type() != elfcpp::STT_FUNC
);
292 // Copy a relocation against a global symbol.
294 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
295 Sized_relobj
<32, false>*, unsigned int,
296 Output_section
*, Symbol
*, const elfcpp::Rel
<32, false>&);
298 // Information about this specific target which we pass to the
299 // general Target structure.
300 static const Target::Target_info i386_info
;
303 Output_data_got
<32, false>* got_
;
305 Output_data_plt_i386
* plt_
;
306 // The GOT PLT section.
307 Output_data_space
* got_plt_
;
308 // The dynamic reloc section.
309 Reloc_section
* rel_dyn_
;
310 // Relocs saved to avoid a COPY reloc.
311 Copy_relocs
<32, false>* copy_relocs_
;
312 // Space for variables copied with a COPY reloc.
313 Output_data_space
* dynbss_
;
314 // Offset of the GOT entry for the TLS module index;
315 unsigned int got_mod_index_offset_
;
318 const Target::Target_info
Target_i386::i386_info
=
321 false, // is_big_endian
322 elfcpp::EM_386
, // machine_code
323 false, // has_make_symbol
324 false, // has_resolve
325 true, // has_code_fill
326 true, // is_default_stack_executable
327 "/usr/lib/libc.so.1", // dynamic_linker
328 0x08048000, // default_text_segment_address
329 0x1000, // abi_pagesize
330 0x1000 // common_pagesize
333 // Get the GOT section, creating it if necessary.
335 Output_data_got
<32, false>*
336 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
338 if (this->got_
== NULL
)
340 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
342 this->got_
= new Output_data_got
<32, false>();
344 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
345 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
348 // The old GNU linker creates a .got.plt section. We just
349 // create another set of data in the .got section. Note that we
350 // always create a PLT if we create a GOT, although the PLT
352 this->got_plt_
= new Output_data_space(4);
353 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
354 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
357 // The first three entries are reserved.
358 this->got_plt_
->set_current_data_size(3 * 4);
360 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
361 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
363 0, 0, elfcpp::STT_OBJECT
,
365 elfcpp::STV_HIDDEN
, 0,
372 // Get the dynamic reloc section, creating it if necessary.
374 Target_i386::Reloc_section
*
375 Target_i386::rel_dyn_section(Layout
* layout
)
377 if (this->rel_dyn_
== NULL
)
379 gold_assert(layout
!= NULL
);
380 this->rel_dyn_
= new Reloc_section();
381 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
382 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
384 return this->rel_dyn_
;
387 // A class to handle the PLT data.
389 class Output_data_plt_i386
: public Output_section_data
392 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
394 Output_data_plt_i386(Layout
*, Output_data_space
*);
396 // Add an entry to the PLT.
398 add_entry(Symbol
* gsym
);
400 // Return the .rel.plt section data.
403 { return this->rel_
; }
407 do_adjust_output_section(Output_section
* os
);
410 // The size of an entry in the PLT.
411 static const int plt_entry_size
= 16;
413 // The first entry in the PLT for an executable.
414 static unsigned char exec_first_plt_entry
[plt_entry_size
];
416 // The first entry in the PLT for a shared object.
417 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
419 // Other entries in the PLT for an executable.
420 static unsigned char exec_plt_entry
[plt_entry_size
];
422 // Other entries in the PLT for a shared object.
423 static unsigned char dyn_plt_entry
[plt_entry_size
];
425 // Set the final size.
427 set_final_data_size()
428 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
430 // Write out the PLT data.
432 do_write(Output_file
*);
434 // The reloc section.
436 // The .got.plt section.
437 Output_data_space
* got_plt_
;
438 // The number of PLT entries.
442 // Create the PLT section. The ordinary .got section is an argument,
443 // since we need to refer to the start. We also create our own .got
444 // section just for PLT entries.
446 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
447 Output_data_space
* got_plt
)
448 : Output_section_data(4), got_plt_(got_plt
), count_(0)
450 this->rel_
= new Reloc_section();
451 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
452 elfcpp::SHF_ALLOC
, this->rel_
);
456 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
458 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
459 // linker, and so do we.
463 // Add an entry to the PLT.
466 Output_data_plt_i386::add_entry(Symbol
* gsym
)
468 gold_assert(!gsym
->has_plt_offset());
470 // Note that when setting the PLT offset we skip the initial
471 // reserved PLT entry.
472 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
476 off_t got_offset
= this->got_plt_
->current_data_size();
478 // Every PLT entry needs a GOT entry which points back to the PLT
479 // entry (this will be changed by the dynamic linker, normally
480 // lazily when the function is called).
481 this->got_plt_
->set_current_data_size(got_offset
+ 4);
483 // Every PLT entry needs a reloc.
484 gsym
->set_needs_dynsym_entry();
485 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
488 // Note that we don't need to save the symbol. The contents of the
489 // PLT are independent of which symbols are used. The symbols only
490 // appear in the relocations.
493 // The first entry in the PLT for an executable.
495 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
497 0xff, 0x35, // pushl contents of memory address
498 0, 0, 0, 0, // replaced with address of .got + 4
499 0xff, 0x25, // jmp indirect
500 0, 0, 0, 0, // replaced with address of .got + 8
504 // The first entry in the PLT for a shared object.
506 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
508 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
509 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
513 // Subsequent entries in the PLT for an executable.
515 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
517 0xff, 0x25, // jmp indirect
518 0, 0, 0, 0, // replaced with address of symbol in .got
519 0x68, // pushl immediate
520 0, 0, 0, 0, // replaced with offset into relocation table
521 0xe9, // jmp relative
522 0, 0, 0, 0 // replaced with offset to start of .plt
525 // Subsequent entries in the PLT for a shared object.
527 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
529 0xff, 0xa3, // jmp *offset(%ebx)
530 0, 0, 0, 0, // replaced with offset of symbol in .got
531 0x68, // pushl immediate
532 0, 0, 0, 0, // replaced with offset into relocation table
533 0xe9, // jmp relative
534 0, 0, 0, 0 // replaced with offset to start of .plt
537 // Write out the PLT. This uses the hand-coded instructions above,
538 // and adjusts them as needed. This is all specified by the i386 ELF
539 // Processor Supplement.
542 Output_data_plt_i386::do_write(Output_file
* of
)
544 const off_t offset
= this->offset();
545 const off_t oview_size
= this->data_size();
546 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
548 const off_t got_file_offset
= this->got_plt_
->offset();
549 const off_t got_size
= this->got_plt_
->data_size();
550 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
553 unsigned char* pov
= oview
;
555 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
556 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
558 if (parameters
->output_is_shared())
559 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
562 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
563 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
564 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
566 pov
+= plt_entry_size
;
568 unsigned char* got_pov
= got_view
;
570 memset(got_pov
, 0, 12);
573 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
575 unsigned int plt_offset
= plt_entry_size
;
576 unsigned int plt_rel_offset
= 0;
577 unsigned int got_offset
= 12;
578 const unsigned int count
= this->count_
;
579 for (unsigned int i
= 0;
582 pov
+= plt_entry_size
,
584 plt_offset
+= plt_entry_size
,
585 plt_rel_offset
+= rel_size
,
588 // Set and adjust the PLT entry itself.
590 if (parameters
->output_is_shared())
592 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
593 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
597 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
598 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
603 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
604 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
605 - (plt_offset
+ plt_entry_size
));
607 // Set the entry in the GOT.
608 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
611 gold_assert(pov
- oview
== oview_size
);
612 gold_assert(got_pov
- got_view
== got_size
);
614 of
->write_output_view(offset
, oview_size
, oview
);
615 of
->write_output_view(got_file_offset
, got_size
, got_view
);
618 // Create a PLT entry for a global symbol.
621 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
623 if (gsym
->has_plt_offset())
626 if (this->plt_
== NULL
)
628 // Create the GOT sections first.
629 this->got_section(symtab
, layout
);
631 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
632 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
634 | elfcpp::SHF_EXECINSTR
),
638 this->plt_
->add_entry(gsym
);
641 // Create a GOT entry for the TLS module index.
644 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
645 Sized_relobj
<32, false>* object
)
647 if (this->got_mod_index_offset_
== -1U)
649 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
650 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
651 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
652 unsigned int got_offset
= got
->add_constant(0);
653 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
655 got
->add_constant(0);
656 this->got_mod_index_offset_
= got_offset
;
658 return this->got_mod_index_offset_
;
661 // Handle a relocation against a non-function symbol defined in a
662 // dynamic object. The traditional way to handle this is to generate
663 // a COPY relocation to copy the variable at runtime from the shared
664 // object into the executable's data segment. However, this is
665 // undesirable in general, as if the size of the object changes in the
666 // dynamic object, the executable will no longer work correctly. If
667 // this relocation is in a writable section, then we can create a
668 // dynamic reloc and the dynamic linker will resolve it to the correct
669 // address at runtime. However, we do not want do that if the
670 // relocation is in a read-only section, as it would prevent the
671 // readonly segment from being shared. And if we have to eventually
672 // generate a COPY reloc, then any dynamic relocations will be
673 // useless. So this means that if this is a writable section, we need
674 // to save the relocation until we see whether we have to create a
675 // COPY relocation for this symbol for any other relocation.
678 Target_i386::copy_reloc(const General_options
* options
,
679 Symbol_table
* symtab
,
681 Sized_relobj
<32, false>* object
,
682 unsigned int data_shndx
,
683 Output_section
* output_section
,
685 const elfcpp::Rel
<32, false>& rel
)
687 Sized_symbol
<32>* ssym
;
688 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
691 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
694 // So far we do not need a COPY reloc. Save this relocation.
695 // If it turns out that we never need a COPY reloc for this
696 // symbol, then we will emit the relocation.
697 if (this->copy_relocs_
== NULL
)
698 this->copy_relocs_
= new Copy_relocs
<32, false>();
699 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rel
);
703 // Allocate space for this symbol in the .bss section.
705 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
707 // There is no defined way to determine the required alignment
708 // of the symbol. We pick the alignment based on the size. We
709 // set an arbitrary maximum of 256.
711 for (align
= 1; align
< 512; align
<<= 1)
712 if ((symsize
& align
) != 0)
715 if (this->dynbss_
== NULL
)
717 this->dynbss_
= new Output_data_space(align
);
718 layout
->add_output_section_data(".bss",
721 | elfcpp::SHF_WRITE
),
725 Output_data_space
* dynbss
= this->dynbss_
;
727 if (align
> dynbss
->addralign())
728 dynbss
->set_space_alignment(align
);
730 off_t dynbss_size
= dynbss
->current_data_size();
731 dynbss_size
= align_address(dynbss_size
, align
);
732 off_t offset
= dynbss_size
;
733 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
735 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
737 // Add the COPY reloc.
738 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
739 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
743 // Optimize the TLS relocation type based on what we know about the
744 // symbol. IS_FINAL is true if the final address of this symbol is
745 // known at link time.
747 tls::Tls_optimization
748 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
750 // If we are generating a shared library, then we can't do anything
752 if (parameters
->output_is_shared())
753 return tls::TLSOPT_NONE
;
757 case elfcpp::R_386_TLS_GD
:
758 case elfcpp::R_386_TLS_GOTDESC
:
759 case elfcpp::R_386_TLS_DESC_CALL
:
760 // These are General-Dynamic which permits fully general TLS
761 // access. Since we know that we are generating an executable,
762 // we can convert this to Initial-Exec. If we also know that
763 // this is a local symbol, we can further switch to Local-Exec.
765 return tls::TLSOPT_TO_LE
;
766 return tls::TLSOPT_TO_IE
;
768 case elfcpp::R_386_TLS_LDM
:
769 // This is Local-Dynamic, which refers to a local symbol in the
770 // dynamic TLS block. Since we know that we generating an
771 // executable, we can switch to Local-Exec.
772 return tls::TLSOPT_TO_LE
;
774 case elfcpp::R_386_TLS_LDO_32
:
775 // Another type of Local-Dynamic relocation.
776 return tls::TLSOPT_TO_LE
;
778 case elfcpp::R_386_TLS_IE
:
779 case elfcpp::R_386_TLS_GOTIE
:
780 case elfcpp::R_386_TLS_IE_32
:
781 // These are Initial-Exec relocs which get the thread offset
782 // from the GOT. If we know that we are linking against the
783 // local symbol, we can switch to Local-Exec, which links the
784 // thread offset into the instruction.
786 return tls::TLSOPT_TO_LE
;
787 return tls::TLSOPT_NONE
;
789 case elfcpp::R_386_TLS_LE
:
790 case elfcpp::R_386_TLS_LE_32
:
791 // When we already have Local-Exec, there is nothing further we
793 return tls::TLSOPT_NONE
;
800 // Report an unsupported relocation against a local symbol.
803 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
806 gold_error(_("%s: unsupported reloc %u against local symbol"),
807 object
->name().c_str(), r_type
);
810 // Scan a relocation for a local symbol.
813 Target_i386::Scan::local(const General_options
&,
814 Symbol_table
* symtab
,
817 Sized_relobj
<32, false>* object
,
818 unsigned int data_shndx
,
819 Output_section
* output_section
,
820 const elfcpp::Rel
<32, false>& reloc
,
822 const elfcpp::Sym
<32, false>& lsym
)
826 case elfcpp::R_386_NONE
:
827 case elfcpp::R_386_GNU_VTINHERIT
:
828 case elfcpp::R_386_GNU_VTENTRY
:
831 case elfcpp::R_386_32
:
832 // If building a shared library (or a position-independent
833 // executable), we need to create a dynamic relocation for
834 // this location. The relocation applied at link time will
835 // apply the link-time value, so we flag the location with
836 // an R_386_RELATIVE relocation so the dynamic loader can
837 // relocate it easily.
838 if (parameters
->output_is_position_independent())
840 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
841 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
842 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
843 output_section
, data_shndx
,
844 reloc
.get_r_offset());
848 case elfcpp::R_386_16
:
849 case elfcpp::R_386_8
:
850 // If building a shared library (or a position-independent
851 // executable), we need to create a dynamic relocation for
852 // this location. Because the addend needs to remain in the
853 // data section, we need to be careful not to apply this
854 // relocation statically.
855 if (parameters
->output_is_position_independent())
857 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
858 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
859 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
, data_shndx
,
860 reloc
.get_r_offset());
864 case elfcpp::R_386_PC32
:
865 case elfcpp::R_386_PC16
:
866 case elfcpp::R_386_PC8
:
869 case elfcpp::R_386_PLT32
:
870 // Since we know this is a local symbol, we can handle this as a
874 case elfcpp::R_386_GOTOFF
:
875 case elfcpp::R_386_GOTPC
:
876 // We need a GOT section.
877 target
->got_section(symtab
, layout
);
880 case elfcpp::R_386_GOT32
:
882 // The symbol requires a GOT entry.
883 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
884 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
885 if (got
->add_local(object
, r_sym
))
887 // If we are generating a shared object, we need to add a
888 // dynamic RELATIVE relocation for this symbol's GOT entry.
889 if (parameters
->output_is_position_independent())
891 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
892 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
893 rel_dyn
->add_local_relative(object
, r_sym
,
894 elfcpp::R_386_RELATIVE
,
896 object
->local_got_offset(r_sym
));
902 // These are relocations which should only be seen by the
903 // dynamic linker, and should never be seen here.
904 case elfcpp::R_386_COPY
:
905 case elfcpp::R_386_GLOB_DAT
:
906 case elfcpp::R_386_JUMP_SLOT
:
907 case elfcpp::R_386_RELATIVE
:
908 case elfcpp::R_386_TLS_TPOFF
:
909 case elfcpp::R_386_TLS_DTPMOD32
:
910 case elfcpp::R_386_TLS_DTPOFF32
:
911 case elfcpp::R_386_TLS_TPOFF32
:
912 case elfcpp::R_386_TLS_DESC
:
913 gold_error(_("%s: unexpected reloc %u in object file"),
914 object
->name().c_str(), r_type
);
917 // These are initial TLS relocs, which are expected when
919 case elfcpp::R_386_TLS_GD
: // Global-dynamic
920 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
921 case elfcpp::R_386_TLS_DESC_CALL
:
922 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
923 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
924 case elfcpp::R_386_TLS_IE
: // Initial-exec
925 case elfcpp::R_386_TLS_IE_32
:
926 case elfcpp::R_386_TLS_GOTIE
:
927 case elfcpp::R_386_TLS_LE
: // Local-exec
928 case elfcpp::R_386_TLS_LE_32
:
930 bool output_is_shared
= parameters
->output_is_shared();
931 const tls::Tls_optimization optimized_type
932 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
935 case elfcpp::R_386_TLS_GD
: // Global-dynamic
936 if (optimized_type
== tls::TLSOPT_NONE
)
938 // Create a pair of GOT entries for the module index and
939 // dtv-relative offset.
940 Output_data_got
<32, false>* got
941 = target
->got_section(symtab
, layout
);
942 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
943 got
->add_local_tls_with_rel(object
, r_sym
,
944 lsym
.get_st_shndx(), true,
945 target
->rel_dyn_section(layout
),
946 elfcpp::R_386_TLS_DTPMOD32
);
948 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
949 unsupported_reloc_local(object
, r_type
);
952 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
953 case elfcpp::R_386_TLS_DESC_CALL
:
954 // FIXME: If not relaxing to LE, we need to generate
955 // a GOT entry with an R_386_TLS_DESC reloc.
956 if (optimized_type
!= tls::TLSOPT_TO_LE
)
957 unsupported_reloc_local(object
, r_type
);
960 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
961 if (optimized_type
== tls::TLSOPT_NONE
)
963 // Create a GOT entry for the module index.
964 target
->got_mod_index_entry(symtab
, layout
, object
);
966 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
967 unsupported_reloc_local(object
, r_type
);
970 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
973 case elfcpp::R_386_TLS_IE
: // Initial-exec
974 case elfcpp::R_386_TLS_IE_32
:
975 case elfcpp::R_386_TLS_GOTIE
:
976 layout
->set_has_static_tls();
977 if (optimized_type
== tls::TLSOPT_NONE
)
979 // For the R_386_TLS_IE relocation, we need to create a
980 // dynamic relocation when building a shared library.
981 if (r_type
== elfcpp::R_386_TLS_IE
982 && parameters
->output_is_shared())
984 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
986 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
987 rel_dyn
->add_local_relative(object
, r_sym
,
988 elfcpp::R_386_RELATIVE
,
989 output_section
, data_shndx
,
990 reloc
.get_r_offset());
992 // Create a GOT entry for the tp-relative offset.
993 Output_data_got
<32, false>* got
994 = target
->got_section(symtab
, layout
);
995 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
996 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
997 ? elfcpp::R_386_TLS_TPOFF32
998 : elfcpp::R_386_TLS_TPOFF
);
999 got
->add_local_with_rel(object
, r_sym
,
1000 target
->rel_dyn_section(layout
),
1003 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1004 unsupported_reloc_local(object
, r_type
);
1007 case elfcpp::R_386_TLS_LE
: // Local-exec
1008 case elfcpp::R_386_TLS_LE_32
:
1009 layout
->set_has_static_tls();
1010 if (output_is_shared
)
1012 // We need to create a dynamic relocation.
1013 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1014 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1015 ? elfcpp::R_386_TLS_TPOFF32
1016 : elfcpp::R_386_TLS_TPOFF
);
1017 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1018 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1019 data_shndx
, reloc
.get_r_offset());
1029 case elfcpp::R_386_32PLT
:
1030 case elfcpp::R_386_TLS_GD_32
:
1031 case elfcpp::R_386_TLS_GD_PUSH
:
1032 case elfcpp::R_386_TLS_GD_CALL
:
1033 case elfcpp::R_386_TLS_GD_POP
:
1034 case elfcpp::R_386_TLS_LDM_32
:
1035 case elfcpp::R_386_TLS_LDM_PUSH
:
1036 case elfcpp::R_386_TLS_LDM_CALL
:
1037 case elfcpp::R_386_TLS_LDM_POP
:
1038 case elfcpp::R_386_USED_BY_INTEL_200
:
1040 unsupported_reloc_local(object
, r_type
);
1045 // Report an unsupported relocation against a global symbol.
1048 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1049 unsigned int r_type
,
1052 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1053 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1056 // Scan a relocation for a global symbol.
1059 Target_i386::Scan::global(const General_options
& options
,
1060 Symbol_table
* symtab
,
1062 Target_i386
* target
,
1063 Sized_relobj
<32, false>* object
,
1064 unsigned int data_shndx
,
1065 Output_section
* output_section
,
1066 const elfcpp::Rel
<32, false>& reloc
,
1067 unsigned int r_type
,
1072 case elfcpp::R_386_NONE
:
1073 case elfcpp::R_386_GNU_VTINHERIT
:
1074 case elfcpp::R_386_GNU_VTENTRY
:
1077 case elfcpp::R_386_32
:
1078 case elfcpp::R_386_16
:
1079 case elfcpp::R_386_8
:
1081 // Make a PLT entry if necessary.
1082 if (gsym
->needs_plt_entry())
1084 target
->make_plt_entry(symtab
, layout
, gsym
);
1085 // Since this is not a PC-relative relocation, we may be
1086 // taking the address of a function. In that case we need to
1087 // set the entry in the dynamic symbol table to the address of
1089 if (gsym
->is_from_dynobj())
1090 gsym
->set_needs_dynsym_value();
1092 // Make a dynamic relocation if necessary.
1093 if (gsym
->needs_dynamic_reloc(true, false))
1095 if (target
->may_need_copy_reloc(gsym
))
1097 target
->copy_reloc(&options
, symtab
, layout
, object
,
1098 data_shndx
, output_section
, gsym
, reloc
);
1100 else if (r_type
== elfcpp::R_386_32
1101 && gsym
->can_use_relative_reloc(false))
1103 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1104 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1105 output_section
, object
,
1106 data_shndx
, reloc
.get_r_offset());
1110 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1111 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1112 data_shndx
, reloc
.get_r_offset());
1118 case elfcpp::R_386_PC32
:
1119 case elfcpp::R_386_PC16
:
1120 case elfcpp::R_386_PC8
:
1122 // Make a PLT entry if necessary.
1123 if (gsym
->needs_plt_entry())
1125 // These relocations are used for function calls only in
1126 // non-PIC code. For a 32-bit relocation in a shared library,
1127 // we'll need a text relocation anyway, so we can skip the
1128 // PLT entry and let the dynamic linker bind the call directly
1129 // to the target. For smaller relocations, we should use a
1130 // PLT entry to ensure that the call can reach.
1131 if (!parameters
->output_is_shared()
1132 || r_type
!= elfcpp::R_386_PC32
)
1133 target
->make_plt_entry(symtab
, layout
, gsym
);
1135 // Make a dynamic relocation if necessary.
1136 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1137 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1139 if (target
->may_need_copy_reloc(gsym
))
1141 target
->copy_reloc(&options
, symtab
, layout
, object
,
1142 data_shndx
, output_section
, gsym
, reloc
);
1146 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1147 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1148 data_shndx
, reloc
.get_r_offset());
1154 case elfcpp::R_386_GOT32
:
1156 // The symbol requires a GOT entry.
1157 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1158 if (gsym
->final_value_is_known())
1159 got
->add_global(gsym
);
1162 // If this symbol is not fully resolved, we need to add a
1163 // GOT entry with a dynamic relocation.
1164 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1165 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1166 got
->add_global_with_rel(gsym
, rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1169 if (got
->add_global(gsym
))
1170 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1171 got
, gsym
->got_offset());
1177 case elfcpp::R_386_PLT32
:
1178 // If the symbol is fully resolved, this is just a PC32 reloc.
1179 // Otherwise we need a PLT entry.
1180 if (gsym
->final_value_is_known())
1182 // If building a shared library, we can also skip the PLT entry
1183 // if the symbol is defined in the output file and is protected
1185 if (gsym
->is_defined()
1186 && !gsym
->is_from_dynobj()
1187 && !gsym
->is_preemptible())
1189 target
->make_plt_entry(symtab
, layout
, gsym
);
1192 case elfcpp::R_386_GOTOFF
:
1193 case elfcpp::R_386_GOTPC
:
1194 // We need a GOT section.
1195 target
->got_section(symtab
, layout
);
1198 // These are relocations which should only be seen by the
1199 // dynamic linker, and should never be seen here.
1200 case elfcpp::R_386_COPY
:
1201 case elfcpp::R_386_GLOB_DAT
:
1202 case elfcpp::R_386_JUMP_SLOT
:
1203 case elfcpp::R_386_RELATIVE
:
1204 case elfcpp::R_386_TLS_TPOFF
:
1205 case elfcpp::R_386_TLS_DTPMOD32
:
1206 case elfcpp::R_386_TLS_DTPOFF32
:
1207 case elfcpp::R_386_TLS_TPOFF32
:
1208 case elfcpp::R_386_TLS_DESC
:
1209 gold_error(_("%s: unexpected reloc %u in object file"),
1210 object
->name().c_str(), r_type
);
1213 // These are initial tls relocs, which are expected when
1215 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1216 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1217 case elfcpp::R_386_TLS_DESC_CALL
:
1218 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1219 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1220 case elfcpp::R_386_TLS_IE
: // Initial-exec
1221 case elfcpp::R_386_TLS_IE_32
:
1222 case elfcpp::R_386_TLS_GOTIE
:
1223 case elfcpp::R_386_TLS_LE
: // Local-exec
1224 case elfcpp::R_386_TLS_LE_32
:
1226 const bool is_final
= gsym
->final_value_is_known();
1227 const tls::Tls_optimization optimized_type
1228 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1231 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1232 if (optimized_type
== tls::TLSOPT_NONE
)
1234 // Create a pair of GOT entries for the module index and
1235 // dtv-relative offset.
1236 Output_data_got
<32, false>* got
1237 = target
->got_section(symtab
, layout
);
1238 got
->add_global_tls_with_rel(gsym
,
1239 target
->rel_dyn_section(layout
),
1240 elfcpp::R_386_TLS_DTPMOD32
,
1241 elfcpp::R_386_TLS_DTPOFF32
);
1243 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1245 // Create a GOT entry for the tp-relative offset.
1246 Output_data_got
<32, false>* got
1247 = target
->got_section(symtab
, layout
);
1248 got
->add_global_with_rel(gsym
, target
->rel_dyn_section(layout
),
1249 elfcpp::R_386_TLS_TPOFF32
);
1251 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1252 unsupported_reloc_global(object
, r_type
, gsym
);
1255 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1256 case elfcpp::R_386_TLS_DESC_CALL
:
1257 // FIXME: If not relaxing to LE, we need to generate
1258 // a GOT entry with an R_386_TLS_DESC reloc.
1259 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1260 unsupported_reloc_global(object
, r_type
, gsym
);
1261 unsupported_reloc_global(object
, r_type
, gsym
);
1264 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1265 if (optimized_type
== tls::TLSOPT_NONE
)
1267 // Create a GOT entry for the module index.
1268 target
->got_mod_index_entry(symtab
, layout
, object
);
1270 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1271 unsupported_reloc_global(object
, r_type
, gsym
);
1274 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1277 case elfcpp::R_386_TLS_IE
: // Initial-exec
1278 case elfcpp::R_386_TLS_IE_32
:
1279 case elfcpp::R_386_TLS_GOTIE
:
1280 layout
->set_has_static_tls();
1281 if (optimized_type
== tls::TLSOPT_NONE
)
1283 // For the R_386_TLS_IE relocation, we need to create a
1284 // dynamic relocation when building a shared library.
1285 if (r_type
== elfcpp::R_386_TLS_IE
1286 && parameters
->output_is_shared())
1288 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1289 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1290 output_section
, object
,
1292 reloc
.get_r_offset());
1294 // Create a GOT entry for the tp-relative offset.
1295 Output_data_got
<32, false>* got
1296 = target
->got_section(symtab
, layout
);
1297 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1298 ? elfcpp::R_386_TLS_TPOFF32
1299 : elfcpp::R_386_TLS_TPOFF
);
1300 got
->add_global_with_rel(gsym
,
1301 target
->rel_dyn_section(layout
),
1304 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1305 unsupported_reloc_global(object
, r_type
, gsym
);
1308 case elfcpp::R_386_TLS_LE
: // Local-exec
1309 case elfcpp::R_386_TLS_LE_32
:
1310 layout
->set_has_static_tls();
1311 if (parameters
->output_is_shared())
1313 // We need to create a dynamic relocation.
1314 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1315 ? elfcpp::R_386_TLS_TPOFF32
1316 : elfcpp::R_386_TLS_TPOFF
);
1317 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1318 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1319 data_shndx
, reloc
.get_r_offset());
1329 case elfcpp::R_386_32PLT
:
1330 case elfcpp::R_386_TLS_GD_32
:
1331 case elfcpp::R_386_TLS_GD_PUSH
:
1332 case elfcpp::R_386_TLS_GD_CALL
:
1333 case elfcpp::R_386_TLS_GD_POP
:
1334 case elfcpp::R_386_TLS_LDM_32
:
1335 case elfcpp::R_386_TLS_LDM_PUSH
:
1336 case elfcpp::R_386_TLS_LDM_CALL
:
1337 case elfcpp::R_386_TLS_LDM_POP
:
1338 case elfcpp::R_386_USED_BY_INTEL_200
:
1340 unsupported_reloc_global(object
, r_type
, gsym
);
1345 // Scan relocations for a section.
1348 Target_i386::scan_relocs(const General_options
& options
,
1349 Symbol_table
* symtab
,
1351 Sized_relobj
<32, false>* object
,
1352 unsigned int data_shndx
,
1353 unsigned int sh_type
,
1354 const unsigned char* prelocs
,
1356 Output_section
* output_section
,
1357 bool needs_special_offset_handling
,
1358 size_t local_symbol_count
,
1359 const unsigned char* plocal_symbols
)
1361 if (sh_type
== elfcpp::SHT_RELA
)
1363 gold_error(_("%s: unsupported RELA reloc section"),
1364 object
->name().c_str());
1368 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1379 needs_special_offset_handling
,
1384 // Finalize the sections.
1387 Target_i386::do_finalize_sections(Layout
* layout
)
1389 // Fill in some more dynamic tags.
1390 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1393 if (this->got_plt_
!= NULL
)
1394 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1396 if (this->plt_
!= NULL
)
1398 const Output_data
* od
= this->plt_
->rel_plt();
1399 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1400 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1401 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1404 if (this->rel_dyn_
!= NULL
)
1406 const Output_data
* od
= this->rel_dyn_
;
1407 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1408 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1409 odyn
->add_constant(elfcpp::DT_RELENT
,
1410 elfcpp::Elf_sizes
<32>::rel_size
);
1413 if (!parameters
->output_is_shared())
1415 // The value of the DT_DEBUG tag is filled in by the dynamic
1416 // linker at run time, and used by the debugger.
1417 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1421 // Emit any relocs we saved in an attempt to avoid generating COPY
1423 if (this->copy_relocs_
== NULL
)
1425 if (this->copy_relocs_
->any_to_emit())
1427 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1428 this->copy_relocs_
->emit(rel_dyn
);
1430 delete this->copy_relocs_
;
1431 this->copy_relocs_
= NULL
;
1434 // Return whether a direct absolute static relocation needs to be applied.
1435 // In cases where Scan::local() or Scan::global() has created
1436 // a dynamic relocation other than R_386_RELATIVE, the addend
1437 // of the relocation is carried in the data, and we must not
1438 // apply the static relocation.
1441 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1442 bool is_absolute_ref
,
1443 bool is_function_call
,
1446 // For local symbols, we will have created a non-RELATIVE dynamic
1447 // relocation only if (a) the output is position independent,
1448 // (b) the relocation is absolute (not pc- or segment-relative), and
1449 // (c) the relocation is not 32 bits wide.
1451 return !(parameters
->output_is_position_independent()
1455 // For global symbols, we use the same helper routines used in the scan pass.
1456 return !(gsym
->needs_dynamic_reloc(is_absolute_ref
, is_function_call
)
1457 && !gsym
->can_use_relative_reloc(is_function_call
));
1460 // Perform a relocation.
1463 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1464 Target_i386
* target
,
1466 const elfcpp::Rel
<32, false>& rel
,
1467 unsigned int r_type
,
1468 const Sized_symbol
<32>* gsym
,
1469 const Symbol_value
<32>* psymval
,
1470 unsigned char* view
,
1471 elfcpp::Elf_types
<32>::Elf_Addr address
,
1474 if (this->skip_call_tls_get_addr_
)
1476 if (r_type
!= elfcpp::R_386_PLT32
1478 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1479 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1480 _("missing expected TLS relocation"));
1483 this->skip_call_tls_get_addr_
= false;
1488 // Pick the value to use for symbols defined in shared objects.
1489 Symbol_value
<32> symval
;
1491 && (gsym
->is_from_dynobj()
1492 || (parameters
->output_is_shared()
1493 && gsym
->is_preemptible()))
1494 && gsym
->has_plt_offset())
1496 symval
.set_output_value(target
->plt_section()->address()
1497 + gsym
->plt_offset());
1501 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1503 // Get the GOT offset if needed.
1504 // The GOT pointer points to the end of the GOT section.
1505 // We need to subtract the size of the GOT section to get
1506 // the actual offset to use in the relocation.
1507 bool have_got_offset
= false;
1508 unsigned int got_offset
= 0;
1511 case elfcpp::R_386_GOT32
:
1514 gold_assert(gsym
->has_got_offset());
1515 got_offset
= gsym
->got_offset() - target
->got_size();
1519 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1520 gold_assert(object
->local_has_got_offset(r_sym
));
1521 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1523 have_got_offset
= true;
1532 case elfcpp::R_386_NONE
:
1533 case elfcpp::R_386_GNU_VTINHERIT
:
1534 case elfcpp::R_386_GNU_VTENTRY
:
1537 case elfcpp::R_386_32
:
1538 if (should_apply_static_reloc(gsym
, true, false, true))
1539 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1542 case elfcpp::R_386_PC32
:
1544 bool is_function_call
= (gsym
!= NULL
1545 && gsym
->type() == elfcpp::STT_FUNC
);
1546 if (should_apply_static_reloc(gsym
, false, is_function_call
, true))
1547 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1551 case elfcpp::R_386_16
:
1552 if (should_apply_static_reloc(gsym
, true, false, false))
1553 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1556 case elfcpp::R_386_PC16
:
1558 bool is_function_call
= (gsym
!= NULL
1559 && gsym
->type() == elfcpp::STT_FUNC
);
1560 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1561 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1565 case elfcpp::R_386_8
:
1566 if (should_apply_static_reloc(gsym
, true, false, false))
1567 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1570 case elfcpp::R_386_PC8
:
1572 bool is_function_call
= (gsym
!= NULL
1573 && gsym
->type() == elfcpp::STT_FUNC
);
1574 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1575 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1579 case elfcpp::R_386_PLT32
:
1580 gold_assert(gsym
== NULL
1581 || gsym
->has_plt_offset()
1582 || gsym
->final_value_is_known());
1583 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1586 case elfcpp::R_386_GOT32
:
1587 gold_assert(have_got_offset
);
1588 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1591 case elfcpp::R_386_GOTOFF
:
1593 elfcpp::Elf_types
<32>::Elf_Addr value
;
1594 value
= (psymval
->value(object
, 0)
1595 - target
->got_plt_section()->address());
1596 Relocate_functions
<32, false>::rel32(view
, value
);
1600 case elfcpp::R_386_GOTPC
:
1602 elfcpp::Elf_types
<32>::Elf_Addr value
;
1603 value
= target
->got_plt_section()->address();
1604 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1608 case elfcpp::R_386_COPY
:
1609 case elfcpp::R_386_GLOB_DAT
:
1610 case elfcpp::R_386_JUMP_SLOT
:
1611 case elfcpp::R_386_RELATIVE
:
1612 // These are outstanding tls relocs, which are unexpected when
1614 case elfcpp::R_386_TLS_TPOFF
:
1615 case elfcpp::R_386_TLS_DTPMOD32
:
1616 case elfcpp::R_386_TLS_DTPOFF32
:
1617 case elfcpp::R_386_TLS_TPOFF32
:
1618 case elfcpp::R_386_TLS_DESC
:
1619 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1620 _("unexpected reloc %u in object file"),
1624 // These are initial tls relocs, which are expected when
1626 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1627 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1628 case elfcpp::R_386_TLS_DESC_CALL
:
1629 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1630 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1631 case elfcpp::R_386_TLS_IE
: // Initial-exec
1632 case elfcpp::R_386_TLS_IE_32
:
1633 case elfcpp::R_386_TLS_GOTIE
:
1634 case elfcpp::R_386_TLS_LE
: // Local-exec
1635 case elfcpp::R_386_TLS_LE_32
:
1636 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1637 view
, address
, view_size
);
1640 case elfcpp::R_386_32PLT
:
1641 case elfcpp::R_386_TLS_GD_32
:
1642 case elfcpp::R_386_TLS_GD_PUSH
:
1643 case elfcpp::R_386_TLS_GD_CALL
:
1644 case elfcpp::R_386_TLS_GD_POP
:
1645 case elfcpp::R_386_TLS_LDM_32
:
1646 case elfcpp::R_386_TLS_LDM_PUSH
:
1647 case elfcpp::R_386_TLS_LDM_CALL
:
1648 case elfcpp::R_386_TLS_LDM_POP
:
1649 case elfcpp::R_386_USED_BY_INTEL_200
:
1651 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1652 _("unsupported reloc %u"),
1660 // Perform a TLS relocation.
1663 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1664 Target_i386
* target
,
1666 const elfcpp::Rel
<32, false>& rel
,
1667 unsigned int r_type
,
1668 const Sized_symbol
<32>* gsym
,
1669 const Symbol_value
<32>* psymval
,
1670 unsigned char* view
,
1671 elfcpp::Elf_types
<32>::Elf_Addr
,
1674 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1676 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1678 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1680 const bool is_final
= (gsym
== NULL
1681 ? !parameters
->output_is_position_independent()
1682 : gsym
->final_value_is_known());
1683 const tls::Tls_optimization optimized_type
1684 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1687 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1688 if (optimized_type
== tls::TLSOPT_TO_LE
)
1690 gold_assert(tls_segment
!= NULL
);
1691 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1692 rel
, r_type
, value
, view
,
1698 unsigned int got_offset
;
1701 gold_assert(gsym
->has_tls_got_offset(true));
1702 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1706 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1707 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1708 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1709 - target
->got_size());
1711 if (optimized_type
== tls::TLSOPT_TO_IE
)
1713 gold_assert(tls_segment
!= NULL
);
1714 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1715 got_offset
, view
, view_size
);
1718 else if (optimized_type
== tls::TLSOPT_NONE
)
1720 // Relocate the field with the offset of the pair of GOT
1722 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1726 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1727 _("unsupported reloc %u"),
1731 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1732 case elfcpp::R_386_TLS_DESC_CALL
:
1733 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1734 _("unsupported reloc %u"),
1738 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1739 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1741 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1742 _("both SUN and GNU model "
1743 "TLS relocations"));
1746 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1747 if (optimized_type
== tls::TLSOPT_TO_LE
)
1749 gold_assert(tls_segment
!= NULL
);
1750 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1751 value
, view
, view_size
);
1754 else if (optimized_type
== tls::TLSOPT_NONE
)
1756 // Relocate the field with the offset of the GOT entry for
1757 // the module index.
1758 unsigned int got_offset
;
1759 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1760 - target
->got_size());
1761 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1764 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1765 _("unsupported reloc %u"),
1769 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1770 // This reloc can appear in debugging sections, in which case we
1771 // won't see the TLS_LDM reloc. The local_dynamic_type field
1773 if (optimized_type
== tls::TLSOPT_TO_LE
)
1775 gold_assert(tls_segment
!= NULL
);
1776 value
-= tls_segment
->memsz();
1778 Relocate_functions
<32, false>::rel32(view
, value
);
1781 case elfcpp::R_386_TLS_IE
: // Initial-exec
1782 case elfcpp::R_386_TLS_GOTIE
:
1783 case elfcpp::R_386_TLS_IE_32
:
1784 if (optimized_type
== tls::TLSOPT_TO_LE
)
1786 gold_assert(tls_segment
!= NULL
);
1787 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1788 rel
, r_type
, value
, view
,
1792 else if (optimized_type
== tls::TLSOPT_NONE
)
1794 // Relocate the field with the offset of the GOT entry for
1795 // the tp-relative offset of the symbol.
1796 unsigned int got_offset
;
1799 gold_assert(gsym
->has_got_offset());
1800 got_offset
= gsym
->got_offset();
1804 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1805 gold_assert(object
->local_has_got_offset(r_sym
));
1806 got_offset
= object
->local_got_offset(r_sym
);
1808 // For the R_386_TLS_IE relocation, we need to apply the
1809 // absolute address of the GOT entry.
1810 if (r_type
== elfcpp::R_386_TLS_IE
)
1811 got_offset
+= target
->got_plt_section()->address();
1812 // All GOT offsets are relative to the end of the GOT.
1813 got_offset
-= target
->got_size();
1814 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1817 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1818 _("unsupported reloc %u"),
1822 case elfcpp::R_386_TLS_LE
: // Local-exec
1823 // If we're creating a shared library, a dynamic relocation will
1824 // have been created for this location, so do not apply it now.
1825 if (!parameters
->output_is_shared())
1827 gold_assert(tls_segment
!= NULL
);
1828 value
-= tls_segment
->memsz();
1829 Relocate_functions
<32, false>::rel32(view
, value
);
1833 case elfcpp::R_386_TLS_LE_32
:
1834 // If we're creating a shared library, a dynamic relocation will
1835 // have been created for this location, so do not apply it now.
1836 if (!parameters
->output_is_shared())
1838 gold_assert(tls_segment
!= NULL
);
1839 value
= tls_segment
->memsz() - value
;
1840 Relocate_functions
<32, false>::rel32(view
, value
);
1846 // Do a relocation in which we convert a TLS General-Dynamic to a
1850 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1852 Output_segment
* tls_segment
,
1853 const elfcpp::Rel
<32, false>& rel
,
1855 elfcpp::Elf_types
<32>::Elf_Addr value
,
1856 unsigned char* view
,
1859 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1860 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1861 // leal foo(%reg),%eax; call ___tls_get_addr
1862 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1864 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1865 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1867 unsigned char op1
= view
[-1];
1868 unsigned char op2
= view
[-2];
1870 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1871 op2
== 0x8d || op2
== 0x04);
1872 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1878 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1879 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1880 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1881 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1882 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1886 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1887 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1888 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1891 // There is a trailing nop. Use the size byte subl.
1892 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1897 // Use the five byte subl.
1898 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1902 value
= tls_segment
->memsz() - value
;
1903 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1905 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1907 this->skip_call_tls_get_addr_
= true;
1910 // Do a relocation in which we convert a TLS General-Dynamic to an
1914 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
1916 Output_segment
* tls_segment
,
1917 const elfcpp::Rel
<32, false>& rel
,
1919 elfcpp::Elf_types
<32>::Elf_Addr value
,
1920 unsigned char* view
,
1923 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
1924 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
1926 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1927 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1929 unsigned char op1
= view
[-1];
1930 unsigned char op2
= view
[-2];
1932 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1933 op2
== 0x8d || op2
== 0x04);
1934 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1938 // FIXME: For now, support only one form.
1939 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1940 op1
== 0x8d && op2
== 0x04);
1944 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1945 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1946 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1947 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1948 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
1952 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1953 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1954 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1957 // FIXME: This is not the right instruction sequence.
1958 // There is a trailing nop. Use the size byte subl.
1959 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1964 // FIXME: This is not the right instruction sequence.
1965 // Use the five byte subl.
1966 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1970 value
= tls_segment
->memsz() - value
;
1971 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1973 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1975 this->skip_call_tls_get_addr_
= true;
1978 // Do a relocation in which we convert a TLS Local-Dynamic to a
1982 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1985 const elfcpp::Rel
<32, false>& rel
,
1987 elfcpp::Elf_types
<32>::Elf_Addr
,
1988 unsigned char* view
,
1991 // leal foo(%reg), %eax; call ___tls_get_addr
1992 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1994 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1995 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1997 // FIXME: Does this test really always pass?
1998 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1999 view
[-2] == 0x8d && view
[-1] == 0x83);
2001 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2003 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2005 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2007 this->skip_call_tls_get_addr_
= true;
2010 // Do a relocation in which we convert a TLS Initial-Exec to a
2014 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2016 Output_segment
* tls_segment
,
2017 const elfcpp::Rel
<32, false>& rel
,
2018 unsigned int r_type
,
2019 elfcpp::Elf_types
<32>::Elf_Addr value
,
2020 unsigned char* view
,
2023 // We have to actually change the instructions, which means that we
2024 // need to examine the opcodes to figure out which instruction we
2026 if (r_type
== elfcpp::R_386_TLS_IE
)
2028 // movl %gs:XX,%eax ==> movl $YY,%eax
2029 // movl %gs:XX,%reg ==> movl $YY,%reg
2030 // addl %gs:XX,%reg ==> addl $YY,%reg
2031 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2032 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2034 unsigned char op1
= view
[-1];
2037 // movl XX,%eax ==> movl $YY,%eax
2042 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2044 unsigned char op2
= view
[-2];
2047 // movl XX,%reg ==> movl $YY,%reg
2048 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2049 (op1
& 0xc7) == 0x05);
2051 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2053 else if (op2
== 0x03)
2055 // addl XX,%reg ==> addl $YY,%reg
2056 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2057 (op1
& 0xc7) == 0x05);
2059 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2062 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2067 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2068 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2069 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2070 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2071 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2073 unsigned char op1
= view
[-1];
2074 unsigned char op2
= view
[-2];
2075 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2076 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2079 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2081 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2083 else if (op2
== 0x2b)
2085 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2087 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2089 else if (op2
== 0x03)
2091 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2093 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2096 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2099 value
= tls_segment
->memsz() - value
;
2100 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2103 Relocate_functions
<32, false>::rel32(view
, value
);
2106 // Relocate section data.
2109 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2110 unsigned int sh_type
,
2111 const unsigned char* prelocs
,
2113 Output_section
* output_section
,
2114 bool needs_special_offset_handling
,
2115 unsigned char* view
,
2116 elfcpp::Elf_types
<32>::Elf_Addr address
,
2119 gold_assert(sh_type
== elfcpp::SHT_REL
);
2121 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2122 Target_i386::Relocate
>(
2128 needs_special_offset_handling
,
2134 // Return the value to use for a dynamic which requires special
2135 // treatment. This is how we support equality comparisons of function
2136 // pointers across shared library boundaries, as described in the
2137 // processor specific ABI supplement.
2140 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2142 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2143 return this->plt_section()->address() + gsym
->plt_offset();
2146 // Return a string used to fill a code section with nops to take up
2147 // the specified length.
2150 Target_i386::do_code_fill(off_t length
)
2154 // Build a jmp instruction to skip over the bytes.
2155 unsigned char jmp
[5];
2157 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2158 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2159 + std::string(length
- 5, '\0'));
2162 // Nop sequences of various lengths.
2163 const char nop1
[1] = { 0x90 }; // nop
2164 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2165 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2166 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2167 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2168 0x00 }; // leal 0(%esi,1),%esi
2169 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2171 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2173 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2174 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2175 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2176 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2178 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2179 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2181 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2182 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2184 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2185 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2186 0x00, 0x00, 0x00, 0x00 };
2187 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2188 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2189 0x27, 0x00, 0x00, 0x00,
2191 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2192 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2193 0xbc, 0x27, 0x00, 0x00,
2195 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2196 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2197 0x90, 0x90, 0x90, 0x90,
2200 const char* nops
[16] = {
2202 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2203 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2206 return std::string(nops
[length
], length
);
2209 // The selector for i386 object files.
2211 class Target_selector_i386
: public Target_selector
2214 Target_selector_i386()
2215 : Target_selector(elfcpp::EM_386
, 32, false)
2219 recognize(int machine
, int osabi
, int abiversion
);
2222 Target_i386
* target_
;
2225 // Recognize an i386 object file when we already know that the machine
2226 // number is EM_386.
2229 Target_selector_i386::recognize(int, int, int)
2231 if (this->target_
== NULL
)
2232 this->target_
= new Target_i386();
2233 return this->target_
;
2236 Target_selector_i386 target_selector_i386
;
2238 } // End anonymous namespace.