1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
28 #include "parameters.h"
36 #include "target-reloc.h"
37 #include "target-select.h"
45 class Output_data_plt_i386
;
47 // The i386 target class.
48 // TLS info comes from
49 // http://people.redhat.com/drepper/tls.pdf
50 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52 class Target_i386
: public Sized_target
<32, false>
55 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
58 : Sized_target
<32, false>(&i386_info
),
59 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
60 copy_relocs_(NULL
), dynbss_(NULL
), got_mod_index_offset_(-1U)
63 // Scan the relocations to look for symbol adjustments.
65 scan_relocs(const General_options
& options
,
68 Sized_relobj
<32, false>* object
,
69 unsigned int data_shndx
,
71 const unsigned char* prelocs
,
73 Output_section
* output_section
,
74 bool needs_special_offset_handling
,
75 size_t local_symbol_count
,
76 const unsigned char* plocal_symbols
);
78 // Finalize the sections.
80 do_finalize_sections(Layout
*);
82 // Return the value to use for a dynamic which requires special
85 do_dynsym_value(const Symbol
*) const;
87 // Relocate a section.
89 relocate_section(const Relocate_info
<32, false>*,
91 const unsigned char* prelocs
,
93 Output_section
* output_section
,
94 bool needs_special_offset_handling
,
96 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
97 section_size_type view_size
);
99 // Return a string used to fill a code section with nops.
101 do_code_fill(section_size_type length
);
103 // Return whether SYM is defined by the ABI.
105 do_is_defined_by_abi(Symbol
* sym
) const
106 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
108 // Return the size of the GOT section.
112 gold_assert(this->got_
!= NULL
);
113 return this->got_
->data_size();
117 // The class which scans relocations.
121 local(const General_options
& options
, Symbol_table
* symtab
,
122 Layout
* layout
, Target_i386
* target
,
123 Sized_relobj
<32, false>* object
,
124 unsigned int data_shndx
,
125 Output_section
* output_section
,
126 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
127 const elfcpp::Sym
<32, false>& lsym
);
130 global(const General_options
& options
, Symbol_table
* symtab
,
131 Layout
* layout
, Target_i386
* target
,
132 Sized_relobj
<32, false>* object
,
133 unsigned int data_shndx
,
134 Output_section
* output_section
,
135 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
139 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
142 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
146 // The class which implements relocation.
151 : skip_call_tls_get_addr_(false),
152 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
157 if (this->skip_call_tls_get_addr_
)
159 // FIXME: This needs to specify the location somehow.
160 gold_error(_("missing expected TLS relocation"));
164 // Return whether the static relocation needs to be applied.
166 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
167 bool is_absolute_ref
,
168 bool is_function_call
,
171 // Do a relocation. Return false if the caller should not issue
172 // any warnings about this relocation.
174 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
175 const elfcpp::Rel
<32, false>&,
176 unsigned int r_type
, const Sized_symbol
<32>*,
177 const Symbol_value
<32>*,
178 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
182 // Do a TLS relocation.
184 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
185 size_t relnum
, const elfcpp::Rel
<32, false>&,
186 unsigned int r_type
, const Sized_symbol
<32>*,
187 const Symbol_value
<32>*,
188 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
191 // Do a TLS General-Dynamic to Initial-Exec transition.
193 tls_gd_to_ie(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
,
198 section_size_type view_size
);
200 // Do a TLS General-Dynamic to Local-Exec transition.
202 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
203 Output_segment
* tls_segment
,
204 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
205 elfcpp::Elf_types
<32>::Elf_Addr value
,
207 section_size_type view_size
);
209 // Do a TLS Local-Dynamic to Local-Exec transition.
211 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
212 Output_segment
* tls_segment
,
213 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
214 elfcpp::Elf_types
<32>::Elf_Addr value
,
216 section_size_type view_size
);
218 // Do a TLS Initial-Exec to Local-Exec transition.
220 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
221 Output_segment
* tls_segment
,
222 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
223 elfcpp::Elf_types
<32>::Elf_Addr value
,
225 section_size_type view_size
);
227 // We need to keep track of which type of local dynamic relocation
228 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
229 enum Local_dynamic_type
236 // This is set if we should skip the next reloc, which should be a
237 // PLT32 reloc against ___tls_get_addr.
238 bool skip_call_tls_get_addr_
;
239 // The type of local dynamic relocation we have seen in the section
240 // being relocated, if any.
241 Local_dynamic_type local_dynamic_type_
;
244 // Adjust TLS relocation type based on the options and whether this
245 // is a local symbol.
246 static tls::Tls_optimization
247 optimize_tls_reloc(bool is_final
, int r_type
);
249 // Get the GOT section, creating it if necessary.
250 Output_data_got
<32, false>*
251 got_section(Symbol_table
*, Layout
*);
253 // Get the GOT PLT section.
255 got_plt_section() const
257 gold_assert(this->got_plt_
!= NULL
);
258 return this->got_plt_
;
261 // Create a PLT entry for a global symbol.
263 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
265 // Create a GOT entry for the TLS module index.
267 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
268 Sized_relobj
<32, false>* object
);
270 // Get the PLT section.
271 const Output_data_plt_i386
*
274 gold_assert(this->plt_
!= NULL
);
278 // Get the dynamic reloc section, creating it if necessary.
280 rel_dyn_section(Layout
*);
282 // Return true if the symbol may need a COPY relocation.
283 // References from an executable object to non-function symbols
284 // defined in a dynamic object may need a COPY relocation.
286 may_need_copy_reloc(Symbol
* gsym
)
288 return (!parameters
->output_is_shared()
289 && gsym
->is_from_dynobj()
290 && gsym
->type() != elfcpp::STT_FUNC
);
293 // Copy a relocation against a global symbol.
295 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
296 Sized_relobj
<32, false>*, unsigned int,
297 Output_section
*, Symbol
*, const elfcpp::Rel
<32, false>&);
299 // Information about this specific target which we pass to the
300 // general Target structure.
301 static const Target::Target_info i386_info
;
304 Output_data_got
<32, false>* got_
;
306 Output_data_plt_i386
* plt_
;
307 // The GOT PLT section.
308 Output_data_space
* got_plt_
;
309 // The dynamic reloc section.
310 Reloc_section
* rel_dyn_
;
311 // Relocs saved to avoid a COPY reloc.
312 Copy_relocs
<32, false>* copy_relocs_
;
313 // Space for variables copied with a COPY reloc.
314 Output_data_space
* dynbss_
;
315 // Offset of the GOT entry for the TLS module index;
316 unsigned int got_mod_index_offset_
;
319 const Target::Target_info
Target_i386::i386_info
=
322 false, // is_big_endian
323 elfcpp::EM_386
, // machine_code
324 false, // has_make_symbol
325 false, // has_resolve
326 true, // has_code_fill
327 true, // is_default_stack_executable
328 "/usr/lib/libc.so.1", // dynamic_linker
329 0x08048000, // default_text_segment_address
330 0x1000, // abi_pagesize
331 0x1000 // common_pagesize
334 // Get the GOT section, creating it if necessary.
336 Output_data_got
<32, false>*
337 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
339 if (this->got_
== NULL
)
341 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
343 this->got_
= new Output_data_got
<32, false>();
345 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
346 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
349 // The old GNU linker creates a .got.plt section. We just
350 // create another set of data in the .got section. Note that we
351 // always create a PLT if we create a GOT, although the PLT
353 this->got_plt_
= new Output_data_space(4);
354 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
355 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
358 // The first three entries are reserved.
359 this->got_plt_
->set_current_data_size(3 * 4);
361 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
362 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL
,
364 0, 0, elfcpp::STT_OBJECT
,
366 elfcpp::STV_HIDDEN
, 0,
373 // Get the dynamic reloc section, creating it if necessary.
375 Target_i386::Reloc_section
*
376 Target_i386::rel_dyn_section(Layout
* layout
)
378 if (this->rel_dyn_
== NULL
)
380 gold_assert(layout
!= NULL
);
381 this->rel_dyn_
= new Reloc_section();
382 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
383 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
385 return this->rel_dyn_
;
388 // A class to handle the PLT data.
390 class Output_data_plt_i386
: public Output_section_data
393 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
395 Output_data_plt_i386(Layout
*, Output_data_space
*);
397 // Add an entry to the PLT.
399 add_entry(Symbol
* gsym
);
401 // Return the .rel.plt section data.
404 { return this->rel_
; }
408 do_adjust_output_section(Output_section
* os
);
411 // The size of an entry in the PLT.
412 static const int plt_entry_size
= 16;
414 // The first entry in the PLT for an executable.
415 static unsigned char exec_first_plt_entry
[plt_entry_size
];
417 // The first entry in the PLT for a shared object.
418 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
420 // Other entries in the PLT for an executable.
421 static unsigned char exec_plt_entry
[plt_entry_size
];
423 // Other entries in the PLT for a shared object.
424 static unsigned char dyn_plt_entry
[plt_entry_size
];
426 // Set the final size.
428 set_final_data_size()
429 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
431 // Write out the PLT data.
433 do_write(Output_file
*);
435 // The reloc section.
437 // The .got.plt section.
438 Output_data_space
* got_plt_
;
439 // The number of PLT entries.
443 // Create the PLT section. The ordinary .got section is an argument,
444 // since we need to refer to the start. We also create our own .got
445 // section just for PLT entries.
447 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
448 Output_data_space
* got_plt
)
449 : Output_section_data(4), got_plt_(got_plt
), count_(0)
451 this->rel_
= new Reloc_section();
452 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
453 elfcpp::SHF_ALLOC
, this->rel_
);
457 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
459 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
460 // linker, and so do we.
464 // Add an entry to the PLT.
467 Output_data_plt_i386::add_entry(Symbol
* gsym
)
469 gold_assert(!gsym
->has_plt_offset());
471 // Note that when setting the PLT offset we skip the initial
472 // reserved PLT entry.
473 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
477 section_offset_type got_offset
= this->got_plt_
->current_data_size();
479 // Every PLT entry needs a GOT entry which points back to the PLT
480 // entry (this will be changed by the dynamic linker, normally
481 // lazily when the function is called).
482 this->got_plt_
->set_current_data_size(got_offset
+ 4);
484 // Every PLT entry needs a reloc.
485 gsym
->set_needs_dynsym_entry();
486 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
489 // Note that we don't need to save the symbol. The contents of the
490 // PLT are independent of which symbols are used. The symbols only
491 // appear in the relocations.
494 // The first entry in the PLT for an executable.
496 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
498 0xff, 0x35, // pushl contents of memory address
499 0, 0, 0, 0, // replaced with address of .got + 4
500 0xff, 0x25, // jmp indirect
501 0, 0, 0, 0, // replaced with address of .got + 8
505 // The first entry in the PLT for a shared object.
507 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
509 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
510 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
514 // Subsequent entries in the PLT for an executable.
516 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
518 0xff, 0x25, // jmp indirect
519 0, 0, 0, 0, // replaced with address of symbol in .got
520 0x68, // pushl immediate
521 0, 0, 0, 0, // replaced with offset into relocation table
522 0xe9, // jmp relative
523 0, 0, 0, 0 // replaced with offset to start of .plt
526 // Subsequent entries in the PLT for a shared object.
528 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
530 0xff, 0xa3, // jmp *offset(%ebx)
531 0, 0, 0, 0, // replaced with offset of symbol in .got
532 0x68, // pushl immediate
533 0, 0, 0, 0, // replaced with offset into relocation table
534 0xe9, // jmp relative
535 0, 0, 0, 0 // replaced with offset to start of .plt
538 // Write out the PLT. This uses the hand-coded instructions above,
539 // and adjusts them as needed. This is all specified by the i386 ELF
540 // Processor Supplement.
543 Output_data_plt_i386::do_write(Output_file
* of
)
545 const off_t offset
= this->offset();
546 const section_size_type oview_size
=
547 convert_to_section_size_type(this->data_size());
548 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
550 const off_t got_file_offset
= this->got_plt_
->offset();
551 const section_size_type got_size
=
552 convert_to_section_size_type(this->got_plt_
->data_size());
553 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
556 unsigned char* pov
= oview
;
558 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
559 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
561 if (parameters
->output_is_shared())
562 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
565 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
566 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
567 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
569 pov
+= plt_entry_size
;
571 unsigned char* got_pov
= got_view
;
573 memset(got_pov
, 0, 12);
576 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
578 unsigned int plt_offset
= plt_entry_size
;
579 unsigned int plt_rel_offset
= 0;
580 unsigned int got_offset
= 12;
581 const unsigned int count
= this->count_
;
582 for (unsigned int i
= 0;
585 pov
+= plt_entry_size
,
587 plt_offset
+= plt_entry_size
,
588 plt_rel_offset
+= rel_size
,
591 // Set and adjust the PLT entry itself.
593 if (parameters
->output_is_shared())
595 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
596 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
600 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
601 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
606 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
607 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
608 - (plt_offset
+ plt_entry_size
));
610 // Set the entry in the GOT.
611 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
614 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
615 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
617 of
->write_output_view(offset
, oview_size
, oview
);
618 of
->write_output_view(got_file_offset
, got_size
, got_view
);
621 // Create a PLT entry for a global symbol.
624 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
626 if (gsym
->has_plt_offset())
629 if (this->plt_
== NULL
)
631 // Create the GOT sections first.
632 this->got_section(symtab
, layout
);
634 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
635 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
637 | elfcpp::SHF_EXECINSTR
),
641 this->plt_
->add_entry(gsym
);
644 // Create a GOT entry for the TLS module index.
647 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
648 Sized_relobj
<32, false>* object
)
650 if (this->got_mod_index_offset_
== -1U)
652 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
653 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
654 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
655 unsigned int got_offset
= got
->add_constant(0);
656 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
658 got
->add_constant(0);
659 this->got_mod_index_offset_
= got_offset
;
661 return this->got_mod_index_offset_
;
664 // Handle a relocation against a non-function symbol defined in a
665 // dynamic object. The traditional way to handle this is to generate
666 // a COPY relocation to copy the variable at runtime from the shared
667 // object into the executable's data segment. However, this is
668 // undesirable in general, as if the size of the object changes in the
669 // dynamic object, the executable will no longer work correctly. If
670 // this relocation is in a writable section, then we can create a
671 // dynamic reloc and the dynamic linker will resolve it to the correct
672 // address at runtime. However, we do not want do that if the
673 // relocation is in a read-only section, as it would prevent the
674 // readonly segment from being shared. And if we have to eventually
675 // generate a COPY reloc, then any dynamic relocations will be
676 // useless. So this means that if this is a writable section, we need
677 // to save the relocation until we see whether we have to create a
678 // COPY relocation for this symbol for any other relocation.
681 Target_i386::copy_reloc(const General_options
* options
,
682 Symbol_table
* symtab
,
684 Sized_relobj
<32, false>* object
,
685 unsigned int data_shndx
,
686 Output_section
* output_section
,
688 const elfcpp::Rel
<32, false>& rel
)
690 Sized_symbol
<32>* ssym
;
691 ssym
= symtab
->get_sized_symbol
SELECT_SIZE_NAME(32) (gsym
694 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
697 // So far we do not need a COPY reloc. Save this relocation.
698 // If it turns out that we never need a COPY reloc for this
699 // symbol, then we will emit the relocation.
700 if (this->copy_relocs_
== NULL
)
701 this->copy_relocs_
= new Copy_relocs
<32, false>();
702 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rel
);
706 // Allocate space for this symbol in the .bss section.
708 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
710 // There is no defined way to determine the required alignment
711 // of the symbol. We pick the alignment based on the size. We
712 // set an arbitrary maximum of 256.
714 for (align
= 1; align
< 512; align
<<= 1)
715 if ((symsize
& align
) != 0)
718 if (this->dynbss_
== NULL
)
720 this->dynbss_
= new Output_data_space(align
);
721 layout
->add_output_section_data(".bss",
724 | elfcpp::SHF_WRITE
),
728 Output_data_space
* dynbss
= this->dynbss_
;
730 if (align
> dynbss
->addralign())
731 dynbss
->set_space_alignment(align
);
733 section_size_type dynbss_size
=
734 convert_to_section_size_type(dynbss
->current_data_size());
735 dynbss_size
= align_address(dynbss_size
, align
);
736 section_size_type offset
= dynbss_size
;
737 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
739 symtab
->define_with_copy_reloc(this, ssym
, dynbss
, offset
);
741 // Add the COPY reloc.
742 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
743 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
747 // Optimize the TLS relocation type based on what we know about the
748 // symbol. IS_FINAL is true if the final address of this symbol is
749 // known at link time.
751 tls::Tls_optimization
752 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
754 // If we are generating a shared library, then we can't do anything
756 if (parameters
->output_is_shared())
757 return tls::TLSOPT_NONE
;
761 case elfcpp::R_386_TLS_GD
:
762 case elfcpp::R_386_TLS_GOTDESC
:
763 case elfcpp::R_386_TLS_DESC_CALL
:
764 // These are General-Dynamic which permits fully general TLS
765 // access. Since we know that we are generating an executable,
766 // we can convert this to Initial-Exec. If we also know that
767 // this is a local symbol, we can further switch to Local-Exec.
769 return tls::TLSOPT_TO_LE
;
770 return tls::TLSOPT_TO_IE
;
772 case elfcpp::R_386_TLS_LDM
:
773 // This is Local-Dynamic, which refers to a local symbol in the
774 // dynamic TLS block. Since we know that we generating an
775 // executable, we can switch to Local-Exec.
776 return tls::TLSOPT_TO_LE
;
778 case elfcpp::R_386_TLS_LDO_32
:
779 // Another type of Local-Dynamic relocation.
780 return tls::TLSOPT_TO_LE
;
782 case elfcpp::R_386_TLS_IE
:
783 case elfcpp::R_386_TLS_GOTIE
:
784 case elfcpp::R_386_TLS_IE_32
:
785 // These are Initial-Exec relocs which get the thread offset
786 // from the GOT. If we know that we are linking against the
787 // local symbol, we can switch to Local-Exec, which links the
788 // thread offset into the instruction.
790 return tls::TLSOPT_TO_LE
;
791 return tls::TLSOPT_NONE
;
793 case elfcpp::R_386_TLS_LE
:
794 case elfcpp::R_386_TLS_LE_32
:
795 // When we already have Local-Exec, there is nothing further we
797 return tls::TLSOPT_NONE
;
804 // Report an unsupported relocation against a local symbol.
807 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
810 gold_error(_("%s: unsupported reloc %u against local symbol"),
811 object
->name().c_str(), r_type
);
814 // Scan a relocation for a local symbol.
817 Target_i386::Scan::local(const General_options
&,
818 Symbol_table
* symtab
,
821 Sized_relobj
<32, false>* object
,
822 unsigned int data_shndx
,
823 Output_section
* output_section
,
824 const elfcpp::Rel
<32, false>& reloc
,
826 const elfcpp::Sym
<32, false>& lsym
)
830 case elfcpp::R_386_NONE
:
831 case elfcpp::R_386_GNU_VTINHERIT
:
832 case elfcpp::R_386_GNU_VTENTRY
:
835 case elfcpp::R_386_32
:
836 // If building a shared library (or a position-independent
837 // executable), we need to create a dynamic relocation for
838 // this location. The relocation applied at link time will
839 // apply the link-time value, so we flag the location with
840 // an R_386_RELATIVE relocation so the dynamic loader can
841 // relocate it easily.
842 if (parameters
->output_is_position_independent())
844 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
845 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
846 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
847 output_section
, data_shndx
,
848 reloc
.get_r_offset());
852 case elfcpp::R_386_16
:
853 case elfcpp::R_386_8
:
854 // If building a shared library (or a position-independent
855 // executable), we need to create a dynamic relocation for
856 // this location. Because the addend needs to remain in the
857 // data section, we need to be careful not to apply this
858 // relocation statically.
859 if (parameters
->output_is_position_independent())
861 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
862 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
863 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
, data_shndx
,
864 reloc
.get_r_offset());
868 case elfcpp::R_386_PC32
:
869 case elfcpp::R_386_PC16
:
870 case elfcpp::R_386_PC8
:
873 case elfcpp::R_386_PLT32
:
874 // Since we know this is a local symbol, we can handle this as a
878 case elfcpp::R_386_GOTOFF
:
879 case elfcpp::R_386_GOTPC
:
880 // We need a GOT section.
881 target
->got_section(symtab
, layout
);
884 case elfcpp::R_386_GOT32
:
886 // The symbol requires a GOT entry.
887 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
888 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
889 if (got
->add_local(object
, r_sym
))
891 // If we are generating a shared object, we need to add a
892 // dynamic RELATIVE relocation for this symbol's GOT entry.
893 if (parameters
->output_is_position_independent())
895 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
896 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
897 rel_dyn
->add_local_relative(object
, r_sym
,
898 elfcpp::R_386_RELATIVE
,
900 object
->local_got_offset(r_sym
));
906 // These are relocations which should only be seen by the
907 // dynamic linker, and should never be seen here.
908 case elfcpp::R_386_COPY
:
909 case elfcpp::R_386_GLOB_DAT
:
910 case elfcpp::R_386_JUMP_SLOT
:
911 case elfcpp::R_386_RELATIVE
:
912 case elfcpp::R_386_TLS_TPOFF
:
913 case elfcpp::R_386_TLS_DTPMOD32
:
914 case elfcpp::R_386_TLS_DTPOFF32
:
915 case elfcpp::R_386_TLS_TPOFF32
:
916 case elfcpp::R_386_TLS_DESC
:
917 gold_error(_("%s: unexpected reloc %u in object file"),
918 object
->name().c_str(), r_type
);
921 // These are initial TLS relocs, which are expected when
923 case elfcpp::R_386_TLS_GD
: // Global-dynamic
924 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
925 case elfcpp::R_386_TLS_DESC_CALL
:
926 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
927 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
928 case elfcpp::R_386_TLS_IE
: // Initial-exec
929 case elfcpp::R_386_TLS_IE_32
:
930 case elfcpp::R_386_TLS_GOTIE
:
931 case elfcpp::R_386_TLS_LE
: // Local-exec
932 case elfcpp::R_386_TLS_LE_32
:
934 bool output_is_shared
= parameters
->output_is_shared();
935 const tls::Tls_optimization optimized_type
936 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
939 case elfcpp::R_386_TLS_GD
: // Global-dynamic
940 if (optimized_type
== tls::TLSOPT_NONE
)
942 // Create a pair of GOT entries for the module index and
943 // dtv-relative offset.
944 Output_data_got
<32, false>* got
945 = target
->got_section(symtab
, layout
);
946 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
947 got
->add_local_tls_with_rel(object
, r_sym
,
948 lsym
.get_st_shndx(), true,
949 target
->rel_dyn_section(layout
),
950 elfcpp::R_386_TLS_DTPMOD32
);
952 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
953 unsupported_reloc_local(object
, r_type
);
956 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
957 case elfcpp::R_386_TLS_DESC_CALL
:
958 // FIXME: If not relaxing to LE, we need to generate
959 // a GOT entry with an R_386_TLS_DESC reloc.
960 if (optimized_type
!= tls::TLSOPT_TO_LE
)
961 unsupported_reloc_local(object
, r_type
);
964 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
965 if (optimized_type
== tls::TLSOPT_NONE
)
967 // Create a GOT entry for the module index.
968 target
->got_mod_index_entry(symtab
, layout
, object
);
970 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
971 unsupported_reloc_local(object
, r_type
);
974 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
977 case elfcpp::R_386_TLS_IE
: // Initial-exec
978 case elfcpp::R_386_TLS_IE_32
:
979 case elfcpp::R_386_TLS_GOTIE
:
980 layout
->set_has_static_tls();
981 if (optimized_type
== tls::TLSOPT_NONE
)
983 // For the R_386_TLS_IE relocation, we need to create a
984 // dynamic relocation when building a shared library.
985 if (r_type
== elfcpp::R_386_TLS_IE
986 && parameters
->output_is_shared())
988 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
990 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
991 rel_dyn
->add_local_relative(object
, r_sym
,
992 elfcpp::R_386_RELATIVE
,
993 output_section
, data_shndx
,
994 reloc
.get_r_offset());
996 // Create a GOT entry for the tp-relative offset.
997 Output_data_got
<32, false>* got
998 = target
->got_section(symtab
, layout
);
999 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1000 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1001 ? elfcpp::R_386_TLS_TPOFF32
1002 : elfcpp::R_386_TLS_TPOFF
);
1003 got
->add_local_with_rel(object
, r_sym
,
1004 target
->rel_dyn_section(layout
),
1007 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1008 unsupported_reloc_local(object
, r_type
);
1011 case elfcpp::R_386_TLS_LE
: // Local-exec
1012 case elfcpp::R_386_TLS_LE_32
:
1013 layout
->set_has_static_tls();
1014 if (output_is_shared
)
1016 // We need to create a dynamic relocation.
1017 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1018 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1019 ? elfcpp::R_386_TLS_TPOFF32
1020 : elfcpp::R_386_TLS_TPOFF
);
1021 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1022 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1023 data_shndx
, reloc
.get_r_offset());
1033 case elfcpp::R_386_32PLT
:
1034 case elfcpp::R_386_TLS_GD_32
:
1035 case elfcpp::R_386_TLS_GD_PUSH
:
1036 case elfcpp::R_386_TLS_GD_CALL
:
1037 case elfcpp::R_386_TLS_GD_POP
:
1038 case elfcpp::R_386_TLS_LDM_32
:
1039 case elfcpp::R_386_TLS_LDM_PUSH
:
1040 case elfcpp::R_386_TLS_LDM_CALL
:
1041 case elfcpp::R_386_TLS_LDM_POP
:
1042 case elfcpp::R_386_USED_BY_INTEL_200
:
1044 unsupported_reloc_local(object
, r_type
);
1049 // Report an unsupported relocation against a global symbol.
1052 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1053 unsigned int r_type
,
1056 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1057 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1060 // Scan a relocation for a global symbol.
1063 Target_i386::Scan::global(const General_options
& options
,
1064 Symbol_table
* symtab
,
1066 Target_i386
* target
,
1067 Sized_relobj
<32, false>* object
,
1068 unsigned int data_shndx
,
1069 Output_section
* output_section
,
1070 const elfcpp::Rel
<32, false>& reloc
,
1071 unsigned int r_type
,
1076 case elfcpp::R_386_NONE
:
1077 case elfcpp::R_386_GNU_VTINHERIT
:
1078 case elfcpp::R_386_GNU_VTENTRY
:
1081 case elfcpp::R_386_32
:
1082 case elfcpp::R_386_16
:
1083 case elfcpp::R_386_8
:
1085 // Make a PLT entry if necessary.
1086 if (gsym
->needs_plt_entry())
1088 target
->make_plt_entry(symtab
, layout
, gsym
);
1089 // Since this is not a PC-relative relocation, we may be
1090 // taking the address of a function. In that case we need to
1091 // set the entry in the dynamic symbol table to the address of
1093 if (gsym
->is_from_dynobj())
1094 gsym
->set_needs_dynsym_value();
1096 // Make a dynamic relocation if necessary.
1097 if (gsym
->needs_dynamic_reloc(true, false))
1099 if (target
->may_need_copy_reloc(gsym
))
1101 target
->copy_reloc(&options
, symtab
, layout
, object
,
1102 data_shndx
, output_section
, gsym
, reloc
);
1104 else if (r_type
== elfcpp::R_386_32
1105 && gsym
->can_use_relative_reloc(false))
1107 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1108 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1109 output_section
, object
,
1110 data_shndx
, reloc
.get_r_offset());
1114 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1115 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1116 data_shndx
, reloc
.get_r_offset());
1122 case elfcpp::R_386_PC32
:
1123 case elfcpp::R_386_PC16
:
1124 case elfcpp::R_386_PC8
:
1126 // Make a PLT entry if necessary.
1127 if (gsym
->needs_plt_entry())
1129 // These relocations are used for function calls only in
1130 // non-PIC code. For a 32-bit relocation in a shared library,
1131 // we'll need a text relocation anyway, so we can skip the
1132 // PLT entry and let the dynamic linker bind the call directly
1133 // to the target. For smaller relocations, we should use a
1134 // PLT entry to ensure that the call can reach.
1135 if (!parameters
->output_is_shared()
1136 || r_type
!= elfcpp::R_386_PC32
)
1137 target
->make_plt_entry(symtab
, layout
, gsym
);
1139 // Make a dynamic relocation if necessary.
1140 bool is_function_call
= (gsym
->type() == elfcpp::STT_FUNC
);
1141 if (gsym
->needs_dynamic_reloc(false, is_function_call
))
1143 if (target
->may_need_copy_reloc(gsym
))
1145 target
->copy_reloc(&options
, symtab
, layout
, object
,
1146 data_shndx
, output_section
, gsym
, reloc
);
1150 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1151 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1152 data_shndx
, reloc
.get_r_offset());
1158 case elfcpp::R_386_GOT32
:
1160 // The symbol requires a GOT entry.
1161 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1162 if (gsym
->final_value_is_known())
1163 got
->add_global(gsym
);
1166 // If this symbol is not fully resolved, we need to add a
1167 // GOT entry with a dynamic relocation.
1168 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1169 if (gsym
->is_from_dynobj() || gsym
->is_preemptible())
1170 got
->add_global_with_rel(gsym
, rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1173 if (got
->add_global(gsym
))
1174 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1175 got
, gsym
->got_offset());
1181 case elfcpp::R_386_PLT32
:
1182 // If the symbol is fully resolved, this is just a PC32 reloc.
1183 // Otherwise we need a PLT entry.
1184 if (gsym
->final_value_is_known())
1186 // If building a shared library, we can also skip the PLT entry
1187 // if the symbol is defined in the output file and is protected
1189 if (gsym
->is_defined()
1190 && !gsym
->is_from_dynobj()
1191 && !gsym
->is_preemptible())
1193 target
->make_plt_entry(symtab
, layout
, gsym
);
1196 case elfcpp::R_386_GOTOFF
:
1197 case elfcpp::R_386_GOTPC
:
1198 // We need a GOT section.
1199 target
->got_section(symtab
, layout
);
1202 // These are relocations which should only be seen by the
1203 // dynamic linker, and should never be seen here.
1204 case elfcpp::R_386_COPY
:
1205 case elfcpp::R_386_GLOB_DAT
:
1206 case elfcpp::R_386_JUMP_SLOT
:
1207 case elfcpp::R_386_RELATIVE
:
1208 case elfcpp::R_386_TLS_TPOFF
:
1209 case elfcpp::R_386_TLS_DTPMOD32
:
1210 case elfcpp::R_386_TLS_DTPOFF32
:
1211 case elfcpp::R_386_TLS_TPOFF32
:
1212 case elfcpp::R_386_TLS_DESC
:
1213 gold_error(_("%s: unexpected reloc %u in object file"),
1214 object
->name().c_str(), r_type
);
1217 // These are initial tls relocs, which are expected when
1219 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1220 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1221 case elfcpp::R_386_TLS_DESC_CALL
:
1222 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1223 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1224 case elfcpp::R_386_TLS_IE
: // Initial-exec
1225 case elfcpp::R_386_TLS_IE_32
:
1226 case elfcpp::R_386_TLS_GOTIE
:
1227 case elfcpp::R_386_TLS_LE
: // Local-exec
1228 case elfcpp::R_386_TLS_LE_32
:
1230 const bool is_final
= gsym
->final_value_is_known();
1231 const tls::Tls_optimization optimized_type
1232 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1235 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1236 if (optimized_type
== tls::TLSOPT_NONE
)
1238 // Create a pair of GOT entries for the module index and
1239 // dtv-relative offset.
1240 Output_data_got
<32, false>* got
1241 = target
->got_section(symtab
, layout
);
1242 got
->add_global_tls_with_rel(gsym
,
1243 target
->rel_dyn_section(layout
),
1244 elfcpp::R_386_TLS_DTPMOD32
,
1245 elfcpp::R_386_TLS_DTPOFF32
);
1247 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1249 // Create a GOT entry for the tp-relative offset.
1250 Output_data_got
<32, false>* got
1251 = target
->got_section(symtab
, layout
);
1252 got
->add_global_with_rel(gsym
, target
->rel_dyn_section(layout
),
1253 elfcpp::R_386_TLS_TPOFF32
);
1255 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1256 unsupported_reloc_global(object
, r_type
, gsym
);
1259 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1260 case elfcpp::R_386_TLS_DESC_CALL
:
1261 // FIXME: If not relaxing to LE, we need to generate
1262 // a GOT entry with an R_386_TLS_DESC reloc.
1263 if (optimized_type
!= tls::TLSOPT_TO_LE
)
1264 unsupported_reloc_global(object
, r_type
, gsym
);
1265 unsupported_reloc_global(object
, r_type
, gsym
);
1268 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1269 if (optimized_type
== tls::TLSOPT_NONE
)
1271 // Create a GOT entry for the module index.
1272 target
->got_mod_index_entry(symtab
, layout
, object
);
1274 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1275 unsupported_reloc_global(object
, r_type
, gsym
);
1278 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1281 case elfcpp::R_386_TLS_IE
: // Initial-exec
1282 case elfcpp::R_386_TLS_IE_32
:
1283 case elfcpp::R_386_TLS_GOTIE
:
1284 layout
->set_has_static_tls();
1285 if (optimized_type
== tls::TLSOPT_NONE
)
1287 // For the R_386_TLS_IE relocation, we need to create a
1288 // dynamic relocation when building a shared library.
1289 if (r_type
== elfcpp::R_386_TLS_IE
1290 && parameters
->output_is_shared())
1292 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1293 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1294 output_section
, object
,
1296 reloc
.get_r_offset());
1298 // Create a GOT entry for the tp-relative offset.
1299 Output_data_got
<32, false>* got
1300 = target
->got_section(symtab
, layout
);
1301 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1302 ? elfcpp::R_386_TLS_TPOFF32
1303 : elfcpp::R_386_TLS_TPOFF
);
1304 got
->add_global_with_rel(gsym
,
1305 target
->rel_dyn_section(layout
),
1308 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1309 unsupported_reloc_global(object
, r_type
, gsym
);
1312 case elfcpp::R_386_TLS_LE
: // Local-exec
1313 case elfcpp::R_386_TLS_LE_32
:
1314 layout
->set_has_static_tls();
1315 if (parameters
->output_is_shared())
1317 // We need to create a dynamic relocation.
1318 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1319 ? elfcpp::R_386_TLS_TPOFF32
1320 : elfcpp::R_386_TLS_TPOFF
);
1321 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1322 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1323 data_shndx
, reloc
.get_r_offset());
1333 case elfcpp::R_386_32PLT
:
1334 case elfcpp::R_386_TLS_GD_32
:
1335 case elfcpp::R_386_TLS_GD_PUSH
:
1336 case elfcpp::R_386_TLS_GD_CALL
:
1337 case elfcpp::R_386_TLS_GD_POP
:
1338 case elfcpp::R_386_TLS_LDM_32
:
1339 case elfcpp::R_386_TLS_LDM_PUSH
:
1340 case elfcpp::R_386_TLS_LDM_CALL
:
1341 case elfcpp::R_386_TLS_LDM_POP
:
1342 case elfcpp::R_386_USED_BY_INTEL_200
:
1344 unsupported_reloc_global(object
, r_type
, gsym
);
1349 // Scan relocations for a section.
1352 Target_i386::scan_relocs(const General_options
& options
,
1353 Symbol_table
* symtab
,
1355 Sized_relobj
<32, false>* object
,
1356 unsigned int data_shndx
,
1357 unsigned int sh_type
,
1358 const unsigned char* prelocs
,
1360 Output_section
* output_section
,
1361 bool needs_special_offset_handling
,
1362 size_t local_symbol_count
,
1363 const unsigned char* plocal_symbols
)
1365 if (sh_type
== elfcpp::SHT_RELA
)
1367 gold_error(_("%s: unsupported RELA reloc section"),
1368 object
->name().c_str());
1372 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1383 needs_special_offset_handling
,
1388 // Finalize the sections.
1391 Target_i386::do_finalize_sections(Layout
* layout
)
1393 // Fill in some more dynamic tags.
1394 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1397 if (this->got_plt_
!= NULL
)
1398 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1400 if (this->plt_
!= NULL
)
1402 const Output_data
* od
= this->plt_
->rel_plt();
1403 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1404 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1405 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1408 if (this->rel_dyn_
!= NULL
)
1410 const Output_data
* od
= this->rel_dyn_
;
1411 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1412 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1413 odyn
->add_constant(elfcpp::DT_RELENT
,
1414 elfcpp::Elf_sizes
<32>::rel_size
);
1417 if (!parameters
->output_is_shared())
1419 // The value of the DT_DEBUG tag is filled in by the dynamic
1420 // linker at run time, and used by the debugger.
1421 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1425 // Emit any relocs we saved in an attempt to avoid generating COPY
1427 if (this->copy_relocs_
== NULL
)
1429 if (this->copy_relocs_
->any_to_emit())
1431 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1432 this->copy_relocs_
->emit(rel_dyn
);
1434 delete this->copy_relocs_
;
1435 this->copy_relocs_
= NULL
;
1438 // Return whether a direct absolute static relocation needs to be applied.
1439 // In cases where Scan::local() or Scan::global() has created
1440 // a dynamic relocation other than R_386_RELATIVE, the addend
1441 // of the relocation is carried in the data, and we must not
1442 // apply the static relocation.
1445 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1446 bool is_absolute_ref
,
1447 bool is_function_call
,
1450 // For local symbols, we will have created a non-RELATIVE dynamic
1451 // relocation only if (a) the output is position independent,
1452 // (b) the relocation is absolute (not pc- or segment-relative), and
1453 // (c) the relocation is not 32 bits wide.
1455 return !(parameters
->output_is_position_independent()
1459 // For global symbols, we use the same helper routines used in the scan pass.
1460 return !(gsym
->needs_dynamic_reloc(is_absolute_ref
, is_function_call
)
1461 && !gsym
->can_use_relative_reloc(is_function_call
));
1464 // Perform a relocation.
1467 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1468 Target_i386
* target
,
1470 const elfcpp::Rel
<32, false>& rel
,
1471 unsigned int r_type
,
1472 const Sized_symbol
<32>* gsym
,
1473 const Symbol_value
<32>* psymval
,
1474 unsigned char* view
,
1475 elfcpp::Elf_types
<32>::Elf_Addr address
,
1476 section_size_type view_size
)
1478 if (this->skip_call_tls_get_addr_
)
1480 if (r_type
!= elfcpp::R_386_PLT32
1482 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1483 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1484 _("missing expected TLS relocation"));
1487 this->skip_call_tls_get_addr_
= false;
1492 // Pick the value to use for symbols defined in shared objects.
1493 Symbol_value
<32> symval
;
1495 && (gsym
->is_from_dynobj()
1496 || (parameters
->output_is_shared()
1497 && gsym
->is_preemptible()))
1498 && gsym
->has_plt_offset())
1500 symval
.set_output_value(target
->plt_section()->address()
1501 + gsym
->plt_offset());
1505 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1507 // Get the GOT offset if needed.
1508 // The GOT pointer points to the end of the GOT section.
1509 // We need to subtract the size of the GOT section to get
1510 // the actual offset to use in the relocation.
1511 bool have_got_offset
= false;
1512 unsigned int got_offset
= 0;
1515 case elfcpp::R_386_GOT32
:
1518 gold_assert(gsym
->has_got_offset());
1519 got_offset
= gsym
->got_offset() - target
->got_size();
1523 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1524 gold_assert(object
->local_has_got_offset(r_sym
));
1525 got_offset
= object
->local_got_offset(r_sym
) - target
->got_size();
1527 have_got_offset
= true;
1536 case elfcpp::R_386_NONE
:
1537 case elfcpp::R_386_GNU_VTINHERIT
:
1538 case elfcpp::R_386_GNU_VTENTRY
:
1541 case elfcpp::R_386_32
:
1542 if (should_apply_static_reloc(gsym
, true, false, true))
1543 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1546 case elfcpp::R_386_PC32
:
1548 bool is_function_call
= (gsym
!= NULL
1549 && gsym
->type() == elfcpp::STT_FUNC
);
1550 if (should_apply_static_reloc(gsym
, false, is_function_call
, true))
1551 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1555 case elfcpp::R_386_16
:
1556 if (should_apply_static_reloc(gsym
, true, false, false))
1557 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1560 case elfcpp::R_386_PC16
:
1562 bool is_function_call
= (gsym
!= NULL
1563 && gsym
->type() == elfcpp::STT_FUNC
);
1564 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1565 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1569 case elfcpp::R_386_8
:
1570 if (should_apply_static_reloc(gsym
, true, false, false))
1571 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1574 case elfcpp::R_386_PC8
:
1576 bool is_function_call
= (gsym
!= NULL
1577 && gsym
->type() == elfcpp::STT_FUNC
);
1578 if (should_apply_static_reloc(gsym
, false, is_function_call
, false))
1579 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1583 case elfcpp::R_386_PLT32
:
1584 gold_assert(gsym
== NULL
1585 || gsym
->has_plt_offset()
1586 || gsym
->final_value_is_known()
1587 || (gsym
->is_defined()
1588 && !gsym
->is_from_dynobj()
1589 && !gsym
->is_preemptible()));
1590 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1593 case elfcpp::R_386_GOT32
:
1594 gold_assert(have_got_offset
);
1595 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1598 case elfcpp::R_386_GOTOFF
:
1600 elfcpp::Elf_types
<32>::Elf_Addr value
;
1601 value
= (psymval
->value(object
, 0)
1602 - target
->got_plt_section()->address());
1603 Relocate_functions
<32, false>::rel32(view
, value
);
1607 case elfcpp::R_386_GOTPC
:
1609 elfcpp::Elf_types
<32>::Elf_Addr value
;
1610 value
= target
->got_plt_section()->address();
1611 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1615 case elfcpp::R_386_COPY
:
1616 case elfcpp::R_386_GLOB_DAT
:
1617 case elfcpp::R_386_JUMP_SLOT
:
1618 case elfcpp::R_386_RELATIVE
:
1619 // These are outstanding tls relocs, which are unexpected when
1621 case elfcpp::R_386_TLS_TPOFF
:
1622 case elfcpp::R_386_TLS_DTPMOD32
:
1623 case elfcpp::R_386_TLS_DTPOFF32
:
1624 case elfcpp::R_386_TLS_TPOFF32
:
1625 case elfcpp::R_386_TLS_DESC
:
1626 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1627 _("unexpected reloc %u in object file"),
1631 // These are initial tls relocs, which are expected when
1633 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1634 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1635 case elfcpp::R_386_TLS_DESC_CALL
:
1636 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1637 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1638 case elfcpp::R_386_TLS_IE
: // Initial-exec
1639 case elfcpp::R_386_TLS_IE_32
:
1640 case elfcpp::R_386_TLS_GOTIE
:
1641 case elfcpp::R_386_TLS_LE
: // Local-exec
1642 case elfcpp::R_386_TLS_LE_32
:
1643 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1644 view
, address
, view_size
);
1647 case elfcpp::R_386_32PLT
:
1648 case elfcpp::R_386_TLS_GD_32
:
1649 case elfcpp::R_386_TLS_GD_PUSH
:
1650 case elfcpp::R_386_TLS_GD_CALL
:
1651 case elfcpp::R_386_TLS_GD_POP
:
1652 case elfcpp::R_386_TLS_LDM_32
:
1653 case elfcpp::R_386_TLS_LDM_PUSH
:
1654 case elfcpp::R_386_TLS_LDM_CALL
:
1655 case elfcpp::R_386_TLS_LDM_POP
:
1656 case elfcpp::R_386_USED_BY_INTEL_200
:
1658 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1659 _("unsupported reloc %u"),
1667 // Perform a TLS relocation.
1670 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1671 Target_i386
* target
,
1673 const elfcpp::Rel
<32, false>& rel
,
1674 unsigned int r_type
,
1675 const Sized_symbol
<32>* gsym
,
1676 const Symbol_value
<32>* psymval
,
1677 unsigned char* view
,
1678 elfcpp::Elf_types
<32>::Elf_Addr
,
1679 section_size_type view_size
)
1681 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1683 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1685 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1687 const bool is_final
= (gsym
== NULL
1688 ? !parameters
->output_is_position_independent()
1689 : gsym
->final_value_is_known());
1690 const tls::Tls_optimization optimized_type
1691 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1694 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1695 if (optimized_type
== tls::TLSOPT_TO_LE
)
1697 gold_assert(tls_segment
!= NULL
);
1698 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1699 rel
, r_type
, value
, view
,
1705 unsigned int got_offset
;
1708 gold_assert(gsym
->has_tls_got_offset(true));
1709 got_offset
= gsym
->tls_got_offset(true) - target
->got_size();
1713 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1714 gold_assert(object
->local_has_tls_got_offset(r_sym
, true));
1715 got_offset
= (object
->local_tls_got_offset(r_sym
, true)
1716 - target
->got_size());
1718 if (optimized_type
== tls::TLSOPT_TO_IE
)
1720 gold_assert(tls_segment
!= NULL
);
1721 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1722 got_offset
, view
, view_size
);
1725 else if (optimized_type
== tls::TLSOPT_NONE
)
1727 // Relocate the field with the offset of the pair of GOT
1729 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1733 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1734 _("unsupported reloc %u"),
1738 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1739 case elfcpp::R_386_TLS_DESC_CALL
:
1740 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1741 _("unsupported reloc %u"),
1745 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1746 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1748 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1749 _("both SUN and GNU model "
1750 "TLS relocations"));
1753 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1754 if (optimized_type
== tls::TLSOPT_TO_LE
)
1756 gold_assert(tls_segment
!= NULL
);
1757 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1758 value
, view
, view_size
);
1761 else if (optimized_type
== tls::TLSOPT_NONE
)
1763 // Relocate the field with the offset of the GOT entry for
1764 // the module index.
1765 unsigned int got_offset
;
1766 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1767 - target
->got_size());
1768 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1771 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1772 _("unsupported reloc %u"),
1776 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1777 // This reloc can appear in debugging sections, in which case we
1778 // won't see the TLS_LDM reloc. The local_dynamic_type field
1780 if (optimized_type
== tls::TLSOPT_TO_LE
)
1782 gold_assert(tls_segment
!= NULL
);
1783 value
-= tls_segment
->memsz();
1785 Relocate_functions
<32, false>::rel32(view
, value
);
1788 case elfcpp::R_386_TLS_IE
: // Initial-exec
1789 case elfcpp::R_386_TLS_GOTIE
:
1790 case elfcpp::R_386_TLS_IE_32
:
1791 if (optimized_type
== tls::TLSOPT_TO_LE
)
1793 gold_assert(tls_segment
!= NULL
);
1794 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1795 rel
, r_type
, value
, view
,
1799 else if (optimized_type
== tls::TLSOPT_NONE
)
1801 // Relocate the field with the offset of the GOT entry for
1802 // the tp-relative offset of the symbol.
1803 unsigned int got_offset
;
1806 gold_assert(gsym
->has_got_offset());
1807 got_offset
= gsym
->got_offset();
1811 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1812 gold_assert(object
->local_has_got_offset(r_sym
));
1813 got_offset
= object
->local_got_offset(r_sym
);
1815 // For the R_386_TLS_IE relocation, we need to apply the
1816 // absolute address of the GOT entry.
1817 if (r_type
== elfcpp::R_386_TLS_IE
)
1818 got_offset
+= target
->got_plt_section()->address();
1819 // All GOT offsets are relative to the end of the GOT.
1820 got_offset
-= target
->got_size();
1821 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1824 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1825 _("unsupported reloc %u"),
1829 case elfcpp::R_386_TLS_LE
: // Local-exec
1830 // If we're creating a shared library, a dynamic relocation will
1831 // have been created for this location, so do not apply it now.
1832 if (!parameters
->output_is_shared())
1834 gold_assert(tls_segment
!= NULL
);
1835 value
-= tls_segment
->memsz();
1836 Relocate_functions
<32, false>::rel32(view
, value
);
1840 case elfcpp::R_386_TLS_LE_32
:
1841 // If we're creating a shared library, a dynamic relocation will
1842 // have been created for this location, so do not apply it now.
1843 if (!parameters
->output_is_shared())
1845 gold_assert(tls_segment
!= NULL
);
1846 value
= tls_segment
->memsz() - value
;
1847 Relocate_functions
<32, false>::rel32(view
, value
);
1853 // Do a relocation in which we convert a TLS General-Dynamic to a
1857 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
1859 Output_segment
* tls_segment
,
1860 const elfcpp::Rel
<32, false>& rel
,
1862 elfcpp::Elf_types
<32>::Elf_Addr value
,
1863 unsigned char* view
,
1864 section_size_type view_size
)
1866 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1867 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1868 // leal foo(%reg),%eax; call ___tls_get_addr
1869 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1871 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1872 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1874 unsigned char op1
= view
[-1];
1875 unsigned char op2
= view
[-2];
1877 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1878 op2
== 0x8d || op2
== 0x04);
1879 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1885 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1886 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1887 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1888 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1889 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1893 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1894 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1895 if (rel
.get_r_offset() + 9 < view_size
1898 // There is a trailing nop. Use the size byte subl.
1899 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1904 // Use the five byte subl.
1905 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1909 value
= tls_segment
->memsz() - value
;
1910 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1912 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1914 this->skip_call_tls_get_addr_
= true;
1917 // Do a relocation in which we convert a TLS General-Dynamic to an
1921 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
1923 Output_segment
* tls_segment
,
1924 const elfcpp::Rel
<32, false>& rel
,
1926 elfcpp::Elf_types
<32>::Elf_Addr value
,
1927 unsigned char* view
,
1928 section_size_type view_size
)
1930 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
1931 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
1933 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
1934 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
1936 unsigned char op1
= view
[-1];
1937 unsigned char op2
= view
[-2];
1939 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1940 op2
== 0x8d || op2
== 0x04);
1941 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
1945 // FIXME: For now, support only one form.
1946 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1947 op1
== 0x8d && op2
== 0x04);
1951 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
1952 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
1953 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1954 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
1955 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
1959 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
1960 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
1961 if (rel
.get_r_offset() + 9 < view_size
1964 // FIXME: This is not the right instruction sequence.
1965 // There is a trailing nop. Use the size byte subl.
1966 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1971 // FIXME: This is not the right instruction sequence.
1972 // Use the five byte subl.
1973 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1977 value
= tls_segment
->memsz() - value
;
1978 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
1980 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1982 this->skip_call_tls_get_addr_
= true;
1985 // Do a relocation in which we convert a TLS Local-Dynamic to a
1989 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
1992 const elfcpp::Rel
<32, false>& rel
,
1994 elfcpp::Elf_types
<32>::Elf_Addr
,
1995 unsigned char* view
,
1996 section_size_type view_size
)
1998 // leal foo(%reg), %eax; call ___tls_get_addr
1999 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2001 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2002 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2004 // FIXME: Does this test really always pass?
2005 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2006 view
[-2] == 0x8d && view
[-1] == 0x83);
2008 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2010 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2012 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2014 this->skip_call_tls_get_addr_
= true;
2017 // Do a relocation in which we convert a TLS Initial-Exec to a
2021 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2023 Output_segment
* tls_segment
,
2024 const elfcpp::Rel
<32, false>& rel
,
2025 unsigned int r_type
,
2026 elfcpp::Elf_types
<32>::Elf_Addr value
,
2027 unsigned char* view
,
2028 section_size_type view_size
)
2030 // We have to actually change the instructions, which means that we
2031 // need to examine the opcodes to figure out which instruction we
2033 if (r_type
== elfcpp::R_386_TLS_IE
)
2035 // movl %gs:XX,%eax ==> movl $YY,%eax
2036 // movl %gs:XX,%reg ==> movl $YY,%reg
2037 // addl %gs:XX,%reg ==> addl $YY,%reg
2038 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2039 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2041 unsigned char op1
= view
[-1];
2044 // movl XX,%eax ==> movl $YY,%eax
2049 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2051 unsigned char op2
= view
[-2];
2054 // movl XX,%reg ==> movl $YY,%reg
2055 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2056 (op1
& 0xc7) == 0x05);
2058 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2060 else if (op2
== 0x03)
2062 // addl XX,%reg ==> addl $YY,%reg
2063 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2064 (op1
& 0xc7) == 0x05);
2066 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2069 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2074 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2075 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2076 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2077 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2078 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2080 unsigned char op1
= view
[-1];
2081 unsigned char op2
= view
[-2];
2082 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2083 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2086 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2088 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2090 else if (op2
== 0x2b)
2092 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2094 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2096 else if (op2
== 0x03)
2098 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2100 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2103 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2106 value
= tls_segment
->memsz() - value
;
2107 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2110 Relocate_functions
<32, false>::rel32(view
, value
);
2113 // Relocate section data.
2116 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2117 unsigned int sh_type
,
2118 const unsigned char* prelocs
,
2120 Output_section
* output_section
,
2121 bool needs_special_offset_handling
,
2122 unsigned char* view
,
2123 elfcpp::Elf_types
<32>::Elf_Addr address
,
2124 section_size_type view_size
)
2126 gold_assert(sh_type
== elfcpp::SHT_REL
);
2128 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2129 Target_i386::Relocate
>(
2135 needs_special_offset_handling
,
2141 // Return the value to use for a dynamic which requires special
2142 // treatment. This is how we support equality comparisons of function
2143 // pointers across shared library boundaries, as described in the
2144 // processor specific ABI supplement.
2147 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2149 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2150 return this->plt_section()->address() + gsym
->plt_offset();
2153 // Return a string used to fill a code section with nops to take up
2154 // the specified length.
2157 Target_i386::do_code_fill(section_size_type length
)
2161 // Build a jmp instruction to skip over the bytes.
2162 unsigned char jmp
[5];
2164 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2165 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2166 + std::string(length
- 5, '\0'));
2169 // Nop sequences of various lengths.
2170 const char nop1
[1] = { 0x90 }; // nop
2171 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2172 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2173 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2174 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2175 0x00 }; // leal 0(%esi,1),%esi
2176 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2178 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2180 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2181 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2182 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2183 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2185 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2186 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2188 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2189 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2191 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2192 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2193 0x00, 0x00, 0x00, 0x00 };
2194 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2195 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2196 0x27, 0x00, 0x00, 0x00,
2198 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2199 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2200 0xbc, 0x27, 0x00, 0x00,
2202 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2203 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2204 0x90, 0x90, 0x90, 0x90,
2207 const char* nops
[16] = {
2209 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2210 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2213 return std::string(nops
[length
], length
);
2216 // The selector for i386 object files.
2218 class Target_selector_i386
: public Target_selector
2221 Target_selector_i386()
2222 : Target_selector(elfcpp::EM_386
, 32, false)
2226 recognize(int machine
, int osabi
, int abiversion
);
2229 Target_i386
* target_
;
2232 // Recognize an i386 object file when we already know that the machine
2233 // number is EM_386.
2236 Target_selector_i386::recognize(int, int, int)
2238 if (this->target_
== NULL
)
2239 this->target_
= new Target_i386();
2240 return this->target_
;
2243 Target_selector_i386 target_selector_i386
;
2245 } // End anonymous namespace.