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
), global_offset_table_(NULL
),
63 rel_dyn_(NULL
), copy_relocs_(elfcpp::R_386_COPY
), dynbss_(NULL
),
64 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
68 can_check_for_function_pointers() const
71 // Process the relocations to determine unreferenced sections for
72 // garbage collection.
74 gc_process_relocs(Symbol_table
* symtab
,
76 Sized_relobj
<32, false>* object
,
77 unsigned int data_shndx
,
79 const unsigned char* prelocs
,
81 Output_section
* output_section
,
82 bool needs_special_offset_handling
,
83 size_t local_symbol_count
,
84 const unsigned char* plocal_symbols
);
86 // Scan the relocations to look for symbol adjustments.
88 scan_relocs(Symbol_table
* symtab
,
90 Sized_relobj
<32, false>* object
,
91 unsigned int data_shndx
,
93 const unsigned char* prelocs
,
95 Output_section
* output_section
,
96 bool needs_special_offset_handling
,
97 size_t local_symbol_count
,
98 const unsigned char* plocal_symbols
);
100 // Finalize the sections.
102 do_finalize_sections(Layout
*, const Input_objects
*, Symbol_table
*);
104 // Return the value to use for a dynamic which requires special
107 do_dynsym_value(const Symbol
*) const;
109 // Relocate a section.
111 relocate_section(const Relocate_info
<32, false>*,
112 unsigned int sh_type
,
113 const unsigned char* prelocs
,
115 Output_section
* output_section
,
116 bool needs_special_offset_handling
,
118 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
119 section_size_type view_size
,
120 const Reloc_symbol_changes
*);
122 // Scan the relocs during a relocatable link.
124 scan_relocatable_relocs(Symbol_table
* symtab
,
126 Sized_relobj
<32, false>* object
,
127 unsigned int data_shndx
,
128 unsigned int sh_type
,
129 const unsigned char* prelocs
,
131 Output_section
* output_section
,
132 bool needs_special_offset_handling
,
133 size_t local_symbol_count
,
134 const unsigned char* plocal_symbols
,
135 Relocatable_relocs
*);
137 // Relocate a section during a relocatable link.
139 relocate_for_relocatable(const Relocate_info
<32, false>*,
140 unsigned int sh_type
,
141 const unsigned char* prelocs
,
143 Output_section
* output_section
,
144 off_t offset_in_output_section
,
145 const Relocatable_relocs
*,
147 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
148 section_size_type view_size
,
149 unsigned char* reloc_view
,
150 section_size_type reloc_view_size
);
152 // Return a string used to fill a code section with nops.
154 do_code_fill(section_size_type length
) const;
156 // Return whether SYM is defined by the ABI.
158 do_is_defined_by_abi(const Symbol
* sym
) const
159 { return strcmp(sym
->name(), "___tls_get_addr") == 0; }
161 // Return whether a symbol name implies a local label. The UnixWare
162 // 2.1 cc generates temporary symbols that start with .X, so we
163 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
164 // If so, we should move the .X recognition into
165 // Target::do_is_local_label_name.
167 do_is_local_label_name(const char* name
) const
169 if (name
[0] == '.' && name
[1] == 'X')
171 return Target::do_is_local_label_name(name
);
174 // Adjust -fstack-split code which calls non-stack-split code.
176 do_calls_non_split(Relobj
* object
, unsigned int shndx
,
177 section_offset_type fnoffset
, section_size_type fnsize
,
178 unsigned char* view
, section_size_type view_size
,
179 std::string
* from
, std::string
* to
) const;
181 // Return the size of the GOT section.
185 gold_assert(this->got_
!= NULL
);
186 return this->got_
->data_size();
190 // The class which scans relocations.
194 local(Symbol_table
* symtab
, 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(Symbol_table
* symtab
, Layout
* layout
, Target_i386
* target
,
203 Sized_relobj
<32, false>* object
,
204 unsigned int data_shndx
,
205 Output_section
* output_section
,
206 const elfcpp::Rel
<32, false>& reloc
, unsigned int r_type
,
210 local_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
212 Sized_relobj
<32, false>* object
,
213 unsigned int data_shndx
,
214 Output_section
* output_section
,
215 const elfcpp::Rel
<32, false>& reloc
,
217 const elfcpp::Sym
<32, false>& lsym
);
220 global_reloc_may_be_function_pointer(Symbol_table
* symtab
, Layout
* layout
,
222 Sized_relobj
<32, false>* object
,
223 unsigned int data_shndx
,
224 Output_section
* output_section
,
225 const elfcpp::Rel
<32, false>& reloc
,
230 possible_function_pointer_reloc(unsigned int r_type
);
233 unsupported_reloc_local(Sized_relobj
<32, false>*, unsigned int r_type
);
236 unsupported_reloc_global(Sized_relobj
<32, false>*, unsigned int r_type
,
240 // The class which implements relocation.
245 : skip_call_tls_get_addr_(false),
246 local_dynamic_type_(LOCAL_DYNAMIC_NONE
)
251 if (this->skip_call_tls_get_addr_
)
253 // FIXME: This needs to specify the location somehow.
254 gold_error(_("missing expected TLS relocation"));
258 // Return whether the static relocation needs to be applied.
260 should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
263 Output_section
* output_section
);
265 // Do a relocation. Return false if the caller should not issue
266 // any warnings about this relocation.
268 relocate(const Relocate_info
<32, false>*, Target_i386
*, Output_section
*,
269 size_t relnum
, const elfcpp::Rel
<32, false>&,
270 unsigned int r_type
, const Sized_symbol
<32>*,
271 const Symbol_value
<32>*,
272 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
276 // Do a TLS relocation.
278 relocate_tls(const Relocate_info
<32, false>*, Target_i386
* target
,
279 size_t relnum
, const elfcpp::Rel
<32, false>&,
280 unsigned int r_type
, const Sized_symbol
<32>*,
281 const Symbol_value
<32>*,
282 unsigned char*, elfcpp::Elf_types
<32>::Elf_Addr
,
285 // Do a TLS General-Dynamic to Initial-Exec transition.
287 tls_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
288 Output_segment
* tls_segment
,
289 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
290 elfcpp::Elf_types
<32>::Elf_Addr value
,
292 section_size_type view_size
);
294 // Do a TLS General-Dynamic to Local-Exec transition.
296 tls_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
297 Output_segment
* tls_segment
,
298 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
299 elfcpp::Elf_types
<32>::Elf_Addr value
,
301 section_size_type view_size
);
303 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
306 tls_desc_gd_to_ie(const Relocate_info
<32, false>*, size_t relnum
,
307 Output_segment
* tls_segment
,
308 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
309 elfcpp::Elf_types
<32>::Elf_Addr value
,
311 section_size_type view_size
);
313 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
316 tls_desc_gd_to_le(const Relocate_info
<32, false>*, size_t relnum
,
317 Output_segment
* tls_segment
,
318 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
319 elfcpp::Elf_types
<32>::Elf_Addr value
,
321 section_size_type view_size
);
323 // Do a TLS Local-Dynamic to Local-Exec transition.
325 tls_ld_to_le(const Relocate_info
<32, false>*, size_t relnum
,
326 Output_segment
* tls_segment
,
327 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
328 elfcpp::Elf_types
<32>::Elf_Addr value
,
330 section_size_type view_size
);
332 // Do a TLS Initial-Exec to Local-Exec transition.
334 tls_ie_to_le(const Relocate_info
<32, false>*, size_t relnum
,
335 Output_segment
* tls_segment
,
336 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
337 elfcpp::Elf_types
<32>::Elf_Addr value
,
339 section_size_type view_size
);
341 // We need to keep track of which type of local dynamic relocation
342 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
343 enum Local_dynamic_type
350 // This is set if we should skip the next reloc, which should be a
351 // PLT32 reloc against ___tls_get_addr.
352 bool skip_call_tls_get_addr_
;
353 // The type of local dynamic relocation we have seen in the section
354 // being relocated, if any.
355 Local_dynamic_type local_dynamic_type_
;
358 // A class which returns the size required for a relocation type,
359 // used while scanning relocs during a relocatable link.
360 class Relocatable_size_for_reloc
364 get_size_for_reloc(unsigned int, Relobj
*);
367 // Adjust TLS relocation type based on the options and whether this
368 // is a local symbol.
369 static tls::Tls_optimization
370 optimize_tls_reloc(bool is_final
, int r_type
);
372 // Get the GOT section, creating it if necessary.
373 Output_data_got
<32, false>*
374 got_section(Symbol_table
*, Layout
*);
376 // Get the GOT PLT section.
378 got_plt_section() const
380 gold_assert(this->got_plt_
!= NULL
);
381 return this->got_plt_
;
384 // Create a PLT entry for a global symbol.
386 make_plt_entry(Symbol_table
*, Layout
*, Symbol
*);
388 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
390 define_tls_base_symbol(Symbol_table
*, Layout
*);
392 // Create a GOT entry for the TLS module index.
394 got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
395 Sized_relobj
<32, false>* object
);
397 // Get the PLT section.
398 Output_data_plt_i386
*
401 gold_assert(this->plt_
!= NULL
);
405 // Get the dynamic reloc section, creating it if necessary.
407 rel_dyn_section(Layout
*);
409 // Get the section to use for TLS_DESC relocations.
411 rel_tls_desc_section(Layout
*) const;
413 // Add a potential copy relocation.
415 copy_reloc(Symbol_table
* symtab
, Layout
* layout
,
416 Sized_relobj
<32, false>* object
,
417 unsigned int shndx
, Output_section
* output_section
,
418 Symbol
* sym
, const elfcpp::Rel
<32, false>& reloc
)
420 this->copy_relocs_
.copy_reloc(symtab
, layout
,
421 symtab
->get_sized_symbol
<32>(sym
),
422 object
, shndx
, output_section
, reloc
,
423 this->rel_dyn_section(layout
));
426 // Information about this specific target which we pass to the
427 // general Target structure.
428 static const Target::Target_info i386_info
;
430 // The types of GOT entries needed for this platform.
433 GOT_TYPE_STANDARD
= 0, // GOT entry for a regular symbol
434 GOT_TYPE_TLS_NOFFSET
= 1, // GOT entry for negative TLS offset
435 GOT_TYPE_TLS_OFFSET
= 2, // GOT entry for positive TLS offset
436 GOT_TYPE_TLS_PAIR
= 3, // GOT entry for TLS module/offset pair
437 GOT_TYPE_TLS_DESC
= 4 // GOT entry for TLS_DESC pair
441 Output_data_got
<32, false>* got_
;
443 Output_data_plt_i386
* plt_
;
444 // The GOT PLT section.
445 Output_data_space
* got_plt_
;
446 // The _GLOBAL_OFFSET_TABLE_ symbol.
447 Symbol
* global_offset_table_
;
448 // The dynamic reloc section.
449 Reloc_section
* rel_dyn_
;
450 // Relocs saved to avoid a COPY reloc.
451 Copy_relocs
<elfcpp::SHT_REL
, 32, false> copy_relocs_
;
452 // Space for variables copied with a COPY reloc.
453 Output_data_space
* dynbss_
;
454 // Offset of the GOT entry for the TLS module index.
455 unsigned int got_mod_index_offset_
;
456 // True if the _TLS_MODULE_BASE_ symbol has been defined.
457 bool tls_base_symbol_defined_
;
460 const Target::Target_info
Target_i386::i386_info
=
463 false, // is_big_endian
464 elfcpp::EM_386
, // machine_code
465 false, // has_make_symbol
466 false, // has_resolve
467 true, // has_code_fill
468 true, // is_default_stack_executable
470 "/usr/lib/libc.so.1", // dynamic_linker
471 0x08048000, // default_text_segment_address
472 0x1000, // abi_pagesize (overridable by -z max-page-size)
473 0x1000, // common_pagesize (overridable by -z common-page-size)
474 elfcpp::SHN_UNDEF
, // small_common_shndx
475 elfcpp::SHN_UNDEF
, // large_common_shndx
476 0, // small_common_section_flags
477 0, // large_common_section_flags
478 NULL
, // attributes_section
479 NULL
// attributes_vendor
482 // Get the GOT section, creating it if necessary.
484 Output_data_got
<32, false>*
485 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
487 if (this->got_
== NULL
)
489 gold_assert(symtab
!= NULL
&& layout
!= NULL
);
491 this->got_
= new Output_data_got
<32, false>();
494 os
= layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
496 | elfcpp::SHF_WRITE
),
497 this->got_
, false, true, true,
500 this->got_plt_
= new Output_data_space(4, "** GOT PLT");
501 os
= layout
->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS
,
503 | elfcpp::SHF_WRITE
),
504 this->got_plt_
, false, false, false,
507 // The first three entries are reserved.
508 this->got_plt_
->set_current_data_size(3 * 4);
510 // Those bytes can go into the relro segment.
511 layout
->increase_relro(3 * 4);
513 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
514 this->global_offset_table_
=
515 symtab
->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL
,
516 Symbol_table::PREDEFINED
,
518 0, 0, elfcpp::STT_OBJECT
,
520 elfcpp::STV_HIDDEN
, 0,
527 // Get the dynamic reloc section, creating it if necessary.
529 Target_i386::Reloc_section
*
530 Target_i386::rel_dyn_section(Layout
* layout
)
532 if (this->rel_dyn_
== NULL
)
534 gold_assert(layout
!= NULL
);
535 this->rel_dyn_
= new Reloc_section(parameters
->options().combreloc());
536 layout
->add_output_section_data(".rel.dyn", elfcpp::SHT_REL
,
537 elfcpp::SHF_ALLOC
, this->rel_dyn_
, true,
538 false, false, false);
540 return this->rel_dyn_
;
543 // A class to handle the PLT data.
545 class Output_data_plt_i386
: public Output_section_data
548 typedef Output_data_reloc
<elfcpp::SHT_REL
, true, 32, false> Reloc_section
;
550 Output_data_plt_i386(Layout
*, Output_data_space
*);
552 // Add an entry to the PLT.
554 add_entry(Symbol
* gsym
);
556 // Return the .rel.plt section data.
559 { return this->rel_
; }
561 // Return where the TLS_DESC relocations should go.
563 rel_tls_desc(Layout
*);
567 do_adjust_output_section(Output_section
* os
);
569 // Write to a map file.
571 do_print_to_mapfile(Mapfile
* mapfile
) const
572 { mapfile
->print_output_data(this, _("** PLT")); }
575 // The size of an entry in the PLT.
576 static const int plt_entry_size
= 16;
578 // The first entry in the PLT for an executable.
579 static unsigned char exec_first_plt_entry
[plt_entry_size
];
581 // The first entry in the PLT for a shared object.
582 static unsigned char dyn_first_plt_entry
[plt_entry_size
];
584 // Other entries in the PLT for an executable.
585 static unsigned char exec_plt_entry
[plt_entry_size
];
587 // Other entries in the PLT for a shared object.
588 static unsigned char dyn_plt_entry
[plt_entry_size
];
590 // Set the final size.
592 set_final_data_size()
593 { this->set_data_size((this->count_
+ 1) * plt_entry_size
); }
595 // Write out the PLT data.
597 do_write(Output_file
*);
599 // The reloc section.
601 // The TLS_DESC relocations, if necessary. These must follow the
602 // regular PLT relocs.
603 Reloc_section
* tls_desc_rel_
;
604 // The .got.plt section.
605 Output_data_space
* got_plt_
;
606 // The number of PLT entries.
610 // Create the PLT section. The ordinary .got section is an argument,
611 // since we need to refer to the start. We also create our own .got
612 // section just for PLT entries.
614 Output_data_plt_i386::Output_data_plt_i386(Layout
* layout
,
615 Output_data_space
* got_plt
)
616 : Output_section_data(4), tls_desc_rel_(NULL
), got_plt_(got_plt
), count_(0)
618 this->rel_
= new Reloc_section(false);
619 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
620 elfcpp::SHF_ALLOC
, this->rel_
, true,
621 false, false, false);
625 Output_data_plt_i386::do_adjust_output_section(Output_section
* os
)
627 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
628 // linker, and so do we.
632 // Add an entry to the PLT.
635 Output_data_plt_i386::add_entry(Symbol
* gsym
)
637 gold_assert(!gsym
->has_plt_offset());
639 // Note that when setting the PLT offset we skip the initial
640 // reserved PLT entry.
641 gsym
->set_plt_offset((this->count_
+ 1) * plt_entry_size
);
645 section_offset_type got_offset
= this->got_plt_
->current_data_size();
647 // Every PLT entry needs a GOT entry which points back to the PLT
648 // entry (this will be changed by the dynamic linker, normally
649 // lazily when the function is called).
650 this->got_plt_
->set_current_data_size(got_offset
+ 4);
652 // Every PLT entry needs a reloc.
653 gsym
->set_needs_dynsym_entry();
654 this->rel_
->add_global(gsym
, elfcpp::R_386_JUMP_SLOT
, this->got_plt_
,
657 // Note that we don't need to save the symbol. The contents of the
658 // PLT are independent of which symbols are used. The symbols only
659 // appear in the relocations.
662 // Return where the TLS_DESC relocations should go, creating it if
663 // necessary. These follow the JUMP_SLOT relocations.
665 Output_data_plt_i386::Reloc_section
*
666 Output_data_plt_i386::rel_tls_desc(Layout
* layout
)
668 if (this->tls_desc_rel_
== NULL
)
670 this->tls_desc_rel_
= new Reloc_section(false);
671 layout
->add_output_section_data(".rel.plt", elfcpp::SHT_REL
,
672 elfcpp::SHF_ALLOC
, this->tls_desc_rel_
,
673 true, false, false, false);
674 gold_assert(this->tls_desc_rel_
->output_section() ==
675 this->rel_
->output_section());
677 return this->tls_desc_rel_
;
680 // The first entry in the PLT for an executable.
682 unsigned char Output_data_plt_i386::exec_first_plt_entry
[plt_entry_size
] =
684 0xff, 0x35, // pushl contents of memory address
685 0, 0, 0, 0, // replaced with address of .got + 4
686 0xff, 0x25, // jmp indirect
687 0, 0, 0, 0, // replaced with address of .got + 8
691 // The first entry in the PLT for a shared object.
693 unsigned char Output_data_plt_i386::dyn_first_plt_entry
[plt_entry_size
] =
695 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
696 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
700 // Subsequent entries in the PLT for an executable.
702 unsigned char Output_data_plt_i386::exec_plt_entry
[plt_entry_size
] =
704 0xff, 0x25, // jmp indirect
705 0, 0, 0, 0, // replaced with address of symbol in .got
706 0x68, // pushl immediate
707 0, 0, 0, 0, // replaced with offset into relocation table
708 0xe9, // jmp relative
709 0, 0, 0, 0 // replaced with offset to start of .plt
712 // Subsequent entries in the PLT for a shared object.
714 unsigned char Output_data_plt_i386::dyn_plt_entry
[plt_entry_size
] =
716 0xff, 0xa3, // jmp *offset(%ebx)
717 0, 0, 0, 0, // replaced with offset of symbol in .got
718 0x68, // pushl immediate
719 0, 0, 0, 0, // replaced with offset into relocation table
720 0xe9, // jmp relative
721 0, 0, 0, 0 // replaced with offset to start of .plt
724 // Write out the PLT. This uses the hand-coded instructions above,
725 // and adjusts them as needed. This is all specified by the i386 ELF
726 // Processor Supplement.
729 Output_data_plt_i386::do_write(Output_file
* of
)
731 const off_t offset
= this->offset();
732 const section_size_type oview_size
=
733 convert_to_section_size_type(this->data_size());
734 unsigned char* const oview
= of
->get_output_view(offset
, oview_size
);
736 const off_t got_file_offset
= this->got_plt_
->offset();
737 const section_size_type got_size
=
738 convert_to_section_size_type(this->got_plt_
->data_size());
739 unsigned char* const got_view
= of
->get_output_view(got_file_offset
,
742 unsigned char* pov
= oview
;
744 elfcpp::Elf_types
<32>::Elf_Addr plt_address
= this->address();
745 elfcpp::Elf_types
<32>::Elf_Addr got_address
= this->got_plt_
->address();
747 if (parameters
->options().output_is_position_independent())
748 memcpy(pov
, dyn_first_plt_entry
, plt_entry_size
);
751 memcpy(pov
, exec_first_plt_entry
, plt_entry_size
);
752 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_address
+ 4);
753 elfcpp::Swap
<32, false>::writeval(pov
+ 8, got_address
+ 8);
755 pov
+= plt_entry_size
;
757 unsigned char* got_pov
= got_view
;
759 memset(got_pov
, 0, 12);
762 const int rel_size
= elfcpp::Elf_sizes
<32>::rel_size
;
764 unsigned int plt_offset
= plt_entry_size
;
765 unsigned int plt_rel_offset
= 0;
766 unsigned int got_offset
= 12;
767 const unsigned int count
= this->count_
;
768 for (unsigned int i
= 0;
771 pov
+= plt_entry_size
,
773 plt_offset
+= plt_entry_size
,
774 plt_rel_offset
+= rel_size
,
777 // Set and adjust the PLT entry itself.
779 if (parameters
->options().output_is_position_independent())
781 memcpy(pov
, dyn_plt_entry
, plt_entry_size
);
782 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2, got_offset
);
786 memcpy(pov
, exec_plt_entry
, plt_entry_size
);
787 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 2,
792 elfcpp::Swap_unaligned
<32, false>::writeval(pov
+ 7, plt_rel_offset
);
793 elfcpp::Swap
<32, false>::writeval(pov
+ 12,
794 - (plt_offset
+ plt_entry_size
));
796 // Set the entry in the GOT.
797 elfcpp::Swap
<32, false>::writeval(got_pov
, plt_address
+ plt_offset
+ 6);
800 gold_assert(static_cast<section_size_type
>(pov
- oview
) == oview_size
);
801 gold_assert(static_cast<section_size_type
>(got_pov
- got_view
) == got_size
);
803 of
->write_output_view(offset
, oview_size
, oview
);
804 of
->write_output_view(got_file_offset
, got_size
, got_view
);
807 // Create a PLT entry for a global symbol.
810 Target_i386::make_plt_entry(Symbol_table
* symtab
, Layout
* layout
, Symbol
* gsym
)
812 if (gsym
->has_plt_offset())
815 if (this->plt_
== NULL
)
817 // Create the GOT sections first.
818 this->got_section(symtab
, layout
);
820 this->plt_
= new Output_data_plt_i386(layout
, this->got_plt_
);
821 layout
->add_output_section_data(".plt", elfcpp::SHT_PROGBITS
,
823 | elfcpp::SHF_EXECINSTR
),
824 this->plt_
, false, false, false, false);
827 this->plt_
->add_entry(gsym
);
830 // Get the section to use for TLS_DESC relocations.
832 Target_i386::Reloc_section
*
833 Target_i386::rel_tls_desc_section(Layout
* layout
) const
835 return this->plt_section()->rel_tls_desc(layout
);
838 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
841 Target_i386::define_tls_base_symbol(Symbol_table
* symtab
, Layout
* layout
)
843 if (this->tls_base_symbol_defined_
)
846 Output_segment
* tls_segment
= layout
->tls_segment();
847 if (tls_segment
!= NULL
)
849 bool is_exec
= parameters
->options().output_is_executable();
850 symtab
->define_in_output_segment("_TLS_MODULE_BASE_", NULL
,
851 Symbol_table::PREDEFINED
,
855 elfcpp::STV_HIDDEN
, 0,
857 ? Symbol::SEGMENT_END
858 : Symbol::SEGMENT_START
),
861 this->tls_base_symbol_defined_
= true;
864 // Create a GOT entry for the TLS module index.
867 Target_i386::got_mod_index_entry(Symbol_table
* symtab
, Layout
* layout
,
868 Sized_relobj
<32, false>* object
)
870 if (this->got_mod_index_offset_
== -1U)
872 gold_assert(symtab
!= NULL
&& layout
!= NULL
&& object
!= NULL
);
873 Reloc_section
* rel_dyn
= this->rel_dyn_section(layout
);
874 Output_data_got
<32, false>* got
= this->got_section(symtab
, layout
);
875 unsigned int got_offset
= got
->add_constant(0);
876 rel_dyn
->add_local(object
, 0, elfcpp::R_386_TLS_DTPMOD32
, got
,
878 got
->add_constant(0);
879 this->got_mod_index_offset_
= got_offset
;
881 return this->got_mod_index_offset_
;
884 // Optimize the TLS relocation type based on what we know about the
885 // symbol. IS_FINAL is true if the final address of this symbol is
886 // known at link time.
888 tls::Tls_optimization
889 Target_i386::optimize_tls_reloc(bool is_final
, int r_type
)
891 // If we are generating a shared library, then we can't do anything
893 if (parameters
->options().shared())
894 return tls::TLSOPT_NONE
;
898 case elfcpp::R_386_TLS_GD
:
899 case elfcpp::R_386_TLS_GOTDESC
:
900 case elfcpp::R_386_TLS_DESC_CALL
:
901 // These are General-Dynamic which permits fully general TLS
902 // access. Since we know that we are generating an executable,
903 // we can convert this to Initial-Exec. If we also know that
904 // this is a local symbol, we can further switch to Local-Exec.
906 return tls::TLSOPT_TO_LE
;
907 return tls::TLSOPT_TO_IE
;
909 case elfcpp::R_386_TLS_LDM
:
910 // This is Local-Dynamic, which refers to a local symbol in the
911 // dynamic TLS block. Since we know that we generating an
912 // executable, we can switch to Local-Exec.
913 return tls::TLSOPT_TO_LE
;
915 case elfcpp::R_386_TLS_LDO_32
:
916 // Another type of Local-Dynamic relocation.
917 return tls::TLSOPT_TO_LE
;
919 case elfcpp::R_386_TLS_IE
:
920 case elfcpp::R_386_TLS_GOTIE
:
921 case elfcpp::R_386_TLS_IE_32
:
922 // These are Initial-Exec relocs which get the thread offset
923 // from the GOT. If we know that we are linking against the
924 // local symbol, we can switch to Local-Exec, which links the
925 // thread offset into the instruction.
927 return tls::TLSOPT_TO_LE
;
928 return tls::TLSOPT_NONE
;
930 case elfcpp::R_386_TLS_LE
:
931 case elfcpp::R_386_TLS_LE_32
:
932 // When we already have Local-Exec, there is nothing further we
934 return tls::TLSOPT_NONE
;
941 // Report an unsupported relocation against a local symbol.
944 Target_i386::Scan::unsupported_reloc_local(Sized_relobj
<32, false>* object
,
947 gold_error(_("%s: unsupported reloc %u against local symbol"),
948 object
->name().c_str(), r_type
);
951 // Scan a relocation for a local symbol.
954 Target_i386::Scan::local(Symbol_table
* symtab
,
957 Sized_relobj
<32, false>* object
,
958 unsigned int data_shndx
,
959 Output_section
* output_section
,
960 const elfcpp::Rel
<32, false>& reloc
,
962 const elfcpp::Sym
<32, false>& lsym
)
966 case elfcpp::R_386_NONE
:
967 case elfcpp::R_386_GNU_VTINHERIT
:
968 case elfcpp::R_386_GNU_VTENTRY
:
971 case elfcpp::R_386_32
:
972 // If building a shared library (or a position-independent
973 // executable), we need to create a dynamic relocation for
974 // this location. The relocation applied at link time will
975 // apply the link-time value, so we flag the location with
976 // an R_386_RELATIVE relocation so the dynamic loader can
977 // relocate it easily.
978 if (parameters
->options().output_is_position_independent())
980 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
981 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
982 rel_dyn
->add_local_relative(object
, r_sym
, elfcpp::R_386_RELATIVE
,
983 output_section
, data_shndx
,
984 reloc
.get_r_offset());
988 case elfcpp::R_386_16
:
989 case elfcpp::R_386_8
:
990 // If building a shared library (or a position-independent
991 // executable), we need to create a dynamic relocation for
992 // this location. Because the addend needs to remain in the
993 // data section, we need to be careful not to apply this
994 // relocation statically.
995 if (parameters
->options().output_is_position_independent())
997 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
998 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
999 if (lsym
.get_st_type() != elfcpp::STT_SECTION
)
1000 rel_dyn
->add_local(object
, r_sym
, r_type
, output_section
,
1001 data_shndx
, reloc
.get_r_offset());
1004 gold_assert(lsym
.get_st_value() == 0);
1005 unsigned int shndx
= lsym
.get_st_shndx();
1007 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
,
1010 object
->error(_("section symbol %u has bad shndx %u"),
1013 rel_dyn
->add_local_section(object
, shndx
,
1014 r_type
, output_section
,
1015 data_shndx
, reloc
.get_r_offset());
1020 case elfcpp::R_386_PC32
:
1021 case elfcpp::R_386_PC16
:
1022 case elfcpp::R_386_PC8
:
1025 case elfcpp::R_386_PLT32
:
1026 // Since we know this is a local symbol, we can handle this as a
1030 case elfcpp::R_386_GOTOFF
:
1031 case elfcpp::R_386_GOTPC
:
1032 // We need a GOT section.
1033 target
->got_section(symtab
, layout
);
1036 case elfcpp::R_386_GOT32
:
1038 // The symbol requires a GOT entry.
1039 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1040 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1041 if (got
->add_local(object
, r_sym
, GOT_TYPE_STANDARD
))
1043 // If we are generating a shared object, we need to add a
1044 // dynamic RELATIVE relocation for this symbol's GOT entry.
1045 if (parameters
->options().output_is_position_independent())
1047 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1048 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1049 rel_dyn
->add_local_relative(
1050 object
, r_sym
, elfcpp::R_386_RELATIVE
, got
,
1051 object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1057 // These are relocations which should only be seen by the
1058 // dynamic linker, and should never be seen here.
1059 case elfcpp::R_386_COPY
:
1060 case elfcpp::R_386_GLOB_DAT
:
1061 case elfcpp::R_386_JUMP_SLOT
:
1062 case elfcpp::R_386_RELATIVE
:
1063 case elfcpp::R_386_TLS_TPOFF
:
1064 case elfcpp::R_386_TLS_DTPMOD32
:
1065 case elfcpp::R_386_TLS_DTPOFF32
:
1066 case elfcpp::R_386_TLS_TPOFF32
:
1067 case elfcpp::R_386_TLS_DESC
:
1068 gold_error(_("%s: unexpected reloc %u in object file"),
1069 object
->name().c_str(), r_type
);
1072 // These are initial TLS relocs, which are expected when
1074 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1075 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1076 case elfcpp::R_386_TLS_DESC_CALL
:
1077 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1078 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1079 case elfcpp::R_386_TLS_IE
: // Initial-exec
1080 case elfcpp::R_386_TLS_IE_32
:
1081 case elfcpp::R_386_TLS_GOTIE
:
1082 case elfcpp::R_386_TLS_LE
: // Local-exec
1083 case elfcpp::R_386_TLS_LE_32
:
1085 bool output_is_shared
= parameters
->options().shared();
1086 const tls::Tls_optimization optimized_type
1087 = Target_i386::optimize_tls_reloc(!output_is_shared
, r_type
);
1090 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1091 if (optimized_type
== tls::TLSOPT_NONE
)
1093 // Create a pair of GOT entries for the module index and
1094 // dtv-relative offset.
1095 Output_data_got
<32, false>* got
1096 = target
->got_section(symtab
, layout
);
1097 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1098 unsigned int shndx
= lsym
.get_st_shndx();
1100 shndx
= object
->adjust_sym_shndx(r_sym
, shndx
, &is_ordinary
);
1102 object
->error(_("local symbol %u has bad shndx %u"),
1105 got
->add_local_pair_with_rel(object
, r_sym
, shndx
,
1107 target
->rel_dyn_section(layout
),
1108 elfcpp::R_386_TLS_DTPMOD32
, 0);
1110 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1111 unsupported_reloc_local(object
, r_type
);
1114 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva)
1115 target
->define_tls_base_symbol(symtab
, layout
);
1116 if (optimized_type
== tls::TLSOPT_NONE
)
1118 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1119 Output_data_got
<32, false>* got
1120 = target
->got_section(symtab
, layout
);
1121 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1122 if (!object
->local_has_got_offset(r_sym
, GOT_TYPE_TLS_DESC
))
1124 unsigned int got_offset
= got
->add_constant(0);
1125 // The local symbol value is stored in the second
1127 got
->add_local(object
, r_sym
, GOT_TYPE_TLS_DESC
);
1128 // That set the GOT offset of the local symbol to
1129 // point to the second entry, but we want it to
1130 // point to the first.
1131 object
->set_local_got_offset(r_sym
, GOT_TYPE_TLS_DESC
,
1133 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1134 rt
->add_absolute(elfcpp::R_386_TLS_DESC
, got
, got_offset
);
1137 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1138 unsupported_reloc_local(object
, r_type
);
1141 case elfcpp::R_386_TLS_DESC_CALL
:
1144 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1145 if (optimized_type
== tls::TLSOPT_NONE
)
1147 // Create a GOT entry for the module index.
1148 target
->got_mod_index_entry(symtab
, layout
, object
);
1150 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1151 unsupported_reloc_local(object
, r_type
);
1154 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1157 case elfcpp::R_386_TLS_IE
: // Initial-exec
1158 case elfcpp::R_386_TLS_IE_32
:
1159 case elfcpp::R_386_TLS_GOTIE
:
1160 layout
->set_has_static_tls();
1161 if (optimized_type
== tls::TLSOPT_NONE
)
1163 // For the R_386_TLS_IE relocation, we need to create a
1164 // dynamic relocation when building a shared library.
1165 if (r_type
== elfcpp::R_386_TLS_IE
1166 && parameters
->options().shared())
1168 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1170 = elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1171 rel_dyn
->add_local_relative(object
, r_sym
,
1172 elfcpp::R_386_RELATIVE
,
1173 output_section
, data_shndx
,
1174 reloc
.get_r_offset());
1176 // Create a GOT entry for the tp-relative offset.
1177 Output_data_got
<32, false>* got
1178 = target
->got_section(symtab
, layout
);
1179 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1180 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1181 ? elfcpp::R_386_TLS_TPOFF32
1182 : elfcpp::R_386_TLS_TPOFF
);
1183 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1184 ? GOT_TYPE_TLS_OFFSET
1185 : GOT_TYPE_TLS_NOFFSET
);
1186 got
->add_local_with_rel(object
, r_sym
, got_type
,
1187 target
->rel_dyn_section(layout
),
1190 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1191 unsupported_reloc_local(object
, r_type
);
1194 case elfcpp::R_386_TLS_LE
: // Local-exec
1195 case elfcpp::R_386_TLS_LE_32
:
1196 layout
->set_has_static_tls();
1197 if (output_is_shared
)
1199 // We need to create a dynamic relocation.
1200 gold_assert(lsym
.get_st_type() != elfcpp::STT_SECTION
);
1201 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(reloc
.get_r_info());
1202 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1203 ? elfcpp::R_386_TLS_TPOFF32
1204 : elfcpp::R_386_TLS_TPOFF
);
1205 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1206 rel_dyn
->add_local(object
, r_sym
, dyn_r_type
, output_section
,
1207 data_shndx
, reloc
.get_r_offset());
1217 case elfcpp::R_386_32PLT
:
1218 case elfcpp::R_386_TLS_GD_32
:
1219 case elfcpp::R_386_TLS_GD_PUSH
:
1220 case elfcpp::R_386_TLS_GD_CALL
:
1221 case elfcpp::R_386_TLS_GD_POP
:
1222 case elfcpp::R_386_TLS_LDM_32
:
1223 case elfcpp::R_386_TLS_LDM_PUSH
:
1224 case elfcpp::R_386_TLS_LDM_CALL
:
1225 case elfcpp::R_386_TLS_LDM_POP
:
1226 case elfcpp::R_386_USED_BY_INTEL_200
:
1228 unsupported_reloc_local(object
, r_type
);
1233 // Report an unsupported relocation against a global symbol.
1236 Target_i386::Scan::unsupported_reloc_global(Sized_relobj
<32, false>* object
,
1237 unsigned int r_type
,
1240 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1241 object
->name().c_str(), r_type
, gsym
->demangled_name().c_str());
1245 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type
)
1249 case elfcpp::R_386_32
:
1250 case elfcpp::R_386_16
:
1251 case elfcpp::R_386_8
:
1252 case elfcpp::R_386_GOTOFF
:
1253 case elfcpp::R_386_GOT32
:
1264 Target_i386::Scan::local_reloc_may_be_function_pointer(
1268 Sized_relobj
<32, false>* ,
1271 const elfcpp::Rel
<32, false>& ,
1272 unsigned int r_type
,
1273 const elfcpp::Sym
<32, false>&)
1275 return possible_function_pointer_reloc(r_type
);
1279 Target_i386::Scan::global_reloc_may_be_function_pointer(
1283 Sized_relobj
<32, false>* ,
1286 const elfcpp::Rel
<32, false>& ,
1287 unsigned int r_type
,
1290 return possible_function_pointer_reloc(r_type
);
1293 // Scan a relocation for a global symbol.
1296 Target_i386::Scan::global(Symbol_table
* symtab
,
1298 Target_i386
* target
,
1299 Sized_relobj
<32, false>* object
,
1300 unsigned int data_shndx
,
1301 Output_section
* output_section
,
1302 const elfcpp::Rel
<32, false>& reloc
,
1303 unsigned int r_type
,
1308 case elfcpp::R_386_NONE
:
1309 case elfcpp::R_386_GNU_VTINHERIT
:
1310 case elfcpp::R_386_GNU_VTENTRY
:
1313 case elfcpp::R_386_32
:
1314 case elfcpp::R_386_16
:
1315 case elfcpp::R_386_8
:
1317 // Make a PLT entry if necessary.
1318 if (gsym
->needs_plt_entry())
1320 target
->make_plt_entry(symtab
, layout
, gsym
);
1321 // Since this is not a PC-relative relocation, we may be
1322 // taking the address of a function. In that case we need to
1323 // set the entry in the dynamic symbol table to the address of
1325 if (gsym
->is_from_dynobj() && !parameters
->options().shared())
1326 gsym
->set_needs_dynsym_value();
1328 // Make a dynamic relocation if necessary.
1329 if (gsym
->needs_dynamic_reloc(Symbol::ABSOLUTE_REF
))
1331 if (gsym
->may_need_copy_reloc())
1333 target
->copy_reloc(symtab
, layout
, object
,
1334 data_shndx
, output_section
, gsym
, reloc
);
1336 else if (r_type
== elfcpp::R_386_32
1337 && gsym
->can_use_relative_reloc(false))
1339 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1340 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1341 output_section
, object
,
1342 data_shndx
, reloc
.get_r_offset());
1346 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1347 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1348 data_shndx
, reloc
.get_r_offset());
1354 case elfcpp::R_386_PC32
:
1355 case elfcpp::R_386_PC16
:
1356 case elfcpp::R_386_PC8
:
1358 // Make a PLT entry if necessary.
1359 if (gsym
->needs_plt_entry())
1361 // These relocations are used for function calls only in
1362 // non-PIC code. For a 32-bit relocation in a shared library,
1363 // we'll need a text relocation anyway, so we can skip the
1364 // PLT entry and let the dynamic linker bind the call directly
1365 // to the target. For smaller relocations, we should use a
1366 // PLT entry to ensure that the call can reach.
1367 if (!parameters
->options().shared()
1368 || r_type
!= elfcpp::R_386_PC32
)
1369 target
->make_plt_entry(symtab
, layout
, gsym
);
1371 // Make a dynamic relocation if necessary.
1372 int flags
= Symbol::NON_PIC_REF
;
1373 if (gsym
->is_func())
1374 flags
|= Symbol::FUNCTION_CALL
;
1375 if (gsym
->needs_dynamic_reloc(flags
))
1377 if (gsym
->may_need_copy_reloc())
1379 target
->copy_reloc(symtab
, layout
, object
,
1380 data_shndx
, output_section
, gsym
, reloc
);
1384 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1385 rel_dyn
->add_global(gsym
, r_type
, output_section
, object
,
1386 data_shndx
, reloc
.get_r_offset());
1392 case elfcpp::R_386_GOT32
:
1394 // The symbol requires a GOT entry.
1395 Output_data_got
<32, false>* got
= target
->got_section(symtab
, layout
);
1396 if (gsym
->final_value_is_known())
1397 got
->add_global(gsym
, GOT_TYPE_STANDARD
);
1400 // If this symbol is not fully resolved, we need to add a
1401 // GOT entry with a dynamic relocation.
1402 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1403 if (gsym
->is_from_dynobj()
1404 || gsym
->is_undefined()
1405 || gsym
->is_preemptible())
1406 got
->add_global_with_rel(gsym
, GOT_TYPE_STANDARD
,
1407 rel_dyn
, elfcpp::R_386_GLOB_DAT
);
1410 if (got
->add_global(gsym
, GOT_TYPE_STANDARD
))
1411 rel_dyn
->add_global_relative(
1412 gsym
, elfcpp::R_386_RELATIVE
, got
,
1413 gsym
->got_offset(GOT_TYPE_STANDARD
));
1419 case elfcpp::R_386_PLT32
:
1420 // If the symbol is fully resolved, this is just a PC32 reloc.
1421 // Otherwise we need a PLT entry.
1422 if (gsym
->final_value_is_known())
1424 // If building a shared library, we can also skip the PLT entry
1425 // if the symbol is defined in the output file and is protected
1427 if (gsym
->is_defined()
1428 && !gsym
->is_from_dynobj()
1429 && !gsym
->is_preemptible())
1431 target
->make_plt_entry(symtab
, layout
, gsym
);
1434 case elfcpp::R_386_GOTOFF
:
1435 case elfcpp::R_386_GOTPC
:
1436 // We need a GOT section.
1437 target
->got_section(symtab
, layout
);
1440 // These are relocations which should only be seen by the
1441 // dynamic linker, and should never be seen here.
1442 case elfcpp::R_386_COPY
:
1443 case elfcpp::R_386_GLOB_DAT
:
1444 case elfcpp::R_386_JUMP_SLOT
:
1445 case elfcpp::R_386_RELATIVE
:
1446 case elfcpp::R_386_TLS_TPOFF
:
1447 case elfcpp::R_386_TLS_DTPMOD32
:
1448 case elfcpp::R_386_TLS_DTPOFF32
:
1449 case elfcpp::R_386_TLS_TPOFF32
:
1450 case elfcpp::R_386_TLS_DESC
:
1451 gold_error(_("%s: unexpected reloc %u in object file"),
1452 object
->name().c_str(), r_type
);
1455 // These are initial tls relocs, which are expected when
1457 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1458 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1459 case elfcpp::R_386_TLS_DESC_CALL
:
1460 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1461 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1462 case elfcpp::R_386_TLS_IE
: // Initial-exec
1463 case elfcpp::R_386_TLS_IE_32
:
1464 case elfcpp::R_386_TLS_GOTIE
:
1465 case elfcpp::R_386_TLS_LE
: // Local-exec
1466 case elfcpp::R_386_TLS_LE_32
:
1468 const bool is_final
= gsym
->final_value_is_known();
1469 const tls::Tls_optimization optimized_type
1470 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1473 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1474 if (optimized_type
== tls::TLSOPT_NONE
)
1476 // Create a pair of GOT entries for the module index and
1477 // dtv-relative offset.
1478 Output_data_got
<32, false>* got
1479 = target
->got_section(symtab
, layout
);
1480 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_PAIR
,
1481 target
->rel_dyn_section(layout
),
1482 elfcpp::R_386_TLS_DTPMOD32
,
1483 elfcpp::R_386_TLS_DTPOFF32
);
1485 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1487 // Create a GOT entry for the tp-relative offset.
1488 Output_data_got
<32, false>* got
1489 = target
->got_section(symtab
, layout
);
1490 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1491 target
->rel_dyn_section(layout
),
1492 elfcpp::R_386_TLS_TPOFF
);
1494 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1495 unsupported_reloc_global(object
, r_type
, gsym
);
1498 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (~oliva url)
1499 target
->define_tls_base_symbol(symtab
, layout
);
1500 if (optimized_type
== tls::TLSOPT_NONE
)
1502 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1503 Output_data_got
<32, false>* got
1504 = target
->got_section(symtab
, layout
);
1505 Reloc_section
* rt
= target
->rel_tls_desc_section(layout
);
1506 got
->add_global_pair_with_rel(gsym
, GOT_TYPE_TLS_DESC
, rt
,
1507 elfcpp::R_386_TLS_DESC
, 0);
1509 else if (optimized_type
== tls::TLSOPT_TO_IE
)
1511 // Create a GOT entry for the tp-relative offset.
1512 Output_data_got
<32, false>* got
1513 = target
->got_section(symtab
, layout
);
1514 got
->add_global_with_rel(gsym
, GOT_TYPE_TLS_NOFFSET
,
1515 target
->rel_dyn_section(layout
),
1516 elfcpp::R_386_TLS_TPOFF
);
1518 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1519 unsupported_reloc_global(object
, r_type
, gsym
);
1522 case elfcpp::R_386_TLS_DESC_CALL
:
1525 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1526 if (optimized_type
== tls::TLSOPT_NONE
)
1528 // Create a GOT entry for the module index.
1529 target
->got_mod_index_entry(symtab
, layout
, object
);
1531 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1532 unsupported_reloc_global(object
, r_type
, gsym
);
1535 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1538 case elfcpp::R_386_TLS_IE
: // Initial-exec
1539 case elfcpp::R_386_TLS_IE_32
:
1540 case elfcpp::R_386_TLS_GOTIE
:
1541 layout
->set_has_static_tls();
1542 if (optimized_type
== tls::TLSOPT_NONE
)
1544 // For the R_386_TLS_IE relocation, we need to create a
1545 // dynamic relocation when building a shared library.
1546 if (r_type
== elfcpp::R_386_TLS_IE
1547 && parameters
->options().shared())
1549 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1550 rel_dyn
->add_global_relative(gsym
, elfcpp::R_386_RELATIVE
,
1551 output_section
, object
,
1553 reloc
.get_r_offset());
1555 // Create a GOT entry for the tp-relative offset.
1556 Output_data_got
<32, false>* got
1557 = target
->got_section(symtab
, layout
);
1558 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1559 ? elfcpp::R_386_TLS_TPOFF32
1560 : elfcpp::R_386_TLS_TPOFF
);
1561 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
1562 ? GOT_TYPE_TLS_OFFSET
1563 : GOT_TYPE_TLS_NOFFSET
);
1564 got
->add_global_with_rel(gsym
, got_type
,
1565 target
->rel_dyn_section(layout
),
1568 else if (optimized_type
!= tls::TLSOPT_TO_LE
)
1569 unsupported_reloc_global(object
, r_type
, gsym
);
1572 case elfcpp::R_386_TLS_LE
: // Local-exec
1573 case elfcpp::R_386_TLS_LE_32
:
1574 layout
->set_has_static_tls();
1575 if (parameters
->options().shared())
1577 // We need to create a dynamic relocation.
1578 unsigned int dyn_r_type
= (r_type
== elfcpp::R_386_TLS_LE_32
1579 ? elfcpp::R_386_TLS_TPOFF32
1580 : elfcpp::R_386_TLS_TPOFF
);
1581 Reloc_section
* rel_dyn
= target
->rel_dyn_section(layout
);
1582 rel_dyn
->add_global(gsym
, dyn_r_type
, output_section
, object
,
1583 data_shndx
, reloc
.get_r_offset());
1593 case elfcpp::R_386_32PLT
:
1594 case elfcpp::R_386_TLS_GD_32
:
1595 case elfcpp::R_386_TLS_GD_PUSH
:
1596 case elfcpp::R_386_TLS_GD_CALL
:
1597 case elfcpp::R_386_TLS_GD_POP
:
1598 case elfcpp::R_386_TLS_LDM_32
:
1599 case elfcpp::R_386_TLS_LDM_PUSH
:
1600 case elfcpp::R_386_TLS_LDM_CALL
:
1601 case elfcpp::R_386_TLS_LDM_POP
:
1602 case elfcpp::R_386_USED_BY_INTEL_200
:
1604 unsupported_reloc_global(object
, r_type
, gsym
);
1609 // Process relocations for gc.
1612 Target_i386::gc_process_relocs(Symbol_table
* symtab
,
1614 Sized_relobj
<32, false>* object
,
1615 unsigned int data_shndx
,
1617 const unsigned char* prelocs
,
1619 Output_section
* output_section
,
1620 bool needs_special_offset_handling
,
1621 size_t local_symbol_count
,
1622 const unsigned char* plocal_symbols
)
1624 gold::gc_process_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1634 needs_special_offset_handling
,
1639 // Scan relocations for a section.
1642 Target_i386::scan_relocs(Symbol_table
* symtab
,
1644 Sized_relobj
<32, false>* object
,
1645 unsigned int data_shndx
,
1646 unsigned int sh_type
,
1647 const unsigned char* prelocs
,
1649 Output_section
* output_section
,
1650 bool needs_special_offset_handling
,
1651 size_t local_symbol_count
,
1652 const unsigned char* plocal_symbols
)
1654 if (sh_type
== elfcpp::SHT_RELA
)
1656 gold_error(_("%s: unsupported RELA reloc section"),
1657 object
->name().c_str());
1661 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
1671 needs_special_offset_handling
,
1676 // Finalize the sections.
1679 Target_i386::do_finalize_sections(
1681 const Input_objects
*,
1682 Symbol_table
* symtab
)
1684 const Reloc_section
* rel_plt
= (this->plt_
== NULL
1686 : this->plt_
->rel_plt());
1687 layout
->add_target_dynamic_tags(true, this->got_plt_
, rel_plt
,
1688 this->rel_dyn_
, true, false);
1690 // Emit any relocs we saved in an attempt to avoid generating COPY
1692 if (this->copy_relocs_
.any_saved_relocs())
1693 this->copy_relocs_
.emit(this->rel_dyn_section(layout
));
1695 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1696 // the .got.plt section.
1697 Symbol
* sym
= this->global_offset_table_
;
1700 uint32_t data_size
= this->got_plt_
->current_data_size();
1701 symtab
->get_sized_symbol
<32>(sym
)->set_symsize(data_size
);
1705 // Return whether a direct absolute static relocation needs to be applied.
1706 // In cases where Scan::local() or Scan::global() has created
1707 // a dynamic relocation other than R_386_RELATIVE, the addend
1708 // of the relocation is carried in the data, and we must not
1709 // apply the static relocation.
1712 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol
<32>* gsym
,
1715 Output_section
* output_section
)
1717 // If the output section is not allocated, then we didn't call
1718 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1720 if ((output_section
->flags() & elfcpp::SHF_ALLOC
) == 0)
1723 // For local symbols, we will have created a non-RELATIVE dynamic
1724 // relocation only if (a) the output is position independent,
1725 // (b) the relocation is absolute (not pc- or segment-relative), and
1726 // (c) the relocation is not 32 bits wide.
1728 return !(parameters
->options().output_is_position_independent()
1729 && (ref_flags
& Symbol::ABSOLUTE_REF
)
1732 // For global symbols, we use the same helper routines used in the
1733 // scan pass. If we did not create a dynamic relocation, or if we
1734 // created a RELATIVE dynamic relocation, we should apply the static
1736 bool has_dyn
= gsym
->needs_dynamic_reloc(ref_flags
);
1737 bool is_rel
= (ref_flags
& Symbol::ABSOLUTE_REF
)
1738 && gsym
->can_use_relative_reloc(ref_flags
1739 & Symbol::FUNCTION_CALL
);
1740 return !has_dyn
|| is_rel
;
1743 // Perform a relocation.
1746 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
1747 Target_i386
* target
,
1748 Output_section
*output_section
,
1750 const elfcpp::Rel
<32, false>& rel
,
1751 unsigned int r_type
,
1752 const Sized_symbol
<32>* gsym
,
1753 const Symbol_value
<32>* psymval
,
1754 unsigned char* view
,
1755 elfcpp::Elf_types
<32>::Elf_Addr address
,
1756 section_size_type view_size
)
1758 if (this->skip_call_tls_get_addr_
)
1760 if ((r_type
!= elfcpp::R_386_PLT32
1761 && r_type
!= elfcpp::R_386_PC32
)
1763 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
1764 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1765 _("missing expected TLS relocation"));
1768 this->skip_call_tls_get_addr_
= false;
1773 // Pick the value to use for symbols defined in shared objects.
1774 Symbol_value
<32> symval
;
1776 && gsym
->use_plt_offset(r_type
== elfcpp::R_386_PC8
1777 || r_type
== elfcpp::R_386_PC16
1778 || r_type
== elfcpp::R_386_PC32
))
1780 symval
.set_output_value(target
->plt_section()->address()
1781 + gsym
->plt_offset());
1785 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1787 // Get the GOT offset if needed.
1788 // The GOT pointer points to the end of the GOT section.
1789 // We need to subtract the size of the GOT section to get
1790 // the actual offset to use in the relocation.
1791 bool have_got_offset
= false;
1792 unsigned int got_offset
= 0;
1795 case elfcpp::R_386_GOT32
:
1798 gold_assert(gsym
->has_got_offset(GOT_TYPE_STANDARD
));
1799 got_offset
= (gsym
->got_offset(GOT_TYPE_STANDARD
)
1800 - target
->got_size());
1804 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
1805 gold_assert(object
->local_has_got_offset(r_sym
, GOT_TYPE_STANDARD
));
1806 got_offset
= (object
->local_got_offset(r_sym
, GOT_TYPE_STANDARD
)
1807 - target
->got_size());
1809 have_got_offset
= true;
1818 case elfcpp::R_386_NONE
:
1819 case elfcpp::R_386_GNU_VTINHERIT
:
1820 case elfcpp::R_386_GNU_VTENTRY
:
1823 case elfcpp::R_386_32
:
1824 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, true,
1826 Relocate_functions
<32, false>::rel32(view
, object
, psymval
);
1829 case elfcpp::R_386_PC32
:
1831 int ref_flags
= Symbol::NON_PIC_REF
;
1832 if (gsym
!= NULL
&& gsym
->is_func())
1833 ref_flags
|= Symbol::FUNCTION_CALL
;
1834 if (should_apply_static_reloc(gsym
, ref_flags
, true, output_section
))
1835 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1839 case elfcpp::R_386_16
:
1840 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1842 Relocate_functions
<32, false>::rel16(view
, object
, psymval
);
1845 case elfcpp::R_386_PC16
:
1847 int ref_flags
= Symbol::NON_PIC_REF
;
1848 if (gsym
!= NULL
&& gsym
->is_func())
1849 ref_flags
|= Symbol::FUNCTION_CALL
;
1850 if (should_apply_static_reloc(gsym
, ref_flags
, false, output_section
))
1851 Relocate_functions
<32, false>::pcrel16(view
, object
, psymval
, address
);
1855 case elfcpp::R_386_8
:
1856 if (should_apply_static_reloc(gsym
, Symbol::ABSOLUTE_REF
, false,
1858 Relocate_functions
<32, false>::rel8(view
, object
, psymval
);
1861 case elfcpp::R_386_PC8
:
1863 int ref_flags
= Symbol::NON_PIC_REF
;
1864 if (gsym
!= NULL
&& gsym
->is_func())
1865 ref_flags
|= Symbol::FUNCTION_CALL
;
1866 if (should_apply_static_reloc(gsym
, ref_flags
, false,
1868 Relocate_functions
<32, false>::pcrel8(view
, object
, psymval
, address
);
1872 case elfcpp::R_386_PLT32
:
1873 gold_assert(gsym
== NULL
1874 || gsym
->has_plt_offset()
1875 || gsym
->final_value_is_known()
1876 || (gsym
->is_defined()
1877 && !gsym
->is_from_dynobj()
1878 && !gsym
->is_preemptible()));
1879 Relocate_functions
<32, false>::pcrel32(view
, object
, psymval
, address
);
1882 case elfcpp::R_386_GOT32
:
1883 gold_assert(have_got_offset
);
1884 Relocate_functions
<32, false>::rel32(view
, got_offset
);
1887 case elfcpp::R_386_GOTOFF
:
1889 elfcpp::Elf_types
<32>::Elf_Addr value
;
1890 value
= (psymval
->value(object
, 0)
1891 - target
->got_plt_section()->address());
1892 Relocate_functions
<32, false>::rel32(view
, value
);
1896 case elfcpp::R_386_GOTPC
:
1898 elfcpp::Elf_types
<32>::Elf_Addr value
;
1899 value
= target
->got_plt_section()->address();
1900 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
1904 case elfcpp::R_386_COPY
:
1905 case elfcpp::R_386_GLOB_DAT
:
1906 case elfcpp::R_386_JUMP_SLOT
:
1907 case elfcpp::R_386_RELATIVE
:
1908 // These are outstanding tls relocs, which are unexpected when
1910 case elfcpp::R_386_TLS_TPOFF
:
1911 case elfcpp::R_386_TLS_DTPMOD32
:
1912 case elfcpp::R_386_TLS_DTPOFF32
:
1913 case elfcpp::R_386_TLS_TPOFF32
:
1914 case elfcpp::R_386_TLS_DESC
:
1915 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1916 _("unexpected reloc %u in object file"),
1920 // These are initial tls relocs, which are expected when
1922 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1923 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
1924 case elfcpp::R_386_TLS_DESC_CALL
:
1925 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
1926 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
1927 case elfcpp::R_386_TLS_IE
: // Initial-exec
1928 case elfcpp::R_386_TLS_IE_32
:
1929 case elfcpp::R_386_TLS_GOTIE
:
1930 case elfcpp::R_386_TLS_LE
: // Local-exec
1931 case elfcpp::R_386_TLS_LE_32
:
1932 this->relocate_tls(relinfo
, target
, relnum
, rel
, r_type
, gsym
, psymval
,
1933 view
, address
, view_size
);
1936 case elfcpp::R_386_32PLT
:
1937 case elfcpp::R_386_TLS_GD_32
:
1938 case elfcpp::R_386_TLS_GD_PUSH
:
1939 case elfcpp::R_386_TLS_GD_CALL
:
1940 case elfcpp::R_386_TLS_GD_POP
:
1941 case elfcpp::R_386_TLS_LDM_32
:
1942 case elfcpp::R_386_TLS_LDM_PUSH
:
1943 case elfcpp::R_386_TLS_LDM_CALL
:
1944 case elfcpp::R_386_TLS_LDM_POP
:
1945 case elfcpp::R_386_USED_BY_INTEL_200
:
1947 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
1948 _("unsupported reloc %u"),
1956 // Perform a TLS relocation.
1959 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
1960 Target_i386
* target
,
1962 const elfcpp::Rel
<32, false>& rel
,
1963 unsigned int r_type
,
1964 const Sized_symbol
<32>* gsym
,
1965 const Symbol_value
<32>* psymval
,
1966 unsigned char* view
,
1967 elfcpp::Elf_types
<32>::Elf_Addr
,
1968 section_size_type view_size
)
1970 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
1972 const Sized_relobj
<32, false>* object
= relinfo
->object
;
1974 elfcpp::Elf_types
<32>::Elf_Addr value
= psymval
->value(object
, 0);
1976 const bool is_final
= (gsym
== NULL
1977 ? !parameters
->options().shared()
1978 : gsym
->final_value_is_known());
1979 const tls::Tls_optimization optimized_type
1980 = Target_i386::optimize_tls_reloc(is_final
, r_type
);
1983 case elfcpp::R_386_TLS_GD
: // Global-dynamic
1984 if (optimized_type
== tls::TLSOPT_TO_LE
)
1986 gold_assert(tls_segment
!= NULL
);
1987 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
1988 rel
, r_type
, value
, view
,
1994 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
1995 ? GOT_TYPE_TLS_NOFFSET
1996 : GOT_TYPE_TLS_PAIR
);
1997 unsigned int got_offset
;
2000 gold_assert(gsym
->has_got_offset(got_type
));
2001 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2005 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2006 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2007 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2008 - target
->got_size());
2010 if (optimized_type
== tls::TLSOPT_TO_IE
)
2012 gold_assert(tls_segment
!= NULL
);
2013 this->tls_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2014 got_offset
, view
, view_size
);
2017 else if (optimized_type
== tls::TLSOPT_NONE
)
2019 // Relocate the field with the offset of the pair of GOT
2021 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2025 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2026 _("unsupported reloc %u"),
2030 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2031 case elfcpp::R_386_TLS_DESC_CALL
:
2032 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2033 if (optimized_type
== tls::TLSOPT_TO_LE
)
2035 gold_assert(tls_segment
!= NULL
);
2036 this->tls_desc_gd_to_le(relinfo
, relnum
, tls_segment
,
2037 rel
, r_type
, value
, view
,
2043 unsigned int got_type
= (optimized_type
== tls::TLSOPT_TO_IE
2044 ? GOT_TYPE_TLS_NOFFSET
2045 : GOT_TYPE_TLS_DESC
);
2046 unsigned int got_offset
;
2049 gold_assert(gsym
->has_got_offset(got_type
));
2050 got_offset
= gsym
->got_offset(got_type
) - target
->got_size();
2054 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2055 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2056 got_offset
= (object
->local_got_offset(r_sym
, got_type
)
2057 - target
->got_size());
2059 if (optimized_type
== tls::TLSOPT_TO_IE
)
2061 gold_assert(tls_segment
!= NULL
);
2062 this->tls_desc_gd_to_ie(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2063 got_offset
, view
, view_size
);
2066 else if (optimized_type
== tls::TLSOPT_NONE
)
2068 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2070 // Relocate the field with the offset of the pair of GOT
2072 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2077 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2078 _("unsupported reloc %u"),
2082 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2083 if (this->local_dynamic_type_
== LOCAL_DYNAMIC_SUN
)
2085 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2086 _("both SUN and GNU model "
2087 "TLS relocations"));
2090 this->local_dynamic_type_
= LOCAL_DYNAMIC_GNU
;
2091 if (optimized_type
== tls::TLSOPT_TO_LE
)
2093 gold_assert(tls_segment
!= NULL
);
2094 this->tls_ld_to_le(relinfo
, relnum
, tls_segment
, rel
, r_type
,
2095 value
, view
, view_size
);
2098 else if (optimized_type
== tls::TLSOPT_NONE
)
2100 // Relocate the field with the offset of the GOT entry for
2101 // the module index.
2102 unsigned int got_offset
;
2103 got_offset
= (target
->got_mod_index_entry(NULL
, NULL
, NULL
)
2104 - target
->got_size());
2105 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2108 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2109 _("unsupported reloc %u"),
2113 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2114 if (optimized_type
== tls::TLSOPT_TO_LE
)
2116 // This reloc can appear in debugging sections, in which
2117 // case we must not convert to local-exec. We decide what
2118 // to do based on whether the section is marked as
2119 // containing executable code. That is what the GNU linker
2121 elfcpp::Shdr
<32, false> shdr(relinfo
->data_shdr
);
2122 if ((shdr
.get_sh_flags() & elfcpp::SHF_EXECINSTR
) != 0)
2124 gold_assert(tls_segment
!= NULL
);
2125 value
-= tls_segment
->memsz();
2128 Relocate_functions
<32, false>::rel32(view
, value
);
2131 case elfcpp::R_386_TLS_IE
: // Initial-exec
2132 case elfcpp::R_386_TLS_GOTIE
:
2133 case elfcpp::R_386_TLS_IE_32
:
2134 if (optimized_type
== tls::TLSOPT_TO_LE
)
2136 gold_assert(tls_segment
!= NULL
);
2137 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
2138 rel
, r_type
, value
, view
,
2142 else if (optimized_type
== tls::TLSOPT_NONE
)
2144 // Relocate the field with the offset of the GOT entry for
2145 // the tp-relative offset of the symbol.
2146 unsigned int got_type
= (r_type
== elfcpp::R_386_TLS_IE_32
2147 ? GOT_TYPE_TLS_OFFSET
2148 : GOT_TYPE_TLS_NOFFSET
);
2149 unsigned int got_offset
;
2152 gold_assert(gsym
->has_got_offset(got_type
));
2153 got_offset
= gsym
->got_offset(got_type
);
2157 unsigned int r_sym
= elfcpp::elf_r_sym
<32>(rel
.get_r_info());
2158 gold_assert(object
->local_has_got_offset(r_sym
, got_type
));
2159 got_offset
= object
->local_got_offset(r_sym
, got_type
);
2161 // For the R_386_TLS_IE relocation, we need to apply the
2162 // absolute address of the GOT entry.
2163 if (r_type
== elfcpp::R_386_TLS_IE
)
2164 got_offset
+= target
->got_plt_section()->address();
2165 // All GOT offsets are relative to the end of the GOT.
2166 got_offset
-= target
->got_size();
2167 Relocate_functions
<32, false>::rel32(view
, got_offset
);
2170 gold_error_at_location(relinfo
, relnum
, rel
.get_r_offset(),
2171 _("unsupported reloc %u"),
2175 case elfcpp::R_386_TLS_LE
: // Local-exec
2176 // If we're creating a shared library, a dynamic relocation will
2177 // have been created for this location, so do not apply it now.
2178 if (!parameters
->options().shared())
2180 gold_assert(tls_segment
!= NULL
);
2181 value
-= tls_segment
->memsz();
2182 Relocate_functions
<32, false>::rel32(view
, value
);
2186 case elfcpp::R_386_TLS_LE_32
:
2187 // If we're creating a shared library, a dynamic relocation will
2188 // have been created for this location, so do not apply it now.
2189 if (!parameters
->options().shared())
2191 gold_assert(tls_segment
!= NULL
);
2192 value
= tls_segment
->memsz() - value
;
2193 Relocate_functions
<32, false>::rel32(view
, value
);
2199 // Do a relocation in which we convert a TLS General-Dynamic to a
2203 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
2205 Output_segment
* tls_segment
,
2206 const elfcpp::Rel
<32, false>& rel
,
2208 elfcpp::Elf_types
<32>::Elf_Addr value
,
2209 unsigned char* view
,
2210 section_size_type view_size
)
2212 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2213 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2214 // leal foo(%reg),%eax; call ___tls_get_addr
2215 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2217 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2218 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2220 unsigned char op1
= view
[-1];
2221 unsigned char op2
= view
[-2];
2223 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2224 op2
== 0x8d || op2
== 0x04);
2225 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2231 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2232 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2233 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2234 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2235 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2239 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2240 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2241 if (rel
.get_r_offset() + 9 < view_size
2244 // There is a trailing nop. Use the size byte subl.
2245 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2250 // Use the five byte subl.
2251 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2255 value
= tls_segment
->memsz() - value
;
2256 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2258 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2260 this->skip_call_tls_get_addr_
= true;
2263 // Do a relocation in which we convert a TLS General-Dynamic to an
2267 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info
<32, false>* relinfo
,
2270 const elfcpp::Rel
<32, false>& rel
,
2272 elfcpp::Elf_types
<32>::Elf_Addr value
,
2273 unsigned char* view
,
2274 section_size_type view_size
)
2276 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2277 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2279 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2280 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2282 unsigned char op1
= view
[-1];
2283 unsigned char op2
= view
[-2];
2285 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2286 op2
== 0x8d || op2
== 0x04);
2287 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2291 // FIXME: For now, support only the first (SIB) form.
2292 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), op2
== 0x04);
2296 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -3);
2297 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[-3] == 0x8d);
2298 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2299 ((op1
& 0xc7) == 0x05 && op1
!= (4 << 3)));
2300 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2304 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2305 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
2306 if (rel
.get_r_offset() + 9 < view_size
2309 // FIXME: This is not the right instruction sequence.
2310 // There is a trailing nop. Use the size byte subl.
2311 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2316 // FIXME: This is not the right instruction sequence.
2317 // Use the five byte subl.
2318 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2322 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
2324 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2326 this->skip_call_tls_get_addr_
= true;
2329 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2330 // General-Dynamic to a Local-Exec.
2333 Target_i386::Relocate::tls_desc_gd_to_le(
2334 const Relocate_info
<32, false>* relinfo
,
2336 Output_segment
* tls_segment
,
2337 const elfcpp::Rel
<32, false>& rel
,
2338 unsigned int r_type
,
2339 elfcpp::Elf_types
<32>::Elf_Addr value
,
2340 unsigned char* view
,
2341 section_size_type view_size
)
2343 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2345 // leal foo@TLSDESC(%ebx), %eax
2346 // ==> leal foo@NTPOFF, %eax
2347 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2348 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2349 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2350 view
[-2] == 0x8d && view
[-1] == 0x83);
2352 value
-= tls_segment
->memsz();
2353 Relocate_functions
<32, false>::rel32(view
, value
);
2357 // call *foo@TLSCALL(%eax)
2359 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2360 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2361 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2362 view
[0] == 0xff && view
[1] == 0x10);
2368 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2369 // General-Dynamic to an Initial-Exec.
2372 Target_i386::Relocate::tls_desc_gd_to_ie(
2373 const Relocate_info
<32, false>* relinfo
,
2376 const elfcpp::Rel
<32, false>& rel
,
2377 unsigned int r_type
,
2378 elfcpp::Elf_types
<32>::Elf_Addr value
,
2379 unsigned char* view
,
2380 section_size_type view_size
)
2382 if (r_type
== elfcpp::R_386_TLS_GOTDESC
)
2384 // leal foo@TLSDESC(%ebx), %eax
2385 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2386 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2387 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2388 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2389 view
[-2] == 0x8d && view
[-1] == 0x83);
2391 Relocate_functions
<32, false>::rel32(view
, value
);
2395 // call *foo@TLSCALL(%eax)
2397 gold_assert(r_type
== elfcpp::R_386_TLS_DESC_CALL
);
2398 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 2);
2399 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2400 view
[0] == 0xff && view
[1] == 0x10);
2406 // Do a relocation in which we convert a TLS Local-Dynamic to a
2410 Target_i386::Relocate::tls_ld_to_le(const Relocate_info
<32, false>* relinfo
,
2413 const elfcpp::Rel
<32, false>& rel
,
2415 elfcpp::Elf_types
<32>::Elf_Addr
,
2416 unsigned char* view
,
2417 section_size_type view_size
)
2419 // leal foo(%reg), %eax; call ___tls_get_addr
2420 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2422 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2423 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 9);
2425 // FIXME: Does this test really always pass?
2426 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2427 view
[-2] == 0x8d && view
[-1] == 0x83);
2429 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), view
[4] == 0xe8);
2431 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2433 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2435 this->skip_call_tls_get_addr_
= true;
2438 // Do a relocation in which we convert a TLS Initial-Exec to a
2442 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
2444 Output_segment
* tls_segment
,
2445 const elfcpp::Rel
<32, false>& rel
,
2446 unsigned int r_type
,
2447 elfcpp::Elf_types
<32>::Elf_Addr value
,
2448 unsigned char* view
,
2449 section_size_type view_size
)
2451 // We have to actually change the instructions, which means that we
2452 // need to examine the opcodes to figure out which instruction we
2454 if (r_type
== elfcpp::R_386_TLS_IE
)
2456 // movl %gs:XX,%eax ==> movl $YY,%eax
2457 // movl %gs:XX,%reg ==> movl $YY,%reg
2458 // addl %gs:XX,%reg ==> addl $YY,%reg
2459 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -1);
2460 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2462 unsigned char op1
= view
[-1];
2465 // movl XX,%eax ==> movl $YY,%eax
2470 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2472 unsigned char op2
= view
[-2];
2475 // movl XX,%reg ==> movl $YY,%reg
2476 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2477 (op1
& 0xc7) == 0x05);
2479 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2481 else if (op2
== 0x03)
2483 // addl XX,%reg ==> addl $YY,%reg
2484 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2485 (op1
& 0xc7) == 0x05);
2487 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2490 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2495 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2496 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2497 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2498 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, -2);
2499 tls::check_range(relinfo
, relnum
, rel
.get_r_offset(), view_size
, 4);
2501 unsigned char op1
= view
[-1];
2502 unsigned char op2
= view
[-2];
2503 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(),
2504 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
2507 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2509 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2511 else if (op2
== 0x2b)
2513 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2515 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
2517 else if (op2
== 0x03)
2519 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2521 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
2524 tls::check_tls(relinfo
, relnum
, rel
.get_r_offset(), 0);
2527 value
= tls_segment
->memsz() - value
;
2528 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
2531 Relocate_functions
<32, false>::rel32(view
, value
);
2534 // Relocate section data.
2537 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
2538 unsigned int sh_type
,
2539 const unsigned char* prelocs
,
2541 Output_section
* output_section
,
2542 bool needs_special_offset_handling
,
2543 unsigned char* view
,
2544 elfcpp::Elf_types
<32>::Elf_Addr address
,
2545 section_size_type view_size
,
2546 const Reloc_symbol_changes
* reloc_symbol_changes
)
2548 gold_assert(sh_type
== elfcpp::SHT_REL
);
2550 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
2551 Target_i386::Relocate
>(
2557 needs_special_offset_handling
,
2561 reloc_symbol_changes
);
2564 // Return the size of a relocation while scanning during a relocatable
2568 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2569 unsigned int r_type
,
2574 case elfcpp::R_386_NONE
:
2575 case elfcpp::R_386_GNU_VTINHERIT
:
2576 case elfcpp::R_386_GNU_VTENTRY
:
2577 case elfcpp::R_386_TLS_GD
: // Global-dynamic
2578 case elfcpp::R_386_TLS_GOTDESC
: // Global-dynamic (from ~oliva url)
2579 case elfcpp::R_386_TLS_DESC_CALL
:
2580 case elfcpp::R_386_TLS_LDM
: // Local-dynamic
2581 case elfcpp::R_386_TLS_LDO_32
: // Alternate local-dynamic
2582 case elfcpp::R_386_TLS_IE
: // Initial-exec
2583 case elfcpp::R_386_TLS_IE_32
:
2584 case elfcpp::R_386_TLS_GOTIE
:
2585 case elfcpp::R_386_TLS_LE
: // Local-exec
2586 case elfcpp::R_386_TLS_LE_32
:
2589 case elfcpp::R_386_32
:
2590 case elfcpp::R_386_PC32
:
2591 case elfcpp::R_386_GOT32
:
2592 case elfcpp::R_386_PLT32
:
2593 case elfcpp::R_386_GOTOFF
:
2594 case elfcpp::R_386_GOTPC
:
2597 case elfcpp::R_386_16
:
2598 case elfcpp::R_386_PC16
:
2601 case elfcpp::R_386_8
:
2602 case elfcpp::R_386_PC8
:
2605 // These are relocations which should only be seen by the
2606 // dynamic linker, and should never be seen here.
2607 case elfcpp::R_386_COPY
:
2608 case elfcpp::R_386_GLOB_DAT
:
2609 case elfcpp::R_386_JUMP_SLOT
:
2610 case elfcpp::R_386_RELATIVE
:
2611 case elfcpp::R_386_TLS_TPOFF
:
2612 case elfcpp::R_386_TLS_DTPMOD32
:
2613 case elfcpp::R_386_TLS_DTPOFF32
:
2614 case elfcpp::R_386_TLS_TPOFF32
:
2615 case elfcpp::R_386_TLS_DESC
:
2616 object
->error(_("unexpected reloc %u in object file"), r_type
);
2619 case elfcpp::R_386_32PLT
:
2620 case elfcpp::R_386_TLS_GD_32
:
2621 case elfcpp::R_386_TLS_GD_PUSH
:
2622 case elfcpp::R_386_TLS_GD_CALL
:
2623 case elfcpp::R_386_TLS_GD_POP
:
2624 case elfcpp::R_386_TLS_LDM_32
:
2625 case elfcpp::R_386_TLS_LDM_PUSH
:
2626 case elfcpp::R_386_TLS_LDM_CALL
:
2627 case elfcpp::R_386_TLS_LDM_POP
:
2628 case elfcpp::R_386_USED_BY_INTEL_200
:
2630 object
->error(_("unsupported reloc %u in object file"), r_type
);
2635 // Scan the relocs during a relocatable link.
2638 Target_i386::scan_relocatable_relocs(Symbol_table
* symtab
,
2640 Sized_relobj
<32, false>* object
,
2641 unsigned int data_shndx
,
2642 unsigned int sh_type
,
2643 const unsigned char* prelocs
,
2645 Output_section
* output_section
,
2646 bool needs_special_offset_handling
,
2647 size_t local_symbol_count
,
2648 const unsigned char* plocal_symbols
,
2649 Relocatable_relocs
* rr
)
2651 gold_assert(sh_type
== elfcpp::SHT_REL
);
2653 typedef gold::Default_scan_relocatable_relocs
<elfcpp::SHT_REL
,
2654 Relocatable_size_for_reloc
> Scan_relocatable_relocs
;
2656 gold::scan_relocatable_relocs
<32, false, elfcpp::SHT_REL
,
2657 Scan_relocatable_relocs
>(
2665 needs_special_offset_handling
,
2671 // Relocate a section during a relocatable link.
2674 Target_i386::relocate_for_relocatable(
2675 const Relocate_info
<32, false>* relinfo
,
2676 unsigned int sh_type
,
2677 const unsigned char* prelocs
,
2679 Output_section
* output_section
,
2680 off_t offset_in_output_section
,
2681 const Relocatable_relocs
* rr
,
2682 unsigned char* view
,
2683 elfcpp::Elf_types
<32>::Elf_Addr view_address
,
2684 section_size_type view_size
,
2685 unsigned char* reloc_view
,
2686 section_size_type reloc_view_size
)
2688 gold_assert(sh_type
== elfcpp::SHT_REL
);
2690 gold::relocate_for_relocatable
<32, false, elfcpp::SHT_REL
>(
2695 offset_in_output_section
,
2704 // Return the value to use for a dynamic which requires special
2705 // treatment. This is how we support equality comparisons of function
2706 // pointers across shared library boundaries, as described in the
2707 // processor specific ABI supplement.
2710 Target_i386::do_dynsym_value(const Symbol
* gsym
) const
2712 gold_assert(gsym
->is_from_dynobj() && gsym
->has_plt_offset());
2713 return this->plt_section()->address() + gsym
->plt_offset();
2716 // Return a string used to fill a code section with nops to take up
2717 // the specified length.
2720 Target_i386::do_code_fill(section_size_type length
) const
2724 // Build a jmp instruction to skip over the bytes.
2725 unsigned char jmp
[5];
2727 elfcpp::Swap_unaligned
<32, false>::writeval(jmp
+ 1, length
- 5);
2728 return (std::string(reinterpret_cast<char*>(&jmp
[0]), 5)
2729 + std::string(length
- 5, '\0'));
2732 // Nop sequences of various lengths.
2733 const char nop1
[1] = { 0x90 }; // nop
2734 const char nop2
[2] = { 0x66, 0x90 }; // xchg %ax %ax
2735 const char nop3
[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2736 const char nop4
[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2737 const char nop5
[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2738 0x00 }; // leal 0(%esi,1),%esi
2739 const char nop6
[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2741 const char nop7
[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2743 const char nop8
[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2744 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2745 const char nop9
[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2746 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2748 const char nop10
[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2749 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2751 const char nop11
[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2752 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2754 const char nop12
[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2755 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2756 0x00, 0x00, 0x00, 0x00 };
2757 const char nop13
[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2758 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2759 0x27, 0x00, 0x00, 0x00,
2761 const char nop14
[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2762 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2763 0xbc, 0x27, 0x00, 0x00,
2765 const char nop15
[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2766 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2767 0x90, 0x90, 0x90, 0x90,
2770 const char* nops
[16] = {
2772 nop1
, nop2
, nop3
, nop4
, nop5
, nop6
, nop7
,
2773 nop8
, nop9
, nop10
, nop11
, nop12
, nop13
, nop14
, nop15
2776 return std::string(nops
[length
], length
);
2779 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2780 // compiled with -fstack-split. The function calls non-stack-split
2781 // code. We have to change the function so that it always ensures
2782 // that it has enough stack space to run some random function.
2785 Target_i386::do_calls_non_split(Relobj
* object
, unsigned int shndx
,
2786 section_offset_type fnoffset
,
2787 section_size_type fnsize
,
2788 unsigned char* view
,
2789 section_size_type view_size
,
2791 std::string
* to
) const
2793 // The function starts with a comparison of the stack pointer and a
2794 // field in the TCB. This is followed by a jump.
2797 if (this->match_view(view
, view_size
, fnoffset
, "\x65\x3b\x25", 3)
2800 // We will call __morestack if the carry flag is set after this
2801 // comparison. We turn the comparison into an stc instruction
2803 view
[fnoffset
] = '\xf9';
2804 this->set_view_to_nop(view
, view_size
, fnoffset
+ 1, 6);
2806 // lea NN(%esp),%ecx
2807 // lea NN(%esp),%edx
2808 else if ((this->match_view(view
, view_size
, fnoffset
, "\x8d\x8c\x24", 3)
2809 || this->match_view(view
, view_size
, fnoffset
, "\x8d\x94\x24", 3))
2812 // This is loading an offset from the stack pointer for a
2813 // comparison. The offset is negative, so we decrease the
2814 // offset by the amount of space we need for the stack. This
2815 // means we will avoid calling __morestack if there happens to
2816 // be plenty of space on the stack already.
2817 unsigned char* pval
= view
+ fnoffset
+ 3;
2818 uint32_t val
= elfcpp::Swap_unaligned
<32, false>::readval(pval
);
2819 val
-= parameters
->options().split_stack_adjust_size();
2820 elfcpp::Swap_unaligned
<32, false>::writeval(pval
, val
);
2824 if (!object
->has_no_split_stack())
2825 object
->error(_("failed to match split-stack sequence at "
2826 "section %u offset %0zx"),
2827 shndx
, static_cast<size_t>(fnoffset
));
2831 // We have to change the function so that it calls
2832 // __morestack_non_split instead of __morestack. The former will
2833 // allocate additional stack space.
2834 *from
= "__morestack";
2835 *to
= "__morestack_non_split";
2838 // The selector for i386 object files.
2840 class Target_selector_i386
: public Target_selector_freebsd
2843 Target_selector_i386()
2844 : Target_selector_freebsd(elfcpp::EM_386
, 32, false,
2845 "elf32-i386", "elf32-i386-freebsd")
2849 do_instantiate_target()
2850 { return new Target_i386(); }
2853 Target_selector_i386 target_selector_i386
;
2855 } // End anonymous namespace.