2007-04-18 Matthias Klose <doko@ubuntu.com>
[binutils.git] / gold / i386.cc
blobdbbd2c09b73a0b727b377878759296ecf482a959
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 elfcpp::Elf_types<32>::Elf_Addr,
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 elfcpp::Elf_types<32>::Elf_Addr,
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 elfcpp::Elf_types<32>::Elf_Addr value,
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 if (gsym != NULL && gsym->is_from_dynobj())
1055 if (gsym->has_plt_offset())
1056 value = target->plt_section()->address() + gsym->plt_offset();
1057 else
1058 gold_unreachable();
1061 switch (r_type)
1063 case elfcpp::R_386_NONE:
1064 case elfcpp::R_386_GNU_VTINHERIT:
1065 case elfcpp::R_386_GNU_VTENTRY:
1066 break;
1068 case elfcpp::R_386_32:
1069 Relocate_functions<32, false>::rel32(view, value);
1070 break;
1072 case elfcpp::R_386_PC32:
1073 Relocate_functions<32, false>::pcrel32(view, value, address);
1074 break;
1076 case elfcpp::R_386_16:
1077 Relocate_functions<32, false>::rel16(view, value);
1078 break;
1080 case elfcpp::R_386_PC16:
1081 Relocate_functions<32, false>::pcrel16(view, value, address);
1082 break;
1084 case elfcpp::R_386_8:
1085 Relocate_functions<32, false>::rel8(view, value);
1086 break;
1088 case elfcpp::R_386_PC8:
1089 Relocate_functions<32, false>::pcrel8(view, value, address);
1090 break;
1092 case elfcpp::R_386_PLT32:
1093 gold_assert(gsym->has_plt_offset()
1094 || gsym->final_value_is_known(relinfo->options));
1095 Relocate_functions<32, false>::pcrel32(view, value, address);
1096 break;
1098 case elfcpp::R_386_GOT32:
1099 // Local GOT offsets not yet supported.
1100 gold_assert(gsym);
1101 gold_assert(gsym->has_got_offset());
1102 value = gsym->got_offset();
1103 Relocate_functions<32, false>::rel32(view, value);
1104 break;
1106 case elfcpp::R_386_GOTOFF:
1107 value -= target->got_section(NULL, NULL, NULL)->address();
1108 Relocate_functions<32, false>::rel32(view, value);
1109 break;
1111 case elfcpp::R_386_GOTPC:
1112 value = target->got_section(NULL, NULL, NULL)->address();
1113 Relocate_functions<32, false>::pcrel32(view, value, address);
1114 break;
1116 case elfcpp::R_386_COPY:
1117 case elfcpp::R_386_GLOB_DAT:
1118 case elfcpp::R_386_JUMP_SLOT:
1119 case elfcpp::R_386_RELATIVE:
1120 case elfcpp::R_386_TLS_TPOFF:
1121 case elfcpp::R_386_TLS_DTPMOD32:
1122 case elfcpp::R_386_TLS_DTPOFF32:
1123 case elfcpp::R_386_TLS_TPOFF32:
1124 case elfcpp::R_386_TLS_DESC:
1125 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1126 program_name,
1127 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1128 r_type);
1129 gold_exit(false);
1130 break;
1132 case elfcpp::R_386_TLS_IE:
1133 case elfcpp::R_386_TLS_GOTIE:
1134 case elfcpp::R_386_TLS_LE:
1135 case elfcpp::R_386_TLS_GD:
1136 case elfcpp::R_386_TLS_LDM:
1137 case elfcpp::R_386_TLS_LDO_32:
1138 case elfcpp::R_386_TLS_IE_32:
1139 case elfcpp::R_386_TLS_LE_32:
1140 case elfcpp::R_386_TLS_GOTDESC:
1141 case elfcpp::R_386_TLS_DESC_CALL:
1142 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
1143 address, view_size);
1144 break;
1146 case elfcpp::R_386_32PLT:
1147 case elfcpp::R_386_TLS_GD_32:
1148 case elfcpp::R_386_TLS_GD_PUSH:
1149 case elfcpp::R_386_TLS_GD_CALL:
1150 case elfcpp::R_386_TLS_GD_POP:
1151 case elfcpp::R_386_TLS_LDM_32:
1152 case elfcpp::R_386_TLS_LDM_PUSH:
1153 case elfcpp::R_386_TLS_LDM_CALL:
1154 case elfcpp::R_386_TLS_LDM_POP:
1155 case elfcpp::R_386_USED_BY_INTEL_200:
1156 default:
1157 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1158 program_name,
1159 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1160 r_type);
1161 // gold_exit(false);
1162 break;
1165 return true;
1168 // Perform a TLS relocation.
1170 inline void
1171 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1172 size_t relnum,
1173 const elfcpp::Rel<32, false>& rel,
1174 unsigned int r_type,
1175 const Sized_symbol<32>* gsym,
1176 elfcpp::Elf_types<32>::Elf_Addr value,
1177 unsigned char* view,
1178 elfcpp::Elf_types<32>::Elf_Addr,
1179 off_t view_size)
1181 Output_segment* tls_segment = relinfo->layout->tls_segment();
1182 if (tls_segment == NULL)
1184 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1185 program_name,
1186 relinfo->location(relnum, rel.get_r_offset()).c_str());
1187 gold_exit(false);
1190 const bool is_final = (gsym == NULL
1191 ? !relinfo->options->is_shared()
1192 : gsym->final_value_is_known(relinfo->options));
1193 const unsigned int opt_r_type =
1194 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
1195 switch (r_type)
1197 case elfcpp::R_386_TLS_LE_32:
1198 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1199 Relocate_functions<32, false>::rel32(view, value);
1200 break;
1202 case elfcpp::R_386_TLS_LE:
1203 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1204 Relocate_functions<32, false>::rel32(view, value);
1205 break;
1207 case elfcpp::R_386_TLS_IE:
1208 case elfcpp::R_386_TLS_GOTIE:
1209 case elfcpp::R_386_TLS_IE_32:
1210 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1212 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1213 rel, r_type, value, view,
1214 view_size);
1215 break;
1217 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1218 program_name,
1219 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1220 r_type);
1221 // gold_exit(false);
1222 break;
1224 case elfcpp::R_386_TLS_GD:
1225 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1227 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1228 rel, r_type, value, view,
1229 view_size);
1230 break;
1232 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1233 program_name,
1234 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1235 r_type);
1236 // gold_exit(false);
1237 break;
1239 case elfcpp::R_386_TLS_LDM:
1240 case elfcpp::R_386_TLS_LDO_32:
1241 case elfcpp::R_386_TLS_GOTDESC:
1242 case elfcpp::R_386_TLS_DESC_CALL:
1243 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1244 program_name,
1245 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1246 r_type);
1247 // gold_exit(false);
1248 break;
1252 // Do a relocation in which we convert a TLS Initial-Exec to a
1253 // Local-Exec.
1255 inline void
1256 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1257 size_t relnum,
1258 Output_segment* tls_segment,
1259 const elfcpp::Rel<32, false>& rel,
1260 unsigned int r_type,
1261 elfcpp::Elf_types<32>::Elf_Addr value,
1262 unsigned char* view,
1263 off_t view_size)
1265 // We have to actually change the instructions, which means that we
1266 // need to examine the opcodes to figure out which instruction we
1267 // are looking at.
1268 if (r_type == elfcpp::R_386_TLS_IE)
1270 // movl %gs:XX,%eax ==> movl $YY,%eax
1271 // movl %gs:XX,%reg ==> movl $YY,%reg
1272 // addl %gs:XX,%reg ==> addl $YY,%reg
1273 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1274 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1276 unsigned char op1 = view[-1];
1277 if (op1 == 0xa1)
1279 // movl XX,%eax ==> movl $YY,%eax
1280 view[-1] = 0xb8;
1282 else
1284 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1285 view_size, -2);
1287 unsigned char op2 = view[-2];
1288 if (op2 == 0x8b)
1290 // movl XX,%reg ==> movl $YY,%reg
1291 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1292 (op1 & 0xc7) == 0x05);
1293 view[-2] = 0xc7;
1294 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1296 else if (op2 == 0x03)
1298 // addl XX,%reg ==> addl $YY,%reg
1299 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1300 (op1 & 0xc7) == 0x05);
1301 view[-2] = 0x81;
1302 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1304 else
1305 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1308 else
1310 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1311 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1312 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1313 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1314 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1316 unsigned char op1 = view[-1];
1317 unsigned char op2 = view[-2];
1318 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1319 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1320 if (op2 == 0x8b)
1322 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1323 view[-2] = 0xc7;
1324 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1326 else if (op2 == 0x2b)
1328 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1329 view[-2] = 0x81;
1330 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1332 else if (op2 == 0x03)
1334 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1335 view[-2] = 0x81;
1336 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1338 else
1339 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1342 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1343 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1344 value = - value;
1346 Relocate_functions<32, false>::rel32(view, value);
1349 // Do a relocation in which we convert a TLS Global-Dynamic to a
1350 // Local-Exec.
1352 inline void
1353 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1354 size_t relnum,
1355 Output_segment* tls_segment,
1356 const elfcpp::Rel<32, false>& rel,
1357 unsigned int,
1358 elfcpp::Elf_types<32>::Elf_Addr value,
1359 unsigned char* view,
1360 off_t view_size)
1362 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1363 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1364 // leal foo(%reg),%eax; call ___tls_get_addr
1365 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1367 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1368 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1370 unsigned char op1 = view[-1];
1371 unsigned char op2 = view[-2];
1373 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1374 op2 == 0x8d || op2 == 0x04);
1375 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1376 view[4] == 0xe8);
1378 int roff = 5;
1380 if (op2 == 0x04)
1382 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1383 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1384 view[-3] == 0x8d);
1385 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1386 ((op1 & 0xc7) == 0x05
1387 && op1 != (4 << 3)));
1388 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1390 else
1392 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1393 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1394 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1396 // There is a trailing nop. Use the size byte subl.
1397 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1398 roff = 6;
1400 else
1402 // Use the five byte subl.
1403 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1407 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1408 Relocate_functions<32, false>::rel32(view + roff, value);
1410 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1411 // We can skip it.
1412 this->skip_call_tls_get_addr_ = true;
1415 // Check the range for a TLS relocation.
1417 inline void
1418 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1419 size_t relnum,
1420 const elfcpp::Rel<32, false>& rel,
1421 off_t view_size, off_t off)
1423 off_t offset = rel.get_r_offset() + off;
1424 if (offset < 0 || offset > view_size)
1426 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1427 program_name,
1428 relinfo->location(relnum, rel.get_r_offset()).c_str());
1429 gold_exit(false);
1433 // Check the validity of a TLS relocation. This is like assert.
1435 inline void
1436 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1437 size_t relnum,
1438 const elfcpp::Rel<32, false>& rel,
1439 bool valid)
1441 if (!valid)
1443 fprintf(stderr,
1444 _("%s: %s: TLS relocation against invalid instruction\n"),
1445 program_name,
1446 relinfo->location(relnum, rel.get_r_offset()).c_str());
1447 gold_exit(false);
1451 // Relocate section data.
1453 void
1454 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1455 unsigned int sh_type,
1456 const unsigned char* prelocs,
1457 size_t reloc_count,
1458 unsigned char* view,
1459 elfcpp::Elf_types<32>::Elf_Addr address,
1460 off_t view_size)
1462 gold_assert(sh_type == elfcpp::SHT_REL);
1464 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1465 Target_i386::Relocate>(
1466 relinfo,
1467 this,
1468 prelocs,
1469 reloc_count,
1470 view,
1471 address,
1472 view_size);
1475 // The selector for i386 object files.
1477 class Target_selector_i386 : public Target_selector
1479 public:
1480 Target_selector_i386()
1481 : Target_selector(elfcpp::EM_386, 32, false)
1484 Target*
1485 recognize(int machine, int osabi, int abiversion);
1487 private:
1488 Target_i386* target_;
1491 // Recognize an i386 object file when we already know that the machine
1492 // number is EM_386.
1494 Target*
1495 Target_selector_i386::recognize(int, int, int)
1497 if (this->target_ == NULL)
1498 this->target_ = new Target_i386();
1499 return this->target_;
1502 Target_selector_i386 target_selector_i386;
1504 } // End anonymous namespace.