Fix PE-COFF archiving long name tables
[binutils/dougsmingw.git] / gold / i386.cc
blob1776383249f58ba2f90f371a74e3b26aa8d7b54c
1 // i386.cc -- i386 target support for gold.
3 // Copyright 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
23 #include "gold.h"
25 #include <cstring>
27 #include "elfcpp.h"
28 #include "parameters.h"
29 #include "reloc.h"
30 #include "i386.h"
31 #include "object.h"
32 #include "symtab.h"
33 #include "layout.h"
34 #include "output.h"
35 #include "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40 #include "freebsd.h"
41 #include "gc.h"
43 namespace
46 using namespace gold;
48 class Output_data_plt_i386;
50 // The i386 target class.
51 // TLS info comes from
52 // http://people.redhat.com/drepper/tls.pdf
53 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
55 class Target_i386 : public Target_freebsd<32, false>
57 public:
58 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
60 Target_i386()
61 : Target_freebsd<32, false>(&i386_info),
62 got_(NULL), plt_(NULL), got_plt_(NULL), global_offset_table_(NULL),
63 rel_dyn_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
64 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
65 { }
67 inline bool
68 can_check_for_function_pointers() const
69 { return true; }
71 // Process the relocations to determine unreferenced sections for
72 // garbage collection.
73 void
74 gc_process_relocs(Symbol_table* symtab,
75 Layout* layout,
76 Sized_relobj<32, false>* object,
77 unsigned int data_shndx,
78 unsigned int sh_type,
79 const unsigned char* prelocs,
80 size_t reloc_count,
81 Output_section* output_section,
82 bool needs_special_offset_handling,
83 size_t local_symbol_count,
84 const unsigned char* plocal_symbols);
86 // Scan the relocations to look for symbol adjustments.
87 void
88 scan_relocs(Symbol_table* symtab,
89 Layout* layout,
90 Sized_relobj<32, false>* object,
91 unsigned int data_shndx,
92 unsigned int sh_type,
93 const unsigned char* prelocs,
94 size_t reloc_count,
95 Output_section* output_section,
96 bool needs_special_offset_handling,
97 size_t local_symbol_count,
98 const unsigned char* plocal_symbols);
100 // Finalize the sections.
101 void
102 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
104 // Return the value to use for a dynamic which requires special
105 // treatment.
106 uint64_t
107 do_dynsym_value(const Symbol*) const;
109 // Relocate a section.
110 void
111 relocate_section(const Relocate_info<32, false>*,
112 unsigned int sh_type,
113 const unsigned char* prelocs,
114 size_t reloc_count,
115 Output_section* output_section,
116 bool needs_special_offset_handling,
117 unsigned char* view,
118 elfcpp::Elf_types<32>::Elf_Addr view_address,
119 section_size_type view_size,
120 const Reloc_symbol_changes*);
122 // Scan the relocs during a relocatable link.
123 void
124 scan_relocatable_relocs(Symbol_table* symtab,
125 Layout* layout,
126 Sized_relobj<32, false>* object,
127 unsigned int data_shndx,
128 unsigned int sh_type,
129 const unsigned char* prelocs,
130 size_t reloc_count,
131 Output_section* output_section,
132 bool needs_special_offset_handling,
133 size_t local_symbol_count,
134 const unsigned char* plocal_symbols,
135 Relocatable_relocs*);
137 // Relocate a section during a relocatable link.
138 void
139 relocate_for_relocatable(const Relocate_info<32, false>*,
140 unsigned int sh_type,
141 const unsigned char* prelocs,
142 size_t reloc_count,
143 Output_section* output_section,
144 off_t offset_in_output_section,
145 const Relocatable_relocs*,
146 unsigned char* view,
147 elfcpp::Elf_types<32>::Elf_Addr view_address,
148 section_size_type view_size,
149 unsigned char* reloc_view,
150 section_size_type reloc_view_size);
152 // Return a string used to fill a code section with nops.
153 std::string
154 do_code_fill(section_size_type length) const;
156 // Return whether SYM is defined by the ABI.
157 bool
158 do_is_defined_by_abi(const Symbol* sym) const
159 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
161 // Return whether a symbol name implies a local label. The UnixWare
162 // 2.1 cc generates temporary symbols that start with .X, so we
163 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
164 // If so, we should move the .X recognition into
165 // Target::do_is_local_label_name.
166 bool
167 do_is_local_label_name(const char* name) const
169 if (name[0] == '.' && name[1] == 'X')
170 return true;
171 return Target::do_is_local_label_name(name);
174 // Return whether SYM is call to a non-split function.
175 bool
176 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
178 // Adjust -fstack-split code which calls non-stack-split code.
179 void
180 do_calls_non_split(Relobj* object, unsigned int shndx,
181 section_offset_type fnoffset, section_size_type fnsize,
182 unsigned char* view, section_size_type view_size,
183 std::string* from, std::string* to) const;
185 // Return the size of the GOT section.
186 section_size_type
187 got_size()
189 gold_assert(this->got_ != NULL);
190 return this->got_->data_size();
193 private:
194 // The class which scans relocations.
195 struct Scan
197 inline void
198 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
199 Sized_relobj<32, false>* object,
200 unsigned int data_shndx,
201 Output_section* output_section,
202 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
203 const elfcpp::Sym<32, false>& lsym);
205 inline void
206 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
207 Sized_relobj<32, false>* object,
208 unsigned int data_shndx,
209 Output_section* output_section,
210 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
211 Symbol* gsym);
213 inline bool
214 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
215 Target_i386* target,
216 Sized_relobj<32, false>* object,
217 unsigned int data_shndx,
218 Output_section* output_section,
219 const elfcpp::Rel<32, false>& reloc,
220 unsigned int r_type,
221 const elfcpp::Sym<32, false>& lsym);
223 inline bool
224 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
225 Target_i386* target,
226 Sized_relobj<32, false>* object,
227 unsigned int data_shndx,
228 Output_section* output_section,
229 const elfcpp::Rel<32, false>& reloc,
230 unsigned int r_type,
231 Symbol* gsym);
233 inline bool
234 possible_function_pointer_reloc(unsigned int r_type);
236 static void
237 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
239 static void
240 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
241 Symbol*);
244 // The class which implements relocation.
245 class Relocate
247 public:
248 Relocate()
249 : skip_call_tls_get_addr_(false),
250 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
253 ~Relocate()
255 if (this->skip_call_tls_get_addr_)
257 // FIXME: This needs to specify the location somehow.
258 gold_error(_("missing expected TLS relocation"));
262 // Return whether the static relocation needs to be applied.
263 inline bool
264 should_apply_static_reloc(const Sized_symbol<32>* gsym,
265 int ref_flags,
266 bool is_32bit,
267 Output_section* output_section);
269 // Do a relocation. Return false if the caller should not issue
270 // any warnings about this relocation.
271 inline bool
272 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
273 size_t relnum, const elfcpp::Rel<32, false>&,
274 unsigned int r_type, const Sized_symbol<32>*,
275 const Symbol_value<32>*,
276 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
277 section_size_type);
279 private:
280 // Do a TLS relocation.
281 inline void
282 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
283 size_t relnum, const elfcpp::Rel<32, false>&,
284 unsigned int r_type, const Sized_symbol<32>*,
285 const Symbol_value<32>*,
286 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
287 section_size_type);
289 // Do a TLS General-Dynamic to Initial-Exec transition.
290 inline void
291 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
292 Output_segment* tls_segment,
293 const elfcpp::Rel<32, false>&, unsigned int r_type,
294 elfcpp::Elf_types<32>::Elf_Addr value,
295 unsigned char* view,
296 section_size_type view_size);
298 // Do a TLS General-Dynamic to Local-Exec transition.
299 inline void
300 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
301 Output_segment* tls_segment,
302 const elfcpp::Rel<32, false>&, unsigned int r_type,
303 elfcpp::Elf_types<32>::Elf_Addr value,
304 unsigned char* view,
305 section_size_type view_size);
307 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
308 // transition.
309 inline void
310 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
311 Output_segment* tls_segment,
312 const elfcpp::Rel<32, false>&, unsigned int r_type,
313 elfcpp::Elf_types<32>::Elf_Addr value,
314 unsigned char* view,
315 section_size_type view_size);
317 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
318 // transition.
319 inline void
320 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
321 Output_segment* tls_segment,
322 const elfcpp::Rel<32, false>&, unsigned int r_type,
323 elfcpp::Elf_types<32>::Elf_Addr value,
324 unsigned char* view,
325 section_size_type view_size);
327 // Do a TLS Local-Dynamic to Local-Exec transition.
328 inline void
329 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
330 Output_segment* tls_segment,
331 const elfcpp::Rel<32, false>&, unsigned int r_type,
332 elfcpp::Elf_types<32>::Elf_Addr value,
333 unsigned char* view,
334 section_size_type view_size);
336 // Do a TLS Initial-Exec to Local-Exec transition.
337 static inline void
338 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
339 Output_segment* tls_segment,
340 const elfcpp::Rel<32, false>&, unsigned int r_type,
341 elfcpp::Elf_types<32>::Elf_Addr value,
342 unsigned char* view,
343 section_size_type view_size);
345 // We need to keep track of which type of local dynamic relocation
346 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
347 enum Local_dynamic_type
349 LOCAL_DYNAMIC_NONE,
350 LOCAL_DYNAMIC_SUN,
351 LOCAL_DYNAMIC_GNU
354 // This is set if we should skip the next reloc, which should be a
355 // PLT32 reloc against ___tls_get_addr.
356 bool skip_call_tls_get_addr_;
357 // The type of local dynamic relocation we have seen in the section
358 // being relocated, if any.
359 Local_dynamic_type local_dynamic_type_;
362 // A class which returns the size required for a relocation type,
363 // used while scanning relocs during a relocatable link.
364 class Relocatable_size_for_reloc
366 public:
367 unsigned int
368 get_size_for_reloc(unsigned int, Relobj*);
371 // Adjust TLS relocation type based on the options and whether this
372 // is a local symbol.
373 static tls::Tls_optimization
374 optimize_tls_reloc(bool is_final, int r_type);
376 // Get the GOT section, creating it if necessary.
377 Output_data_got<32, false>*
378 got_section(Symbol_table*, Layout*);
380 // Get the GOT PLT section.
381 Output_data_space*
382 got_plt_section() const
384 gold_assert(this->got_plt_ != NULL);
385 return this->got_plt_;
388 // Create a PLT entry for a global symbol.
389 void
390 make_plt_entry(Symbol_table*, Layout*, Symbol*);
392 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
393 void
394 define_tls_base_symbol(Symbol_table*, Layout*);
396 // Create a GOT entry for the TLS module index.
397 unsigned int
398 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
399 Sized_relobj<32, false>* object);
401 // Get the PLT section.
402 Output_data_plt_i386*
403 plt_section() const
405 gold_assert(this->plt_ != NULL);
406 return this->plt_;
409 // Get the dynamic reloc section, creating it if necessary.
410 Reloc_section*
411 rel_dyn_section(Layout*);
413 // Get the section to use for TLS_DESC relocations.
414 Reloc_section*
415 rel_tls_desc_section(Layout*) const;
417 // Add a potential copy relocation.
418 void
419 copy_reloc(Symbol_table* symtab, Layout* layout,
420 Sized_relobj<32, false>* object,
421 unsigned int shndx, Output_section* output_section,
422 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
424 this->copy_relocs_.copy_reloc(symtab, layout,
425 symtab->get_sized_symbol<32>(sym),
426 object, shndx, output_section, reloc,
427 this->rel_dyn_section(layout));
430 // Information about this specific target which we pass to the
431 // general Target structure.
432 static const Target::Target_info i386_info;
434 // The types of GOT entries needed for this platform.
435 enum Got_type
437 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
438 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
439 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
440 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
441 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
444 // The GOT section.
445 Output_data_got<32, false>* got_;
446 // The PLT section.
447 Output_data_plt_i386* plt_;
448 // The GOT PLT section.
449 Output_data_space* got_plt_;
450 // The _GLOBAL_OFFSET_TABLE_ symbol.
451 Symbol* global_offset_table_;
452 // The dynamic reloc section.
453 Reloc_section* rel_dyn_;
454 // Relocs saved to avoid a COPY reloc.
455 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
456 // Space for variables copied with a COPY reloc.
457 Output_data_space* dynbss_;
458 // Offset of the GOT entry for the TLS module index.
459 unsigned int got_mod_index_offset_;
460 // True if the _TLS_MODULE_BASE_ symbol has been defined.
461 bool tls_base_symbol_defined_;
464 const Target::Target_info Target_i386::i386_info =
466 32, // size
467 false, // is_big_endian
468 elfcpp::EM_386, // machine_code
469 false, // has_make_symbol
470 false, // has_resolve
471 true, // has_code_fill
472 true, // is_default_stack_executable
473 '\0', // wrap_char
474 "/usr/lib/libc.so.1", // dynamic_linker
475 0x08048000, // default_text_segment_address
476 0x1000, // abi_pagesize (overridable by -z max-page-size)
477 0x1000, // common_pagesize (overridable by -z common-page-size)
478 elfcpp::SHN_UNDEF, // small_common_shndx
479 elfcpp::SHN_UNDEF, // large_common_shndx
480 0, // small_common_section_flags
481 0, // large_common_section_flags
482 NULL, // attributes_section
483 NULL // attributes_vendor
486 // Get the GOT section, creating it if necessary.
488 Output_data_got<32, false>*
489 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
491 if (this->got_ == NULL)
493 gold_assert(symtab != NULL && layout != NULL);
495 this->got_ = new Output_data_got<32, false>();
497 Output_section* os;
498 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
499 (elfcpp::SHF_ALLOC
500 | elfcpp::SHF_WRITE),
501 this->got_, false, true, true,
502 false);
504 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
505 os = layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
506 (elfcpp::SHF_ALLOC
507 | elfcpp::SHF_WRITE),
508 this->got_plt_, false, false, false,
509 true);
511 // The first three entries are reserved.
512 this->got_plt_->set_current_data_size(3 * 4);
514 // Those bytes can go into the relro segment.
515 layout->increase_relro(3 * 4);
517 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
518 this->global_offset_table_ =
519 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
520 Symbol_table::PREDEFINED,
521 this->got_plt_,
522 0, 0, elfcpp::STT_OBJECT,
523 elfcpp::STB_LOCAL,
524 elfcpp::STV_HIDDEN, 0,
525 false, false);
528 return this->got_;
531 // Get the dynamic reloc section, creating it if necessary.
533 Target_i386::Reloc_section*
534 Target_i386::rel_dyn_section(Layout* layout)
536 if (this->rel_dyn_ == NULL)
538 gold_assert(layout != NULL);
539 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
540 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
541 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
542 false, false, false);
544 return this->rel_dyn_;
547 // A class to handle the PLT data.
549 class Output_data_plt_i386 : public Output_section_data
551 public:
552 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
554 Output_data_plt_i386(Layout*, Output_data_space*);
556 // Add an entry to the PLT.
557 void
558 add_entry(Symbol* gsym);
560 // Return the .rel.plt section data.
561 const Reloc_section*
562 rel_plt() const
563 { return this->rel_; }
565 // Return where the TLS_DESC relocations should go.
566 Reloc_section*
567 rel_tls_desc(Layout*);
569 protected:
570 void
571 do_adjust_output_section(Output_section* os);
573 // Write to a map file.
574 void
575 do_print_to_mapfile(Mapfile* mapfile) const
576 { mapfile->print_output_data(this, _("** PLT")); }
578 private:
579 // The size of an entry in the PLT.
580 static const int plt_entry_size = 16;
582 // The first entry in the PLT for an executable.
583 static unsigned char exec_first_plt_entry[plt_entry_size];
585 // The first entry in the PLT for a shared object.
586 static unsigned char dyn_first_plt_entry[plt_entry_size];
588 // Other entries in the PLT for an executable.
589 static unsigned char exec_plt_entry[plt_entry_size];
591 // Other entries in the PLT for a shared object.
592 static unsigned char dyn_plt_entry[plt_entry_size];
594 // Set the final size.
595 void
596 set_final_data_size()
597 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
599 // Write out the PLT data.
600 void
601 do_write(Output_file*);
603 // The reloc section.
604 Reloc_section* rel_;
605 // The TLS_DESC relocations, if necessary. These must follow the
606 // regular PLT relocs.
607 Reloc_section* tls_desc_rel_;
608 // The .got.plt section.
609 Output_data_space* got_plt_;
610 // The number of PLT entries.
611 unsigned int count_;
614 // Create the PLT section. The ordinary .got section is an argument,
615 // since we need to refer to the start. We also create our own .got
616 // section just for PLT entries.
618 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
619 Output_data_space* got_plt)
620 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0)
622 this->rel_ = new Reloc_section(false);
623 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
624 elfcpp::SHF_ALLOC, this->rel_, true,
625 false, false, false);
628 void
629 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
631 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
632 // linker, and so do we.
633 os->set_entsize(4);
636 // Add an entry to the PLT.
638 void
639 Output_data_plt_i386::add_entry(Symbol* gsym)
641 gold_assert(!gsym->has_plt_offset());
643 // Note that when setting the PLT offset we skip the initial
644 // reserved PLT entry.
645 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
647 ++this->count_;
649 section_offset_type got_offset = this->got_plt_->current_data_size();
651 // Every PLT entry needs a GOT entry which points back to the PLT
652 // entry (this will be changed by the dynamic linker, normally
653 // lazily when the function is called).
654 this->got_plt_->set_current_data_size(got_offset + 4);
656 // Every PLT entry needs a reloc.
657 gsym->set_needs_dynsym_entry();
658 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
659 got_offset);
661 // Note that we don't need to save the symbol. The contents of the
662 // PLT are independent of which symbols are used. The symbols only
663 // appear in the relocations.
666 // Return where the TLS_DESC relocations should go, creating it if
667 // necessary. These follow the JUMP_SLOT relocations.
669 Output_data_plt_i386::Reloc_section*
670 Output_data_plt_i386::rel_tls_desc(Layout* layout)
672 if (this->tls_desc_rel_ == NULL)
674 this->tls_desc_rel_ = new Reloc_section(false);
675 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
676 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
677 true, false, false, false);
678 gold_assert(this->tls_desc_rel_->output_section() ==
679 this->rel_->output_section());
681 return this->tls_desc_rel_;
684 // The first entry in the PLT for an executable.
686 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
688 0xff, 0x35, // pushl contents of memory address
689 0, 0, 0, 0, // replaced with address of .got + 4
690 0xff, 0x25, // jmp indirect
691 0, 0, 0, 0, // replaced with address of .got + 8
692 0, 0, 0, 0 // unused
695 // The first entry in the PLT for a shared object.
697 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
699 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
700 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
701 0, 0, 0, 0 // unused
704 // Subsequent entries in the PLT for an executable.
706 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
708 0xff, 0x25, // jmp indirect
709 0, 0, 0, 0, // replaced with address of symbol in .got
710 0x68, // pushl immediate
711 0, 0, 0, 0, // replaced with offset into relocation table
712 0xe9, // jmp relative
713 0, 0, 0, 0 // replaced with offset to start of .plt
716 // Subsequent entries in the PLT for a shared object.
718 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
720 0xff, 0xa3, // jmp *offset(%ebx)
721 0, 0, 0, 0, // replaced with offset of symbol in .got
722 0x68, // pushl immediate
723 0, 0, 0, 0, // replaced with offset into relocation table
724 0xe9, // jmp relative
725 0, 0, 0, 0 // replaced with offset to start of .plt
728 // Write out the PLT. This uses the hand-coded instructions above,
729 // and adjusts them as needed. This is all specified by the i386 ELF
730 // Processor Supplement.
732 void
733 Output_data_plt_i386::do_write(Output_file* of)
735 const off_t offset = this->offset();
736 const section_size_type oview_size =
737 convert_to_section_size_type(this->data_size());
738 unsigned char* const oview = of->get_output_view(offset, oview_size);
740 const off_t got_file_offset = this->got_plt_->offset();
741 const section_size_type got_size =
742 convert_to_section_size_type(this->got_plt_->data_size());
743 unsigned char* const got_view = of->get_output_view(got_file_offset,
744 got_size);
746 unsigned char* pov = oview;
748 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
749 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
751 if (parameters->options().output_is_position_independent())
752 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
753 else
755 memcpy(pov, exec_first_plt_entry, plt_entry_size);
756 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
757 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
759 pov += plt_entry_size;
761 unsigned char* got_pov = got_view;
763 memset(got_pov, 0, 12);
764 got_pov += 12;
766 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
768 unsigned int plt_offset = plt_entry_size;
769 unsigned int plt_rel_offset = 0;
770 unsigned int got_offset = 12;
771 const unsigned int count = this->count_;
772 for (unsigned int i = 0;
773 i < count;
774 ++i,
775 pov += plt_entry_size,
776 got_pov += 4,
777 plt_offset += plt_entry_size,
778 plt_rel_offset += rel_size,
779 got_offset += 4)
781 // Set and adjust the PLT entry itself.
783 if (parameters->options().output_is_position_independent())
785 memcpy(pov, dyn_plt_entry, plt_entry_size);
786 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
788 else
790 memcpy(pov, exec_plt_entry, plt_entry_size);
791 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
792 (got_address
793 + got_offset));
796 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
797 elfcpp::Swap<32, false>::writeval(pov + 12,
798 - (plt_offset + plt_entry_size));
800 // Set the entry in the GOT.
801 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
804 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
805 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
807 of->write_output_view(offset, oview_size, oview);
808 of->write_output_view(got_file_offset, got_size, got_view);
811 // Create a PLT entry for a global symbol.
813 void
814 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
816 if (gsym->has_plt_offset())
817 return;
819 if (this->plt_ == NULL)
821 // Create the GOT sections first.
822 this->got_section(symtab, layout);
824 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
825 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
826 (elfcpp::SHF_ALLOC
827 | elfcpp::SHF_EXECINSTR),
828 this->plt_, false, false, false, false);
831 this->plt_->add_entry(gsym);
834 // Get the section to use for TLS_DESC relocations.
836 Target_i386::Reloc_section*
837 Target_i386::rel_tls_desc_section(Layout* layout) const
839 return this->plt_section()->rel_tls_desc(layout);
842 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
844 void
845 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
847 if (this->tls_base_symbol_defined_)
848 return;
850 Output_segment* tls_segment = layout->tls_segment();
851 if (tls_segment != NULL)
853 bool is_exec = parameters->options().output_is_executable();
854 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
855 Symbol_table::PREDEFINED,
856 tls_segment, 0, 0,
857 elfcpp::STT_TLS,
858 elfcpp::STB_LOCAL,
859 elfcpp::STV_HIDDEN, 0,
860 (is_exec
861 ? Symbol::SEGMENT_END
862 : Symbol::SEGMENT_START),
863 true);
865 this->tls_base_symbol_defined_ = true;
868 // Create a GOT entry for the TLS module index.
870 unsigned int
871 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
872 Sized_relobj<32, false>* object)
874 if (this->got_mod_index_offset_ == -1U)
876 gold_assert(symtab != NULL && layout != NULL && object != NULL);
877 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
878 Output_data_got<32, false>* got = this->got_section(symtab, layout);
879 unsigned int got_offset = got->add_constant(0);
880 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
881 got_offset);
882 got->add_constant(0);
883 this->got_mod_index_offset_ = got_offset;
885 return this->got_mod_index_offset_;
888 // Optimize the TLS relocation type based on what we know about the
889 // symbol. IS_FINAL is true if the final address of this symbol is
890 // known at link time.
892 tls::Tls_optimization
893 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
895 // If we are generating a shared library, then we can't do anything
896 // in the linker.
897 if (parameters->options().shared())
898 return tls::TLSOPT_NONE;
900 switch (r_type)
902 case elfcpp::R_386_TLS_GD:
903 case elfcpp::R_386_TLS_GOTDESC:
904 case elfcpp::R_386_TLS_DESC_CALL:
905 // These are General-Dynamic which permits fully general TLS
906 // access. Since we know that we are generating an executable,
907 // we can convert this to Initial-Exec. If we also know that
908 // this is a local symbol, we can further switch to Local-Exec.
909 if (is_final)
910 return tls::TLSOPT_TO_LE;
911 return tls::TLSOPT_TO_IE;
913 case elfcpp::R_386_TLS_LDM:
914 // This is Local-Dynamic, which refers to a local symbol in the
915 // dynamic TLS block. Since we know that we generating an
916 // executable, we can switch to Local-Exec.
917 return tls::TLSOPT_TO_LE;
919 case elfcpp::R_386_TLS_LDO_32:
920 // Another type of Local-Dynamic relocation.
921 return tls::TLSOPT_TO_LE;
923 case elfcpp::R_386_TLS_IE:
924 case elfcpp::R_386_TLS_GOTIE:
925 case elfcpp::R_386_TLS_IE_32:
926 // These are Initial-Exec relocs which get the thread offset
927 // from the GOT. If we know that we are linking against the
928 // local symbol, we can switch to Local-Exec, which links the
929 // thread offset into the instruction.
930 if (is_final)
931 return tls::TLSOPT_TO_LE;
932 return tls::TLSOPT_NONE;
934 case elfcpp::R_386_TLS_LE:
935 case elfcpp::R_386_TLS_LE_32:
936 // When we already have Local-Exec, there is nothing further we
937 // can do.
938 return tls::TLSOPT_NONE;
940 default:
941 gold_unreachable();
945 // Report an unsupported relocation against a local symbol.
947 void
948 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
949 unsigned int r_type)
951 gold_error(_("%s: unsupported reloc %u against local symbol"),
952 object->name().c_str(), r_type);
955 // Scan a relocation for a local symbol.
957 inline void
958 Target_i386::Scan::local(Symbol_table* symtab,
959 Layout* layout,
960 Target_i386* target,
961 Sized_relobj<32, false>* object,
962 unsigned int data_shndx,
963 Output_section* output_section,
964 const elfcpp::Rel<32, false>& reloc,
965 unsigned int r_type,
966 const elfcpp::Sym<32, false>& lsym)
968 switch (r_type)
970 case elfcpp::R_386_NONE:
971 case elfcpp::R_386_GNU_VTINHERIT:
972 case elfcpp::R_386_GNU_VTENTRY:
973 break;
975 case elfcpp::R_386_32:
976 // If building a shared library (or a position-independent
977 // executable), we need to create a dynamic relocation for
978 // this location. The relocation applied at link time will
979 // apply the link-time value, so we flag the location with
980 // an R_386_RELATIVE relocation so the dynamic loader can
981 // relocate it easily.
982 if (parameters->options().output_is_position_independent())
984 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
985 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
986 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
987 output_section, data_shndx,
988 reloc.get_r_offset());
990 break;
992 case elfcpp::R_386_16:
993 case elfcpp::R_386_8:
994 // If building a shared library (or a position-independent
995 // executable), we need to create a dynamic relocation for
996 // this location. Because the addend needs to remain in the
997 // data section, we need to be careful not to apply this
998 // relocation statically.
999 if (parameters->options().output_is_position_independent())
1001 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1002 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1003 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1004 rel_dyn->add_local(object, r_sym, r_type, output_section,
1005 data_shndx, reloc.get_r_offset());
1006 else
1008 gold_assert(lsym.get_st_value() == 0);
1009 unsigned int shndx = lsym.get_st_shndx();
1010 bool is_ordinary;
1011 shndx = object->adjust_sym_shndx(r_sym, shndx,
1012 &is_ordinary);
1013 if (!is_ordinary)
1014 object->error(_("section symbol %u has bad shndx %u"),
1015 r_sym, shndx);
1016 else
1017 rel_dyn->add_local_section(object, shndx,
1018 r_type, output_section,
1019 data_shndx, reloc.get_r_offset());
1022 break;
1024 case elfcpp::R_386_PC32:
1025 case elfcpp::R_386_PC16:
1026 case elfcpp::R_386_PC8:
1027 break;
1029 case elfcpp::R_386_PLT32:
1030 // Since we know this is a local symbol, we can handle this as a
1031 // PC32 reloc.
1032 break;
1034 case elfcpp::R_386_GOTOFF:
1035 case elfcpp::R_386_GOTPC:
1036 // We need a GOT section.
1037 target->got_section(symtab, layout);
1038 break;
1040 case elfcpp::R_386_GOT32:
1042 // The symbol requires a GOT entry.
1043 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1044 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1045 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1047 // If we are generating a shared object, we need to add a
1048 // dynamic RELATIVE relocation for this symbol's GOT entry.
1049 if (parameters->options().output_is_position_independent())
1051 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1052 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1053 rel_dyn->add_local_relative(
1054 object, r_sym, elfcpp::R_386_RELATIVE, got,
1055 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1059 break;
1061 // These are relocations which should only be seen by the
1062 // dynamic linker, and should never be seen here.
1063 case elfcpp::R_386_COPY:
1064 case elfcpp::R_386_GLOB_DAT:
1065 case elfcpp::R_386_JUMP_SLOT:
1066 case elfcpp::R_386_RELATIVE:
1067 case elfcpp::R_386_TLS_TPOFF:
1068 case elfcpp::R_386_TLS_DTPMOD32:
1069 case elfcpp::R_386_TLS_DTPOFF32:
1070 case elfcpp::R_386_TLS_TPOFF32:
1071 case elfcpp::R_386_TLS_DESC:
1072 gold_error(_("%s: unexpected reloc %u in object file"),
1073 object->name().c_str(), r_type);
1074 break;
1076 // These are initial TLS relocs, which are expected when
1077 // linking.
1078 case elfcpp::R_386_TLS_GD: // Global-dynamic
1079 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1080 case elfcpp::R_386_TLS_DESC_CALL:
1081 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1082 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1083 case elfcpp::R_386_TLS_IE: // Initial-exec
1084 case elfcpp::R_386_TLS_IE_32:
1085 case elfcpp::R_386_TLS_GOTIE:
1086 case elfcpp::R_386_TLS_LE: // Local-exec
1087 case elfcpp::R_386_TLS_LE_32:
1089 bool output_is_shared = parameters->options().shared();
1090 const tls::Tls_optimization optimized_type
1091 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1092 switch (r_type)
1094 case elfcpp::R_386_TLS_GD: // Global-dynamic
1095 if (optimized_type == tls::TLSOPT_NONE)
1097 // Create a pair of GOT entries for the module index and
1098 // dtv-relative offset.
1099 Output_data_got<32, false>* got
1100 = target->got_section(symtab, layout);
1101 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1102 unsigned int shndx = lsym.get_st_shndx();
1103 bool is_ordinary;
1104 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1105 if (!is_ordinary)
1106 object->error(_("local symbol %u has bad shndx %u"),
1107 r_sym, shndx);
1108 else
1109 got->add_local_pair_with_rel(object, r_sym, shndx,
1110 GOT_TYPE_TLS_PAIR,
1111 target->rel_dyn_section(layout),
1112 elfcpp::R_386_TLS_DTPMOD32, 0);
1114 else if (optimized_type != tls::TLSOPT_TO_LE)
1115 unsupported_reloc_local(object, r_type);
1116 break;
1118 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1119 target->define_tls_base_symbol(symtab, layout);
1120 if (optimized_type == tls::TLSOPT_NONE)
1122 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1123 Output_data_got<32, false>* got
1124 = target->got_section(symtab, layout);
1125 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1126 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1128 unsigned int got_offset = got->add_constant(0);
1129 // The local symbol value is stored in the second
1130 // GOT entry.
1131 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1132 // That set the GOT offset of the local symbol to
1133 // point to the second entry, but we want it to
1134 // point to the first.
1135 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1136 got_offset);
1137 Reloc_section* rt = target->rel_tls_desc_section(layout);
1138 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1141 else if (optimized_type != tls::TLSOPT_TO_LE)
1142 unsupported_reloc_local(object, r_type);
1143 break;
1145 case elfcpp::R_386_TLS_DESC_CALL:
1146 break;
1148 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1149 if (optimized_type == tls::TLSOPT_NONE)
1151 // Create a GOT entry for the module index.
1152 target->got_mod_index_entry(symtab, layout, object);
1154 else if (optimized_type != tls::TLSOPT_TO_LE)
1155 unsupported_reloc_local(object, r_type);
1156 break;
1158 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1159 break;
1161 case elfcpp::R_386_TLS_IE: // Initial-exec
1162 case elfcpp::R_386_TLS_IE_32:
1163 case elfcpp::R_386_TLS_GOTIE:
1164 layout->set_has_static_tls();
1165 if (optimized_type == tls::TLSOPT_NONE)
1167 // For the R_386_TLS_IE relocation, we need to create a
1168 // dynamic relocation when building a shared library.
1169 if (r_type == elfcpp::R_386_TLS_IE
1170 && parameters->options().shared())
1172 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1173 unsigned int r_sym
1174 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1175 rel_dyn->add_local_relative(object, r_sym,
1176 elfcpp::R_386_RELATIVE,
1177 output_section, data_shndx,
1178 reloc.get_r_offset());
1180 // Create a GOT entry for the tp-relative offset.
1181 Output_data_got<32, false>* got
1182 = target->got_section(symtab, layout);
1183 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1184 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1185 ? elfcpp::R_386_TLS_TPOFF32
1186 : elfcpp::R_386_TLS_TPOFF);
1187 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1188 ? GOT_TYPE_TLS_OFFSET
1189 : GOT_TYPE_TLS_NOFFSET);
1190 got->add_local_with_rel(object, r_sym, got_type,
1191 target->rel_dyn_section(layout),
1192 dyn_r_type);
1194 else if (optimized_type != tls::TLSOPT_TO_LE)
1195 unsupported_reloc_local(object, r_type);
1196 break;
1198 case elfcpp::R_386_TLS_LE: // Local-exec
1199 case elfcpp::R_386_TLS_LE_32:
1200 layout->set_has_static_tls();
1201 if (output_is_shared)
1203 // We need to create a dynamic relocation.
1204 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1205 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1206 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1207 ? elfcpp::R_386_TLS_TPOFF32
1208 : elfcpp::R_386_TLS_TPOFF);
1209 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1210 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1211 data_shndx, reloc.get_r_offset());
1213 break;
1215 default:
1216 gold_unreachable();
1219 break;
1221 case elfcpp::R_386_32PLT:
1222 case elfcpp::R_386_TLS_GD_32:
1223 case elfcpp::R_386_TLS_GD_PUSH:
1224 case elfcpp::R_386_TLS_GD_CALL:
1225 case elfcpp::R_386_TLS_GD_POP:
1226 case elfcpp::R_386_TLS_LDM_32:
1227 case elfcpp::R_386_TLS_LDM_PUSH:
1228 case elfcpp::R_386_TLS_LDM_CALL:
1229 case elfcpp::R_386_TLS_LDM_POP:
1230 case elfcpp::R_386_USED_BY_INTEL_200:
1231 default:
1232 unsupported_reloc_local(object, r_type);
1233 break;
1237 // Report an unsupported relocation against a global symbol.
1239 void
1240 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1241 unsigned int r_type,
1242 Symbol* gsym)
1244 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1245 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1248 inline bool
1249 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1251 switch (r_type)
1253 case elfcpp::R_386_32:
1254 case elfcpp::R_386_16:
1255 case elfcpp::R_386_8:
1256 case elfcpp::R_386_GOTOFF:
1257 case elfcpp::R_386_GOT32:
1259 return true;
1261 default:
1262 return false;
1264 return false;
1267 inline bool
1268 Target_i386::Scan::local_reloc_may_be_function_pointer(
1269 Symbol_table* ,
1270 Layout* ,
1271 Target_i386* ,
1272 Sized_relobj<32, false>* ,
1273 unsigned int ,
1274 Output_section* ,
1275 const elfcpp::Rel<32, false>& ,
1276 unsigned int r_type,
1277 const elfcpp::Sym<32, false>&)
1279 return possible_function_pointer_reloc(r_type);
1282 inline bool
1283 Target_i386::Scan::global_reloc_may_be_function_pointer(
1284 Symbol_table* ,
1285 Layout* ,
1286 Target_i386* ,
1287 Sized_relobj<32, false>* ,
1288 unsigned int ,
1289 Output_section* ,
1290 const elfcpp::Rel<32, false>& ,
1291 unsigned int r_type,
1292 Symbol*)
1294 return possible_function_pointer_reloc(r_type);
1297 // Scan a relocation for a global symbol.
1299 inline void
1300 Target_i386::Scan::global(Symbol_table* symtab,
1301 Layout* layout,
1302 Target_i386* target,
1303 Sized_relobj<32, false>* object,
1304 unsigned int data_shndx,
1305 Output_section* output_section,
1306 const elfcpp::Rel<32, false>& reloc,
1307 unsigned int r_type,
1308 Symbol* gsym)
1310 switch (r_type)
1312 case elfcpp::R_386_NONE:
1313 case elfcpp::R_386_GNU_VTINHERIT:
1314 case elfcpp::R_386_GNU_VTENTRY:
1315 break;
1317 case elfcpp::R_386_32:
1318 case elfcpp::R_386_16:
1319 case elfcpp::R_386_8:
1321 // Make a PLT entry if necessary.
1322 if (gsym->needs_plt_entry())
1324 target->make_plt_entry(symtab, layout, gsym);
1325 // Since this is not a PC-relative relocation, we may be
1326 // taking the address of a function. In that case we need to
1327 // set the entry in the dynamic symbol table to the address of
1328 // the PLT entry.
1329 if (gsym->is_from_dynobj() && !parameters->options().shared())
1330 gsym->set_needs_dynsym_value();
1332 // Make a dynamic relocation if necessary.
1333 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1335 if (gsym->may_need_copy_reloc())
1337 target->copy_reloc(symtab, layout, object,
1338 data_shndx, output_section, gsym, reloc);
1340 else if (r_type == elfcpp::R_386_32
1341 && gsym->can_use_relative_reloc(false))
1343 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1344 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1345 output_section, object,
1346 data_shndx, reloc.get_r_offset());
1348 else
1350 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1351 rel_dyn->add_global(gsym, r_type, output_section, object,
1352 data_shndx, reloc.get_r_offset());
1356 break;
1358 case elfcpp::R_386_PC32:
1359 case elfcpp::R_386_PC16:
1360 case elfcpp::R_386_PC8:
1362 // Make a PLT entry if necessary.
1363 if (gsym->needs_plt_entry())
1365 // These relocations are used for function calls only in
1366 // non-PIC code. For a 32-bit relocation in a shared library,
1367 // we'll need a text relocation anyway, so we can skip the
1368 // PLT entry and let the dynamic linker bind the call directly
1369 // to the target. For smaller relocations, we should use a
1370 // PLT entry to ensure that the call can reach.
1371 if (!parameters->options().shared()
1372 || r_type != elfcpp::R_386_PC32)
1373 target->make_plt_entry(symtab, layout, gsym);
1375 // Make a dynamic relocation if necessary.
1376 int flags = Symbol::NON_PIC_REF;
1377 if (gsym->is_func())
1378 flags |= Symbol::FUNCTION_CALL;
1379 if (gsym->needs_dynamic_reloc(flags))
1381 if (gsym->may_need_copy_reloc())
1383 target->copy_reloc(symtab, layout, object,
1384 data_shndx, output_section, gsym, reloc);
1386 else
1388 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1389 rel_dyn->add_global(gsym, r_type, output_section, object,
1390 data_shndx, reloc.get_r_offset());
1394 break;
1396 case elfcpp::R_386_GOT32:
1398 // The symbol requires a GOT entry.
1399 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1400 if (gsym->final_value_is_known())
1401 got->add_global(gsym, GOT_TYPE_STANDARD);
1402 else
1404 // If this symbol is not fully resolved, we need to add a
1405 // GOT entry with a dynamic relocation.
1406 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1407 if (gsym->is_from_dynobj()
1408 || gsym->is_undefined()
1409 || gsym->is_preemptible())
1410 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1411 rel_dyn, elfcpp::R_386_GLOB_DAT);
1412 else
1414 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1415 rel_dyn->add_global_relative(
1416 gsym, elfcpp::R_386_RELATIVE, got,
1417 gsym->got_offset(GOT_TYPE_STANDARD));
1421 break;
1423 case elfcpp::R_386_PLT32:
1424 // If the symbol is fully resolved, this is just a PC32 reloc.
1425 // Otherwise we need a PLT entry.
1426 if (gsym->final_value_is_known())
1427 break;
1428 // If building a shared library, we can also skip the PLT entry
1429 // if the symbol is defined in the output file and is protected
1430 // or hidden.
1431 if (gsym->is_defined()
1432 && !gsym->is_from_dynobj()
1433 && !gsym->is_preemptible())
1434 break;
1435 target->make_plt_entry(symtab, layout, gsym);
1436 break;
1438 case elfcpp::R_386_GOTOFF:
1439 case elfcpp::R_386_GOTPC:
1440 // We need a GOT section.
1441 target->got_section(symtab, layout);
1442 break;
1444 // These are relocations which should only be seen by the
1445 // dynamic linker, and should never be seen here.
1446 case elfcpp::R_386_COPY:
1447 case elfcpp::R_386_GLOB_DAT:
1448 case elfcpp::R_386_JUMP_SLOT:
1449 case elfcpp::R_386_RELATIVE:
1450 case elfcpp::R_386_TLS_TPOFF:
1451 case elfcpp::R_386_TLS_DTPMOD32:
1452 case elfcpp::R_386_TLS_DTPOFF32:
1453 case elfcpp::R_386_TLS_TPOFF32:
1454 case elfcpp::R_386_TLS_DESC:
1455 gold_error(_("%s: unexpected reloc %u in object file"),
1456 object->name().c_str(), r_type);
1457 break;
1459 // These are initial tls relocs, which are expected when
1460 // linking.
1461 case elfcpp::R_386_TLS_GD: // Global-dynamic
1462 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1463 case elfcpp::R_386_TLS_DESC_CALL:
1464 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1465 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1466 case elfcpp::R_386_TLS_IE: // Initial-exec
1467 case elfcpp::R_386_TLS_IE_32:
1468 case elfcpp::R_386_TLS_GOTIE:
1469 case elfcpp::R_386_TLS_LE: // Local-exec
1470 case elfcpp::R_386_TLS_LE_32:
1472 const bool is_final = gsym->final_value_is_known();
1473 const tls::Tls_optimization optimized_type
1474 = Target_i386::optimize_tls_reloc(is_final, r_type);
1475 switch (r_type)
1477 case elfcpp::R_386_TLS_GD: // Global-dynamic
1478 if (optimized_type == tls::TLSOPT_NONE)
1480 // Create a pair of GOT entries for the module index and
1481 // dtv-relative offset.
1482 Output_data_got<32, false>* got
1483 = target->got_section(symtab, layout);
1484 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1485 target->rel_dyn_section(layout),
1486 elfcpp::R_386_TLS_DTPMOD32,
1487 elfcpp::R_386_TLS_DTPOFF32);
1489 else if (optimized_type == tls::TLSOPT_TO_IE)
1491 // Create a GOT entry for the tp-relative offset.
1492 Output_data_got<32, false>* got
1493 = target->got_section(symtab, layout);
1494 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1495 target->rel_dyn_section(layout),
1496 elfcpp::R_386_TLS_TPOFF);
1498 else if (optimized_type != tls::TLSOPT_TO_LE)
1499 unsupported_reloc_global(object, r_type, gsym);
1500 break;
1502 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1503 target->define_tls_base_symbol(symtab, layout);
1504 if (optimized_type == tls::TLSOPT_NONE)
1506 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1507 Output_data_got<32, false>* got
1508 = target->got_section(symtab, layout);
1509 Reloc_section* rt = target->rel_tls_desc_section(layout);
1510 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1511 elfcpp::R_386_TLS_DESC, 0);
1513 else if (optimized_type == tls::TLSOPT_TO_IE)
1515 // Create a GOT entry for the tp-relative offset.
1516 Output_data_got<32, false>* got
1517 = target->got_section(symtab, layout);
1518 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1519 target->rel_dyn_section(layout),
1520 elfcpp::R_386_TLS_TPOFF);
1522 else if (optimized_type != tls::TLSOPT_TO_LE)
1523 unsupported_reloc_global(object, r_type, gsym);
1524 break;
1526 case elfcpp::R_386_TLS_DESC_CALL:
1527 break;
1529 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1530 if (optimized_type == tls::TLSOPT_NONE)
1532 // Create a GOT entry for the module index.
1533 target->got_mod_index_entry(symtab, layout, object);
1535 else if (optimized_type != tls::TLSOPT_TO_LE)
1536 unsupported_reloc_global(object, r_type, gsym);
1537 break;
1539 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1540 break;
1542 case elfcpp::R_386_TLS_IE: // Initial-exec
1543 case elfcpp::R_386_TLS_IE_32:
1544 case elfcpp::R_386_TLS_GOTIE:
1545 layout->set_has_static_tls();
1546 if (optimized_type == tls::TLSOPT_NONE)
1548 // For the R_386_TLS_IE relocation, we need to create a
1549 // dynamic relocation when building a shared library.
1550 if (r_type == elfcpp::R_386_TLS_IE
1551 && parameters->options().shared())
1553 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1554 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1555 output_section, object,
1556 data_shndx,
1557 reloc.get_r_offset());
1559 // Create a GOT entry for the tp-relative offset.
1560 Output_data_got<32, false>* got
1561 = target->got_section(symtab, layout);
1562 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1563 ? elfcpp::R_386_TLS_TPOFF32
1564 : elfcpp::R_386_TLS_TPOFF);
1565 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1566 ? GOT_TYPE_TLS_OFFSET
1567 : GOT_TYPE_TLS_NOFFSET);
1568 got->add_global_with_rel(gsym, got_type,
1569 target->rel_dyn_section(layout),
1570 dyn_r_type);
1572 else if (optimized_type != tls::TLSOPT_TO_LE)
1573 unsupported_reloc_global(object, r_type, gsym);
1574 break;
1576 case elfcpp::R_386_TLS_LE: // Local-exec
1577 case elfcpp::R_386_TLS_LE_32:
1578 layout->set_has_static_tls();
1579 if (parameters->options().shared())
1581 // We need to create a dynamic relocation.
1582 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1583 ? elfcpp::R_386_TLS_TPOFF32
1584 : elfcpp::R_386_TLS_TPOFF);
1585 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1586 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1587 data_shndx, reloc.get_r_offset());
1589 break;
1591 default:
1592 gold_unreachable();
1595 break;
1597 case elfcpp::R_386_32PLT:
1598 case elfcpp::R_386_TLS_GD_32:
1599 case elfcpp::R_386_TLS_GD_PUSH:
1600 case elfcpp::R_386_TLS_GD_CALL:
1601 case elfcpp::R_386_TLS_GD_POP:
1602 case elfcpp::R_386_TLS_LDM_32:
1603 case elfcpp::R_386_TLS_LDM_PUSH:
1604 case elfcpp::R_386_TLS_LDM_CALL:
1605 case elfcpp::R_386_TLS_LDM_POP:
1606 case elfcpp::R_386_USED_BY_INTEL_200:
1607 default:
1608 unsupported_reloc_global(object, r_type, gsym);
1609 break;
1613 // Process relocations for gc.
1615 void
1616 Target_i386::gc_process_relocs(Symbol_table* symtab,
1617 Layout* layout,
1618 Sized_relobj<32, false>* object,
1619 unsigned int data_shndx,
1620 unsigned int,
1621 const unsigned char* prelocs,
1622 size_t reloc_count,
1623 Output_section* output_section,
1624 bool needs_special_offset_handling,
1625 size_t local_symbol_count,
1626 const unsigned char* plocal_symbols)
1628 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1629 Target_i386::Scan>(
1630 symtab,
1631 layout,
1632 this,
1633 object,
1634 data_shndx,
1635 prelocs,
1636 reloc_count,
1637 output_section,
1638 needs_special_offset_handling,
1639 local_symbol_count,
1640 plocal_symbols);
1643 // Scan relocations for a section.
1645 void
1646 Target_i386::scan_relocs(Symbol_table* symtab,
1647 Layout* layout,
1648 Sized_relobj<32, false>* object,
1649 unsigned int data_shndx,
1650 unsigned int sh_type,
1651 const unsigned char* prelocs,
1652 size_t reloc_count,
1653 Output_section* output_section,
1654 bool needs_special_offset_handling,
1655 size_t local_symbol_count,
1656 const unsigned char* plocal_symbols)
1658 if (sh_type == elfcpp::SHT_RELA)
1660 gold_error(_("%s: unsupported RELA reloc section"),
1661 object->name().c_str());
1662 return;
1665 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1666 Target_i386::Scan>(
1667 symtab,
1668 layout,
1669 this,
1670 object,
1671 data_shndx,
1672 prelocs,
1673 reloc_count,
1674 output_section,
1675 needs_special_offset_handling,
1676 local_symbol_count,
1677 plocal_symbols);
1680 // Finalize the sections.
1682 void
1683 Target_i386::do_finalize_sections(
1684 Layout* layout,
1685 const Input_objects*,
1686 Symbol_table* symtab)
1688 const Reloc_section* rel_plt = (this->plt_ == NULL
1689 ? NULL
1690 : this->plt_->rel_plt());
1691 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
1692 this->rel_dyn_, true, false);
1694 // Emit any relocs we saved in an attempt to avoid generating COPY
1695 // relocs.
1696 if (this->copy_relocs_.any_saved_relocs())
1697 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1699 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1700 // the .got.plt section.
1701 Symbol* sym = this->global_offset_table_;
1702 if (sym != NULL)
1704 uint32_t data_size = this->got_plt_->current_data_size();
1705 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
1709 // Return whether a direct absolute static relocation needs to be applied.
1710 // In cases where Scan::local() or Scan::global() has created
1711 // a dynamic relocation other than R_386_RELATIVE, the addend
1712 // of the relocation is carried in the data, and we must not
1713 // apply the static relocation.
1715 inline bool
1716 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1717 int ref_flags,
1718 bool is_32bit,
1719 Output_section* output_section)
1721 // If the output section is not allocated, then we didn't call
1722 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1723 // the reloc here.
1724 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1725 return true;
1727 // For local symbols, we will have created a non-RELATIVE dynamic
1728 // relocation only if (a) the output is position independent,
1729 // (b) the relocation is absolute (not pc- or segment-relative), and
1730 // (c) the relocation is not 32 bits wide.
1731 if (gsym == NULL)
1732 return !(parameters->options().output_is_position_independent()
1733 && (ref_flags & Symbol::ABSOLUTE_REF)
1734 && !is_32bit);
1736 // For global symbols, we use the same helper routines used in the
1737 // scan pass. If we did not create a dynamic relocation, or if we
1738 // created a RELATIVE dynamic relocation, we should apply the static
1739 // relocation.
1740 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1741 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1742 && gsym->can_use_relative_reloc(ref_flags
1743 & Symbol::FUNCTION_CALL);
1744 return !has_dyn || is_rel;
1747 // Perform a relocation.
1749 inline bool
1750 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1751 Target_i386* target,
1752 Output_section *output_section,
1753 size_t relnum,
1754 const elfcpp::Rel<32, false>& rel,
1755 unsigned int r_type,
1756 const Sized_symbol<32>* gsym,
1757 const Symbol_value<32>* psymval,
1758 unsigned char* view,
1759 elfcpp::Elf_types<32>::Elf_Addr address,
1760 section_size_type view_size)
1762 if (this->skip_call_tls_get_addr_)
1764 if ((r_type != elfcpp::R_386_PLT32
1765 && r_type != elfcpp::R_386_PC32)
1766 || gsym == NULL
1767 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1768 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1769 _("missing expected TLS relocation"));
1770 else
1772 this->skip_call_tls_get_addr_ = false;
1773 return false;
1777 // Pick the value to use for symbols defined in shared objects.
1778 Symbol_value<32> symval;
1779 if (gsym != NULL
1780 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1781 || r_type == elfcpp::R_386_PC16
1782 || r_type == elfcpp::R_386_PC32))
1784 symval.set_output_value(target->plt_section()->address()
1785 + gsym->plt_offset());
1786 psymval = &symval;
1789 const Sized_relobj<32, false>* object = relinfo->object;
1791 // Get the GOT offset if needed.
1792 // The GOT pointer points to the end of the GOT section.
1793 // We need to subtract the size of the GOT section to get
1794 // the actual offset to use in the relocation.
1795 bool have_got_offset = false;
1796 unsigned int got_offset = 0;
1797 switch (r_type)
1799 case elfcpp::R_386_GOT32:
1800 if (gsym != NULL)
1802 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1803 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1804 - target->got_size());
1806 else
1808 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1809 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1810 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1811 - target->got_size());
1813 have_got_offset = true;
1814 break;
1816 default:
1817 break;
1820 switch (r_type)
1822 case elfcpp::R_386_NONE:
1823 case elfcpp::R_386_GNU_VTINHERIT:
1824 case elfcpp::R_386_GNU_VTENTRY:
1825 break;
1827 case elfcpp::R_386_32:
1828 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1829 output_section))
1830 Relocate_functions<32, false>::rel32(view, object, psymval);
1831 break;
1833 case elfcpp::R_386_PC32:
1835 int ref_flags = Symbol::NON_PIC_REF;
1836 if (gsym != NULL && gsym->is_func())
1837 ref_flags |= Symbol::FUNCTION_CALL;
1838 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1839 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1841 break;
1843 case elfcpp::R_386_16:
1844 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1845 output_section))
1846 Relocate_functions<32, false>::rel16(view, object, psymval);
1847 break;
1849 case elfcpp::R_386_PC16:
1851 int ref_flags = Symbol::NON_PIC_REF;
1852 if (gsym != NULL && gsym->is_func())
1853 ref_flags |= Symbol::FUNCTION_CALL;
1854 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1855 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1857 break;
1859 case elfcpp::R_386_8:
1860 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1861 output_section))
1862 Relocate_functions<32, false>::rel8(view, object, psymval);
1863 break;
1865 case elfcpp::R_386_PC8:
1867 int ref_flags = Symbol::NON_PIC_REF;
1868 if (gsym != NULL && gsym->is_func())
1869 ref_flags |= Symbol::FUNCTION_CALL;
1870 if (should_apply_static_reloc(gsym, ref_flags, false,
1871 output_section))
1872 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1874 break;
1876 case elfcpp::R_386_PLT32:
1877 gold_assert(gsym == NULL
1878 || gsym->has_plt_offset()
1879 || gsym->final_value_is_known()
1880 || (gsym->is_defined()
1881 && !gsym->is_from_dynobj()
1882 && !gsym->is_preemptible()));
1883 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1884 break;
1886 case elfcpp::R_386_GOT32:
1887 gold_assert(have_got_offset);
1888 Relocate_functions<32, false>::rel32(view, got_offset);
1889 break;
1891 case elfcpp::R_386_GOTOFF:
1893 elfcpp::Elf_types<32>::Elf_Addr value;
1894 value = (psymval->value(object, 0)
1895 - target->got_plt_section()->address());
1896 Relocate_functions<32, false>::rel32(view, value);
1898 break;
1900 case elfcpp::R_386_GOTPC:
1902 elfcpp::Elf_types<32>::Elf_Addr value;
1903 value = target->got_plt_section()->address();
1904 Relocate_functions<32, false>::pcrel32(view, value, address);
1906 break;
1908 case elfcpp::R_386_COPY:
1909 case elfcpp::R_386_GLOB_DAT:
1910 case elfcpp::R_386_JUMP_SLOT:
1911 case elfcpp::R_386_RELATIVE:
1912 // These are outstanding tls relocs, which are unexpected when
1913 // linking.
1914 case elfcpp::R_386_TLS_TPOFF:
1915 case elfcpp::R_386_TLS_DTPMOD32:
1916 case elfcpp::R_386_TLS_DTPOFF32:
1917 case elfcpp::R_386_TLS_TPOFF32:
1918 case elfcpp::R_386_TLS_DESC:
1919 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1920 _("unexpected reloc %u in object file"),
1921 r_type);
1922 break;
1924 // These are initial tls relocs, which are expected when
1925 // linking.
1926 case elfcpp::R_386_TLS_GD: // Global-dynamic
1927 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1928 case elfcpp::R_386_TLS_DESC_CALL:
1929 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1930 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1931 case elfcpp::R_386_TLS_IE: // Initial-exec
1932 case elfcpp::R_386_TLS_IE_32:
1933 case elfcpp::R_386_TLS_GOTIE:
1934 case elfcpp::R_386_TLS_LE: // Local-exec
1935 case elfcpp::R_386_TLS_LE_32:
1936 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1937 view, address, view_size);
1938 break;
1940 case elfcpp::R_386_32PLT:
1941 case elfcpp::R_386_TLS_GD_32:
1942 case elfcpp::R_386_TLS_GD_PUSH:
1943 case elfcpp::R_386_TLS_GD_CALL:
1944 case elfcpp::R_386_TLS_GD_POP:
1945 case elfcpp::R_386_TLS_LDM_32:
1946 case elfcpp::R_386_TLS_LDM_PUSH:
1947 case elfcpp::R_386_TLS_LDM_CALL:
1948 case elfcpp::R_386_TLS_LDM_POP:
1949 case elfcpp::R_386_USED_BY_INTEL_200:
1950 default:
1951 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1952 _("unsupported reloc %u"),
1953 r_type);
1954 break;
1957 return true;
1960 // Perform a TLS relocation.
1962 inline void
1963 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1964 Target_i386* target,
1965 size_t relnum,
1966 const elfcpp::Rel<32, false>& rel,
1967 unsigned int r_type,
1968 const Sized_symbol<32>* gsym,
1969 const Symbol_value<32>* psymval,
1970 unsigned char* view,
1971 elfcpp::Elf_types<32>::Elf_Addr,
1972 section_size_type view_size)
1974 Output_segment* tls_segment = relinfo->layout->tls_segment();
1976 const Sized_relobj<32, false>* object = relinfo->object;
1978 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1980 const bool is_final = (gsym == NULL
1981 ? !parameters->options().shared()
1982 : gsym->final_value_is_known());
1983 const tls::Tls_optimization optimized_type
1984 = Target_i386::optimize_tls_reloc(is_final, r_type);
1985 switch (r_type)
1987 case elfcpp::R_386_TLS_GD: // Global-dynamic
1988 if (optimized_type == tls::TLSOPT_TO_LE)
1990 gold_assert(tls_segment != NULL);
1991 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1992 rel, r_type, value, view,
1993 view_size);
1994 break;
1996 else
1998 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1999 ? GOT_TYPE_TLS_NOFFSET
2000 : GOT_TYPE_TLS_PAIR);
2001 unsigned int got_offset;
2002 if (gsym != NULL)
2004 gold_assert(gsym->has_got_offset(got_type));
2005 got_offset = gsym->got_offset(got_type) - target->got_size();
2007 else
2009 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2010 gold_assert(object->local_has_got_offset(r_sym, got_type));
2011 got_offset = (object->local_got_offset(r_sym, got_type)
2012 - target->got_size());
2014 if (optimized_type == tls::TLSOPT_TO_IE)
2016 gold_assert(tls_segment != NULL);
2017 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2018 got_offset, view, view_size);
2019 break;
2021 else if (optimized_type == tls::TLSOPT_NONE)
2023 // Relocate the field with the offset of the pair of GOT
2024 // entries.
2025 Relocate_functions<32, false>::rel32(view, got_offset);
2026 break;
2029 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2030 _("unsupported reloc %u"),
2031 r_type);
2032 break;
2034 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2035 case elfcpp::R_386_TLS_DESC_CALL:
2036 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2037 if (optimized_type == tls::TLSOPT_TO_LE)
2039 gold_assert(tls_segment != NULL);
2040 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2041 rel, r_type, value, view,
2042 view_size);
2043 break;
2045 else
2047 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2048 ? GOT_TYPE_TLS_NOFFSET
2049 : GOT_TYPE_TLS_DESC);
2050 unsigned int got_offset;
2051 if (gsym != NULL)
2053 gold_assert(gsym->has_got_offset(got_type));
2054 got_offset = gsym->got_offset(got_type) - target->got_size();
2056 else
2058 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2059 gold_assert(object->local_has_got_offset(r_sym, got_type));
2060 got_offset = (object->local_got_offset(r_sym, got_type)
2061 - target->got_size());
2063 if (optimized_type == tls::TLSOPT_TO_IE)
2065 gold_assert(tls_segment != NULL);
2066 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2067 got_offset, view, view_size);
2068 break;
2070 else if (optimized_type == tls::TLSOPT_NONE)
2072 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2074 // Relocate the field with the offset of the pair of GOT
2075 // entries.
2076 Relocate_functions<32, false>::rel32(view, got_offset);
2078 break;
2081 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2082 _("unsupported reloc %u"),
2083 r_type);
2084 break;
2086 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2087 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2089 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2090 _("both SUN and GNU model "
2091 "TLS relocations"));
2092 break;
2094 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2095 if (optimized_type == tls::TLSOPT_TO_LE)
2097 gold_assert(tls_segment != NULL);
2098 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2099 value, view, view_size);
2100 break;
2102 else if (optimized_type == tls::TLSOPT_NONE)
2104 // Relocate the field with the offset of the GOT entry for
2105 // the module index.
2106 unsigned int got_offset;
2107 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2108 - target->got_size());
2109 Relocate_functions<32, false>::rel32(view, got_offset);
2110 break;
2112 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2113 _("unsupported reloc %u"),
2114 r_type);
2115 break;
2117 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2118 if (optimized_type == tls::TLSOPT_TO_LE)
2120 // This reloc can appear in debugging sections, in which
2121 // case we must not convert to local-exec. We decide what
2122 // to do based on whether the section is marked as
2123 // containing executable code. That is what the GNU linker
2124 // does as well.
2125 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2126 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2128 gold_assert(tls_segment != NULL);
2129 value -= tls_segment->memsz();
2132 Relocate_functions<32, false>::rel32(view, value);
2133 break;
2135 case elfcpp::R_386_TLS_IE: // Initial-exec
2136 case elfcpp::R_386_TLS_GOTIE:
2137 case elfcpp::R_386_TLS_IE_32:
2138 if (optimized_type == tls::TLSOPT_TO_LE)
2140 gold_assert(tls_segment != NULL);
2141 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2142 rel, r_type, value, view,
2143 view_size);
2144 break;
2146 else if (optimized_type == tls::TLSOPT_NONE)
2148 // Relocate the field with the offset of the GOT entry for
2149 // the tp-relative offset of the symbol.
2150 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2151 ? GOT_TYPE_TLS_OFFSET
2152 : GOT_TYPE_TLS_NOFFSET);
2153 unsigned int got_offset;
2154 if (gsym != NULL)
2156 gold_assert(gsym->has_got_offset(got_type));
2157 got_offset = gsym->got_offset(got_type);
2159 else
2161 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2162 gold_assert(object->local_has_got_offset(r_sym, got_type));
2163 got_offset = object->local_got_offset(r_sym, got_type);
2165 // For the R_386_TLS_IE relocation, we need to apply the
2166 // absolute address of the GOT entry.
2167 if (r_type == elfcpp::R_386_TLS_IE)
2168 got_offset += target->got_plt_section()->address();
2169 // All GOT offsets are relative to the end of the GOT.
2170 got_offset -= target->got_size();
2171 Relocate_functions<32, false>::rel32(view, got_offset);
2172 break;
2174 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2175 _("unsupported reloc %u"),
2176 r_type);
2177 break;
2179 case elfcpp::R_386_TLS_LE: // Local-exec
2180 // If we're creating a shared library, a dynamic relocation will
2181 // have been created for this location, so do not apply it now.
2182 if (!parameters->options().shared())
2184 gold_assert(tls_segment != NULL);
2185 value -= tls_segment->memsz();
2186 Relocate_functions<32, false>::rel32(view, value);
2188 break;
2190 case elfcpp::R_386_TLS_LE_32:
2191 // If we're creating a shared library, a dynamic relocation will
2192 // have been created for this location, so do not apply it now.
2193 if (!parameters->options().shared())
2195 gold_assert(tls_segment != NULL);
2196 value = tls_segment->memsz() - value;
2197 Relocate_functions<32, false>::rel32(view, value);
2199 break;
2203 // Do a relocation in which we convert a TLS General-Dynamic to a
2204 // Local-Exec.
2206 inline void
2207 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2208 size_t relnum,
2209 Output_segment* tls_segment,
2210 const elfcpp::Rel<32, false>& rel,
2211 unsigned int,
2212 elfcpp::Elf_types<32>::Elf_Addr value,
2213 unsigned char* view,
2214 section_size_type view_size)
2216 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2217 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2218 // leal foo(%reg),%eax; call ___tls_get_addr
2219 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2221 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2222 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2224 unsigned char op1 = view[-1];
2225 unsigned char op2 = view[-2];
2227 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2228 op2 == 0x8d || op2 == 0x04);
2229 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2231 int roff = 5;
2233 if (op2 == 0x04)
2235 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2236 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2237 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2238 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2239 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2241 else
2243 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2244 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2245 if (rel.get_r_offset() + 9 < view_size
2246 && view[9] == 0x90)
2248 // There is a trailing nop. Use the size byte subl.
2249 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2250 roff = 6;
2252 else
2254 // Use the five byte subl.
2255 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2259 value = tls_segment->memsz() - value;
2260 Relocate_functions<32, false>::rel32(view + roff, value);
2262 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2263 // We can skip it.
2264 this->skip_call_tls_get_addr_ = true;
2267 // Do a relocation in which we convert a TLS General-Dynamic to an
2268 // Initial-Exec.
2270 inline void
2271 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2272 size_t relnum,
2273 Output_segment*,
2274 const elfcpp::Rel<32, false>& rel,
2275 unsigned int,
2276 elfcpp::Elf_types<32>::Elf_Addr value,
2277 unsigned char* view,
2278 section_size_type view_size)
2280 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2281 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2283 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2284 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2286 unsigned char op1 = view[-1];
2287 unsigned char op2 = view[-2];
2289 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2290 op2 == 0x8d || op2 == 0x04);
2291 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2293 int roff = 5;
2295 // FIXME: For now, support only the first (SIB) form.
2296 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2298 if (op2 == 0x04)
2300 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2301 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2302 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2303 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2304 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2306 else
2308 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2309 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2310 if (rel.get_r_offset() + 9 < view_size
2311 && view[9] == 0x90)
2313 // FIXME: This is not the right instruction sequence.
2314 // There is a trailing nop. Use the size byte subl.
2315 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2316 roff = 6;
2318 else
2320 // FIXME: This is not the right instruction sequence.
2321 // Use the five byte subl.
2322 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2326 Relocate_functions<32, false>::rel32(view + roff, value);
2328 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2329 // We can skip it.
2330 this->skip_call_tls_get_addr_ = true;
2333 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2334 // General-Dynamic to a Local-Exec.
2336 inline void
2337 Target_i386::Relocate::tls_desc_gd_to_le(
2338 const Relocate_info<32, false>* relinfo,
2339 size_t relnum,
2340 Output_segment* tls_segment,
2341 const elfcpp::Rel<32, false>& rel,
2342 unsigned int r_type,
2343 elfcpp::Elf_types<32>::Elf_Addr value,
2344 unsigned char* view,
2345 section_size_type view_size)
2347 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2349 // leal foo@TLSDESC(%ebx), %eax
2350 // ==> leal foo@NTPOFF, %eax
2351 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2352 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2353 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2354 view[-2] == 0x8d && view[-1] == 0x83);
2355 view[-1] = 0x05;
2356 value -= tls_segment->memsz();
2357 Relocate_functions<32, false>::rel32(view, value);
2359 else
2361 // call *foo@TLSCALL(%eax)
2362 // ==> nop; nop
2363 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2364 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2365 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2366 view[0] == 0xff && view[1] == 0x10);
2367 view[0] = 0x66;
2368 view[1] = 0x90;
2372 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2373 // General-Dynamic to an Initial-Exec.
2375 inline void
2376 Target_i386::Relocate::tls_desc_gd_to_ie(
2377 const Relocate_info<32, false>* relinfo,
2378 size_t relnum,
2379 Output_segment*,
2380 const elfcpp::Rel<32, false>& rel,
2381 unsigned int r_type,
2382 elfcpp::Elf_types<32>::Elf_Addr value,
2383 unsigned char* view,
2384 section_size_type view_size)
2386 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2388 // leal foo@TLSDESC(%ebx), %eax
2389 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2390 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2391 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2392 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2393 view[-2] == 0x8d && view[-1] == 0x83);
2394 view[-2] = 0x8b;
2395 Relocate_functions<32, false>::rel32(view, value);
2397 else
2399 // call *foo@TLSCALL(%eax)
2400 // ==> nop; nop
2401 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2402 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2403 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2404 view[0] == 0xff && view[1] == 0x10);
2405 view[0] = 0x66;
2406 view[1] = 0x90;
2410 // Do a relocation in which we convert a TLS Local-Dynamic to a
2411 // Local-Exec.
2413 inline void
2414 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2415 size_t relnum,
2416 Output_segment*,
2417 const elfcpp::Rel<32, false>& rel,
2418 unsigned int,
2419 elfcpp::Elf_types<32>::Elf_Addr,
2420 unsigned char* view,
2421 section_size_type view_size)
2423 // leal foo(%reg), %eax; call ___tls_get_addr
2424 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2426 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2427 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2429 // FIXME: Does this test really always pass?
2430 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2431 view[-2] == 0x8d && view[-1] == 0x83);
2433 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2435 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2437 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2438 // We can skip it.
2439 this->skip_call_tls_get_addr_ = true;
2442 // Do a relocation in which we convert a TLS Initial-Exec to a
2443 // Local-Exec.
2445 inline void
2446 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2447 size_t relnum,
2448 Output_segment* tls_segment,
2449 const elfcpp::Rel<32, false>& rel,
2450 unsigned int r_type,
2451 elfcpp::Elf_types<32>::Elf_Addr value,
2452 unsigned char* view,
2453 section_size_type view_size)
2455 // We have to actually change the instructions, which means that we
2456 // need to examine the opcodes to figure out which instruction we
2457 // are looking at.
2458 if (r_type == elfcpp::R_386_TLS_IE)
2460 // movl %gs:XX,%eax ==> movl $YY,%eax
2461 // movl %gs:XX,%reg ==> movl $YY,%reg
2462 // addl %gs:XX,%reg ==> addl $YY,%reg
2463 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2464 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2466 unsigned char op1 = view[-1];
2467 if (op1 == 0xa1)
2469 // movl XX,%eax ==> movl $YY,%eax
2470 view[-1] = 0xb8;
2472 else
2474 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2476 unsigned char op2 = view[-2];
2477 if (op2 == 0x8b)
2479 // movl XX,%reg ==> movl $YY,%reg
2480 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2481 (op1 & 0xc7) == 0x05);
2482 view[-2] = 0xc7;
2483 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2485 else if (op2 == 0x03)
2487 // addl XX,%reg ==> addl $YY,%reg
2488 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2489 (op1 & 0xc7) == 0x05);
2490 view[-2] = 0x81;
2491 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2493 else
2494 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2497 else
2499 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2500 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2501 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2502 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2503 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2505 unsigned char op1 = view[-1];
2506 unsigned char op2 = view[-2];
2507 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2508 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2509 if (op2 == 0x8b)
2511 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2512 view[-2] = 0xc7;
2513 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2515 else if (op2 == 0x2b)
2517 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2518 view[-2] = 0x81;
2519 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2521 else if (op2 == 0x03)
2523 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2524 view[-2] = 0x81;
2525 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2527 else
2528 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2531 value = tls_segment->memsz() - value;
2532 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2533 value = - value;
2535 Relocate_functions<32, false>::rel32(view, value);
2538 // Relocate section data.
2540 void
2541 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2542 unsigned int sh_type,
2543 const unsigned char* prelocs,
2544 size_t reloc_count,
2545 Output_section* output_section,
2546 bool needs_special_offset_handling,
2547 unsigned char* view,
2548 elfcpp::Elf_types<32>::Elf_Addr address,
2549 section_size_type view_size,
2550 const Reloc_symbol_changes* reloc_symbol_changes)
2552 gold_assert(sh_type == elfcpp::SHT_REL);
2554 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2555 Target_i386::Relocate>(
2556 relinfo,
2557 this,
2558 prelocs,
2559 reloc_count,
2560 output_section,
2561 needs_special_offset_handling,
2562 view,
2563 address,
2564 view_size,
2565 reloc_symbol_changes);
2568 // Return the size of a relocation while scanning during a relocatable
2569 // link.
2571 unsigned int
2572 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2573 unsigned int r_type,
2574 Relobj* object)
2576 switch (r_type)
2578 case elfcpp::R_386_NONE:
2579 case elfcpp::R_386_GNU_VTINHERIT:
2580 case elfcpp::R_386_GNU_VTENTRY:
2581 case elfcpp::R_386_TLS_GD: // Global-dynamic
2582 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2583 case elfcpp::R_386_TLS_DESC_CALL:
2584 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2585 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2586 case elfcpp::R_386_TLS_IE: // Initial-exec
2587 case elfcpp::R_386_TLS_IE_32:
2588 case elfcpp::R_386_TLS_GOTIE:
2589 case elfcpp::R_386_TLS_LE: // Local-exec
2590 case elfcpp::R_386_TLS_LE_32:
2591 return 0;
2593 case elfcpp::R_386_32:
2594 case elfcpp::R_386_PC32:
2595 case elfcpp::R_386_GOT32:
2596 case elfcpp::R_386_PLT32:
2597 case elfcpp::R_386_GOTOFF:
2598 case elfcpp::R_386_GOTPC:
2599 return 4;
2601 case elfcpp::R_386_16:
2602 case elfcpp::R_386_PC16:
2603 return 2;
2605 case elfcpp::R_386_8:
2606 case elfcpp::R_386_PC8:
2607 return 1;
2609 // These are relocations which should only be seen by the
2610 // dynamic linker, and should never be seen here.
2611 case elfcpp::R_386_COPY:
2612 case elfcpp::R_386_GLOB_DAT:
2613 case elfcpp::R_386_JUMP_SLOT:
2614 case elfcpp::R_386_RELATIVE:
2615 case elfcpp::R_386_TLS_TPOFF:
2616 case elfcpp::R_386_TLS_DTPMOD32:
2617 case elfcpp::R_386_TLS_DTPOFF32:
2618 case elfcpp::R_386_TLS_TPOFF32:
2619 case elfcpp::R_386_TLS_DESC:
2620 object->error(_("unexpected reloc %u in object file"), r_type);
2621 return 0;
2623 case elfcpp::R_386_32PLT:
2624 case elfcpp::R_386_TLS_GD_32:
2625 case elfcpp::R_386_TLS_GD_PUSH:
2626 case elfcpp::R_386_TLS_GD_CALL:
2627 case elfcpp::R_386_TLS_GD_POP:
2628 case elfcpp::R_386_TLS_LDM_32:
2629 case elfcpp::R_386_TLS_LDM_PUSH:
2630 case elfcpp::R_386_TLS_LDM_CALL:
2631 case elfcpp::R_386_TLS_LDM_POP:
2632 case elfcpp::R_386_USED_BY_INTEL_200:
2633 default:
2634 object->error(_("unsupported reloc %u in object file"), r_type);
2635 return 0;
2639 // Scan the relocs during a relocatable link.
2641 void
2642 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
2643 Layout* layout,
2644 Sized_relobj<32, false>* object,
2645 unsigned int data_shndx,
2646 unsigned int sh_type,
2647 const unsigned char* prelocs,
2648 size_t reloc_count,
2649 Output_section* output_section,
2650 bool needs_special_offset_handling,
2651 size_t local_symbol_count,
2652 const unsigned char* plocal_symbols,
2653 Relocatable_relocs* rr)
2655 gold_assert(sh_type == elfcpp::SHT_REL);
2657 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2658 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2660 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2661 Scan_relocatable_relocs>(
2662 symtab,
2663 layout,
2664 object,
2665 data_shndx,
2666 prelocs,
2667 reloc_count,
2668 output_section,
2669 needs_special_offset_handling,
2670 local_symbol_count,
2671 plocal_symbols,
2672 rr);
2675 // Relocate a section during a relocatable link.
2677 void
2678 Target_i386::relocate_for_relocatable(
2679 const Relocate_info<32, false>* relinfo,
2680 unsigned int sh_type,
2681 const unsigned char* prelocs,
2682 size_t reloc_count,
2683 Output_section* output_section,
2684 off_t offset_in_output_section,
2685 const Relocatable_relocs* rr,
2686 unsigned char* view,
2687 elfcpp::Elf_types<32>::Elf_Addr view_address,
2688 section_size_type view_size,
2689 unsigned char* reloc_view,
2690 section_size_type reloc_view_size)
2692 gold_assert(sh_type == elfcpp::SHT_REL);
2694 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2695 relinfo,
2696 prelocs,
2697 reloc_count,
2698 output_section,
2699 offset_in_output_section,
2701 view,
2702 view_address,
2703 view_size,
2704 reloc_view,
2705 reloc_view_size);
2708 // Return the value to use for a dynamic which requires special
2709 // treatment. This is how we support equality comparisons of function
2710 // pointers across shared library boundaries, as described in the
2711 // processor specific ABI supplement.
2713 uint64_t
2714 Target_i386::do_dynsym_value(const Symbol* gsym) const
2716 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2717 return this->plt_section()->address() + gsym->plt_offset();
2720 // Return a string used to fill a code section with nops to take up
2721 // the specified length.
2723 std::string
2724 Target_i386::do_code_fill(section_size_type length) const
2726 if (length >= 16)
2728 // Build a jmp instruction to skip over the bytes.
2729 unsigned char jmp[5];
2730 jmp[0] = 0xe9;
2731 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2732 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2733 + std::string(length - 5, '\0'));
2736 // Nop sequences of various lengths.
2737 const char nop1[1] = { 0x90 }; // nop
2738 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2739 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2740 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2741 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2742 0x00 }; // leal 0(%esi,1),%esi
2743 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2744 0x00, 0x00 };
2745 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2746 0x00, 0x00, 0x00 };
2747 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2748 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2749 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2750 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2751 0x00 };
2752 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2753 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2754 0x00, 0x00 };
2755 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2756 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2757 0x00, 0x00, 0x00 };
2758 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2759 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2760 0x00, 0x00, 0x00, 0x00 };
2761 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2762 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2763 0x27, 0x00, 0x00, 0x00,
2764 0x00 };
2765 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2766 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2767 0xbc, 0x27, 0x00, 0x00,
2768 0x00, 0x00 };
2769 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2770 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2771 0x90, 0x90, 0x90, 0x90,
2772 0x90, 0x90, 0x90 };
2774 const char* nops[16] = {
2775 NULL,
2776 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2777 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2780 return std::string(nops[length], length);
2783 // Return whether SYM should be treated as a call to a non-split
2784 // function. We don't want that to be true of a call to a
2785 // get_pc_thunk function.
2787 bool
2788 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
2790 return (sym->type() == elfcpp::STT_FUNC
2791 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
2794 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2795 // compiled with -fstack-split. The function calls non-stack-split
2796 // code. We have to change the function so that it always ensures
2797 // that it has enough stack space to run some random function.
2799 void
2800 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2801 section_offset_type fnoffset,
2802 section_size_type fnsize,
2803 unsigned char* view,
2804 section_size_type view_size,
2805 std::string* from,
2806 std::string* to) const
2808 // The function starts with a comparison of the stack pointer and a
2809 // field in the TCB. This is followed by a jump.
2811 // cmp %gs:NN,%esp
2812 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2813 && fnsize > 7)
2815 // We will call __morestack if the carry flag is set after this
2816 // comparison. We turn the comparison into an stc instruction
2817 // and some nops.
2818 view[fnoffset] = '\xf9';
2819 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2821 // lea NN(%esp),%ecx
2822 // lea NN(%esp),%edx
2823 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2824 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
2825 && fnsize > 7)
2827 // This is loading an offset from the stack pointer for a
2828 // comparison. The offset is negative, so we decrease the
2829 // offset by the amount of space we need for the stack. This
2830 // means we will avoid calling __morestack if there happens to
2831 // be plenty of space on the stack already.
2832 unsigned char* pval = view + fnoffset + 3;
2833 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2834 val -= parameters->options().split_stack_adjust_size();
2835 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2837 else
2839 if (!object->has_no_split_stack())
2840 object->error(_("failed to match split-stack sequence at "
2841 "section %u offset %0zx"),
2842 shndx, static_cast<size_t>(fnoffset));
2843 return;
2846 // We have to change the function so that it calls
2847 // __morestack_non_split instead of __morestack. The former will
2848 // allocate additional stack space.
2849 *from = "__morestack";
2850 *to = "__morestack_non_split";
2853 // The selector for i386 object files.
2855 class Target_selector_i386 : public Target_selector_freebsd
2857 public:
2858 Target_selector_i386()
2859 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2860 "elf32-i386", "elf32-i386-freebsd")
2863 Target*
2864 do_instantiate_target()
2865 { return new Target_i386(); }
2868 Target_selector_i386 target_selector_i386;
2870 } // End anonymous namespace.