PR binutils/13534
[binutils.git] / gold / i386.cc
blobefb6248de1befd3219c1be54811efb260ed7893f
1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009, 2010, 2011 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.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "dwarf.h"
29 #include "parameters.h"
30 #include "reloc.h"
31 #include "i386.h"
32 #include "object.h"
33 #include "symtab.h"
34 #include "layout.h"
35 #include "output.h"
36 #include "copy-relocs.h"
37 #include "target.h"
38 #include "target-reloc.h"
39 #include "target-select.h"
40 #include "tls.h"
41 #include "freebsd.h"
42 #include "gc.h"
44 namespace
47 using namespace gold;
49 // A class to handle the PLT data.
51 class Output_data_plt_i386 : public Output_section_data
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
56 Output_data_plt_i386(Layout*, Output_data_space*, Output_data_space*);
58 // Add an entry to the PLT.
59 void
60 add_entry(Symbol_table*, Layout*, Symbol* gsym);
62 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
63 unsigned int
64 add_local_ifunc_entry(Symbol_table*, Layout*,
65 Sized_relobj_file<32, false>* relobj,
66 unsigned int local_sym_index);
68 // Return the .rel.plt section data.
69 Reloc_section*
70 rel_plt() const
71 { return this->rel_; }
73 // Return where the TLS_DESC relocations should go.
74 Reloc_section*
75 rel_tls_desc(Layout*);
77 // Return where the IRELATIVE relocations should go.
78 Reloc_section*
79 rel_irelative(Symbol_table*, Layout*);
81 // Return whether we created a section for IRELATIVE relocations.
82 bool
83 has_irelative_section() const
84 { return this->irelative_rel_ != NULL; }
86 // Return the number of PLT entries.
87 unsigned int
88 entry_count() const
89 { return this->count_ + this->irelative_count_; }
91 // Return the offset of the first non-reserved PLT entry.
92 static unsigned int
93 first_plt_entry_offset()
94 { return plt_entry_size; }
96 // Return the size of a PLT entry.
97 static unsigned int
98 get_plt_entry_size()
99 { return plt_entry_size; }
101 // Return the PLT address to use for a global symbol.
102 uint64_t
103 address_for_global(const Symbol*);
105 // Return the PLT address to use for a local symbol.
106 uint64_t
107 address_for_local(const Relobj*, unsigned int symndx);
109 protected:
110 void
111 do_adjust_output_section(Output_section* os);
113 // Write to a map file.
114 void
115 do_print_to_mapfile(Mapfile* mapfile) const
116 { mapfile->print_output_data(this, _("** PLT")); }
118 private:
119 // The size of an entry in the PLT.
120 static const int plt_entry_size = 16;
122 // The first entry in the PLT for an executable.
123 static const unsigned char exec_first_plt_entry[plt_entry_size];
125 // The first entry in the PLT for a shared object.
126 static const unsigned char dyn_first_plt_entry[plt_entry_size];
128 // Other entries in the PLT for an executable.
129 static const unsigned char exec_plt_entry[plt_entry_size];
131 // Other entries in the PLT for a shared object.
132 static const unsigned char dyn_plt_entry[plt_entry_size];
134 // The .eh_frame unwind information for the PLT.
135 static const int plt_eh_frame_cie_size = 16;
136 static const int plt_eh_frame_fde_size = 32;
137 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
138 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
140 // Set the final size.
141 void
142 set_final_data_size()
144 this->set_data_size((this->count_ + this->irelative_count_ + 1)
145 * plt_entry_size);
148 // Write out the PLT data.
149 void
150 do_write(Output_file*);
152 // We keep a list of global STT_GNU_IFUNC symbols, each with its
153 // offset in the GOT.
154 struct Global_ifunc
156 Symbol* sym;
157 unsigned int got_offset;
160 // We keep a list of local STT_GNU_IFUNC symbols, each with its
161 // offset in the GOT.
162 struct Local_ifunc
164 Sized_relobj_file<32, false>* object;
165 unsigned int local_sym_index;
166 unsigned int got_offset;
169 // A pointer to the Layout class, so that we can find the .dynamic
170 // section when we write out the GOT PLT section.
171 Layout* layout_;
172 // The reloc section.
173 Reloc_section* rel_;
174 // The TLS_DESC relocations, if necessary. These must follow the
175 // regular PLT relocs.
176 Reloc_section* tls_desc_rel_;
177 // The IRELATIVE relocations, if necessary. These must follow the
178 // regular relocatoins and the TLS_DESC relocations.
179 Reloc_section* irelative_rel_;
180 // The .got.plt section.
181 Output_data_space* got_plt_;
182 // The part of the .got.plt section used for IRELATIVE relocs.
183 Output_data_space* got_irelative_;
184 // The number of PLT entries.
185 unsigned int count_;
186 // Number of PLT entries with R_386_IRELATIVE relocs. These follow
187 // the regular PLT entries.
188 unsigned int irelative_count_;
189 // Global STT_GNU_IFUNC symbols.
190 std::vector<Global_ifunc> global_ifuncs_;
191 // Local STT_GNU_IFUNC symbols.
192 std::vector<Local_ifunc> local_ifuncs_;
195 // The i386 target class.
196 // TLS info comes from
197 // http://people.redhat.com/drepper/tls.pdf
198 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
200 class Target_i386 : public Sized_target<32, false>
202 public:
203 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
205 Target_i386()
206 : Sized_target<32, false>(&i386_info),
207 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
208 got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
209 rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
210 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
213 // Process the relocations to determine unreferenced sections for
214 // garbage collection.
215 void
216 gc_process_relocs(Symbol_table* symtab,
217 Layout* layout,
218 Sized_relobj_file<32, false>* object,
219 unsigned int data_shndx,
220 unsigned int sh_type,
221 const unsigned char* prelocs,
222 size_t reloc_count,
223 Output_section* output_section,
224 bool needs_special_offset_handling,
225 size_t local_symbol_count,
226 const unsigned char* plocal_symbols);
228 // Scan the relocations to look for symbol adjustments.
229 void
230 scan_relocs(Symbol_table* symtab,
231 Layout* layout,
232 Sized_relobj_file<32, false>* object,
233 unsigned int data_shndx,
234 unsigned int sh_type,
235 const unsigned char* prelocs,
236 size_t reloc_count,
237 Output_section* output_section,
238 bool needs_special_offset_handling,
239 size_t local_symbol_count,
240 const unsigned char* plocal_symbols);
242 // Finalize the sections.
243 void
244 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
246 // Return the value to use for a dynamic which requires special
247 // treatment.
248 uint64_t
249 do_dynsym_value(const Symbol*) const;
251 // Relocate a section.
252 void
253 relocate_section(const Relocate_info<32, false>*,
254 unsigned int sh_type,
255 const unsigned char* prelocs,
256 size_t reloc_count,
257 Output_section* output_section,
258 bool needs_special_offset_handling,
259 unsigned char* view,
260 elfcpp::Elf_types<32>::Elf_Addr view_address,
261 section_size_type view_size,
262 const Reloc_symbol_changes*);
264 // Scan the relocs during a relocatable link.
265 void
266 scan_relocatable_relocs(Symbol_table* symtab,
267 Layout* layout,
268 Sized_relobj_file<32, false>* object,
269 unsigned int data_shndx,
270 unsigned int sh_type,
271 const unsigned char* prelocs,
272 size_t reloc_count,
273 Output_section* output_section,
274 bool needs_special_offset_handling,
275 size_t local_symbol_count,
276 const unsigned char* plocal_symbols,
277 Relocatable_relocs*);
279 // Relocate a section during a relocatable link.
280 void
281 relocate_for_relocatable(const Relocate_info<32, false>*,
282 unsigned int sh_type,
283 const unsigned char* prelocs,
284 size_t reloc_count,
285 Output_section* output_section,
286 off_t offset_in_output_section,
287 const Relocatable_relocs*,
288 unsigned char* view,
289 elfcpp::Elf_types<32>::Elf_Addr view_address,
290 section_size_type view_size,
291 unsigned char* reloc_view,
292 section_size_type reloc_view_size);
294 // Return a string used to fill a code section with nops.
295 std::string
296 do_code_fill(section_size_type length) const;
298 // Return whether SYM is defined by the ABI.
299 bool
300 do_is_defined_by_abi(const Symbol* sym) const
301 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
303 // Return whether a symbol name implies a local label. The UnixWare
304 // 2.1 cc generates temporary symbols that start with .X, so we
305 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
306 // If so, we should move the .X recognition into
307 // Target::do_is_local_label_name.
308 bool
309 do_is_local_label_name(const char* name) const
311 if (name[0] == '.' && name[1] == 'X')
312 return true;
313 return Target::do_is_local_label_name(name);
316 // Return the PLT address to use for a global symbol.
317 uint64_t
318 do_plt_address_for_global(const Symbol* gsym) const
319 { return this->plt_section()->address_for_global(gsym); }
321 uint64_t
322 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
323 { return this->plt_section()->address_for_local(relobj, symndx); }
325 // We can tell whether we take the address of a function.
326 inline bool
327 do_can_check_for_function_pointers() const
328 { return true; }
330 // Return the base for a DW_EH_PE_datarel encoding.
331 uint64_t
332 do_ehframe_datarel_base() const;
334 // Return whether SYM is call to a non-split function.
335 bool
336 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
338 // Adjust -fsplit-stack code which calls non-split-stack code.
339 void
340 do_calls_non_split(Relobj* object, unsigned int shndx,
341 section_offset_type fnoffset, section_size_type fnsize,
342 unsigned char* view, section_size_type view_size,
343 std::string* from, std::string* to) const;
345 // Return the size of the GOT section.
346 section_size_type
347 got_size() const
349 gold_assert(this->got_ != NULL);
350 return this->got_->data_size();
353 // Return the number of entries in the GOT.
354 unsigned int
355 got_entry_count() const
357 if (this->got_ == NULL)
358 return 0;
359 return this->got_size() / 4;
362 // Return the number of entries in the PLT.
363 unsigned int
364 plt_entry_count() const;
366 // Return the offset of the first non-reserved PLT entry.
367 unsigned int
368 first_plt_entry_offset() const;
370 // Return the size of each PLT entry.
371 unsigned int
372 plt_entry_size() const;
374 private:
375 // The class which scans relocations.
376 struct Scan
378 static inline int
380 get_reference_flags(unsigned int r_type);
382 inline void
383 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
384 Sized_relobj_file<32, false>* object,
385 unsigned int data_shndx,
386 Output_section* output_section,
387 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
388 const elfcpp::Sym<32, false>& lsym);
390 inline void
391 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
392 Sized_relobj_file<32, false>* object,
393 unsigned int data_shndx,
394 Output_section* output_section,
395 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
396 Symbol* gsym);
398 inline bool
399 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
400 Target_i386* target,
401 Sized_relobj_file<32, false>* object,
402 unsigned int data_shndx,
403 Output_section* output_section,
404 const elfcpp::Rel<32, false>& reloc,
405 unsigned int r_type,
406 const elfcpp::Sym<32, false>& lsym);
408 inline bool
409 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
410 Target_i386* target,
411 Sized_relobj_file<32, false>* object,
412 unsigned int data_shndx,
413 Output_section* output_section,
414 const elfcpp::Rel<32, false>& reloc,
415 unsigned int r_type,
416 Symbol* gsym);
418 inline bool
419 possible_function_pointer_reloc(unsigned int r_type);
421 bool
422 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
423 unsigned int r_type);
425 static void
426 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
428 static void
429 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
430 Symbol*);
433 // The class which implements relocation.
434 class Relocate
436 public:
437 Relocate()
438 : skip_call_tls_get_addr_(false),
439 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
442 ~Relocate()
444 if (this->skip_call_tls_get_addr_)
446 // FIXME: This needs to specify the location somehow.
447 gold_error(_("missing expected TLS relocation"));
451 // Return whether the static relocation needs to be applied.
452 inline bool
453 should_apply_static_reloc(const Sized_symbol<32>* gsym,
454 unsigned int r_type,
455 bool is_32bit,
456 Output_section* output_section);
458 // Do a relocation. Return false if the caller should not issue
459 // any warnings about this relocation.
460 inline bool
461 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
462 size_t relnum, const elfcpp::Rel<32, false>&,
463 unsigned int r_type, const Sized_symbol<32>*,
464 const Symbol_value<32>*,
465 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
466 section_size_type);
468 private:
469 // Do a TLS relocation.
470 inline void
471 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
472 size_t relnum, const elfcpp::Rel<32, false>&,
473 unsigned int r_type, const Sized_symbol<32>*,
474 const Symbol_value<32>*,
475 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
476 section_size_type);
478 // Do a TLS General-Dynamic to Initial-Exec transition.
479 inline void
480 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
481 Output_segment* tls_segment,
482 const elfcpp::Rel<32, false>&, unsigned int r_type,
483 elfcpp::Elf_types<32>::Elf_Addr value,
484 unsigned char* view,
485 section_size_type view_size);
487 // Do a TLS General-Dynamic to Local-Exec transition.
488 inline void
489 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
490 Output_segment* tls_segment,
491 const elfcpp::Rel<32, false>&, unsigned int r_type,
492 elfcpp::Elf_types<32>::Elf_Addr value,
493 unsigned char* view,
494 section_size_type view_size);
496 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
497 // transition.
498 inline void
499 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
500 Output_segment* tls_segment,
501 const elfcpp::Rel<32, false>&, unsigned int r_type,
502 elfcpp::Elf_types<32>::Elf_Addr value,
503 unsigned char* view,
504 section_size_type view_size);
506 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
507 // transition.
508 inline void
509 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
510 Output_segment* tls_segment,
511 const elfcpp::Rel<32, false>&, unsigned int r_type,
512 elfcpp::Elf_types<32>::Elf_Addr value,
513 unsigned char* view,
514 section_size_type view_size);
516 // Do a TLS Local-Dynamic to Local-Exec transition.
517 inline void
518 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
519 Output_segment* tls_segment,
520 const elfcpp::Rel<32, false>&, unsigned int r_type,
521 elfcpp::Elf_types<32>::Elf_Addr value,
522 unsigned char* view,
523 section_size_type view_size);
525 // Do a TLS Initial-Exec to Local-Exec transition.
526 static inline void
527 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
528 Output_segment* tls_segment,
529 const elfcpp::Rel<32, false>&, unsigned int r_type,
530 elfcpp::Elf_types<32>::Elf_Addr value,
531 unsigned char* view,
532 section_size_type view_size);
534 // We need to keep track of which type of local dynamic relocation
535 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
536 enum Local_dynamic_type
538 LOCAL_DYNAMIC_NONE,
539 LOCAL_DYNAMIC_SUN,
540 LOCAL_DYNAMIC_GNU
543 // This is set if we should skip the next reloc, which should be a
544 // PLT32 reloc against ___tls_get_addr.
545 bool skip_call_tls_get_addr_;
546 // The type of local dynamic relocation we have seen in the section
547 // being relocated, if any.
548 Local_dynamic_type local_dynamic_type_;
551 // A class which returns the size required for a relocation type,
552 // used while scanning relocs during a relocatable link.
553 class Relocatable_size_for_reloc
555 public:
556 unsigned int
557 get_size_for_reloc(unsigned int, Relobj*);
560 // Adjust TLS relocation type based on the options and whether this
561 // is a local symbol.
562 static tls::Tls_optimization
563 optimize_tls_reloc(bool is_final, int r_type);
565 // Get the GOT section, creating it if necessary.
566 Output_data_got<32, false>*
567 got_section(Symbol_table*, Layout*);
569 // Get the GOT PLT section.
570 Output_data_space*
571 got_plt_section() const
573 gold_assert(this->got_plt_ != NULL);
574 return this->got_plt_;
577 // Get the GOT section for TLSDESC entries.
578 Output_data_got<32, false>*
579 got_tlsdesc_section() const
581 gold_assert(this->got_tlsdesc_ != NULL);
582 return this->got_tlsdesc_;
585 // Create the PLT section.
586 void
587 make_plt_section(Symbol_table* symtab, Layout* layout);
589 // Create a PLT entry for a global symbol.
590 void
591 make_plt_entry(Symbol_table*, Layout*, Symbol*);
593 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
594 void
595 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
596 Sized_relobj_file<32, false>* relobj,
597 unsigned int local_sym_index);
599 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
600 void
601 define_tls_base_symbol(Symbol_table*, Layout*);
603 // Create a GOT entry for the TLS module index.
604 unsigned int
605 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
606 Sized_relobj_file<32, false>* object);
608 // Get the PLT section.
609 Output_data_plt_i386*
610 plt_section() const
612 gold_assert(this->plt_ != NULL);
613 return this->plt_;
616 // Get the dynamic reloc section, creating it if necessary.
617 Reloc_section*
618 rel_dyn_section(Layout*);
620 // Get the section to use for TLS_DESC relocations.
621 Reloc_section*
622 rel_tls_desc_section(Layout*) const;
624 // Get the section to use for IRELATIVE relocations.
625 Reloc_section*
626 rel_irelative_section(Layout*);
628 // Add a potential copy relocation.
629 void
630 copy_reloc(Symbol_table* symtab, Layout* layout,
631 Sized_relobj_file<32, false>* object,
632 unsigned int shndx, Output_section* output_section,
633 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
635 this->copy_relocs_.copy_reloc(symtab, layout,
636 symtab->get_sized_symbol<32>(sym),
637 object, shndx, output_section, reloc,
638 this->rel_dyn_section(layout));
641 // Information about this specific target which we pass to the
642 // general Target structure.
643 static const Target::Target_info i386_info;
645 // The types of GOT entries needed for this platform.
646 // These values are exposed to the ABI in an incremental link.
647 // Do not renumber existing values without changing the version
648 // number of the .gnu_incremental_inputs section.
649 enum Got_type
651 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
652 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
653 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
654 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
655 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
658 // The GOT section.
659 Output_data_got<32, false>* got_;
660 // The PLT section.
661 Output_data_plt_i386* plt_;
662 // The GOT PLT section.
663 Output_data_space* got_plt_;
664 // The GOT section for IRELATIVE relocations.
665 Output_data_space* got_irelative_;
666 // The GOT section for TLSDESC relocations.
667 Output_data_got<32, false>* got_tlsdesc_;
668 // The _GLOBAL_OFFSET_TABLE_ symbol.
669 Symbol* global_offset_table_;
670 // The dynamic reloc section.
671 Reloc_section* rel_dyn_;
672 // The section to use for IRELATIVE relocs.
673 Reloc_section* rel_irelative_;
674 // Relocs saved to avoid a COPY reloc.
675 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
676 // Space for variables copied with a COPY reloc.
677 Output_data_space* dynbss_;
678 // Offset of the GOT entry for the TLS module index.
679 unsigned int got_mod_index_offset_;
680 // True if the _TLS_MODULE_BASE_ symbol has been defined.
681 bool tls_base_symbol_defined_;
684 const Target::Target_info Target_i386::i386_info =
686 32, // size
687 false, // is_big_endian
688 elfcpp::EM_386, // machine_code
689 false, // has_make_symbol
690 false, // has_resolve
691 true, // has_code_fill
692 true, // is_default_stack_executable
693 true, // can_icf_inline_merge_sections
694 '\0', // wrap_char
695 "/usr/lib/libc.so.1", // dynamic_linker
696 0x08048000, // default_text_segment_address
697 0x1000, // abi_pagesize (overridable by -z max-page-size)
698 0x1000, // common_pagesize (overridable by -z common-page-size)
699 elfcpp::SHN_UNDEF, // small_common_shndx
700 elfcpp::SHN_UNDEF, // large_common_shndx
701 0, // small_common_section_flags
702 0, // large_common_section_flags
703 NULL, // attributes_section
704 NULL // attributes_vendor
707 // Get the GOT section, creating it if necessary.
709 Output_data_got<32, false>*
710 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
712 if (this->got_ == NULL)
714 gold_assert(symtab != NULL && layout != NULL);
716 this->got_ = new Output_data_got<32, false>();
718 // When using -z now, we can treat .got.plt as a relro section.
719 // Without -z now, it is modified after program startup by lazy
720 // PLT relocations.
721 bool is_got_plt_relro = parameters->options().now();
722 Output_section_order got_order = (is_got_plt_relro
723 ? ORDER_RELRO
724 : ORDER_RELRO_LAST);
725 Output_section_order got_plt_order = (is_got_plt_relro
726 ? ORDER_RELRO
727 : ORDER_NON_RELRO_FIRST);
729 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
730 (elfcpp::SHF_ALLOC
731 | elfcpp::SHF_WRITE),
732 this->got_, got_order, true);
734 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
735 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
736 (elfcpp::SHF_ALLOC
737 | elfcpp::SHF_WRITE),
738 this->got_plt_, got_plt_order,
739 is_got_plt_relro);
741 // The first three entries are reserved.
742 this->got_plt_->set_current_data_size(3 * 4);
744 if (!is_got_plt_relro)
746 // Those bytes can go into the relro segment.
747 layout->increase_relro(3 * 4);
750 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
751 this->global_offset_table_ =
752 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
753 Symbol_table::PREDEFINED,
754 this->got_plt_,
755 0, 0, elfcpp::STT_OBJECT,
756 elfcpp::STB_LOCAL,
757 elfcpp::STV_HIDDEN, 0,
758 false, false);
760 // If there are any IRELATIVE relocations, they get GOT entries
761 // in .got.plt after the jump slot relocations.
762 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
763 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
764 (elfcpp::SHF_ALLOC
765 | elfcpp::SHF_WRITE),
766 this->got_irelative_,
767 got_plt_order, is_got_plt_relro);
769 // If there are any TLSDESC relocations, they get GOT entries in
770 // .got.plt after the jump slot entries.
771 this->got_tlsdesc_ = new Output_data_got<32, false>();
772 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
773 (elfcpp::SHF_ALLOC
774 | elfcpp::SHF_WRITE),
775 this->got_tlsdesc_,
776 got_plt_order, is_got_plt_relro);
779 return this->got_;
782 // Get the dynamic reloc section, creating it if necessary.
784 Target_i386::Reloc_section*
785 Target_i386::rel_dyn_section(Layout* layout)
787 if (this->rel_dyn_ == NULL)
789 gold_assert(layout != NULL);
790 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
791 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
792 elfcpp::SHF_ALLOC, this->rel_dyn_,
793 ORDER_DYNAMIC_RELOCS, false);
795 return this->rel_dyn_;
798 // Get the section to use for IRELATIVE relocs, creating it if
799 // necessary. These go in .rel.dyn, but only after all other dynamic
800 // relocations. They need to follow the other dynamic relocations so
801 // that they can refer to global variables initialized by those
802 // relocs.
804 Target_i386::Reloc_section*
805 Target_i386::rel_irelative_section(Layout* layout)
807 if (this->rel_irelative_ == NULL)
809 // Make sure we have already create the dynamic reloc section.
810 this->rel_dyn_section(layout);
811 this->rel_irelative_ = new Reloc_section(false);
812 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
813 elfcpp::SHF_ALLOC, this->rel_irelative_,
814 ORDER_DYNAMIC_RELOCS, false);
815 gold_assert(this->rel_dyn_->output_section()
816 == this->rel_irelative_->output_section());
818 return this->rel_irelative_;
821 // Create the PLT section. The ordinary .got section is an argument,
822 // since we need to refer to the start. We also create our own .got
823 // section just for PLT entries.
825 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
826 Output_data_space* got_plt,
827 Output_data_space* got_irelative)
828 : Output_section_data(16), layout_(layout), tls_desc_rel_(NULL),
829 irelative_rel_(NULL), got_plt_(got_plt), got_irelative_(got_irelative),
830 count_(0), irelative_count_(0), global_ifuncs_(), local_ifuncs_()
832 this->rel_ = new Reloc_section(false);
833 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
834 elfcpp::SHF_ALLOC, this->rel_,
835 ORDER_DYNAMIC_PLT_RELOCS, false);
837 // Add unwind information if requested.
838 if (parameters->options().ld_generated_unwind_info())
839 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
840 plt_eh_frame_fde, plt_eh_frame_fde_size);
843 void
844 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
846 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
847 // linker, and so do we.
848 os->set_entsize(4);
851 // Add an entry to the PLT.
853 void
854 Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
855 Symbol* gsym)
857 gold_assert(!gsym->has_plt_offset());
859 // Every PLT entry needs a reloc.
860 if (gsym->type() == elfcpp::STT_GNU_IFUNC
861 && gsym->can_use_relative_reloc(false))
863 gsym->set_plt_offset(this->irelative_count_ * plt_entry_size);
864 ++this->irelative_count_;
865 section_offset_type got_offset =
866 this->got_irelative_->current_data_size();
867 this->got_irelative_->set_current_data_size(got_offset + 4);
868 Reloc_section* rel = this->rel_irelative(symtab, layout);
869 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
870 this->got_irelative_, got_offset);
871 struct Global_ifunc gi;
872 gi.sym = gsym;
873 gi.got_offset = got_offset;
874 this->global_ifuncs_.push_back(gi);
876 else
878 // When setting the PLT offset we skip the initial reserved PLT
879 // entry.
880 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
882 ++this->count_;
884 section_offset_type got_offset = this->got_plt_->current_data_size();
886 // Every PLT entry needs a GOT entry which points back to the
887 // PLT entry (this will be changed by the dynamic linker,
888 // normally lazily when the function is called).
889 this->got_plt_->set_current_data_size(got_offset + 4);
891 gsym->set_needs_dynsym_entry();
892 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
893 got_offset);
896 // Note that we don't need to save the symbol. The contents of the
897 // PLT are independent of which symbols are used. The symbols only
898 // appear in the relocations.
901 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
902 // the PLT offset.
904 unsigned int
905 Output_data_plt_i386::add_local_ifunc_entry(
906 Symbol_table* symtab,
907 Layout* layout,
908 Sized_relobj_file<32, false>* relobj,
909 unsigned int local_sym_index)
911 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
912 ++this->irelative_count_;
914 section_offset_type got_offset = this->got_irelative_->current_data_size();
916 // Every PLT entry needs a GOT entry which points back to the PLT
917 // entry.
918 this->got_irelative_->set_current_data_size(got_offset + 4);
920 // Every PLT entry needs a reloc.
921 Reloc_section* rel = this->rel_irelative(symtab, layout);
922 rel->add_symbolless_local_addend(relobj, local_sym_index,
923 elfcpp::R_386_IRELATIVE,
924 this->got_irelative_, got_offset);
926 struct Local_ifunc li;
927 li.object = relobj;
928 li.local_sym_index = local_sym_index;
929 li.got_offset = got_offset;
930 this->local_ifuncs_.push_back(li);
932 return plt_offset;
935 // Return where the TLS_DESC relocations should go, creating it if
936 // necessary. These follow the JUMP_SLOT relocations.
938 Output_data_plt_i386::Reloc_section*
939 Output_data_plt_i386::rel_tls_desc(Layout* layout)
941 if (this->tls_desc_rel_ == NULL)
943 this->tls_desc_rel_ = new Reloc_section(false);
944 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
945 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
946 ORDER_DYNAMIC_PLT_RELOCS, false);
947 gold_assert(this->tls_desc_rel_->output_section()
948 == this->rel_->output_section());
950 return this->tls_desc_rel_;
953 // Return where the IRELATIVE relocations should go in the PLT. These
954 // follow the JUMP_SLOT and TLS_DESC relocations.
956 Output_data_plt_i386::Reloc_section*
957 Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
959 if (this->irelative_rel_ == NULL)
961 // Make sure we have a place for the TLS_DESC relocations, in
962 // case we see any later on.
963 this->rel_tls_desc(layout);
964 this->irelative_rel_ = new Reloc_section(false);
965 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
966 elfcpp::SHF_ALLOC, this->irelative_rel_,
967 ORDER_DYNAMIC_PLT_RELOCS, false);
968 gold_assert(this->irelative_rel_->output_section()
969 == this->rel_->output_section());
971 if (parameters->doing_static_link())
973 // A statically linked executable will only have a .rel.plt
974 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
975 // symbols. The library will use these symbols to locate
976 // the IRELATIVE relocs at program startup time.
977 symtab->define_in_output_data("__rel_iplt_start", NULL,
978 Symbol_table::PREDEFINED,
979 this->irelative_rel_, 0, 0,
980 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
981 elfcpp::STV_HIDDEN, 0, false, true);
982 symtab->define_in_output_data("__rel_iplt_end", NULL,
983 Symbol_table::PREDEFINED,
984 this->irelative_rel_, 0, 0,
985 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
986 elfcpp::STV_HIDDEN, 0, true, true);
989 return this->irelative_rel_;
992 // Return the PLT address to use for a global symbol.
994 uint64_t
995 Output_data_plt_i386::address_for_global(const Symbol* gsym)
997 uint64_t offset = 0;
998 if (gsym->type() == elfcpp::STT_GNU_IFUNC
999 && gsym->can_use_relative_reloc(false))
1000 offset = (this->count_ + 1) * plt_entry_size;
1001 return this->address() + offset;
1004 // Return the PLT address to use for a local symbol. These are always
1005 // IRELATIVE relocs.
1007 uint64_t
1008 Output_data_plt_i386::address_for_local(const Relobj*, unsigned int)
1010 return this->address() + (this->count_ + 1) * plt_entry_size;
1013 // The first entry in the PLT for an executable.
1015 const unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
1017 0xff, 0x35, // pushl contents of memory address
1018 0, 0, 0, 0, // replaced with address of .got + 4
1019 0xff, 0x25, // jmp indirect
1020 0, 0, 0, 0, // replaced with address of .got + 8
1021 0, 0, 0, 0 // unused
1024 // The first entry in the PLT for a shared object.
1026 const unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
1028 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1029 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1030 0, 0, 0, 0 // unused
1033 // Subsequent entries in the PLT for an executable.
1035 const unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
1037 0xff, 0x25, // jmp indirect
1038 0, 0, 0, 0, // replaced with address of symbol in .got
1039 0x68, // pushl immediate
1040 0, 0, 0, 0, // replaced with offset into relocation table
1041 0xe9, // jmp relative
1042 0, 0, 0, 0 // replaced with offset to start of .plt
1045 // Subsequent entries in the PLT for a shared object.
1047 const unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
1049 0xff, 0xa3, // jmp *offset(%ebx)
1050 0, 0, 0, 0, // replaced with offset of symbol in .got
1051 0x68, // pushl immediate
1052 0, 0, 0, 0, // replaced with offset into relocation table
1053 0xe9, // jmp relative
1054 0, 0, 0, 0 // replaced with offset to start of .plt
1057 // The .eh_frame unwind information for the PLT.
1059 const unsigned char
1060 Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1062 1, // CIE version.
1063 'z', // Augmentation: augmentation size included.
1064 'R', // Augmentation: FDE encoding included.
1065 '\0', // End of augmentation string.
1066 1, // Code alignment factor.
1067 0x7c, // Data alignment factor.
1068 8, // Return address column.
1069 1, // Augmentation size.
1070 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1071 | elfcpp::DW_EH_PE_sdata4),
1072 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1073 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1074 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1075 elfcpp::DW_CFA_nop
1078 const unsigned char
1079 Output_data_plt_i386::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1081 0, 0, 0, 0, // Replaced with offset to .plt.
1082 0, 0, 0, 0, // Replaced with size of .plt.
1083 0, // Augmentation size.
1084 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
1085 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1086 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
1087 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1088 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1089 11, // Block length.
1090 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
1091 elfcpp::DW_OP_breg8, 0, // Push %eip.
1092 elfcpp::DW_OP_lit15, // Push 0xf.
1093 elfcpp::DW_OP_and, // & (%eip & 0xf).
1094 elfcpp::DW_OP_lit11, // Push 0xb.
1095 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
1096 elfcpp::DW_OP_lit2, // Push 2.
1097 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
1098 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
1099 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1100 elfcpp::DW_CFA_nop,
1101 elfcpp::DW_CFA_nop,
1102 elfcpp::DW_CFA_nop
1105 // Write out the PLT. This uses the hand-coded instructions above,
1106 // and adjusts them as needed. This is all specified by the i386 ELF
1107 // Processor Supplement.
1109 void
1110 Output_data_plt_i386::do_write(Output_file* of)
1112 const off_t offset = this->offset();
1113 const section_size_type oview_size =
1114 convert_to_section_size_type(this->data_size());
1115 unsigned char* const oview = of->get_output_view(offset, oview_size);
1117 const off_t got_file_offset = this->got_plt_->offset();
1118 gold_assert(parameters->incremental_update()
1119 || (got_file_offset + this->got_plt_->data_size()
1120 == this->got_irelative_->offset()));
1121 const section_size_type got_size =
1122 convert_to_section_size_type(this->got_plt_->data_size()
1123 + this->got_irelative_->data_size());
1124 unsigned char* const got_view = of->get_output_view(got_file_offset,
1125 got_size);
1127 unsigned char* pov = oview;
1129 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1130 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1132 if (parameters->options().output_is_position_independent())
1133 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
1134 else
1136 memcpy(pov, exec_first_plt_entry, plt_entry_size);
1137 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1138 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1140 pov += plt_entry_size;
1142 unsigned char* got_pov = got_view;
1144 // The first entry in the GOT is the address of the .dynamic section
1145 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1146 // We saved space for them when we created the section in
1147 // Target_i386::got_section.
1148 Output_section* dynamic = this->layout_->dynamic_section();
1149 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1150 elfcpp::Swap<32, false>::writeval(got_pov, dynamic_addr);
1151 got_pov += 4;
1152 memset(got_pov, 0, 8);
1153 got_pov += 8;
1155 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1157 unsigned int plt_offset = plt_entry_size;
1158 unsigned int plt_rel_offset = 0;
1159 unsigned int got_offset = 12;
1160 const unsigned int count = this->count_ + this->irelative_count_;
1161 for (unsigned int i = 0;
1162 i < count;
1163 ++i,
1164 pov += plt_entry_size,
1165 got_pov += 4,
1166 plt_offset += plt_entry_size,
1167 plt_rel_offset += rel_size,
1168 got_offset += 4)
1170 // Set and adjust the PLT entry itself.
1172 if (parameters->options().output_is_position_independent())
1174 memcpy(pov, dyn_plt_entry, plt_entry_size);
1175 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1177 else
1179 memcpy(pov, exec_plt_entry, plt_entry_size);
1180 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1181 (got_address
1182 + got_offset));
1185 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1186 elfcpp::Swap<32, false>::writeval(pov + 12,
1187 - (plt_offset + plt_entry_size));
1189 // Set the entry in the GOT.
1190 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
1193 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1194 // the GOT to point to the actual symbol value, rather than point to
1195 // the PLT entry. That will let the dynamic linker call the right
1196 // function when resolving IRELATIVE relocations.
1197 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
1198 for (std::vector<Global_ifunc>::const_iterator p =
1199 this->global_ifuncs_.begin();
1200 p != this->global_ifuncs_.end();
1201 ++p)
1203 const Sized_symbol<32>* ssym =
1204 static_cast<const Sized_symbol<32>*>(p->sym);
1205 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1206 ssym->value());
1209 for (std::vector<Local_ifunc>::const_iterator p =
1210 this->local_ifuncs_.begin();
1211 p != this->local_ifuncs_.end();
1212 ++p)
1214 const Symbol_value<32>* psymval =
1215 p->object->local_symbol(p->local_sym_index);
1216 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
1217 psymval->value(p->object, 0));
1220 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1221 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1223 of->write_output_view(offset, oview_size, oview);
1224 of->write_output_view(got_file_offset, got_size, got_view);
1227 // Create the PLT section.
1229 void
1230 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1232 if (this->plt_ == NULL)
1234 // Create the GOT sections first.
1235 this->got_section(symtab, layout);
1237 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
1238 this->got_irelative_);
1239 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1240 (elfcpp::SHF_ALLOC
1241 | elfcpp::SHF_EXECINSTR),
1242 this->plt_, ORDER_PLT, false);
1244 // Make the sh_info field of .rel.plt point to .plt.
1245 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1246 rel_plt_os->set_info_section(this->plt_->output_section());
1250 // Create a PLT entry for a global symbol.
1252 void
1253 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1255 if (gsym->has_plt_offset())
1256 return;
1257 if (this->plt_ == NULL)
1258 this->make_plt_section(symtab, layout);
1259 this->plt_->add_entry(symtab, layout, gsym);
1262 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1264 void
1265 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1266 Sized_relobj_file<32, false>* relobj,
1267 unsigned int local_sym_index)
1269 if (relobj->local_has_plt_offset(local_sym_index))
1270 return;
1271 if (this->plt_ == NULL)
1272 this->make_plt_section(symtab, layout);
1273 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1274 relobj,
1275 local_sym_index);
1276 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1279 // Return the number of entries in the PLT.
1281 unsigned int
1282 Target_i386::plt_entry_count() const
1284 if (this->plt_ == NULL)
1285 return 0;
1286 return this->plt_->entry_count();
1289 // Return the offset of the first non-reserved PLT entry.
1291 unsigned int
1292 Target_i386::first_plt_entry_offset() const
1294 return Output_data_plt_i386::first_plt_entry_offset();
1297 // Return the size of each PLT entry.
1299 unsigned int
1300 Target_i386::plt_entry_size() const
1302 return Output_data_plt_i386::get_plt_entry_size();
1305 // Get the section to use for TLS_DESC relocations.
1307 Target_i386::Reloc_section*
1308 Target_i386::rel_tls_desc_section(Layout* layout) const
1310 return this->plt_section()->rel_tls_desc(layout);
1313 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1315 void
1316 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1318 if (this->tls_base_symbol_defined_)
1319 return;
1321 Output_segment* tls_segment = layout->tls_segment();
1322 if (tls_segment != NULL)
1324 bool is_exec = parameters->options().output_is_executable();
1325 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1326 Symbol_table::PREDEFINED,
1327 tls_segment, 0, 0,
1328 elfcpp::STT_TLS,
1329 elfcpp::STB_LOCAL,
1330 elfcpp::STV_HIDDEN, 0,
1331 (is_exec
1332 ? Symbol::SEGMENT_END
1333 : Symbol::SEGMENT_START),
1334 true);
1336 this->tls_base_symbol_defined_ = true;
1339 // Create a GOT entry for the TLS module index.
1341 unsigned int
1342 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1343 Sized_relobj_file<32, false>* object)
1345 if (this->got_mod_index_offset_ == -1U)
1347 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1348 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1349 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1350 unsigned int got_offset = got->add_constant(0);
1351 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1352 got_offset);
1353 got->add_constant(0);
1354 this->got_mod_index_offset_ = got_offset;
1356 return this->got_mod_index_offset_;
1359 // Optimize the TLS relocation type based on what we know about the
1360 // symbol. IS_FINAL is true if the final address of this symbol is
1361 // known at link time.
1363 tls::Tls_optimization
1364 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1366 // If we are generating a shared library, then we can't do anything
1367 // in the linker.
1368 if (parameters->options().shared())
1369 return tls::TLSOPT_NONE;
1371 switch (r_type)
1373 case elfcpp::R_386_TLS_GD:
1374 case elfcpp::R_386_TLS_GOTDESC:
1375 case elfcpp::R_386_TLS_DESC_CALL:
1376 // These are General-Dynamic which permits fully general TLS
1377 // access. Since we know that we are generating an executable,
1378 // we can convert this to Initial-Exec. If we also know that
1379 // this is a local symbol, we can further switch to Local-Exec.
1380 if (is_final)
1381 return tls::TLSOPT_TO_LE;
1382 return tls::TLSOPT_TO_IE;
1384 case elfcpp::R_386_TLS_LDM:
1385 // This is Local-Dynamic, which refers to a local symbol in the
1386 // dynamic TLS block. Since we know that we generating an
1387 // executable, we can switch to Local-Exec.
1388 return tls::TLSOPT_TO_LE;
1390 case elfcpp::R_386_TLS_LDO_32:
1391 // Another type of Local-Dynamic relocation.
1392 return tls::TLSOPT_TO_LE;
1394 case elfcpp::R_386_TLS_IE:
1395 case elfcpp::R_386_TLS_GOTIE:
1396 case elfcpp::R_386_TLS_IE_32:
1397 // These are Initial-Exec relocs which get the thread offset
1398 // from the GOT. If we know that we are linking against the
1399 // local symbol, we can switch to Local-Exec, which links the
1400 // thread offset into the instruction.
1401 if (is_final)
1402 return tls::TLSOPT_TO_LE;
1403 return tls::TLSOPT_NONE;
1405 case elfcpp::R_386_TLS_LE:
1406 case elfcpp::R_386_TLS_LE_32:
1407 // When we already have Local-Exec, there is nothing further we
1408 // can do.
1409 return tls::TLSOPT_NONE;
1411 default:
1412 gold_unreachable();
1416 // Get the Reference_flags for a particular relocation.
1419 Target_i386::Scan::get_reference_flags(unsigned int r_type)
1421 switch (r_type)
1423 case elfcpp::R_386_NONE:
1424 case elfcpp::R_386_GNU_VTINHERIT:
1425 case elfcpp::R_386_GNU_VTENTRY:
1426 case elfcpp::R_386_GOTPC:
1427 // No symbol reference.
1428 return 0;
1430 case elfcpp::R_386_32:
1431 case elfcpp::R_386_16:
1432 case elfcpp::R_386_8:
1433 return Symbol::ABSOLUTE_REF;
1435 case elfcpp::R_386_PC32:
1436 case elfcpp::R_386_PC16:
1437 case elfcpp::R_386_PC8:
1438 case elfcpp::R_386_GOTOFF:
1439 return Symbol::RELATIVE_REF;
1441 case elfcpp::R_386_PLT32:
1442 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1444 case elfcpp::R_386_GOT32:
1445 // Absolute in GOT.
1446 return Symbol::ABSOLUTE_REF;
1448 case elfcpp::R_386_TLS_GD: // Global-dynamic
1449 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1450 case elfcpp::R_386_TLS_DESC_CALL:
1451 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1452 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1453 case elfcpp::R_386_TLS_IE: // Initial-exec
1454 case elfcpp::R_386_TLS_IE_32:
1455 case elfcpp::R_386_TLS_GOTIE:
1456 case elfcpp::R_386_TLS_LE: // Local-exec
1457 case elfcpp::R_386_TLS_LE_32:
1458 return Symbol::TLS_REF;
1460 case elfcpp::R_386_COPY:
1461 case elfcpp::R_386_GLOB_DAT:
1462 case elfcpp::R_386_JUMP_SLOT:
1463 case elfcpp::R_386_RELATIVE:
1464 case elfcpp::R_386_IRELATIVE:
1465 case elfcpp::R_386_TLS_TPOFF:
1466 case elfcpp::R_386_TLS_DTPMOD32:
1467 case elfcpp::R_386_TLS_DTPOFF32:
1468 case elfcpp::R_386_TLS_TPOFF32:
1469 case elfcpp::R_386_TLS_DESC:
1470 case elfcpp::R_386_32PLT:
1471 case elfcpp::R_386_TLS_GD_32:
1472 case elfcpp::R_386_TLS_GD_PUSH:
1473 case elfcpp::R_386_TLS_GD_CALL:
1474 case elfcpp::R_386_TLS_GD_POP:
1475 case elfcpp::R_386_TLS_LDM_32:
1476 case elfcpp::R_386_TLS_LDM_PUSH:
1477 case elfcpp::R_386_TLS_LDM_CALL:
1478 case elfcpp::R_386_TLS_LDM_POP:
1479 case elfcpp::R_386_USED_BY_INTEL_200:
1480 default:
1481 // Not expected. We will give an error later.
1482 return 0;
1486 // Report an unsupported relocation against a local symbol.
1488 void
1489 Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
1490 unsigned int r_type)
1492 gold_error(_("%s: unsupported reloc %u against local symbol"),
1493 object->name().c_str(), r_type);
1496 // Return whether we need to make a PLT entry for a relocation of a
1497 // given type against a STT_GNU_IFUNC symbol.
1499 bool
1500 Target_i386::Scan::reloc_needs_plt_for_ifunc(
1501 Sized_relobj_file<32, false>* object,
1502 unsigned int r_type)
1504 int flags = Scan::get_reference_flags(r_type);
1505 if (flags & Symbol::TLS_REF)
1506 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1507 object->name().c_str(), r_type);
1508 return flags != 0;
1511 // Scan a relocation for a local symbol.
1513 inline void
1514 Target_i386::Scan::local(Symbol_table* symtab,
1515 Layout* layout,
1516 Target_i386* target,
1517 Sized_relobj_file<32, false>* object,
1518 unsigned int data_shndx,
1519 Output_section* output_section,
1520 const elfcpp::Rel<32, false>& reloc,
1521 unsigned int r_type,
1522 const elfcpp::Sym<32, false>& lsym)
1524 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1525 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1526 && this->reloc_needs_plt_for_ifunc(object, r_type))
1528 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1529 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1532 switch (r_type)
1534 case elfcpp::R_386_NONE:
1535 case elfcpp::R_386_GNU_VTINHERIT:
1536 case elfcpp::R_386_GNU_VTENTRY:
1537 break;
1539 case elfcpp::R_386_32:
1540 // If building a shared library (or a position-independent
1541 // executable), we need to create a dynamic relocation for
1542 // this location. The relocation applied at link time will
1543 // apply the link-time value, so we flag the location with
1544 // an R_386_RELATIVE relocation so the dynamic loader can
1545 // relocate it easily.
1546 if (parameters->options().output_is_position_independent())
1548 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1549 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1550 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1551 output_section, data_shndx,
1552 reloc.get_r_offset());
1554 break;
1556 case elfcpp::R_386_16:
1557 case elfcpp::R_386_8:
1558 // If building a shared library (or a position-independent
1559 // executable), we need to create a dynamic relocation for
1560 // this location. Because the addend needs to remain in the
1561 // data section, we need to be careful not to apply this
1562 // relocation statically.
1563 if (parameters->options().output_is_position_independent())
1565 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1566 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1567 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1568 rel_dyn->add_local(object, r_sym, r_type, output_section,
1569 data_shndx, reloc.get_r_offset());
1570 else
1572 gold_assert(lsym.get_st_value() == 0);
1573 unsigned int shndx = lsym.get_st_shndx();
1574 bool is_ordinary;
1575 shndx = object->adjust_sym_shndx(r_sym, shndx,
1576 &is_ordinary);
1577 if (!is_ordinary)
1578 object->error(_("section symbol %u has bad shndx %u"),
1579 r_sym, shndx);
1580 else
1581 rel_dyn->add_local_section(object, shndx,
1582 r_type, output_section,
1583 data_shndx, reloc.get_r_offset());
1586 break;
1588 case elfcpp::R_386_PC32:
1589 case elfcpp::R_386_PC16:
1590 case elfcpp::R_386_PC8:
1591 break;
1593 case elfcpp::R_386_PLT32:
1594 // Since we know this is a local symbol, we can handle this as a
1595 // PC32 reloc.
1596 break;
1598 case elfcpp::R_386_GOTOFF:
1599 case elfcpp::R_386_GOTPC:
1600 // We need a GOT section.
1601 target->got_section(symtab, layout);
1602 break;
1604 case elfcpp::R_386_GOT32:
1606 // The symbol requires a GOT entry.
1607 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1608 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1610 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1611 // lets function pointers compare correctly with shared
1612 // libraries. Otherwise we would need an IRELATIVE reloc.
1613 bool is_new;
1614 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1615 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1616 else
1617 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1618 if (is_new)
1620 // If we are generating a shared object, we need to add a
1621 // dynamic RELATIVE relocation for this symbol's GOT entry.
1622 if (parameters->options().output_is_position_independent())
1624 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1625 unsigned int got_offset =
1626 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1627 rel_dyn->add_local_relative(object, r_sym,
1628 elfcpp::R_386_RELATIVE,
1629 got, got_offset);
1633 break;
1635 // These are relocations which should only be seen by the
1636 // dynamic linker, and should never be seen here.
1637 case elfcpp::R_386_COPY:
1638 case elfcpp::R_386_GLOB_DAT:
1639 case elfcpp::R_386_JUMP_SLOT:
1640 case elfcpp::R_386_RELATIVE:
1641 case elfcpp::R_386_IRELATIVE:
1642 case elfcpp::R_386_TLS_TPOFF:
1643 case elfcpp::R_386_TLS_DTPMOD32:
1644 case elfcpp::R_386_TLS_DTPOFF32:
1645 case elfcpp::R_386_TLS_TPOFF32:
1646 case elfcpp::R_386_TLS_DESC:
1647 gold_error(_("%s: unexpected reloc %u in object file"),
1648 object->name().c_str(), r_type);
1649 break;
1651 // These are initial TLS relocs, which are expected when
1652 // linking.
1653 case elfcpp::R_386_TLS_GD: // Global-dynamic
1654 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1655 case elfcpp::R_386_TLS_DESC_CALL:
1656 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1657 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1658 case elfcpp::R_386_TLS_IE: // Initial-exec
1659 case elfcpp::R_386_TLS_IE_32:
1660 case elfcpp::R_386_TLS_GOTIE:
1661 case elfcpp::R_386_TLS_LE: // Local-exec
1662 case elfcpp::R_386_TLS_LE_32:
1664 bool output_is_shared = parameters->options().shared();
1665 const tls::Tls_optimization optimized_type
1666 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1667 switch (r_type)
1669 case elfcpp::R_386_TLS_GD: // Global-dynamic
1670 if (optimized_type == tls::TLSOPT_NONE)
1672 // Create a pair of GOT entries for the module index and
1673 // dtv-relative offset.
1674 Output_data_got<32, false>* got
1675 = target->got_section(symtab, layout);
1676 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1677 unsigned int shndx = lsym.get_st_shndx();
1678 bool is_ordinary;
1679 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1680 if (!is_ordinary)
1681 object->error(_("local symbol %u has bad shndx %u"),
1682 r_sym, shndx);
1683 else
1684 got->add_local_pair_with_rel(object, r_sym, shndx,
1685 GOT_TYPE_TLS_PAIR,
1686 target->rel_dyn_section(layout),
1687 elfcpp::R_386_TLS_DTPMOD32, 0);
1689 else if (optimized_type != tls::TLSOPT_TO_LE)
1690 unsupported_reloc_local(object, r_type);
1691 break;
1693 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1694 target->define_tls_base_symbol(symtab, layout);
1695 if (optimized_type == tls::TLSOPT_NONE)
1697 // Create a double GOT entry with an R_386_TLS_DESC
1698 // reloc. The R_386_TLS_DESC reloc is resolved
1699 // lazily, so the GOT entry needs to be in an area in
1700 // .got.plt, not .got. Call got_section to make sure
1701 // the section has been created.
1702 target->got_section(symtab, layout);
1703 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1704 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1705 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1707 unsigned int got_offset = got->add_constant(0);
1708 // The local symbol value is stored in the second
1709 // GOT entry.
1710 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1711 // That set the GOT offset of the local symbol to
1712 // point to the second entry, but we want it to
1713 // point to the first.
1714 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1715 got_offset);
1716 Reloc_section* rt = target->rel_tls_desc_section(layout);
1717 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1720 else if (optimized_type != tls::TLSOPT_TO_LE)
1721 unsupported_reloc_local(object, r_type);
1722 break;
1724 case elfcpp::R_386_TLS_DESC_CALL:
1725 break;
1727 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1728 if (optimized_type == tls::TLSOPT_NONE)
1730 // Create a GOT entry for the module index.
1731 target->got_mod_index_entry(symtab, layout, object);
1733 else if (optimized_type != tls::TLSOPT_TO_LE)
1734 unsupported_reloc_local(object, r_type);
1735 break;
1737 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1738 break;
1740 case elfcpp::R_386_TLS_IE: // Initial-exec
1741 case elfcpp::R_386_TLS_IE_32:
1742 case elfcpp::R_386_TLS_GOTIE:
1743 layout->set_has_static_tls();
1744 if (optimized_type == tls::TLSOPT_NONE)
1746 // For the R_386_TLS_IE relocation, we need to create a
1747 // dynamic relocation when building a shared library.
1748 if (r_type == elfcpp::R_386_TLS_IE
1749 && parameters->options().shared())
1751 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1752 unsigned int r_sym
1753 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1754 rel_dyn->add_local_relative(object, r_sym,
1755 elfcpp::R_386_RELATIVE,
1756 output_section, data_shndx,
1757 reloc.get_r_offset());
1759 // Create a GOT entry for the tp-relative offset.
1760 Output_data_got<32, false>* got
1761 = target->got_section(symtab, layout);
1762 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1763 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1764 ? elfcpp::R_386_TLS_TPOFF32
1765 : elfcpp::R_386_TLS_TPOFF);
1766 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1767 ? GOT_TYPE_TLS_OFFSET
1768 : GOT_TYPE_TLS_NOFFSET);
1769 got->add_local_with_rel(object, r_sym, got_type,
1770 target->rel_dyn_section(layout),
1771 dyn_r_type);
1773 else if (optimized_type != tls::TLSOPT_TO_LE)
1774 unsupported_reloc_local(object, r_type);
1775 break;
1777 case elfcpp::R_386_TLS_LE: // Local-exec
1778 case elfcpp::R_386_TLS_LE_32:
1779 layout->set_has_static_tls();
1780 if (output_is_shared)
1782 // We need to create a dynamic relocation.
1783 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1784 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1785 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1786 ? elfcpp::R_386_TLS_TPOFF32
1787 : elfcpp::R_386_TLS_TPOFF);
1788 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1789 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1790 data_shndx, reloc.get_r_offset());
1792 break;
1794 default:
1795 gold_unreachable();
1798 break;
1800 case elfcpp::R_386_32PLT:
1801 case elfcpp::R_386_TLS_GD_32:
1802 case elfcpp::R_386_TLS_GD_PUSH:
1803 case elfcpp::R_386_TLS_GD_CALL:
1804 case elfcpp::R_386_TLS_GD_POP:
1805 case elfcpp::R_386_TLS_LDM_32:
1806 case elfcpp::R_386_TLS_LDM_PUSH:
1807 case elfcpp::R_386_TLS_LDM_CALL:
1808 case elfcpp::R_386_TLS_LDM_POP:
1809 case elfcpp::R_386_USED_BY_INTEL_200:
1810 default:
1811 unsupported_reloc_local(object, r_type);
1812 break;
1816 // Report an unsupported relocation against a global symbol.
1818 void
1819 Target_i386::Scan::unsupported_reloc_global(
1820 Sized_relobj_file<32, false>* object,
1821 unsigned int r_type,
1822 Symbol* gsym)
1824 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1825 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1828 inline bool
1829 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1831 switch (r_type)
1833 case elfcpp::R_386_32:
1834 case elfcpp::R_386_16:
1835 case elfcpp::R_386_8:
1836 case elfcpp::R_386_GOTOFF:
1837 case elfcpp::R_386_GOT32:
1839 return true;
1841 default:
1842 return false;
1844 return false;
1847 inline bool
1848 Target_i386::Scan::local_reloc_may_be_function_pointer(
1849 Symbol_table* ,
1850 Layout* ,
1851 Target_i386* ,
1852 Sized_relobj_file<32, false>* ,
1853 unsigned int ,
1854 Output_section* ,
1855 const elfcpp::Rel<32, false>& ,
1856 unsigned int r_type,
1857 const elfcpp::Sym<32, false>&)
1859 return possible_function_pointer_reloc(r_type);
1862 inline bool
1863 Target_i386::Scan::global_reloc_may_be_function_pointer(
1864 Symbol_table* ,
1865 Layout* ,
1866 Target_i386* ,
1867 Sized_relobj_file<32, false>* ,
1868 unsigned int ,
1869 Output_section* ,
1870 const elfcpp::Rel<32, false>& ,
1871 unsigned int r_type,
1872 Symbol*)
1874 return possible_function_pointer_reloc(r_type);
1877 // Scan a relocation for a global symbol.
1879 inline void
1880 Target_i386::Scan::global(Symbol_table* symtab,
1881 Layout* layout,
1882 Target_i386* target,
1883 Sized_relobj_file<32, false>* object,
1884 unsigned int data_shndx,
1885 Output_section* output_section,
1886 const elfcpp::Rel<32, false>& reloc,
1887 unsigned int r_type,
1888 Symbol* gsym)
1890 // A STT_GNU_IFUNC symbol may require a PLT entry.
1891 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1892 && this->reloc_needs_plt_for_ifunc(object, r_type))
1893 target->make_plt_entry(symtab, layout, gsym);
1895 switch (r_type)
1897 case elfcpp::R_386_NONE:
1898 case elfcpp::R_386_GNU_VTINHERIT:
1899 case elfcpp::R_386_GNU_VTENTRY:
1900 break;
1902 case elfcpp::R_386_32:
1903 case elfcpp::R_386_16:
1904 case elfcpp::R_386_8:
1906 // Make a PLT entry if necessary.
1907 if (gsym->needs_plt_entry())
1909 target->make_plt_entry(symtab, layout, gsym);
1910 // Since this is not a PC-relative relocation, we may be
1911 // taking the address of a function. In that case we need to
1912 // set the entry in the dynamic symbol table to the address of
1913 // the PLT entry.
1914 if (gsym->is_from_dynobj() && !parameters->options().shared())
1915 gsym->set_needs_dynsym_value();
1917 // Make a dynamic relocation if necessary.
1918 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1920 if (gsym->may_need_copy_reloc())
1922 target->copy_reloc(symtab, layout, object,
1923 data_shndx, output_section, gsym, reloc);
1925 else if (r_type == elfcpp::R_386_32
1926 && gsym->type() == elfcpp::STT_GNU_IFUNC
1927 && gsym->can_use_relative_reloc(false)
1928 && !gsym->is_from_dynobj()
1929 && !gsym->is_undefined()
1930 && !gsym->is_preemptible())
1932 // Use an IRELATIVE reloc for a locally defined
1933 // STT_GNU_IFUNC symbol. This makes a function
1934 // address in a PIE executable match the address in a
1935 // shared library that it links against.
1936 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
1937 rel_dyn->add_symbolless_global_addend(gsym,
1938 elfcpp::R_386_IRELATIVE,
1939 output_section,
1940 object, data_shndx,
1941 reloc.get_r_offset());
1943 else if (r_type == elfcpp::R_386_32
1944 && gsym->can_use_relative_reloc(false))
1946 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1947 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1948 output_section, object,
1949 data_shndx, reloc.get_r_offset());
1951 else
1953 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1954 rel_dyn->add_global(gsym, r_type, output_section, object,
1955 data_shndx, reloc.get_r_offset());
1959 break;
1961 case elfcpp::R_386_PC32:
1962 case elfcpp::R_386_PC16:
1963 case elfcpp::R_386_PC8:
1965 // Make a PLT entry if necessary.
1966 if (gsym->needs_plt_entry())
1968 // These relocations are used for function calls only in
1969 // non-PIC code. For a 32-bit relocation in a shared library,
1970 // we'll need a text relocation anyway, so we can skip the
1971 // PLT entry and let the dynamic linker bind the call directly
1972 // to the target. For smaller relocations, we should use a
1973 // PLT entry to ensure that the call can reach.
1974 if (!parameters->options().shared()
1975 || r_type != elfcpp::R_386_PC32)
1976 target->make_plt_entry(symtab, layout, gsym);
1978 // Make a dynamic relocation if necessary.
1979 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
1981 if (gsym->may_need_copy_reloc())
1983 target->copy_reloc(symtab, layout, object,
1984 data_shndx, output_section, gsym, reloc);
1986 else
1988 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1989 rel_dyn->add_global(gsym, r_type, output_section, object,
1990 data_shndx, reloc.get_r_offset());
1994 break;
1996 case elfcpp::R_386_GOT32:
1998 // The symbol requires a GOT entry.
1999 Output_data_got<32, false>* got = target->got_section(symtab, layout);
2000 if (gsym->final_value_is_known())
2002 // For a STT_GNU_IFUNC symbol we want the PLT address.
2003 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2004 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2005 else
2006 got->add_global(gsym, GOT_TYPE_STANDARD);
2008 else
2010 // If this symbol is not fully resolved, we need to add a
2011 // GOT entry with a dynamic relocation.
2012 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2014 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2016 // 1) The symbol may be defined in some other module.
2018 // 2) We are building a shared library and this is a
2019 // protected symbol; using GLOB_DAT means that the dynamic
2020 // linker can use the address of the PLT in the main
2021 // executable when appropriate so that function address
2022 // comparisons work.
2024 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2025 // code, again so that function address comparisons work.
2026 if (gsym->is_from_dynobj()
2027 || gsym->is_undefined()
2028 || gsym->is_preemptible()
2029 || (gsym->visibility() == elfcpp::STV_PROTECTED
2030 && parameters->options().shared())
2031 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2032 && parameters->options().output_is_position_independent()))
2033 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2034 rel_dyn, elfcpp::R_386_GLOB_DAT);
2035 else
2037 // For a STT_GNU_IFUNC symbol we want to write the PLT
2038 // offset into the GOT, so that function pointer
2039 // comparisons work correctly.
2040 bool is_new;
2041 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2042 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2043 else
2045 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2046 // Tell the dynamic linker to use the PLT address
2047 // when resolving relocations.
2048 if (gsym->is_from_dynobj()
2049 && !parameters->options().shared())
2050 gsym->set_needs_dynsym_value();
2052 if (is_new)
2054 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2055 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2056 got, got_off);
2061 break;
2063 case elfcpp::R_386_PLT32:
2064 // If the symbol is fully resolved, this is just a PC32 reloc.
2065 // Otherwise we need a PLT entry.
2066 if (gsym->final_value_is_known())
2067 break;
2068 // If building a shared library, we can also skip the PLT entry
2069 // if the symbol is defined in the output file and is protected
2070 // or hidden.
2071 if (gsym->is_defined()
2072 && !gsym->is_from_dynobj()
2073 && !gsym->is_preemptible())
2074 break;
2075 target->make_plt_entry(symtab, layout, gsym);
2076 break;
2078 case elfcpp::R_386_GOTOFF:
2079 case elfcpp::R_386_GOTPC:
2080 // We need a GOT section.
2081 target->got_section(symtab, layout);
2082 break;
2084 // These are relocations which should only be seen by the
2085 // dynamic linker, and should never be seen here.
2086 case elfcpp::R_386_COPY:
2087 case elfcpp::R_386_GLOB_DAT:
2088 case elfcpp::R_386_JUMP_SLOT:
2089 case elfcpp::R_386_RELATIVE:
2090 case elfcpp::R_386_IRELATIVE:
2091 case elfcpp::R_386_TLS_TPOFF:
2092 case elfcpp::R_386_TLS_DTPMOD32:
2093 case elfcpp::R_386_TLS_DTPOFF32:
2094 case elfcpp::R_386_TLS_TPOFF32:
2095 case elfcpp::R_386_TLS_DESC:
2096 gold_error(_("%s: unexpected reloc %u in object file"),
2097 object->name().c_str(), r_type);
2098 break;
2100 // These are initial tls relocs, which are expected when
2101 // linking.
2102 case elfcpp::R_386_TLS_GD: // Global-dynamic
2103 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2104 case elfcpp::R_386_TLS_DESC_CALL:
2105 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2106 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2107 case elfcpp::R_386_TLS_IE: // Initial-exec
2108 case elfcpp::R_386_TLS_IE_32:
2109 case elfcpp::R_386_TLS_GOTIE:
2110 case elfcpp::R_386_TLS_LE: // Local-exec
2111 case elfcpp::R_386_TLS_LE_32:
2113 const bool is_final = gsym->final_value_is_known();
2114 const tls::Tls_optimization optimized_type
2115 = Target_i386::optimize_tls_reloc(is_final, r_type);
2116 switch (r_type)
2118 case elfcpp::R_386_TLS_GD: // Global-dynamic
2119 if (optimized_type == tls::TLSOPT_NONE)
2121 // Create a pair of GOT entries for the module index and
2122 // dtv-relative offset.
2123 Output_data_got<32, false>* got
2124 = target->got_section(symtab, layout);
2125 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
2126 target->rel_dyn_section(layout),
2127 elfcpp::R_386_TLS_DTPMOD32,
2128 elfcpp::R_386_TLS_DTPOFF32);
2130 else if (optimized_type == tls::TLSOPT_TO_IE)
2132 // Create a GOT entry for the tp-relative offset.
2133 Output_data_got<32, false>* got
2134 = target->got_section(symtab, layout);
2135 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2136 target->rel_dyn_section(layout),
2137 elfcpp::R_386_TLS_TPOFF);
2139 else if (optimized_type != tls::TLSOPT_TO_LE)
2140 unsupported_reloc_global(object, r_type, gsym);
2141 break;
2143 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
2144 target->define_tls_base_symbol(symtab, layout);
2145 if (optimized_type == tls::TLSOPT_NONE)
2147 // Create a double GOT entry with an R_386_TLS_DESC
2148 // reloc. The R_386_TLS_DESC reloc is resolved
2149 // lazily, so the GOT entry needs to be in an area in
2150 // .got.plt, not .got. Call got_section to make sure
2151 // the section has been created.
2152 target->got_section(symtab, layout);
2153 Output_data_got<32, false>* got = target->got_tlsdesc_section();
2154 Reloc_section* rt = target->rel_tls_desc_section(layout);
2155 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
2156 elfcpp::R_386_TLS_DESC, 0);
2158 else if (optimized_type == tls::TLSOPT_TO_IE)
2160 // Create a GOT entry for the tp-relative offset.
2161 Output_data_got<32, false>* got
2162 = target->got_section(symtab, layout);
2163 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2164 target->rel_dyn_section(layout),
2165 elfcpp::R_386_TLS_TPOFF);
2167 else if (optimized_type != tls::TLSOPT_TO_LE)
2168 unsupported_reloc_global(object, r_type, gsym);
2169 break;
2171 case elfcpp::R_386_TLS_DESC_CALL:
2172 break;
2174 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2175 if (optimized_type == tls::TLSOPT_NONE)
2177 // Create a GOT entry for the module index.
2178 target->got_mod_index_entry(symtab, layout, object);
2180 else if (optimized_type != tls::TLSOPT_TO_LE)
2181 unsupported_reloc_global(object, r_type, gsym);
2182 break;
2184 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2185 break;
2187 case elfcpp::R_386_TLS_IE: // Initial-exec
2188 case elfcpp::R_386_TLS_IE_32:
2189 case elfcpp::R_386_TLS_GOTIE:
2190 layout->set_has_static_tls();
2191 if (optimized_type == tls::TLSOPT_NONE)
2193 // For the R_386_TLS_IE relocation, we need to create a
2194 // dynamic relocation when building a shared library.
2195 if (r_type == elfcpp::R_386_TLS_IE
2196 && parameters->options().shared())
2198 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2199 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2200 output_section, object,
2201 data_shndx,
2202 reloc.get_r_offset());
2204 // Create a GOT entry for the tp-relative offset.
2205 Output_data_got<32, false>* got
2206 = target->got_section(symtab, layout);
2207 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2208 ? elfcpp::R_386_TLS_TPOFF32
2209 : elfcpp::R_386_TLS_TPOFF);
2210 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2211 ? GOT_TYPE_TLS_OFFSET
2212 : GOT_TYPE_TLS_NOFFSET);
2213 got->add_global_with_rel(gsym, got_type,
2214 target->rel_dyn_section(layout),
2215 dyn_r_type);
2217 else if (optimized_type != tls::TLSOPT_TO_LE)
2218 unsupported_reloc_global(object, r_type, gsym);
2219 break;
2221 case elfcpp::R_386_TLS_LE: // Local-exec
2222 case elfcpp::R_386_TLS_LE_32:
2223 layout->set_has_static_tls();
2224 if (parameters->options().shared())
2226 // We need to create a dynamic relocation.
2227 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2228 ? elfcpp::R_386_TLS_TPOFF32
2229 : elfcpp::R_386_TLS_TPOFF);
2230 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2231 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2232 data_shndx, reloc.get_r_offset());
2234 break;
2236 default:
2237 gold_unreachable();
2240 break;
2242 case elfcpp::R_386_32PLT:
2243 case elfcpp::R_386_TLS_GD_32:
2244 case elfcpp::R_386_TLS_GD_PUSH:
2245 case elfcpp::R_386_TLS_GD_CALL:
2246 case elfcpp::R_386_TLS_GD_POP:
2247 case elfcpp::R_386_TLS_LDM_32:
2248 case elfcpp::R_386_TLS_LDM_PUSH:
2249 case elfcpp::R_386_TLS_LDM_CALL:
2250 case elfcpp::R_386_TLS_LDM_POP:
2251 case elfcpp::R_386_USED_BY_INTEL_200:
2252 default:
2253 unsupported_reloc_global(object, r_type, gsym);
2254 break;
2258 // Process relocations for gc.
2260 void
2261 Target_i386::gc_process_relocs(Symbol_table* symtab,
2262 Layout* layout,
2263 Sized_relobj_file<32, false>* object,
2264 unsigned int data_shndx,
2265 unsigned int,
2266 const unsigned char* prelocs,
2267 size_t reloc_count,
2268 Output_section* output_section,
2269 bool needs_special_offset_handling,
2270 size_t local_symbol_count,
2271 const unsigned char* plocal_symbols)
2273 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2274 Target_i386::Scan,
2275 Target_i386::Relocatable_size_for_reloc>(
2276 symtab,
2277 layout,
2278 this,
2279 object,
2280 data_shndx,
2281 prelocs,
2282 reloc_count,
2283 output_section,
2284 needs_special_offset_handling,
2285 local_symbol_count,
2286 plocal_symbols);
2289 // Scan relocations for a section.
2291 void
2292 Target_i386::scan_relocs(Symbol_table* symtab,
2293 Layout* layout,
2294 Sized_relobj_file<32, false>* object,
2295 unsigned int data_shndx,
2296 unsigned int sh_type,
2297 const unsigned char* prelocs,
2298 size_t reloc_count,
2299 Output_section* output_section,
2300 bool needs_special_offset_handling,
2301 size_t local_symbol_count,
2302 const unsigned char* plocal_symbols)
2304 if (sh_type == elfcpp::SHT_RELA)
2306 gold_error(_("%s: unsupported RELA reloc section"),
2307 object->name().c_str());
2308 return;
2311 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2312 Target_i386::Scan>(
2313 symtab,
2314 layout,
2315 this,
2316 object,
2317 data_shndx,
2318 prelocs,
2319 reloc_count,
2320 output_section,
2321 needs_special_offset_handling,
2322 local_symbol_count,
2323 plocal_symbols);
2326 // Finalize the sections.
2328 void
2329 Target_i386::do_finalize_sections(
2330 Layout* layout,
2331 const Input_objects*,
2332 Symbol_table* symtab)
2334 const Reloc_section* rel_plt = (this->plt_ == NULL
2335 ? NULL
2336 : this->plt_->rel_plt());
2337 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2338 this->rel_dyn_, true, false);
2340 // Emit any relocs we saved in an attempt to avoid generating COPY
2341 // relocs.
2342 if (this->copy_relocs_.any_saved_relocs())
2343 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2345 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2346 // the .got.plt section.
2347 Symbol* sym = this->global_offset_table_;
2348 if (sym != NULL)
2350 uint32_t data_size = this->got_plt_->current_data_size();
2351 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2354 if (parameters->doing_static_link()
2355 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
2357 // If linking statically, make sure that the __rel_iplt symbols
2358 // were defined if necessary, even if we didn't create a PLT.
2359 static const Define_symbol_in_segment syms[] =
2362 "__rel_iplt_start", // name
2363 elfcpp::PT_LOAD, // segment_type
2364 elfcpp::PF_W, // segment_flags_set
2365 elfcpp::PF(0), // segment_flags_clear
2366 0, // value
2367 0, // size
2368 elfcpp::STT_NOTYPE, // type
2369 elfcpp::STB_GLOBAL, // binding
2370 elfcpp::STV_HIDDEN, // visibility
2371 0, // nonvis
2372 Symbol::SEGMENT_START, // offset_from_base
2373 true // only_if_ref
2376 "__rel_iplt_end", // name
2377 elfcpp::PT_LOAD, // segment_type
2378 elfcpp::PF_W, // segment_flags_set
2379 elfcpp::PF(0), // segment_flags_clear
2380 0, // value
2381 0, // size
2382 elfcpp::STT_NOTYPE, // type
2383 elfcpp::STB_GLOBAL, // binding
2384 elfcpp::STV_HIDDEN, // visibility
2385 0, // nonvis
2386 Symbol::SEGMENT_START, // offset_from_base
2387 true // only_if_ref
2391 symtab->define_symbols(layout, 2, syms,
2392 layout->script_options()->saw_sections_clause());
2396 // Return whether a direct absolute static relocation needs to be applied.
2397 // In cases where Scan::local() or Scan::global() has created
2398 // a dynamic relocation other than R_386_RELATIVE, the addend
2399 // of the relocation is carried in the data, and we must not
2400 // apply the static relocation.
2402 inline bool
2403 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2404 unsigned int r_type,
2405 bool is_32bit,
2406 Output_section* output_section)
2408 // If the output section is not allocated, then we didn't call
2409 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2410 // the reloc here.
2411 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2412 return true;
2414 int ref_flags = Scan::get_reference_flags(r_type);
2416 // For local symbols, we will have created a non-RELATIVE dynamic
2417 // relocation only if (a) the output is position independent,
2418 // (b) the relocation is absolute (not pc- or segment-relative), and
2419 // (c) the relocation is not 32 bits wide.
2420 if (gsym == NULL)
2421 return !(parameters->options().output_is_position_independent()
2422 && (ref_flags & Symbol::ABSOLUTE_REF)
2423 && !is_32bit);
2425 // For global symbols, we use the same helper routines used in the
2426 // scan pass. If we did not create a dynamic relocation, or if we
2427 // created a RELATIVE dynamic relocation, we should apply the static
2428 // relocation.
2429 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2430 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2431 && gsym->can_use_relative_reloc(ref_flags
2432 & Symbol::FUNCTION_CALL);
2433 return !has_dyn || is_rel;
2436 // Perform a relocation.
2438 inline bool
2439 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2440 Target_i386* target,
2441 Output_section* output_section,
2442 size_t relnum,
2443 const elfcpp::Rel<32, false>& rel,
2444 unsigned int r_type,
2445 const Sized_symbol<32>* gsym,
2446 const Symbol_value<32>* psymval,
2447 unsigned char* view,
2448 elfcpp::Elf_types<32>::Elf_Addr address,
2449 section_size_type view_size)
2451 if (this->skip_call_tls_get_addr_)
2453 if ((r_type != elfcpp::R_386_PLT32
2454 && r_type != elfcpp::R_386_PC32)
2455 || gsym == NULL
2456 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2457 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2458 _("missing expected TLS relocation"));
2459 else
2461 this->skip_call_tls_get_addr_ = false;
2462 return false;
2466 const Sized_relobj_file<32, false>* object = relinfo->object;
2468 // Pick the value to use for symbols defined in shared objects.
2469 Symbol_value<32> symval;
2470 if (gsym != NULL
2471 && gsym->type() == elfcpp::STT_GNU_IFUNC
2472 && r_type == elfcpp::R_386_32
2473 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
2474 && gsym->can_use_relative_reloc(false)
2475 && !gsym->is_from_dynobj()
2476 && !gsym->is_undefined()
2477 && !gsym->is_preemptible())
2479 // In this case we are generating a R_386_IRELATIVE reloc. We
2480 // want to use the real value of the symbol, not the PLT offset.
2482 else if (gsym != NULL
2483 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
2485 symval.set_output_value(target->plt_address_for_global(gsym)
2486 + gsym->plt_offset());
2487 psymval = &symval;
2489 else if (gsym == NULL && psymval->is_ifunc_symbol())
2491 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2492 if (object->local_has_plt_offset(r_sym))
2494 symval.set_output_value(target->plt_address_for_local(object, r_sym)
2495 + object->local_plt_offset(r_sym));
2496 psymval = &symval;
2500 // Get the GOT offset if needed.
2501 // The GOT pointer points to the end of the GOT section.
2502 // We need to subtract the size of the GOT section to get
2503 // the actual offset to use in the relocation.
2504 bool have_got_offset = false;
2505 unsigned int got_offset = 0;
2506 switch (r_type)
2508 case elfcpp::R_386_GOT32:
2509 if (gsym != NULL)
2511 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2512 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2513 - target->got_size());
2515 else
2517 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2518 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2519 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2520 - target->got_size());
2522 have_got_offset = true;
2523 break;
2525 default:
2526 break;
2529 switch (r_type)
2531 case elfcpp::R_386_NONE:
2532 case elfcpp::R_386_GNU_VTINHERIT:
2533 case elfcpp::R_386_GNU_VTENTRY:
2534 break;
2536 case elfcpp::R_386_32:
2537 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2538 Relocate_functions<32, false>::rel32(view, object, psymval);
2539 break;
2541 case elfcpp::R_386_PC32:
2542 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2543 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2544 break;
2546 case elfcpp::R_386_16:
2547 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2548 Relocate_functions<32, false>::rel16(view, object, psymval);
2549 break;
2551 case elfcpp::R_386_PC16:
2552 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2553 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2554 break;
2556 case elfcpp::R_386_8:
2557 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2558 Relocate_functions<32, false>::rel8(view, object, psymval);
2559 break;
2561 case elfcpp::R_386_PC8:
2562 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2563 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2564 break;
2566 case elfcpp::R_386_PLT32:
2567 gold_assert(gsym == NULL
2568 || gsym->has_plt_offset()
2569 || gsym->final_value_is_known()
2570 || (gsym->is_defined()
2571 && !gsym->is_from_dynobj()
2572 && !gsym->is_preemptible()));
2573 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2574 break;
2576 case elfcpp::R_386_GOT32:
2577 gold_assert(have_got_offset);
2578 Relocate_functions<32, false>::rel32(view, got_offset);
2579 break;
2581 case elfcpp::R_386_GOTOFF:
2583 elfcpp::Elf_types<32>::Elf_Addr value;
2584 value = (psymval->value(object, 0)
2585 - target->got_plt_section()->address());
2586 Relocate_functions<32, false>::rel32(view, value);
2588 break;
2590 case elfcpp::R_386_GOTPC:
2592 elfcpp::Elf_types<32>::Elf_Addr value;
2593 value = target->got_plt_section()->address();
2594 Relocate_functions<32, false>::pcrel32(view, value, address);
2596 break;
2598 case elfcpp::R_386_COPY:
2599 case elfcpp::R_386_GLOB_DAT:
2600 case elfcpp::R_386_JUMP_SLOT:
2601 case elfcpp::R_386_RELATIVE:
2602 case elfcpp::R_386_IRELATIVE:
2603 // These are outstanding tls relocs, which are unexpected when
2604 // linking.
2605 case elfcpp::R_386_TLS_TPOFF:
2606 case elfcpp::R_386_TLS_DTPMOD32:
2607 case elfcpp::R_386_TLS_DTPOFF32:
2608 case elfcpp::R_386_TLS_TPOFF32:
2609 case elfcpp::R_386_TLS_DESC:
2610 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2611 _("unexpected reloc %u in object file"),
2612 r_type);
2613 break;
2615 // These are initial tls relocs, which are expected when
2616 // linking.
2617 case elfcpp::R_386_TLS_GD: // Global-dynamic
2618 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2619 case elfcpp::R_386_TLS_DESC_CALL:
2620 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2621 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2622 case elfcpp::R_386_TLS_IE: // Initial-exec
2623 case elfcpp::R_386_TLS_IE_32:
2624 case elfcpp::R_386_TLS_GOTIE:
2625 case elfcpp::R_386_TLS_LE: // Local-exec
2626 case elfcpp::R_386_TLS_LE_32:
2627 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2628 view, address, view_size);
2629 break;
2631 case elfcpp::R_386_32PLT:
2632 case elfcpp::R_386_TLS_GD_32:
2633 case elfcpp::R_386_TLS_GD_PUSH:
2634 case elfcpp::R_386_TLS_GD_CALL:
2635 case elfcpp::R_386_TLS_GD_POP:
2636 case elfcpp::R_386_TLS_LDM_32:
2637 case elfcpp::R_386_TLS_LDM_PUSH:
2638 case elfcpp::R_386_TLS_LDM_CALL:
2639 case elfcpp::R_386_TLS_LDM_POP:
2640 case elfcpp::R_386_USED_BY_INTEL_200:
2641 default:
2642 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2643 _("unsupported reloc %u"),
2644 r_type);
2645 break;
2648 return true;
2651 // Perform a TLS relocation.
2653 inline void
2654 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2655 Target_i386* target,
2656 size_t relnum,
2657 const elfcpp::Rel<32, false>& rel,
2658 unsigned int r_type,
2659 const Sized_symbol<32>* gsym,
2660 const Symbol_value<32>* psymval,
2661 unsigned char* view,
2662 elfcpp::Elf_types<32>::Elf_Addr,
2663 section_size_type view_size)
2665 Output_segment* tls_segment = relinfo->layout->tls_segment();
2667 const Sized_relobj_file<32, false>* object = relinfo->object;
2669 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2671 const bool is_final = (gsym == NULL
2672 ? !parameters->options().shared()
2673 : gsym->final_value_is_known());
2674 const tls::Tls_optimization optimized_type
2675 = Target_i386::optimize_tls_reloc(is_final, r_type);
2676 switch (r_type)
2678 case elfcpp::R_386_TLS_GD: // Global-dynamic
2679 if (optimized_type == tls::TLSOPT_TO_LE)
2681 if (tls_segment == NULL)
2683 gold_assert(parameters->errors()->error_count() > 0
2684 || issue_undefined_symbol_error(gsym));
2685 return;
2687 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2688 rel, r_type, value, view,
2689 view_size);
2690 break;
2692 else
2694 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2695 ? GOT_TYPE_TLS_NOFFSET
2696 : GOT_TYPE_TLS_PAIR);
2697 unsigned int got_offset;
2698 if (gsym != NULL)
2700 gold_assert(gsym->has_got_offset(got_type));
2701 got_offset = gsym->got_offset(got_type) - target->got_size();
2703 else
2705 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2706 gold_assert(object->local_has_got_offset(r_sym, got_type));
2707 got_offset = (object->local_got_offset(r_sym, got_type)
2708 - target->got_size());
2710 if (optimized_type == tls::TLSOPT_TO_IE)
2712 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2713 got_offset, view, view_size);
2714 break;
2716 else if (optimized_type == tls::TLSOPT_NONE)
2718 // Relocate the field with the offset of the pair of GOT
2719 // entries.
2720 Relocate_functions<32, false>::rel32(view, got_offset);
2721 break;
2724 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2725 _("unsupported reloc %u"),
2726 r_type);
2727 break;
2729 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2730 case elfcpp::R_386_TLS_DESC_CALL:
2731 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2732 if (optimized_type == tls::TLSOPT_TO_LE)
2734 if (tls_segment == NULL)
2736 gold_assert(parameters->errors()->error_count() > 0
2737 || issue_undefined_symbol_error(gsym));
2738 return;
2740 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2741 rel, r_type, value, view,
2742 view_size);
2743 break;
2745 else
2747 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2748 ? GOT_TYPE_TLS_NOFFSET
2749 : GOT_TYPE_TLS_DESC);
2750 unsigned int got_offset = 0;
2751 if (r_type == elfcpp::R_386_TLS_GOTDESC
2752 && optimized_type == tls::TLSOPT_NONE)
2754 // We created GOT entries in the .got.tlsdesc portion of
2755 // the .got.plt section, but the offset stored in the
2756 // symbol is the offset within .got.tlsdesc.
2757 got_offset = (target->got_size()
2758 + target->got_plt_section()->data_size());
2760 if (gsym != NULL)
2762 gold_assert(gsym->has_got_offset(got_type));
2763 got_offset += gsym->got_offset(got_type) - target->got_size();
2765 else
2767 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2768 gold_assert(object->local_has_got_offset(r_sym, got_type));
2769 got_offset += (object->local_got_offset(r_sym, got_type)
2770 - target->got_size());
2772 if (optimized_type == tls::TLSOPT_TO_IE)
2774 if (tls_segment == NULL)
2776 gold_assert(parameters->errors()->error_count() > 0
2777 || issue_undefined_symbol_error(gsym));
2778 return;
2780 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2781 got_offset, view, view_size);
2782 break;
2784 else if (optimized_type == tls::TLSOPT_NONE)
2786 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2788 // Relocate the field with the offset of the pair of GOT
2789 // entries.
2790 Relocate_functions<32, false>::rel32(view, got_offset);
2792 break;
2795 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2796 _("unsupported reloc %u"),
2797 r_type);
2798 break;
2800 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2801 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2803 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2804 _("both SUN and GNU model "
2805 "TLS relocations"));
2806 break;
2808 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2809 if (optimized_type == tls::TLSOPT_TO_LE)
2811 if (tls_segment == NULL)
2813 gold_assert(parameters->errors()->error_count() > 0
2814 || issue_undefined_symbol_error(gsym));
2815 return;
2817 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2818 value, view, view_size);
2819 break;
2821 else if (optimized_type == tls::TLSOPT_NONE)
2823 // Relocate the field with the offset of the GOT entry for
2824 // the module index.
2825 unsigned int got_offset;
2826 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2827 - target->got_size());
2828 Relocate_functions<32, false>::rel32(view, got_offset);
2829 break;
2831 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2832 _("unsupported reloc %u"),
2833 r_type);
2834 break;
2836 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2837 if (optimized_type == tls::TLSOPT_TO_LE)
2839 // This reloc can appear in debugging sections, in which
2840 // case we must not convert to local-exec. We decide what
2841 // to do based on whether the section is marked as
2842 // containing executable code. That is what the GNU linker
2843 // does as well.
2844 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2845 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2847 if (tls_segment == NULL)
2849 gold_assert(parameters->errors()->error_count() > 0
2850 || issue_undefined_symbol_error(gsym));
2851 return;
2853 value -= tls_segment->memsz();
2856 Relocate_functions<32, false>::rel32(view, value);
2857 break;
2859 case elfcpp::R_386_TLS_IE: // Initial-exec
2860 case elfcpp::R_386_TLS_GOTIE:
2861 case elfcpp::R_386_TLS_IE_32:
2862 if (optimized_type == tls::TLSOPT_TO_LE)
2864 if (tls_segment == NULL)
2866 gold_assert(parameters->errors()->error_count() > 0
2867 || issue_undefined_symbol_error(gsym));
2868 return;
2870 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2871 rel, r_type, value, view,
2872 view_size);
2873 break;
2875 else if (optimized_type == tls::TLSOPT_NONE)
2877 // Relocate the field with the offset of the GOT entry for
2878 // the tp-relative offset of the symbol.
2879 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2880 ? GOT_TYPE_TLS_OFFSET
2881 : GOT_TYPE_TLS_NOFFSET);
2882 unsigned int got_offset;
2883 if (gsym != NULL)
2885 gold_assert(gsym->has_got_offset(got_type));
2886 got_offset = gsym->got_offset(got_type);
2888 else
2890 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2891 gold_assert(object->local_has_got_offset(r_sym, got_type));
2892 got_offset = object->local_got_offset(r_sym, got_type);
2894 // For the R_386_TLS_IE relocation, we need to apply the
2895 // absolute address of the GOT entry.
2896 if (r_type == elfcpp::R_386_TLS_IE)
2897 got_offset += target->got_plt_section()->address();
2898 // All GOT offsets are relative to the end of the GOT.
2899 got_offset -= target->got_size();
2900 Relocate_functions<32, false>::rel32(view, got_offset);
2901 break;
2903 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2904 _("unsupported reloc %u"),
2905 r_type);
2906 break;
2908 case elfcpp::R_386_TLS_LE: // Local-exec
2909 // If we're creating a shared library, a dynamic relocation will
2910 // have been created for this location, so do not apply it now.
2911 if (!parameters->options().shared())
2913 if (tls_segment == NULL)
2915 gold_assert(parameters->errors()->error_count() > 0
2916 || issue_undefined_symbol_error(gsym));
2917 return;
2919 value -= tls_segment->memsz();
2920 Relocate_functions<32, false>::rel32(view, value);
2922 break;
2924 case elfcpp::R_386_TLS_LE_32:
2925 // If we're creating a shared library, a dynamic relocation will
2926 // have been created for this location, so do not apply it now.
2927 if (!parameters->options().shared())
2929 if (tls_segment == NULL)
2931 gold_assert(parameters->errors()->error_count() > 0
2932 || issue_undefined_symbol_error(gsym));
2933 return;
2935 value = tls_segment->memsz() - value;
2936 Relocate_functions<32, false>::rel32(view, value);
2938 break;
2942 // Do a relocation in which we convert a TLS General-Dynamic to a
2943 // Local-Exec.
2945 inline void
2946 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2947 size_t relnum,
2948 Output_segment* tls_segment,
2949 const elfcpp::Rel<32, false>& rel,
2950 unsigned int,
2951 elfcpp::Elf_types<32>::Elf_Addr value,
2952 unsigned char* view,
2953 section_size_type view_size)
2955 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2956 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2957 // leal foo(%reg),%eax; call ___tls_get_addr
2958 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2960 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2961 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2963 unsigned char op1 = view[-1];
2964 unsigned char op2 = view[-2];
2966 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2967 op2 == 0x8d || op2 == 0x04);
2968 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2970 int roff = 5;
2972 if (op2 == 0x04)
2974 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2975 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2976 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2977 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2978 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2980 else
2982 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2983 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2984 if (rel.get_r_offset() + 9 < view_size
2985 && view[9] == 0x90)
2987 // There is a trailing nop. Use the size byte subl.
2988 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2989 roff = 6;
2991 else
2993 // Use the five byte subl.
2994 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2998 value = tls_segment->memsz() - value;
2999 Relocate_functions<32, false>::rel32(view + roff, value);
3001 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3002 // We can skip it.
3003 this->skip_call_tls_get_addr_ = true;
3006 // Do a relocation in which we convert a TLS General-Dynamic to an
3007 // Initial-Exec.
3009 inline void
3010 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3011 size_t relnum,
3012 Output_segment*,
3013 const elfcpp::Rel<32, false>& rel,
3014 unsigned int,
3015 elfcpp::Elf_types<32>::Elf_Addr value,
3016 unsigned char* view,
3017 section_size_type view_size)
3019 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3020 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3022 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3023 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3025 unsigned char op1 = view[-1];
3026 unsigned char op2 = view[-2];
3028 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3029 op2 == 0x8d || op2 == 0x04);
3030 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3032 int roff = 5;
3034 // FIXME: For now, support only the first (SIB) form.
3035 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
3037 if (op2 == 0x04)
3039 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3040 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3041 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3042 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3043 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3045 else
3047 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3048 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
3049 if (rel.get_r_offset() + 9 < view_size
3050 && view[9] == 0x90)
3052 // FIXME: This is not the right instruction sequence.
3053 // There is a trailing nop. Use the size byte subl.
3054 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3055 roff = 6;
3057 else
3059 // FIXME: This is not the right instruction sequence.
3060 // Use the five byte subl.
3061 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3065 Relocate_functions<32, false>::rel32(view + roff, value);
3067 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3068 // We can skip it.
3069 this->skip_call_tls_get_addr_ = true;
3072 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3073 // General-Dynamic to a Local-Exec.
3075 inline void
3076 Target_i386::Relocate::tls_desc_gd_to_le(
3077 const Relocate_info<32, false>* relinfo,
3078 size_t relnum,
3079 Output_segment* tls_segment,
3080 const elfcpp::Rel<32, false>& rel,
3081 unsigned int r_type,
3082 elfcpp::Elf_types<32>::Elf_Addr value,
3083 unsigned char* view,
3084 section_size_type view_size)
3086 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3088 // leal foo@TLSDESC(%ebx), %eax
3089 // ==> leal foo@NTPOFF, %eax
3090 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3091 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3092 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3093 view[-2] == 0x8d && view[-1] == 0x83);
3094 view[-1] = 0x05;
3095 value -= tls_segment->memsz();
3096 Relocate_functions<32, false>::rel32(view, value);
3098 else
3100 // call *foo@TLSCALL(%eax)
3101 // ==> nop; nop
3102 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3103 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3104 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3105 view[0] == 0xff && view[1] == 0x10);
3106 view[0] = 0x66;
3107 view[1] = 0x90;
3111 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3112 // General-Dynamic to an Initial-Exec.
3114 inline void
3115 Target_i386::Relocate::tls_desc_gd_to_ie(
3116 const Relocate_info<32, false>* relinfo,
3117 size_t relnum,
3118 Output_segment*,
3119 const elfcpp::Rel<32, false>& rel,
3120 unsigned int r_type,
3121 elfcpp::Elf_types<32>::Elf_Addr value,
3122 unsigned char* view,
3123 section_size_type view_size)
3125 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3127 // leal foo@TLSDESC(%ebx), %eax
3128 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3129 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3130 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3131 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3132 view[-2] == 0x8d && view[-1] == 0x83);
3133 view[-2] = 0x8b;
3134 Relocate_functions<32, false>::rel32(view, value);
3136 else
3138 // call *foo@TLSCALL(%eax)
3139 // ==> nop; nop
3140 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3141 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3142 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3143 view[0] == 0xff && view[1] == 0x10);
3144 view[0] = 0x66;
3145 view[1] = 0x90;
3149 // Do a relocation in which we convert a TLS Local-Dynamic to a
3150 // Local-Exec.
3152 inline void
3153 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3154 size_t relnum,
3155 Output_segment*,
3156 const elfcpp::Rel<32, false>& rel,
3157 unsigned int,
3158 elfcpp::Elf_types<32>::Elf_Addr,
3159 unsigned char* view,
3160 section_size_type view_size)
3162 // leal foo(%reg), %eax; call ___tls_get_addr
3163 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3165 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3166 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3168 // FIXME: Does this test really always pass?
3169 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3170 view[-2] == 0x8d && view[-1] == 0x83);
3172 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3174 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3176 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3177 // We can skip it.
3178 this->skip_call_tls_get_addr_ = true;
3181 // Do a relocation in which we convert a TLS Initial-Exec to a
3182 // Local-Exec.
3184 inline void
3185 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3186 size_t relnum,
3187 Output_segment* tls_segment,
3188 const elfcpp::Rel<32, false>& rel,
3189 unsigned int r_type,
3190 elfcpp::Elf_types<32>::Elf_Addr value,
3191 unsigned char* view,
3192 section_size_type view_size)
3194 // We have to actually change the instructions, which means that we
3195 // need to examine the opcodes to figure out which instruction we
3196 // are looking at.
3197 if (r_type == elfcpp::R_386_TLS_IE)
3199 // movl %gs:XX,%eax ==> movl $YY,%eax
3200 // movl %gs:XX,%reg ==> movl $YY,%reg
3201 // addl %gs:XX,%reg ==> addl $YY,%reg
3202 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3203 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3205 unsigned char op1 = view[-1];
3206 if (op1 == 0xa1)
3208 // movl XX,%eax ==> movl $YY,%eax
3209 view[-1] = 0xb8;
3211 else
3213 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3215 unsigned char op2 = view[-2];
3216 if (op2 == 0x8b)
3218 // movl XX,%reg ==> movl $YY,%reg
3219 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3220 (op1 & 0xc7) == 0x05);
3221 view[-2] = 0xc7;
3222 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3224 else if (op2 == 0x03)
3226 // addl XX,%reg ==> addl $YY,%reg
3227 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3228 (op1 & 0xc7) == 0x05);
3229 view[-2] = 0x81;
3230 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3232 else
3233 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3236 else
3238 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3239 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3240 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3241 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3242 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3244 unsigned char op1 = view[-1];
3245 unsigned char op2 = view[-2];
3246 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3247 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3248 if (op2 == 0x8b)
3250 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3251 view[-2] = 0xc7;
3252 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3254 else if (op2 == 0x2b)
3256 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3257 view[-2] = 0x81;
3258 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3260 else if (op2 == 0x03)
3262 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3263 view[-2] = 0x81;
3264 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3266 else
3267 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3270 value = tls_segment->memsz() - value;
3271 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3272 value = - value;
3274 Relocate_functions<32, false>::rel32(view, value);
3277 // Relocate section data.
3279 void
3280 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
3281 unsigned int sh_type,
3282 const unsigned char* prelocs,
3283 size_t reloc_count,
3284 Output_section* output_section,
3285 bool needs_special_offset_handling,
3286 unsigned char* view,
3287 elfcpp::Elf_types<32>::Elf_Addr address,
3288 section_size_type view_size,
3289 const Reloc_symbol_changes* reloc_symbol_changes)
3291 gold_assert(sh_type == elfcpp::SHT_REL);
3293 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3294 Target_i386::Relocate>(
3295 relinfo,
3296 this,
3297 prelocs,
3298 reloc_count,
3299 output_section,
3300 needs_special_offset_handling,
3301 view,
3302 address,
3303 view_size,
3304 reloc_symbol_changes);
3307 // Return the size of a relocation while scanning during a relocatable
3308 // link.
3310 unsigned int
3311 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3312 unsigned int r_type,
3313 Relobj* object)
3315 switch (r_type)
3317 case elfcpp::R_386_NONE:
3318 case elfcpp::R_386_GNU_VTINHERIT:
3319 case elfcpp::R_386_GNU_VTENTRY:
3320 case elfcpp::R_386_TLS_GD: // Global-dynamic
3321 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3322 case elfcpp::R_386_TLS_DESC_CALL:
3323 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3324 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3325 case elfcpp::R_386_TLS_IE: // Initial-exec
3326 case elfcpp::R_386_TLS_IE_32:
3327 case elfcpp::R_386_TLS_GOTIE:
3328 case elfcpp::R_386_TLS_LE: // Local-exec
3329 case elfcpp::R_386_TLS_LE_32:
3330 return 0;
3332 case elfcpp::R_386_32:
3333 case elfcpp::R_386_PC32:
3334 case elfcpp::R_386_GOT32:
3335 case elfcpp::R_386_PLT32:
3336 case elfcpp::R_386_GOTOFF:
3337 case elfcpp::R_386_GOTPC:
3338 return 4;
3340 case elfcpp::R_386_16:
3341 case elfcpp::R_386_PC16:
3342 return 2;
3344 case elfcpp::R_386_8:
3345 case elfcpp::R_386_PC8:
3346 return 1;
3348 // These are relocations which should only be seen by the
3349 // dynamic linker, and should never be seen here.
3350 case elfcpp::R_386_COPY:
3351 case elfcpp::R_386_GLOB_DAT:
3352 case elfcpp::R_386_JUMP_SLOT:
3353 case elfcpp::R_386_RELATIVE:
3354 case elfcpp::R_386_IRELATIVE:
3355 case elfcpp::R_386_TLS_TPOFF:
3356 case elfcpp::R_386_TLS_DTPMOD32:
3357 case elfcpp::R_386_TLS_DTPOFF32:
3358 case elfcpp::R_386_TLS_TPOFF32:
3359 case elfcpp::R_386_TLS_DESC:
3360 object->error(_("unexpected reloc %u in object file"), r_type);
3361 return 0;
3363 case elfcpp::R_386_32PLT:
3364 case elfcpp::R_386_TLS_GD_32:
3365 case elfcpp::R_386_TLS_GD_PUSH:
3366 case elfcpp::R_386_TLS_GD_CALL:
3367 case elfcpp::R_386_TLS_GD_POP:
3368 case elfcpp::R_386_TLS_LDM_32:
3369 case elfcpp::R_386_TLS_LDM_PUSH:
3370 case elfcpp::R_386_TLS_LDM_CALL:
3371 case elfcpp::R_386_TLS_LDM_POP:
3372 case elfcpp::R_386_USED_BY_INTEL_200:
3373 default:
3374 object->error(_("unsupported reloc %u in object file"), r_type);
3375 return 0;
3379 // Scan the relocs during a relocatable link.
3381 void
3382 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3383 Layout* layout,
3384 Sized_relobj_file<32, false>* object,
3385 unsigned int data_shndx,
3386 unsigned int sh_type,
3387 const unsigned char* prelocs,
3388 size_t reloc_count,
3389 Output_section* output_section,
3390 bool needs_special_offset_handling,
3391 size_t local_symbol_count,
3392 const unsigned char* plocal_symbols,
3393 Relocatable_relocs* rr)
3395 gold_assert(sh_type == elfcpp::SHT_REL);
3397 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3398 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3400 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3401 Scan_relocatable_relocs>(
3402 symtab,
3403 layout,
3404 object,
3405 data_shndx,
3406 prelocs,
3407 reloc_count,
3408 output_section,
3409 needs_special_offset_handling,
3410 local_symbol_count,
3411 plocal_symbols,
3412 rr);
3415 // Relocate a section during a relocatable link.
3417 void
3418 Target_i386::relocate_for_relocatable(
3419 const Relocate_info<32, false>* relinfo,
3420 unsigned int sh_type,
3421 const unsigned char* prelocs,
3422 size_t reloc_count,
3423 Output_section* output_section,
3424 off_t offset_in_output_section,
3425 const Relocatable_relocs* rr,
3426 unsigned char* view,
3427 elfcpp::Elf_types<32>::Elf_Addr view_address,
3428 section_size_type view_size,
3429 unsigned char* reloc_view,
3430 section_size_type reloc_view_size)
3432 gold_assert(sh_type == elfcpp::SHT_REL);
3434 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3435 relinfo,
3436 prelocs,
3437 reloc_count,
3438 output_section,
3439 offset_in_output_section,
3441 view,
3442 view_address,
3443 view_size,
3444 reloc_view,
3445 reloc_view_size);
3448 // Return the value to use for a dynamic which requires special
3449 // treatment. This is how we support equality comparisons of function
3450 // pointers across shared library boundaries, as described in the
3451 // processor specific ABI supplement.
3453 uint64_t
3454 Target_i386::do_dynsym_value(const Symbol* gsym) const
3456 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3457 return this->plt_address_for_global(gsym) + gsym->plt_offset();
3460 // Return a string used to fill a code section with nops to take up
3461 // the specified length.
3463 std::string
3464 Target_i386::do_code_fill(section_size_type length) const
3466 if (length >= 16)
3468 // Build a jmp instruction to skip over the bytes.
3469 unsigned char jmp[5];
3470 jmp[0] = 0xe9;
3471 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3472 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3473 + std::string(length - 5, '\0'));
3476 // Nop sequences of various lengths.
3477 const char nop1[1] = { '\x90' }; // nop
3478 const char nop2[2] = { '\x66', '\x90' }; // xchg %ax %ax
3479 const char nop3[3] = { '\x8d', '\x76', '\x00' }; // leal 0(%esi),%esi
3480 const char nop4[4] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3481 '\x00'};
3482 const char nop5[5] = { '\x90', '\x8d', '\x74', // nop
3483 '\x26', '\x00' }; // leal 0(%esi,1),%esi
3484 const char nop6[6] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3485 '\x00', '\x00', '\x00' };
3486 const char nop7[7] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3487 '\x00', '\x00', '\x00',
3488 '\x00' };
3489 const char nop8[8] = { '\x90', '\x8d', '\xb4', // nop
3490 '\x26', '\x00', '\x00', // leal 0L(%esi,1),%esi
3491 '\x00', '\x00' };
3492 const char nop9[9] = { '\x89', '\xf6', '\x8d', // movl %esi,%esi
3493 '\xbc', '\x27', '\x00', // leal 0L(%edi,1),%edi
3494 '\x00', '\x00', '\x00' };
3495 const char nop10[10] = { '\x8d', '\x76', '\x00', // leal 0(%esi),%esi
3496 '\x8d', '\xbc', '\x27', // leal 0L(%edi,1),%edi
3497 '\x00', '\x00', '\x00',
3498 '\x00' };
3499 const char nop11[11] = { '\x8d', '\x74', '\x26', // leal 0(%esi,1),%esi
3500 '\x00', '\x8d', '\xbc', // leal 0L(%edi,1),%edi
3501 '\x27', '\x00', '\x00',
3502 '\x00', '\x00' };
3503 const char nop12[12] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3504 '\x00', '\x00', '\x00', // leal 0L(%edi),%edi
3505 '\x8d', '\xbf', '\x00',
3506 '\x00', '\x00', '\x00' };
3507 const char nop13[13] = { '\x8d', '\xb6', '\x00', // leal 0L(%esi),%esi
3508 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3509 '\x8d', '\xbc', '\x27',
3510 '\x00', '\x00', '\x00',
3511 '\x00' };
3512 const char nop14[14] = { '\x8d', '\xb4', '\x26', // leal 0L(%esi,1),%esi
3513 '\x00', '\x00', '\x00', // leal 0L(%edi,1),%edi
3514 '\x00', '\x8d', '\xbc',
3515 '\x27', '\x00', '\x00',
3516 '\x00', '\x00' };
3517 const char nop15[15] = { '\xeb', '\x0d', '\x90', // jmp .+15
3518 '\x90', '\x90', '\x90', // nop,nop,nop,...
3519 '\x90', '\x90', '\x90',
3520 '\x90', '\x90', '\x90',
3521 '\x90', '\x90', '\x90' };
3523 const char* nops[16] = {
3524 NULL,
3525 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3526 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3529 return std::string(nops[length], length);
3532 // Return the value to use for the base of a DW_EH_PE_datarel offset
3533 // in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3534 // assembler can not write out the difference between two labels in
3535 // different sections, so instead of using a pc-relative value they
3536 // use an offset from the GOT.
3538 uint64_t
3539 Target_i386::do_ehframe_datarel_base() const
3541 gold_assert(this->global_offset_table_ != NULL);
3542 Symbol* sym = this->global_offset_table_;
3543 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3544 return ssym->value();
3547 // Return whether SYM should be treated as a call to a non-split
3548 // function. We don't want that to be true of a call to a
3549 // get_pc_thunk function.
3551 bool
3552 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3554 return (sym->type() == elfcpp::STT_FUNC
3555 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3558 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3559 // compiled with -fsplit-stack. The function calls non-split-stack
3560 // code. We have to change the function so that it always ensures
3561 // that it has enough stack space to run some random function.
3563 void
3564 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3565 section_offset_type fnoffset,
3566 section_size_type fnsize,
3567 unsigned char* view,
3568 section_size_type view_size,
3569 std::string* from,
3570 std::string* to) const
3572 // The function starts with a comparison of the stack pointer and a
3573 // field in the TCB. This is followed by a jump.
3575 // cmp %gs:NN,%esp
3576 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3577 && fnsize > 7)
3579 // We will call __morestack if the carry flag is set after this
3580 // comparison. We turn the comparison into an stc instruction
3581 // and some nops.
3582 view[fnoffset] = '\xf9';
3583 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3585 // lea NN(%esp),%ecx
3586 // lea NN(%esp),%edx
3587 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3588 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3589 && fnsize > 7)
3591 // This is loading an offset from the stack pointer for a
3592 // comparison. The offset is negative, so we decrease the
3593 // offset by the amount of space we need for the stack. This
3594 // means we will avoid calling __morestack if there happens to
3595 // be plenty of space on the stack already.
3596 unsigned char* pval = view + fnoffset + 3;
3597 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3598 val -= parameters->options().split_stack_adjust_size();
3599 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3601 else
3603 if (!object->has_no_split_stack())
3604 object->error(_("failed to match split-stack sequence at "
3605 "section %u offset %0zx"),
3606 shndx, static_cast<size_t>(fnoffset));
3607 return;
3610 // We have to change the function so that it calls
3611 // __morestack_non_split instead of __morestack. The former will
3612 // allocate additional stack space.
3613 *from = "__morestack";
3614 *to = "__morestack_non_split";
3617 // The selector for i386 object files.
3619 class Target_selector_i386 : public Target_selector_freebsd
3621 public:
3622 Target_selector_i386()
3623 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3624 "elf32-i386", "elf32-i386-freebsd",
3625 "elf_i386")
3628 Target*
3629 do_instantiate_target()
3630 { return new Target_i386(); }
3633 Target_selector_i386 target_selector_i386;
3635 } // End anonymous namespace.