Change to "Copyright 2010"
[binutils.git] / gold / i386.cc
blob2eab3f86c08c655ff026dccffac334d421d579c6
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 // Process the relocations to determine unreferenced sections for
68 // garbage collection.
69 void
70 gc_process_relocs(Symbol_table* symtab,
71 Layout* layout,
72 Sized_relobj<32, false>* object,
73 unsigned int data_shndx,
74 unsigned int sh_type,
75 const unsigned char* prelocs,
76 size_t reloc_count,
77 Output_section* output_section,
78 bool needs_special_offset_handling,
79 size_t local_symbol_count,
80 const unsigned char* plocal_symbols);
82 // Scan the relocations to look for symbol adjustments.
83 void
84 scan_relocs(Symbol_table* symtab,
85 Layout* layout,
86 Sized_relobj<32, false>* object,
87 unsigned int data_shndx,
88 unsigned int sh_type,
89 const unsigned char* prelocs,
90 size_t reloc_count,
91 Output_section* output_section,
92 bool needs_special_offset_handling,
93 size_t local_symbol_count,
94 const unsigned char* plocal_symbols);
96 // Finalize the sections.
97 void
98 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
100 // Return the value to use for a dynamic which requires special
101 // treatment.
102 uint64_t
103 do_dynsym_value(const Symbol*) const;
105 // Relocate a section.
106 void
107 relocate_section(const Relocate_info<32, false>*,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
110 size_t reloc_count,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
113 unsigned char* view,
114 elfcpp::Elf_types<32>::Elf_Addr view_address,
115 section_size_type view_size,
116 const Reloc_symbol_changes*);
118 // Scan the relocs during a relocatable link.
119 void
120 scan_relocatable_relocs(Symbol_table* symtab,
121 Layout* layout,
122 Sized_relobj<32, false>* object,
123 unsigned int data_shndx,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
126 size_t reloc_count,
127 Output_section* output_section,
128 bool needs_special_offset_handling,
129 size_t local_symbol_count,
130 const unsigned char* plocal_symbols,
131 Relocatable_relocs*);
133 // Relocate a section during a relocatable link.
134 void
135 relocate_for_relocatable(const Relocate_info<32, false>*,
136 unsigned int sh_type,
137 const unsigned char* prelocs,
138 size_t reloc_count,
139 Output_section* output_section,
140 off_t offset_in_output_section,
141 const Relocatable_relocs*,
142 unsigned char* view,
143 elfcpp::Elf_types<32>::Elf_Addr view_address,
144 section_size_type view_size,
145 unsigned char* reloc_view,
146 section_size_type reloc_view_size);
148 // Return a string used to fill a code section with nops.
149 std::string
150 do_code_fill(section_size_type length) const;
152 // Return whether SYM is defined by the ABI.
153 bool
154 do_is_defined_by_abi(const Symbol* sym) const
155 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
157 // Return whether a symbol name implies a local label. The UnixWare
158 // 2.1 cc generates temporary symbols that start with .X, so we
159 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
160 // If so, we should move the .X recognition into
161 // Target::do_is_local_label_name.
162 bool
163 do_is_local_label_name(const char* name) const
165 if (name[0] == '.' && name[1] == 'X')
166 return true;
167 return Target::do_is_local_label_name(name);
170 // Adjust -fstack-split code which calls non-stack-split code.
171 void
172 do_calls_non_split(Relobj* object, unsigned int shndx,
173 section_offset_type fnoffset, section_size_type fnsize,
174 unsigned char* view, section_size_type view_size,
175 std::string* from, std::string* to) const;
177 // Return the size of the GOT section.
178 section_size_type
179 got_size()
181 gold_assert(this->got_ != NULL);
182 return this->got_->data_size();
185 private:
186 // The class which scans relocations.
187 struct Scan
189 inline void
190 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
191 Sized_relobj<32, false>* object,
192 unsigned int data_shndx,
193 Output_section* output_section,
194 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
195 const elfcpp::Sym<32, false>& lsym);
197 inline void
198 global(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 Symbol* gsym);
205 static void
206 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
208 static void
209 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
210 Symbol*);
213 // The class which implements relocation.
214 class Relocate
216 public:
217 Relocate()
218 : skip_call_tls_get_addr_(false),
219 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
222 ~Relocate()
224 if (this->skip_call_tls_get_addr_)
226 // FIXME: This needs to specify the location somehow.
227 gold_error(_("missing expected TLS relocation"));
231 // Return whether the static relocation needs to be applied.
232 inline bool
233 should_apply_static_reloc(const Sized_symbol<32>* gsym,
234 int ref_flags,
235 bool is_32bit,
236 Output_section* output_section);
238 // Do a relocation. Return false if the caller should not issue
239 // any warnings about this relocation.
240 inline bool
241 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
242 size_t relnum, const elfcpp::Rel<32, false>&,
243 unsigned int r_type, const Sized_symbol<32>*,
244 const Symbol_value<32>*,
245 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
246 section_size_type);
248 private:
249 // Do a TLS relocation.
250 inline void
251 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
252 size_t relnum, const elfcpp::Rel<32, false>&,
253 unsigned int r_type, const Sized_symbol<32>*,
254 const Symbol_value<32>*,
255 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
256 section_size_type);
258 // Do a TLS General-Dynamic to Initial-Exec transition.
259 inline void
260 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
261 Output_segment* tls_segment,
262 const elfcpp::Rel<32, false>&, unsigned int r_type,
263 elfcpp::Elf_types<32>::Elf_Addr value,
264 unsigned char* view,
265 section_size_type view_size);
267 // Do a TLS General-Dynamic to Local-Exec transition.
268 inline void
269 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
270 Output_segment* tls_segment,
271 const elfcpp::Rel<32, false>&, unsigned int r_type,
272 elfcpp::Elf_types<32>::Elf_Addr value,
273 unsigned char* view,
274 section_size_type view_size);
276 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
277 // transition.
278 inline void
279 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
280 Output_segment* tls_segment,
281 const elfcpp::Rel<32, false>&, unsigned int r_type,
282 elfcpp::Elf_types<32>::Elf_Addr value,
283 unsigned char* view,
284 section_size_type view_size);
286 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
287 // transition.
288 inline void
289 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
290 Output_segment* tls_segment,
291 const elfcpp::Rel<32, false>&, unsigned int r_type,
292 elfcpp::Elf_types<32>::Elf_Addr value,
293 unsigned char* view,
294 section_size_type view_size);
296 // Do a TLS Local-Dynamic to Local-Exec transition.
297 inline void
298 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
299 Output_segment* tls_segment,
300 const elfcpp::Rel<32, false>&, unsigned int r_type,
301 elfcpp::Elf_types<32>::Elf_Addr value,
302 unsigned char* view,
303 section_size_type view_size);
305 // Do a TLS Initial-Exec to Local-Exec transition.
306 static inline void
307 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
308 Output_segment* tls_segment,
309 const elfcpp::Rel<32, false>&, unsigned int r_type,
310 elfcpp::Elf_types<32>::Elf_Addr value,
311 unsigned char* view,
312 section_size_type view_size);
314 // We need to keep track of which type of local dynamic relocation
315 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
316 enum Local_dynamic_type
318 LOCAL_DYNAMIC_NONE,
319 LOCAL_DYNAMIC_SUN,
320 LOCAL_DYNAMIC_GNU
323 // This is set if we should skip the next reloc, which should be a
324 // PLT32 reloc against ___tls_get_addr.
325 bool skip_call_tls_get_addr_;
326 // The type of local dynamic relocation we have seen in the section
327 // being relocated, if any.
328 Local_dynamic_type local_dynamic_type_;
331 // A class which returns the size required for a relocation type,
332 // used while scanning relocs during a relocatable link.
333 class Relocatable_size_for_reloc
335 public:
336 unsigned int
337 get_size_for_reloc(unsigned int, Relobj*);
340 // Adjust TLS relocation type based on the options and whether this
341 // is a local symbol.
342 static tls::Tls_optimization
343 optimize_tls_reloc(bool is_final, int r_type);
345 // Get the GOT section, creating it if necessary.
346 Output_data_got<32, false>*
347 got_section(Symbol_table*, Layout*);
349 // Get the GOT PLT section.
350 Output_data_space*
351 got_plt_section() const
353 gold_assert(this->got_plt_ != NULL);
354 return this->got_plt_;
357 // Create a PLT entry for a global symbol.
358 void
359 make_plt_entry(Symbol_table*, Layout*, Symbol*);
361 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
362 void
363 define_tls_base_symbol(Symbol_table*, Layout*);
365 // Create a GOT entry for the TLS module index.
366 unsigned int
367 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
368 Sized_relobj<32, false>* object);
370 // Get the PLT section.
371 Output_data_plt_i386*
372 plt_section() const
374 gold_assert(this->plt_ != NULL);
375 return this->plt_;
378 // Get the dynamic reloc section, creating it if necessary.
379 Reloc_section*
380 rel_dyn_section(Layout*);
382 // Get the section to use for TLS_DESC relocations.
383 Reloc_section*
384 rel_tls_desc_section(Layout*) const;
386 // Add a potential copy relocation.
387 void
388 copy_reloc(Symbol_table* symtab, Layout* layout,
389 Sized_relobj<32, false>* object,
390 unsigned int shndx, Output_section* output_section,
391 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
393 this->copy_relocs_.copy_reloc(symtab, layout,
394 symtab->get_sized_symbol<32>(sym),
395 object, shndx, output_section, reloc,
396 this->rel_dyn_section(layout));
399 // Information about this specific target which we pass to the
400 // general Target structure.
401 static const Target::Target_info i386_info;
403 // The types of GOT entries needed for this platform.
404 enum Got_type
406 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
407 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
408 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
409 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
410 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
413 // The GOT section.
414 Output_data_got<32, false>* got_;
415 // The PLT section.
416 Output_data_plt_i386* plt_;
417 // The GOT PLT section.
418 Output_data_space* got_plt_;
419 // The _GLOBAL_OFFSET_TABLE_ symbol.
420 Symbol* global_offset_table_;
421 // The dynamic reloc section.
422 Reloc_section* rel_dyn_;
423 // Relocs saved to avoid a COPY reloc.
424 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
425 // Space for variables copied with a COPY reloc.
426 Output_data_space* dynbss_;
427 // Offset of the GOT entry for the TLS module index.
428 unsigned int got_mod_index_offset_;
429 // True if the _TLS_MODULE_BASE_ symbol has been defined.
430 bool tls_base_symbol_defined_;
433 const Target::Target_info Target_i386::i386_info =
435 32, // size
436 false, // is_big_endian
437 elfcpp::EM_386, // machine_code
438 false, // has_make_symbol
439 false, // has_resolve
440 true, // has_code_fill
441 true, // is_default_stack_executable
442 '\0', // wrap_char
443 "/usr/lib/libc.so.1", // dynamic_linker
444 0x08048000, // default_text_segment_address
445 0x1000, // abi_pagesize (overridable by -z max-page-size)
446 0x1000, // common_pagesize (overridable by -z common-page-size)
447 elfcpp::SHN_UNDEF, // small_common_shndx
448 elfcpp::SHN_UNDEF, // large_common_shndx
449 0, // small_common_section_flags
450 0, // large_common_section_flags
451 NULL, // attributes_section
452 NULL // attributes_vendor
455 // Get the GOT section, creating it if necessary.
457 Output_data_got<32, false>*
458 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
460 if (this->got_ == NULL)
462 gold_assert(symtab != NULL && layout != NULL);
464 this->got_ = new Output_data_got<32, false>();
466 Output_section* os;
467 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
468 (elfcpp::SHF_ALLOC
469 | elfcpp::SHF_WRITE),
470 this->got_, false, true, true,
471 false);
473 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
474 os = layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
475 (elfcpp::SHF_ALLOC
476 | elfcpp::SHF_WRITE),
477 this->got_plt_, false, false, false,
478 true);
480 // The first three entries are reserved.
481 this->got_plt_->set_current_data_size(3 * 4);
483 // Those bytes can go into the relro segment.
484 layout->increase_relro(3 * 4);
486 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
487 this->global_offset_table_ =
488 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
489 Symbol_table::PREDEFINED,
490 this->got_plt_,
491 0, 0, elfcpp::STT_OBJECT,
492 elfcpp::STB_LOCAL,
493 elfcpp::STV_HIDDEN, 0,
494 false, false);
497 return this->got_;
500 // Get the dynamic reloc section, creating it if necessary.
502 Target_i386::Reloc_section*
503 Target_i386::rel_dyn_section(Layout* layout)
505 if (this->rel_dyn_ == NULL)
507 gold_assert(layout != NULL);
508 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
509 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
510 elfcpp::SHF_ALLOC, this->rel_dyn_, true,
511 false, false, false);
513 return this->rel_dyn_;
516 // A class to handle the PLT data.
518 class Output_data_plt_i386 : public Output_section_data
520 public:
521 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
523 Output_data_plt_i386(Layout*, Output_data_space*);
525 // Add an entry to the PLT.
526 void
527 add_entry(Symbol* gsym);
529 // Return the .rel.plt section data.
530 const Reloc_section*
531 rel_plt() const
532 { return this->rel_; }
534 // Return where the TLS_DESC relocations should go.
535 Reloc_section*
536 rel_tls_desc(Layout*);
538 protected:
539 void
540 do_adjust_output_section(Output_section* os);
542 // Write to a map file.
543 void
544 do_print_to_mapfile(Mapfile* mapfile) const
545 { mapfile->print_output_data(this, _("** PLT")); }
547 private:
548 // The size of an entry in the PLT.
549 static const int plt_entry_size = 16;
551 // The first entry in the PLT for an executable.
552 static unsigned char exec_first_plt_entry[plt_entry_size];
554 // The first entry in the PLT for a shared object.
555 static unsigned char dyn_first_plt_entry[plt_entry_size];
557 // Other entries in the PLT for an executable.
558 static unsigned char exec_plt_entry[plt_entry_size];
560 // Other entries in the PLT for a shared object.
561 static unsigned char dyn_plt_entry[plt_entry_size];
563 // Set the final size.
564 void
565 set_final_data_size()
566 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
568 // Write out the PLT data.
569 void
570 do_write(Output_file*);
572 // The reloc section.
573 Reloc_section* rel_;
574 // The TLS_DESC relocations, if necessary. These must follow the
575 // regular PLT relocs.
576 Reloc_section* tls_desc_rel_;
577 // The .got.plt section.
578 Output_data_space* got_plt_;
579 // The number of PLT entries.
580 unsigned int count_;
583 // Create the PLT section. The ordinary .got section is an argument,
584 // since we need to refer to the start. We also create our own .got
585 // section just for PLT entries.
587 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
588 Output_data_space* got_plt)
589 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0)
591 this->rel_ = new Reloc_section(false);
592 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
593 elfcpp::SHF_ALLOC, this->rel_, true,
594 false, false, false);
597 void
598 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
600 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
601 // linker, and so do we.
602 os->set_entsize(4);
605 // Add an entry to the PLT.
607 void
608 Output_data_plt_i386::add_entry(Symbol* gsym)
610 gold_assert(!gsym->has_plt_offset());
612 // Note that when setting the PLT offset we skip the initial
613 // reserved PLT entry.
614 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
616 ++this->count_;
618 section_offset_type got_offset = this->got_plt_->current_data_size();
620 // Every PLT entry needs a GOT entry which points back to the PLT
621 // entry (this will be changed by the dynamic linker, normally
622 // lazily when the function is called).
623 this->got_plt_->set_current_data_size(got_offset + 4);
625 // Every PLT entry needs a reloc.
626 gsym->set_needs_dynsym_entry();
627 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
628 got_offset);
630 // Note that we don't need to save the symbol. The contents of the
631 // PLT are independent of which symbols are used. The symbols only
632 // appear in the relocations.
635 // Return where the TLS_DESC relocations should go, creating it if
636 // necessary. These follow the JUMP_SLOT relocations.
638 Output_data_plt_i386::Reloc_section*
639 Output_data_plt_i386::rel_tls_desc(Layout* layout)
641 if (this->tls_desc_rel_ == NULL)
643 this->tls_desc_rel_ = new Reloc_section(false);
644 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
645 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
646 true, false, false, false);
647 gold_assert(this->tls_desc_rel_->output_section() ==
648 this->rel_->output_section());
650 return this->tls_desc_rel_;
653 // The first entry in the PLT for an executable.
655 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
657 0xff, 0x35, // pushl contents of memory address
658 0, 0, 0, 0, // replaced with address of .got + 4
659 0xff, 0x25, // jmp indirect
660 0, 0, 0, 0, // replaced with address of .got + 8
661 0, 0, 0, 0 // unused
664 // The first entry in the PLT for a shared object.
666 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
668 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
669 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
670 0, 0, 0, 0 // unused
673 // Subsequent entries in the PLT for an executable.
675 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
677 0xff, 0x25, // jmp indirect
678 0, 0, 0, 0, // replaced with address of symbol in .got
679 0x68, // pushl immediate
680 0, 0, 0, 0, // replaced with offset into relocation table
681 0xe9, // jmp relative
682 0, 0, 0, 0 // replaced with offset to start of .plt
685 // Subsequent entries in the PLT for a shared object.
687 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
689 0xff, 0xa3, // jmp *offset(%ebx)
690 0, 0, 0, 0, // replaced with offset of symbol in .got
691 0x68, // pushl immediate
692 0, 0, 0, 0, // replaced with offset into relocation table
693 0xe9, // jmp relative
694 0, 0, 0, 0 // replaced with offset to start of .plt
697 // Write out the PLT. This uses the hand-coded instructions above,
698 // and adjusts them as needed. This is all specified by the i386 ELF
699 // Processor Supplement.
701 void
702 Output_data_plt_i386::do_write(Output_file* of)
704 const off_t offset = this->offset();
705 const section_size_type oview_size =
706 convert_to_section_size_type(this->data_size());
707 unsigned char* const oview = of->get_output_view(offset, oview_size);
709 const off_t got_file_offset = this->got_plt_->offset();
710 const section_size_type got_size =
711 convert_to_section_size_type(this->got_plt_->data_size());
712 unsigned char* const got_view = of->get_output_view(got_file_offset,
713 got_size);
715 unsigned char* pov = oview;
717 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
718 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
720 if (parameters->options().output_is_position_independent())
721 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
722 else
724 memcpy(pov, exec_first_plt_entry, plt_entry_size);
725 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
726 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
728 pov += plt_entry_size;
730 unsigned char* got_pov = got_view;
732 memset(got_pov, 0, 12);
733 got_pov += 12;
735 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
737 unsigned int plt_offset = plt_entry_size;
738 unsigned int plt_rel_offset = 0;
739 unsigned int got_offset = 12;
740 const unsigned int count = this->count_;
741 for (unsigned int i = 0;
742 i < count;
743 ++i,
744 pov += plt_entry_size,
745 got_pov += 4,
746 plt_offset += plt_entry_size,
747 plt_rel_offset += rel_size,
748 got_offset += 4)
750 // Set and adjust the PLT entry itself.
752 if (parameters->options().output_is_position_independent())
754 memcpy(pov, dyn_plt_entry, plt_entry_size);
755 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
757 else
759 memcpy(pov, exec_plt_entry, plt_entry_size);
760 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
761 (got_address
762 + got_offset));
765 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
766 elfcpp::Swap<32, false>::writeval(pov + 12,
767 - (plt_offset + plt_entry_size));
769 // Set the entry in the GOT.
770 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
773 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
774 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
776 of->write_output_view(offset, oview_size, oview);
777 of->write_output_view(got_file_offset, got_size, got_view);
780 // Create a PLT entry for a global symbol.
782 void
783 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
785 if (gsym->has_plt_offset())
786 return;
788 if (this->plt_ == NULL)
790 // Create the GOT sections first.
791 this->got_section(symtab, layout);
793 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
794 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
795 (elfcpp::SHF_ALLOC
796 | elfcpp::SHF_EXECINSTR),
797 this->plt_, false, false, false, false);
800 this->plt_->add_entry(gsym);
803 // Get the section to use for TLS_DESC relocations.
805 Target_i386::Reloc_section*
806 Target_i386::rel_tls_desc_section(Layout* layout) const
808 return this->plt_section()->rel_tls_desc(layout);
811 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
813 void
814 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
816 if (this->tls_base_symbol_defined_)
817 return;
819 Output_segment* tls_segment = layout->tls_segment();
820 if (tls_segment != NULL)
822 bool is_exec = parameters->options().output_is_executable();
823 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
824 Symbol_table::PREDEFINED,
825 tls_segment, 0, 0,
826 elfcpp::STT_TLS,
827 elfcpp::STB_LOCAL,
828 elfcpp::STV_HIDDEN, 0,
829 (is_exec
830 ? Symbol::SEGMENT_END
831 : Symbol::SEGMENT_START),
832 true);
834 this->tls_base_symbol_defined_ = true;
837 // Create a GOT entry for the TLS module index.
839 unsigned int
840 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
841 Sized_relobj<32, false>* object)
843 if (this->got_mod_index_offset_ == -1U)
845 gold_assert(symtab != NULL && layout != NULL && object != NULL);
846 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
847 Output_data_got<32, false>* got = this->got_section(symtab, layout);
848 unsigned int got_offset = got->add_constant(0);
849 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
850 got_offset);
851 got->add_constant(0);
852 this->got_mod_index_offset_ = got_offset;
854 return this->got_mod_index_offset_;
857 // Optimize the TLS relocation type based on what we know about the
858 // symbol. IS_FINAL is true if the final address of this symbol is
859 // known at link time.
861 tls::Tls_optimization
862 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
864 // If we are generating a shared library, then we can't do anything
865 // in the linker.
866 if (parameters->options().shared())
867 return tls::TLSOPT_NONE;
869 switch (r_type)
871 case elfcpp::R_386_TLS_GD:
872 case elfcpp::R_386_TLS_GOTDESC:
873 case elfcpp::R_386_TLS_DESC_CALL:
874 // These are General-Dynamic which permits fully general TLS
875 // access. Since we know that we are generating an executable,
876 // we can convert this to Initial-Exec. If we also know that
877 // this is a local symbol, we can further switch to Local-Exec.
878 if (is_final)
879 return tls::TLSOPT_TO_LE;
880 return tls::TLSOPT_TO_IE;
882 case elfcpp::R_386_TLS_LDM:
883 // This is Local-Dynamic, which refers to a local symbol in the
884 // dynamic TLS block. Since we know that we generating an
885 // executable, we can switch to Local-Exec.
886 return tls::TLSOPT_TO_LE;
888 case elfcpp::R_386_TLS_LDO_32:
889 // Another type of Local-Dynamic relocation.
890 return tls::TLSOPT_TO_LE;
892 case elfcpp::R_386_TLS_IE:
893 case elfcpp::R_386_TLS_GOTIE:
894 case elfcpp::R_386_TLS_IE_32:
895 // These are Initial-Exec relocs which get the thread offset
896 // from the GOT. If we know that we are linking against the
897 // local symbol, we can switch to Local-Exec, which links the
898 // thread offset into the instruction.
899 if (is_final)
900 return tls::TLSOPT_TO_LE;
901 return tls::TLSOPT_NONE;
903 case elfcpp::R_386_TLS_LE:
904 case elfcpp::R_386_TLS_LE_32:
905 // When we already have Local-Exec, there is nothing further we
906 // can do.
907 return tls::TLSOPT_NONE;
909 default:
910 gold_unreachable();
914 // Report an unsupported relocation against a local symbol.
916 void
917 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
918 unsigned int r_type)
920 gold_error(_("%s: unsupported reloc %u against local symbol"),
921 object->name().c_str(), r_type);
924 // Scan a relocation for a local symbol.
926 inline void
927 Target_i386::Scan::local(Symbol_table* symtab,
928 Layout* layout,
929 Target_i386* target,
930 Sized_relobj<32, false>* object,
931 unsigned int data_shndx,
932 Output_section* output_section,
933 const elfcpp::Rel<32, false>& reloc,
934 unsigned int r_type,
935 const elfcpp::Sym<32, false>& lsym)
937 switch (r_type)
939 case elfcpp::R_386_NONE:
940 case elfcpp::R_386_GNU_VTINHERIT:
941 case elfcpp::R_386_GNU_VTENTRY:
942 break;
944 case elfcpp::R_386_32:
945 // If building a shared library (or a position-independent
946 // executable), we need to create a dynamic relocation for
947 // this location. The relocation applied at link time will
948 // apply the link-time value, so we flag the location with
949 // an R_386_RELATIVE relocation so the dynamic loader can
950 // relocate it easily.
951 if (parameters->options().output_is_position_independent())
953 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
954 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
955 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
956 output_section, data_shndx,
957 reloc.get_r_offset());
959 break;
961 case elfcpp::R_386_16:
962 case elfcpp::R_386_8:
963 // If building a shared library (or a position-independent
964 // executable), we need to create a dynamic relocation for
965 // this location. Because the addend needs to remain in the
966 // data section, we need to be careful not to apply this
967 // relocation statically.
968 if (parameters->options().output_is_position_independent())
970 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
971 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
972 if (lsym.get_st_type() != elfcpp::STT_SECTION)
973 rel_dyn->add_local(object, r_sym, r_type, output_section,
974 data_shndx, reloc.get_r_offset());
975 else
977 gold_assert(lsym.get_st_value() == 0);
978 unsigned int shndx = lsym.get_st_shndx();
979 bool is_ordinary;
980 shndx = object->adjust_sym_shndx(r_sym, shndx,
981 &is_ordinary);
982 if (!is_ordinary)
983 object->error(_("section symbol %u has bad shndx %u"),
984 r_sym, shndx);
985 else
986 rel_dyn->add_local_section(object, shndx,
987 r_type, output_section,
988 data_shndx, reloc.get_r_offset());
991 break;
993 case elfcpp::R_386_PC32:
994 case elfcpp::R_386_PC16:
995 case elfcpp::R_386_PC8:
996 break;
998 case elfcpp::R_386_PLT32:
999 // Since we know this is a local symbol, we can handle this as a
1000 // PC32 reloc.
1001 break;
1003 case elfcpp::R_386_GOTOFF:
1004 case elfcpp::R_386_GOTPC:
1005 // We need a GOT section.
1006 target->got_section(symtab, layout);
1007 break;
1009 case elfcpp::R_386_GOT32:
1011 // The symbol requires a GOT entry.
1012 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1013 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1014 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1016 // If we are generating a shared object, we need to add a
1017 // dynamic RELATIVE relocation for this symbol's GOT entry.
1018 if (parameters->options().output_is_position_independent())
1020 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1021 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1022 rel_dyn->add_local_relative(
1023 object, r_sym, elfcpp::R_386_RELATIVE, got,
1024 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1028 break;
1030 // These are relocations which should only be seen by the
1031 // dynamic linker, and should never be seen here.
1032 case elfcpp::R_386_COPY:
1033 case elfcpp::R_386_GLOB_DAT:
1034 case elfcpp::R_386_JUMP_SLOT:
1035 case elfcpp::R_386_RELATIVE:
1036 case elfcpp::R_386_TLS_TPOFF:
1037 case elfcpp::R_386_TLS_DTPMOD32:
1038 case elfcpp::R_386_TLS_DTPOFF32:
1039 case elfcpp::R_386_TLS_TPOFF32:
1040 case elfcpp::R_386_TLS_DESC:
1041 gold_error(_("%s: unexpected reloc %u in object file"),
1042 object->name().c_str(), r_type);
1043 break;
1045 // These are initial TLS relocs, which are expected when
1046 // linking.
1047 case elfcpp::R_386_TLS_GD: // Global-dynamic
1048 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1049 case elfcpp::R_386_TLS_DESC_CALL:
1050 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1051 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1052 case elfcpp::R_386_TLS_IE: // Initial-exec
1053 case elfcpp::R_386_TLS_IE_32:
1054 case elfcpp::R_386_TLS_GOTIE:
1055 case elfcpp::R_386_TLS_LE: // Local-exec
1056 case elfcpp::R_386_TLS_LE_32:
1058 bool output_is_shared = parameters->options().shared();
1059 const tls::Tls_optimization optimized_type
1060 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1061 switch (r_type)
1063 case elfcpp::R_386_TLS_GD: // Global-dynamic
1064 if (optimized_type == tls::TLSOPT_NONE)
1066 // Create a pair of GOT entries for the module index and
1067 // dtv-relative offset.
1068 Output_data_got<32, false>* got
1069 = target->got_section(symtab, layout);
1070 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1071 unsigned int shndx = lsym.get_st_shndx();
1072 bool is_ordinary;
1073 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1074 if (!is_ordinary)
1075 object->error(_("local symbol %u has bad shndx %u"),
1076 r_sym, shndx);
1077 else
1078 got->add_local_pair_with_rel(object, r_sym, shndx,
1079 GOT_TYPE_TLS_PAIR,
1080 target->rel_dyn_section(layout),
1081 elfcpp::R_386_TLS_DTPMOD32, 0);
1083 else if (optimized_type != tls::TLSOPT_TO_LE)
1084 unsupported_reloc_local(object, r_type);
1085 break;
1087 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1088 target->define_tls_base_symbol(symtab, layout);
1089 if (optimized_type == tls::TLSOPT_NONE)
1091 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1092 Output_data_got<32, false>* got
1093 = target->got_section(symtab, layout);
1094 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1095 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1097 unsigned int got_offset = got->add_constant(0);
1098 // The local symbol value is stored in the second
1099 // GOT entry.
1100 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1101 // That set the GOT offset of the local symbol to
1102 // point to the second entry, but we want it to
1103 // point to the first.
1104 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1105 got_offset);
1106 Reloc_section* rt = target->rel_tls_desc_section(layout);
1107 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1110 else if (optimized_type != tls::TLSOPT_TO_LE)
1111 unsupported_reloc_local(object, r_type);
1112 break;
1114 case elfcpp::R_386_TLS_DESC_CALL:
1115 break;
1117 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1118 if (optimized_type == tls::TLSOPT_NONE)
1120 // Create a GOT entry for the module index.
1121 target->got_mod_index_entry(symtab, layout, object);
1123 else if (optimized_type != tls::TLSOPT_TO_LE)
1124 unsupported_reloc_local(object, r_type);
1125 break;
1127 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1128 break;
1130 case elfcpp::R_386_TLS_IE: // Initial-exec
1131 case elfcpp::R_386_TLS_IE_32:
1132 case elfcpp::R_386_TLS_GOTIE:
1133 layout->set_has_static_tls();
1134 if (optimized_type == tls::TLSOPT_NONE)
1136 // For the R_386_TLS_IE relocation, we need to create a
1137 // dynamic relocation when building a shared library.
1138 if (r_type == elfcpp::R_386_TLS_IE
1139 && parameters->options().shared())
1141 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1142 unsigned int r_sym
1143 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1144 rel_dyn->add_local_relative(object, r_sym,
1145 elfcpp::R_386_RELATIVE,
1146 output_section, data_shndx,
1147 reloc.get_r_offset());
1149 // Create a GOT entry for the tp-relative offset.
1150 Output_data_got<32, false>* got
1151 = target->got_section(symtab, layout);
1152 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1153 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1154 ? elfcpp::R_386_TLS_TPOFF32
1155 : elfcpp::R_386_TLS_TPOFF);
1156 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1157 ? GOT_TYPE_TLS_OFFSET
1158 : GOT_TYPE_TLS_NOFFSET);
1159 got->add_local_with_rel(object, r_sym, got_type,
1160 target->rel_dyn_section(layout),
1161 dyn_r_type);
1163 else if (optimized_type != tls::TLSOPT_TO_LE)
1164 unsupported_reloc_local(object, r_type);
1165 break;
1167 case elfcpp::R_386_TLS_LE: // Local-exec
1168 case elfcpp::R_386_TLS_LE_32:
1169 layout->set_has_static_tls();
1170 if (output_is_shared)
1172 // We need to create a dynamic relocation.
1173 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1174 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1175 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1176 ? elfcpp::R_386_TLS_TPOFF32
1177 : elfcpp::R_386_TLS_TPOFF);
1178 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1179 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1180 data_shndx, reloc.get_r_offset());
1182 break;
1184 default:
1185 gold_unreachable();
1188 break;
1190 case elfcpp::R_386_32PLT:
1191 case elfcpp::R_386_TLS_GD_32:
1192 case elfcpp::R_386_TLS_GD_PUSH:
1193 case elfcpp::R_386_TLS_GD_CALL:
1194 case elfcpp::R_386_TLS_GD_POP:
1195 case elfcpp::R_386_TLS_LDM_32:
1196 case elfcpp::R_386_TLS_LDM_PUSH:
1197 case elfcpp::R_386_TLS_LDM_CALL:
1198 case elfcpp::R_386_TLS_LDM_POP:
1199 case elfcpp::R_386_USED_BY_INTEL_200:
1200 default:
1201 unsupported_reloc_local(object, r_type);
1202 break;
1206 // Report an unsupported relocation against a global symbol.
1208 void
1209 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1210 unsigned int r_type,
1211 Symbol* gsym)
1213 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1214 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1217 // Scan a relocation for a global symbol.
1219 inline void
1220 Target_i386::Scan::global(Symbol_table* symtab,
1221 Layout* layout,
1222 Target_i386* target,
1223 Sized_relobj<32, false>* object,
1224 unsigned int data_shndx,
1225 Output_section* output_section,
1226 const elfcpp::Rel<32, false>& reloc,
1227 unsigned int r_type,
1228 Symbol* gsym)
1230 switch (r_type)
1232 case elfcpp::R_386_NONE:
1233 case elfcpp::R_386_GNU_VTINHERIT:
1234 case elfcpp::R_386_GNU_VTENTRY:
1235 break;
1237 case elfcpp::R_386_32:
1238 case elfcpp::R_386_16:
1239 case elfcpp::R_386_8:
1241 // Make a PLT entry if necessary.
1242 if (gsym->needs_plt_entry())
1244 target->make_plt_entry(symtab, layout, gsym);
1245 // Since this is not a PC-relative relocation, we may be
1246 // taking the address of a function. In that case we need to
1247 // set the entry in the dynamic symbol table to the address of
1248 // the PLT entry.
1249 if (gsym->is_from_dynobj() && !parameters->options().shared())
1250 gsym->set_needs_dynsym_value();
1252 // Make a dynamic relocation if necessary.
1253 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1255 if (gsym->may_need_copy_reloc())
1257 target->copy_reloc(symtab, layout, object,
1258 data_shndx, output_section, gsym, reloc);
1260 else if (r_type == elfcpp::R_386_32
1261 && gsym->can_use_relative_reloc(false))
1263 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1264 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1265 output_section, object,
1266 data_shndx, reloc.get_r_offset());
1268 else
1270 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1271 rel_dyn->add_global(gsym, r_type, output_section, object,
1272 data_shndx, reloc.get_r_offset());
1276 break;
1278 case elfcpp::R_386_PC32:
1279 case elfcpp::R_386_PC16:
1280 case elfcpp::R_386_PC8:
1282 // Make a PLT entry if necessary.
1283 if (gsym->needs_plt_entry())
1285 // These relocations are used for function calls only in
1286 // non-PIC code. For a 32-bit relocation in a shared library,
1287 // we'll need a text relocation anyway, so we can skip the
1288 // PLT entry and let the dynamic linker bind the call directly
1289 // to the target. For smaller relocations, we should use a
1290 // PLT entry to ensure that the call can reach.
1291 if (!parameters->options().shared()
1292 || r_type != elfcpp::R_386_PC32)
1293 target->make_plt_entry(symtab, layout, gsym);
1295 // Make a dynamic relocation if necessary.
1296 int flags = Symbol::NON_PIC_REF;
1297 if (gsym->is_func())
1298 flags |= Symbol::FUNCTION_CALL;
1299 if (gsym->needs_dynamic_reloc(flags))
1301 if (gsym->may_need_copy_reloc())
1303 target->copy_reloc(symtab, layout, object,
1304 data_shndx, output_section, gsym, reloc);
1306 else
1308 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1309 rel_dyn->add_global(gsym, r_type, output_section, object,
1310 data_shndx, reloc.get_r_offset());
1314 break;
1316 case elfcpp::R_386_GOT32:
1318 // The symbol requires a GOT entry.
1319 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1320 if (gsym->final_value_is_known())
1321 got->add_global(gsym, GOT_TYPE_STANDARD);
1322 else
1324 // If this symbol is not fully resolved, we need to add a
1325 // GOT entry with a dynamic relocation.
1326 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1327 if (gsym->is_from_dynobj()
1328 || gsym->is_undefined()
1329 || gsym->is_preemptible())
1330 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1331 rel_dyn, elfcpp::R_386_GLOB_DAT);
1332 else
1334 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1335 rel_dyn->add_global_relative(
1336 gsym, elfcpp::R_386_RELATIVE, got,
1337 gsym->got_offset(GOT_TYPE_STANDARD));
1341 break;
1343 case elfcpp::R_386_PLT32:
1344 // If the symbol is fully resolved, this is just a PC32 reloc.
1345 // Otherwise we need a PLT entry.
1346 if (gsym->final_value_is_known())
1347 break;
1348 // If building a shared library, we can also skip the PLT entry
1349 // if the symbol is defined in the output file and is protected
1350 // or hidden.
1351 if (gsym->is_defined()
1352 && !gsym->is_from_dynobj()
1353 && !gsym->is_preemptible())
1354 break;
1355 target->make_plt_entry(symtab, layout, gsym);
1356 break;
1358 case elfcpp::R_386_GOTOFF:
1359 case elfcpp::R_386_GOTPC:
1360 // We need a GOT section.
1361 target->got_section(symtab, layout);
1362 break;
1364 // These are relocations which should only be seen by the
1365 // dynamic linker, and should never be seen here.
1366 case elfcpp::R_386_COPY:
1367 case elfcpp::R_386_GLOB_DAT:
1368 case elfcpp::R_386_JUMP_SLOT:
1369 case elfcpp::R_386_RELATIVE:
1370 case elfcpp::R_386_TLS_TPOFF:
1371 case elfcpp::R_386_TLS_DTPMOD32:
1372 case elfcpp::R_386_TLS_DTPOFF32:
1373 case elfcpp::R_386_TLS_TPOFF32:
1374 case elfcpp::R_386_TLS_DESC:
1375 gold_error(_("%s: unexpected reloc %u in object file"),
1376 object->name().c_str(), r_type);
1377 break;
1379 // These are initial tls relocs, which are expected when
1380 // linking.
1381 case elfcpp::R_386_TLS_GD: // Global-dynamic
1382 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1383 case elfcpp::R_386_TLS_DESC_CALL:
1384 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1385 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1386 case elfcpp::R_386_TLS_IE: // Initial-exec
1387 case elfcpp::R_386_TLS_IE_32:
1388 case elfcpp::R_386_TLS_GOTIE:
1389 case elfcpp::R_386_TLS_LE: // Local-exec
1390 case elfcpp::R_386_TLS_LE_32:
1392 const bool is_final = gsym->final_value_is_known();
1393 const tls::Tls_optimization optimized_type
1394 = Target_i386::optimize_tls_reloc(is_final, r_type);
1395 switch (r_type)
1397 case elfcpp::R_386_TLS_GD: // Global-dynamic
1398 if (optimized_type == tls::TLSOPT_NONE)
1400 // Create a pair of GOT entries for the module index and
1401 // dtv-relative offset.
1402 Output_data_got<32, false>* got
1403 = target->got_section(symtab, layout);
1404 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1405 target->rel_dyn_section(layout),
1406 elfcpp::R_386_TLS_DTPMOD32,
1407 elfcpp::R_386_TLS_DTPOFF32);
1409 else if (optimized_type == tls::TLSOPT_TO_IE)
1411 // Create a GOT entry for the tp-relative offset.
1412 Output_data_got<32, false>* got
1413 = target->got_section(symtab, layout);
1414 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1415 target->rel_dyn_section(layout),
1416 elfcpp::R_386_TLS_TPOFF);
1418 else if (optimized_type != tls::TLSOPT_TO_LE)
1419 unsupported_reloc_global(object, r_type, gsym);
1420 break;
1422 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1423 target->define_tls_base_symbol(symtab, layout);
1424 if (optimized_type == tls::TLSOPT_NONE)
1426 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1427 Output_data_got<32, false>* got
1428 = target->got_section(symtab, layout);
1429 Reloc_section* rt = target->rel_tls_desc_section(layout);
1430 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1431 elfcpp::R_386_TLS_DESC, 0);
1433 else if (optimized_type == tls::TLSOPT_TO_IE)
1435 // Create a GOT entry for the tp-relative offset.
1436 Output_data_got<32, false>* got
1437 = target->got_section(symtab, layout);
1438 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1439 target->rel_dyn_section(layout),
1440 elfcpp::R_386_TLS_TPOFF);
1442 else if (optimized_type != tls::TLSOPT_TO_LE)
1443 unsupported_reloc_global(object, r_type, gsym);
1444 break;
1446 case elfcpp::R_386_TLS_DESC_CALL:
1447 break;
1449 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1450 if (optimized_type == tls::TLSOPT_NONE)
1452 // Create a GOT entry for the module index.
1453 target->got_mod_index_entry(symtab, layout, object);
1455 else if (optimized_type != tls::TLSOPT_TO_LE)
1456 unsupported_reloc_global(object, r_type, gsym);
1457 break;
1459 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1460 break;
1462 case elfcpp::R_386_TLS_IE: // Initial-exec
1463 case elfcpp::R_386_TLS_IE_32:
1464 case elfcpp::R_386_TLS_GOTIE:
1465 layout->set_has_static_tls();
1466 if (optimized_type == tls::TLSOPT_NONE)
1468 // For the R_386_TLS_IE relocation, we need to create a
1469 // dynamic relocation when building a shared library.
1470 if (r_type == elfcpp::R_386_TLS_IE
1471 && parameters->options().shared())
1473 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1474 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1475 output_section, object,
1476 data_shndx,
1477 reloc.get_r_offset());
1479 // Create a GOT entry for the tp-relative offset.
1480 Output_data_got<32, false>* got
1481 = target->got_section(symtab, layout);
1482 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1483 ? elfcpp::R_386_TLS_TPOFF32
1484 : elfcpp::R_386_TLS_TPOFF);
1485 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1486 ? GOT_TYPE_TLS_OFFSET
1487 : GOT_TYPE_TLS_NOFFSET);
1488 got->add_global_with_rel(gsym, got_type,
1489 target->rel_dyn_section(layout),
1490 dyn_r_type);
1492 else if (optimized_type != tls::TLSOPT_TO_LE)
1493 unsupported_reloc_global(object, r_type, gsym);
1494 break;
1496 case elfcpp::R_386_TLS_LE: // Local-exec
1497 case elfcpp::R_386_TLS_LE_32:
1498 layout->set_has_static_tls();
1499 if (parameters->options().shared())
1501 // We need to create a dynamic relocation.
1502 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1503 ? elfcpp::R_386_TLS_TPOFF32
1504 : elfcpp::R_386_TLS_TPOFF);
1505 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1506 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1507 data_shndx, reloc.get_r_offset());
1509 break;
1511 default:
1512 gold_unreachable();
1515 break;
1517 case elfcpp::R_386_32PLT:
1518 case elfcpp::R_386_TLS_GD_32:
1519 case elfcpp::R_386_TLS_GD_PUSH:
1520 case elfcpp::R_386_TLS_GD_CALL:
1521 case elfcpp::R_386_TLS_GD_POP:
1522 case elfcpp::R_386_TLS_LDM_32:
1523 case elfcpp::R_386_TLS_LDM_PUSH:
1524 case elfcpp::R_386_TLS_LDM_CALL:
1525 case elfcpp::R_386_TLS_LDM_POP:
1526 case elfcpp::R_386_USED_BY_INTEL_200:
1527 default:
1528 unsupported_reloc_global(object, r_type, gsym);
1529 break;
1533 // Process relocations for gc.
1535 void
1536 Target_i386::gc_process_relocs(Symbol_table* symtab,
1537 Layout* layout,
1538 Sized_relobj<32, false>* object,
1539 unsigned int data_shndx,
1540 unsigned int,
1541 const unsigned char* prelocs,
1542 size_t reloc_count,
1543 Output_section* output_section,
1544 bool needs_special_offset_handling,
1545 size_t local_symbol_count,
1546 const unsigned char* plocal_symbols)
1548 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1549 Target_i386::Scan>(
1550 symtab,
1551 layout,
1552 this,
1553 object,
1554 data_shndx,
1555 prelocs,
1556 reloc_count,
1557 output_section,
1558 needs_special_offset_handling,
1559 local_symbol_count,
1560 plocal_symbols);
1563 // Scan relocations for a section.
1565 void
1566 Target_i386::scan_relocs(Symbol_table* symtab,
1567 Layout* layout,
1568 Sized_relobj<32, false>* object,
1569 unsigned int data_shndx,
1570 unsigned int sh_type,
1571 const unsigned char* prelocs,
1572 size_t reloc_count,
1573 Output_section* output_section,
1574 bool needs_special_offset_handling,
1575 size_t local_symbol_count,
1576 const unsigned char* plocal_symbols)
1578 if (sh_type == elfcpp::SHT_RELA)
1580 gold_error(_("%s: unsupported RELA reloc section"),
1581 object->name().c_str());
1582 return;
1585 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1586 Target_i386::Scan>(
1587 symtab,
1588 layout,
1589 this,
1590 object,
1591 data_shndx,
1592 prelocs,
1593 reloc_count,
1594 output_section,
1595 needs_special_offset_handling,
1596 local_symbol_count,
1597 plocal_symbols);
1600 // Finalize the sections.
1602 void
1603 Target_i386::do_finalize_sections(
1604 Layout* layout,
1605 const Input_objects*,
1606 Symbol_table* symtab)
1608 const Reloc_section* rel_plt = (this->plt_ == NULL
1609 ? NULL
1610 : this->plt_->rel_plt());
1611 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
1612 this->rel_dyn_, true);
1614 // Emit any relocs we saved in an attempt to avoid generating COPY
1615 // relocs.
1616 if (this->copy_relocs_.any_saved_relocs())
1617 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1619 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
1620 // the .got.plt section.
1621 Symbol* sym = this->global_offset_table_;
1622 if (sym != NULL)
1624 uint32_t data_size = this->got_plt_->current_data_size();
1625 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
1629 // Return whether a direct absolute static relocation needs to be applied.
1630 // In cases where Scan::local() or Scan::global() has created
1631 // a dynamic relocation other than R_386_RELATIVE, the addend
1632 // of the relocation is carried in the data, and we must not
1633 // apply the static relocation.
1635 inline bool
1636 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1637 int ref_flags,
1638 bool is_32bit,
1639 Output_section* output_section)
1641 // If the output section is not allocated, then we didn't call
1642 // scan_relocs, we didn't create a dynamic reloc, and we must apply
1643 // the reloc here.
1644 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1645 return true;
1647 // For local symbols, we will have created a non-RELATIVE dynamic
1648 // relocation only if (a) the output is position independent,
1649 // (b) the relocation is absolute (not pc- or segment-relative), and
1650 // (c) the relocation is not 32 bits wide.
1651 if (gsym == NULL)
1652 return !(parameters->options().output_is_position_independent()
1653 && (ref_flags & Symbol::ABSOLUTE_REF)
1654 && !is_32bit);
1656 // For global symbols, we use the same helper routines used in the
1657 // scan pass. If we did not create a dynamic relocation, or if we
1658 // created a RELATIVE dynamic relocation, we should apply the static
1659 // relocation.
1660 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1661 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1662 && gsym->can_use_relative_reloc(ref_flags
1663 & Symbol::FUNCTION_CALL);
1664 return !has_dyn || is_rel;
1667 // Perform a relocation.
1669 inline bool
1670 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1671 Target_i386* target,
1672 Output_section *output_section,
1673 size_t relnum,
1674 const elfcpp::Rel<32, false>& rel,
1675 unsigned int r_type,
1676 const Sized_symbol<32>* gsym,
1677 const Symbol_value<32>* psymval,
1678 unsigned char* view,
1679 elfcpp::Elf_types<32>::Elf_Addr address,
1680 section_size_type view_size)
1682 if (this->skip_call_tls_get_addr_)
1684 if ((r_type != elfcpp::R_386_PLT32
1685 && r_type != elfcpp::R_386_PC32)
1686 || gsym == NULL
1687 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1688 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1689 _("missing expected TLS relocation"));
1690 else
1692 this->skip_call_tls_get_addr_ = false;
1693 return false;
1697 // Pick the value to use for symbols defined in shared objects.
1698 Symbol_value<32> symval;
1699 if (gsym != NULL
1700 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1701 || r_type == elfcpp::R_386_PC16
1702 || r_type == elfcpp::R_386_PC32))
1704 symval.set_output_value(target->plt_section()->address()
1705 + gsym->plt_offset());
1706 psymval = &symval;
1709 const Sized_relobj<32, false>* object = relinfo->object;
1711 // Get the GOT offset if needed.
1712 // The GOT pointer points to the end of the GOT section.
1713 // We need to subtract the size of the GOT section to get
1714 // the actual offset to use in the relocation.
1715 bool have_got_offset = false;
1716 unsigned int got_offset = 0;
1717 switch (r_type)
1719 case elfcpp::R_386_GOT32:
1720 if (gsym != NULL)
1722 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1723 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1724 - target->got_size());
1726 else
1728 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1729 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1730 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1731 - target->got_size());
1733 have_got_offset = true;
1734 break;
1736 default:
1737 break;
1740 switch (r_type)
1742 case elfcpp::R_386_NONE:
1743 case elfcpp::R_386_GNU_VTINHERIT:
1744 case elfcpp::R_386_GNU_VTENTRY:
1745 break;
1747 case elfcpp::R_386_32:
1748 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
1749 output_section))
1750 Relocate_functions<32, false>::rel32(view, object, psymval);
1751 break;
1753 case elfcpp::R_386_PC32:
1755 int ref_flags = Symbol::NON_PIC_REF;
1756 if (gsym != NULL && gsym->is_func())
1757 ref_flags |= Symbol::FUNCTION_CALL;
1758 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
1759 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1761 break;
1763 case elfcpp::R_386_16:
1764 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1765 output_section))
1766 Relocate_functions<32, false>::rel16(view, object, psymval);
1767 break;
1769 case elfcpp::R_386_PC16:
1771 int ref_flags = Symbol::NON_PIC_REF;
1772 if (gsym != NULL && gsym->is_func())
1773 ref_flags |= Symbol::FUNCTION_CALL;
1774 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
1775 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1777 break;
1779 case elfcpp::R_386_8:
1780 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
1781 output_section))
1782 Relocate_functions<32, false>::rel8(view, object, psymval);
1783 break;
1785 case elfcpp::R_386_PC8:
1787 int ref_flags = Symbol::NON_PIC_REF;
1788 if (gsym != NULL && gsym->is_func())
1789 ref_flags |= Symbol::FUNCTION_CALL;
1790 if (should_apply_static_reloc(gsym, ref_flags, false,
1791 output_section))
1792 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1794 break;
1796 case elfcpp::R_386_PLT32:
1797 gold_assert(gsym == NULL
1798 || gsym->has_plt_offset()
1799 || gsym->final_value_is_known()
1800 || (gsym->is_defined()
1801 && !gsym->is_from_dynobj()
1802 && !gsym->is_preemptible()));
1803 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1804 break;
1806 case elfcpp::R_386_GOT32:
1807 gold_assert(have_got_offset);
1808 Relocate_functions<32, false>::rel32(view, got_offset);
1809 break;
1811 case elfcpp::R_386_GOTOFF:
1813 elfcpp::Elf_types<32>::Elf_Addr value;
1814 value = (psymval->value(object, 0)
1815 - target->got_plt_section()->address());
1816 Relocate_functions<32, false>::rel32(view, value);
1818 break;
1820 case elfcpp::R_386_GOTPC:
1822 elfcpp::Elf_types<32>::Elf_Addr value;
1823 value = target->got_plt_section()->address();
1824 Relocate_functions<32, false>::pcrel32(view, value, address);
1826 break;
1828 case elfcpp::R_386_COPY:
1829 case elfcpp::R_386_GLOB_DAT:
1830 case elfcpp::R_386_JUMP_SLOT:
1831 case elfcpp::R_386_RELATIVE:
1832 // These are outstanding tls relocs, which are unexpected when
1833 // linking.
1834 case elfcpp::R_386_TLS_TPOFF:
1835 case elfcpp::R_386_TLS_DTPMOD32:
1836 case elfcpp::R_386_TLS_DTPOFF32:
1837 case elfcpp::R_386_TLS_TPOFF32:
1838 case elfcpp::R_386_TLS_DESC:
1839 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1840 _("unexpected reloc %u in object file"),
1841 r_type);
1842 break;
1844 // These are initial tls relocs, which are expected when
1845 // linking.
1846 case elfcpp::R_386_TLS_GD: // Global-dynamic
1847 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1848 case elfcpp::R_386_TLS_DESC_CALL:
1849 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1850 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1851 case elfcpp::R_386_TLS_IE: // Initial-exec
1852 case elfcpp::R_386_TLS_IE_32:
1853 case elfcpp::R_386_TLS_GOTIE:
1854 case elfcpp::R_386_TLS_LE: // Local-exec
1855 case elfcpp::R_386_TLS_LE_32:
1856 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1857 view, address, view_size);
1858 break;
1860 case elfcpp::R_386_32PLT:
1861 case elfcpp::R_386_TLS_GD_32:
1862 case elfcpp::R_386_TLS_GD_PUSH:
1863 case elfcpp::R_386_TLS_GD_CALL:
1864 case elfcpp::R_386_TLS_GD_POP:
1865 case elfcpp::R_386_TLS_LDM_32:
1866 case elfcpp::R_386_TLS_LDM_PUSH:
1867 case elfcpp::R_386_TLS_LDM_CALL:
1868 case elfcpp::R_386_TLS_LDM_POP:
1869 case elfcpp::R_386_USED_BY_INTEL_200:
1870 default:
1871 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1872 _("unsupported reloc %u"),
1873 r_type);
1874 break;
1877 return true;
1880 // Perform a TLS relocation.
1882 inline void
1883 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1884 Target_i386* target,
1885 size_t relnum,
1886 const elfcpp::Rel<32, false>& rel,
1887 unsigned int r_type,
1888 const Sized_symbol<32>* gsym,
1889 const Symbol_value<32>* psymval,
1890 unsigned char* view,
1891 elfcpp::Elf_types<32>::Elf_Addr,
1892 section_size_type view_size)
1894 Output_segment* tls_segment = relinfo->layout->tls_segment();
1896 const Sized_relobj<32, false>* object = relinfo->object;
1898 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1900 const bool is_final =
1901 (gsym == NULL
1902 ? !parameters->options().output_is_position_independent()
1903 : gsym->final_value_is_known());
1904 const tls::Tls_optimization optimized_type
1905 = Target_i386::optimize_tls_reloc(is_final, r_type);
1906 switch (r_type)
1908 case elfcpp::R_386_TLS_GD: // Global-dynamic
1909 if (optimized_type == tls::TLSOPT_TO_LE)
1911 gold_assert(tls_segment != NULL);
1912 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1913 rel, r_type, value, view,
1914 view_size);
1915 break;
1917 else
1919 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1920 ? GOT_TYPE_TLS_NOFFSET
1921 : GOT_TYPE_TLS_PAIR);
1922 unsigned int got_offset;
1923 if (gsym != NULL)
1925 gold_assert(gsym->has_got_offset(got_type));
1926 got_offset = gsym->got_offset(got_type) - target->got_size();
1928 else
1930 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1931 gold_assert(object->local_has_got_offset(r_sym, got_type));
1932 got_offset = (object->local_got_offset(r_sym, got_type)
1933 - target->got_size());
1935 if (optimized_type == tls::TLSOPT_TO_IE)
1937 gold_assert(tls_segment != NULL);
1938 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1939 got_offset, view, view_size);
1940 break;
1942 else if (optimized_type == tls::TLSOPT_NONE)
1944 // Relocate the field with the offset of the pair of GOT
1945 // entries.
1946 Relocate_functions<32, false>::rel32(view, got_offset);
1947 break;
1950 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1951 _("unsupported reloc %u"),
1952 r_type);
1953 break;
1955 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1956 case elfcpp::R_386_TLS_DESC_CALL:
1957 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1958 if (optimized_type == tls::TLSOPT_TO_LE)
1960 gold_assert(tls_segment != NULL);
1961 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1962 rel, r_type, value, view,
1963 view_size);
1964 break;
1966 else
1968 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1969 ? GOT_TYPE_TLS_NOFFSET
1970 : GOT_TYPE_TLS_DESC);
1971 unsigned int got_offset;
1972 if (gsym != NULL)
1974 gold_assert(gsym->has_got_offset(got_type));
1975 got_offset = gsym->got_offset(got_type) - target->got_size();
1977 else
1979 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1980 gold_assert(object->local_has_got_offset(r_sym, got_type));
1981 got_offset = (object->local_got_offset(r_sym, got_type)
1982 - target->got_size());
1984 if (optimized_type == tls::TLSOPT_TO_IE)
1986 gold_assert(tls_segment != NULL);
1987 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1988 got_offset, view, view_size);
1989 break;
1991 else if (optimized_type == tls::TLSOPT_NONE)
1993 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1995 // Relocate the field with the offset of the pair of GOT
1996 // entries.
1997 Relocate_functions<32, false>::rel32(view, got_offset);
1999 break;
2002 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2003 _("unsupported reloc %u"),
2004 r_type);
2005 break;
2007 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2008 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2010 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2011 _("both SUN and GNU model "
2012 "TLS relocations"));
2013 break;
2015 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2016 if (optimized_type == tls::TLSOPT_TO_LE)
2018 gold_assert(tls_segment != NULL);
2019 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2020 value, view, view_size);
2021 break;
2023 else if (optimized_type == tls::TLSOPT_NONE)
2025 // Relocate the field with the offset of the GOT entry for
2026 // the module index.
2027 unsigned int got_offset;
2028 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2029 - target->got_size());
2030 Relocate_functions<32, false>::rel32(view, got_offset);
2031 break;
2033 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2034 _("unsupported reloc %u"),
2035 r_type);
2036 break;
2038 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2039 if (optimized_type == tls::TLSOPT_TO_LE)
2041 // This reloc can appear in debugging sections, in which
2042 // case we must not convert to local-exec. We decide what
2043 // to do based on whether the section is marked as
2044 // containing executable code. That is what the GNU linker
2045 // does as well.
2046 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2047 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2049 gold_assert(tls_segment != NULL);
2050 value -= tls_segment->memsz();
2053 Relocate_functions<32, false>::rel32(view, value);
2054 break;
2056 case elfcpp::R_386_TLS_IE: // Initial-exec
2057 case elfcpp::R_386_TLS_GOTIE:
2058 case elfcpp::R_386_TLS_IE_32:
2059 if (optimized_type == tls::TLSOPT_TO_LE)
2061 gold_assert(tls_segment != NULL);
2062 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2063 rel, r_type, value, view,
2064 view_size);
2065 break;
2067 else if (optimized_type == tls::TLSOPT_NONE)
2069 // Relocate the field with the offset of the GOT entry for
2070 // the tp-relative offset of the symbol.
2071 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2072 ? GOT_TYPE_TLS_OFFSET
2073 : GOT_TYPE_TLS_NOFFSET);
2074 unsigned int got_offset;
2075 if (gsym != NULL)
2077 gold_assert(gsym->has_got_offset(got_type));
2078 got_offset = gsym->got_offset(got_type);
2080 else
2082 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2083 gold_assert(object->local_has_got_offset(r_sym, got_type));
2084 got_offset = object->local_got_offset(r_sym, got_type);
2086 // For the R_386_TLS_IE relocation, we need to apply the
2087 // absolute address of the GOT entry.
2088 if (r_type == elfcpp::R_386_TLS_IE)
2089 got_offset += target->got_plt_section()->address();
2090 // All GOT offsets are relative to the end of the GOT.
2091 got_offset -= target->got_size();
2092 Relocate_functions<32, false>::rel32(view, got_offset);
2093 break;
2095 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2096 _("unsupported reloc %u"),
2097 r_type);
2098 break;
2100 case elfcpp::R_386_TLS_LE: // Local-exec
2101 // If we're creating a shared library, a dynamic relocation will
2102 // have been created for this location, so do not apply it now.
2103 if (!parameters->options().shared())
2105 gold_assert(tls_segment != NULL);
2106 value -= tls_segment->memsz();
2107 Relocate_functions<32, false>::rel32(view, value);
2109 break;
2111 case elfcpp::R_386_TLS_LE_32:
2112 // If we're creating a shared library, a dynamic relocation will
2113 // have been created for this location, so do not apply it now.
2114 if (!parameters->options().shared())
2116 gold_assert(tls_segment != NULL);
2117 value = tls_segment->memsz() - value;
2118 Relocate_functions<32, false>::rel32(view, value);
2120 break;
2124 // Do a relocation in which we convert a TLS General-Dynamic to a
2125 // Local-Exec.
2127 inline void
2128 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2129 size_t relnum,
2130 Output_segment* tls_segment,
2131 const elfcpp::Rel<32, false>& rel,
2132 unsigned int,
2133 elfcpp::Elf_types<32>::Elf_Addr value,
2134 unsigned char* view,
2135 section_size_type view_size)
2137 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2138 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2139 // leal foo(%reg),%eax; call ___tls_get_addr
2140 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2142 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2143 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2145 unsigned char op1 = view[-1];
2146 unsigned char op2 = view[-2];
2148 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2149 op2 == 0x8d || op2 == 0x04);
2150 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2152 int roff = 5;
2154 if (op2 == 0x04)
2156 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2157 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2158 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2159 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2160 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2162 else
2164 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2165 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2166 if (rel.get_r_offset() + 9 < view_size
2167 && view[9] == 0x90)
2169 // There is a trailing nop. Use the size byte subl.
2170 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2171 roff = 6;
2173 else
2175 // Use the five byte subl.
2176 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2180 value = tls_segment->memsz() - value;
2181 Relocate_functions<32, false>::rel32(view + roff, value);
2183 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2184 // We can skip it.
2185 this->skip_call_tls_get_addr_ = true;
2188 // Do a relocation in which we convert a TLS General-Dynamic to an
2189 // Initial-Exec.
2191 inline void
2192 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2193 size_t relnum,
2194 Output_segment*,
2195 const elfcpp::Rel<32, false>& rel,
2196 unsigned int,
2197 elfcpp::Elf_types<32>::Elf_Addr value,
2198 unsigned char* view,
2199 section_size_type view_size)
2201 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2202 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2204 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2205 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2207 unsigned char op1 = view[-1];
2208 unsigned char op2 = view[-2];
2210 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2211 op2 == 0x8d || op2 == 0x04);
2212 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2214 int roff = 5;
2216 // FIXME: For now, support only the first (SIB) form.
2217 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2219 if (op2 == 0x04)
2221 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2222 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2223 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2224 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2225 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2227 else
2229 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2230 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2231 if (rel.get_r_offset() + 9 < view_size
2232 && view[9] == 0x90)
2234 // FIXME: This is not the right instruction sequence.
2235 // There is a trailing nop. Use the size byte subl.
2236 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2237 roff = 6;
2239 else
2241 // FIXME: This is not the right instruction sequence.
2242 // Use the five byte subl.
2243 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2247 Relocate_functions<32, false>::rel32(view + roff, value);
2249 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2250 // We can skip it.
2251 this->skip_call_tls_get_addr_ = true;
2254 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2255 // General-Dynamic to a Local-Exec.
2257 inline void
2258 Target_i386::Relocate::tls_desc_gd_to_le(
2259 const Relocate_info<32, false>* relinfo,
2260 size_t relnum,
2261 Output_segment* tls_segment,
2262 const elfcpp::Rel<32, false>& rel,
2263 unsigned int r_type,
2264 elfcpp::Elf_types<32>::Elf_Addr value,
2265 unsigned char* view,
2266 section_size_type view_size)
2268 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2270 // leal foo@TLSDESC(%ebx), %eax
2271 // ==> leal foo@NTPOFF, %eax
2272 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2273 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2274 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2275 view[-2] == 0x8d && view[-1] == 0x83);
2276 view[-1] = 0x05;
2277 value -= tls_segment->memsz();
2278 Relocate_functions<32, false>::rel32(view, value);
2280 else
2282 // call *foo@TLSCALL(%eax)
2283 // ==> nop; nop
2284 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2285 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2286 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2287 view[0] == 0xff && view[1] == 0x10);
2288 view[0] = 0x66;
2289 view[1] = 0x90;
2293 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2294 // General-Dynamic to an Initial-Exec.
2296 inline void
2297 Target_i386::Relocate::tls_desc_gd_to_ie(
2298 const Relocate_info<32, false>* relinfo,
2299 size_t relnum,
2300 Output_segment*,
2301 const elfcpp::Rel<32, false>& rel,
2302 unsigned int r_type,
2303 elfcpp::Elf_types<32>::Elf_Addr value,
2304 unsigned char* view,
2305 section_size_type view_size)
2307 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2309 // leal foo@TLSDESC(%ebx), %eax
2310 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2311 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2312 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2313 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2314 view[-2] == 0x8d && view[-1] == 0x83);
2315 view[-2] = 0x8b;
2316 Relocate_functions<32, false>::rel32(view, value);
2318 else
2320 // call *foo@TLSCALL(%eax)
2321 // ==> nop; nop
2322 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2323 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2324 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2325 view[0] == 0xff && view[1] == 0x10);
2326 view[0] = 0x66;
2327 view[1] = 0x90;
2331 // Do a relocation in which we convert a TLS Local-Dynamic to a
2332 // Local-Exec.
2334 inline void
2335 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2336 size_t relnum,
2337 Output_segment*,
2338 const elfcpp::Rel<32, false>& rel,
2339 unsigned int,
2340 elfcpp::Elf_types<32>::Elf_Addr,
2341 unsigned char* view,
2342 section_size_type view_size)
2344 // leal foo(%reg), %eax; call ___tls_get_addr
2345 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2347 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2348 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2350 // FIXME: Does this test really always pass?
2351 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2352 view[-2] == 0x8d && view[-1] == 0x83);
2354 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2356 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2358 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2359 // We can skip it.
2360 this->skip_call_tls_get_addr_ = true;
2363 // Do a relocation in which we convert a TLS Initial-Exec to a
2364 // Local-Exec.
2366 inline void
2367 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2368 size_t relnum,
2369 Output_segment* tls_segment,
2370 const elfcpp::Rel<32, false>& rel,
2371 unsigned int r_type,
2372 elfcpp::Elf_types<32>::Elf_Addr value,
2373 unsigned char* view,
2374 section_size_type view_size)
2376 // We have to actually change the instructions, which means that we
2377 // need to examine the opcodes to figure out which instruction we
2378 // are looking at.
2379 if (r_type == elfcpp::R_386_TLS_IE)
2381 // movl %gs:XX,%eax ==> movl $YY,%eax
2382 // movl %gs:XX,%reg ==> movl $YY,%reg
2383 // addl %gs:XX,%reg ==> addl $YY,%reg
2384 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2385 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2387 unsigned char op1 = view[-1];
2388 if (op1 == 0xa1)
2390 // movl XX,%eax ==> movl $YY,%eax
2391 view[-1] = 0xb8;
2393 else
2395 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2397 unsigned char op2 = view[-2];
2398 if (op2 == 0x8b)
2400 // movl XX,%reg ==> movl $YY,%reg
2401 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2402 (op1 & 0xc7) == 0x05);
2403 view[-2] = 0xc7;
2404 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2406 else if (op2 == 0x03)
2408 // addl XX,%reg ==> addl $YY,%reg
2409 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2410 (op1 & 0xc7) == 0x05);
2411 view[-2] = 0x81;
2412 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2414 else
2415 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2418 else
2420 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2421 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2422 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2423 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2424 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2426 unsigned char op1 = view[-1];
2427 unsigned char op2 = view[-2];
2428 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2429 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2430 if (op2 == 0x8b)
2432 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2433 view[-2] = 0xc7;
2434 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2436 else if (op2 == 0x2b)
2438 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2439 view[-2] = 0x81;
2440 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2442 else if (op2 == 0x03)
2444 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2445 view[-2] = 0x81;
2446 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2448 else
2449 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2452 value = tls_segment->memsz() - value;
2453 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2454 value = - value;
2456 Relocate_functions<32, false>::rel32(view, value);
2459 // Relocate section data.
2461 void
2462 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2463 unsigned int sh_type,
2464 const unsigned char* prelocs,
2465 size_t reloc_count,
2466 Output_section* output_section,
2467 bool needs_special_offset_handling,
2468 unsigned char* view,
2469 elfcpp::Elf_types<32>::Elf_Addr address,
2470 section_size_type view_size,
2471 const Reloc_symbol_changes* reloc_symbol_changes)
2473 gold_assert(sh_type == elfcpp::SHT_REL);
2475 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2476 Target_i386::Relocate>(
2477 relinfo,
2478 this,
2479 prelocs,
2480 reloc_count,
2481 output_section,
2482 needs_special_offset_handling,
2483 view,
2484 address,
2485 view_size,
2486 reloc_symbol_changes);
2489 // Return the size of a relocation while scanning during a relocatable
2490 // link.
2492 unsigned int
2493 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2494 unsigned int r_type,
2495 Relobj* object)
2497 switch (r_type)
2499 case elfcpp::R_386_NONE:
2500 case elfcpp::R_386_GNU_VTINHERIT:
2501 case elfcpp::R_386_GNU_VTENTRY:
2502 case elfcpp::R_386_TLS_GD: // Global-dynamic
2503 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2504 case elfcpp::R_386_TLS_DESC_CALL:
2505 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2506 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2507 case elfcpp::R_386_TLS_IE: // Initial-exec
2508 case elfcpp::R_386_TLS_IE_32:
2509 case elfcpp::R_386_TLS_GOTIE:
2510 case elfcpp::R_386_TLS_LE: // Local-exec
2511 case elfcpp::R_386_TLS_LE_32:
2512 return 0;
2514 case elfcpp::R_386_32:
2515 case elfcpp::R_386_PC32:
2516 case elfcpp::R_386_GOT32:
2517 case elfcpp::R_386_PLT32:
2518 case elfcpp::R_386_GOTOFF:
2519 case elfcpp::R_386_GOTPC:
2520 return 4;
2522 case elfcpp::R_386_16:
2523 case elfcpp::R_386_PC16:
2524 return 2;
2526 case elfcpp::R_386_8:
2527 case elfcpp::R_386_PC8:
2528 return 1;
2530 // These are relocations which should only be seen by the
2531 // dynamic linker, and should never be seen here.
2532 case elfcpp::R_386_COPY:
2533 case elfcpp::R_386_GLOB_DAT:
2534 case elfcpp::R_386_JUMP_SLOT:
2535 case elfcpp::R_386_RELATIVE:
2536 case elfcpp::R_386_TLS_TPOFF:
2537 case elfcpp::R_386_TLS_DTPMOD32:
2538 case elfcpp::R_386_TLS_DTPOFF32:
2539 case elfcpp::R_386_TLS_TPOFF32:
2540 case elfcpp::R_386_TLS_DESC:
2541 object->error(_("unexpected reloc %u in object file"), r_type);
2542 return 0;
2544 case elfcpp::R_386_32PLT:
2545 case elfcpp::R_386_TLS_GD_32:
2546 case elfcpp::R_386_TLS_GD_PUSH:
2547 case elfcpp::R_386_TLS_GD_CALL:
2548 case elfcpp::R_386_TLS_GD_POP:
2549 case elfcpp::R_386_TLS_LDM_32:
2550 case elfcpp::R_386_TLS_LDM_PUSH:
2551 case elfcpp::R_386_TLS_LDM_CALL:
2552 case elfcpp::R_386_TLS_LDM_POP:
2553 case elfcpp::R_386_USED_BY_INTEL_200:
2554 default:
2555 object->error(_("unsupported reloc %u in object file"), r_type);
2556 return 0;
2560 // Scan the relocs during a relocatable link.
2562 void
2563 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
2564 Layout* layout,
2565 Sized_relobj<32, false>* object,
2566 unsigned int data_shndx,
2567 unsigned int sh_type,
2568 const unsigned char* prelocs,
2569 size_t reloc_count,
2570 Output_section* output_section,
2571 bool needs_special_offset_handling,
2572 size_t local_symbol_count,
2573 const unsigned char* plocal_symbols,
2574 Relocatable_relocs* rr)
2576 gold_assert(sh_type == elfcpp::SHT_REL);
2578 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2579 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2581 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2582 Scan_relocatable_relocs>(
2583 symtab,
2584 layout,
2585 object,
2586 data_shndx,
2587 prelocs,
2588 reloc_count,
2589 output_section,
2590 needs_special_offset_handling,
2591 local_symbol_count,
2592 plocal_symbols,
2593 rr);
2596 // Relocate a section during a relocatable link.
2598 void
2599 Target_i386::relocate_for_relocatable(
2600 const Relocate_info<32, false>* relinfo,
2601 unsigned int sh_type,
2602 const unsigned char* prelocs,
2603 size_t reloc_count,
2604 Output_section* output_section,
2605 off_t offset_in_output_section,
2606 const Relocatable_relocs* rr,
2607 unsigned char* view,
2608 elfcpp::Elf_types<32>::Elf_Addr view_address,
2609 section_size_type view_size,
2610 unsigned char* reloc_view,
2611 section_size_type reloc_view_size)
2613 gold_assert(sh_type == elfcpp::SHT_REL);
2615 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2616 relinfo,
2617 prelocs,
2618 reloc_count,
2619 output_section,
2620 offset_in_output_section,
2622 view,
2623 view_address,
2624 view_size,
2625 reloc_view,
2626 reloc_view_size);
2629 // Return the value to use for a dynamic which requires special
2630 // treatment. This is how we support equality comparisons of function
2631 // pointers across shared library boundaries, as described in the
2632 // processor specific ABI supplement.
2634 uint64_t
2635 Target_i386::do_dynsym_value(const Symbol* gsym) const
2637 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2638 return this->plt_section()->address() + gsym->plt_offset();
2641 // Return a string used to fill a code section with nops to take up
2642 // the specified length.
2644 std::string
2645 Target_i386::do_code_fill(section_size_type length) const
2647 if (length >= 16)
2649 // Build a jmp instruction to skip over the bytes.
2650 unsigned char jmp[5];
2651 jmp[0] = 0xe9;
2652 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2653 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2654 + std::string(length - 5, '\0'));
2657 // Nop sequences of various lengths.
2658 const char nop1[1] = { 0x90 }; // nop
2659 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2660 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2661 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2662 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2663 0x00 }; // leal 0(%esi,1),%esi
2664 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2665 0x00, 0x00 };
2666 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2667 0x00, 0x00, 0x00 };
2668 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2669 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2670 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2671 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2672 0x00 };
2673 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2674 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2675 0x00, 0x00 };
2676 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2677 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2678 0x00, 0x00, 0x00 };
2679 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2680 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2681 0x00, 0x00, 0x00, 0x00 };
2682 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2683 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2684 0x27, 0x00, 0x00, 0x00,
2685 0x00 };
2686 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2687 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2688 0xbc, 0x27, 0x00, 0x00,
2689 0x00, 0x00 };
2690 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2691 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2692 0x90, 0x90, 0x90, 0x90,
2693 0x90, 0x90, 0x90 };
2695 const char* nops[16] = {
2696 NULL,
2697 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2698 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2701 return std::string(nops[length], length);
2704 // FNOFFSET in section SHNDX in OBJECT is the start of a function
2705 // compiled with -fstack-split. The function calls non-stack-split
2706 // code. We have to change the function so that it always ensures
2707 // that it has enough stack space to run some random function.
2709 void
2710 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2711 section_offset_type fnoffset,
2712 section_size_type fnsize,
2713 unsigned char* view,
2714 section_size_type view_size,
2715 std::string* from,
2716 std::string* to) const
2718 // The function starts with a comparison of the stack pointer and a
2719 // field in the TCB. This is followed by a jump.
2721 // cmp %gs:NN,%esp
2722 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2723 && fnsize > 7)
2725 // We will call __morestack if the carry flag is set after this
2726 // comparison. We turn the comparison into an stc instruction
2727 // and some nops.
2728 view[fnoffset] = '\xf9';
2729 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2731 // lea NN(%esp),%ecx
2732 // lea NN(%esp),%edx
2733 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2734 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
2735 && fnsize > 7)
2737 // This is loading an offset from the stack pointer for a
2738 // comparison. The offset is negative, so we decrease the
2739 // offset by the amount of space we need for the stack. This
2740 // means we will avoid calling __morestack if there happens to
2741 // be plenty of space on the stack already.
2742 unsigned char* pval = view + fnoffset + 3;
2743 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2744 val -= parameters->options().split_stack_adjust_size();
2745 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2747 else
2749 if (!object->has_no_split_stack())
2750 object->error(_("failed to match split-stack sequence at "
2751 "section %u offset %0zx"),
2752 shndx, static_cast<size_t>(fnoffset));
2753 return;
2756 // We have to change the function so that it calls
2757 // __morestack_non_split instead of __morestack. The former will
2758 // allocate additional stack space.
2759 *from = "__morestack";
2760 *to = "__morestack_non_split";
2763 // The selector for i386 object files.
2765 class Target_selector_i386 : public Target_selector_freebsd
2767 public:
2768 Target_selector_i386()
2769 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2770 "elf32-i386", "elf32-i386-freebsd")
2773 Target*
2774 do_instantiate_target()
2775 { return new Target_i386(); }
2778 Target_selector_i386 target_selector_i386;
2780 } // End anonymous namespace.