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
);
100 // Return the size of the GOT section.
104 gold_assert(this->got_
!= NULL
);
105 return this->got_
->data_size();
109 // The class which scans relocations.
113 local(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
,
118 const elfcpp::Sym
<32, false>& lsym
);
121 global(const General_options
& options
, Symbol_table
* symtab
,
122 Layout
* layout
, Target_i386
* target
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
129 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
132 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
136 // The class which implements relocation.
141 : skip_call_tls_get_addr_(false),
142 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
147 if (this->skip_call_tls_get_addr_
)
149 // FIXME: This needs to specify the location somehow.
150 gold_error(_("missing expected TLS relocation"));
154 // Do a relocation. Return false if the caller should not issue
155 // any warnings about this relocation.
157 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
158 const elfcpp::Rel
<32, false>&,
159 unsigned int r_type
, const Sized_symbol
<32>*,
160 const Symbol_value
<32>*,
161 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
165 // Do a TLS relocation.
167 relocate_tls(const Relocate_info
<32, false>*, size_t relnum
,
168 const elfcpp::Rel
<32, false>&,
169 unsigned int r_type
, const Sized_symbol
<32>*,
170 const Symbol_value
<32>*,
171 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
, off_t
);
173 // Do a TLS General-Dynamic to Local-Exec transition.
175 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
176 Output_segment
* tls_segment
,
177 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
178 elfcpp::Elf_types
<32>::Elf_Addr value
,
182 // Do a TLS Local-Dynamic to Local-Exec transition.
184 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
185 Output_segment
* tls_segment
,
186 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
187 elfcpp::Elf_types
<32>::Elf_Addr value
,
191 // Do a TLS Initial-Exec to Local-Exec transition.
193 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
194 Output_segment
* tls_segment
,
195 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
196 elfcpp::Elf_types
<32>::Elf_Addr value
,
200 // We need to keep track of which type of local dynamic relocation
201 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
202 enum Local_dynamic_type
209 // This is set if we should skip the next reloc, which should be a
210 // PLT32 reloc against ___tls_get_addr.
211 bool skip_call_tls_get_addr_
;
212 // The type of local dynamic relocation we have seen in the section
213 // being relocated, if any.
214 Local_dynamic_type local_dynamic_type_
;
217 // Adjust TLS relocation type based on the options and whether this
218 // is a local symbol.
219 static tls::Tls_optimization
220 optimize_tls_reloc(bool is_final
, int r_type
);
222 // Get the GOT section, creating it if necessary.
223 Output_data_got
<32, false>*
224 got_section(Symbol_table
*, Layout
*);
226 // Get the GOT PLT section.
228 got_plt_section() const
230 gold_assert(this->got_plt_
!= NULL
);
231 return this->got_plt_
;
234 // Create a PLT entry for a global symbol.
236 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
238 // Get the PLT section.
239 const Output_data_plt_i386
*
242 gold_assert(this->plt_
!= NULL
);
246 // Get the dynamic reloc section, creating it if necessary.
248 rel_dyn_section(Layout
*);
250 // Copy a relocation against a global symbol.
252 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
253 Sized_relobj
<32, false>*, unsigned int,
254 Symbol
*, const elfcpp::Rel
<32, false>&);
256 // Information about this specific target which we pass to the
257 // general Target structure.
258 static const Target::Target_info i386_info
;
261 Output_data_got
<32, false>* got_
;
263 Output_data_plt_i386
* plt_
;
264 // The GOT PLT section.
265 Output_data_space
* got_plt_
;
266 // The dynamic reloc section.
267 Reloc_section
* rel_dyn_
;
268 // Relocs saved to avoid a COPY reloc.
269 Copy_relocs
<32, false>* copy_relocs_
;
270 // Space for variables copied with a COPY reloc.
271 Output_data_space
* dynbss_
;
274 const Target::Target_info
Target_i386::i386_info
=
277 false, // is_big_endian
278 elfcpp::EM_386
, // machine_code
279 false, // has_make_symbol
280 false, // has_resolve
281 true, // has_code_fill
282 true, // is_default_stack_executable
283 "/usr/lib/libc.so.1", // dynamic_linker
284 0x08048000, // default_text_segment_address
285 0x1000, // abi_pagesize
286 0x1000 // common_pagesize
289 // Get the GOT section, creating it if necessary.
291 Output_data_got
<32, false>*
292 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
294 if (this->got_
== NULL
)
296 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
298 this->got_
= new Output_data_got
<32, false>();
300 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
301 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
304 // The old GNU linker creates a .got.plt section. We just
305 // create another set of data in the .got section. Note that we
306 // always create a PLT if we create a GOT, although the PLT
308 this->got_plt_
= new Output_data_space(4);
309 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
310 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
313 // The first three entries are reserved.
314 this->got_plt_
->set_space_size(3 * 4);
316 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
317 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
319 0, 0, elfcpp::STT_OBJECT
,
321 elfcpp::STV_HIDDEN
, 0,
328 // Get the dynamic reloc section, creating it if necessary.
330 Target_i386::Reloc_section
*
331 Target_i386::rel_dyn_section(Layout
* layout
)
333 if (this->rel_dyn_
== NULL
)
335 gold_assert(layout
!= NULL
);
336 this->rel_dyn_
= new Reloc_section();
337 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
338 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
340 return this->rel_dyn_
;
343 // A class to handle the PLT data.
345 class Output_data_plt_i386
: public Output_section_data
348 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
350 Output_data_plt_i386(Layout
*, Output_data_space
*);
352 // Add an entry to the PLT.
354 add_entry(Symbol
* gsym
);
356 // Return the .rel.plt section data.
359 { return this->rel_
; }
363 do_adjust_output_section(Output_section
* os
);
366 // The size of an entry in the PLT.
367 static const int plt_entry_size
= 16;
369 // The first entry in the PLT for an executable.
370 static unsigned char exec_first_plt_entry
[plt_entry_size
];
372 // The first entry in the PLT for a shared object.
373 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
375 // Other entries in the PLT for an executable.
376 static unsigned char exec_plt_entry
[plt_entry_size
];
378 // Other entries in the PLT for a shared object.
379 static unsigned char dyn_plt_entry
[plt_entry_size
];
381 // Set the final size.
383 do_set_address(uint64_t, off_t
)
384 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
386 // Write out the PLT data.
388 do_write(Output_file
*);
390 // The reloc section.
392 // The .got.plt section.
393 Output_data_space
* got_plt_
;
394 // The number of PLT entries.
398 // Create the PLT section. The ordinary .got section is an argument,
399 // since we need to refer to the start. We also create our own .got
400 // section just for PLT entries.
402 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
403 Output_data_space
* got_plt
)
404 : Output_section_data(4), got_plt_(got_plt
), count_(0)
406 this->rel_
= new Reloc_section();
407 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
408 elfcpp::SHF_ALLOC
, this->rel_
);
412 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
414 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
415 // linker, and so do we.
419 // Add an entry to the PLT.
422 Output_data_plt_i386::add_entry(Symbol
* gsym
)
424 gold_assert(!gsym
->has_plt_offset());
426 // Note that when setting the PLT offset we skip the initial
427 // reserved PLT entry.
428 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
432 off_t got_offset
= this->got_plt_
->data_size();
434 // Every PLT entry needs a GOT entry which points back to the PLT
435 // entry (this will be changed by the dynamic linker, normally
436 // lazily when the function is called).
437 this->got_plt_
->set_space_size(got_offset
+ 4);
439 // Every PLT entry needs a reloc.
440 gsym
->set_needs_dynsym_entry();
441 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
444 // Note that we don't need to save the symbol. The contents of the
445 // PLT are independent of which symbols are used. The symbols only
446 // appear in the relocations.
449 // The first entry in the PLT for an executable.
451 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
453 0xff, 0x35, // pushl contents of memory address
454 0, 0, 0, 0, // replaced with address of .got + 4
455 0xff, 0x25, // jmp indirect
456 0, 0, 0, 0, // replaced with address of .got + 8
460 // The first entry in the PLT for a shared object.
462 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
464 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
465 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
469 // Subsequent entries in the PLT for an executable.
471 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
473 0xff, 0x25, // jmp indirect
474 0, 0, 0, 0, // replaced with address of symbol in .got
475 0x68, // pushl immediate
476 0, 0, 0, 0, // replaced with offset into relocation table
477 0xe9, // jmp relative
478 0, 0, 0, 0 // replaced with offset to start of .plt
481 // Subsequent entries in the PLT for a shared object.
483 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
485 0xff, 0xa3, // jmp *offset(%ebx)
486 0, 0, 0, 0, // replaced with offset of symbol in .got
487 0x68, // pushl immediate
488 0, 0, 0, 0, // replaced with offset into relocation table
489 0xe9, // jmp relative
490 0, 0, 0, 0 // replaced with offset to start of .plt
493 // Write out the PLT. This uses the hand-coded instructions above,
494 // and adjusts them as needed. This is all specified by the i386 ELF
495 // Processor Supplement.
498 Output_data_plt_i386::do_write(Output_file
* of
)
500 const off_t offset
= this->offset();
501 const off_t oview_size
= this->data_size();
502 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
504 const off_t got_file_offset
= this->got_plt_
->offset();
505 const off_t got_size
= this->got_plt_
->data_size();
506 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
509 unsigned char* pov
= oview
;
511 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
512 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
514 if (parameters
->output_is_shared())
515 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
518 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
519 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
520 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
522 pov
+= plt_entry_size
;
524 unsigned char* got_pov
= got_view
;
526 memset(got_pov
, 0, 12);
529 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
531 unsigned int plt_offset
= plt_entry_size
;
532 unsigned int plt_rel_offset
= 0;
533 unsigned int got_offset
= 12;
534 const unsigned int count
= this->count_
;
535 for (unsigned int i
= 0;
538 pov
+= plt_entry_size
,
540 plt_offset
+= plt_entry_size
,
541 plt_rel_offset
+= rel_size
,
544 // Set and adjust the PLT entry itself.
546 if (parameters
->output_is_shared())
548 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
549 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
553 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
554 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
559 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
560 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
561 - (plt_offset
+ plt_entry_size
));
563 // Set the entry in the GOT.
564 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
567 gold_assert(pov
- oview
== oview_size
);
568 gold_assert(got_pov
- got_view
== got_size
);
570 of
->write_output_view(offset
, oview_size
, oview
);
571 of
->write_output_view(got_file_offset
, got_size
, got_view
);
574 // Create a PLT entry for a global symbol.
577 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
579 if (gsym
->has_plt_offset())
582 if (this->plt_
== NULL
)
584 // Create the GOT sections first.
585 this->got_section(symtab
, layout
);
587 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
588 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
590 | elfcpp::SHF_EXECINSTR
),
594 this->plt_
->add_entry(gsym
);
597 // Handle a relocation against a non-function symbol defined in a
598 // dynamic object. The traditional way to handle this is to generate
599 // a COPY relocation to copy the variable at runtime from the shared
600 // object into the executable's data segment. However, this is
601 // undesirable in general, as if the size of the object changes in the
602 // dynamic object, the executable will no longer work correctly. If
603 // this relocation is in a writable section, then we can create a
604 // dynamic reloc and the dynamic linker will resolve it to the correct
605 // address at runtime. However, we do not want do that if the
606 // relocation is in a read-only section, as it would prevent the
607 // readonly segment from being shared. And if we have to eventually
608 // generate a COPY reloc, then any dynamic relocations will be
609 // useless. So this means that if this is a writable section, we need
610 // to save the relocation until we see whether we have to create a
611 // COPY relocation for this symbol for any other relocation.
614 Target_i386::copy_reloc(const General_options
* options
,
615 Symbol_table
* symtab
,
617 Sized_relobj
<32, false>* object
,
618 unsigned int data_shndx
, Symbol
* gsym
,
619 const elfcpp::Rel
<32, false>& rel
)
621 Sized_symbol
<32>* ssym
;
622 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
625 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
628 // So far we do not need a COPY reloc. Save this relocation.
629 // If it turns out that we never need a COPY reloc for this
630 // symbol, then we will emit the relocation.
631 if (this->copy_relocs_
== NULL
)
632 this->copy_relocs_
= new Copy_relocs
<32, false>();
633 this->copy_relocs_
->save(ssym
, object
, data_shndx
, rel
);
637 // Allocate space for this symbol in the .bss section.
639 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
641 // There is no defined way to determine the required alignment
642 // of the symbol. We pick the alignment based on the size. We
643 // set an arbitrary maximum of 256.
645 for (align
= 1; align
< 512; align
<<= 1)
646 if ((symsize
& align
) != 0)
649 if (this->dynbss_
== NULL
)
651 this->dynbss_
= new Output_data_space(align
);
652 layout
->add_output_section_data(".bss",
655 | elfcpp::SHF_WRITE
),
659 Output_data_space
* dynbss
= this->dynbss_
;
661 if (align
> dynbss
->addralign())
662 dynbss
->set_space_alignment(align
);
664 off_t dynbss_size
= dynbss
->data_size();
665 dynbss_size
= align_address(dynbss_size
, align
);
666 off_t offset
= dynbss_size
;
667 dynbss
->set_space_size(dynbss_size
+ symsize
);
669 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
671 // Add the COPY reloc.
672 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
673 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
677 // Optimize the TLS relocation type based on what we know about the
678 // symbol. IS_FINAL is true if the final address of this symbol is
679 // known at link time.
681 tls::Tls_optimization
682 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
684 // If we are generating a shared library, then we can't do anything
686 if (parameters
->output_is_shared())
687 return tls::TLSOPT_NONE
;
691 case elfcpp::R_386_TLS_GD
:
692 case elfcpp::R_386_TLS_GOTDESC
:
693 case elfcpp::R_386_TLS_DESC_CALL
:
694 // These are General-Dynamic which permits fully general TLS
695 // access. Since we know that we are generating an executable,
696 // we can convert this to Initial-Exec. If we also know that
697 // this is a local symbol, we can further switch to Local-Exec.
699 return tls::TLSOPT_TO_LE
;
700 return tls::TLSOPT_TO_IE
;
702 case elfcpp::R_386_TLS_LDM
:
703 // This is Local-Dynamic, which refers to a local symbol in the
704 // dynamic TLS block. Since we know that we generating an
705 // executable, we can switch to Local-Exec.
706 return tls::TLSOPT_TO_LE
;
708 case elfcpp::R_386_TLS_LDO_32
:
709 // Another type of Local-Dynamic relocation.
710 return tls::TLSOPT_TO_LE
;
712 case elfcpp::R_386_TLS_IE
:
713 case elfcpp::R_386_TLS_GOTIE
:
714 case elfcpp::R_386_TLS_IE_32
:
715 // These are Initial-Exec relocs which get the thread offset
716 // from the GOT. If we know that we are linking against the
717 // local symbol, we can switch to Local-Exec, which links the
718 // thread offset into the instruction.
720 return tls::TLSOPT_TO_LE
;
721 return tls::TLSOPT_NONE
;
723 case elfcpp::R_386_TLS_LE
:
724 case elfcpp::R_386_TLS_LE_32
:
725 // When we already have Local-Exec, there is nothing further we
727 return tls::TLSOPT_NONE
;
734 // Report an unsupported relocation against a local symbol.
737 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
740 gold_error(_("%s: unsupported reloc %u against local symbol"),
741 object
->name().c_str(), r_type
);
744 // Scan a relocation for a local symbol.
747 Target_i386::Scan::local(const General_options
&,
748 Symbol_table
* symtab
,
751 Sized_relobj
<32, false>* object
,
752 unsigned int data_shndx
,
753 const elfcpp::Rel
<32, false>& reloc
,
755 const elfcpp::Sym
<32, false>&)
759 case elfcpp::R_386_NONE
:
760 case elfcpp::R_386_GNU_VTINHERIT
:
761 case elfcpp::R_386_GNU_VTENTRY
:
764 case elfcpp::R_386_32
:
765 case elfcpp::R_386_16
:
766 case elfcpp::R_386_8
:
767 // If building a shared library (or a position-independent
768 // executable), we need to create a dynamic relocation for
769 // this location. The relocation applied at link time will
770 // apply the link-time value, so we flag the location with
771 // an R_386_RELATIVE relocation so the dynamic loader can
772 // relocate it easily.
773 if (parameters
->output_is_position_independent())
775 // FIXME: R_386_RELATIVE only works for a 32-bit relocation.
776 gold_assert(r_type
!= elfcpp::R_386_16
&& r_type
!= elfcpp::R_386_8
);
778 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
779 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
780 reloc
.get_r_offset());
784 case elfcpp::R_386_PC32
:
785 case elfcpp::R_386_PC16
:
786 case elfcpp::R_386_PC8
:
789 case elfcpp::R_386_PLT32
:
790 // Since we know this is a local symbol, we can handle this as a
794 case elfcpp::R_386_GOTOFF
:
795 case elfcpp::R_386_GOTPC
:
796 // We need a GOT section.
797 target
->got_section(symtab
, layout
);
800 case elfcpp::R_386_GOT32
:
802 // The symbol requires a GOT entry.
803 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
804 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
805 if (got
->add_local(object
, r_sym
))
807 // If we are generating a shared object, we need to add a
808 // dynamic RELATIVE relocation for this symbol.
809 if (parameters
->output_is_position_independent())
811 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
812 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
813 data_shndx
, reloc
.get_r_offset());
819 // These are relocations which should only be seen by the
820 // dynamic linker, and should never be seen here.
821 case elfcpp::R_386_COPY
:
822 case elfcpp::R_386_GLOB_DAT
:
823 case elfcpp::R_386_JUMP_SLOT
:
824 case elfcpp::R_386_RELATIVE
:
825 case elfcpp::R_386_TLS_TPOFF
:
826 case elfcpp::R_386_TLS_DTPMOD32
:
827 case elfcpp::R_386_TLS_DTPOFF32
:
828 case elfcpp::R_386_TLS_TPOFF32
:
829 case elfcpp::R_386_TLS_DESC
:
830 gold_error(_("%s: unexpected reloc %u in object file"),
831 object
->name().c_str(), r_type
);
834 // These are initial TLS relocs, which are expected when
836 case elfcpp::R_386_TLS_GD
: // Global-dynamic
837 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
838 case elfcpp::R_386_TLS_DESC_CALL
:
839 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
840 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
841 case elfcpp::R_386_TLS_IE
: // Initial-exec
842 case elfcpp::R_386_TLS_IE_32
:
843 case elfcpp::R_386_TLS_GOTIE
:
844 case elfcpp::R_386_TLS_LE
: // Local-exec
845 case elfcpp::R_386_TLS_LE_32
:
847 bool output_is_shared
= parameters
->output_is_shared();
848 const tls::Tls_optimization optimized_type
849 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
852 case elfcpp::R_386_TLS_GD
: // Global-dynamic
853 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
854 case elfcpp::R_386_TLS_DESC_CALL
:
855 // FIXME: If not relaxing to LE, we need to generate
856 // DTPMOD32 and DTPOFF32 relocs.
857 if (optimized_type
!= tls::TLSOPT_TO_LE
)
858 unsupported_reloc_local(object
, r_type
);
861 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
862 // FIXME: If not relaxing to LE, we need to generate a
864 if (optimized_type
!= tls::TLSOPT_TO_LE
)
865 unsupported_reloc_local(object
, r_type
);
868 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
871 case elfcpp::R_386_TLS_IE
: // Initial-exec
872 case elfcpp::R_386_TLS_IE_32
:
873 case elfcpp::R_386_TLS_GOTIE
:
874 // FIXME: If not relaxing to LE, we need to generate a
875 // TPOFF or TPOFF32 reloc.
876 if (optimized_type
!= tls::TLSOPT_TO_LE
)
877 unsupported_reloc_local(object
, r_type
);
880 case elfcpp::R_386_TLS_LE
: // Local-exec
881 case elfcpp::R_386_TLS_LE_32
:
882 // FIXME: If generating a shared object, we need to copy
883 // this relocation into the object.
884 gold_assert(!output_is_shared
);
893 case elfcpp::R_386_32PLT
:
894 case elfcpp::R_386_TLS_GD_32
:
895 case elfcpp::R_386_TLS_GD_PUSH
:
896 case elfcpp::R_386_TLS_GD_CALL
:
897 case elfcpp::R_386_TLS_GD_POP
:
898 case elfcpp::R_386_TLS_LDM_32
:
899 case elfcpp::R_386_TLS_LDM_PUSH
:
900 case elfcpp::R_386_TLS_LDM_CALL
:
901 case elfcpp::R_386_TLS_LDM_POP
:
902 case elfcpp::R_386_USED_BY_INTEL_200
:
904 unsupported_reloc_local(object
, r_type
);
909 // Report an unsupported relocation against a global symbol.
912 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
916 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
917 object
->name().c_str(), r_type
, gsym
->name());
920 // Scan a relocation for a global symbol.
923 Target_i386::Scan::global(const General_options
& options
,
924 Symbol_table
* symtab
,
927 Sized_relobj
<32, false>* object
,
928 unsigned int data_shndx
,
929 const elfcpp::Rel
<32, false>& reloc
,
935 case elfcpp::R_386_NONE
:
936 case elfcpp::R_386_GNU_VTINHERIT
:
937 case elfcpp::R_386_GNU_VTENTRY
:
940 case elfcpp::R_386_32
:
941 case elfcpp::R_386_PC32
:
942 case elfcpp::R_386_16
:
943 case elfcpp::R_386_PC16
:
944 case elfcpp::R_386_8
:
945 case elfcpp::R_386_PC8
:
947 bool is_pcrel
= (r_type
== elfcpp::R_386_PC32
948 || r_type
== elfcpp::R_386_PC16
949 || r_type
== elfcpp::R_386_PC8
);
951 if (gsym
->is_from_dynobj()
952 || (parameters
->output_is_shared()
953 && gsym
->is_preemptible()))
955 // (a) This symbol is defined in a dynamic object. If it is a
956 // function, we make a PLT entry. Otherwise we need to
957 // either generate a COPY reloc or copy this reloc.
958 // (b) We are building a shared object and this symbol is
959 // preemptible. If it is a function, we make a PLT entry.
960 // Otherwise, we copy the reloc.
961 if (gsym
->type() == elfcpp::STT_FUNC
)
963 target
->make_plt_entry(symtab
, layout
, gsym
);
965 // If this is not a PC relative reference, then we may
966 // be taking the address of the function. In that case
967 // we need to set the entry in the dynamic symbol table
968 // to the address of the PLT entry. We will also need to
969 // create a dynamic relocation.
972 if (gsym
->is_from_dynobj())
973 gsym
->set_needs_dynsym_value();
974 if (parameters
->output_is_position_independent())
976 // FIXME: If this is an 8-bit or 16-bit
977 // relocation, R_386_RELATIVE won't work.
978 gold_assert(r_type
!= elfcpp::R_386_16
979 && r_type
!= elfcpp::R_386_8
);
981 Reloc_section
* rel_dyn
=
982 target
->rel_dyn_section(layout
);
983 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
,
984 data_shndx
, reloc
.get_r_offset());
988 else if (parameters
->output_is_shared())
990 // We do not make COPY relocs in shared objects.
991 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
992 rel_dyn
->add_global(gsym
, r_type
, object
, data_shndx
,
993 reloc
.get_r_offset());
996 target
->copy_reloc(&options
, symtab
, layout
, object
, data_shndx
,
999 else if (!is_pcrel
&& parameters
->output_is_position_independent())
1001 // FIXME: If this is an 8-bit or 16-bit relocation,
1002 // R_386_RELATIVE won't work.
1003 gold_assert(r_type
!= elfcpp::R_386_16
1004 && r_type
!= elfcpp::R_386_8
);
1006 // This is not a PC-relative reference, so we need to generate
1007 // a dynamic relocation.
1008 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1009 rel_dyn
->add_local(object
, 0, elfcpp::R_386_RELATIVE
, data_shndx
,
1010 reloc
.get_r_offset());
1015 case elfcpp::R_386_GOT32
:
1017 // The symbol requires a GOT entry.
1018 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1019 if (got
->add_global(gsym
))
1021 // If this symbol is not fully resolved, we need to add a
1022 // dynamic relocation for it.
1023 if (!gsym
->final_value_is_known())
1025 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1026 rel_dyn
->add_global(gsym
, elfcpp::R_386_GLOB_DAT
, got
,
1027 gsym
->got_offset());
1033 case elfcpp::R_386_PLT32
:
1034 // If the symbol is fully resolved, this is just a PC32 reloc.
1035 // Otherwise we need a PLT entry.
1036 if (gsym
->final_value_is_known())
1038 // If building a shared library, we can also skip the PLT entry
1039 // if the symbol is defined in the output file and is protected
1041 if (gsym
->is_defined()
1042 && !gsym
->is_from_dynobj()
1043 && !gsym
->is_preemptible())
1045 target
->make_plt_entry(symtab
, layout
, gsym
);
1048 case elfcpp::R_386_GOTOFF
:
1049 case elfcpp::R_386_GOTPC
:
1050 // We need a GOT section.
1051 target
->got_section(symtab
, layout
);
1054 // These are relocations which should only be seen by the
1055 // dynamic linker, and should never be seen here.
1056 case elfcpp::R_386_COPY
:
1057 case elfcpp::R_386_GLOB_DAT
:
1058 case elfcpp::R_386_JUMP_SLOT
:
1059 case elfcpp::R_386_RELATIVE
:
1060 case elfcpp::R_386_TLS_TPOFF
:
1061 case elfcpp::R_386_TLS_DTPMOD32
:
1062 case elfcpp::R_386_TLS_DTPOFF32
:
1063 case elfcpp::R_386_TLS_TPOFF32
:
1064 case elfcpp::R_386_TLS_DESC
:
1065 gold_error(_("%s: unexpected reloc %u in object file"),
1066 object
->name().c_str(), r_type
);
1069 // These are initial tls relocs, which are expected when
1071 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1072 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1073 case elfcpp::R_386_TLS_DESC_CALL
:
1074 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1075 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1076 case elfcpp::R_386_TLS_IE
: // Initial-exec
1077 case elfcpp::R_386_TLS_IE_32
:
1078 case elfcpp::R_386_TLS_GOTIE
:
1079 case elfcpp::R_386_TLS_LE
: // Local-exec
1080 case elfcpp::R_386_TLS_LE_32
:
1082 const bool is_final
= gsym
->final_value_is_known();
1083 const tls::Tls_optimization optimized_type
1084 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1087 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1088 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1089 case elfcpp::R_386_TLS_DESC_CALL
:
1090 // FIXME: If not relaxing to LE, we need to generate
1091 // DTPMOD32 and DTPOFF32 relocs.
1092 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1093 unsupported_reloc_global(object
, r_type
, gsym
);
1096 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1097 // FIXME: If not relaxing to LE, we need to generate a
1099 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1100 unsupported_reloc_global(object
, r_type
, gsym
);
1103 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1106 case elfcpp::R_386_TLS_IE
: // Initial-exec
1107 case elfcpp::R_386_TLS_IE_32
:
1108 case elfcpp::R_386_TLS_GOTIE
:
1109 // FIXME: If not relaxing to LE, we need to generate a
1110 // TPOFF or TPOFF32 reloc.
1111 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1112 unsupported_reloc_global(object
, r_type
, gsym
);
1115 case elfcpp::R_386_TLS_LE
: // Local-exec
1116 case elfcpp::R_386_TLS_LE_32
:
1117 // FIXME: If generating a shared object, we need to copy
1118 // this relocation into the object.
1119 gold_assert(!parameters
->output_is_shared());
1128 case elfcpp::R_386_32PLT
:
1129 case elfcpp::R_386_TLS_GD_32
:
1130 case elfcpp::R_386_TLS_GD_PUSH
:
1131 case elfcpp::R_386_TLS_GD_CALL
:
1132 case elfcpp::R_386_TLS_GD_POP
:
1133 case elfcpp::R_386_TLS_LDM_32
:
1134 case elfcpp::R_386_TLS_LDM_PUSH
:
1135 case elfcpp::R_386_TLS_LDM_CALL
:
1136 case elfcpp::R_386_TLS_LDM_POP
:
1137 case elfcpp::R_386_USED_BY_INTEL_200
:
1139 unsupported_reloc_global(object
, r_type
, gsym
);
1144 // Scan relocations for a section.
1147 Target_i386::scan_relocs(const General_options
& options
,
1148 Symbol_table
* symtab
,
1150 Sized_relobj
<32, false>* object
,
1151 unsigned int data_shndx
,
1152 unsigned int sh_type
,
1153 const unsigned char* prelocs
,
1155 size_t local_symbol_count
,
1156 const unsigned char* plocal_symbols
,
1157 Symbol
** global_symbols
)
1159 if (sh_type
== elfcpp::SHT_RELA
)
1161 gold_error(_("%s: unsupported RELA reloc section"),
1162 object
->name().c_str());
1166 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1181 // Finalize the sections.
1184 Target_i386::do_finalize_sections(Layout
* layout
)
1186 // Fill in some more dynamic tags.
1187 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1190 if (this->got_plt_
!= NULL
)
1191 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1193 if (this->plt_
!= NULL
)
1195 const Output_data
* od
= this->plt_
->rel_plt();
1196 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1197 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1198 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1201 if (this->rel_dyn_
!= NULL
)
1203 const Output_data
* od
= this->rel_dyn_
;
1204 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1205 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1206 odyn
->add_constant(elfcpp::DT_RELENT
,
1207 elfcpp::Elf_sizes
<32>::rel_size
);
1210 if (!parameters
->output_is_shared())
1212 // The value of the DT_DEBUG tag is filled in by the dynamic
1213 // linker at run time, and used by the debugger.
1214 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1218 // Emit any relocs we saved in an attempt to avoid generating COPY
1220 if (this->copy_relocs_
== NULL
)
1222 if (this->copy_relocs_
->any_to_emit())
1224 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1225 this->copy_relocs_
->emit(rel_dyn
);
1227 delete this->copy_relocs_
;
1228 this->copy_relocs_
= NULL
;
1231 // Perform a relocation.
1234 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1235 Target_i386
* target
,
1237 const elfcpp::Rel
<32, false>& rel
,
1238 unsigned int r_type
,
1239 const Sized_symbol
<32>* gsym
,
1240 const Symbol_value
<32>* psymval
,
1241 unsigned char* view
,
1242 elfcpp::Elf_types
<32>::Elf_Addr address
,
1245 if (this->skip_call_tls_get_addr_
)
1247 if (r_type
!= elfcpp::R_386_PLT32
1249 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1250 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1251 _("missing expected TLS relocation"));
1254 this->skip_call_tls_get_addr_
= false;
1259 // Pick the value to use for symbols defined in shared objects.
1260 Symbol_value
<32> symval
;
1262 && (gsym
->is_from_dynobj()
1263 || (parameters
->output_is_shared()
1264 && gsym
->is_preemptible()))
1265 && gsym
->has_plt_offset())
1267 symval
.set_output_value(target
->plt_section()->address()
1268 + gsym
->plt_offset());
1272 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1274 // Get the GOT offset if needed.
1275 // The GOT pointer points to the end of the GOT section.
1276 // We need to subtract the size of the GOT section to get
1277 // the actual offset to use in the relocation.
1278 bool have_got_offset
= false;
1279 unsigned int got_offset
= 0;
1282 case elfcpp::R_386_GOT32
:
1285 gold_assert(gsym
->has_got_offset());
1286 got_offset
= gsym
->got_offset() - target
->got_size();
1290 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1291 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1293 have_got_offset
= true;
1302 case elfcpp::R_386_NONE
:
1303 case elfcpp::R_386_GNU_VTINHERIT
:
1304 case elfcpp::R_386_GNU_VTENTRY
:
1307 case elfcpp::R_386_32
:
1308 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1311 case elfcpp::R_386_PC32
:
1312 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1315 case elfcpp::R_386_16
:
1316 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1319 case elfcpp::R_386_PC16
:
1320 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1323 case elfcpp::R_386_8
:
1324 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1327 case elfcpp::R_386_PC8
:
1328 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1331 case elfcpp::R_386_PLT32
:
1332 gold_assert(gsym
== NULL
1333 || gsym
->has_plt_offset()
1334 || gsym
->final_value_is_known());
1335 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1338 case elfcpp::R_386_GOT32
:
1339 gold_assert(have_got_offset
);
1340 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1343 case elfcpp::R_386_GOTOFF
:
1345 elfcpp::Elf_types
<32>::Elf_Addr value
;
1346 value
= (psymval
->value(object
, 0)
1347 - target
->got_plt_section()->address());
1348 Relocate_functions
<32, false>::rel32(view
, value
);
1352 case elfcpp::R_386_GOTPC
:
1354 elfcpp::Elf_types
<32>::Elf_Addr value
;
1355 value
= target
->got_plt_section()->address();
1356 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1360 case elfcpp::R_386_COPY
:
1361 case elfcpp::R_386_GLOB_DAT
:
1362 case elfcpp::R_386_JUMP_SLOT
:
1363 case elfcpp::R_386_RELATIVE
:
1364 // These are outstanding tls relocs, which are unexpected when
1366 case elfcpp::R_386_TLS_TPOFF
:
1367 case elfcpp::R_386_TLS_DTPMOD32
:
1368 case elfcpp::R_386_TLS_DTPOFF32
:
1369 case elfcpp::R_386_TLS_TPOFF32
:
1370 case elfcpp::R_386_TLS_DESC
:
1371 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1372 _("unexpected reloc %u in object file"),
1376 // These are initial tls relocs, which are expected when
1378 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1379 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1380 case elfcpp::R_386_TLS_DESC_CALL
:
1381 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1382 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1383 case elfcpp::R_386_TLS_IE
: // Initial-exec
1384 case elfcpp::R_386_TLS_IE_32
:
1385 case elfcpp::R_386_TLS_GOTIE
:
1386 case elfcpp::R_386_TLS_LE
: // Local-exec
1387 case elfcpp::R_386_TLS_LE_32
:
1388 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, psymval
, view
,
1389 address
, view_size
);
1392 case elfcpp::R_386_32PLT
:
1393 case elfcpp::R_386_TLS_GD_32
:
1394 case elfcpp::R_386_TLS_GD_PUSH
:
1395 case elfcpp::R_386_TLS_GD_CALL
:
1396 case elfcpp::R_386_TLS_GD_POP
:
1397 case elfcpp::R_386_TLS_LDM_32
:
1398 case elfcpp::R_386_TLS_LDM_PUSH
:
1399 case elfcpp::R_386_TLS_LDM_CALL
:
1400 case elfcpp::R_386_TLS_LDM_POP
:
1401 case elfcpp::R_386_USED_BY_INTEL_200
:
1403 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1404 _("unsupported reloc %u"),
1412 // Perform a TLS relocation.
1415 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1417 const elfcpp::Rel
<32, false>& rel
,
1418 unsigned int r_type
,
1419 const Sized_symbol
<32>* gsym
,
1420 const Symbol_value
<32>* psymval
,
1421 unsigned char* view
,
1422 elfcpp::Elf_types
<32>::Elf_Addr
,
1425 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1426 if (tls_segment
== NULL
)
1428 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1429 _("TLS reloc but no TLS segment"));
1433 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(relinfo
->object
, 0);
1435 const bool is_final
= (gsym
== NULL
1436 ? !parameters
->output_is_position_independent()
1437 : gsym
->final_value_is_known());
1438 const tls::Tls_optimization optimized_type
1439 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1442 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1443 if (optimized_type
== tls::TLSOPT_TO_LE
)
1445 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1446 rel
, r_type
, value
, view
,
1450 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1451 _("unsupported reloc %u"),
1455 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1456 case elfcpp::R_386_TLS_DESC_CALL
:
1457 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1458 _("unsupported reloc %u"),
1462 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1463 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1465 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1466 _("both SUN and GNU model "
1467 "TLS relocations"));
1470 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1471 if (optimized_type
== tls::TLSOPT_TO_LE
)
1473 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1474 value
, view
, view_size
);
1477 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1478 _("unsupported reloc %u"),
1482 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1483 // This reloc can appear in debugging sections, in which case we
1484 // won't see the TLS_LDM reloc. The local_dynamic_type field
1486 if (optimized_type
!= tls::TLSOPT_TO_LE
1487 || this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1488 value
= value
- tls_segment
->vaddr();
1489 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_GNU
)
1490 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1492 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1493 Relocate_functions
<32, false>::rel32(view
, value
);
1496 case elfcpp::R_386_TLS_IE
: // Initial-exec
1497 case elfcpp::R_386_TLS_GOTIE
:
1498 case elfcpp::R_386_TLS_IE_32
:
1499 if (optimized_type
== tls::TLSOPT_TO_LE
)
1501 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1502 rel
, r_type
, value
, view
,
1506 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1507 _("unsupported reloc %u"),
1511 case elfcpp::R_386_TLS_LE
: // Local-exec
1512 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
1513 Relocate_functions
<32, false>::rel32(view
, value
);
1516 case elfcpp::R_386_TLS_LE_32
:
1517 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1518 Relocate_functions
<32, false>::rel32(view
, value
);
1523 // Do a relocation in which we convert a TLS General-Dynamic to a
1527 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1529 Output_segment
* tls_segment
,
1530 const elfcpp::Rel
<32, false>& rel
,
1532 elfcpp::Elf_types
<32>::Elf_Addr value
,
1533 unsigned char* view
,
1536 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1537 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1538 // leal foo(%reg),%eax; call ___tls_get_addr
1539 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1541 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1542 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1544 unsigned char op1
= view
[-1];
1545 unsigned char op2
= view
[-2];
1547 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1548 op2
== 0x8d || op2
== 0x04);
1549 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1555 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1556 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1557 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1558 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1559 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1563 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1564 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1565 if (static_cast<off_t
>(rel
.get_r_offset() + 9) < view_size
1568 // There is a trailing nop. Use the size byte subl.
1569 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1574 // Use the five byte subl.
1575 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1579 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1580 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1582 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1584 this->skip_call_tls_get_addr_
= true;
1587 // Do a relocation in which we convert a TLS Local-Dynamic to a
1591 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1594 const elfcpp::Rel
<32, false>& rel
,
1596 elfcpp::Elf_types
<32>::Elf_Addr
,
1597 unsigned char* view
,
1600 // leal foo(%reg), %eax; call ___tls_get_addr
1601 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1603 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1604 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1606 // FIXME: Does this test really always pass?
1607 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1608 view
[-2] == 0x8d && view
[-1] == 0x83);
1610 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1612 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1614 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1616 this->skip_call_tls_get_addr_
= true;
1619 // Do a relocation in which we convert a TLS Initial-Exec to a
1623 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
1625 Output_segment
* tls_segment
,
1626 const elfcpp::Rel
<32, false>& rel
,
1627 unsigned int r_type
,
1628 elfcpp::Elf_types
<32>::Elf_Addr value
,
1629 unsigned char* view
,
1632 // We have to actually change the instructions, which means that we
1633 // need to examine the opcodes to figure out which instruction we
1635 if (r_type
== elfcpp::R_386_TLS_IE
)
1637 // movl %gs:XX,%eax ==> movl $YY,%eax
1638 // movl %gs:XX,%reg ==> movl $YY,%reg
1639 // addl %gs:XX,%reg ==> addl $YY,%reg
1640 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
1641 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1643 unsigned char op1
= view
[-1];
1646 // movl XX,%eax ==> movl $YY,%eax
1651 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1653 unsigned char op2
= view
[-2];
1656 // movl XX,%reg ==> movl $YY,%reg
1657 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1658 (op1
& 0xc7) == 0x05);
1660 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1662 else if (op2
== 0x03)
1664 // addl XX,%reg ==> addl $YY,%reg
1665 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1666 (op1
& 0xc7) == 0x05);
1668 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1671 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1676 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1677 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1678 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1679 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1680 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
1682 unsigned char op1
= view
[-1];
1683 unsigned char op2
= view
[-2];
1684 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1685 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
1688 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1690 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1692 else if (op2
== 0x2b)
1694 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1696 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
1698 else if (op2
== 0x03)
1700 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1702 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
1705 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
1708 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
1709 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
1712 Relocate_functions
<32, false>::rel32(view
, value
);
1715 // Relocate section data.
1718 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
1719 unsigned int sh_type
,
1720 const unsigned char* prelocs
,
1722 unsigned char* view
,
1723 elfcpp::Elf_types
<32>::Elf_Addr address
,
1726 gold_assert(sh_type
== elfcpp::SHT_REL
);
1728 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
1729 Target_i386::Relocate
>(
1739 // Return the value to use for a dynamic which requires special
1740 // treatment. This is how we support equality comparisons of function
1741 // pointers across shared library boundaries, as described in the
1742 // processor specific ABI supplement.
1745 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
1747 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
1748 return this->plt_section()->address() + gsym
->plt_offset();
1751 // Return a string used to fill a code section with nops to take up
1752 // the specified length.
1755 Target_i386::do_code_fill(off_t length
)
1759 // Build a jmp instruction to skip over the bytes.
1760 unsigned char jmp
[5];
1762 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
1763 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
1764 + std::string(length
- 5, '\0'));
1767 // Nop sequences of various lengths.
1768 const char nop1
[1] = { 0x90 }; // nop
1769 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
1770 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1771 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1772 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1773 0x00 }; // leal 0(%esi,1),%esi
1774 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1776 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1778 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1779 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1780 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1781 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1783 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1784 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1786 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1787 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1789 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1790 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1791 0x00, 0x00, 0x00, 0x00 };
1792 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1793 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1794 0x27, 0x00, 0x00, 0x00,
1796 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1797 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1798 0xbc, 0x27, 0x00, 0x00,
1800 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1801 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1802 0x90, 0x90, 0x90, 0x90,
1805 const char* nops
[16] = {
1807 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
1808 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
1811 return std::string(nops
[length
], length
);
1814 // The selector for i386 object files.
1816 class Target_selector_i386
: public Target_selector
1819 Target_selector_i386()
1820 : Target_selector(elfcpp::EM_386
, 32, false)
1824 recognize(int machine
, int osabi
, int abiversion
);
1827 Target_i386
* target_
;
1830 // Recognize an i386 object file when we already know that the machine
1831 // number is EM_386.
1834 Target_selector_i386::recognize(int, int, int)
1836 if (this->target_
== NULL
)
1837 this->target_
= new Target_i386();
1838 return this->target_
;
1841 Target_selector_i386 target_selector_i386
;
1843 } // End anonymous namespace.