1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008 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 // Scan the relocs during a relocatable link.
101 scan_relocatable_relocs(const General_options
& options
,
102 Symbol_table
* symtab
,
104 Sized_relobj
<32, false>* object
,
105 unsigned int data_shndx
,
106 unsigned int sh_type
,
107 const unsigned char* prelocs
,
109 Output_section
* output_section
,
110 bool needs_special_offset_handling
,
111 size_t local_symbol_count
,
112 const unsigned char* plocal_symbols
,
113 Relocatable_relocs
*);
115 // Relocate a section during a relocatable link.
117 relocate_for_relocatable(const Relocate_info
<32, false>*,
118 unsigned int sh_type
,
119 const unsigned char* prelocs
,
121 Output_section
* output_section
,
122 off_t offset_in_output_section
,
123 const Relocatable_relocs
*,
125 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
126 section_size_type view_size
,
127 unsigned char* reloc_view
,
128 section_size_type reloc_view_size
);
130 // Return a string used to fill a code section with nops.
132 do_code_fill(section_size_type length
) const;
134 // Return whether SYM is defined by the ABI.
136 do_is_defined_by_abi(Symbol
* sym
) const
137 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
139 // Return the size of the GOT section.
143 gold_assert(this->got_
!= NULL
);
144 return this->got_
->data_size();
148 // The class which scans relocations.
152 local(const General_options
& options
, Symbol_table
* symtab
,
153 Layout
* layout
, Target_i386
* target
,
154 Sized_relobj
<32, false>* object
,
155 unsigned int data_shndx
,
156 Output_section
* output_section
,
157 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
158 const elfcpp::Sym
<32, false>& lsym
);
161 global(const General_options
& options
, Symbol_table
* symtab
,
162 Layout
* layout
, Target_i386
* target
,
163 Sized_relobj
<32, false>* object
,
164 unsigned int data_shndx
,
165 Output_section
* output_section
,
166 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
170 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
173 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
177 // The class which implements relocation.
182 : skip_call_tls_get_addr_(false),
183 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
188 if (this->skip_call_tls_get_addr_
)
190 // FIXME: This needs to specify the location somehow.
191 gold_error(_("missing expected TLS relocation"));
195 // Return whether the static relocation needs to be applied.
197 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
201 // Do a relocation. Return false if the caller should not issue
202 // any warnings about this relocation.
204 relocate(const Relocate_info
<32, false>*, Target_i386
*, size_t relnum
,
205 const elfcpp::Rel
<32, false>&,
206 unsigned int r_type
, const Sized_symbol
<32>*,
207 const Symbol_value
<32>*,
208 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
212 // Do a TLS relocation.
214 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
215 size_t relnum
, const elfcpp::Rel
<32, false>&,
216 unsigned int r_type
, const Sized_symbol
<32>*,
217 const Symbol_value
<32>*,
218 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
221 // Do a TLS General-Dynamic to Initial-Exec transition.
223 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
224 Output_segment
* tls_segment
,
225 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
226 elfcpp::Elf_types
<32>::Elf_Addr value
,
228 section_size_type view_size
);
230 // Do a TLS General-Dynamic to Local-Exec transition.
232 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
233 Output_segment
* tls_segment
,
234 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
235 elfcpp::Elf_types
<32>::Elf_Addr value
,
237 section_size_type view_size
);
239 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
242 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
243 Output_segment
* tls_segment
,
244 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
245 elfcpp::Elf_types
<32>::Elf_Addr value
,
247 section_size_type view_size
);
249 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
252 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
253 Output_segment
* tls_segment
,
254 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
255 elfcpp::Elf_types
<32>::Elf_Addr value
,
257 section_size_type view_size
);
259 // Do a TLS Local-Dynamic to Local-Exec transition.
261 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
262 Output_segment
* tls_segment
,
263 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
264 elfcpp::Elf_types
<32>::Elf_Addr value
,
266 section_size_type view_size
);
268 // Do a TLS Initial-Exec to Local-Exec transition.
270 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
271 Output_segment
* tls_segment
,
272 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
273 elfcpp::Elf_types
<32>::Elf_Addr value
,
275 section_size_type view_size
);
277 // We need to keep track of which type of local dynamic relocation
278 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
279 enum Local_dynamic_type
286 // This is set if we should skip the next reloc, which should be a
287 // PLT32 reloc against ___tls_get_addr.
288 bool skip_call_tls_get_addr_
;
289 // The type of local dynamic relocation we have seen in the section
290 // being relocated, if any.
291 Local_dynamic_type local_dynamic_type_
;
294 // A class which returns the size required for a relocation type,
295 // used while scanning relocs during a relocatable link.
296 class Relocatable_size_for_reloc
300 get_size_for_reloc(unsigned int, Relobj
*);
303 // Adjust TLS relocation type based on the options and whether this
304 // is a local symbol.
305 static tls::Tls_optimization
306 optimize_tls_reloc(bool is_final
, int r_type
);
308 // Get the GOT section, creating it if necessary.
309 Output_data_got
<32, false>*
310 got_section(Symbol_table
*, Layout
*);
312 // Get the GOT PLT section.
314 got_plt_section() const
316 gold_assert(this->got_plt_
!= NULL
);
317 return this->got_plt_
;
320 // Create a PLT entry for a global symbol.
322 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
324 // Create a GOT entry for the TLS module index.
326 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
327 Sized_relobj
<32, false>* object
);
329 // Get the PLT section.
330 const Output_data_plt_i386
*
333 gold_assert(this->plt_
!= NULL
);
337 // Get the dynamic reloc section, creating it if necessary.
339 rel_dyn_section(Layout
*);
341 // Return true if the symbol may need a COPY relocation.
342 // References from an executable object to non-function symbols
343 // defined in a dynamic object may need a COPY relocation.
345 may_need_copy_reloc(Symbol
* gsym
)
347 return (!parameters
->options().shared()
348 && gsym
->is_from_dynobj()
349 && gsym
->type() != elfcpp::STT_FUNC
);
352 // Copy a relocation against a global symbol.
354 copy_reloc(const General_options
*, Symbol_table
*, Layout
*,
355 Sized_relobj
<32, false>*, unsigned int,
356 Output_section
*, Symbol
*, const elfcpp::Rel
<32, false>&);
358 // Information about this specific target which we pass to the
359 // general Target structure.
360 static const Target::Target_info i386_info
;
362 // The types of GOT entries needed for this platform.
365 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
366 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
367 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
368 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
369 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
373 Output_data_got
<32, false>* got_
;
375 Output_data_plt_i386
* plt_
;
376 // The GOT PLT section.
377 Output_data_space
* got_plt_
;
378 // The dynamic reloc section.
379 Reloc_section
* rel_dyn_
;
380 // Relocs saved to avoid a COPY reloc.
381 Copy_relocs
<32, false>* copy_relocs_
;
382 // Space for variables copied with a COPY reloc.
383 Output_data_space
* dynbss_
;
384 // Offset of the GOT entry for the TLS module index.
385 unsigned int got_mod_index_offset_
;
388 const Target::Target_info
Target_i386::i386_info
=
391 false, // is_big_endian
392 elfcpp::EM_386
, // machine_code
393 false, // has_make_symbol
394 false, // has_resolve
395 true, // has_code_fill
396 true, // is_default_stack_executable
398 "/usr/lib/libc.so.1", // dynamic_linker
399 0x08048000, // default_text_segment_address
400 0x1000, // abi_pagesize (overridable by -z max-page-size)
401 0x1000 // common_pagesize (overridable by -z common-page-size)
404 // Get the GOT section, creating it if necessary.
406 Output_data_got
<32, false>*
407 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
409 if (this->got_
== NULL
)
411 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
413 this->got_
= new Output_data_got
<32, false>();
415 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
416 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
419 // The old GNU linker creates a .got.plt section. We just
420 // create another set of data in the .got section. Note that we
421 // always create a PLT if we create a GOT, although the PLT
423 this->got_plt_
= new Output_data_space(4);
424 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
425 elfcpp::SHF_ALLOC
| elfcpp::SHF_WRITE
,
428 // The first three entries are reserved.
429 this->got_plt_
->set_current_data_size(3 * 4);
431 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
432 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
434 0, 0, elfcpp::STT_OBJECT
,
436 elfcpp::STV_HIDDEN
, 0,
443 // Get the dynamic reloc section, creating it if necessary.
445 Target_i386::Reloc_section
*
446 Target_i386::rel_dyn_section(Layout
* layout
)
448 if (this->rel_dyn_
== NULL
)
450 gold_assert(layout
!= NULL
);
451 this->rel_dyn_
= new Reloc_section();
452 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
453 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
455 return this->rel_dyn_
;
458 // A class to handle the PLT data.
460 class Output_data_plt_i386
: public Output_section_data
463 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
465 Output_data_plt_i386(Layout
*, Output_data_space
*);
467 // Add an entry to the PLT.
469 add_entry(Symbol
* gsym
);
471 // Return the .rel.plt section data.
474 { return this->rel_
; }
478 do_adjust_output_section(Output_section
* os
);
481 // The size of an entry in the PLT.
482 static const int plt_entry_size
= 16;
484 // The first entry in the PLT for an executable.
485 static unsigned char exec_first_plt_entry
[plt_entry_size
];
487 // The first entry in the PLT for a shared object.
488 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
490 // Other entries in the PLT for an executable.
491 static unsigned char exec_plt_entry
[plt_entry_size
];
493 // Other entries in the PLT for a shared object.
494 static unsigned char dyn_plt_entry
[plt_entry_size
];
496 // Set the final size.
498 set_final_data_size()
499 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
501 // Write out the PLT data.
503 do_write(Output_file
*);
505 // The reloc section.
507 // The .got.plt section.
508 Output_data_space
* got_plt_
;
509 // The number of PLT entries.
513 // Create the PLT section. The ordinary .got section is an argument,
514 // since we need to refer to the start. We also create our own .got
515 // section just for PLT entries.
517 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
518 Output_data_space
* got_plt
)
519 : Output_section_data(4), got_plt_(got_plt
), count_(0)
521 this->rel_
= new Reloc_section();
522 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
523 elfcpp::SHF_ALLOC
, this->rel_
);
527 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
529 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
530 // linker, and so do we.
534 // Add an entry to the PLT.
537 Output_data_plt_i386::add_entry(Symbol
* gsym
)
539 gold_assert(!gsym
->has_plt_offset());
541 // Note that when setting the PLT offset we skip the initial
542 // reserved PLT entry.
543 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
547 section_offset_type got_offset
= this->got_plt_
->current_data_size();
549 // Every PLT entry needs a GOT entry which points back to the PLT
550 // entry (this will be changed by the dynamic linker, normally
551 // lazily when the function is called).
552 this->got_plt_
->set_current_data_size(got_offset
+ 4);
554 // Every PLT entry needs a reloc.
555 gsym
->set_needs_dynsym_entry();
556 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
559 // Note that we don't need to save the symbol. The contents of the
560 // PLT are independent of which symbols are used. The symbols only
561 // appear in the relocations.
564 // The first entry in the PLT for an executable.
566 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
568 0xff, 0x35, // pushl contents of memory address
569 0, 0, 0, 0, // replaced with address of .got + 4
570 0xff, 0x25, // jmp indirect
571 0, 0, 0, 0, // replaced with address of .got + 8
575 // The first entry in the PLT for a shared object.
577 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
579 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
580 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
584 // Subsequent entries in the PLT for an executable.
586 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
588 0xff, 0x25, // jmp indirect
589 0, 0, 0, 0, // replaced with address of symbol in .got
590 0x68, // pushl immediate
591 0, 0, 0, 0, // replaced with offset into relocation table
592 0xe9, // jmp relative
593 0, 0, 0, 0 // replaced with offset to start of .plt
596 // Subsequent entries in the PLT for a shared object.
598 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
600 0xff, 0xa3, // jmp *offset(%ebx)
601 0, 0, 0, 0, // replaced with offset of symbol in .got
602 0x68, // pushl immediate
603 0, 0, 0, 0, // replaced with offset into relocation table
604 0xe9, // jmp relative
605 0, 0, 0, 0 // replaced with offset to start of .plt
608 // Write out the PLT. This uses the hand-coded instructions above,
609 // and adjusts them as needed. This is all specified by the i386 ELF
610 // Processor Supplement.
613 Output_data_plt_i386::do_write(Output_file
* of
)
615 const off_t offset
= this->offset();
616 const section_size_type oview_size
=
617 convert_to_section_size_type(this->data_size());
618 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
620 const off_t got_file_offset
= this->got_plt_
->offset();
621 const section_size_type got_size
=
622 convert_to_section_size_type(this->got_plt_
->data_size());
623 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
626 unsigned char* pov
= oview
;
628 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
629 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
631 if (parameters
->options().shared())
632 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
635 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
636 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
637 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
639 pov
+= plt_entry_size
;
641 unsigned char* got_pov
= got_view
;
643 memset(got_pov
, 0, 12);
646 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
648 unsigned int plt_offset
= plt_entry_size
;
649 unsigned int plt_rel_offset
= 0;
650 unsigned int got_offset
= 12;
651 const unsigned int count
= this->count_
;
652 for (unsigned int i
= 0;
655 pov
+= plt_entry_size
,
657 plt_offset
+= plt_entry_size
,
658 plt_rel_offset
+= rel_size
,
661 // Set and adjust the PLT entry itself.
663 if (parameters
->options().shared())
665 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
666 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
670 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
671 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
676 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
677 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
678 - (plt_offset
+ plt_entry_size
));
680 // Set the entry in the GOT.
681 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
684 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
685 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
687 of
->write_output_view(offset
, oview_size
, oview
);
688 of
->write_output_view(got_file_offset
, got_size
, got_view
);
691 // Create a PLT entry for a global symbol.
694 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
696 if (gsym
->has_plt_offset())
699 if (this->plt_
== NULL
)
701 // Create the GOT sections first.
702 this->got_section(symtab
, layout
);
704 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
705 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
707 | elfcpp::SHF_EXECINSTR
),
711 this->plt_
->add_entry(gsym
);
714 // Create a GOT entry for the TLS module index.
717 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
718 Sized_relobj
<32, false>* object
)
720 if (this->got_mod_index_offset_
== -1U)
722 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
723 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
724 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
725 unsigned int got_offset
= got
->add_constant(0);
726 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
728 got
->add_constant(0);
729 this->got_mod_index_offset_
= got_offset
;
731 return this->got_mod_index_offset_
;
734 // Handle a relocation against a non-function symbol defined in a
735 // dynamic object. The traditional way to handle this is to generate
736 // a COPY relocation to copy the variable at runtime from the shared
737 // object into the executable's data segment. However, this is
738 // undesirable in general, as if the size of the object changes in the
739 // dynamic object, the executable will no longer work correctly. If
740 // this relocation is in a writable section, then we can create a
741 // dynamic reloc and the dynamic linker will resolve it to the correct
742 // address at runtime. However, we do not want do that if the
743 // relocation is in a read-only section, as it would prevent the
744 // readonly segment from being shared. And if we have to eventually
745 // generate a COPY reloc, then any dynamic relocations will be
746 // useless. So this means that if this is a writable section, we need
747 // to save the relocation until we see whether we have to create a
748 // COPY relocation for this symbol for any other relocation.
751 Target_i386::copy_reloc(const General_options
* options
,
752 Symbol_table
* symtab
,
754 Sized_relobj
<32, false>* object
,
755 unsigned int data_shndx
,
756 Output_section
* output_section
,
758 const elfcpp::Rel
<32, false>& rel
)
760 Sized_symbol
<32>* ssym
= symtab
->get_sized_symbol
<32>(gsym
);
762 if (!Copy_relocs
<32, false>::need_copy_reloc(options
, object
,
765 // So far we do not need a COPY reloc. Save this relocation.
766 // If it turns out that we never need a COPY reloc for this
767 // symbol, then we will emit the relocation.
768 if (this->copy_relocs_
== NULL
)
769 this->copy_relocs_
= new Copy_relocs
<32, false>();
770 this->copy_relocs_
->save(ssym
, object
, data_shndx
, output_section
, rel
);
774 // Allocate space for this symbol in the .bss section.
776 elfcpp::Elf_types
<32>::Elf_WXword symsize
= ssym
->symsize();
778 // There is no defined way to determine the required alignment
779 // of the symbol. We pick the alignment based on the size. We
780 // set an arbitrary maximum of 256.
782 for (align
= 1; align
< 512; align
<<= 1)
783 if ((symsize
& align
) != 0)
786 if (this->dynbss_
== NULL
)
788 this->dynbss_
= new Output_data_space(align
);
789 layout
->add_output_section_data(".bss",
792 | elfcpp::SHF_WRITE
),
796 Output_data_space
* dynbss
= this->dynbss_
;
798 if (align
> dynbss
->addralign())
799 dynbss
->set_space_alignment(align
);
801 section_size_type dynbss_size
=
802 convert_to_section_size_type(dynbss
->current_data_size());
803 dynbss_size
= align_address(dynbss_size
, align
);
804 section_size_type offset
= dynbss_size
;
805 dynbss
->set_current_data_size(dynbss_size
+ symsize
);
807 symtab
->define_with_copy_reloc(ssym
, dynbss
, offset
);
809 // Add the COPY reloc.
810 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
811 rel_dyn
->add_global(ssym
, elfcpp::R_386_COPY
, dynbss
, offset
);
815 // Optimize the TLS relocation type based on what we know about the
816 // symbol. IS_FINAL is true if the final address of this symbol is
817 // known at link time.
819 tls::Tls_optimization
820 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
822 // If we are generating a shared library, then we can't do anything
824 if (parameters
->options().shared())
825 return tls::TLSOPT_NONE
;
829 case elfcpp::R_386_TLS_GD
:
830 case elfcpp::R_386_TLS_GOTDESC
:
831 case elfcpp::R_386_TLS_DESC_CALL
:
832 // These are General-Dynamic which permits fully general TLS
833 // access. Since we know that we are generating an executable,
834 // we can convert this to Initial-Exec. If we also know that
835 // this is a local symbol, we can further switch to Local-Exec.
837 return tls::TLSOPT_TO_LE
;
838 return tls::TLSOPT_TO_IE
;
840 case elfcpp::R_386_TLS_LDM
:
841 // This is Local-Dynamic, which refers to a local symbol in the
842 // dynamic TLS block. Since we know that we generating an
843 // executable, we can switch to Local-Exec.
844 return tls::TLSOPT_TO_LE
;
846 case elfcpp::R_386_TLS_LDO_32
:
847 // Another type of Local-Dynamic relocation.
848 return tls::TLSOPT_TO_LE
;
850 case elfcpp::R_386_TLS_IE
:
851 case elfcpp::R_386_TLS_GOTIE
:
852 case elfcpp::R_386_TLS_IE_32
:
853 // These are Initial-Exec relocs which get the thread offset
854 // from the GOT. If we know that we are linking against the
855 // local symbol, we can switch to Local-Exec, which links the
856 // thread offset into the instruction.
858 return tls::TLSOPT_TO_LE
;
859 return tls::TLSOPT_NONE
;
861 case elfcpp::R_386_TLS_LE
:
862 case elfcpp::R_386_TLS_LE_32
:
863 // When we already have Local-Exec, there is nothing further we
865 return tls::TLSOPT_NONE
;
872 // Report an unsupported relocation against a local symbol.
875 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
878 gold_error(_("%s: unsupported reloc %u against local symbol"),
879 object
->name().c_str(), r_type
);
882 // Scan a relocation for a local symbol.
885 Target_i386::Scan::local(const General_options
&,
886 Symbol_table
* symtab
,
889 Sized_relobj
<32, false>* object
,
890 unsigned int data_shndx
,
891 Output_section
* output_section
,
892 const elfcpp::Rel
<32, false>& reloc
,
894 const elfcpp::Sym
<32, false>& lsym
)
898 case elfcpp::R_386_NONE
:
899 case elfcpp::R_386_GNU_VTINHERIT
:
900 case elfcpp::R_386_GNU_VTENTRY
:
903 case elfcpp::R_386_32
:
904 // If building a shared library (or a position-independent
905 // executable), we need to create a dynamic relocation for
906 // this location. The relocation applied at link time will
907 // apply the link-time value, so we flag the location with
908 // an R_386_RELATIVE relocation so the dynamic loader can
909 // relocate it easily.
910 if (parameters
->options().output_is_position_independent())
912 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
913 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
914 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
915 output_section
, data_shndx
,
916 reloc
.get_r_offset());
920 case elfcpp::R_386_16
:
921 case elfcpp::R_386_8
:
922 // If building a shared library (or a position-independent
923 // executable), we need to create a dynamic relocation for
924 // this location. Because the addend needs to remain in the
925 // data section, we need to be careful not to apply this
926 // relocation statically.
927 if (parameters
->options().output_is_position_independent())
929 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
930 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
932 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
933 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
934 data_shndx
, reloc
.get_r_offset());
938 gold_assert(lsym
.get_st_value() == 0);
939 rel_dyn
->add_local_section(object
, lsym
.get_st_shndx(),
940 r_type
, output_section
,
941 data_shndx
, reloc
.get_r_offset());
946 case elfcpp::R_386_PC32
:
947 case elfcpp::R_386_PC16
:
948 case elfcpp::R_386_PC8
:
951 case elfcpp::R_386_PLT32
:
952 // Since we know this is a local symbol, we can handle this as a
956 case elfcpp::R_386_GOTOFF
:
957 case elfcpp::R_386_GOTPC
:
958 // We need a GOT section.
959 target
->got_section(symtab
, layout
);
962 case elfcpp::R_386_GOT32
:
964 // The symbol requires a GOT entry.
965 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
966 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
967 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
969 // If we are generating a shared object, we need to add a
970 // dynamic RELATIVE relocation for this symbol's GOT entry.
971 if (parameters
->options().output_is_position_independent())
973 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
974 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
975 rel_dyn
->add_local_relative(
976 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
977 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
983 // These are relocations which should only be seen by the
984 // dynamic linker, and should never be seen here.
985 case elfcpp::R_386_COPY
:
986 case elfcpp::R_386_GLOB_DAT
:
987 case elfcpp::R_386_JUMP_SLOT
:
988 case elfcpp::R_386_RELATIVE
:
989 case elfcpp::R_386_TLS_TPOFF
:
990 case elfcpp::R_386_TLS_DTPMOD32
:
991 case elfcpp::R_386_TLS_DTPOFF32
:
992 case elfcpp::R_386_TLS_TPOFF32
:
993 case elfcpp::R_386_TLS_DESC
:
994 gold_error(_("%s: unexpected reloc %u in object file"),
995 object
->name().c_str(), r_type
);
998 // These are initial TLS relocs, which are expected when
1000 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1001 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1002 case elfcpp::R_386_TLS_DESC_CALL
:
1003 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1004 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1005 case elfcpp::R_386_TLS_IE
: // Initial-exec
1006 case elfcpp::R_386_TLS_IE_32
:
1007 case elfcpp::R_386_TLS_GOTIE
:
1008 case elfcpp::R_386_TLS_LE
: // Local-exec
1009 case elfcpp::R_386_TLS_LE_32
:
1011 bool output_is_shared
= parameters
->options().shared();
1012 const tls::Tls_optimization optimized_type
1013 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1016 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1017 if (optimized_type
== tls::TLSOPT_NONE
)
1019 // Create a pair of GOT entries for the module index and
1020 // dtv-relative offset.
1021 Output_data_got
<32, false>* got
1022 = target
->got_section(symtab
, layout
);
1023 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1024 got
->add_local_pair_with_rel(object
, r_sym
,
1025 lsym
.get_st_shndx(),
1027 target
->rel_dyn_section(layout
),
1028 elfcpp::R_386_TLS_DTPMOD32
, 0);
1030 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1031 unsupported_reloc_local(object
, r_type
);
1034 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1035 if (optimized_type
== tls::TLSOPT_NONE
)
1037 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1038 Output_data_got
<32, false>* got
1039 = target
->got_section(symtab
, layout
);
1040 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1041 got
->add_local_pair_with_rel(object
, r_sym
,
1042 lsym
.get_st_shndx(),
1044 target
->rel_dyn_section(layout
),
1045 elfcpp::R_386_TLS_DESC
, 0);
1047 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1048 unsupported_reloc_local(object
, r_type
);
1051 case elfcpp::R_386_TLS_DESC_CALL
:
1054 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1055 if (optimized_type
== tls::TLSOPT_NONE
)
1057 // Create a GOT entry for the module index.
1058 target
->got_mod_index_entry(symtab
, layout
, object
);
1060 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1061 unsupported_reloc_local(object
, r_type
);
1064 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1067 case elfcpp::R_386_TLS_IE
: // Initial-exec
1068 case elfcpp::R_386_TLS_IE_32
:
1069 case elfcpp::R_386_TLS_GOTIE
:
1070 layout
->set_has_static_tls();
1071 if (optimized_type
== tls::TLSOPT_NONE
)
1073 // For the R_386_TLS_IE relocation, we need to create a
1074 // dynamic relocation when building a shared library.
1075 if (r_type
== elfcpp::R_386_TLS_IE
1076 && parameters
->options().shared())
1078 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1080 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1081 rel_dyn
->add_local_relative(object
, r_sym
,
1082 elfcpp::R_386_RELATIVE
,
1083 output_section
, data_shndx
,
1084 reloc
.get_r_offset());
1086 // Create a GOT entry for the tp-relative offset.
1087 Output_data_got
<32, false>* got
1088 = target
->got_section(symtab
, layout
);
1089 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1090 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1091 ? elfcpp::R_386_TLS_TPOFF32
1092 : elfcpp::R_386_TLS_TPOFF
);
1093 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1094 ? GOT_TYPE_TLS_OFFSET
1095 : GOT_TYPE_TLS_NOFFSET
);
1096 got
->add_local_with_rel(object
, r_sym
, got_type
,
1097 target
->rel_dyn_section(layout
),
1100 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1101 unsupported_reloc_local(object
, r_type
);
1104 case elfcpp::R_386_TLS_LE
: // Local-exec
1105 case elfcpp::R_386_TLS_LE_32
:
1106 layout
->set_has_static_tls();
1107 if (output_is_shared
)
1109 // We need to create a dynamic relocation.
1110 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1111 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1112 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1113 ? elfcpp::R_386_TLS_TPOFF32
1114 : elfcpp::R_386_TLS_TPOFF
);
1115 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1116 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1117 data_shndx
, reloc
.get_r_offset());
1127 case elfcpp::R_386_32PLT
:
1128 case elfcpp::R_386_TLS_GD_32
:
1129 case elfcpp::R_386_TLS_GD_PUSH
:
1130 case elfcpp::R_386_TLS_GD_CALL
:
1131 case elfcpp::R_386_TLS_GD_POP
:
1132 case elfcpp::R_386_TLS_LDM_32
:
1133 case elfcpp::R_386_TLS_LDM_PUSH
:
1134 case elfcpp::R_386_TLS_LDM_CALL
:
1135 case elfcpp::R_386_TLS_LDM_POP
:
1136 case elfcpp::R_386_USED_BY_INTEL_200
:
1138 unsupported_reloc_local(object
, r_type
);
1143 // Report an unsupported relocation against a global symbol.
1146 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1147 unsigned int r_type
,
1150 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1151 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1154 // Scan a relocation for a global symbol.
1157 Target_i386::Scan::global(const General_options
& options
,
1158 Symbol_table
* symtab
,
1160 Target_i386
* target
,
1161 Sized_relobj
<32, false>* object
,
1162 unsigned int data_shndx
,
1163 Output_section
* output_section
,
1164 const elfcpp::Rel
<32, false>& reloc
,
1165 unsigned int r_type
,
1170 case elfcpp::R_386_NONE
:
1171 case elfcpp::R_386_GNU_VTINHERIT
:
1172 case elfcpp::R_386_GNU_VTENTRY
:
1175 case elfcpp::R_386_32
:
1176 case elfcpp::R_386_16
:
1177 case elfcpp::R_386_8
:
1179 // Make a PLT entry if necessary.
1180 if (gsym
->needs_plt_entry())
1182 target
->make_plt_entry(symtab
, layout
, gsym
);
1183 // Since this is not a PC-relative relocation, we may be
1184 // taking the address of a function. In that case we need to
1185 // set the entry in the dynamic symbol table to the address of
1187 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1188 gsym
->set_needs_dynsym_value();
1190 // Make a dynamic relocation if necessary.
1191 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1193 if (target
->may_need_copy_reloc(gsym
))
1195 target
->copy_reloc(&options
, symtab
, layout
, object
,
1196 data_shndx
, output_section
, gsym
, reloc
);
1198 else if (r_type
== elfcpp::R_386_32
1199 && gsym
->can_use_relative_reloc(false))
1201 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1202 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1203 output_section
, object
,
1204 data_shndx
, reloc
.get_r_offset());
1208 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1209 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1210 data_shndx
, reloc
.get_r_offset());
1216 case elfcpp::R_386_PC32
:
1217 case elfcpp::R_386_PC16
:
1218 case elfcpp::R_386_PC8
:
1220 // Make a PLT entry if necessary.
1221 if (gsym
->needs_plt_entry())
1223 // These relocations are used for function calls only in
1224 // non-PIC code. For a 32-bit relocation in a shared library,
1225 // we'll need a text relocation anyway, so we can skip the
1226 // PLT entry and let the dynamic linker bind the call directly
1227 // to the target. For smaller relocations, we should use a
1228 // PLT entry to ensure that the call can reach.
1229 if (!parameters
->options().shared()
1230 || r_type
!= elfcpp::R_386_PC32
)
1231 target
->make_plt_entry(symtab
, layout
, gsym
);
1233 // Make a dynamic relocation if necessary.
1234 int flags
= Symbol::NON_PIC_REF
;
1235 if (gsym
->type() == elfcpp::STT_FUNC
)
1236 flags
|= Symbol::FUNCTION_CALL
;
1237 if (gsym
->needs_dynamic_reloc(flags
))
1239 if (target
->may_need_copy_reloc(gsym
))
1241 target
->copy_reloc(&options
, symtab
, layout
, object
,
1242 data_shndx
, output_section
, gsym
, reloc
);
1246 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1247 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1248 data_shndx
, reloc
.get_r_offset());
1254 case elfcpp::R_386_GOT32
:
1256 // The symbol requires a GOT entry.
1257 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1258 if (gsym
->final_value_is_known())
1259 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1262 // If this symbol is not fully resolved, we need to add a
1263 // GOT entry with a dynamic relocation.
1264 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1265 if (gsym
->is_from_dynobj()
1266 || gsym
->is_undefined()
1267 || gsym
->is_preemptible())
1268 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1269 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1272 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1273 rel_dyn
->add_global_relative(
1274 gsym
, elfcpp::R_386_RELATIVE
, got
,
1275 gsym
->got_offset(GOT_TYPE_STANDARD
));
1281 case elfcpp::R_386_PLT32
:
1282 // If the symbol is fully resolved, this is just a PC32 reloc.
1283 // Otherwise we need a PLT entry.
1284 if (gsym
->final_value_is_known())
1286 // If building a shared library, we can also skip the PLT entry
1287 // if the symbol is defined in the output file and is protected
1289 if (gsym
->is_defined()
1290 && !gsym
->is_from_dynobj()
1291 && !gsym
->is_preemptible())
1293 target
->make_plt_entry(symtab
, layout
, gsym
);
1296 case elfcpp::R_386_GOTOFF
:
1297 case elfcpp::R_386_GOTPC
:
1298 // We need a GOT section.
1299 target
->got_section(symtab
, layout
);
1302 // These are relocations which should only be seen by the
1303 // dynamic linker, and should never be seen here.
1304 case elfcpp::R_386_COPY
:
1305 case elfcpp::R_386_GLOB_DAT
:
1306 case elfcpp::R_386_JUMP_SLOT
:
1307 case elfcpp::R_386_RELATIVE
:
1308 case elfcpp::R_386_TLS_TPOFF
:
1309 case elfcpp::R_386_TLS_DTPMOD32
:
1310 case elfcpp::R_386_TLS_DTPOFF32
:
1311 case elfcpp::R_386_TLS_TPOFF32
:
1312 case elfcpp::R_386_TLS_DESC
:
1313 gold_error(_("%s: unexpected reloc %u in object file"),
1314 object
->name().c_str(), r_type
);
1317 // These are initial tls relocs, which are expected when
1319 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1320 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1321 case elfcpp::R_386_TLS_DESC_CALL
:
1322 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1323 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1324 case elfcpp::R_386_TLS_IE
: // Initial-exec
1325 case elfcpp::R_386_TLS_IE_32
:
1326 case elfcpp::R_386_TLS_GOTIE
:
1327 case elfcpp::R_386_TLS_LE
: // Local-exec
1328 case elfcpp::R_386_TLS_LE_32
:
1330 const bool is_final
= gsym
->final_value_is_known();
1331 const tls::Tls_optimization optimized_type
1332 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1335 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1336 if (optimized_type
== tls::TLSOPT_NONE
)
1338 // Create a pair of GOT entries for the module index and
1339 // dtv-relative offset.
1340 Output_data_got
<32, false>* got
1341 = target
->got_section(symtab
, layout
);
1342 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1343 target
->rel_dyn_section(layout
),
1344 elfcpp::R_386_TLS_DTPMOD32
,
1345 elfcpp::R_386_TLS_DTPOFF32
);
1347 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1349 // Create a GOT entry for the tp-relative offset.
1350 Output_data_got
<32, false>* got
1351 = target
->got_section(symtab
, layout
);
1352 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1353 target
->rel_dyn_section(layout
),
1354 elfcpp::R_386_TLS_TPOFF
);
1356 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1357 unsupported_reloc_global(object
, r_type
, gsym
);
1360 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1361 if (optimized_type
== tls::TLSOPT_NONE
)
1363 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1364 Output_data_got
<32, false>* got
1365 = target
->got_section(symtab
, layout
);
1366 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1367 target
->rel_dyn_section(layout
),
1368 elfcpp::R_386_TLS_DESC
, 0);
1370 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1372 // Create a GOT entry for the tp-relative offset.
1373 Output_data_got
<32, false>* got
1374 = target
->got_section(symtab
, layout
);
1375 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1376 target
->rel_dyn_section(layout
),
1377 elfcpp::R_386_TLS_TPOFF
);
1379 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1380 unsupported_reloc_global(object
, r_type
, gsym
);
1383 case elfcpp::R_386_TLS_DESC_CALL
:
1386 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1387 if (optimized_type
== tls::TLSOPT_NONE
)
1389 // Create a GOT entry for the module index.
1390 target
->got_mod_index_entry(symtab
, layout
, object
);
1392 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1393 unsupported_reloc_global(object
, r_type
, gsym
);
1396 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1399 case elfcpp::R_386_TLS_IE
: // Initial-exec
1400 case elfcpp::R_386_TLS_IE_32
:
1401 case elfcpp::R_386_TLS_GOTIE
:
1402 layout
->set_has_static_tls();
1403 if (optimized_type
== tls::TLSOPT_NONE
)
1405 // For the R_386_TLS_IE relocation, we need to create a
1406 // dynamic relocation when building a shared library.
1407 if (r_type
== elfcpp::R_386_TLS_IE
1408 && parameters
->options().shared())
1410 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1411 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1412 output_section
, object
,
1414 reloc
.get_r_offset());
1416 // Create a GOT entry for the tp-relative offset.
1417 Output_data_got
<32, false>* got
1418 = target
->got_section(symtab
, layout
);
1419 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1420 ? elfcpp::R_386_TLS_TPOFF32
1421 : elfcpp::R_386_TLS_TPOFF
);
1422 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1423 ? GOT_TYPE_TLS_OFFSET
1424 : GOT_TYPE_TLS_NOFFSET
);
1425 got
->add_global_with_rel(gsym
, got_type
,
1426 target
->rel_dyn_section(layout
),
1429 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1430 unsupported_reloc_global(object
, r_type
, gsym
);
1433 case elfcpp::R_386_TLS_LE
: // Local-exec
1434 case elfcpp::R_386_TLS_LE_32
:
1435 layout
->set_has_static_tls();
1436 if (parameters
->options().shared())
1438 // We need to create a dynamic relocation.
1439 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1440 ? elfcpp::R_386_TLS_TPOFF32
1441 : elfcpp::R_386_TLS_TPOFF
);
1442 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1443 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1444 data_shndx
, reloc
.get_r_offset());
1454 case elfcpp::R_386_32PLT
:
1455 case elfcpp::R_386_TLS_GD_32
:
1456 case elfcpp::R_386_TLS_GD_PUSH
:
1457 case elfcpp::R_386_TLS_GD_CALL
:
1458 case elfcpp::R_386_TLS_GD_POP
:
1459 case elfcpp::R_386_TLS_LDM_32
:
1460 case elfcpp::R_386_TLS_LDM_PUSH
:
1461 case elfcpp::R_386_TLS_LDM_CALL
:
1462 case elfcpp::R_386_TLS_LDM_POP
:
1463 case elfcpp::R_386_USED_BY_INTEL_200
:
1465 unsupported_reloc_global(object
, r_type
, gsym
);
1470 // Scan relocations for a section.
1473 Target_i386::scan_relocs(const General_options
& options
,
1474 Symbol_table
* symtab
,
1476 Sized_relobj
<32, false>* object
,
1477 unsigned int data_shndx
,
1478 unsigned int sh_type
,
1479 const unsigned char* prelocs
,
1481 Output_section
* output_section
,
1482 bool needs_special_offset_handling
,
1483 size_t local_symbol_count
,
1484 const unsigned char* plocal_symbols
)
1486 if (sh_type
== elfcpp::SHT_RELA
)
1488 gold_error(_("%s: unsupported RELA reloc section"),
1489 object
->name().c_str());
1493 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1504 needs_special_offset_handling
,
1509 // Finalize the sections.
1512 Target_i386::do_finalize_sections(Layout
* layout
)
1514 // Fill in some more dynamic tags.
1515 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1518 if (this->got_plt_
!= NULL
)
1519 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1521 if (this->plt_
!= NULL
)
1523 const Output_data
* od
= this->plt_
->rel_plt();
1524 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1525 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1526 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1529 if (this->rel_dyn_
!= NULL
)
1531 const Output_data
* od
= this->rel_dyn_
;
1532 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1533 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1534 odyn
->add_constant(elfcpp::DT_RELENT
,
1535 elfcpp::Elf_sizes
<32>::rel_size
);
1538 if (!parameters
->options().shared())
1540 // The value of the DT_DEBUG tag is filled in by the dynamic
1541 // linker at run time, and used by the debugger.
1542 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1546 // Emit any relocs we saved in an attempt to avoid generating COPY
1548 if (this->copy_relocs_
== NULL
)
1550 if (this->copy_relocs_
->any_to_emit())
1552 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
1553 this->copy_relocs_
->emit(rel_dyn
);
1555 delete this->copy_relocs_
;
1556 this->copy_relocs_
= NULL
;
1559 // Return whether a direct absolute static relocation needs to be applied.
1560 // In cases where Scan::local() or Scan::global() has created
1561 // a dynamic relocation other than R_386_RELATIVE, the addend
1562 // of the relocation is carried in the data, and we must not
1563 // apply the static relocation.
1566 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1570 // For local symbols, we will have created a non-RELATIVE dynamic
1571 // relocation only if (a) the output is position independent,
1572 // (b) the relocation is absolute (not pc- or segment-relative), and
1573 // (c) the relocation is not 32 bits wide.
1575 return !(parameters
->options().output_is_position_independent()
1576 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1579 // For global symbols, we use the same helper routines used in the
1580 // scan pass. If we did not create a dynamic relocation, or if we
1581 // created a RELATIVE dynamic relocation, we should apply the static
1583 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1584 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1585 && gsym
->can_use_relative_reloc(ref_flags
1586 & Symbol::FUNCTION_CALL
);
1587 return !has_dyn
|| is_rel
;
1590 // Perform a relocation.
1593 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1594 Target_i386
* target
,
1596 const elfcpp::Rel
<32, false>& rel
,
1597 unsigned int r_type
,
1598 const Sized_symbol
<32>* gsym
,
1599 const Symbol_value
<32>* psymval
,
1600 unsigned char* view
,
1601 elfcpp::Elf_types
<32>::Elf_Addr address
,
1602 section_size_type view_size
)
1604 if (this->skip_call_tls_get_addr_
)
1606 if (r_type
!= elfcpp::R_386_PLT32
1608 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1609 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1610 _("missing expected TLS relocation"));
1613 this->skip_call_tls_get_addr_
= false;
1618 // Pick the value to use for symbols defined in shared objects.
1619 Symbol_value
<32> symval
;
1620 bool is_nonpic
= (r_type
== elfcpp::R_386_PC8
1621 || r_type
== elfcpp::R_386_PC16
1622 || r_type
== elfcpp::R_386_PC32
);
1624 && (gsym
->is_from_dynobj()
1625 || (parameters
->options().shared()
1626 && (gsym
->is_undefined() || gsym
->is_preemptible())))
1627 && gsym
->has_plt_offset()
1628 && (!is_nonpic
|| !parameters
->options().shared()))
1630 symval
.set_output_value(target
->plt_section()->address()
1631 + gsym
->plt_offset());
1635 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1637 // Get the GOT offset if needed.
1638 // The GOT pointer points to the end of the GOT section.
1639 // We need to subtract the size of the GOT section to get
1640 // the actual offset to use in the relocation.
1641 bool have_got_offset
= false;
1642 unsigned int got_offset
= 0;
1645 case elfcpp::R_386_GOT32
:
1648 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1649 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1650 - target
->got_size());
1654 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1655 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1656 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1657 - target
->got_size());
1659 have_got_offset
= true;
1668 case elfcpp::R_386_NONE
:
1669 case elfcpp::R_386_GNU_VTINHERIT
:
1670 case elfcpp::R_386_GNU_VTENTRY
:
1673 case elfcpp::R_386_32
:
1674 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true))
1675 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1678 case elfcpp::R_386_PC32
:
1680 int ref_flags
= Symbol::NON_PIC_REF
;
1681 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1682 ref_flags
|= Symbol::FUNCTION_CALL
;
1683 if (should_apply_static_reloc(gsym
, ref_flags
, true))
1684 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1688 case elfcpp::R_386_16
:
1689 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1690 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1693 case elfcpp::R_386_PC16
:
1695 int ref_flags
= Symbol::NON_PIC_REF
;
1696 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1697 ref_flags
|= Symbol::FUNCTION_CALL
;
1698 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1699 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1703 case elfcpp::R_386_8
:
1704 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false))
1705 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1708 case elfcpp::R_386_PC8
:
1710 int ref_flags
= Symbol::NON_PIC_REF
;
1711 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1712 ref_flags
|= Symbol::FUNCTION_CALL
;
1713 if (should_apply_static_reloc(gsym
, ref_flags
, false))
1714 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1718 case elfcpp::R_386_PLT32
:
1719 gold_assert(gsym
== NULL
1720 || gsym
->has_plt_offset()
1721 || gsym
->final_value_is_known()
1722 || (gsym
->is_defined()
1723 && !gsym
->is_from_dynobj()
1724 && !gsym
->is_preemptible()));
1725 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1728 case elfcpp::R_386_GOT32
:
1729 gold_assert(have_got_offset
);
1730 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1733 case elfcpp::R_386_GOTOFF
:
1735 elfcpp::Elf_types
<32>::Elf_Addr value
;
1736 value
= (psymval
->value(object
, 0)
1737 - target
->got_plt_section()->address());
1738 Relocate_functions
<32, false>::rel32(view
, value
);
1742 case elfcpp::R_386_GOTPC
:
1744 elfcpp::Elf_types
<32>::Elf_Addr value
;
1745 value
= target
->got_plt_section()->address();
1746 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1750 case elfcpp::R_386_COPY
:
1751 case elfcpp::R_386_GLOB_DAT
:
1752 case elfcpp::R_386_JUMP_SLOT
:
1753 case elfcpp::R_386_RELATIVE
:
1754 // These are outstanding tls relocs, which are unexpected when
1756 case elfcpp::R_386_TLS_TPOFF
:
1757 case elfcpp::R_386_TLS_DTPMOD32
:
1758 case elfcpp::R_386_TLS_DTPOFF32
:
1759 case elfcpp::R_386_TLS_TPOFF32
:
1760 case elfcpp::R_386_TLS_DESC
:
1761 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1762 _("unexpected reloc %u in object file"),
1766 // These are initial tls relocs, which are expected when
1768 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1769 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1770 case elfcpp::R_386_TLS_DESC_CALL
:
1771 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1772 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1773 case elfcpp::R_386_TLS_IE
: // Initial-exec
1774 case elfcpp::R_386_TLS_IE_32
:
1775 case elfcpp::R_386_TLS_GOTIE
:
1776 case elfcpp::R_386_TLS_LE
: // Local-exec
1777 case elfcpp::R_386_TLS_LE_32
:
1778 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1779 view
, address
, view_size
);
1782 case elfcpp::R_386_32PLT
:
1783 case elfcpp::R_386_TLS_GD_32
:
1784 case elfcpp::R_386_TLS_GD_PUSH
:
1785 case elfcpp::R_386_TLS_GD_CALL
:
1786 case elfcpp::R_386_TLS_GD_POP
:
1787 case elfcpp::R_386_TLS_LDM_32
:
1788 case elfcpp::R_386_TLS_LDM_PUSH
:
1789 case elfcpp::R_386_TLS_LDM_CALL
:
1790 case elfcpp::R_386_TLS_LDM_POP
:
1791 case elfcpp::R_386_USED_BY_INTEL_200
:
1793 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1794 _("unsupported reloc %u"),
1802 // Perform a TLS relocation.
1805 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1806 Target_i386
* target
,
1808 const elfcpp::Rel
<32, false>& rel
,
1809 unsigned int r_type
,
1810 const Sized_symbol
<32>* gsym
,
1811 const Symbol_value
<32>* psymval
,
1812 unsigned char* view
,
1813 elfcpp::Elf_types
<32>::Elf_Addr
,
1814 section_size_type view_size
)
1816 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1818 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1820 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1822 const bool is_final
=
1824 ? !parameters
->options().output_is_position_independent()
1825 : gsym
->final_value_is_known());
1826 const tls::Tls_optimization optimized_type
1827 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1830 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1831 if (optimized_type
== tls::TLSOPT_TO_LE
)
1833 gold_assert(tls_segment
!= NULL
);
1834 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1835 rel
, r_type
, value
, view
,
1841 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1842 ? GOT_TYPE_TLS_NOFFSET
1843 : GOT_TYPE_TLS_PAIR
);
1844 unsigned int got_offset
;
1847 gold_assert(gsym
->has_got_offset(got_type
));
1848 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1852 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1853 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1854 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1855 - target
->got_size());
1857 if (optimized_type
== tls::TLSOPT_TO_IE
)
1859 gold_assert(tls_segment
!= NULL
);
1860 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1861 got_offset
, view
, view_size
);
1864 else if (optimized_type
== tls::TLSOPT_NONE
)
1866 // Relocate the field with the offset of the pair of GOT
1868 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1872 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1873 _("unsupported reloc %u"),
1877 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1878 case elfcpp::R_386_TLS_DESC_CALL
:
1879 if (optimized_type
== tls::TLSOPT_TO_LE
)
1881 gold_assert(tls_segment
!= NULL
);
1882 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1883 rel
, r_type
, value
, view
,
1889 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1890 ? GOT_TYPE_TLS_NOFFSET
1891 : GOT_TYPE_TLS_DESC
);
1892 unsigned int got_offset
;
1895 gold_assert(gsym
->has_got_offset(got_type
));
1896 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1900 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1901 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1902 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1903 - target
->got_size());
1905 if (optimized_type
== tls::TLSOPT_TO_IE
)
1907 gold_assert(tls_segment
!= NULL
);
1908 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1909 got_offset
, view
, view_size
);
1912 else if (optimized_type
== tls::TLSOPT_NONE
)
1914 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1916 // Relocate the field with the offset of the pair of GOT
1918 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1923 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1924 _("unsupported reloc %u"),
1928 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1929 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1931 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1932 _("both SUN and GNU model "
1933 "TLS relocations"));
1936 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1937 if (optimized_type
== tls::TLSOPT_TO_LE
)
1939 gold_assert(tls_segment
!= NULL
);
1940 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1941 value
, view
, view_size
);
1944 else if (optimized_type
== tls::TLSOPT_NONE
)
1946 // Relocate the field with the offset of the GOT entry for
1947 // the module index.
1948 unsigned int got_offset
;
1949 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
1950 - target
->got_size());
1951 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1954 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1955 _("unsupported reloc %u"),
1959 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1960 // This reloc can appear in debugging sections, in which case we
1961 // won't see the TLS_LDM reloc. The local_dynamic_type field
1963 if (optimized_type
== tls::TLSOPT_TO_LE
)
1965 gold_assert(tls_segment
!= NULL
);
1966 value
-= tls_segment
->memsz();
1968 Relocate_functions
<32, false>::rel32(view
, value
);
1971 case elfcpp::R_386_TLS_IE
: // Initial-exec
1972 case elfcpp::R_386_TLS_GOTIE
:
1973 case elfcpp::R_386_TLS_IE_32
:
1974 if (optimized_type
== tls::TLSOPT_TO_LE
)
1976 gold_assert(tls_segment
!= NULL
);
1977 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
1978 rel
, r_type
, value
, view
,
1982 else if (optimized_type
== tls::TLSOPT_NONE
)
1984 // Relocate the field with the offset of the GOT entry for
1985 // the tp-relative offset of the symbol.
1986 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1987 ? GOT_TYPE_TLS_OFFSET
1988 : GOT_TYPE_TLS_NOFFSET
);
1989 unsigned int got_offset
;
1992 gold_assert(gsym
->has_got_offset(got_type
));
1993 got_offset
= gsym
->got_offset(got_type
);
1997 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1998 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1999 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2001 // For the R_386_TLS_IE relocation, we need to apply the
2002 // absolute address of the GOT entry.
2003 if (r_type
== elfcpp::R_386_TLS_IE
)
2004 got_offset
+= target
->got_plt_section()->address();
2005 // All GOT offsets are relative to the end of the GOT.
2006 got_offset
-= target
->got_size();
2007 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2010 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2011 _("unsupported reloc %u"),
2015 case elfcpp::R_386_TLS_LE
: // Local-exec
2016 // If we're creating a shared library, a dynamic relocation will
2017 // have been created for this location, so do not apply it now.
2018 if (!parameters
->options().shared())
2020 gold_assert(tls_segment
!= NULL
);
2021 value
-= tls_segment
->memsz();
2022 Relocate_functions
<32, false>::rel32(view
, value
);
2026 case elfcpp::R_386_TLS_LE_32
:
2027 // If we're creating a shared library, a dynamic relocation will
2028 // have been created for this location, so do not apply it now.
2029 if (!parameters
->options().shared())
2031 gold_assert(tls_segment
!= NULL
);
2032 value
= tls_segment
->memsz() - value
;
2033 Relocate_functions
<32, false>::rel32(view
, value
);
2039 // Do a relocation in which we convert a TLS General-Dynamic to a
2043 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2045 Output_segment
* tls_segment
,
2046 const elfcpp::Rel
<32, false>& rel
,
2048 elfcpp::Elf_types
<32>::Elf_Addr value
,
2049 unsigned char* view
,
2050 section_size_type view_size
)
2052 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2053 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2054 // leal foo(%reg),%eax; call ___tls_get_addr
2055 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2057 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2058 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2060 unsigned char op1
= view
[-1];
2061 unsigned char op2
= view
[-2];
2063 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2064 op2
== 0x8d || op2
== 0x04);
2065 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2071 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2072 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2073 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2074 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2075 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2079 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2080 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2081 if (rel
.get_r_offset() + 9 < view_size
2084 // There is a trailing nop. Use the size byte subl.
2085 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2090 // Use the five byte subl.
2091 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2095 value
= tls_segment
->memsz() - value
;
2096 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2098 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2100 this->skip_call_tls_get_addr_
= true;
2103 // Do a relocation in which we convert a TLS General-Dynamic to an
2107 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2110 const elfcpp::Rel
<32, false>& rel
,
2112 elfcpp::Elf_types
<32>::Elf_Addr value
,
2113 unsigned char* view
,
2114 section_size_type view_size
)
2116 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2117 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2119 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2120 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2122 unsigned char op1
= view
[-1];
2123 unsigned char op2
= view
[-2];
2125 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2126 op2
== 0x8d || op2
== 0x04);
2127 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2131 // FIXME: For now, support only the first (SIB) form.
2132 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2136 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2137 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2138 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2139 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2140 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2144 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2145 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2146 if (rel
.get_r_offset() + 9 < view_size
2149 // FIXME: This is not the right instruction sequence.
2150 // There is a trailing nop. Use the size byte subl.
2151 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2156 // FIXME: This is not the right instruction sequence.
2157 // Use the five byte subl.
2158 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2162 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2164 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2166 this->skip_call_tls_get_addr_
= true;
2169 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2170 // General-Dynamic to a Local-Exec.
2173 Target_i386::Relocate::tls_desc_gd_to_le(
2174 const Relocate_info
<32, false>* relinfo
,
2176 Output_segment
* tls_segment
,
2177 const elfcpp::Rel
<32, false>& rel
,
2178 unsigned int r_type
,
2179 elfcpp::Elf_types
<32>::Elf_Addr value
,
2180 unsigned char* view
,
2181 section_size_type view_size
)
2183 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2185 // leal foo@TLSDESC(%ebx), %eax
2186 // ==> leal foo@NTPOFF, %eax
2187 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2188 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2189 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2190 view
[-2] == 0x8d && view
[-1] == 0x83);
2192 value
-= tls_segment
->memsz();
2193 Relocate_functions
<32, false>::rel32(view
, value
);
2197 // call *foo@TLSCALL(%eax)
2199 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2200 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2201 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2202 view
[0] == 0xff && view
[1] == 0x10);
2208 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2209 // General-Dynamic to an Initial-Exec.
2212 Target_i386::Relocate::tls_desc_gd_to_ie(
2213 const Relocate_info
<32, false>* relinfo
,
2216 const elfcpp::Rel
<32, false>& rel
,
2217 unsigned int r_type
,
2218 elfcpp::Elf_types
<32>::Elf_Addr value
,
2219 unsigned char* view
,
2220 section_size_type view_size
)
2222 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2224 // leal foo@TLSDESC(%ebx), %eax
2225 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2226 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2227 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2228 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2229 view
[-2] == 0x8d && view
[-1] == 0x83);
2231 Relocate_functions
<32, false>::rel32(view
, value
);
2235 // call *foo@TLSCALL(%eax)
2237 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2238 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2239 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2240 view
[0] == 0xff && view
[1] == 0x10);
2246 // Do a relocation in which we convert a TLS Local-Dynamic to a
2250 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2253 const elfcpp::Rel
<32, false>& rel
,
2255 elfcpp::Elf_types
<32>::Elf_Addr
,
2256 unsigned char* view
,
2257 section_size_type view_size
)
2259 // leal foo(%reg), %eax; call ___tls_get_addr
2260 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2262 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2263 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2265 // FIXME: Does this test really always pass?
2266 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2267 view
[-2] == 0x8d && view
[-1] == 0x83);
2269 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2271 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2273 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2275 this->skip_call_tls_get_addr_
= true;
2278 // Do a relocation in which we convert a TLS Initial-Exec to a
2282 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2284 Output_segment
* tls_segment
,
2285 const elfcpp::Rel
<32, false>& rel
,
2286 unsigned int r_type
,
2287 elfcpp::Elf_types
<32>::Elf_Addr value
,
2288 unsigned char* view
,
2289 section_size_type view_size
)
2291 // We have to actually change the instructions, which means that we
2292 // need to examine the opcodes to figure out which instruction we
2294 if (r_type
== elfcpp::R_386_TLS_IE
)
2296 // movl %gs:XX,%eax ==> movl $YY,%eax
2297 // movl %gs:XX,%reg ==> movl $YY,%reg
2298 // addl %gs:XX,%reg ==> addl $YY,%reg
2299 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2300 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2302 unsigned char op1
= view
[-1];
2305 // movl XX,%eax ==> movl $YY,%eax
2310 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2312 unsigned char op2
= view
[-2];
2315 // movl XX,%reg ==> movl $YY,%reg
2316 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2317 (op1
& 0xc7) == 0x05);
2319 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2321 else if (op2
== 0x03)
2323 // addl XX,%reg ==> addl $YY,%reg
2324 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2325 (op1
& 0xc7) == 0x05);
2327 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2330 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2335 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2336 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2337 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2338 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2339 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2341 unsigned char op1
= view
[-1];
2342 unsigned char op2
= view
[-2];
2343 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2344 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2347 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2349 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2351 else if (op2
== 0x2b)
2353 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2355 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2357 else if (op2
== 0x03)
2359 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2361 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2364 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2367 value
= tls_segment
->memsz() - value
;
2368 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2371 Relocate_functions
<32, false>::rel32(view
, value
);
2374 // Relocate section data.
2377 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2378 unsigned int sh_type
,
2379 const unsigned char* prelocs
,
2381 Output_section
* output_section
,
2382 bool needs_special_offset_handling
,
2383 unsigned char* view
,
2384 elfcpp::Elf_types
<32>::Elf_Addr address
,
2385 section_size_type view_size
)
2387 gold_assert(sh_type
== elfcpp::SHT_REL
);
2389 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2390 Target_i386::Relocate
>(
2396 needs_special_offset_handling
,
2402 // Return the size of a relocation while scanning during a relocatable
2406 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2407 unsigned int r_type
,
2412 case elfcpp::R_386_NONE
:
2413 case elfcpp::R_386_GNU_VTINHERIT
:
2414 case elfcpp::R_386_GNU_VTENTRY
:
2415 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2416 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2417 case elfcpp::R_386_TLS_DESC_CALL
:
2418 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2419 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2420 case elfcpp::R_386_TLS_IE
: // Initial-exec
2421 case elfcpp::R_386_TLS_IE_32
:
2422 case elfcpp::R_386_TLS_GOTIE
:
2423 case elfcpp::R_386_TLS_LE
: // Local-exec
2424 case elfcpp::R_386_TLS_LE_32
:
2427 case elfcpp::R_386_32
:
2428 case elfcpp::R_386_PC32
:
2429 case elfcpp::R_386_GOT32
:
2430 case elfcpp::R_386_PLT32
:
2431 case elfcpp::R_386_GOTOFF
:
2432 case elfcpp::R_386_GOTPC
:
2435 case elfcpp::R_386_16
:
2436 case elfcpp::R_386_PC16
:
2439 case elfcpp::R_386_8
:
2440 case elfcpp::R_386_PC8
:
2443 // These are relocations which should only be seen by the
2444 // dynamic linker, and should never be seen here.
2445 case elfcpp::R_386_COPY
:
2446 case elfcpp::R_386_GLOB_DAT
:
2447 case elfcpp::R_386_JUMP_SLOT
:
2448 case elfcpp::R_386_RELATIVE
:
2449 case elfcpp::R_386_TLS_TPOFF
:
2450 case elfcpp::R_386_TLS_DTPMOD32
:
2451 case elfcpp::R_386_TLS_DTPOFF32
:
2452 case elfcpp::R_386_TLS_TPOFF32
:
2453 case elfcpp::R_386_TLS_DESC
:
2454 object
->error(_("unexpected reloc %u in object file"), r_type
);
2457 case elfcpp::R_386_32PLT
:
2458 case elfcpp::R_386_TLS_GD_32
:
2459 case elfcpp::R_386_TLS_GD_PUSH
:
2460 case elfcpp::R_386_TLS_GD_CALL
:
2461 case elfcpp::R_386_TLS_GD_POP
:
2462 case elfcpp::R_386_TLS_LDM_32
:
2463 case elfcpp::R_386_TLS_LDM_PUSH
:
2464 case elfcpp::R_386_TLS_LDM_CALL
:
2465 case elfcpp::R_386_TLS_LDM_POP
:
2466 case elfcpp::R_386_USED_BY_INTEL_200
:
2468 object
->error(_("unsupported reloc %u in object file"), r_type
);
2473 // Scan the relocs during a relocatable link.
2476 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2477 Symbol_table
* symtab
,
2479 Sized_relobj
<32, false>* object
,
2480 unsigned int data_shndx
,
2481 unsigned int sh_type
,
2482 const unsigned char* prelocs
,
2484 Output_section
* output_section
,
2485 bool needs_special_offset_handling
,
2486 size_t local_symbol_count
,
2487 const unsigned char* plocal_symbols
,
2488 Relocatable_relocs
* rr
)
2490 gold_assert(sh_type
== elfcpp::SHT_REL
);
2492 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2493 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2495 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2496 Scan_relocatable_relocs
>(
2505 needs_special_offset_handling
,
2511 // Relocate a section during a relocatable link.
2514 Target_i386::relocate_for_relocatable(
2515 const Relocate_info
<32, false>* relinfo
,
2516 unsigned int sh_type
,
2517 const unsigned char* prelocs
,
2519 Output_section
* output_section
,
2520 off_t offset_in_output_section
,
2521 const Relocatable_relocs
* rr
,
2522 unsigned char* view
,
2523 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2524 section_size_type view_size
,
2525 unsigned char* reloc_view
,
2526 section_size_type reloc_view_size
)
2528 gold_assert(sh_type
== elfcpp::SHT_REL
);
2530 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2535 offset_in_output_section
,
2544 // Return the value to use for a dynamic which requires special
2545 // treatment. This is how we support equality comparisons of function
2546 // pointers across shared library boundaries, as described in the
2547 // processor specific ABI supplement.
2550 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2552 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2553 return this->plt_section()->address() + gsym
->plt_offset();
2556 // Return a string used to fill a code section with nops to take up
2557 // the specified length.
2560 Target_i386::do_code_fill(section_size_type length
) const
2564 // Build a jmp instruction to skip over the bytes.
2565 unsigned char jmp
[5];
2567 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2568 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2569 + std::string(length
- 5, '\0'));
2572 // Nop sequences of various lengths.
2573 const char nop1
[1] = { 0x90 }; // nop
2574 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2575 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2576 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2577 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2578 0x00 }; // leal 0(%esi,1),%esi
2579 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2581 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2583 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2584 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2585 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2586 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2588 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2589 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2591 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2592 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2594 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2595 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2596 0x00, 0x00, 0x00, 0x00 };
2597 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2598 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2599 0x27, 0x00, 0x00, 0x00,
2601 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2602 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2603 0xbc, 0x27, 0x00, 0x00,
2605 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2606 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2607 0x90, 0x90, 0x90, 0x90,
2610 const char* nops
[16] = {
2612 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2613 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2616 return std::string(nops
[length
], length
);
2619 // The selector for i386 object files.
2621 class Target_selector_i386
: public Target_selector
2624 Target_selector_i386()
2625 : Target_selector(elfcpp::EM_386
, 32, false, "elf32-i386")
2629 do_instantiate_target()
2630 { return new Target_i386(); }
2633 Target_selector_i386 target_selector_i386
;
2635 } // End anonymous namespace.