2007-06-14 Paolo Bonzini <bonzini@gnu.org>
[binutils.git] / gold / i386.cc
blob448453aa349505c7234a2d4c4821679159fa0ee9
1 // i386.cc -- i386 target support for gold.
3 #include "gold.h"
5 #include <cstring>
7 #include "elfcpp.h"
8 #include "reloc.h"
9 #include "i386.h"
10 #include "object.h"
11 #include "symtab.h"
12 #include "layout.h"
13 #include "output.h"
14 #include "target.h"
15 #include "target-reloc.h"
16 #include "target-select.h"
18 namespace
21 using namespace gold;
23 class Output_data_plt_i386;
25 // The i386 target class.
27 class Target_i386 : public Sized_target<32, false>
29 public:
30 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
32 Target_i386()
33 : Sized_target<32, false>(&i386_info),
34 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
35 copy_relocs_(NULL), dynbss_(NULL)
36 { }
38 // Scan the relocations to look for symbol adjustments.
39 void
40 scan_relocs(const General_options& options,
41 Symbol_table* symtab,
42 Layout* layout,
43 Sized_relobj<32, false>* object,
44 unsigned int data_shndx,
45 unsigned int sh_type,
46 const unsigned char* prelocs,
47 size_t reloc_count,
48 size_t local_symbol_count,
49 const unsigned char* plocal_symbols,
50 Symbol** global_symbols);
52 // Finalize the sections.
53 void
54 do_finalize_sections(const General_options*, Layout*);
56 // Relocate a section.
57 void
58 relocate_section(const Relocate_info<32, false>*,
59 unsigned int sh_type,
60 const unsigned char* prelocs,
61 size_t reloc_count,
62 unsigned char* view,
63 elfcpp::Elf_types<32>::Elf_Addr view_address,
64 off_t view_size);
66 private:
67 // The class which scans relocations.
68 struct Scan
70 inline void
71 local(const General_options& options, Symbol_table* symtab,
72 Layout* layout, Target_i386* target,
73 Sized_relobj<32, false>* object,
74 unsigned int data_shndx,
75 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
76 const elfcpp::Sym<32, false>& lsym);
78 inline void
79 global(const General_options& options, Symbol_table* symtab,
80 Layout* layout, Target_i386* target,
81 Sized_relobj<32, false>* object,
82 unsigned int data_shndx,
83 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
84 Symbol* gsym);
87 // The class which implements relocation.
88 class Relocate
90 public:
91 Relocate()
92 : skip_call_tls_get_addr_(false)
93 { }
95 ~Relocate()
97 if (this->skip_call_tls_get_addr_)
99 // FIXME: This needs to specify the location somehow.
100 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
101 program_name);
102 gold_exit(false);
106 // Do a relocation. Return false if the caller should not issue
107 // any warnings about this relocation.
108 inline bool
109 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
110 const elfcpp::Rel<32, false>&,
111 unsigned int r_type, const Sized_symbol<32>*,
112 const Symbol_value<32>*,
113 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
114 off_t);
116 private:
117 // Do a TLS relocation.
118 inline void
119 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
120 const elfcpp::Rel<32, false>&,
121 unsigned int r_type, const Sized_symbol<32>*,
122 const Symbol_value<32>*,
123 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
125 // Do a TLS Initial-Exec to Local-Exec transition.
126 static inline void
127 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
128 Output_segment* tls_segment,
129 const elfcpp::Rel<32, false>&, unsigned int r_type,
130 elfcpp::Elf_types<32>::Elf_Addr value,
131 unsigned char* view,
132 off_t view_size);
134 // Do a TLS Global-Dynamic to Local-Exec transition.
135 inline void
136 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
137 Output_segment* tls_segment,
138 const elfcpp::Rel<32, false>&, unsigned int r_type,
139 elfcpp::Elf_types<32>::Elf_Addr value,
140 unsigned char* view,
141 off_t view_size);
143 // Check the range for a TLS relocation.
144 static inline void
145 check_range(const Relocate_info<32, false>*, size_t relnum,
146 const elfcpp::Rel<32, false>&, off_t, off_t);
148 // Check the validity of a TLS relocation. This is like assert.
149 static inline void
150 check_tls(const Relocate_info<32, false>*, size_t relnum,
151 const elfcpp::Rel<32, false>&, bool);
153 // This is set if we should skip the next reloc, which should be a
154 // PLT32 reloc against ___tls_get_addr.
155 bool skip_call_tls_get_addr_;
158 // Adjust TLS relocation type based on the options and whether this
159 // is a local symbol.
160 static unsigned int
161 optimize_tls_reloc(const General_options*, bool is_final, int r_type);
163 // Get the GOT section, creating it if necessary.
164 Output_data_got<32, false>*
165 got_section(const General_options*, Symbol_table*, Layout*);
167 // Create a PLT entry for a global symbol.
168 void
169 make_plt_entry(const General_options* options, Symbol_table*,
170 Layout*, Symbol*);
172 // Get the PLT section.
173 Output_data_plt_i386*
174 plt_section() const
176 gold_assert(this->plt_ != NULL);
177 return this->plt_;
180 // Get the dynamic reloc section, creating it if necessary.
181 Reloc_section*
182 rel_dyn_section(Layout*);
184 // Copy a relocation against a global symbol.
185 void
186 copy_reloc(const General_options*, Symbol_table*, Layout*,
187 Sized_relobj<32, false>*, unsigned int,
188 Symbol*, const elfcpp::Rel<32, false>&);
190 // Information about this specific target which we pass to the
191 // general Target structure.
192 static const Target::Target_info i386_info;
194 // The GOT section.
195 Output_data_got<32, false>* got_;
196 // The PLT section.
197 Output_data_plt_i386* plt_;
198 // The GOT PLT section.
199 Output_data_space* got_plt_;
200 // The dynamic reloc section.
201 Reloc_section* rel_dyn_;
202 // Relocs saved to avoid a COPY reloc.
203 Copy_relocs<32, false>* copy_relocs_;
204 // Space for variables copied with a COPY reloc.
205 Output_data_space* dynbss_;
208 const Target::Target_info Target_i386::i386_info =
210 32, // size
211 false, // is_big_endian
212 elfcpp::EM_386, // machine_code
213 false, // has_make_symbol
214 false, // has_resolve
215 "/usr/lib/libc.so.1", // dynamic_linker
216 0x08048000, // text_segment_address
217 0x1000, // abi_pagesize
218 0x1000 // common_pagesize
221 // Get the GOT section, creating it if necessary.
223 Output_data_got<32, false>*
224 Target_i386::got_section(const General_options* options, Symbol_table* symtab,
225 Layout* layout)
227 if (this->got_ == NULL)
229 gold_assert(options != NULL && symtab != NULL && layout != NULL);
231 this->got_ = new Output_data_got<32, false>(options);
233 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
234 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
235 this->got_);
237 // The old GNU linker creates a .got.plt section. We just
238 // create another set of data in the .got section. Note that we
239 // always create a PLT if we create a GOT, although the PLT
240 // might be empty.
241 this->got_plt_ = new Output_data_space(4);
242 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
243 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
244 this->got_plt_);
246 // The first three entries are reserved.
247 this->got_plt_->set_space_size(3 * 4);
249 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
250 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
251 this->got_plt_,
252 0, 0, elfcpp::STT_OBJECT,
253 elfcpp::STB_LOCAL,
254 elfcpp::STV_HIDDEN, 0,
255 false, false);
258 return this->got_;
261 // Get the dynamic reloc section, creating it if necessary.
263 Target_i386::Reloc_section*
264 Target_i386::rel_dyn_section(Layout* layout)
266 if (this->rel_dyn_ == NULL)
268 gold_assert(layout != NULL);
269 this->rel_dyn_ = new Reloc_section();
270 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
271 elfcpp::SHF_ALLOC, this->rel_dyn_);
273 return this->rel_dyn_;
276 // A class to handle the PLT data.
278 class Output_data_plt_i386 : public Output_section_data
280 public:
281 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
283 Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared);
285 // Add an entry to the PLT.
286 void
287 add_entry(Symbol* gsym);
289 // Return the .rel.plt section data.
290 const Reloc_section*
291 rel_plt() const
292 { return this->rel_; }
294 protected:
295 void
296 do_adjust_output_section(Output_section* os);
298 private:
299 // The size of an entry in the PLT.
300 static const int plt_entry_size = 16;
302 // The first entry in the PLT for an executable.
303 static unsigned char exec_first_plt_entry[plt_entry_size];
305 // The first entry in the PLT for a shared object.
306 static unsigned char dyn_first_plt_entry[plt_entry_size];
308 // Other entries in the PLT for an executable.
309 static unsigned char exec_plt_entry[plt_entry_size];
311 // Other entries in the PLT for a shared object.
312 static unsigned char dyn_plt_entry[plt_entry_size];
314 // Set the final size.
315 void
316 do_set_address(uint64_t, off_t)
317 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
319 // Write out the PLT data.
320 void
321 do_write(Output_file*);
323 // The reloc section.
324 Reloc_section* rel_;
325 // The .got.plt section.
326 Output_data_space* got_plt_;
327 // The number of PLT entries.
328 unsigned int count_;
329 // Whether we are generated a shared object.
330 bool is_shared_;
333 // Create the PLT section. The ordinary .got section is an argument,
334 // since we need to refer to the start. We also create our own .got
335 // section just for PLT entries.
337 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
338 Output_data_space* got_plt,
339 bool is_shared)
340 : Output_section_data(4), got_plt_(got_plt), is_shared_(is_shared)
342 this->rel_ = new Reloc_section();
343 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
344 elfcpp::SHF_ALLOC, this->rel_);
347 // For some reason
349 void
350 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
352 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
353 // linker, and so do we.
354 os->set_entsize(4);
357 // Add an entry to the PLT.
359 void
360 Output_data_plt_i386::add_entry(Symbol* gsym)
362 gold_assert(!gsym->has_plt_offset());
364 // Note that when setting the PLT offset we skip the initial
365 // reserved PLT entry.
366 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
368 ++this->count_;
370 off_t got_offset = this->got_plt_->data_size();
372 // Every PLT entry needs a GOT entry which points back to the PLT
373 // entry (this will be changed by the dynamic linker, normally
374 // lazily when the function is called).
375 this->got_plt_->set_space_size(got_offset + 4);
377 // Every PLT entry needs a reloc.
378 gsym->set_needs_dynsym_entry();
379 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
380 got_offset);
382 // Note that we don't need to save the symbol. The contents of the
383 // PLT are independent of which symbols are used. The symbols only
384 // appear in the relocations.
387 // The first entry in the PLT for an executable.
389 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
391 0xff, 0x35, // pushl contents of memory address
392 0, 0, 0, 0, // replaced with address of .got + 4
393 0xff, 0x25, // jmp indirect
394 0, 0, 0, 0, // replaced with address of .got + 8
395 0, 0, 0, 0 // unused
398 // The first entry in the PLT for a shared object.
400 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
402 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
403 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
404 0, 0, 0, 0 // unused
407 // Subsequent entries in the PLT for an executable.
409 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
411 0xff, 0x25, // jmp indirect
412 0, 0, 0, 0, // replaced with address of symbol in .got
413 0x68, // pushl immediate
414 0, 0, 0, 0, // replaced with offset into relocation table
415 0xe9, // jmp relative
416 0, 0, 0, 0 // replaced with offset to start of .plt
419 // Subsequent entries in the PLT for a shared object.
421 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
423 0xff, 0xa3, // jmp *offset(%ebx)
424 0, 0, 0, 0, // replaced with offset of symbol in .got
425 0x68, // pushl immediate
426 0, 0, 0, 0, // replaced with offset into relocation table
427 0xe9, // jmp relative
428 0, 0, 0, 0 // replaced with offset to start of .plt
431 // Write out the PLT. This uses the hand-coded instructions above,
432 // and adjusts them as needed. This is all specified by the i386 ELF
433 // Processor Supplement.
435 void
436 Output_data_plt_i386::do_write(Output_file* of)
438 const off_t offset = this->offset();
439 const off_t oview_size = this->data_size();
440 unsigned char* const oview = of->get_output_view(offset, oview_size);
442 const off_t got_file_offset = this->got_plt_->offset();
443 const off_t got_size = this->got_plt_->data_size();
444 unsigned char* const got_view = of->get_output_view(got_file_offset,
445 got_size);
447 unsigned char* pov = oview;
449 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
450 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
452 if (this->is_shared_)
453 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
454 else
456 memcpy(pov, exec_first_plt_entry, plt_entry_size);
457 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
458 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
460 pov += plt_entry_size;
462 unsigned char* got_pov = got_view;
464 memset(got_pov, 0, 12);
465 got_pov += 12;
467 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
469 unsigned int plt_offset = plt_entry_size;
470 unsigned int plt_rel_offset = 0;
471 unsigned int got_offset = 12;
472 const unsigned int count = this->count_;
473 for (unsigned int i = 0;
474 i < count;
475 ++i,
476 pov += plt_entry_size,
477 got_pov += 4,
478 plt_offset += plt_entry_size,
479 plt_rel_offset += rel_size,
480 got_offset += 4)
482 // Set and adjust the PLT entry itself.
484 if (this->is_shared_)
486 memcpy(pov, dyn_plt_entry, plt_entry_size);
487 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
489 else
491 memcpy(pov, exec_plt_entry, plt_entry_size);
492 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
493 (got_address
494 + got_offset));
497 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
498 elfcpp::Swap<32, false>::writeval(pov + 12,
499 - (plt_offset + plt_entry_size));
501 // Set the entry in the GOT.
502 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
505 gold_assert(pov - oview == oview_size);
506 gold_assert(got_pov - got_view == got_size);
508 of->write_output_view(offset, oview_size, oview);
509 of->write_output_view(got_file_offset, got_size, got_view);
512 // Create a PLT entry for a global symbol.
514 void
515 Target_i386::make_plt_entry(const General_options* options,
516 Symbol_table* symtab, Layout* layout, Symbol* gsym)
518 if (gsym->has_plt_offset())
519 return;
521 if (this->plt_ == NULL)
523 // Create the GOT sections first.
524 this->got_section(options, symtab, layout);
526 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
527 options->is_shared());
528 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
529 (elfcpp::SHF_ALLOC
530 | elfcpp::SHF_EXECINSTR),
531 this->plt_);
534 this->plt_->add_entry(gsym);
537 // Handle a relocation against a non-function symbol defined in a
538 // dynamic object. The traditional way to handle this is to generate
539 // a COPY relocation to copy the variable at runtime from the shared
540 // object into the executable's data segment. However, this is
541 // undesirable in general, as if the size of the object changes in the
542 // dynamic object, the executable will no longer work correctly. If
543 // this relocation is in a writable section, then we can create a
544 // dynamic reloc and the dynamic linker will resolve it to the correct
545 // address at runtime. However, we do not want do that if the
546 // relocation is in a read-only section, as it would prevent the
547 // readonly segment from being shared. And if we have to eventually
548 // generate a COPY reloc, then any dynamic relocations will be
549 // useless. So this means that if this is a writable section, we need
550 // to save the relocation until we see whether we have to create a
551 // COPY relocation for this symbol for any other relocation.
553 void
554 Target_i386::copy_reloc(const General_options* options,
555 Symbol_table* symtab,
556 Layout* layout,
557 Sized_relobj<32, false>* object,
558 unsigned int data_shndx, Symbol* gsym,
559 const elfcpp::Rel<32, false>& rel)
561 Sized_symbol<32>* ssym;
562 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
563 SELECT_SIZE(32));
565 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
566 data_shndx, ssym))
568 // So far we do not need a COPY reloc. Save this relocation.
569 // If it turns out that we never need a COPY reloc for this
570 // symbol, then we will emit the relocation.
571 if (this->copy_relocs_ == NULL)
572 this->copy_relocs_ = new Copy_relocs<32, false>();
573 this->copy_relocs_->save(ssym, object, data_shndx, rel);
575 else
577 // Allocate space for this symbol in the .bss section.
579 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
581 // There is no defined way to determine the required alignment
582 // of the symbol. We pick the alignment based on the size. We
583 // set an arbitrary maximum of 256.
584 unsigned int align;
585 for (align = 1; align < 512; align <<= 1)
586 if ((symsize & align) != 0)
587 break;
589 if (this->dynbss_ == NULL)
591 this->dynbss_ = new Output_data_space(align);
592 layout->add_output_section_data(".bss",
593 elfcpp::SHT_NOBITS,
594 (elfcpp::SHF_ALLOC
595 | elfcpp::SHF_WRITE),
596 this->dynbss_);
599 Output_data_space* dynbss = this->dynbss_;
601 if (align > dynbss->addralign())
602 dynbss->set_space_alignment(align);
604 off_t dynbss_size = dynbss->data_size();
605 dynbss_size = align_address(dynbss_size, align);
606 off_t offset = dynbss_size;
607 dynbss->set_space_size(dynbss_size + symsize);
609 // Define the symbol in the .dynbss section.
610 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
611 dynbss, offset, symsize, ssym->type(),
612 ssym->binding(), ssym->visibility(),
613 ssym->nonvis(), false, false);
615 // Add the COPY reloc.
616 ssym->set_needs_dynsym_entry();
617 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
618 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
622 // Optimize the TLS relocation type based on what we know about the
623 // symbol. IS_FINAL is true if the final address of this symbol is
624 // known at link time.
626 unsigned int
627 Target_i386::optimize_tls_reloc(const General_options* options,
628 bool is_final,
629 int r_type)
631 // If we are generating a shared library, then we can't do anything
632 // in the linker.
633 if (options->is_shared())
634 return r_type;
636 switch (r_type)
638 case elfcpp::R_386_TLS_GD:
639 case elfcpp::R_386_TLS_GOTDESC:
640 case elfcpp::R_386_TLS_DESC_CALL:
641 // These are Global-Dynamic which permits fully general TLS
642 // access. Since we know that we are generating an executable,
643 // we can convert this to Initial-Exec. If we also know that
644 // this is a local symbol, we can further switch to Local-Exec.
645 if (is_final)
646 return elfcpp::R_386_TLS_LE_32;
647 return elfcpp::R_386_TLS_IE_32;
649 case elfcpp::R_386_TLS_LDM:
650 // This is Local-Dynamic, which refers to a local symbol in the
651 // dynamic TLS block. Since we know that we generating an
652 // executable, we can switch to Local-Exec.
653 return elfcpp::R_386_TLS_LE_32;
655 case elfcpp::R_386_TLS_LDO_32:
656 // Another type of Local-Dynamic relocation.
657 return elfcpp::R_386_TLS_LE;
659 case elfcpp::R_386_TLS_IE:
660 case elfcpp::R_386_TLS_GOTIE:
661 case elfcpp::R_386_TLS_IE_32:
662 // These are Initial-Exec relocs which get the thread offset
663 // from the GOT. If we know that we are linking against the
664 // local symbol, we can switch to Local-Exec, which links the
665 // thread offset into the instruction.
666 if (is_final)
667 return elfcpp::R_386_TLS_LE_32;
668 return r_type;
670 case elfcpp::R_386_TLS_LE:
671 case elfcpp::R_386_TLS_LE_32:
672 // When we already have Local-Exec, there is nothing further we
673 // can do.
674 return r_type;
676 default:
677 gold_unreachable();
681 // Scan a relocation for a local symbol.
683 inline void
684 Target_i386::Scan::local(const General_options& options,
685 Symbol_table* symtab,
686 Layout* layout,
687 Target_i386* target,
688 Sized_relobj<32, false>* object,
689 unsigned int,
690 const elfcpp::Rel<32, false>&,
691 unsigned int r_type,
692 const elfcpp::Sym<32, false>&)
694 switch (r_type)
696 case elfcpp::R_386_NONE:
697 case elfcpp::R_386_GNU_VTINHERIT:
698 case elfcpp::R_386_GNU_VTENTRY:
699 break;
701 case elfcpp::R_386_32:
702 case elfcpp::R_386_16:
703 case elfcpp::R_386_8:
704 // FIXME: If we are generating a shared object we need to copy
705 // this relocation into the object.
706 gold_assert(!options.is_shared());
707 break;
709 case elfcpp::R_386_PC32:
710 case elfcpp::R_386_PC16:
711 case elfcpp::R_386_PC8:
712 break;
714 case elfcpp::R_386_GOTOFF:
715 case elfcpp::R_386_GOTPC:
716 // We need a GOT section.
717 target->got_section(&options, symtab, layout);
718 break;
720 case elfcpp::R_386_COPY:
721 case elfcpp::R_386_GLOB_DAT:
722 case elfcpp::R_386_JUMP_SLOT:
723 case elfcpp::R_386_RELATIVE:
724 case elfcpp::R_386_TLS_TPOFF:
725 case elfcpp::R_386_TLS_DTPMOD32:
726 case elfcpp::R_386_TLS_DTPOFF32:
727 case elfcpp::R_386_TLS_TPOFF32:
728 case elfcpp::R_386_TLS_DESC:
729 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
730 program_name, object->name().c_str(), r_type);
731 gold_exit(false);
732 break;
734 case elfcpp::R_386_TLS_IE:
735 case elfcpp::R_386_TLS_GOTIE:
736 case elfcpp::R_386_TLS_LE:
737 case elfcpp::R_386_TLS_GD:
738 case elfcpp::R_386_TLS_LDM:
739 case elfcpp::R_386_TLS_LDO_32:
740 case elfcpp::R_386_TLS_IE_32:
741 case elfcpp::R_386_TLS_LE_32:
742 case elfcpp::R_386_TLS_GOTDESC:
743 case elfcpp::R_386_TLS_DESC_CALL:
744 r_type = Target_i386::optimize_tls_reloc(&options,
745 !options.is_shared(),
746 r_type);
747 switch (r_type)
749 case elfcpp::R_386_TLS_LE:
750 case elfcpp::R_386_TLS_LE_32:
751 // FIXME: If generating a shared object, we need to copy
752 // this relocation into the object.
753 gold_assert(!options.is_shared());
754 break;
756 case elfcpp::R_386_TLS_IE:
757 case elfcpp::R_386_TLS_GOTIE:
758 case elfcpp::R_386_TLS_GD:
759 case elfcpp::R_386_TLS_LDM:
760 case elfcpp::R_386_TLS_LDO_32:
761 case elfcpp::R_386_TLS_IE_32:
762 case elfcpp::R_386_TLS_GOTDESC:
763 case elfcpp::R_386_TLS_DESC_CALL:
764 fprintf(stderr,
765 _("%s: %s: unsupported reloc %u against local symbol\n"),
766 program_name, object->name().c_str(), r_type);
767 break;
769 break;
771 case elfcpp::R_386_GOT32:
772 case elfcpp::R_386_PLT32:
773 case elfcpp::R_386_32PLT:
774 case elfcpp::R_386_TLS_GD_32:
775 case elfcpp::R_386_TLS_GD_PUSH:
776 case elfcpp::R_386_TLS_GD_CALL:
777 case elfcpp::R_386_TLS_GD_POP:
778 case elfcpp::R_386_TLS_LDM_32:
779 case elfcpp::R_386_TLS_LDM_PUSH:
780 case elfcpp::R_386_TLS_LDM_CALL:
781 case elfcpp::R_386_TLS_LDM_POP:
782 case elfcpp::R_386_USED_BY_INTEL_200:
783 default:
784 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
785 program_name, object->name().c_str(), r_type);
786 break;
790 // Scan a relocation for a global symbol.
792 inline void
793 Target_i386::Scan::global(const General_options& options,
794 Symbol_table* symtab,
795 Layout* layout,
796 Target_i386* target,
797 Sized_relobj<32, false>* object,
798 unsigned int data_shndx,
799 const elfcpp::Rel<32, false>& reloc,
800 unsigned int r_type,
801 Symbol* gsym)
803 switch (r_type)
805 case elfcpp::R_386_NONE:
806 case elfcpp::R_386_GNU_VTINHERIT:
807 case elfcpp::R_386_GNU_VTENTRY:
808 break;
810 case elfcpp::R_386_32:
811 case elfcpp::R_386_PC32:
812 case elfcpp::R_386_16:
813 case elfcpp::R_386_PC16:
814 case elfcpp::R_386_8:
815 case elfcpp::R_386_PC8:
816 // FIXME: If we are generating a shared object we may need to
817 // copy this relocation into the object. If this symbol is
818 // defined in a shared object, we may need to copy this
819 // relocation in order to avoid a COPY relocation.
820 gold_assert(!options.is_shared());
822 if (gsym->is_from_dynobj())
824 // This symbol is defined in a dynamic object. If it is a
825 // function, we make a PLT entry. Otherwise we need to
826 // either generate a COPY reloc or copy this reloc.
827 if (gsym->type() == elfcpp::STT_FUNC)
828 target->make_plt_entry(&options, symtab, layout, gsym);
829 else
830 target->copy_reloc(&options, symtab, layout, object, data_shndx,
831 gsym, reloc);
834 break;
836 case elfcpp::R_386_GOT32:
837 // The symbol requires a GOT entry.
838 if (target->got_section(&options, symtab, layout)->add_global(gsym))
840 // If this symbol is not fully resolved, we need to add a
841 // dynamic relocation for it.
842 if (!gsym->final_value_is_known(&options))
843 gold_unreachable();
845 break;
847 case elfcpp::R_386_PLT32:
848 // If the symbol is fully resolved, this is just a PC32 reloc.
849 // Otherwise we need a PLT entry.
850 if (gsym->final_value_is_known(&options))
851 break;
852 target->make_plt_entry(&options, symtab, layout, gsym);
853 break;
855 case elfcpp::R_386_GOTOFF:
856 case elfcpp::R_386_GOTPC:
857 // We need a GOT section.
858 target->got_section(&options, symtab, layout);
859 break;
861 case elfcpp::R_386_COPY:
862 case elfcpp::R_386_GLOB_DAT:
863 case elfcpp::R_386_JUMP_SLOT:
864 case elfcpp::R_386_RELATIVE:
865 case elfcpp::R_386_TLS_TPOFF:
866 case elfcpp::R_386_TLS_DTPMOD32:
867 case elfcpp::R_386_TLS_DTPOFF32:
868 case elfcpp::R_386_TLS_TPOFF32:
869 case elfcpp::R_386_TLS_DESC:
870 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
871 program_name, object->name().c_str(), r_type);
872 gold_exit(false);
873 break;
875 case elfcpp::R_386_TLS_IE:
876 case elfcpp::R_386_TLS_GOTIE:
877 case elfcpp::R_386_TLS_LE:
878 case elfcpp::R_386_TLS_GD:
879 case elfcpp::R_386_TLS_LDM:
880 case elfcpp::R_386_TLS_LDO_32:
881 case elfcpp::R_386_TLS_IE_32:
882 case elfcpp::R_386_TLS_LE_32:
883 case elfcpp::R_386_TLS_GOTDESC:
884 case elfcpp::R_386_TLS_DESC_CALL:
886 const bool is_final = gsym->final_value_is_known(&options);
887 r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type);
888 switch (r_type)
890 case elfcpp::R_386_TLS_LE:
891 case elfcpp::R_386_TLS_LE_32:
892 // FIXME: If generating a shared object, we need to copy
893 // this relocation into the object.
894 gold_assert(!options.is_shared());
895 break;
897 case elfcpp::R_386_TLS_IE:
898 case elfcpp::R_386_TLS_GOTIE:
899 case elfcpp::R_386_TLS_GD:
900 case elfcpp::R_386_TLS_LDM:
901 case elfcpp::R_386_TLS_LDO_32:
902 case elfcpp::R_386_TLS_IE_32:
903 case elfcpp::R_386_TLS_GOTDESC:
904 case elfcpp::R_386_TLS_DESC_CALL:
905 fprintf(stderr,
906 _("%s: %s: unsupported reloc %u "
907 "against global symbol %s\n"),
908 program_name, object->name().c_str(), r_type,
909 gsym->name());
910 break;
913 break;
915 case elfcpp::R_386_32PLT:
916 case elfcpp::R_386_TLS_GD_32:
917 case elfcpp::R_386_TLS_GD_PUSH:
918 case elfcpp::R_386_TLS_GD_CALL:
919 case elfcpp::R_386_TLS_GD_POP:
920 case elfcpp::R_386_TLS_LDM_32:
921 case elfcpp::R_386_TLS_LDM_PUSH:
922 case elfcpp::R_386_TLS_LDM_CALL:
923 case elfcpp::R_386_TLS_LDM_POP:
924 case elfcpp::R_386_USED_BY_INTEL_200:
925 default:
926 fprintf(stderr,
927 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
928 program_name, object->name().c_str(), r_type, gsym->name());
929 break;
933 // Scan relocations for a section.
935 void
936 Target_i386::scan_relocs(const General_options& options,
937 Symbol_table* symtab,
938 Layout* layout,
939 Sized_relobj<32, false>* object,
940 unsigned int data_shndx,
941 unsigned int sh_type,
942 const unsigned char* prelocs,
943 size_t reloc_count,
944 size_t local_symbol_count,
945 const unsigned char* plocal_symbols,
946 Symbol** global_symbols)
948 if (sh_type == elfcpp::SHT_RELA)
950 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
951 program_name, object->name().c_str());
952 gold_exit(false);
955 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
956 Target_i386::Scan>(
957 options,
958 symtab,
959 layout,
960 this,
961 object,
962 data_shndx,
963 prelocs,
964 reloc_count,
965 local_symbol_count,
966 plocal_symbols,
967 global_symbols);
970 // Finalize the sections.
972 void
973 Target_i386::do_finalize_sections(const General_options* options,
974 Layout* layout)
976 // Fill in some more dynamic tags.
977 Output_data_dynamic* const odyn = layout->dynamic_data();
978 if (odyn != NULL)
980 if (this->got_plt_ != NULL)
981 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
983 if (this->plt_ != NULL)
985 const Output_data* od = this->plt_->rel_plt();
986 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
987 odyn->add_section_address(elfcpp::DT_JMPREL, od);
988 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
991 if (this->rel_dyn_ != NULL)
993 const Output_data* od = this->rel_dyn_;
994 odyn->add_section_address(elfcpp::DT_REL, od);
995 odyn->add_section_size(elfcpp::DT_RELSZ, od);
996 odyn->add_constant(elfcpp::DT_RELENT,
997 elfcpp::Elf_sizes<32>::rel_size);
1000 if (!options->is_shared())
1002 // The value of the DT_DEBUG tag is filled in by the dynamic
1003 // linker at run time, and used by the debugger.
1004 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1008 // Emit any relocs we saved in an attempt to avoid generating COPY
1009 // relocs.
1010 if (this->copy_relocs_ == NULL)
1011 return;
1012 if (this->copy_relocs_->any_to_emit())
1014 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1015 this->copy_relocs_->emit(rel_dyn);
1017 delete this->copy_relocs_;
1018 this->copy_relocs_ = NULL;
1021 // Perform a relocation.
1023 inline bool
1024 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1025 Target_i386* target,
1026 size_t relnum,
1027 const elfcpp::Rel<32, false>& rel,
1028 unsigned int r_type,
1029 const Sized_symbol<32>* gsym,
1030 const Symbol_value<32>* psymval,
1031 unsigned char* view,
1032 elfcpp::Elf_types<32>::Elf_Addr address,
1033 off_t view_size)
1035 if (this->skip_call_tls_get_addr_)
1037 if (r_type != elfcpp::R_386_PLT32
1038 || gsym == NULL
1039 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1041 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1042 program_name,
1043 relinfo->location(relnum, rel.get_r_offset()).c_str());
1044 gold_exit(false);
1047 this->skip_call_tls_get_addr_ = false;
1049 return false;
1052 // Pick the value to use for symbols defined in shared objects.
1053 Symbol_value<32> symval;
1054 if (gsym != NULL && gsym->is_from_dynobj())
1056 if (!gsym->has_plt_offset())
1057 gold_unreachable();
1059 symval.set_output_value(target->plt_section()->address()
1060 + gsym->plt_offset());
1061 psymval = &symval;
1064 const Sized_relobj<32, false>* object = relinfo->object;
1066 switch (r_type)
1068 case elfcpp::R_386_NONE:
1069 case elfcpp::R_386_GNU_VTINHERIT:
1070 case elfcpp::R_386_GNU_VTENTRY:
1071 break;
1073 case elfcpp::R_386_32:
1074 Relocate_functions<32, false>::rel32(view, object, psymval);
1075 break;
1077 case elfcpp::R_386_PC32:
1078 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1079 break;
1081 case elfcpp::R_386_16:
1082 Relocate_functions<32, false>::rel16(view, object, psymval);
1083 break;
1085 case elfcpp::R_386_PC16:
1086 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1087 break;
1089 case elfcpp::R_386_8:
1090 Relocate_functions<32, false>::rel8(view, object, psymval);
1091 break;
1093 case elfcpp::R_386_PC8:
1094 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1095 break;
1097 case elfcpp::R_386_PLT32:
1098 gold_assert(gsym->has_plt_offset()
1099 || gsym->final_value_is_known(relinfo->options));
1100 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1101 break;
1103 case elfcpp::R_386_GOT32:
1104 // Local GOT offsets not yet supported.
1105 gold_assert(gsym);
1106 gold_assert(gsym->has_got_offset());
1107 Relocate_functions<32, false>::rel32(view, gsym->got_offset());
1108 break;
1110 case elfcpp::R_386_GOTOFF:
1112 elfcpp::Elf_types<32>::Elf_Addr value;
1113 value = (psymval->value(object, 0)
1114 - target->got_section(NULL, NULL, NULL)->address());
1115 Relocate_functions<32, false>::rel32(view, value);
1117 break;
1119 case elfcpp::R_386_GOTPC:
1121 elfcpp::Elf_types<32>::Elf_Addr value;
1122 value = target->got_section(NULL, NULL, NULL)->address();
1123 Relocate_functions<32, false>::pcrel32(view, value, address);
1125 break;
1127 case elfcpp::R_386_COPY:
1128 case elfcpp::R_386_GLOB_DAT:
1129 case elfcpp::R_386_JUMP_SLOT:
1130 case elfcpp::R_386_RELATIVE:
1131 case elfcpp::R_386_TLS_TPOFF:
1132 case elfcpp::R_386_TLS_DTPMOD32:
1133 case elfcpp::R_386_TLS_DTPOFF32:
1134 case elfcpp::R_386_TLS_TPOFF32:
1135 case elfcpp::R_386_TLS_DESC:
1136 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1137 program_name,
1138 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1139 r_type);
1140 gold_exit(false);
1141 break;
1143 case elfcpp::R_386_TLS_IE:
1144 case elfcpp::R_386_TLS_GOTIE:
1145 case elfcpp::R_386_TLS_LE:
1146 case elfcpp::R_386_TLS_GD:
1147 case elfcpp::R_386_TLS_LDM:
1148 case elfcpp::R_386_TLS_LDO_32:
1149 case elfcpp::R_386_TLS_IE_32:
1150 case elfcpp::R_386_TLS_LE_32:
1151 case elfcpp::R_386_TLS_GOTDESC:
1152 case elfcpp::R_386_TLS_DESC_CALL:
1153 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1154 address, view_size);
1155 break;
1157 case elfcpp::R_386_32PLT:
1158 case elfcpp::R_386_TLS_GD_32:
1159 case elfcpp::R_386_TLS_GD_PUSH:
1160 case elfcpp::R_386_TLS_GD_CALL:
1161 case elfcpp::R_386_TLS_GD_POP:
1162 case elfcpp::R_386_TLS_LDM_32:
1163 case elfcpp::R_386_TLS_LDM_PUSH:
1164 case elfcpp::R_386_TLS_LDM_CALL:
1165 case elfcpp::R_386_TLS_LDM_POP:
1166 case elfcpp::R_386_USED_BY_INTEL_200:
1167 default:
1168 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1169 program_name,
1170 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1171 r_type);
1172 // gold_exit(false);
1173 break;
1176 return true;
1179 // Perform a TLS relocation.
1181 inline void
1182 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1183 size_t relnum,
1184 const elfcpp::Rel<32, false>& rel,
1185 unsigned int r_type,
1186 const Sized_symbol<32>* gsym,
1187 const Symbol_value<32>* psymval,
1188 unsigned char* view,
1189 elfcpp::Elf_types<32>::Elf_Addr,
1190 off_t view_size)
1192 Output_segment* tls_segment = relinfo->layout->tls_segment();
1193 if (tls_segment == NULL)
1195 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1196 program_name,
1197 relinfo->location(relnum, rel.get_r_offset()).c_str());
1198 gold_exit(false);
1201 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1203 const bool is_final = (gsym == NULL
1204 ? !relinfo->options->is_shared()
1205 : gsym->final_value_is_known(relinfo->options));
1206 const unsigned int opt_r_type =
1207 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
1208 switch (r_type)
1210 case elfcpp::R_386_TLS_LE_32:
1211 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1212 Relocate_functions<32, false>::rel32(view, value);
1213 break;
1215 case elfcpp::R_386_TLS_LE:
1216 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1217 Relocate_functions<32, false>::rel32(view, value);
1218 break;
1220 case elfcpp::R_386_TLS_IE:
1221 case elfcpp::R_386_TLS_GOTIE:
1222 case elfcpp::R_386_TLS_IE_32:
1223 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1225 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1226 rel, r_type, value, view,
1227 view_size);
1228 break;
1230 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1231 program_name,
1232 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1233 r_type);
1234 // gold_exit(false);
1235 break;
1237 case elfcpp::R_386_TLS_GD:
1238 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1240 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1241 rel, r_type, value, view,
1242 view_size);
1243 break;
1245 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1246 program_name,
1247 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1248 r_type);
1249 // gold_exit(false);
1250 break;
1252 case elfcpp::R_386_TLS_LDM:
1253 case elfcpp::R_386_TLS_LDO_32:
1254 case elfcpp::R_386_TLS_GOTDESC:
1255 case elfcpp::R_386_TLS_DESC_CALL:
1256 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1257 program_name,
1258 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1259 r_type);
1260 // gold_exit(false);
1261 break;
1265 // Do a relocation in which we convert a TLS Initial-Exec to a
1266 // Local-Exec.
1268 inline void
1269 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1270 size_t relnum,
1271 Output_segment* tls_segment,
1272 const elfcpp::Rel<32, false>& rel,
1273 unsigned int r_type,
1274 elfcpp::Elf_types<32>::Elf_Addr value,
1275 unsigned char* view,
1276 off_t view_size)
1278 // We have to actually change the instructions, which means that we
1279 // need to examine the opcodes to figure out which instruction we
1280 // are looking at.
1281 if (r_type == elfcpp::R_386_TLS_IE)
1283 // movl %gs:XX,%eax ==> movl $YY,%eax
1284 // movl %gs:XX,%reg ==> movl $YY,%reg
1285 // addl %gs:XX,%reg ==> addl $YY,%reg
1286 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1287 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1289 unsigned char op1 = view[-1];
1290 if (op1 == 0xa1)
1292 // movl XX,%eax ==> movl $YY,%eax
1293 view[-1] = 0xb8;
1295 else
1297 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1298 view_size, -2);
1300 unsigned char op2 = view[-2];
1301 if (op2 == 0x8b)
1303 // movl XX,%reg ==> movl $YY,%reg
1304 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1305 (op1 & 0xc7) == 0x05);
1306 view[-2] = 0xc7;
1307 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1309 else if (op2 == 0x03)
1311 // addl XX,%reg ==> addl $YY,%reg
1312 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1313 (op1 & 0xc7) == 0x05);
1314 view[-2] = 0x81;
1315 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1317 else
1318 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1321 else
1323 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1324 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1325 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1326 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1327 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1329 unsigned char op1 = view[-1];
1330 unsigned char op2 = view[-2];
1331 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1332 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1333 if (op2 == 0x8b)
1335 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1336 view[-2] = 0xc7;
1337 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1339 else if (op2 == 0x2b)
1341 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1342 view[-2] = 0x81;
1343 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1345 else if (op2 == 0x03)
1347 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1348 view[-2] = 0x81;
1349 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1351 else
1352 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1355 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1356 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1357 value = - value;
1359 Relocate_functions<32, false>::rel32(view, value);
1362 // Do a relocation in which we convert a TLS Global-Dynamic to a
1363 // Local-Exec.
1365 inline void
1366 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1367 size_t relnum,
1368 Output_segment* tls_segment,
1369 const elfcpp::Rel<32, false>& rel,
1370 unsigned int,
1371 elfcpp::Elf_types<32>::Elf_Addr value,
1372 unsigned char* view,
1373 off_t view_size)
1375 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1376 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1377 // leal foo(%reg),%eax; call ___tls_get_addr
1378 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1380 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1381 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1383 unsigned char op1 = view[-1];
1384 unsigned char op2 = view[-2];
1386 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1387 op2 == 0x8d || op2 == 0x04);
1388 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1389 view[4] == 0xe8);
1391 int roff = 5;
1393 if (op2 == 0x04)
1395 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1396 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1397 view[-3] == 0x8d);
1398 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1399 ((op1 & 0xc7) == 0x05
1400 && op1 != (4 << 3)));
1401 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1403 else
1405 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1406 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1407 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1409 // There is a trailing nop. Use the size byte subl.
1410 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1411 roff = 6;
1413 else
1415 // Use the five byte subl.
1416 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1420 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1421 Relocate_functions<32, false>::rel32(view + roff, value);
1423 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1424 // We can skip it.
1425 this->skip_call_tls_get_addr_ = true;
1428 // Check the range for a TLS relocation.
1430 inline void
1431 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1432 size_t relnum,
1433 const elfcpp::Rel<32, false>& rel,
1434 off_t view_size, off_t off)
1436 off_t offset = rel.get_r_offset() + off;
1437 if (offset < 0 || offset > view_size)
1439 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1440 program_name,
1441 relinfo->location(relnum, rel.get_r_offset()).c_str());
1442 gold_exit(false);
1446 // Check the validity of a TLS relocation. This is like assert.
1448 inline void
1449 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1450 size_t relnum,
1451 const elfcpp::Rel<32, false>& rel,
1452 bool valid)
1454 if (!valid)
1456 fprintf(stderr,
1457 _("%s: %s: TLS relocation against invalid instruction\n"),
1458 program_name,
1459 relinfo->location(relnum, rel.get_r_offset()).c_str());
1460 gold_exit(false);
1464 // Relocate section data.
1466 void
1467 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1468 unsigned int sh_type,
1469 const unsigned char* prelocs,
1470 size_t reloc_count,
1471 unsigned char* view,
1472 elfcpp::Elf_types<32>::Elf_Addr address,
1473 off_t view_size)
1475 gold_assert(sh_type == elfcpp::SHT_REL);
1477 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1478 Target_i386::Relocate>(
1479 relinfo,
1480 this,
1481 prelocs,
1482 reloc_count,
1483 view,
1484 address,
1485 view_size);
1488 // The selector for i386 object files.
1490 class Target_selector_i386 : public Target_selector
1492 public:
1493 Target_selector_i386()
1494 : Target_selector(elfcpp::EM_386, 32, false)
1497 Target*
1498 recognize(int machine, int osabi, int abiversion);
1500 private:
1501 Target_i386* target_;
1504 // Recognize an i386 object file when we already know that the machine
1505 // number is EM_386.
1507 Target*
1508 Target_selector_i386::recognize(int, int, int)
1510 if (this->target_ == NULL)
1511 this->target_ = new Target_i386();
1512 return this->target_;
1515 Target_selector_i386 target_selector_i386;
1517 } // End anonymous namespace.