1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_i386
;
47 // The i386 target class.
48 // TLS info comes from
49 // http://people.redhat.com/drepper/tls.pdf
50 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52 class Target_i386
: public Sized_target
<32, false>
55 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
58 : Sized_target
<32, false>(&i386_info
),
59 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
60 copy_relocs_(NULL
), dynbss_(NULL
)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options
& options
,
68 Sized_relobj
<32, false>* object
,
69 unsigned int data_shndx
,
71 const unsigned char* prelocs
,
73 size_t local_symbol_count
,
74 const unsigned char* plocal_symbols
,
75 Symbol
** global_symbols
);
77 // Finalize the sections.
79 do_finalize_sections(Layout
*);
81 // Return the value to use for a dynamic which requires special
84 do_dynsym_value(const Symbol
*) const;
86 // Relocate a section.
88 relocate_section(const Relocate_info
<32, false>*,
90 const unsigned char* prelocs
,
93 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
96 // Return a string used to fill a code section with nops.
98 do_code_fill(off_t length
);
101 // The class which scans relocations.
105 local(const General_options
& options
, Symbol_table
* symtab
,
106 Layout
* layout
, Target_i386
* target
,
107 Sized_relobj
<32, false>* object
,
108 unsigned int data_shndx
,
109 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
110 const elfcpp::Sym
<32, false>& lsym
);
113 global(const General_options
& options
, Symbol_table
* symtab
,
114 Layout
* layout
, Target_i386
* target
,
115 Sized_relobj
<32, false>* object
,
116 unsigned int data_shndx
,
117 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
121 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
124 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
128 // The class which implements relocation.
133 : skip_call_tls_get_addr_(false),
134 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
139 if (this->skip_call_tls_get_addr_
)
141 // FIXME: This needs to specify the location somehow.
142 gold_error(_("missing expected TLS relocation"));
146 // Do a relocation. Return false if the caller should not issue
147 // any warnings about this relocation.
149 relocate(const Relocate_info
<32, false>*, Target_i386
*, 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
,
157 // Do a TLS relocation.
159 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
160 const elfcpp::Rel
<32, false>&,
161 unsigned int r_type
, const Sized_symbol
<32>*,
162 const Symbol_value
<32>*,
163 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
165 // Do a TLS Initial-Exec to Local-Exec transition.
167 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
168 Output_segment
* tls_segment
,
169 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
170 elfcpp::Elf_types
<32>::Elf_Addr value
,
174 // Do a TLS General-Dynamic to Local-Exec transition.
176 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
177 Output_segment
* tls_segment
,
178 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
179 elfcpp::Elf_types
<32>::Elf_Addr value
,
183 // Do a TLS Local-Dynamic to Local-Exec transition.
185 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
186 Output_segment
* tls_segment
,
187 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
188 elfcpp::Elf_types
<32>::Elf_Addr value
,
192 // We need to keep track of which type of local dynamic relocation
193 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
194 enum Local_dynamic_type
201 // This is set if we should skip the next reloc, which should be a
202 // PLT32 reloc against ___tls_get_addr.
203 bool skip_call_tls_get_addr_
;
204 // The type of local dynamic relocation we have seen in the section
205 // being relocated, if any.
206 Local_dynamic_type local_dynamic_type_
;
209 // Adjust TLS relocation type based on the options and whether this
210 // is a local symbol.
211 static tls::Tls_optimization
212 optimize_tls_reloc(bool is_final
, int r_type
);
214 // Get the GOT section, creating it if necessary.
215 Output_data_got
<32, false>*
216 got_section(Symbol_table
*, Layout
*);
218 // Create a PLT entry for a global symbol.
220 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
222 // Get the PLT section.
223 const Output_data_plt_i386
*
226 gold_assert(this->plt_
!= NULL
);
230 // Get the dynamic reloc section, creating it if necessary.
232 rel_dyn_section(Layout
*);
234 // Copy a relocation against a global symbol.
236 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
237 Sized_relobj
<32, false>*, unsigned int,
238 Symbol
*, const elfcpp::Rel
<32, false>&);
240 // Information about this specific target which we pass to the
241 // general Target structure.
242 static const Target::Target_info i386_info
;
245 Output_data_got
<32, false>* got_
;
247 Output_data_plt_i386
* plt_
;
248 // The GOT PLT section.
249 Output_data_space
* got_plt_
;
250 // The dynamic reloc section.
251 Reloc_section
* rel_dyn_
;
252 // Relocs saved to avoid a COPY reloc.
253 Copy_relocs
<32, false>* copy_relocs_
;
254 // Space for variables copied with a COPY reloc.
255 Output_data_space
* dynbss_
;
258 const Target::Target_info
Target_i386::i386_info
=
261 false, // is_big_endian
262 elfcpp::EM_386
, // machine_code
263 false, // has_make_symbol
264 false, // has_resolve
265 true, // has_code_fill
266 "/usr/lib/libc.so.1", // dynamic_linker
267 0x08048000, // default_text_segment_address
268 0x1000, // abi_pagesize
269 0x1000 // common_pagesize
272 // Get the GOT section, creating it if necessary.
274 Output_data_got
<32, false>*
275 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
277 if (this->got_
== NULL
)
279 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
281 this->got_
= new Output_data_got
<32, false>();
283 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
284 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
287 // The old GNU linker creates a .got.plt section. We just
288 // create another set of data in the .got section. Note that we
289 // always create a PLT if we create a GOT, although the PLT
291 this->got_plt_
= new Output_data_space(4);
292 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
293 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
296 // The first three entries are reserved.
297 this->got_plt_
->set_space_size(3 * 4);
299 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
300 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
302 0, 0, elfcpp::STT_OBJECT
,
304 elfcpp::STV_HIDDEN
, 0,
311 // Get the dynamic reloc section, creating it if necessary.
313 Target_i386::Reloc_section
*
314 Target_i386::rel_dyn_section(Layout
* layout
)
316 if (this->rel_dyn_
== NULL
)
318 gold_assert(layout
!= NULL
);
319 this->rel_dyn_
= new Reloc_section();
320 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
321 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
323 return this->rel_dyn_
;
326 // A class to handle the PLT data.
328 class Output_data_plt_i386
: public Output_section_data
331 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
333 Output_data_plt_i386(Layout
*, Output_data_space
*);
335 // Add an entry to the PLT.
337 add_entry(Symbol
* gsym
);
339 // Return the .rel.plt section data.
342 { return this->rel_
; }
346 do_adjust_output_section(Output_section
* os
);
349 // The size of an entry in the PLT.
350 static const int plt_entry_size
= 16;
352 // The first entry in the PLT for an executable.
353 static unsigned char exec_first_plt_entry
[plt_entry_size
];
355 // The first entry in the PLT for a shared object.
356 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
358 // Other entries in the PLT for an executable.
359 static unsigned char exec_plt_entry
[plt_entry_size
];
361 // Other entries in the PLT for a shared object.
362 static unsigned char dyn_plt_entry
[plt_entry_size
];
364 // Set the final size.
366 do_set_address(uint64_t, off_t
)
367 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
369 // Write out the PLT data.
371 do_write(Output_file
*);
373 // The reloc section.
375 // The .got.plt section.
376 Output_data_space
* got_plt_
;
377 // The number of PLT entries.
381 // Create the PLT section. The ordinary .got section is an argument,
382 // since we need to refer to the start. We also create our own .got
383 // section just for PLT entries.
385 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
386 Output_data_space
* got_plt
)
387 : Output_section_data(4), got_plt_(got_plt
), count_(0)
389 this->rel_
= new Reloc_section();
390 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
391 elfcpp::SHF_ALLOC
, this->rel_
);
395 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
397 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
398 // linker, and so do we.
402 // Add an entry to the PLT.
405 Output_data_plt_i386::add_entry(Symbol
* gsym
)
407 gold_assert(!gsym
->has_plt_offset());
409 // Note that when setting the PLT offset we skip the initial
410 // reserved PLT entry.
411 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
415 off_t got_offset
= this->got_plt_
->data_size();
417 // Every PLT entry needs a GOT entry which points back to the PLT
418 // entry (this will be changed by the dynamic linker, normally
419 // lazily when the function is called).
420 this->got_plt_
->set_space_size(got_offset
+ 4);
422 // Every PLT entry needs a reloc.
423 gsym
->set_needs_dynsym_entry();
424 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
427 // Note that we don't need to save the symbol. The contents of the
428 // PLT are independent of which symbols are used. The symbols only
429 // appear in the relocations.
432 // The first entry in the PLT for an executable.
434 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
436 0xff, 0x35, // pushl contents of memory address
437 0, 0, 0, 0, // replaced with address of .got + 4
438 0xff, 0x25, // jmp indirect
439 0, 0, 0, 0, // replaced with address of .got + 8
443 // The first entry in the PLT for a shared object.
445 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
447 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
448 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
452 // Subsequent entries in the PLT for an executable.
454 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
456 0xff, 0x25, // jmp indirect
457 0, 0, 0, 0, // replaced with address of symbol in .got
458 0x68, // pushl immediate
459 0, 0, 0, 0, // replaced with offset into relocation table
460 0xe9, // jmp relative
461 0, 0, 0, 0 // replaced with offset to start of .plt
464 // Subsequent entries in the PLT for a shared object.
466 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
468 0xff, 0xa3, // jmp *offset(%ebx)
469 0, 0, 0, 0, // replaced with offset of symbol in .got
470 0x68, // pushl immediate
471 0, 0, 0, 0, // replaced with offset into relocation table
472 0xe9, // jmp relative
473 0, 0, 0, 0 // replaced with offset to start of .plt
476 // Write out the PLT. This uses the hand-coded instructions above,
477 // and adjusts them as needed. This is all specified by the i386 ELF
478 // Processor Supplement.
481 Output_data_plt_i386::do_write(Output_file
* of
)
483 const off_t offset
= this->offset();
484 const off_t oview_size
= this->data_size();
485 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
487 const off_t got_file_offset
= this->got_plt_
->offset();
488 const off_t got_size
= this->got_plt_
->data_size();
489 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
492 unsigned char* pov
= oview
;
494 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
495 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
497 if (parameters
->output_is_shared())
498 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
501 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
502 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
503 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
505 pov
+= plt_entry_size
;
507 unsigned char* got_pov
= got_view
;
509 memset(got_pov
, 0, 12);
512 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
514 unsigned int plt_offset
= plt_entry_size
;
515 unsigned int plt_rel_offset
= 0;
516 unsigned int got_offset
= 12;
517 const unsigned int count
= this->count_
;
518 for (unsigned int i
= 0;
521 pov
+= plt_entry_size
,
523 plt_offset
+= plt_entry_size
,
524 plt_rel_offset
+= rel_size
,
527 // Set and adjust the PLT entry itself.
529 if (parameters
->output_is_shared())
531 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
532 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
536 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
537 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
542 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
543 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
544 - (plt_offset
+ plt_entry_size
));
546 // Set the entry in the GOT.
547 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
550 gold_assert(pov
- oview
== oview_size
);
551 gold_assert(got_pov
- got_view
== got_size
);
553 of
->write_output_view(offset
, oview_size
, oview
);
554 of
->write_output_view(got_file_offset
, got_size
, got_view
);
557 // Create a PLT entry for a global symbol.
560 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
562 if (gsym
->has_plt_offset())
565 if (this->plt_
== NULL
)
567 // Create the GOT sections first.
568 this->got_section(symtab
, layout
);
570 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
571 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
573 | elfcpp::SHF_EXECINSTR
),
577 this->plt_
->add_entry(gsym
);
580 // Handle a relocation against a non-function symbol defined in a
581 // dynamic object. The traditional way to handle this is to generate
582 // a COPY relocation to copy the variable at runtime from the shared
583 // object into the executable's data segment. However, this is
584 // undesirable in general, as if the size of the object changes in the
585 // dynamic object, the executable will no longer work correctly. If
586 // this relocation is in a writable section, then we can create a
587 // dynamic reloc and the dynamic linker will resolve it to the correct
588 // address at runtime. However, we do not want do that if the
589 // relocation is in a read-only section, as it would prevent the
590 // readonly segment from being shared. And if we have to eventually
591 // generate a COPY reloc, then any dynamic relocations will be
592 // useless. So this means that if this is a writable section, we need
593 // to save the relocation until we see whether we have to create a
594 // COPY relocation for this symbol for any other relocation.
597 Target_i386::copy_reloc(const General_options
* options
,
598 Symbol_table
* symtab
,
600 Sized_relobj
<32, false>* object
,
601 unsigned int data_shndx
, Symbol
* gsym
,
602 const elfcpp::Rel
<32, false>& rel
)
604 Sized_symbol
<32>* ssym
;
605 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
608 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
611 // So far we do not need a COPY reloc. Save this relocation.
612 // If it turns out that we never need a COPY reloc for this
613 // symbol, then we will emit the relocation.
614 if (this->copy_relocs_
== NULL
)
615 this->copy_relocs_
= new Copy_relocs
<32, false>();
616 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
620 // Allocate space for this symbol in the .bss section.
622 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
624 // There is no defined way to determine the required alignment
625 // of the symbol. We pick the alignment based on the size. We
626 // set an arbitrary maximum of 256.
628 for (align
= 1; align
< 512; align
<<= 1)
629 if ((symsize
& align
) != 0)
632 if (this->dynbss_
== NULL
)
634 this->dynbss_
= new Output_data_space(align
);
635 layout
->add_output_section_data(".bss",
638 | elfcpp::SHF_WRITE
),
642 Output_data_space
* dynbss
= this->dynbss_
;
644 if (align
> dynbss
->addralign())
645 dynbss
->set_space_alignment(align
);
647 off_t dynbss_size
= dynbss
->data_size();
648 dynbss_size
= align_address(dynbss_size
, align
);
649 off_t offset
= dynbss_size
;
650 dynbss
->set_space_size(dynbss_size
+ symsize
);
652 // Define the symbol in the .dynbss section.
653 symtab
->define_in_output_data(this, ssym
->name(), ssym
->version(),
654 dynbss
, offset
, symsize
, ssym
->type(),
655 ssym
->binding(), ssym
->visibility(),
656 ssym
->nonvis(), false, false);
658 // Add the COPY reloc.
659 ssym
->set_needs_dynsym_entry();
660 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
661 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
665 // Optimize the TLS relocation type based on what we know about the
666 // symbol. IS_FINAL is true if the final address of this symbol is
667 // known at link time.
669 tls::Tls_optimization
670 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
672 // If we are generating a shared library, then we can't do anything
674 if (parameters
->output_is_shared())
675 return tls::TLSOPT_NONE
;
679 case elfcpp::R_386_TLS_GD
:
680 case elfcpp::R_386_TLS_GOTDESC
:
681 case elfcpp::R_386_TLS_DESC_CALL
:
682 // These are General-Dynamic which permits fully general TLS
683 // access. Since we know that we are generating an executable,
684 // we can convert this to Initial-Exec. If we also know that
685 // this is a local symbol, we can further switch to Local-Exec.
687 return tls::TLSOPT_TO_LE
;
688 return tls::TLSOPT_TO_IE
;
690 case elfcpp::R_386_TLS_LDM
:
691 // This is Local-Dynamic, which refers to a local symbol in the
692 // dynamic TLS block. Since we know that we generating an
693 // executable, we can switch to Local-Exec.
694 return tls::TLSOPT_TO_LE
;
696 case elfcpp::R_386_TLS_LDO_32
:
697 // Another type of Local-Dynamic relocation.
698 return tls::TLSOPT_TO_LE
;
700 case elfcpp::R_386_TLS_IE
:
701 case elfcpp::R_386_TLS_GOTIE
:
702 case elfcpp::R_386_TLS_IE_32
:
703 // These are Initial-Exec relocs which get the thread offset
704 // from the GOT. If we know that we are linking against the
705 // local symbol, we can switch to Local-Exec, which links the
706 // thread offset into the instruction.
708 return tls::TLSOPT_TO_LE
;
709 return tls::TLSOPT_NONE
;
711 case elfcpp::R_386_TLS_LE
:
712 case elfcpp::R_386_TLS_LE_32
:
713 // When we already have Local-Exec, there is nothing further we
715 return tls::TLSOPT_NONE
;
722 // Report an unsupported relocation against a local symbol.
725 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
728 gold_error(_("%s: unsupported reloc %u against local symbol"),
729 object
->name().c_str(), r_type
);
732 // Scan a relocation for a local symbol.
735 Target_i386::Scan::local(const General_options
&,
736 Symbol_table
* symtab
,
739 Sized_relobj
<32, false>* object
,
740 unsigned int data_shndx
,
741 const elfcpp::Rel
<32, false>& reloc
,
743 const elfcpp::Sym
<32, false>&)
747 case elfcpp::R_386_NONE
:
748 case elfcpp::R_386_GNU_VTINHERIT
:
749 case elfcpp::R_386_GNU_VTENTRY
:
752 case elfcpp::R_386_32
:
753 case elfcpp::R_386_16
:
754 case elfcpp::R_386_8
:
755 // If building a shared library (or a position-independent
756 // executable), we need to create a dynamic relocation for
757 // this location. The relocation applied at link time will
758 // apply the link-time value, so we flag the location with
759 // an R_386_RELATIVE relocation so the dynamic loader can
760 // relocate it easily.
761 if (parameters
->output_is_position_independent())
763 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
764 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
765 reloc
.get_r_offset());
769 case elfcpp::R_386_PC32
:
770 case elfcpp::R_386_PC16
:
771 case elfcpp::R_386_PC8
:
774 case elfcpp::R_386_PLT32
:
775 // Since we know this is a local symbol, we can handle this as a
779 case elfcpp::R_386_GOTOFF
:
780 case elfcpp::R_386_GOTPC
:
781 // We need a GOT section.
782 target
->got_section(symtab
, layout
);
785 case elfcpp::R_386_GOT32
:
787 // The symbol requires a GOT entry.
788 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
789 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
790 if (got
->add_local(object
, r_sym
))
792 // If we are generating a shared object, we need to add a
793 // dynamic RELATIVE relocation for this symbol.
794 if (parameters
->output_is_position_independent())
796 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
797 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
798 data_shndx
, reloc
.get_r_offset());
804 // These are relocations which should only be seen by the
805 // dynamic linker, and should never be seen here.
806 case elfcpp::R_386_COPY
:
807 case elfcpp::R_386_GLOB_DAT
:
808 case elfcpp::R_386_JUMP_SLOT
:
809 case elfcpp::R_386_RELATIVE
:
810 case elfcpp::R_386_TLS_TPOFF
:
811 case elfcpp::R_386_TLS_DTPMOD32
:
812 case elfcpp::R_386_TLS_DTPOFF32
:
813 case elfcpp::R_386_TLS_TPOFF32
:
814 case elfcpp::R_386_TLS_DESC
:
815 gold_error(_("%s: unexpected reloc %u in object file"),
816 object
->name().c_str(), r_type
);
819 // These are initial TLS relocs, which are expected when
821 case elfcpp::R_386_TLS_IE
:
822 case elfcpp::R_386_TLS_GOTIE
:
823 case elfcpp::R_386_TLS_LE
:
824 case elfcpp::R_386_TLS_GD
:
825 case elfcpp::R_386_TLS_LDM
:
826 case elfcpp::R_386_TLS_LDO_32
:
827 case elfcpp::R_386_TLS_IE_32
:
828 case elfcpp::R_386_TLS_LE_32
:
829 case elfcpp::R_386_TLS_GOTDESC
:
830 case elfcpp::R_386_TLS_DESC_CALL
:
832 bool output_is_shared
= parameters
->output_is_shared();
833 const tls::Tls_optimization optimized_type
834 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
837 case elfcpp::R_386_TLS_LE
:
838 case elfcpp::R_386_TLS_LE_32
:
839 // FIXME: If generating a shared object, we need to copy
840 // this relocation into the object.
841 gold_assert(!output_is_shared
);
844 case elfcpp::R_386_TLS_IE
:
845 case elfcpp::R_386_TLS_IE_32
:
846 case elfcpp::R_386_TLS_GOTIE
:
847 // FIXME: If not relaxing to LE, we need to generate a
848 // TPOFF or TPOFF32 reloc.
849 if (optimized_type
!= tls::TLSOPT_TO_LE
)
850 unsupported_reloc_local(object
, r_type
);
853 case elfcpp::R_386_TLS_LDM
:
854 // FIXME: If not relaxing to LE, we need to generate a
856 if (optimized_type
!= tls::TLSOPT_TO_LE
)
857 unsupported_reloc_local(object
, r_type
);
860 case elfcpp::R_386_TLS_LDO_32
:
863 case elfcpp::R_386_TLS_GD
:
864 case elfcpp::R_386_TLS_GOTDESC
:
865 case elfcpp::R_386_TLS_DESC_CALL
:
866 // FIXME: If not relaxing to LE, we need to generate
867 // DTPMOD32 and DTPOFF32 relocs.
868 if (optimized_type
!= tls::TLSOPT_TO_LE
)
869 unsupported_reloc_local(object
, r_type
);
878 case elfcpp::R_386_32PLT
:
879 case elfcpp::R_386_TLS_GD_32
:
880 case elfcpp::R_386_TLS_GD_PUSH
:
881 case elfcpp::R_386_TLS_GD_CALL
:
882 case elfcpp::R_386_TLS_GD_POP
:
883 case elfcpp::R_386_TLS_LDM_32
:
884 case elfcpp::R_386_TLS_LDM_PUSH
:
885 case elfcpp::R_386_TLS_LDM_CALL
:
886 case elfcpp::R_386_TLS_LDM_POP
:
887 case elfcpp::R_386_USED_BY_INTEL_200
:
889 unsupported_reloc_local(object
, r_type
);
894 // Report an unsupported relocation against a global symbol.
897 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
901 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
902 object
->name().c_str(), r_type
, gsym
->name());
905 // Scan a relocation for a global symbol.
908 Target_i386::Scan::global(const General_options
& options
,
909 Symbol_table
* symtab
,
912 Sized_relobj
<32, false>* object
,
913 unsigned int data_shndx
,
914 const elfcpp::Rel
<32, false>& reloc
,
920 case elfcpp::R_386_NONE
:
921 case elfcpp::R_386_GNU_VTINHERIT
:
922 case elfcpp::R_386_GNU_VTENTRY
:
925 case elfcpp::R_386_32
:
926 case elfcpp::R_386_PC32
:
927 case elfcpp::R_386_16
:
928 case elfcpp::R_386_PC16
:
929 case elfcpp::R_386_8
:
930 case elfcpp::R_386_PC8
:
931 if (gsym
->is_from_dynobj()
932 || (parameters
->output_is_shared()
933 && gsym
->is_preemptible()))
935 // (a) This symbol is defined in a dynamic object. If it is a
936 // function, we make a PLT entry. Otherwise we need to
937 // either generate a COPY reloc or copy this reloc.
938 // (b) We are building a shared object and this symbol is
939 // preemptible. If it is a function, we make a PLT entry.
940 // Otherwise, we copy the reloc. We do not make COPY relocs
941 // in shared objects.
942 if (gsym
->type() == elfcpp::STT_FUNC
)
944 target
->make_plt_entry(symtab
, layout
, gsym
);
946 // If this is not a PC relative reference, then we may
947 // be taking the address of the function. In that case
948 // we need to set the entry in the dynamic symbol table
949 // to the address of the PLT entry.
950 if (r_type
!= elfcpp::R_386_PC32
951 && r_type
!= elfcpp::R_386_PC16
952 && r_type
!= elfcpp::R_386_PC8
953 && gsym
->is_from_dynobj())
954 gsym
->set_needs_dynsym_value();
956 else if (parameters
->output_is_shared())
958 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
959 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
960 reloc
.get_r_offset());
963 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
969 case elfcpp::R_386_GOT32
:
971 // The symbol requires a GOT entry.
972 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
973 if (got
->add_global(gsym
))
975 // If this symbol is not fully resolved, we need to add a
976 // dynamic relocation for it.
977 if (!gsym
->final_value_is_known())
979 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
980 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
987 case elfcpp::R_386_PLT32
:
988 // If the symbol is fully resolved, this is just a PC32 reloc.
989 // Otherwise we need a PLT entry.
990 if (gsym
->final_value_is_known())
992 // If building a shared library, we can also skip the PLT entry
993 // if the symbol is defined in the output file and is protected
995 if (gsym
->is_defined()
996 && !gsym
->is_from_dynobj()
997 && !gsym
->is_preemptible())
999 target
->make_plt_entry(symtab
, layout
, gsym
);
1002 case elfcpp::R_386_GOTOFF
:
1003 case elfcpp::R_386_GOTPC
:
1004 // We need a GOT section.
1005 target
->got_section(symtab
, layout
);
1008 // These are relocations which should only be seen by the
1009 // dynamic linker, and should never be seen here.
1010 case elfcpp::R_386_COPY
:
1011 case elfcpp::R_386_GLOB_DAT
:
1012 case elfcpp::R_386_JUMP_SLOT
:
1013 case elfcpp::R_386_RELATIVE
:
1014 case elfcpp::R_386_TLS_TPOFF
:
1015 case elfcpp::R_386_TLS_DTPMOD32
:
1016 case elfcpp::R_386_TLS_DTPOFF32
:
1017 case elfcpp::R_386_TLS_TPOFF32
:
1018 case elfcpp::R_386_TLS_DESC
:
1019 gold_error(_("%s: unexpected reloc %u in object file"),
1020 object
->name().c_str(), r_type
);
1023 // These are initial tls relocs, which are expected when
1025 case elfcpp::R_386_TLS_IE
:
1026 case elfcpp::R_386_TLS_GOTIE
:
1027 case elfcpp::R_386_TLS_LE
:
1028 case elfcpp::R_386_TLS_GD
:
1029 case elfcpp::R_386_TLS_LDM
:
1030 case elfcpp::R_386_TLS_LDO_32
:
1031 case elfcpp::R_386_TLS_IE_32
:
1032 case elfcpp::R_386_TLS_LE_32
:
1033 case elfcpp::R_386_TLS_GOTDESC
:
1034 case elfcpp::R_386_TLS_DESC_CALL
:
1036 const bool is_final
= gsym
->final_value_is_known();
1037 const tls::Tls_optimization optimized_type
1038 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1041 case elfcpp::R_386_TLS_LE
:
1042 case elfcpp::R_386_TLS_LE_32
:
1043 // FIXME: If generating a shared object, we need to copy
1044 // this relocation into the object.
1045 gold_assert(!parameters
->output_is_shared());
1048 case elfcpp::R_386_TLS_IE
:
1049 case elfcpp::R_386_TLS_IE_32
:
1050 case elfcpp::R_386_TLS_GOTIE
:
1051 // FIXME: If not relaxing to LE, we need to generate a
1052 // TPOFF or TPOFF32 reloc.
1053 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1054 unsupported_reloc_global(object
, r_type
, gsym
);
1057 case elfcpp::R_386_TLS_LDM
:
1058 // FIXME: If not relaxing to LE, we need to generate a
1060 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1061 unsupported_reloc_global(object
, r_type
, gsym
);
1064 case elfcpp::R_386_TLS_LDO_32
:
1067 case elfcpp::R_386_TLS_GD
:
1068 case elfcpp::R_386_TLS_GOTDESC
:
1069 case elfcpp::R_386_TLS_DESC_CALL
:
1070 // FIXME: If not relaxing to LE, we need to generate
1071 // DTPMOD32 and DTPOFF32 relocs.
1072 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1073 unsupported_reloc_global(object
, r_type
, gsym
);
1082 case elfcpp::R_386_32PLT
:
1083 case elfcpp::R_386_TLS_GD_32
:
1084 case elfcpp::R_386_TLS_GD_PUSH
:
1085 case elfcpp::R_386_TLS_GD_CALL
:
1086 case elfcpp::R_386_TLS_GD_POP
:
1087 case elfcpp::R_386_TLS_LDM_32
:
1088 case elfcpp::R_386_TLS_LDM_PUSH
:
1089 case elfcpp::R_386_TLS_LDM_CALL
:
1090 case elfcpp::R_386_TLS_LDM_POP
:
1091 case elfcpp::R_386_USED_BY_INTEL_200
:
1093 unsupported_reloc_global(object
, r_type
, gsym
);
1098 // Scan relocations for a section.
1101 Target_i386::scan_relocs(const General_options
& options
,
1102 Symbol_table
* symtab
,
1104 Sized_relobj
<32, false>* object
,
1105 unsigned int data_shndx
,
1106 unsigned int sh_type
,
1107 const unsigned char* prelocs
,
1109 size_t local_symbol_count
,
1110 const unsigned char* plocal_symbols
,
1111 Symbol
** global_symbols
)
1113 if (sh_type
== elfcpp::SHT_RELA
)
1115 gold_error(_("%s: unsupported RELA reloc section"),
1116 object
->name().c_str());
1120 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1135 // Finalize the sections.
1138 Target_i386::do_finalize_sections(Layout
* layout
)
1140 // Fill in some more dynamic tags.
1141 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1144 if (this->got_plt_
!= NULL
)
1145 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1147 if (this->plt_
!= NULL
)
1149 const Output_data
* od
= this->plt_
->rel_plt();
1150 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1151 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1152 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1155 if (this->rel_dyn_
!= NULL
)
1157 const Output_data
* od
= this->rel_dyn_
;
1158 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1159 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1160 odyn
->add_constant(elfcpp::DT_RELENT
,
1161 elfcpp::Elf_sizes
<32>::rel_size
);
1164 if (!parameters
->output_is_shared())
1166 // The value of the DT_DEBUG tag is filled in by the dynamic
1167 // linker at run time, and used by the debugger.
1168 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1172 // Emit any relocs we saved in an attempt to avoid generating COPY
1174 if (this->copy_relocs_
== NULL
)
1176 if (this->copy_relocs_
->any_to_emit())
1178 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1179 this->copy_relocs_
->emit(rel_dyn
);
1181 delete this->copy_relocs_
;
1182 this->copy_relocs_
= NULL
;
1185 // Perform a relocation.
1188 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1189 Target_i386
* target
,
1191 const elfcpp::Rel
<32, false>& rel
,
1192 unsigned int r_type
,
1193 const Sized_symbol
<32>* gsym
,
1194 const Symbol_value
<32>* psymval
,
1195 unsigned char* view
,
1196 elfcpp::Elf_types
<32>::Elf_Addr address
,
1199 if (this->skip_call_tls_get_addr_
)
1201 if (r_type
!= elfcpp::R_386_PLT32
1203 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1204 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1205 _("missing expected TLS relocation"));
1208 this->skip_call_tls_get_addr_
= false;
1213 // Pick the value to use for symbols defined in shared objects.
1214 Symbol_value
<32> symval
;
1216 && (gsym
->is_from_dynobj()
1217 || (parameters
->output_is_shared()
1218 && gsym
->is_preemptible()))
1219 && gsym
->has_plt_offset())
1221 symval
.set_output_value(target
->plt_section()->address()
1222 + gsym
->plt_offset());
1226 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1228 // Get the GOT offset if needed.
1229 bool have_got_offset
= false;
1230 unsigned int got_offset
= 0;
1233 case elfcpp::R_386_GOT32
:
1236 gold_assert(gsym
->has_got_offset());
1237 got_offset
= gsym
->got_offset();
1241 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1242 got_offset
= object
->local_got_offset(r_sym
);
1244 have_got_offset
= true;
1253 case elfcpp::R_386_NONE
:
1254 case elfcpp::R_386_GNU_VTINHERIT
:
1255 case elfcpp::R_386_GNU_VTENTRY
:
1258 case elfcpp::R_386_32
:
1259 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1262 case elfcpp::R_386_PC32
:
1263 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1266 case elfcpp::R_386_16
:
1267 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1270 case elfcpp::R_386_PC16
:
1271 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1274 case elfcpp::R_386_8
:
1275 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1278 case elfcpp::R_386_PC8
:
1279 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1282 case elfcpp::R_386_PLT32
:
1283 gold_assert(gsym
== NULL
1284 || gsym
->has_plt_offset()
1285 || gsym
->final_value_is_known());
1286 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1289 case elfcpp::R_386_GOT32
:
1290 gold_assert(have_got_offset
);
1291 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1294 case elfcpp::R_386_GOTOFF
:
1296 elfcpp::Elf_types
<32>::Elf_Addr value
;
1297 value
= (psymval
->value(object
, 0)
1298 - target
->got_section(NULL
, NULL
)->address());
1299 Relocate_functions
<32, false>::rel32(view
, value
);
1303 case elfcpp::R_386_GOTPC
:
1305 elfcpp::Elf_types
<32>::Elf_Addr value
;
1306 value
= target
->got_section(NULL
, NULL
)->address();
1307 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1311 case elfcpp::R_386_COPY
:
1312 case elfcpp::R_386_GLOB_DAT
:
1313 case elfcpp::R_386_JUMP_SLOT
:
1314 case elfcpp::R_386_RELATIVE
:
1315 // These are outstanding tls relocs, which are unexpected when
1317 case elfcpp::R_386_TLS_TPOFF
:
1318 case elfcpp::R_386_TLS_DTPMOD32
:
1319 case elfcpp::R_386_TLS_DTPOFF32
:
1320 case elfcpp::R_386_TLS_TPOFF32
:
1321 case elfcpp::R_386_TLS_DESC
:
1322 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1323 _("unexpected reloc %u in object file"),
1327 // These are initial tls relocs, which are expected when
1329 case elfcpp::R_386_TLS_IE
:
1330 case elfcpp::R_386_TLS_GOTIE
:
1331 case elfcpp::R_386_TLS_LE
:
1332 case elfcpp::R_386_TLS_GD
:
1333 case elfcpp::R_386_TLS_LDM
:
1334 case elfcpp::R_386_TLS_LDO_32
:
1335 case elfcpp::R_386_TLS_IE_32
:
1336 case elfcpp::R_386_TLS_LE_32
:
1337 case elfcpp::R_386_TLS_GOTDESC
:
1338 case elfcpp::R_386_TLS_DESC_CALL
:
1339 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1340 address
, view_size
);
1343 case elfcpp::R_386_32PLT
:
1344 case elfcpp::R_386_TLS_GD_32
:
1345 case elfcpp::R_386_TLS_GD_PUSH
:
1346 case elfcpp::R_386_TLS_GD_CALL
:
1347 case elfcpp::R_386_TLS_GD_POP
:
1348 case elfcpp::R_386_TLS_LDM_32
:
1349 case elfcpp::R_386_TLS_LDM_PUSH
:
1350 case elfcpp::R_386_TLS_LDM_CALL
:
1351 case elfcpp::R_386_TLS_LDM_POP
:
1352 case elfcpp::R_386_USED_BY_INTEL_200
:
1354 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1355 _("unsupported reloc %u"),
1363 // Perform a TLS relocation.
1366 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1368 const elfcpp::Rel
<32, false>& rel
,
1369 unsigned int r_type
,
1370 const Sized_symbol
<32>* gsym
,
1371 const Symbol_value
<32>* psymval
,
1372 unsigned char* view
,
1373 elfcpp::Elf_types
<32>::Elf_Addr
,
1376 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1377 if (tls_segment
== NULL
)
1379 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1380 _("TLS reloc but no TLS segment"));
1384 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1386 const bool is_final
= (gsym
== NULL
1387 ? !parameters
->output_is_position_independent()
1388 : gsym
->final_value_is_known());
1389 const tls::Tls_optimization optimized_type
1390 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1393 case elfcpp::R_386_TLS_LE_32
:
1394 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1395 Relocate_functions
<32, false>::rel32(view
, value
);
1398 case elfcpp::R_386_TLS_LE
:
1399 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1400 Relocate_functions
<32, false>::rel32(view
, value
);
1403 case elfcpp::R_386_TLS_IE
:
1404 case elfcpp::R_386_TLS_GOTIE
:
1405 case elfcpp::R_386_TLS_IE_32
:
1406 if (optimized_type
== tls::TLSOPT_TO_LE
)
1408 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1409 rel
, r_type
, value
, view
,
1413 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1414 _("unsupported reloc %u"),
1418 case elfcpp::R_386_TLS_GD
:
1419 if (optimized_type
== tls::TLSOPT_TO_LE
)
1421 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1422 rel
, r_type
, value
, view
,
1426 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1427 _("unsupported reloc %u"),
1431 case elfcpp::R_386_TLS_LDM
:
1432 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1434 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1435 _("both SUN and GNU model "
1436 "TLS relocations"));
1439 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1440 if (optimized_type
== tls::TLSOPT_TO_LE
)
1442 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1443 value
, view
, view_size
);
1446 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1447 _("unsupported reloc %u"),
1451 case elfcpp::R_386_TLS_LDO_32
:
1452 // This reloc can appear in debugging sections, in which case we
1453 // won't see the TLS_LDM reloc. The local_dynamic_type field
1455 if (optimized_type
!= tls::TLSOPT_TO_LE
1456 || this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1457 value
= value
- tls_segment
->vaddr();
1458 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1459 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1461 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1462 Relocate_functions
<32, false>::rel32(view
, value
);
1465 case elfcpp::R_386_TLS_GOTDESC
:
1466 case elfcpp::R_386_TLS_DESC_CALL
:
1467 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1468 _("unsupported reloc %u"),
1474 // Do a relocation in which we convert a TLS Initial-Exec to a
1478 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1480 Output_segment
* tls_segment
,
1481 const elfcpp::Rel
<32, false>& rel
,
1482 unsigned int r_type
,
1483 elfcpp::Elf_types
<32>::Elf_Addr value
,
1484 unsigned char* view
,
1487 // We have to actually change the instructions, which means that we
1488 // need to examine the opcodes to figure out which instruction we
1490 if (r_type
== elfcpp::R_386_TLS_IE
)
1492 // movl %gs:XX,%eax ==> movl $YY,%eax
1493 // movl %gs:XX,%reg ==> movl $YY,%reg
1494 // addl %gs:XX,%reg ==> addl $YY,%reg
1495 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
1496 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1498 unsigned char op1
= view
[-1];
1501 // movl XX,%eax ==> movl $YY,%eax
1506 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1508 unsigned char op2
= view
[-2];
1511 // movl XX,%reg ==> movl $YY,%reg
1512 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1513 (op1
& 0xc7) == 0x05);
1515 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1517 else if (op2
== 0x03)
1519 // addl XX,%reg ==> addl $YY,%reg
1520 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1521 (op1
& 0xc7) == 0x05);
1523 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1526 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1531 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1532 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1533 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1534 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1535 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1537 unsigned char op1
= view
[-1];
1538 unsigned char op2
= view
[-2];
1539 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1540 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1543 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1545 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1547 else if (op2
== 0x2b)
1549 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1551 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1553 else if (op2
== 0x03)
1555 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1557 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1560 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1563 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1564 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1567 Relocate_functions
<32, false>::rel32(view
, value
);
1570 // Do a relocation in which we convert a TLS General-Dynamic to a
1574 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1576 Output_segment
* tls_segment
,
1577 const elfcpp::Rel
<32, false>& rel
,
1579 elfcpp::Elf_types
<32>::Elf_Addr value
,
1580 unsigned char* view
,
1583 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1584 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1585 // leal foo(%reg),%eax; call ___tls_get_addr
1586 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1588 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1589 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1591 unsigned char op1
= view
[-1];
1592 unsigned char op2
= view
[-2];
1594 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1595 op2
== 0x8d || op2
== 0x04);
1596 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1602 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1603 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1604 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1605 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1606 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1610 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1611 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1612 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1615 // There is a trailing nop. Use the size byte subl.
1616 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1621 // Use the five byte subl.
1622 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1626 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1627 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1629 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1631 this->skip_call_tls_get_addr_
= true;
1634 // Do a relocation in which we convert a TLS Local-Dynamic to a
1638 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1641 const elfcpp::Rel
<32, false>& rel
,
1643 elfcpp::Elf_types
<32>::Elf_Addr
,
1644 unsigned char* view
,
1647 // leal foo(%reg), %eax; call ___tls_get_addr
1648 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1650 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1651 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1653 // FIXME: Does this test really always pass?
1654 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1655 view
[-2] == 0x8d && view
[-1] == 0x83);
1657 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1659 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1661 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1663 this->skip_call_tls_get_addr_
= true;
1666 // Relocate section data.
1669 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1670 unsigned int sh_type
,
1671 const unsigned char* prelocs
,
1673 unsigned char* view
,
1674 elfcpp::Elf_types
<32>::Elf_Addr address
,
1677 gold_assert(sh_type
== elfcpp::SHT_REL
);
1679 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1680 Target_i386::Relocate
>(
1690 // Return the value to use for a dynamic which requires special
1691 // treatment. This is how we support equality comparisons of function
1692 // pointers across shared library boundaries, as described in the
1693 // processor specific ABI supplement.
1696 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1698 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1699 return this->plt_section()->address() + gsym
->plt_offset();
1702 // Return a string used to fill a code section with nops to take up
1703 // the specified length.
1706 Target_i386::do_code_fill(off_t length
)
1710 // Build a jmp instruction to skip over the bytes.
1711 unsigned char jmp
[5];
1713 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1714 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1715 + std::string(length
- 5, '\0'));
1718 // Nop sequences of various lengths.
1719 const char nop1
[1] = { 0x90 }; // nop
1720 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1721 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1722 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1723 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1724 0x00 }; // leal 0(%esi,1),%esi
1725 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1727 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1729 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1730 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1731 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1732 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1734 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1735 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1737 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1738 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1740 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1741 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1742 0x00, 0x00, 0x00, 0x00 };
1743 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1744 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1745 0x27, 0x00, 0x00, 0x00,
1747 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1748 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1749 0xbc, 0x27, 0x00, 0x00,
1751 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1752 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1753 0x90, 0x90, 0x90, 0x90,
1756 const char* nops
[16] = {
1758 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1759 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1762 return std::string(nops
[length
], length
);
1765 // The selector for i386 object files.
1767 class Target_selector_i386
: public Target_selector
1770 Target_selector_i386()
1771 : Target_selector(elfcpp::EM_386
, 32, false)
1775 recognize(int machine
, int osabi
, int abiversion
);
1778 Target_i386
* target_
;
1781 // Recognize an i386 object file when we already know that the machine
1782 // number is EM_386.
1785 Target_selector_i386::recognize(int, int, int)
1787 if (this->target_
== NULL
)
1788 this->target_
= new Target_i386();
1789 return this->target_
;
1792 Target_selector_i386 target_selector_i386
;
1794 } // End anonymous namespace.