1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 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"
35 #include "copy-relocs.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
48 class Output_data_plt_i386
;
50 // The i386 target class.
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_i386
: public Target_freebsd
<32, false>
58 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
61 : Target_freebsd
<32, false>(&i386_info
),
62 got_(NULL
), plt_(NULL
), got_plt_(NULL
), rel_dyn_(NULL
),
63 copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
64 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
67 // Process the relocations to determine unreferenced sections for
68 // garbage collection.
70 gc_process_relocs(const General_options
& options
,
73 Sized_relobj
<32, false>* object
,
74 unsigned int data_shndx
,
76 const unsigned char* prelocs
,
78 Output_section
* output_section
,
79 bool needs_special_offset_handling
,
80 size_t local_symbol_count
,
81 const unsigned char* plocal_symbols
);
83 // Scan the relocations to look for symbol adjustments.
85 scan_relocs(const General_options
& options
,
88 Sized_relobj
<32, false>* object
,
89 unsigned int data_shndx
,
91 const unsigned char* prelocs
,
93 Output_section
* output_section
,
94 bool needs_special_offset_handling
,
95 size_t local_symbol_count
,
96 const unsigned char* plocal_symbols
);
98 // Finalize the sections.
100 do_finalize_sections(Layout
*);
102 // Return the value to use for a dynamic which requires special
105 do_dynsym_value(const Symbol
*) const;
107 // Relocate a section.
109 relocate_section(const Relocate_info
<32, false>*,
110 unsigned int sh_type
,
111 const unsigned char* prelocs
,
113 Output_section
* output_section
,
114 bool needs_special_offset_handling
,
116 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
117 section_size_type view_size
,
118 const Reloc_symbol_changes
*);
120 // Scan the relocs during a relocatable link.
122 scan_relocatable_relocs(const General_options
& options
,
123 Symbol_table
* symtab
,
125 Sized_relobj
<32, false>* object
,
126 unsigned int data_shndx
,
127 unsigned int sh_type
,
128 const unsigned char* prelocs
,
130 Output_section
* output_section
,
131 bool needs_special_offset_handling
,
132 size_t local_symbol_count
,
133 const unsigned char* plocal_symbols
,
134 Relocatable_relocs
*);
136 // Relocate a section during a relocatable link.
138 relocate_for_relocatable(const Relocate_info
<32, false>*,
139 unsigned int sh_type
,
140 const unsigned char* prelocs
,
142 Output_section
* output_section
,
143 off_t offset_in_output_section
,
144 const Relocatable_relocs
*,
146 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
147 section_size_type view_size
,
148 unsigned char* reloc_view
,
149 section_size_type reloc_view_size
);
151 // Return a string used to fill a code section with nops.
153 do_code_fill(section_size_type length
) const;
155 // Return whether SYM is defined by the ABI.
157 do_is_defined_by_abi(const Symbol
* sym
) const
158 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
160 // Return whether a symbol name implies a local label. The UnixWare
161 // 2.1 cc generates temporary symbols that start with .X, so we
162 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
163 // If so, we should move the .X recognition into
164 // Target::do_is_local_label_name.
166 do_is_local_label_name(const char* name
) const
168 if (name
[0] == '.' && name
[1] == 'X')
170 return Target::do_is_local_label_name(name
);
173 // Adjust -fstack-split code which calls non-stack-split code.
175 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
176 section_offset_type fnoffset
, section_size_type fnsize
,
177 unsigned char* view
, section_size_type view_size
,
178 std::string
* from
, std::string
* to
) const;
180 // Return the size of the GOT section.
184 gold_assert(this->got_
!= NULL
);
185 return this->got_
->data_size();
189 // The class which scans relocations.
193 local(const General_options
& options
, Symbol_table
* symtab
,
194 Layout
* layout
, Target_i386
* target
,
195 Sized_relobj
<32, false>* object
,
196 unsigned int data_shndx
,
197 Output_section
* output_section
,
198 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
199 const elfcpp::Sym
<32, false>& lsym
);
202 global(const General_options
& options
, Symbol_table
* symtab
,
203 Layout
* layout
, Target_i386
* target
,
204 Sized_relobj
<32, false>* object
,
205 unsigned int data_shndx
,
206 Output_section
* output_section
,
207 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
211 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
214 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
218 // The class which implements relocation.
223 : skip_call_tls_get_addr_(false),
224 local_dynamic_type_(LOCAL_DYNAMIC_NONE
), ldo_addrs_()
229 if (this->skip_call_tls_get_addr_
)
231 // FIXME: This needs to specify the location somehow.
232 gold_error(_("missing expected TLS relocation"));
236 // Return whether the static relocation needs to be applied.
238 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
241 Output_section
* output_section
);
243 // Do a relocation. Return false if the caller should not issue
244 // any warnings about this relocation.
246 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
247 size_t relnum
, const elfcpp::Rel
<32, false>&,
248 unsigned int r_type
, const Sized_symbol
<32>*,
249 const Symbol_value
<32>*,
250 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
254 // Do a TLS relocation.
256 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
257 size_t relnum
, const elfcpp::Rel
<32, false>&,
258 unsigned int r_type
, const Sized_symbol
<32>*,
259 const Symbol_value
<32>*,
260 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
263 // Do a TLS General-Dynamic to Initial-Exec transition.
265 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
266 Output_segment
* tls_segment
,
267 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
268 elfcpp::Elf_types
<32>::Elf_Addr value
,
270 section_size_type view_size
);
272 // Do a TLS General-Dynamic to Local-Exec transition.
274 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
275 Output_segment
* tls_segment
,
276 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
277 elfcpp::Elf_types
<32>::Elf_Addr value
,
279 section_size_type view_size
);
281 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
284 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
285 Output_segment
* tls_segment
,
286 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
287 elfcpp::Elf_types
<32>::Elf_Addr value
,
289 section_size_type view_size
);
291 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
294 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
295 Output_segment
* tls_segment
,
296 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
297 elfcpp::Elf_types
<32>::Elf_Addr value
,
299 section_size_type view_size
);
301 // Do a TLS Local-Dynamic to Local-Exec transition.
303 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
304 Output_segment
* tls_segment
,
305 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
306 elfcpp::Elf_types
<32>::Elf_Addr value
,
308 section_size_type view_size
);
310 // Do a TLS Initial-Exec to Local-Exec transition.
312 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
313 Output_segment
* tls_segment
,
314 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
315 elfcpp::Elf_types
<32>::Elf_Addr value
,
317 section_size_type view_size
);
319 // Fix up LDO_32 relocations we've already seen.
321 fix_up_ldo(const Relocate_info
<32, false>*);
323 // We need to keep track of which type of local dynamic relocation
324 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
325 enum Local_dynamic_type
332 // This is set if we should skip the next reloc, which should be a
333 // PLT32 reloc against ___tls_get_addr.
334 bool skip_call_tls_get_addr_
;
335 // The type of local dynamic relocation we have seen in the section
336 // being relocated, if any.
337 Local_dynamic_type local_dynamic_type_
;
338 // A list of LDO_32 offsets, in case we find LDM after LDO_32.
339 std::vector
<unsigned char*> ldo_addrs_
;
342 // A class which returns the size required for a relocation type,
343 // used while scanning relocs during a relocatable link.
344 class Relocatable_size_for_reloc
348 get_size_for_reloc(unsigned int, Relobj
*);
351 // Adjust TLS relocation type based on the options and whether this
352 // is a local symbol.
353 static tls::Tls_optimization
354 optimize_tls_reloc(bool is_final
, int r_type
);
356 // Get the GOT section, creating it if necessary.
357 Output_data_got
<32, false>*
358 got_section(Symbol_table
*, Layout
*);
360 // Get the GOT PLT section.
362 got_plt_section() const
364 gold_assert(this->got_plt_
!= NULL
);
365 return this->got_plt_
;
368 // Create a PLT entry for a global symbol.
370 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
372 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
374 define_tls_base_symbol(Symbol_table
*, Layout
*);
376 // Create a GOT entry for the TLS module index.
378 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
379 Sized_relobj
<32, false>* object
);
381 // Get the PLT section.
382 const Output_data_plt_i386
*
385 gold_assert(this->plt_
!= NULL
);
389 // Get the dynamic reloc section, creating it if necessary.
391 rel_dyn_section(Layout
*);
393 // Add a potential copy relocation.
395 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
396 Sized_relobj
<32, false>* object
,
397 unsigned int shndx
, Output_section
* output_section
,
398 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
400 this->copy_relocs_
.copy_reloc(symtab
, layout
,
401 symtab
->get_sized_symbol
<32>(sym
),
402 object
, shndx
, output_section
, reloc
,
403 this->rel_dyn_section(layout
));
406 // Information about this specific target which we pass to the
407 // general Target structure.
408 static const Target::Target_info i386_info
;
410 // The types of GOT entries needed for this platform.
413 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
414 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
415 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
416 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
417 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
421 Output_data_got
<32, false>* got_
;
423 Output_data_plt_i386
* plt_
;
424 // The GOT PLT section.
425 Output_data_space
* got_plt_
;
426 // The dynamic reloc section.
427 Reloc_section
* rel_dyn_
;
428 // Relocs saved to avoid a COPY reloc.
429 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
430 // Space for variables copied with a COPY reloc.
431 Output_data_space
* dynbss_
;
432 // Offset of the GOT entry for the TLS module index.
433 unsigned int got_mod_index_offset_
;
434 // True if the _TLS_MODULE_BASE_ symbol has been defined.
435 bool tls_base_symbol_defined_
;
438 const Target::Target_info
Target_i386::i386_info
=
441 false, // is_big_endian
442 elfcpp::EM_386
, // machine_code
443 false, // has_make_symbol
444 false, // has_resolve
445 true, // has_code_fill
446 true, // is_default_stack_executable
448 "/usr/lib/libc.so.1", // dynamic_linker
449 0x08048000, // default_text_segment_address
450 0x1000, // abi_pagesize (overridable by -z max-page-size)
451 0x1000, // common_pagesize (overridable by -z common-page-size)
452 elfcpp::SHN_UNDEF
, // small_common_shndx
453 elfcpp::SHN_UNDEF
, // large_common_shndx
454 0, // small_common_section_flags
455 0 // large_common_section_flags
458 // Get the GOT section, creating it if necessary.
460 Output_data_got
<32, false>*
461 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
463 if (this->got_
== NULL
)
465 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
467 this->got_
= new Output_data_got
<32, false>();
470 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
472 | elfcpp::SHF_WRITE
),
476 // The old GNU linker creates a .got.plt section. We just
477 // create another set of data in the .got section. Note that we
478 // always create a PLT if we create a GOT, although the PLT
480 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
481 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
483 | elfcpp::SHF_WRITE
),
487 // The first three entries are reserved.
488 this->got_plt_
->set_current_data_size(3 * 4);
490 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
491 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
493 0, 0, elfcpp::STT_OBJECT
,
495 elfcpp::STV_HIDDEN
, 0,
502 // Get the dynamic reloc section, creating it if necessary.
504 Target_i386::Reloc_section
*
505 Target_i386::rel_dyn_section(Layout
* layout
)
507 if (this->rel_dyn_
== NULL
)
509 gold_assert(layout
!= NULL
);
510 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
511 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
512 elfcpp::SHF_ALLOC
, this->rel_dyn_
);
514 return this->rel_dyn_
;
517 // A class to handle the PLT data.
519 class Output_data_plt_i386
: public Output_section_data
522 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
524 Output_data_plt_i386(Layout
*, Output_data_space
*);
526 // Add an entry to the PLT.
528 add_entry(Symbol
* gsym
);
530 // Return the .rel.plt section data.
533 { return this->rel_
; }
537 do_adjust_output_section(Output_section
* os
);
539 // Write to a map file.
541 do_print_to_mapfile(Mapfile
* mapfile
) const
542 { mapfile
->print_output_data(this, _("** PLT")); }
545 // The size of an entry in the PLT.
546 static const int plt_entry_size
= 16;
548 // The first entry in the PLT for an executable.
549 static unsigned char exec_first_plt_entry
[plt_entry_size
];
551 // The first entry in the PLT for a shared object.
552 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
554 // Other entries in the PLT for an executable.
555 static unsigned char exec_plt_entry
[plt_entry_size
];
557 // Other entries in the PLT for a shared object.
558 static unsigned char dyn_plt_entry
[plt_entry_size
];
560 // Set the final size.
562 set_final_data_size()
563 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
565 // Write out the PLT data.
567 do_write(Output_file
*);
569 // The reloc section.
571 // The .got.plt section.
572 Output_data_space
* got_plt_
;
573 // The number of PLT entries.
577 // Create the PLT section. The ordinary .got section is an argument,
578 // since we need to refer to the start. We also create our own .got
579 // section just for PLT entries.
581 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
582 Output_data_space
* got_plt
)
583 : Output_section_data(4), got_plt_(got_plt
), count_(0)
585 this->rel_
= new Reloc_section(false);
586 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
587 elfcpp::SHF_ALLOC
, this->rel_
);
591 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
593 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
594 // linker, and so do we.
598 // Add an entry to the PLT.
601 Output_data_plt_i386::add_entry(Symbol
* gsym
)
603 gold_assert(!gsym
->has_plt_offset());
605 // Note that when setting the PLT offset we skip the initial
606 // reserved PLT entry.
607 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
611 section_offset_type got_offset
= this->got_plt_
->current_data_size();
613 // Every PLT entry needs a GOT entry which points back to the PLT
614 // entry (this will be changed by the dynamic linker, normally
615 // lazily when the function is called).
616 this->got_plt_
->set_current_data_size(got_offset
+ 4);
618 // Every PLT entry needs a reloc.
619 gsym
->set_needs_dynsym_entry();
620 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
623 // Note that we don't need to save the symbol. The contents of the
624 // PLT are independent of which symbols are used. The symbols only
625 // appear in the relocations.
628 // The first entry in the PLT for an executable.
630 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
632 0xff, 0x35, // pushl contents of memory address
633 0, 0, 0, 0, // replaced with address of .got + 4
634 0xff, 0x25, // jmp indirect
635 0, 0, 0, 0, // replaced with address of .got + 8
639 // The first entry in the PLT for a shared object.
641 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
643 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
644 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
648 // Subsequent entries in the PLT for an executable.
650 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
652 0xff, 0x25, // jmp indirect
653 0, 0, 0, 0, // replaced with address of symbol in .got
654 0x68, // pushl immediate
655 0, 0, 0, 0, // replaced with offset into relocation table
656 0xe9, // jmp relative
657 0, 0, 0, 0 // replaced with offset to start of .plt
660 // Subsequent entries in the PLT for a shared object.
662 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
664 0xff, 0xa3, // jmp *offset(%ebx)
665 0, 0, 0, 0, // replaced with offset of symbol in .got
666 0x68, // pushl immediate
667 0, 0, 0, 0, // replaced with offset into relocation table
668 0xe9, // jmp relative
669 0, 0, 0, 0 // replaced with offset to start of .plt
672 // Write out the PLT. This uses the hand-coded instructions above,
673 // and adjusts them as needed. This is all specified by the i386 ELF
674 // Processor Supplement.
677 Output_data_plt_i386::do_write(Output_file
* of
)
679 const off_t offset
= this->offset();
680 const section_size_type oview_size
=
681 convert_to_section_size_type(this->data_size());
682 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
684 const off_t got_file_offset
= this->got_plt_
->offset();
685 const section_size_type got_size
=
686 convert_to_section_size_type(this->got_plt_
->data_size());
687 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
690 unsigned char* pov
= oview
;
692 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
693 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
695 if (parameters
->options().shared())
696 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
699 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
700 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
701 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
703 pov
+= plt_entry_size
;
705 unsigned char* got_pov
= got_view
;
707 memset(got_pov
, 0, 12);
710 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
712 unsigned int plt_offset
= plt_entry_size
;
713 unsigned int plt_rel_offset
= 0;
714 unsigned int got_offset
= 12;
715 const unsigned int count
= this->count_
;
716 for (unsigned int i
= 0;
719 pov
+= plt_entry_size
,
721 plt_offset
+= plt_entry_size
,
722 plt_rel_offset
+= rel_size
,
725 // Set and adjust the PLT entry itself.
727 if (parameters
->options().shared())
729 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
730 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
734 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
735 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
740 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
741 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
742 - (plt_offset
+ plt_entry_size
));
744 // Set the entry in the GOT.
745 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
748 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
749 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
751 of
->write_output_view(offset
, oview_size
, oview
);
752 of
->write_output_view(got_file_offset
, got_size
, got_view
);
755 // Create a PLT entry for a global symbol.
758 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
760 if (gsym
->has_plt_offset())
763 if (this->plt_
== NULL
)
765 // Create the GOT sections first.
766 this->got_section(symtab
, layout
);
768 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
769 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
771 | elfcpp::SHF_EXECINSTR
),
775 this->plt_
->add_entry(gsym
);
778 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
781 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
783 if (this->tls_base_symbol_defined_
)
786 Output_segment
* tls_segment
= layout
->tls_segment();
787 if (tls_segment
!= NULL
)
789 bool is_exec
= parameters
->options().output_is_executable();
790 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
794 elfcpp::STV_HIDDEN
, 0,
796 ? Symbol::SEGMENT_END
797 : Symbol::SEGMENT_START
),
800 this->tls_base_symbol_defined_
= true;
803 // Create a GOT entry for the TLS module index.
806 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
807 Sized_relobj
<32, false>* object
)
809 if (this->got_mod_index_offset_
== -1U)
811 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
812 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
813 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
814 unsigned int got_offset
= got
->add_constant(0);
815 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
817 got
->add_constant(0);
818 this->got_mod_index_offset_
= got_offset
;
820 return this->got_mod_index_offset_
;
823 // Optimize the TLS relocation type based on what we know about the
824 // symbol. IS_FINAL is true if the final address of this symbol is
825 // known at link time.
827 tls::Tls_optimization
828 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
830 // If we are generating a shared library, then we can't do anything
832 if (parameters
->options().shared())
833 return tls::TLSOPT_NONE
;
837 case elfcpp::R_386_TLS_GD
:
838 case elfcpp::R_386_TLS_GOTDESC
:
839 case elfcpp::R_386_TLS_DESC_CALL
:
840 // These are General-Dynamic which permits fully general TLS
841 // access. Since we know that we are generating an executable,
842 // we can convert this to Initial-Exec. If we also know that
843 // this is a local symbol, we can further switch to Local-Exec.
845 return tls::TLSOPT_TO_LE
;
846 return tls::TLSOPT_TO_IE
;
848 case elfcpp::R_386_TLS_LDM
:
849 // This is Local-Dynamic, which refers to a local symbol in the
850 // dynamic TLS block. Since we know that we generating an
851 // executable, we can switch to Local-Exec.
852 return tls::TLSOPT_TO_LE
;
854 case elfcpp::R_386_TLS_LDO_32
:
855 // Another type of Local-Dynamic relocation.
856 return tls::TLSOPT_TO_LE
;
858 case elfcpp::R_386_TLS_IE
:
859 case elfcpp::R_386_TLS_GOTIE
:
860 case elfcpp::R_386_TLS_IE_32
:
861 // These are Initial-Exec relocs which get the thread offset
862 // from the GOT. If we know that we are linking against the
863 // local symbol, we can switch to Local-Exec, which links the
864 // thread offset into the instruction.
866 return tls::TLSOPT_TO_LE
;
867 return tls::TLSOPT_NONE
;
869 case elfcpp::R_386_TLS_LE
:
870 case elfcpp::R_386_TLS_LE_32
:
871 // When we already have Local-Exec, there is nothing further we
873 return tls::TLSOPT_NONE
;
880 // Report an unsupported relocation against a local symbol.
883 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
886 gold_error(_("%s: unsupported reloc %u against local symbol"),
887 object
->name().c_str(), r_type
);
890 // Scan a relocation for a local symbol.
893 Target_i386::Scan::local(const General_options
&,
894 Symbol_table
* symtab
,
897 Sized_relobj
<32, false>* object
,
898 unsigned int data_shndx
,
899 Output_section
* output_section
,
900 const elfcpp::Rel
<32, false>& reloc
,
902 const elfcpp::Sym
<32, false>& lsym
)
906 case elfcpp::R_386_NONE
:
907 case elfcpp::R_386_GNU_VTINHERIT
:
908 case elfcpp::R_386_GNU_VTENTRY
:
911 case elfcpp::R_386_32
:
912 // If building a shared library (or a position-independent
913 // executable), we need to create a dynamic relocation for
914 // this location. The relocation applied at link time will
915 // apply the link-time value, so we flag the location with
916 // an R_386_RELATIVE relocation so the dynamic loader can
917 // relocate it easily.
918 if (parameters
->options().output_is_position_independent())
920 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
921 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
922 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
923 output_section
, data_shndx
,
924 reloc
.get_r_offset());
928 case elfcpp::R_386_16
:
929 case elfcpp::R_386_8
:
930 // If building a shared library (or a position-independent
931 // executable), we need to create a dynamic relocation for
932 // this location. Because the addend needs to remain in the
933 // data section, we need to be careful not to apply this
934 // relocation statically.
935 if (parameters
->options().output_is_position_independent())
937 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
938 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
939 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
940 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
941 data_shndx
, reloc
.get_r_offset());
944 gold_assert(lsym
.get_st_value() == 0);
945 unsigned int shndx
= lsym
.get_st_shndx();
947 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
950 object
->error(_("section symbol %u has bad shndx %u"),
953 rel_dyn
->add_local_section(object
, shndx
,
954 r_type
, output_section
,
955 data_shndx
, reloc
.get_r_offset());
960 case elfcpp::R_386_PC32
:
961 case elfcpp::R_386_PC16
:
962 case elfcpp::R_386_PC8
:
965 case elfcpp::R_386_PLT32
:
966 // Since we know this is a local symbol, we can handle this as a
970 case elfcpp::R_386_GOTOFF
:
971 case elfcpp::R_386_GOTPC
:
972 // We need a GOT section.
973 target
->got_section(symtab
, layout
);
976 case elfcpp::R_386_GOT32
:
978 // The symbol requires a GOT entry.
979 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
980 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
981 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
983 // If we are generating a shared object, we need to add a
984 // dynamic RELATIVE relocation for this symbol's GOT entry.
985 if (parameters
->options().output_is_position_independent())
987 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
988 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
989 rel_dyn
->add_local_relative(
990 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
991 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
997 // These are relocations which should only be seen by the
998 // dynamic linker, and should never be seen here.
999 case elfcpp::R_386_COPY
:
1000 case elfcpp::R_386_GLOB_DAT
:
1001 case elfcpp::R_386_JUMP_SLOT
:
1002 case elfcpp::R_386_RELATIVE
:
1003 case elfcpp::R_386_TLS_TPOFF
:
1004 case elfcpp::R_386_TLS_DTPMOD32
:
1005 case elfcpp::R_386_TLS_DTPOFF32
:
1006 case elfcpp::R_386_TLS_TPOFF32
:
1007 case elfcpp::R_386_TLS_DESC
:
1008 gold_error(_("%s: unexpected reloc %u in object file"),
1009 object
->name().c_str(), r_type
);
1012 // These are initial TLS relocs, which are expected when
1014 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1015 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1016 case elfcpp::R_386_TLS_DESC_CALL
:
1017 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1018 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1019 case elfcpp::R_386_TLS_IE
: // Initial-exec
1020 case elfcpp::R_386_TLS_IE_32
:
1021 case elfcpp::R_386_TLS_GOTIE
:
1022 case elfcpp::R_386_TLS_LE
: // Local-exec
1023 case elfcpp::R_386_TLS_LE_32
:
1025 bool output_is_shared
= parameters
->options().shared();
1026 const tls::Tls_optimization optimized_type
1027 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1030 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1031 if (optimized_type
== tls::TLSOPT_NONE
)
1033 // Create a pair of GOT entries for the module index and
1034 // dtv-relative offset.
1035 Output_data_got
<32, false>* got
1036 = target
->got_section(symtab
, layout
);
1037 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1038 unsigned int shndx
= lsym
.get_st_shndx();
1040 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1042 object
->error(_("local symbol %u has bad shndx %u"),
1045 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1047 target
->rel_dyn_section(layout
),
1048 elfcpp::R_386_TLS_DTPMOD32
, 0);
1050 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1051 unsupported_reloc_local(object
, r_type
);
1054 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1055 target
->define_tls_base_symbol(symtab
, layout
);
1056 if (optimized_type
== tls::TLSOPT_NONE
)
1058 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1059 Output_data_got
<32, false>* got
1060 = target
->got_section(symtab
, layout
);
1061 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1062 unsigned int shndx
= lsym
.get_st_shndx();
1064 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1066 object
->error(_("local symbol %u has bad shndx %u"),
1069 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1071 target
->rel_dyn_section(layout
),
1072 elfcpp::R_386_TLS_DESC
, 0);
1074 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1075 unsupported_reloc_local(object
, r_type
);
1078 case elfcpp::R_386_TLS_DESC_CALL
:
1081 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1082 if (optimized_type
== tls::TLSOPT_NONE
)
1084 // Create a GOT entry for the module index.
1085 target
->got_mod_index_entry(symtab
, layout
, object
);
1087 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1088 unsupported_reloc_local(object
, r_type
);
1091 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1094 case elfcpp::R_386_TLS_IE
: // Initial-exec
1095 case elfcpp::R_386_TLS_IE_32
:
1096 case elfcpp::R_386_TLS_GOTIE
:
1097 layout
->set_has_static_tls();
1098 if (optimized_type
== tls::TLSOPT_NONE
)
1100 // For the R_386_TLS_IE relocation, we need to create a
1101 // dynamic relocation when building a shared library.
1102 if (r_type
== elfcpp::R_386_TLS_IE
1103 && parameters
->options().shared())
1105 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1107 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1108 rel_dyn
->add_local_relative(object
, r_sym
,
1109 elfcpp::R_386_RELATIVE
,
1110 output_section
, data_shndx
,
1111 reloc
.get_r_offset());
1113 // Create a GOT entry for the tp-relative offset.
1114 Output_data_got
<32, false>* got
1115 = target
->got_section(symtab
, layout
);
1116 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1117 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1118 ? elfcpp::R_386_TLS_TPOFF32
1119 : elfcpp::R_386_TLS_TPOFF
);
1120 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1121 ? GOT_TYPE_TLS_OFFSET
1122 : GOT_TYPE_TLS_NOFFSET
);
1123 got
->add_local_with_rel(object
, r_sym
, got_type
,
1124 target
->rel_dyn_section(layout
),
1127 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1128 unsupported_reloc_local(object
, r_type
);
1131 case elfcpp::R_386_TLS_LE
: // Local-exec
1132 case elfcpp::R_386_TLS_LE_32
:
1133 layout
->set_has_static_tls();
1134 if (output_is_shared
)
1136 // We need to create a dynamic relocation.
1137 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1138 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1139 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1140 ? elfcpp::R_386_TLS_TPOFF32
1141 : elfcpp::R_386_TLS_TPOFF
);
1142 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1143 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1144 data_shndx
, reloc
.get_r_offset());
1154 case elfcpp::R_386_32PLT
:
1155 case elfcpp::R_386_TLS_GD_32
:
1156 case elfcpp::R_386_TLS_GD_PUSH
:
1157 case elfcpp::R_386_TLS_GD_CALL
:
1158 case elfcpp::R_386_TLS_GD_POP
:
1159 case elfcpp::R_386_TLS_LDM_32
:
1160 case elfcpp::R_386_TLS_LDM_PUSH
:
1161 case elfcpp::R_386_TLS_LDM_CALL
:
1162 case elfcpp::R_386_TLS_LDM_POP
:
1163 case elfcpp::R_386_USED_BY_INTEL_200
:
1165 unsupported_reloc_local(object
, r_type
);
1170 // Report an unsupported relocation against a global symbol.
1173 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1174 unsigned int r_type
,
1177 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1178 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1181 // Scan a relocation for a global symbol.
1184 Target_i386::Scan::global(const General_options
&,
1185 Symbol_table
* symtab
,
1187 Target_i386
* target
,
1188 Sized_relobj
<32, false>* object
,
1189 unsigned int data_shndx
,
1190 Output_section
* output_section
,
1191 const elfcpp::Rel
<32, false>& reloc
,
1192 unsigned int r_type
,
1197 case elfcpp::R_386_NONE
:
1198 case elfcpp::R_386_GNU_VTINHERIT
:
1199 case elfcpp::R_386_GNU_VTENTRY
:
1202 case elfcpp::R_386_32
:
1203 case elfcpp::R_386_16
:
1204 case elfcpp::R_386_8
:
1206 // Make a PLT entry if necessary.
1207 if (gsym
->needs_plt_entry())
1209 target
->make_plt_entry(symtab
, layout
, gsym
);
1210 // Since this is not a PC-relative relocation, we may be
1211 // taking the address of a function. In that case we need to
1212 // set the entry in the dynamic symbol table to the address of
1214 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1215 gsym
->set_needs_dynsym_value();
1217 // Make a dynamic relocation if necessary.
1218 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1220 if (gsym
->may_need_copy_reloc())
1222 target
->copy_reloc(symtab
, layout
, object
,
1223 data_shndx
, output_section
, gsym
, reloc
);
1225 else if (r_type
== elfcpp::R_386_32
1226 && gsym
->can_use_relative_reloc(false))
1228 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1229 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1230 output_section
, object
,
1231 data_shndx
, reloc
.get_r_offset());
1235 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1236 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1237 data_shndx
, reloc
.get_r_offset());
1243 case elfcpp::R_386_PC32
:
1244 case elfcpp::R_386_PC16
:
1245 case elfcpp::R_386_PC8
:
1247 // Make a PLT entry if necessary.
1248 if (gsym
->needs_plt_entry())
1250 // These relocations are used for function calls only in
1251 // non-PIC code. For a 32-bit relocation in a shared library,
1252 // we'll need a text relocation anyway, so we can skip the
1253 // PLT entry and let the dynamic linker bind the call directly
1254 // to the target. For smaller relocations, we should use a
1255 // PLT entry to ensure that the call can reach.
1256 if (!parameters
->options().shared()
1257 || r_type
!= elfcpp::R_386_PC32
)
1258 target
->make_plt_entry(symtab
, layout
, gsym
);
1260 // Make a dynamic relocation if necessary.
1261 int flags
= Symbol::NON_PIC_REF
;
1262 if (gsym
->type() == elfcpp::STT_FUNC
)
1263 flags
|= Symbol::FUNCTION_CALL
;
1264 if (gsym
->needs_dynamic_reloc(flags
))
1266 if (gsym
->may_need_copy_reloc())
1268 target
->copy_reloc(symtab
, layout
, object
,
1269 data_shndx
, output_section
, gsym
, reloc
);
1273 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1274 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1275 data_shndx
, reloc
.get_r_offset());
1281 case elfcpp::R_386_GOT32
:
1283 // The symbol requires a GOT entry.
1284 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1285 if (gsym
->final_value_is_known())
1286 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1289 // If this symbol is not fully resolved, we need to add a
1290 // GOT entry with a dynamic relocation.
1291 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1292 if (gsym
->is_from_dynobj()
1293 || gsym
->is_undefined()
1294 || gsym
->is_preemptible())
1295 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1296 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1299 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1300 rel_dyn
->add_global_relative(
1301 gsym
, elfcpp::R_386_RELATIVE
, got
,
1302 gsym
->got_offset(GOT_TYPE_STANDARD
));
1308 case elfcpp::R_386_PLT32
:
1309 // If the symbol is fully resolved, this is just a PC32 reloc.
1310 // Otherwise we need a PLT entry.
1311 if (gsym
->final_value_is_known())
1313 // If building a shared library, we can also skip the PLT entry
1314 // if the symbol is defined in the output file and is protected
1316 if (gsym
->is_defined()
1317 && !gsym
->is_from_dynobj()
1318 && !gsym
->is_preemptible())
1320 target
->make_plt_entry(symtab
, layout
, gsym
);
1323 case elfcpp::R_386_GOTOFF
:
1324 case elfcpp::R_386_GOTPC
:
1325 // We need a GOT section.
1326 target
->got_section(symtab
, layout
);
1329 // These are relocations which should only be seen by the
1330 // dynamic linker, and should never be seen here.
1331 case elfcpp::R_386_COPY
:
1332 case elfcpp::R_386_GLOB_DAT
:
1333 case elfcpp::R_386_JUMP_SLOT
:
1334 case elfcpp::R_386_RELATIVE
:
1335 case elfcpp::R_386_TLS_TPOFF
:
1336 case elfcpp::R_386_TLS_DTPMOD32
:
1337 case elfcpp::R_386_TLS_DTPOFF32
:
1338 case elfcpp::R_386_TLS_TPOFF32
:
1339 case elfcpp::R_386_TLS_DESC
:
1340 gold_error(_("%s: unexpected reloc %u in object file"),
1341 object
->name().c_str(), r_type
);
1344 // These are initial tls relocs, which are expected when
1346 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1347 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1348 case elfcpp::R_386_TLS_DESC_CALL
:
1349 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1350 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1351 case elfcpp::R_386_TLS_IE
: // Initial-exec
1352 case elfcpp::R_386_TLS_IE_32
:
1353 case elfcpp::R_386_TLS_GOTIE
:
1354 case elfcpp::R_386_TLS_LE
: // Local-exec
1355 case elfcpp::R_386_TLS_LE_32
:
1357 const bool is_final
= gsym
->final_value_is_known();
1358 const tls::Tls_optimization optimized_type
1359 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1362 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1363 if (optimized_type
== tls::TLSOPT_NONE
)
1365 // Create a pair of GOT entries for the module index and
1366 // dtv-relative offset.
1367 Output_data_got
<32, false>* got
1368 = target
->got_section(symtab
, layout
);
1369 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1370 target
->rel_dyn_section(layout
),
1371 elfcpp::R_386_TLS_DTPMOD32
,
1372 elfcpp::R_386_TLS_DTPOFF32
);
1374 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1376 // Create a GOT entry for the tp-relative offset.
1377 Output_data_got
<32, false>* got
1378 = target
->got_section(symtab
, layout
);
1379 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1380 target
->rel_dyn_section(layout
),
1381 elfcpp::R_386_TLS_TPOFF
);
1383 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1384 unsupported_reloc_global(object
, r_type
, gsym
);
1387 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1388 target
->define_tls_base_symbol(symtab
, layout
);
1389 if (optimized_type
== tls::TLSOPT_NONE
)
1391 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1392 Output_data_got
<32, false>* got
1393 = target
->got_section(symtab
, layout
);
1394 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
,
1395 target
->rel_dyn_section(layout
),
1396 elfcpp::R_386_TLS_DESC
, 0);
1398 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1400 // Create a GOT entry for the tp-relative offset.
1401 Output_data_got
<32, false>* got
1402 = target
->got_section(symtab
, layout
);
1403 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1404 target
->rel_dyn_section(layout
),
1405 elfcpp::R_386_TLS_TPOFF
);
1407 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1408 unsupported_reloc_global(object
, r_type
, gsym
);
1411 case elfcpp::R_386_TLS_DESC_CALL
:
1414 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1415 if (optimized_type
== tls::TLSOPT_NONE
)
1417 // Create a GOT entry for the module index.
1418 target
->got_mod_index_entry(symtab
, layout
, object
);
1420 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1421 unsupported_reloc_global(object
, r_type
, gsym
);
1424 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1427 case elfcpp::R_386_TLS_IE
: // Initial-exec
1428 case elfcpp::R_386_TLS_IE_32
:
1429 case elfcpp::R_386_TLS_GOTIE
:
1430 layout
->set_has_static_tls();
1431 if (optimized_type
== tls::TLSOPT_NONE
)
1433 // For the R_386_TLS_IE relocation, we need to create a
1434 // dynamic relocation when building a shared library.
1435 if (r_type
== elfcpp::R_386_TLS_IE
1436 && parameters
->options().shared())
1438 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1439 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1440 output_section
, object
,
1442 reloc
.get_r_offset());
1444 // Create a GOT entry for the tp-relative offset.
1445 Output_data_got
<32, false>* got
1446 = target
->got_section(symtab
, layout
);
1447 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1448 ? elfcpp::R_386_TLS_TPOFF32
1449 : elfcpp::R_386_TLS_TPOFF
);
1450 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1451 ? GOT_TYPE_TLS_OFFSET
1452 : GOT_TYPE_TLS_NOFFSET
);
1453 got
->add_global_with_rel(gsym
, got_type
,
1454 target
->rel_dyn_section(layout
),
1457 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1458 unsupported_reloc_global(object
, r_type
, gsym
);
1461 case elfcpp::R_386_TLS_LE
: // Local-exec
1462 case elfcpp::R_386_TLS_LE_32
:
1463 layout
->set_has_static_tls();
1464 if (parameters
->options().shared())
1466 // We need to create a dynamic relocation.
1467 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1468 ? elfcpp::R_386_TLS_TPOFF32
1469 : elfcpp::R_386_TLS_TPOFF
);
1470 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1471 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1472 data_shndx
, reloc
.get_r_offset());
1482 case elfcpp::R_386_32PLT
:
1483 case elfcpp::R_386_TLS_GD_32
:
1484 case elfcpp::R_386_TLS_GD_PUSH
:
1485 case elfcpp::R_386_TLS_GD_CALL
:
1486 case elfcpp::R_386_TLS_GD_POP
:
1487 case elfcpp::R_386_TLS_LDM_32
:
1488 case elfcpp::R_386_TLS_LDM_PUSH
:
1489 case elfcpp::R_386_TLS_LDM_CALL
:
1490 case elfcpp::R_386_TLS_LDM_POP
:
1491 case elfcpp::R_386_USED_BY_INTEL_200
:
1493 unsupported_reloc_global(object
, r_type
, gsym
);
1498 // Process relocations for gc.
1501 Target_i386::gc_process_relocs(const General_options
& options
,
1502 Symbol_table
* symtab
,
1504 Sized_relobj
<32, false>* object
,
1505 unsigned int data_shndx
,
1507 const unsigned char* prelocs
,
1509 Output_section
* output_section
,
1510 bool needs_special_offset_handling
,
1511 size_t local_symbol_count
,
1512 const unsigned char* plocal_symbols
)
1514 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1525 needs_special_offset_handling
,
1530 // Scan relocations for a section.
1533 Target_i386::scan_relocs(const General_options
& options
,
1534 Symbol_table
* symtab
,
1536 Sized_relobj
<32, false>* object
,
1537 unsigned int data_shndx
,
1538 unsigned int sh_type
,
1539 const unsigned char* prelocs
,
1541 Output_section
* output_section
,
1542 bool needs_special_offset_handling
,
1543 size_t local_symbol_count
,
1544 const unsigned char* plocal_symbols
)
1546 if (sh_type
== elfcpp::SHT_RELA
)
1548 gold_error(_("%s: unsupported RELA reloc section"),
1549 object
->name().c_str());
1553 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1564 needs_special_offset_handling
,
1569 // Finalize the sections.
1572 Target_i386::do_finalize_sections(Layout
* layout
)
1574 // Fill in some more dynamic tags.
1575 Output_data_dynamic
* const odyn
= layout
->dynamic_data();
1578 if (this->got_plt_
!= NULL
)
1579 odyn
->add_section_address(elfcpp::DT_PLTGOT
, this->got_plt_
);
1581 if (this->plt_
!= NULL
)
1583 const Output_data
* od
= this->plt_
->rel_plt();
1584 odyn
->add_section_size(elfcpp::DT_PLTRELSZ
, od
);
1585 odyn
->add_section_address(elfcpp::DT_JMPREL
, od
);
1586 odyn
->add_constant(elfcpp::DT_PLTREL
, elfcpp::DT_REL
);
1589 if (this->rel_dyn_
!= NULL
)
1591 const Output_data
* od
= this->rel_dyn_
;
1592 odyn
->add_section_address(elfcpp::DT_REL
, od
);
1593 odyn
->add_section_size(elfcpp::DT_RELSZ
, od
);
1594 odyn
->add_constant(elfcpp::DT_RELENT
,
1595 elfcpp::Elf_sizes
<32>::rel_size
);
1598 if (!parameters
->options().shared())
1600 // The value of the DT_DEBUG tag is filled in by the dynamic
1601 // linker at run time, and used by the debugger.
1602 odyn
->add_constant(elfcpp::DT_DEBUG
, 0);
1606 // Emit any relocs we saved in an attempt to avoid generating COPY
1608 if (this->copy_relocs_
.any_saved_relocs())
1609 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1612 // Return whether a direct absolute static relocation needs to be applied.
1613 // In cases where Scan::local() or Scan::global() has created
1614 // a dynamic relocation other than R_386_RELATIVE, the addend
1615 // of the relocation is carried in the data, and we must not
1616 // apply the static relocation.
1619 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1622 Output_section
* output_section
)
1624 // If the output section is not allocated, then we didn't call
1625 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1627 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1630 // For local symbols, we will have created a non-RELATIVE dynamic
1631 // relocation only if (a) the output is position independent,
1632 // (b) the relocation is absolute (not pc- or segment-relative), and
1633 // (c) the relocation is not 32 bits wide.
1635 return !(parameters
->options().output_is_position_independent()
1636 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1639 // For global symbols, we use the same helper routines used in the
1640 // scan pass. If we did not create a dynamic relocation, or if we
1641 // created a RELATIVE dynamic relocation, we should apply the static
1643 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1644 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1645 && gsym
->can_use_relative_reloc(ref_flags
1646 & Symbol::FUNCTION_CALL
);
1647 return !has_dyn
|| is_rel
;
1650 // Perform a relocation.
1653 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1654 Target_i386
* target
,
1655 Output_section
*output_section
,
1657 const elfcpp::Rel
<32, false>& rel
,
1658 unsigned int r_type
,
1659 const Sized_symbol
<32>* gsym
,
1660 const Symbol_value
<32>* psymval
,
1661 unsigned char* view
,
1662 elfcpp::Elf_types
<32>::Elf_Addr address
,
1663 section_size_type view_size
)
1665 if (this->skip_call_tls_get_addr_
)
1667 if ((r_type
!= elfcpp::R_386_PLT32
1668 && r_type
!= elfcpp::R_386_PC32
)
1670 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1671 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1672 _("missing expected TLS relocation"));
1675 this->skip_call_tls_get_addr_
= false;
1680 // Pick the value to use for symbols defined in shared objects.
1681 Symbol_value
<32> symval
;
1683 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1684 || r_type
== elfcpp::R_386_PC16
1685 || r_type
== elfcpp::R_386_PC32
))
1687 symval
.set_output_value(target
->plt_section()->address()
1688 + gsym
->plt_offset());
1692 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1694 // Get the GOT offset if needed.
1695 // The GOT pointer points to the end of the GOT section.
1696 // We need to subtract the size of the GOT section to get
1697 // the actual offset to use in the relocation.
1698 bool have_got_offset
= false;
1699 unsigned int got_offset
= 0;
1702 case elfcpp::R_386_GOT32
:
1705 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1706 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1707 - target
->got_size());
1711 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1712 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1713 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1714 - target
->got_size());
1716 have_got_offset
= true;
1725 case elfcpp::R_386_NONE
:
1726 case elfcpp::R_386_GNU_VTINHERIT
:
1727 case elfcpp::R_386_GNU_VTENTRY
:
1730 case elfcpp::R_386_32
:
1731 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1733 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1736 case elfcpp::R_386_PC32
:
1738 int ref_flags
= Symbol::NON_PIC_REF
;
1739 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1740 ref_flags
|= Symbol::FUNCTION_CALL
;
1741 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1742 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1746 case elfcpp::R_386_16
:
1747 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1749 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1752 case elfcpp::R_386_PC16
:
1754 int ref_flags
= Symbol::NON_PIC_REF
;
1755 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1756 ref_flags
|= Symbol::FUNCTION_CALL
;
1757 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1758 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1762 case elfcpp::R_386_8
:
1763 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1765 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1768 case elfcpp::R_386_PC8
:
1770 int ref_flags
= Symbol::NON_PIC_REF
;
1771 if (gsym
!= NULL
&& gsym
->type() == elfcpp::STT_FUNC
)
1772 ref_flags
|= Symbol::FUNCTION_CALL
;
1773 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1775 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1779 case elfcpp::R_386_PLT32
:
1780 gold_assert(gsym
== NULL
1781 || gsym
->has_plt_offset()
1782 || gsym
->final_value_is_known()
1783 || (gsym
->is_defined()
1784 && !gsym
->is_from_dynobj()
1785 && !gsym
->is_preemptible()));
1786 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1789 case elfcpp::R_386_GOT32
:
1790 gold_assert(have_got_offset
);
1791 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1794 case elfcpp::R_386_GOTOFF
:
1796 elfcpp::Elf_types
<32>::Elf_Addr value
;
1797 value
= (psymval
->value(object
, 0)
1798 - target
->got_plt_section()->address());
1799 Relocate_functions
<32, false>::rel32(view
, value
);
1803 case elfcpp::R_386_GOTPC
:
1805 elfcpp::Elf_types
<32>::Elf_Addr value
;
1806 value
= target
->got_plt_section()->address();
1807 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1811 case elfcpp::R_386_COPY
:
1812 case elfcpp::R_386_GLOB_DAT
:
1813 case elfcpp::R_386_JUMP_SLOT
:
1814 case elfcpp::R_386_RELATIVE
:
1815 // These are outstanding tls relocs, which are unexpected when
1817 case elfcpp::R_386_TLS_TPOFF
:
1818 case elfcpp::R_386_TLS_DTPMOD32
:
1819 case elfcpp::R_386_TLS_DTPOFF32
:
1820 case elfcpp::R_386_TLS_TPOFF32
:
1821 case elfcpp::R_386_TLS_DESC
:
1822 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1823 _("unexpected reloc %u in object file"),
1827 // These are initial tls relocs, which are expected when
1829 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1830 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1831 case elfcpp::R_386_TLS_DESC_CALL
:
1832 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1833 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1834 case elfcpp::R_386_TLS_IE
: // Initial-exec
1835 case elfcpp::R_386_TLS_IE_32
:
1836 case elfcpp::R_386_TLS_GOTIE
:
1837 case elfcpp::R_386_TLS_LE
: // Local-exec
1838 case elfcpp::R_386_TLS_LE_32
:
1839 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1840 view
, address
, view_size
);
1843 case elfcpp::R_386_32PLT
:
1844 case elfcpp::R_386_TLS_GD_32
:
1845 case elfcpp::R_386_TLS_GD_PUSH
:
1846 case elfcpp::R_386_TLS_GD_CALL
:
1847 case elfcpp::R_386_TLS_GD_POP
:
1848 case elfcpp::R_386_TLS_LDM_32
:
1849 case elfcpp::R_386_TLS_LDM_PUSH
:
1850 case elfcpp::R_386_TLS_LDM_CALL
:
1851 case elfcpp::R_386_TLS_LDM_POP
:
1852 case elfcpp::R_386_USED_BY_INTEL_200
:
1854 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1855 _("unsupported reloc %u"),
1863 // Perform a TLS relocation.
1866 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1867 Target_i386
* target
,
1869 const elfcpp::Rel
<32, false>& rel
,
1870 unsigned int r_type
,
1871 const Sized_symbol
<32>* gsym
,
1872 const Symbol_value
<32>* psymval
,
1873 unsigned char* view
,
1874 elfcpp::Elf_types
<32>::Elf_Addr
,
1875 section_size_type view_size
)
1877 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1879 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1881 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1883 const bool is_final
=
1885 ? !parameters
->options().output_is_position_independent()
1886 : gsym
->final_value_is_known());
1887 const tls::Tls_optimization optimized_type
1888 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1891 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1892 if (optimized_type
== tls::TLSOPT_TO_LE
)
1894 gold_assert(tls_segment
!= NULL
);
1895 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1896 rel
, r_type
, value
, view
,
1902 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1903 ? GOT_TYPE_TLS_NOFFSET
1904 : GOT_TYPE_TLS_PAIR
);
1905 unsigned int got_offset
;
1908 gold_assert(gsym
->has_got_offset(got_type
));
1909 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1913 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1914 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1915 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1916 - target
->got_size());
1918 if (optimized_type
== tls::TLSOPT_TO_IE
)
1920 gold_assert(tls_segment
!= NULL
);
1921 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1922 got_offset
, view
, view_size
);
1925 else if (optimized_type
== tls::TLSOPT_NONE
)
1927 // Relocate the field with the offset of the pair of GOT
1929 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1933 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1934 _("unsupported reloc %u"),
1938 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1939 case elfcpp::R_386_TLS_DESC_CALL
:
1940 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
1941 this->fix_up_ldo(relinfo
);
1942 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
1943 if (optimized_type
== tls::TLSOPT_TO_LE
)
1945 gold_assert(tls_segment
!= NULL
);
1946 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
1947 rel
, r_type
, value
, view
,
1953 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1954 ? GOT_TYPE_TLS_NOFFSET
1955 : GOT_TYPE_TLS_DESC
);
1956 unsigned int got_offset
;
1959 gold_assert(gsym
->has_got_offset(got_type
));
1960 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
1964 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1965 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
1966 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
1967 - target
->got_size());
1969 if (optimized_type
== tls::TLSOPT_TO_IE
)
1971 gold_assert(tls_segment
!= NULL
);
1972 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
1973 got_offset
, view
, view_size
);
1976 else if (optimized_type
== tls::TLSOPT_NONE
)
1978 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
1980 // Relocate the field with the offset of the pair of GOT
1982 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1987 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1988 _("unsupported reloc %u"),
1992 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1993 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
1995 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1996 _("both SUN and GNU model "
1997 "TLS relocations"));
2000 else if (this->local_dynamic_type_
== LOCAL_DYNAMIC_NONE
)
2001 this->fix_up_ldo(relinfo
);
2002 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2003 if (optimized_type
== tls::TLSOPT_TO_LE
)
2005 gold_assert(tls_segment
!= NULL
);
2006 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2007 value
, view
, view_size
);
2010 else if (optimized_type
== tls::TLSOPT_NONE
)
2012 // Relocate the field with the offset of the GOT entry for
2013 // the module index.
2014 unsigned int got_offset
;
2015 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2016 - target
->got_size());
2017 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2020 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2021 _("unsupported reloc %u"),
2025 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2026 // This reloc can appear in debugging sections, in which case we
2027 // won't see the TLS_LDM reloc. The local_dynamic_type field
2029 if (optimized_type
== tls::TLSOPT_TO_LE
)
2031 if (this->local_dynamic_type_
!= LOCAL_DYNAMIC_NONE
)
2033 gold_assert(tls_segment
!= NULL
);
2034 value
-= tls_segment
->memsz();
2038 // We may see the LDM later.
2039 this->ldo_addrs_
.push_back(view
);
2042 Relocate_functions
<32, false>::rel32(view
, value
);
2045 case elfcpp::R_386_TLS_IE
: // Initial-exec
2046 case elfcpp::R_386_TLS_GOTIE
:
2047 case elfcpp::R_386_TLS_IE_32
:
2048 if (optimized_type
== tls::TLSOPT_TO_LE
)
2050 gold_assert(tls_segment
!= NULL
);
2051 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2052 rel
, r_type
, value
, view
,
2056 else if (optimized_type
== tls::TLSOPT_NONE
)
2058 // Relocate the field with the offset of the GOT entry for
2059 // the tp-relative offset of the symbol.
2060 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2061 ? GOT_TYPE_TLS_OFFSET
2062 : GOT_TYPE_TLS_NOFFSET
);
2063 unsigned int got_offset
;
2066 gold_assert(gsym
->has_got_offset(got_type
));
2067 got_offset
= gsym
->got_offset(got_type
);
2071 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2072 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2073 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2075 // For the R_386_TLS_IE relocation, we need to apply the
2076 // absolute address of the GOT entry.
2077 if (r_type
== elfcpp::R_386_TLS_IE
)
2078 got_offset
+= target
->got_plt_section()->address();
2079 // All GOT offsets are relative to the end of the GOT.
2080 got_offset
-= target
->got_size();
2081 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2084 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2085 _("unsupported reloc %u"),
2089 case elfcpp::R_386_TLS_LE
: // Local-exec
2090 // If we're creating a shared library, a dynamic relocation will
2091 // have been created for this location, so do not apply it now.
2092 if (!parameters
->options().shared())
2094 gold_assert(tls_segment
!= NULL
);
2095 value
-= tls_segment
->memsz();
2096 Relocate_functions
<32, false>::rel32(view
, value
);
2100 case elfcpp::R_386_TLS_LE_32
:
2101 // If we're creating a shared library, a dynamic relocation will
2102 // have been created for this location, so do not apply it now.
2103 if (!parameters
->options().shared())
2105 gold_assert(tls_segment
!= NULL
);
2106 value
= tls_segment
->memsz() - value
;
2107 Relocate_functions
<32, false>::rel32(view
, value
);
2113 // Do a relocation in which we convert a TLS General-Dynamic to a
2117 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2119 Output_segment
* tls_segment
,
2120 const elfcpp::Rel
<32, false>& rel
,
2122 elfcpp::Elf_types
<32>::Elf_Addr value
,
2123 unsigned char* view
,
2124 section_size_type view_size
)
2126 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2127 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2128 // leal foo(%reg),%eax; call ___tls_get_addr
2129 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2131 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2132 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2134 unsigned char op1
= view
[-1];
2135 unsigned char op2
= view
[-2];
2137 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2138 op2
== 0x8d || op2
== 0x04);
2139 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2145 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2146 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2147 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2148 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2149 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2153 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2154 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2155 if (rel
.get_r_offset() + 9 < view_size
2158 // There is a trailing nop. Use the size byte subl.
2159 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2164 // Use the five byte subl.
2165 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2169 value
= tls_segment
->memsz() - value
;
2170 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2172 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2174 this->skip_call_tls_get_addr_
= true;
2177 // Do a relocation in which we convert a TLS General-Dynamic to an
2181 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2184 const elfcpp::Rel
<32, false>& rel
,
2186 elfcpp::Elf_types
<32>::Elf_Addr value
,
2187 unsigned char* view
,
2188 section_size_type view_size
)
2190 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2191 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2193 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2194 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2196 unsigned char op1
= view
[-1];
2197 unsigned char op2
= view
[-2];
2199 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2200 op2
== 0x8d || op2
== 0x04);
2201 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2205 // FIXME: For now, support only the first (SIB) form.
2206 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2210 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2211 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2212 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2213 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2214 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2218 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2219 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2220 if (rel
.get_r_offset() + 9 < view_size
2223 // FIXME: This is not the right instruction sequence.
2224 // There is a trailing nop. Use the size byte subl.
2225 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2230 // FIXME: This is not the right instruction sequence.
2231 // Use the five byte subl.
2232 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2236 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2238 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2240 this->skip_call_tls_get_addr_
= true;
2243 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2244 // General-Dynamic to a Local-Exec.
2247 Target_i386::Relocate::tls_desc_gd_to_le(
2248 const Relocate_info
<32, false>* relinfo
,
2250 Output_segment
* tls_segment
,
2251 const elfcpp::Rel
<32, false>& rel
,
2252 unsigned int r_type
,
2253 elfcpp::Elf_types
<32>::Elf_Addr value
,
2254 unsigned char* view
,
2255 section_size_type view_size
)
2257 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2259 // leal foo@TLSDESC(%ebx), %eax
2260 // ==> leal foo@NTPOFF, %eax
2261 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2262 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2263 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2264 view
[-2] == 0x8d && view
[-1] == 0x83);
2266 value
-= tls_segment
->memsz();
2267 Relocate_functions
<32, false>::rel32(view
, value
);
2271 // call *foo@TLSCALL(%eax)
2273 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2274 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2275 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2276 view
[0] == 0xff && view
[1] == 0x10);
2282 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2283 // General-Dynamic to an Initial-Exec.
2286 Target_i386::Relocate::tls_desc_gd_to_ie(
2287 const Relocate_info
<32, false>* relinfo
,
2290 const elfcpp::Rel
<32, false>& rel
,
2291 unsigned int r_type
,
2292 elfcpp::Elf_types
<32>::Elf_Addr value
,
2293 unsigned char* view
,
2294 section_size_type view_size
)
2296 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2298 // leal foo@TLSDESC(%ebx), %eax
2299 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2300 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2301 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2302 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2303 view
[-2] == 0x8d && view
[-1] == 0x83);
2305 Relocate_functions
<32, false>::rel32(view
, value
);
2309 // call *foo@TLSCALL(%eax)
2311 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2312 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2313 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2314 view
[0] == 0xff && view
[1] == 0x10);
2320 // Do a relocation in which we convert a TLS Local-Dynamic to a
2324 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2327 const elfcpp::Rel
<32, false>& rel
,
2329 elfcpp::Elf_types
<32>::Elf_Addr
,
2330 unsigned char* view
,
2331 section_size_type view_size
)
2333 // leal foo(%reg), %eax; call ___tls_get_addr
2334 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2336 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2337 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2339 // FIXME: Does this test really always pass?
2340 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2341 view
[-2] == 0x8d && view
[-1] == 0x83);
2343 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2345 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2347 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2349 this->skip_call_tls_get_addr_
= true;
2352 // Do a relocation in which we convert a TLS Initial-Exec to a
2356 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2358 Output_segment
* tls_segment
,
2359 const elfcpp::Rel
<32, false>& rel
,
2360 unsigned int r_type
,
2361 elfcpp::Elf_types
<32>::Elf_Addr value
,
2362 unsigned char* view
,
2363 section_size_type view_size
)
2365 // We have to actually change the instructions, which means that we
2366 // need to examine the opcodes to figure out which instruction we
2368 if (r_type
== elfcpp::R_386_TLS_IE
)
2370 // movl %gs:XX,%eax ==> movl $YY,%eax
2371 // movl %gs:XX,%reg ==> movl $YY,%reg
2372 // addl %gs:XX,%reg ==> addl $YY,%reg
2373 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2374 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2376 unsigned char op1
= view
[-1];
2379 // movl XX,%eax ==> movl $YY,%eax
2384 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2386 unsigned char op2
= view
[-2];
2389 // movl XX,%reg ==> movl $YY,%reg
2390 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2391 (op1
& 0xc7) == 0x05);
2393 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2395 else if (op2
== 0x03)
2397 // addl XX,%reg ==> addl $YY,%reg
2398 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2399 (op1
& 0xc7) == 0x05);
2401 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2404 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2409 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2410 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2411 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2412 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2413 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2415 unsigned char op1
= view
[-1];
2416 unsigned char op2
= view
[-2];
2417 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2418 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2421 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2423 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2425 else if (op2
== 0x2b)
2427 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2429 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2431 else if (op2
== 0x03)
2433 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2435 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2438 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2441 value
= tls_segment
->memsz() - value
;
2442 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2445 Relocate_functions
<32, false>::rel32(view
, value
);
2448 // If we see an LDM reloc after we handled any LDO_32 relocs, fix up
2449 // the LDO_32 relocs.
2452 Target_i386::Relocate::fix_up_ldo(const Relocate_info
<32, false>* relinfo
)
2454 if (this->ldo_addrs_
.empty())
2456 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
2457 gold_assert(tls_segment
!= NULL
);
2458 elfcpp::Elf_types
<32>::Elf_Addr value
= - tls_segment
->memsz();
2459 for (std::vector
<unsigned char*>::const_iterator p
= this->ldo_addrs_
.begin();
2460 p
!= this->ldo_addrs_
.end();
2462 Relocate_functions
<32, false>::rel32(*p
, value
);
2463 this->ldo_addrs_
.clear();
2466 // Relocate section data.
2469 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2470 unsigned int sh_type
,
2471 const unsigned char* prelocs
,
2473 Output_section
* output_section
,
2474 bool needs_special_offset_handling
,
2475 unsigned char* view
,
2476 elfcpp::Elf_types
<32>::Elf_Addr address
,
2477 section_size_type view_size
,
2478 const Reloc_symbol_changes
* reloc_symbol_changes
)
2480 gold_assert(sh_type
== elfcpp::SHT_REL
);
2482 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2483 Target_i386::Relocate
>(
2489 needs_special_offset_handling
,
2493 reloc_symbol_changes
);
2496 // Return the size of a relocation while scanning during a relocatable
2500 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2501 unsigned int r_type
,
2506 case elfcpp::R_386_NONE
:
2507 case elfcpp::R_386_GNU_VTINHERIT
:
2508 case elfcpp::R_386_GNU_VTENTRY
:
2509 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2510 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2511 case elfcpp::R_386_TLS_DESC_CALL
:
2512 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2513 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2514 case elfcpp::R_386_TLS_IE
: // Initial-exec
2515 case elfcpp::R_386_TLS_IE_32
:
2516 case elfcpp::R_386_TLS_GOTIE
:
2517 case elfcpp::R_386_TLS_LE
: // Local-exec
2518 case elfcpp::R_386_TLS_LE_32
:
2521 case elfcpp::R_386_32
:
2522 case elfcpp::R_386_PC32
:
2523 case elfcpp::R_386_GOT32
:
2524 case elfcpp::R_386_PLT32
:
2525 case elfcpp::R_386_GOTOFF
:
2526 case elfcpp::R_386_GOTPC
:
2529 case elfcpp::R_386_16
:
2530 case elfcpp::R_386_PC16
:
2533 case elfcpp::R_386_8
:
2534 case elfcpp::R_386_PC8
:
2537 // These are relocations which should only be seen by the
2538 // dynamic linker, and should never be seen here.
2539 case elfcpp::R_386_COPY
:
2540 case elfcpp::R_386_GLOB_DAT
:
2541 case elfcpp::R_386_JUMP_SLOT
:
2542 case elfcpp::R_386_RELATIVE
:
2543 case elfcpp::R_386_TLS_TPOFF
:
2544 case elfcpp::R_386_TLS_DTPMOD32
:
2545 case elfcpp::R_386_TLS_DTPOFF32
:
2546 case elfcpp::R_386_TLS_TPOFF32
:
2547 case elfcpp::R_386_TLS_DESC
:
2548 object
->error(_("unexpected reloc %u in object file"), r_type
);
2551 case elfcpp::R_386_32PLT
:
2552 case elfcpp::R_386_TLS_GD_32
:
2553 case elfcpp::R_386_TLS_GD_PUSH
:
2554 case elfcpp::R_386_TLS_GD_CALL
:
2555 case elfcpp::R_386_TLS_GD_POP
:
2556 case elfcpp::R_386_TLS_LDM_32
:
2557 case elfcpp::R_386_TLS_LDM_PUSH
:
2558 case elfcpp::R_386_TLS_LDM_CALL
:
2559 case elfcpp::R_386_TLS_LDM_POP
:
2560 case elfcpp::R_386_USED_BY_INTEL_200
:
2562 object
->error(_("unsupported reloc %u in object file"), r_type
);
2567 // Scan the relocs during a relocatable link.
2570 Target_i386::scan_relocatable_relocs(const General_options
& options
,
2571 Symbol_table
* symtab
,
2573 Sized_relobj
<32, false>* object
,
2574 unsigned int data_shndx
,
2575 unsigned int sh_type
,
2576 const unsigned char* prelocs
,
2578 Output_section
* output_section
,
2579 bool needs_special_offset_handling
,
2580 size_t local_symbol_count
,
2581 const unsigned char* plocal_symbols
,
2582 Relocatable_relocs
* rr
)
2584 gold_assert(sh_type
== elfcpp::SHT_REL
);
2586 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2587 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2589 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2590 Scan_relocatable_relocs
>(
2599 needs_special_offset_handling
,
2605 // Relocate a section during a relocatable link.
2608 Target_i386::relocate_for_relocatable(
2609 const Relocate_info
<32, false>* relinfo
,
2610 unsigned int sh_type
,
2611 const unsigned char* prelocs
,
2613 Output_section
* output_section
,
2614 off_t offset_in_output_section
,
2615 const Relocatable_relocs
* rr
,
2616 unsigned char* view
,
2617 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2618 section_size_type view_size
,
2619 unsigned char* reloc_view
,
2620 section_size_type reloc_view_size
)
2622 gold_assert(sh_type
== elfcpp::SHT_REL
);
2624 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2629 offset_in_output_section
,
2638 // Return the value to use for a dynamic which requires special
2639 // treatment. This is how we support equality comparisons of function
2640 // pointers across shared library boundaries, as described in the
2641 // processor specific ABI supplement.
2644 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2646 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2647 return this->plt_section()->address() + gsym
->plt_offset();
2650 // Return a string used to fill a code section with nops to take up
2651 // the specified length.
2654 Target_i386::do_code_fill(section_size_type length
) const
2658 // Build a jmp instruction to skip over the bytes.
2659 unsigned char jmp
[5];
2661 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2662 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2663 + std::string(length
- 5, '\0'));
2666 // Nop sequences of various lengths.
2667 const char nop1
[1] = { 0x90 }; // nop
2668 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2669 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2670 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2671 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2672 0x00 }; // leal 0(%esi,1),%esi
2673 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2675 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2677 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2678 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2679 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2680 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2682 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2683 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2685 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2686 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2688 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2689 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2690 0x00, 0x00, 0x00, 0x00 };
2691 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2692 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2693 0x27, 0x00, 0x00, 0x00,
2695 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2696 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2697 0xbc, 0x27, 0x00, 0x00,
2699 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2700 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2701 0x90, 0x90, 0x90, 0x90,
2704 const char* nops
[16] = {
2706 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2707 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2710 return std::string(nops
[length
], length
);
2713 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2714 // compiled with -fstack-split. The function calls non-stack-split
2715 // code. We have to change the function so that it always ensures
2716 // that it has enough stack space to run some random function.
2719 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
2720 section_offset_type fnoffset
,
2721 section_size_type fnsize
,
2722 unsigned char* view
,
2723 section_size_type view_size
,
2725 std::string
* to
) const
2727 // The function starts with a comparison of the stack pointer and a
2728 // field in the TCB. This is followed by a jump.
2731 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
2734 // We will call __morestack if the carry flag is set after this
2735 // comparison. We turn the comparison into an stc instruction
2737 view
[fnoffset
] = '\xf9';
2738 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
2740 // lea NN(%esp),%ecx
2741 else if (this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
2744 // This is loading an offset from the stack pointer for a
2745 // comparison. The offset is negative, so we decrease the
2746 // offset by the amount of space we need for the stack. This
2747 // means we will avoid calling __morestack if there happens to
2748 // be plenty of space on the stack already.
2749 unsigned char* pval
= view
+ fnoffset
+ 3;
2750 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
2751 val
-= parameters
->options().split_stack_adjust_size();
2752 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
2756 if (!object
->has_no_split_stack())
2757 object
->error(_("failed to match split-stack sequence at "
2758 "section %u offset %0zx"),
2763 // We have to change the function so that it calls
2764 // __morestack_non_split instead of __morestack. The former will
2765 // allocate additional stack space.
2766 *from
= "__morestack";
2767 *to
= "__morestack_non_split";
2770 // The selector for i386 object files.
2772 class Target_selector_i386
: public Target_selector_freebsd
2775 Target_selector_i386()
2776 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
2777 "elf32-i386", "elf32-i386-freebsd")
2781 do_instantiate_target()
2782 { return new Target_i386(); }
2785 Target_selector_i386 target_selector_i386
;
2787 } // End anonymous namespace.