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"
44 class Output_data_plt_i386
;
46 // The i386 target class.
48 class Target_i386
: public Sized_target
<32, false>
51 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
54 : Sized_target
<32, false>(&i386_info
),
55 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
56 copy_relocs_(NULL
), dynbss_(NULL
)
59 // Scan the relocations to look for symbol adjustments.
61 scan_relocs(const General_options
& options
,
64 Sized_relobj
<32, false>* object
,
65 unsigned int data_shndx
,
67 const unsigned char* prelocs
,
69 size_t local_symbol_count
,
70 const unsigned char* plocal_symbols
,
71 Symbol
** global_symbols
);
73 // Finalize the sections.
75 do_finalize_sections(Layout
*);
77 // Return the value to use for a dynamic which requires special
80 do_dynsym_value(const Symbol
*) const;
82 // Relocate a section.
84 relocate_section(const Relocate_info
<32, false>*,
86 const unsigned char* prelocs
,
89 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
92 // Return a string used to fill a code section with nops.
94 do_code_fill(off_t length
);
97 // The class which scans relocations.
101 local(const General_options
& options
, Symbol_table
* symtab
,
102 Layout
* layout
, Target_i386
* target
,
103 Sized_relobj
<32, false>* object
,
104 unsigned int data_shndx
,
105 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
106 const elfcpp::Sym
<32, false>& lsym
);
109 global(const General_options
& options
, Symbol_table
* symtab
,
110 Layout
* layout
, Target_i386
* target
,
111 Sized_relobj
<32, false>* object
,
112 unsigned int data_shndx
,
113 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
117 // The class which implements relocation.
122 : skip_call_tls_get_addr_(false)
127 if (this->skip_call_tls_get_addr_
)
129 // FIXME: This needs to specify the location somehow.
130 fprintf(stderr
, _("%s: missing expected TLS relocation\n"),
136 // Do a relocation. Return false if the caller should not issue
137 // any warnings about this relocation.
139 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
140 const elfcpp::Rel
<32, false>&,
141 unsigned int r_type
, const Sized_symbol
<32>*,
142 const Symbol_value
<32>*,
143 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
147 // Do a TLS relocation.
149 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
150 const elfcpp::Rel
<32, false>&,
151 unsigned int r_type
, const Sized_symbol
<32>*,
152 const Symbol_value
<32>*,
153 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
155 // Do a TLS Initial-Exec to Local-Exec transition.
157 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
158 Output_segment
* tls_segment
,
159 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
160 elfcpp::Elf_types
<32>::Elf_Addr value
,
164 // Do a TLS Global-Dynamic to Local-Exec transition.
166 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
167 Output_segment
* tls_segment
,
168 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
169 elfcpp::Elf_types
<32>::Elf_Addr value
,
173 // Check the range for a TLS relocation.
175 check_range(const Relocate_info
<32, false>*, size_t relnum
,
176 const elfcpp::Rel
<32, false>&, off_t
, off_t
);
178 // Check the validity of a TLS relocation. This is like assert.
180 check_tls(const Relocate_info
<32, false>*, size_t relnum
,
181 const elfcpp::Rel
<32, false>&, bool);
183 // This is set if we should skip the next reloc, which should be a
184 // PLT32 reloc against ___tls_get_addr.
185 bool skip_call_tls_get_addr_
;
188 // Adjust TLS relocation type based on the options and whether this
189 // is a local symbol.
191 optimize_tls_reloc(bool is_final
, int r_type
);
193 // Get the GOT section, creating it if necessary.
194 Output_data_got
<32, false>*
195 got_section(Symbol_table
*, Layout
*);
197 // Create a PLT entry for a global symbol.
199 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
201 // Get the PLT section.
202 const Output_data_plt_i386
*
205 gold_assert(this->plt_
!= NULL
);
209 // Get the dynamic reloc section, creating it if necessary.
211 rel_dyn_section(Layout
*);
213 // Copy a relocation against a global symbol.
215 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
216 Sized_relobj
<32, false>*, unsigned int,
217 Symbol
*, const elfcpp::Rel
<32, false>&);
219 // Information about this specific target which we pass to the
220 // general Target structure.
221 static const Target::Target_info i386_info
;
224 Output_data_got
<32, false>* got_
;
226 Output_data_plt_i386
* plt_
;
227 // The GOT PLT section.
228 Output_data_space
* got_plt_
;
229 // The dynamic reloc section.
230 Reloc_section
* rel_dyn_
;
231 // Relocs saved to avoid a COPY reloc.
232 Copy_relocs
<32, false>* copy_relocs_
;
233 // Space for variables copied with a COPY reloc.
234 Output_data_space
* dynbss_
;
237 const Target::Target_info
Target_i386::i386_info
=
240 false, // is_big_endian
241 elfcpp::EM_386
, // machine_code
242 false, // has_make_symbol
243 false, // has_resolve
244 true, // has_code_fill
245 "/usr/lib/libc.so.1", // dynamic_linker
246 0x08048000, // text_segment_address
247 0x1000, // abi_pagesize
248 0x1000 // common_pagesize
251 // Get the GOT section, creating it if necessary.
253 Output_data_got
<32, false>*
254 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
256 if (this->got_
== NULL
)
258 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
260 this->got_
= new Output_data_got
<32, false>();
262 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
263 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
266 // The old GNU linker creates a .got.plt section. We just
267 // create another set of data in the .got section. Note that we
268 // always create a PLT if we create a GOT, although the PLT
270 this->got_plt_
= new Output_data_space(4);
271 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
272 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
275 // The first three entries are reserved.
276 this->got_plt_
->set_space_size(3 * 4);
278 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
279 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
281 0, 0, elfcpp::STT_OBJECT
,
283 elfcpp::STV_HIDDEN
, 0,
290 // Get the dynamic reloc section, creating it if necessary.
292 Target_i386::Reloc_section
*
293 Target_i386::rel_dyn_section(Layout
* layout
)
295 if (this->rel_dyn_
== NULL
)
297 gold_assert(layout
!= NULL
);
298 this->rel_dyn_
= new Reloc_section();
299 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
300 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
302 return this->rel_dyn_
;
305 // A class to handle the PLT data.
307 class Output_data_plt_i386
: public Output_section_data
310 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
312 Output_data_plt_i386(Layout
*, Output_data_space
*);
314 // Add an entry to the PLT.
316 add_entry(Symbol
* gsym
);
318 // Return the .rel.plt section data.
321 { return this->rel_
; }
325 do_adjust_output_section(Output_section
* os
);
328 // The size of an entry in the PLT.
329 static const int plt_entry_size
= 16;
331 // The first entry in the PLT for an executable.
332 static unsigned char exec_first_plt_entry
[plt_entry_size
];
334 // The first entry in the PLT for a shared object.
335 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
337 // Other entries in the PLT for an executable.
338 static unsigned char exec_plt_entry
[plt_entry_size
];
340 // Other entries in the PLT for a shared object.
341 static unsigned char dyn_plt_entry
[plt_entry_size
];
343 // Set the final size.
345 do_set_address(uint64_t, off_t
)
346 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
348 // Write out the PLT data.
350 do_write(Output_file
*);
352 // The reloc section.
354 // The .got.plt section.
355 Output_data_space
* got_plt_
;
356 // The number of PLT entries.
360 // Create the PLT section. The ordinary .got section is an argument,
361 // since we need to refer to the start. We also create our own .got
362 // section just for PLT entries.
364 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
365 Output_data_space
* got_plt
)
366 : Output_section_data(4), got_plt_(got_plt
), count_(0)
368 this->rel_
= new Reloc_section();
369 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
370 elfcpp::SHF_ALLOC
, this->rel_
);
374 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
376 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
377 // linker, and so do we.
381 // Add an entry to the PLT.
384 Output_data_plt_i386::add_entry(Symbol
* gsym
)
386 gold_assert(!gsym
->has_plt_offset());
388 // Note that when setting the PLT offset we skip the initial
389 // reserved PLT entry.
390 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
394 off_t got_offset
= this->got_plt_
->data_size();
396 // Every PLT entry needs a GOT entry which points back to the PLT
397 // entry (this will be changed by the dynamic linker, normally
398 // lazily when the function is called).
399 this->got_plt_
->set_space_size(got_offset
+ 4);
401 // Every PLT entry needs a reloc.
402 gsym
->set_needs_dynsym_entry();
403 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
406 // Note that we don't need to save the symbol. The contents of the
407 // PLT are independent of which symbols are used. The symbols only
408 // appear in the relocations.
411 // The first entry in the PLT for an executable.
413 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
415 0xff, 0x35, // pushl contents of memory address
416 0, 0, 0, 0, // replaced with address of .got + 4
417 0xff, 0x25, // jmp indirect
418 0, 0, 0, 0, // replaced with address of .got + 8
422 // The first entry in the PLT for a shared object.
424 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
426 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
427 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
431 // Subsequent entries in the PLT for an executable.
433 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
435 0xff, 0x25, // jmp indirect
436 0, 0, 0, 0, // replaced with address of symbol in .got
437 0x68, // pushl immediate
438 0, 0, 0, 0, // replaced with offset into relocation table
439 0xe9, // jmp relative
440 0, 0, 0, 0 // replaced with offset to start of .plt
443 // Subsequent entries in the PLT for a shared object.
445 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
447 0xff, 0xa3, // jmp *offset(%ebx)
448 0, 0, 0, 0, // replaced with offset of symbol in .got
449 0x68, // pushl immediate
450 0, 0, 0, 0, // replaced with offset into relocation table
451 0xe9, // jmp relative
452 0, 0, 0, 0 // replaced with offset to start of .plt
455 // Write out the PLT. This uses the hand-coded instructions above,
456 // and adjusts them as needed. This is all specified by the i386 ELF
457 // Processor Supplement.
460 Output_data_plt_i386::do_write(Output_file
* of
)
462 const off_t offset
= this->offset();
463 const off_t oview_size
= this->data_size();
464 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
466 const off_t got_file_offset
= this->got_plt_
->offset();
467 const off_t got_size
= this->got_plt_
->data_size();
468 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
471 unsigned char* pov
= oview
;
473 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
474 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
476 if (parameters
->output_is_shared())
477 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
480 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
481 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
482 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
484 pov
+= plt_entry_size
;
486 unsigned char* got_pov
= got_view
;
488 memset(got_pov
, 0, 12);
491 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
493 unsigned int plt_offset
= plt_entry_size
;
494 unsigned int plt_rel_offset
= 0;
495 unsigned int got_offset
= 12;
496 const unsigned int count
= this->count_
;
497 for (unsigned int i
= 0;
500 pov
+= plt_entry_size
,
502 plt_offset
+= plt_entry_size
,
503 plt_rel_offset
+= rel_size
,
506 // Set and adjust the PLT entry itself.
508 if (parameters
->output_is_shared())
510 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
511 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
515 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
516 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
521 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
522 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
523 - (plt_offset
+ plt_entry_size
));
525 // Set the entry in the GOT.
526 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
529 gold_assert(pov
- oview
== oview_size
);
530 gold_assert(got_pov
- got_view
== got_size
);
532 of
->write_output_view(offset
, oview_size
, oview
);
533 of
->write_output_view(got_file_offset
, got_size
, got_view
);
536 // Create a PLT entry for a global symbol.
539 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
541 if (gsym
->has_plt_offset())
544 if (this->plt_
== NULL
)
546 // Create the GOT sections first.
547 this->got_section(symtab
, layout
);
549 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
550 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
552 | elfcpp::SHF_EXECINSTR
),
556 this->plt_
->add_entry(gsym
);
559 // Handle a relocation against a non-function symbol defined in a
560 // dynamic object. The traditional way to handle this is to generate
561 // a COPY relocation to copy the variable at runtime from the shared
562 // object into the executable's data segment. However, this is
563 // undesirable in general, as if the size of the object changes in the
564 // dynamic object, the executable will no longer work correctly. If
565 // this relocation is in a writable section, then we can create a
566 // dynamic reloc and the dynamic linker will resolve it to the correct
567 // address at runtime. However, we do not want do that if the
568 // relocation is in a read-only section, as it would prevent the
569 // readonly segment from being shared. And if we have to eventually
570 // generate a COPY reloc, then any dynamic relocations will be
571 // useless. So this means that if this is a writable section, we need
572 // to save the relocation until we see whether we have to create a
573 // COPY relocation for this symbol for any other relocation.
576 Target_i386::copy_reloc(const General_options
* options
,
577 Symbol_table
* symtab
,
579 Sized_relobj
<32, false>* object
,
580 unsigned int data_shndx
, Symbol
* gsym
,
581 const elfcpp::Rel
<32, false>& rel
)
583 Sized_symbol
<32>* ssym
;
584 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
587 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
590 // So far we do not need a COPY reloc. Save this relocation.
591 // If it turns out that we never need a COPY reloc for this
592 // symbol, then we will emit the relocation.
593 if (this->copy_relocs_
== NULL
)
594 this->copy_relocs_
= new Copy_relocs
<32, false>();
595 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
599 // Allocate space for this symbol in the .bss section.
601 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
603 // There is no defined way to determine the required alignment
604 // of the symbol. We pick the alignment based on the size. We
605 // set an arbitrary maximum of 256.
607 for (align
= 1; align
< 512; align
<<= 1)
608 if ((symsize
& align
) != 0)
611 if (this->dynbss_
== NULL
)
613 this->dynbss_
= new Output_data_space(align
);
614 layout
->add_output_section_data(".bss",
617 | elfcpp::SHF_WRITE
),
621 Output_data_space
* dynbss
= this->dynbss_
;
623 if (align
> dynbss
->addralign())
624 dynbss
->set_space_alignment(align
);
626 off_t dynbss_size
= dynbss
->data_size();
627 dynbss_size
= align_address(dynbss_size
, align
);
628 off_t offset
= dynbss_size
;
629 dynbss
->set_space_size(dynbss_size
+ symsize
);
631 // Define the symbol in the .dynbss section.
632 symtab
->define_in_output_data(this, ssym
->name(), ssym
->version(),
633 dynbss
, offset
, symsize
, ssym
->type(),
634 ssym
->binding(), ssym
->visibility(),
635 ssym
->nonvis(), false, false);
637 // Add the COPY reloc.
638 ssym
->set_needs_dynsym_entry();
639 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
640 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
644 // Optimize the TLS relocation type based on what we know about the
645 // symbol. IS_FINAL is true if the final address of this symbol is
646 // known at link time.
649 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
651 // If we are generating a shared library, then we can't do anything
653 if (parameters
->output_is_shared())
658 case elfcpp::R_386_TLS_GD
:
659 case elfcpp::R_386_TLS_GOTDESC
:
660 case elfcpp::R_386_TLS_DESC_CALL
:
661 // These are Global-Dynamic which permits fully general TLS
662 // access. Since we know that we are generating an executable,
663 // we can convert this to Initial-Exec. If we also know that
664 // this is a local symbol, we can further switch to Local-Exec.
666 return elfcpp::R_386_TLS_LE_32
;
667 return elfcpp::R_386_TLS_IE_32
;
669 case elfcpp::R_386_TLS_LDM
:
670 // This is Local-Dynamic, which refers to a local symbol in the
671 // dynamic TLS block. Since we know that we generating an
672 // executable, we can switch to Local-Exec.
673 return elfcpp::R_386_TLS_LE_32
;
675 case elfcpp::R_386_TLS_LDO_32
:
676 // Another type of Local-Dynamic relocation.
677 return elfcpp::R_386_TLS_LE
;
679 case elfcpp::R_386_TLS_IE
:
680 case elfcpp::R_386_TLS_GOTIE
:
681 case elfcpp::R_386_TLS_IE_32
:
682 // These are Initial-Exec relocs which get the thread offset
683 // from the GOT. If we know that we are linking against the
684 // local symbol, we can switch to Local-Exec, which links the
685 // thread offset into the instruction.
687 return elfcpp::R_386_TLS_LE_32
;
690 case elfcpp::R_386_TLS_LE
:
691 case elfcpp::R_386_TLS_LE_32
:
692 // When we already have Local-Exec, there is nothing further we
701 // Scan a relocation for a local symbol.
704 Target_i386::Scan::local(const General_options
&,
705 Symbol_table
* symtab
,
708 Sized_relobj
<32, false>* object
,
710 const elfcpp::Rel
<32, false>&,
712 const elfcpp::Sym
<32, false>&)
716 case elfcpp::R_386_NONE
:
717 case elfcpp::R_386_GNU_VTINHERIT
:
718 case elfcpp::R_386_GNU_VTENTRY
:
721 case elfcpp::R_386_32
:
722 case elfcpp::R_386_16
:
723 case elfcpp::R_386_8
:
724 // FIXME: If we are generating a shared object we need to copy
725 // this relocation into the object.
726 gold_assert(!parameters
->output_is_shared());
729 case elfcpp::R_386_PC32
:
730 case elfcpp::R_386_PC16
:
731 case elfcpp::R_386_PC8
:
734 case elfcpp::R_386_GOTOFF
:
735 case elfcpp::R_386_GOTPC
:
736 // We need a GOT section.
737 target
->got_section(symtab
, layout
);
740 case elfcpp::R_386_COPY
:
741 case elfcpp::R_386_GLOB_DAT
:
742 case elfcpp::R_386_JUMP_SLOT
:
743 case elfcpp::R_386_RELATIVE
:
744 // These are outstanding tls relocs, which are unexpected when
746 case elfcpp::R_386_TLS_TPOFF
:
747 case elfcpp::R_386_TLS_DTPMOD32
:
748 case elfcpp::R_386_TLS_DTPOFF32
:
749 case elfcpp::R_386_TLS_TPOFF32
:
750 case elfcpp::R_386_TLS_DESC
:
751 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
752 program_name
, object
->name().c_str(), r_type
);
756 // These are initial tls relocs, which are expected when
758 case elfcpp::R_386_TLS_IE
:
759 case elfcpp::R_386_TLS_GOTIE
:
760 case elfcpp::R_386_TLS_LE
:
761 case elfcpp::R_386_TLS_GD
:
762 case elfcpp::R_386_TLS_LDM
:
763 case elfcpp::R_386_TLS_LDO_32
:
764 case elfcpp::R_386_TLS_IE_32
:
765 case elfcpp::R_386_TLS_LE_32
:
766 case elfcpp::R_386_TLS_GOTDESC
:
767 case elfcpp::R_386_TLS_DESC_CALL
:
769 bool output_is_shared
= parameters
->output_is_shared();
770 r_type
= Target_i386::optimize_tls_reloc(!output_is_shared
,
774 case elfcpp::R_386_TLS_LE
:
775 case elfcpp::R_386_TLS_LE_32
:
776 // FIXME: If generating a shared object, we need to copy
777 // this relocation into the object.
778 gold_assert(!output_is_shared
);
781 case elfcpp::R_386_TLS_IE
:
782 case elfcpp::R_386_TLS_GOTIE
:
783 case elfcpp::R_386_TLS_GD
:
784 case elfcpp::R_386_TLS_LDM
:
785 case elfcpp::R_386_TLS_LDO_32
:
786 case elfcpp::R_386_TLS_IE_32
:
787 case elfcpp::R_386_TLS_GOTDESC
:
788 case elfcpp::R_386_TLS_DESC_CALL
:
790 _("%s: %s: unsupported reloc %u against local symbol\n"),
791 program_name
, object
->name().c_str(), r_type
);
797 case elfcpp::R_386_GOT32
:
798 case elfcpp::R_386_PLT32
:
799 case elfcpp::R_386_32PLT
:
800 case elfcpp::R_386_TLS_GD_32
:
801 case elfcpp::R_386_TLS_GD_PUSH
:
802 case elfcpp::R_386_TLS_GD_CALL
:
803 case elfcpp::R_386_TLS_GD_POP
:
804 case elfcpp::R_386_TLS_LDM_32
:
805 case elfcpp::R_386_TLS_LDM_PUSH
:
806 case elfcpp::R_386_TLS_LDM_CALL
:
807 case elfcpp::R_386_TLS_LDM_POP
:
808 case elfcpp::R_386_USED_BY_INTEL_200
:
810 fprintf(stderr
, _("%s: %s: unsupported reloc %u against local symbol\n"),
811 program_name
, object
->name().c_str(), r_type
);
816 // Scan a relocation for a global symbol.
819 Target_i386::Scan::global(const General_options
& options
,
820 Symbol_table
* symtab
,
823 Sized_relobj
<32, false>* object
,
824 unsigned int data_shndx
,
825 const elfcpp::Rel
<32, false>& reloc
,
831 case elfcpp::R_386_NONE
:
832 case elfcpp::R_386_GNU_VTINHERIT
:
833 case elfcpp::R_386_GNU_VTENTRY
:
836 case elfcpp::R_386_32
:
837 case elfcpp::R_386_PC32
:
838 case elfcpp::R_386_16
:
839 case elfcpp::R_386_PC16
:
840 case elfcpp::R_386_8
:
841 case elfcpp::R_386_PC8
:
842 // FIXME: If we are generating a shared object we may need to
843 // copy this relocation into the object. If this symbol is
844 // defined in a shared object, we may need to copy this
845 // relocation in order to avoid a COPY relocation.
846 gold_assert(!parameters
->output_is_shared());
848 if (gsym
->is_from_dynobj())
850 // This symbol is defined in a dynamic object. If it is a
851 // function, we make a PLT entry. Otherwise we need to
852 // either generate a COPY reloc or copy this reloc.
853 if (gsym
->type() == elfcpp::STT_FUNC
)
855 target
->make_plt_entry(symtab
, layout
, gsym
);
857 // If this is not a PC relative reference, then we may
858 // be taking the address of the function. In that case
859 // we need to set the entry in the dynamic symbol table
860 // to the address of the PLT entry.
861 if (r_type
!= elfcpp::R_386_PC32
862 && r_type
!= elfcpp::R_386_PC16
863 && r_type
!= elfcpp::R_386_PC8
)
864 gsym
->set_needs_dynsym_value();
867 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
873 case elfcpp::R_386_GOT32
:
875 // The symbol requires a GOT entry.
876 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
877 if (got
->add_global(gsym
))
879 // If this symbol is not fully resolved, we need to add a
880 // dynamic relocation for it.
881 if (!gsym
->final_value_is_known())
883 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
884 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
891 case elfcpp::R_386_PLT32
:
892 // If the symbol is fully resolved, this is just a PC32 reloc.
893 // Otherwise we need a PLT entry.
894 if (gsym
->final_value_is_known())
896 target
->make_plt_entry(symtab
, layout
, gsym
);
899 case elfcpp::R_386_GOTOFF
:
900 case elfcpp::R_386_GOTPC
:
901 // We need a GOT section.
902 target
->got_section(symtab
, layout
);
905 case elfcpp::R_386_COPY
:
906 case elfcpp::R_386_GLOB_DAT
:
907 case elfcpp::R_386_JUMP_SLOT
:
908 case elfcpp::R_386_RELATIVE
:
909 // These are outstanding tls relocs, which are unexpected when
911 case elfcpp::R_386_TLS_TPOFF
:
912 case elfcpp::R_386_TLS_DTPMOD32
:
913 case elfcpp::R_386_TLS_DTPOFF32
:
914 case elfcpp::R_386_TLS_TPOFF32
:
915 case elfcpp::R_386_TLS_DESC
:
916 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
917 program_name
, object
->name().c_str(), r_type
);
921 // These are initial tls relocs, which are expected when
923 case elfcpp::R_386_TLS_IE
:
924 case elfcpp::R_386_TLS_GOTIE
:
925 case elfcpp::R_386_TLS_LE
:
926 case elfcpp::R_386_TLS_GD
:
927 case elfcpp::R_386_TLS_LDM
:
928 case elfcpp::R_386_TLS_LDO_32
:
929 case elfcpp::R_386_TLS_IE_32
:
930 case elfcpp::R_386_TLS_LE_32
:
931 case elfcpp::R_386_TLS_GOTDESC
:
932 case elfcpp::R_386_TLS_DESC_CALL
:
934 const bool is_final
= gsym
->final_value_is_known();
935 r_type
= Target_i386::optimize_tls_reloc(is_final
, r_type
);
938 case elfcpp::R_386_TLS_LE
:
939 case elfcpp::R_386_TLS_LE_32
:
940 // FIXME: If generating a shared object, we need to copy
941 // this relocation into the object.
942 gold_assert(!parameters
->output_is_shared());
945 case elfcpp::R_386_TLS_IE
:
946 case elfcpp::R_386_TLS_GOTIE
:
947 case elfcpp::R_386_TLS_GD
:
948 case elfcpp::R_386_TLS_LDM
:
949 case elfcpp::R_386_TLS_LDO_32
:
950 case elfcpp::R_386_TLS_IE_32
:
951 case elfcpp::R_386_TLS_GOTDESC
:
952 case elfcpp::R_386_TLS_DESC_CALL
:
954 _("%s: %s: unsupported reloc %u "
955 "against global symbol %s\n"),
956 program_name
, object
->name().c_str(), r_type
,
963 case elfcpp::R_386_32PLT
:
964 case elfcpp::R_386_TLS_GD_32
:
965 case elfcpp::R_386_TLS_GD_PUSH
:
966 case elfcpp::R_386_TLS_GD_CALL
:
967 case elfcpp::R_386_TLS_GD_POP
:
968 case elfcpp::R_386_TLS_LDM_32
:
969 case elfcpp::R_386_TLS_LDM_PUSH
:
970 case elfcpp::R_386_TLS_LDM_CALL
:
971 case elfcpp::R_386_TLS_LDM_POP
:
972 case elfcpp::R_386_USED_BY_INTEL_200
:
975 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
976 program_name
, object
->name().c_str(), r_type
, gsym
->name());
981 // Scan relocations for a section.
984 Target_i386::scan_relocs(const General_options
& options
,
985 Symbol_table
* symtab
,
987 Sized_relobj
<32, false>* object
,
988 unsigned int data_shndx
,
989 unsigned int sh_type
,
990 const unsigned char* prelocs
,
992 size_t local_symbol_count
,
993 const unsigned char* plocal_symbols
,
994 Symbol
** global_symbols
)
996 if (sh_type
== elfcpp::SHT_RELA
)
998 fprintf(stderr
, _("%s: %s: unsupported RELA reloc section\n"),
999 program_name
, object
->name().c_str());
1003 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1018 // Finalize the sections.
1021 Target_i386::do_finalize_sections(Layout
* layout
)
1023 // Fill in some more dynamic tags.
1024 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1027 if (this->got_plt_
!= NULL
)
1028 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1030 if (this->plt_
!= NULL
)
1032 const Output_data
* od
= this->plt_
->rel_plt();
1033 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1034 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1035 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1038 if (this->rel_dyn_
!= NULL
)
1040 const Output_data
* od
= this->rel_dyn_
;
1041 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1042 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1043 odyn
->add_constant(elfcpp::DT_RELENT
,
1044 elfcpp::Elf_sizes
<32>::rel_size
);
1047 if (!parameters
->output_is_shared())
1049 // The value of the DT_DEBUG tag is filled in by the dynamic
1050 // linker at run time, and used by the debugger.
1051 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1055 // Emit any relocs we saved in an attempt to avoid generating COPY
1057 if (this->copy_relocs_
== NULL
)
1059 if (this->copy_relocs_
->any_to_emit())
1061 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1062 this->copy_relocs_
->emit(rel_dyn
);
1064 delete this->copy_relocs_
;
1065 this->copy_relocs_
= NULL
;
1068 // Perform a relocation.
1071 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1072 Target_i386
* target
,
1074 const elfcpp::Rel
<32, false>& rel
,
1075 unsigned int r_type
,
1076 const Sized_symbol
<32>* gsym
,
1077 const Symbol_value
<32>* psymval
,
1078 unsigned char* view
,
1079 elfcpp::Elf_types
<32>::Elf_Addr address
,
1082 if (this->skip_call_tls_get_addr_
)
1084 if (r_type
!= elfcpp::R_386_PLT32
1086 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1088 fprintf(stderr
, _("%s: %s: missing expected TLS relocation\n"),
1090 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1094 this->skip_call_tls_get_addr_
= false;
1099 // Pick the value to use for symbols defined in shared objects.
1100 Symbol_value
<32> symval
;
1101 if (gsym
!= NULL
&& gsym
->is_from_dynobj() && gsym
->has_plt_offset())
1103 symval
.set_output_value(target
->plt_section()->address()
1104 + gsym
->plt_offset());
1108 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1112 case elfcpp::R_386_NONE
:
1113 case elfcpp::R_386_GNU_VTINHERIT
:
1114 case elfcpp::R_386_GNU_VTENTRY
:
1117 case elfcpp::R_386_32
:
1118 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1121 case elfcpp::R_386_PC32
:
1122 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1125 case elfcpp::R_386_16
:
1126 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1129 case elfcpp::R_386_PC16
:
1130 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1133 case elfcpp::R_386_8
:
1134 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1137 case elfcpp::R_386_PC8
:
1138 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1141 case elfcpp::R_386_PLT32
:
1142 gold_assert(gsym
->has_plt_offset()
1143 || gsym
->final_value_is_known());
1144 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1147 case elfcpp::R_386_GOT32
:
1148 // Local GOT offsets not yet supported.
1150 gold_assert(gsym
->has_got_offset());
1151 Relocate_functions
<32, false>::rel32(view
, gsym
->got_offset());
1154 case elfcpp::R_386_GOTOFF
:
1156 elfcpp::Elf_types
<32>::Elf_Addr value
;
1157 value
= (psymval
->value(object
, 0)
1158 - target
->got_section(NULL
, NULL
)->address());
1159 Relocate_functions
<32, false>::rel32(view
, value
);
1163 case elfcpp::R_386_GOTPC
:
1165 elfcpp::Elf_types
<32>::Elf_Addr value
;
1166 value
= target
->got_section(NULL
, NULL
)->address();
1167 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1171 case elfcpp::R_386_COPY
:
1172 case elfcpp::R_386_GLOB_DAT
:
1173 case elfcpp::R_386_JUMP_SLOT
:
1174 case elfcpp::R_386_RELATIVE
:
1175 // These are outstanding tls relocs, which are unexpected when
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 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
1184 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1189 // These are initial tls relocs, which are expected when
1191 case elfcpp::R_386_TLS_IE
:
1192 case elfcpp::R_386_TLS_GOTIE
:
1193 case elfcpp::R_386_TLS_LE
:
1194 case elfcpp::R_386_TLS_GD
:
1195 case elfcpp::R_386_TLS_LDM
:
1196 case elfcpp::R_386_TLS_LDO_32
:
1197 case elfcpp::R_386_TLS_IE_32
:
1198 case elfcpp::R_386_TLS_LE_32
:
1199 case elfcpp::R_386_TLS_GOTDESC
:
1200 case elfcpp::R_386_TLS_DESC_CALL
:
1201 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1202 address
, view_size
);
1205 case elfcpp::R_386_32PLT
:
1206 case elfcpp::R_386_TLS_GD_32
:
1207 case elfcpp::R_386_TLS_GD_PUSH
:
1208 case elfcpp::R_386_TLS_GD_CALL
:
1209 case elfcpp::R_386_TLS_GD_POP
:
1210 case elfcpp::R_386_TLS_LDM_32
:
1211 case elfcpp::R_386_TLS_LDM_PUSH
:
1212 case elfcpp::R_386_TLS_LDM_CALL
:
1213 case elfcpp::R_386_TLS_LDM_POP
:
1214 case elfcpp::R_386_USED_BY_INTEL_200
:
1216 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1218 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1220 // gold_exit(false);
1227 // Perform a TLS relocation.
1230 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1232 const elfcpp::Rel
<32, false>& rel
,
1233 unsigned int r_type
,
1234 const Sized_symbol
<32>* gsym
,
1235 const Symbol_value
<32>* psymval
,
1236 unsigned char* view
,
1237 elfcpp::Elf_types
<32>::Elf_Addr
,
1240 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1241 if (tls_segment
== NULL
)
1243 fprintf(stderr
, _("%s: %s: TLS reloc but no TLS segment\n"),
1245 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1249 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1251 const bool is_final
= (gsym
== NULL
1252 ? !parameters
->output_is_shared()
1253 : gsym
->final_value_is_known());
1254 const unsigned int opt_r_type
=
1255 Target_i386::optimize_tls_reloc(is_final
, r_type
);
1258 case elfcpp::R_386_TLS_LE_32
:
1259 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1260 Relocate_functions
<32, false>::rel32(view
, value
);
1263 case elfcpp::R_386_TLS_LE
:
1264 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1265 Relocate_functions
<32, false>::rel32(view
, value
);
1268 case elfcpp::R_386_TLS_IE
:
1269 case elfcpp::R_386_TLS_GOTIE
:
1270 case elfcpp::R_386_TLS_IE_32
:
1271 if (opt_r_type
== elfcpp::R_386_TLS_LE_32
)
1273 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1274 rel
, r_type
, value
, view
,
1278 fprintf(stderr
, _("%s: %s: unsupported reloc type %u\n"),
1280 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1282 // gold_exit(false);
1285 case elfcpp::R_386_TLS_GD
:
1286 if (opt_r_type
== elfcpp::R_386_TLS_LE_32
)
1288 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1289 rel
, r_type
, value
, view
,
1293 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1295 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1297 // gold_exit(false);
1300 case elfcpp::R_386_TLS_LDM
:
1301 case elfcpp::R_386_TLS_LDO_32
:
1302 case elfcpp::R_386_TLS_GOTDESC
:
1303 case elfcpp::R_386_TLS_DESC_CALL
:
1304 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
1306 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
1308 // gold_exit(false);
1313 // Do a relocation in which we convert a TLS Initial-Exec to a
1317 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1319 Output_segment
* tls_segment
,
1320 const elfcpp::Rel
<32, false>& rel
,
1321 unsigned int r_type
,
1322 elfcpp::Elf_types
<32>::Elf_Addr value
,
1323 unsigned char* view
,
1326 // We have to actually change the instructions, which means that we
1327 // need to examine the opcodes to figure out which instruction we
1329 if (r_type
== elfcpp::R_386_TLS_IE
)
1331 // movl %gs:XX,%eax ==> movl $YY,%eax
1332 // movl %gs:XX,%reg ==> movl $YY,%reg
1333 // addl %gs:XX,%reg ==> addl $YY,%reg
1334 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -1);
1335 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
1337 unsigned char op1
= view
[-1];
1340 // movl XX,%eax ==> movl $YY,%eax
1345 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
,
1348 unsigned char op2
= view
[-2];
1351 // movl XX,%reg ==> movl $YY,%reg
1352 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1353 (op1
& 0xc7) == 0x05);
1355 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1357 else if (op2
== 0x03)
1359 // addl XX,%reg ==> addl $YY,%reg
1360 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1361 (op1
& 0xc7) == 0x05);
1363 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1366 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
1371 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1372 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1373 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1374 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
1375 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
1377 unsigned char op1
= view
[-1];
1378 unsigned char op2
= view
[-2];
1379 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1380 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1383 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1385 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1387 else if (op2
== 0x2b)
1389 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1391 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1393 else if (op2
== 0x03)
1395 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1397 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1400 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
1403 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1404 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1407 Relocate_functions
<32, false>::rel32(view
, value
);
1410 // Do a relocation in which we convert a TLS Global-Dynamic to a
1414 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1416 Output_segment
* tls_segment
,
1417 const elfcpp::Rel
<32, false>& rel
,
1419 elfcpp::Elf_types
<32>::Elf_Addr value
,
1420 unsigned char* view
,
1423 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1424 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1425 // leal foo(%reg),%eax; call ___tls_get_addr
1426 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1428 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
1429 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 9);
1431 unsigned char op1
= view
[-1];
1432 unsigned char op2
= view
[-2];
1434 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1435 op2
== 0x8d || op2
== 0x04);
1436 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1443 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -3);
1444 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1446 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1447 ((op1
& 0xc7) == 0x05
1448 && op1
!= (4 << 3)));
1449 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1453 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
1454 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1455 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1458 // There is a trailing nop. Use the size byte subl.
1459 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1464 // Use the five byte subl.
1465 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1469 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1470 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1472 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1474 this->skip_call_tls_get_addr_
= true;
1477 // Check the range for a TLS relocation.
1480 Target_i386::Relocate::check_range(const Relocate_info
<32, false>* relinfo
,
1482 const elfcpp::Rel
<32, false>& rel
,
1483 off_t view_size
, off_t off
)
1485 off_t offset
= rel
.get_r_offset() + off
;
1486 if (offset
< 0 || offset
> view_size
)
1488 fprintf(stderr
, _("%s: %s: TLS relocation out of range\n"),
1490 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1495 // Check the validity of a TLS relocation. This is like assert.
1498 Target_i386::Relocate::check_tls(const Relocate_info
<32, false>* relinfo
,
1500 const elfcpp::Rel
<32, false>& rel
,
1506 _("%s: %s: TLS relocation against invalid instruction\n"),
1508 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
1513 // Relocate section data.
1516 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1517 unsigned int sh_type
,
1518 const unsigned char* prelocs
,
1520 unsigned char* view
,
1521 elfcpp::Elf_types
<32>::Elf_Addr address
,
1524 gold_assert(sh_type
== elfcpp::SHT_REL
);
1526 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1527 Target_i386::Relocate
>(
1537 // Return the value to use for a dynamic which requires special
1538 // treatment. This is how we support equality comparisons of function
1539 // pointers across shared library boundaries, as described in the
1540 // processor specific ABI supplement.
1543 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1545 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1546 return this->plt_section()->address() + gsym
->plt_offset();
1549 // Return a string used to fill a code section with nops to take up
1550 // the specified length.
1553 Target_i386::do_code_fill(off_t length
)
1557 // Build a jmp instruction to skip over the bytes.
1558 unsigned char jmp
[5];
1560 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1561 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1562 + std::string(length
- 5, '\0'));
1565 // Nop sequences of various lengths.
1566 const char nop1
[1] = { 0x90 }; // nop
1567 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1568 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1569 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1570 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1571 0x00 }; // leal 0(%esi,1),%esi
1572 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1574 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1576 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1577 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1578 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1579 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1581 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1582 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1584 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1585 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1587 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1588 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1589 0x00, 0x00, 0x00, 0x00 };
1590 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1591 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1592 0x27, 0x00, 0x00, 0x00,
1594 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1595 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1596 0xbc, 0x27, 0x00, 0x00,
1598 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1599 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1600 0x90, 0x90, 0x90, 0x90,
1603 const char* nops
[16] = {
1605 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1606 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1609 return std::string(nops
[length
], length
);
1612 // The selector for i386 object files.
1614 class Target_selector_i386
: public Target_selector
1617 Target_selector_i386()
1618 : Target_selector(elfcpp::EM_386
, 32, false)
1622 recognize(int machine
, int osabi
, int abiversion
);
1625 Target_i386
* target_
;
1628 // Recognize an i386 object file when we already know that the machine
1629 // number is EM_386.
1632 Target_selector_i386::recognize(int, int, int)
1634 if (this->target_
== NULL
)
1635 this->target_
= new Target_i386();
1636 return this->target_
;
1639 Target_selector_i386 target_selector_i386
;
1641 } // End anonymous namespace.