1 // i386.cc -- i386 target support for gold.
15 #include "target-reloc.h"
16 #include "target-select.h"
23 // The i386 target class.
25 class Target_i386 : public Sized_target<32, false>
29 : Sized_target<32, false>(&i386_info),
33 // Scan the relocations to look for symbol adjustments.
35 scan_relocs(const General_options& options,
38 Sized_relobj<32, false>* object,
40 const unsigned char* prelocs,
42 size_t local_symbol_count,
43 const unsigned char* plocal_symbols,
44 Symbol** global_symbols);
46 // Relocate a section.
48 relocate_section(const Relocate_info<32, false>*,
50 const unsigned char* prelocs,
53 elfcpp::Elf_types<32>::Elf_Addr view_address,
57 // The class which scans relocations.
61 local(const General_options& options, Symbol_table* symtab,
62 Layout* layout, Target_i386* target,
63 Sized_relobj<32, false>* object,
64 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
65 const elfcpp::Sym<32, false>& lsym);
68 global(const General_options& options, Symbol_table* symtab,
69 Layout* layout, Target_i386* target,
70 Sized_relobj<32, false>* object,
71 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
75 // The class which implements relocation.
80 : skip_call_tls_get_addr_(false)
85 if (this->skip_call_tls_get_addr_)
87 // FIXME: This needs to specify the location somehow.
88 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
94 // Do a relocation. Return false if the caller should not issue
95 // any warnings about this relocation.
97 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
98 const elfcpp::Rel<32, false>&,
99 unsigned int r_type, Sized_symbol<32>*,
100 elfcpp::Elf_types<32>::Elf_Addr,
101 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
105 // Do a TLS relocation.
107 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
108 const elfcpp::Rel<32, false>&,
109 unsigned int r_type, Sized_symbol<32>*,
110 elfcpp::Elf_types<32>::Elf_Addr,
111 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
113 // Do a TLS Initial-Exec to Local-Exec transition.
115 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
116 Output_segment* tls_segment,
117 const elfcpp::Rel<32, false>&, unsigned int r_type,
118 elfcpp::Elf_types<32>::Elf_Addr value,
122 // Do a TLS Global-Dynamic to Local-Exec transition.
124 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
125 Output_segment* tls_segment,
126 const elfcpp::Rel<32, false>&, unsigned int r_type,
127 elfcpp::Elf_types<32>::Elf_Addr value,
131 // Check the range for a TLS relocation.
133 check_range(const Relocate_info<32, false>*, size_t relnum,
134 const elfcpp::Rel<32, false>&, off_t, off_t);
136 // Check the validity of a TLS relocation. This is like assert.
138 check_tls(const Relocate_info<32, false>*, size_t relnum,
139 const elfcpp::Rel<32, false>&, bool);
141 // This is set if we should skip the next reloc, which should be a
142 // PLT32 reloc against ___tls_get_addr.
143 bool skip_call_tls_get_addr_;
146 // Adjust TLS relocation type based on the options and whether this
147 // is a local symbol.
149 optimize_tls_reloc(const General_options*, bool is_local, int r_type);
151 // Get the GOT section, creating it if necessary.
152 Output_section_got<32, false>*
153 got_section(Symbol_table*, Layout*);
155 // Information about this specific target which we pass to the
156 // general Target structure.
157 static const Target::Target_info i386_info;
160 Output_section_got<32, false>* got_;
163 const Target::Target_info Target_i386::i386_info =
166 false, // is_big_endian
167 elfcpp::EM_386, // machine_code
168 false, // has_make_symbol
169 false, // has_resolve,
170 0x08048000, // text_segment_address,
171 0x1000, // abi_pagesize
172 0x1000 // common_pagesize
175 // Get the GOT section, creating it if necessary.
177 Output_section_got<32, false>*
178 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
180 if (this->got_ == NULL)
182 this->got_ = new Output_section_got<32, false>();
184 assert(symtab != NULL && layout != NULL);
185 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
186 elfcpp::SHF_ALLOC, this->got_);
188 // The first three entries are reserved.
189 this->got_->add_constant(0);
190 this->got_->add_constant(0);
191 this->got_->add_constant(0);
193 // Define _GLOBAL_OFFSET_TABLE_ at the start of the section.
194 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", this->got_,
195 0, 0, elfcpp::STT_OBJECT,
197 elfcpp::STV_HIDDEN, 0,
203 // Optimize the TLS relocation type based on what we know about the
204 // symbol. IS_LOCAL is true if this symbol can be resolved entirely
205 // locally--i.e., does not have to be in the dynamic symbol table.
208 Target_i386::optimize_tls_reloc(const General_options* options, bool is_local,
211 // If we are generating a shared library, then we can't do anything
213 if (options->is_shared())
218 case elfcpp::R_386_TLS_GD:
219 case elfcpp::R_386_TLS_GOTDESC:
220 case elfcpp::R_386_TLS_DESC_CALL:
221 // These are Global-Dynamic which permits fully general TLS
222 // access. Since we know that we are generating an executable,
223 // we can convert this to Initial-Exec. If we also know that
224 // this is a local symbol, we can further switch to Local-Exec.
226 return elfcpp::R_386_TLS_LE_32;
227 return elfcpp::R_386_TLS_IE_32;
229 case elfcpp::R_386_TLS_LDM:
230 // This is Local-Dynamic, which refers to a local symbol in the
231 // dynamic TLS block. Since we know that we generating an
232 // executable, we can switch to Local-Exec.
233 return elfcpp::R_386_TLS_LE_32;
235 case elfcpp::R_386_TLS_LDO_32:
236 // Another type of Local-Dynamic relocation.
237 return elfcpp::R_386_TLS_LE;
239 case elfcpp::R_386_TLS_IE:
240 case elfcpp::R_386_TLS_GOTIE:
241 case elfcpp::R_386_TLS_IE_32:
242 // These are Initial-Exec relocs which get the thread offset
243 // from the GOT. If we know that we are linking against the
244 // local symbol, we can switch to Local-Exec, which links the
245 // thread offset into the instruction.
247 return elfcpp::R_386_TLS_LE_32;
250 case elfcpp::R_386_TLS_LE:
251 case elfcpp::R_386_TLS_LE_32:
252 // When we already have Local-Exec, there is nothing further we
261 // Scan a relocation for a local symbol.
264 Target_i386::Scan::local(const General_options& options,
265 Symbol_table* symtab,
268 Sized_relobj<32, false>* object,
269 const elfcpp::Rel<32, false>&, unsigned int r_type,
270 const elfcpp::Sym<32, false>&)
274 case elfcpp::R_386_NONE:
275 case elfcpp::R_386_GNU_VTINHERIT:
276 case elfcpp::R_386_GNU_VTENTRY:
279 case elfcpp::R_386_32:
280 case elfcpp::R_386_16:
281 case elfcpp::R_386_8:
282 // FIXME: If we are generating a shared object we need to copy
283 // this relocation into the object.
286 case elfcpp::R_386_PC32:
287 case elfcpp::R_386_PC16:
288 case elfcpp::R_386_PC8:
291 case elfcpp::R_386_GOTOFF:
292 case elfcpp::R_386_GOTPC:
293 // We need a GOT section.
294 target->got_section(symtab, layout);
297 case elfcpp::R_386_COPY:
298 case elfcpp::R_386_GLOB_DAT:
299 case elfcpp::R_386_JUMP_SLOT:
300 case elfcpp::R_386_RELATIVE:
301 case elfcpp::R_386_TLS_TPOFF:
302 case elfcpp::R_386_TLS_DTPMOD32:
303 case elfcpp::R_386_TLS_DTPOFF32:
304 case elfcpp::R_386_TLS_TPOFF32:
305 case elfcpp::R_386_TLS_DESC:
306 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
307 program_name, object->name().c_str(), r_type);
311 case elfcpp::R_386_TLS_IE:
312 case elfcpp::R_386_TLS_GOTIE:
313 case elfcpp::R_386_TLS_LE:
314 case elfcpp::R_386_TLS_GD:
315 case elfcpp::R_386_TLS_LDM:
316 case elfcpp::R_386_TLS_LDO_32:
317 case elfcpp::R_386_TLS_IE_32:
318 case elfcpp::R_386_TLS_LE_32:
319 case elfcpp::R_386_TLS_GOTDESC:
320 case elfcpp::R_386_TLS_DESC_CALL:
321 r_type = Target_i386::optimize_tls_reloc(&options, true, r_type);
324 case elfcpp::R_386_TLS_LE:
325 case elfcpp::R_386_TLS_LE_32:
326 // FIXME: If generating a shared object, we need to copy
327 // this relocation into the object.
330 case elfcpp::R_386_TLS_IE:
331 case elfcpp::R_386_TLS_GOTIE:
332 case elfcpp::R_386_TLS_GD:
333 case elfcpp::R_386_TLS_LDM:
334 case elfcpp::R_386_TLS_LDO_32:
335 case elfcpp::R_386_TLS_IE_32:
336 case elfcpp::R_386_TLS_GOTDESC:
337 case elfcpp::R_386_TLS_DESC_CALL:
339 _("%s: %s: unsupported reloc %u against local symbol\n"),
340 program_name, object->name().c_str(), r_type);
345 case elfcpp::R_386_GOT32:
346 case elfcpp::R_386_PLT32:
347 case elfcpp::R_386_32PLT:
348 case elfcpp::R_386_TLS_GD_32:
349 case elfcpp::R_386_TLS_GD_PUSH:
350 case elfcpp::R_386_TLS_GD_CALL:
351 case elfcpp::R_386_TLS_GD_POP:
352 case elfcpp::R_386_TLS_LDM_32:
353 case elfcpp::R_386_TLS_LDM_PUSH:
354 case elfcpp::R_386_TLS_LDM_CALL:
355 case elfcpp::R_386_TLS_LDM_POP:
356 case elfcpp::R_386_USED_BY_INTEL_200:
358 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
359 program_name, object->name().c_str(), r_type);
364 // Scan a relocation for a global symbol.
367 Target_i386::Scan::global(const General_options& options,
368 Symbol_table* symtab,
371 Sized_relobj<32, false>* object,
372 const elfcpp::Rel<32, false>&, unsigned int r_type,
377 case elfcpp::R_386_NONE:
378 case elfcpp::R_386_GNU_VTINHERIT:
379 case elfcpp::R_386_GNU_VTENTRY:
382 case elfcpp::R_386_32:
383 case elfcpp::R_386_PC32:
384 case elfcpp::R_386_16:
385 case elfcpp::R_386_PC16:
386 case elfcpp::R_386_8:
387 case elfcpp::R_386_PC8:
388 // FIXME: If we are generating a shared object we may need to
389 // copy this relocation into the object. If this symbol is
390 // defined in a shared object, we may need to copy this
391 // relocation in order to avoid a COPY relocation.
394 case elfcpp::R_386_GOT32:
395 // The symbol requires a GOT entry.
396 if (!gsym->has_got_offset())
398 Output_section_got<32, false>* got = target->got_section(symtab,
400 const unsigned int got_offset = got->add_global(gsym);
401 gsym->set_got_offset(got_offset);
403 // If this symbol is not resolved locally, we need to add a
404 // dynamic relocation for it.
405 if (!gsym->is_resolved_locally())
410 case elfcpp::R_386_PLT32:
411 // If the symbol is resolved locally, this is just a PC32 reloc.
412 if (gsym->is_resolved_locally())
415 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
416 program_name, object->name().c_str(), r_type, gsym->name());
419 case elfcpp::R_386_GOTOFF:
420 case elfcpp::R_386_GOTPC:
421 // We need a GOT section.
422 target->got_section(symtab, layout);
425 case elfcpp::R_386_COPY:
426 case elfcpp::R_386_GLOB_DAT:
427 case elfcpp::R_386_JUMP_SLOT:
428 case elfcpp::R_386_RELATIVE:
429 case elfcpp::R_386_TLS_TPOFF:
430 case elfcpp::R_386_TLS_DTPMOD32:
431 case elfcpp::R_386_TLS_DTPOFF32:
432 case elfcpp::R_386_TLS_TPOFF32:
433 case elfcpp::R_386_TLS_DESC:
434 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
435 program_name, object->name().c_str(), r_type);
439 case elfcpp::R_386_TLS_IE:
440 case elfcpp::R_386_TLS_GOTIE:
441 case elfcpp::R_386_TLS_LE:
442 case elfcpp::R_386_TLS_GD:
443 case elfcpp::R_386_TLS_LDM:
444 case elfcpp::R_386_TLS_LDO_32:
445 case elfcpp::R_386_TLS_IE_32:
446 case elfcpp::R_386_TLS_LE_32:
447 case elfcpp::R_386_TLS_GOTDESC:
448 case elfcpp::R_386_TLS_DESC_CALL:
449 r_type = Target_i386::optimize_tls_reloc(&options,
450 gsym->is_resolved_locally(),
454 case elfcpp::R_386_TLS_LE:
455 case elfcpp::R_386_TLS_LE_32:
456 // FIXME: If generating a shared object, we need to copy
457 // this relocation into the object.
460 case elfcpp::R_386_TLS_IE:
461 case elfcpp::R_386_TLS_GOTIE:
462 case elfcpp::R_386_TLS_GD:
463 case elfcpp::R_386_TLS_LDM:
464 case elfcpp::R_386_TLS_LDO_32:
465 case elfcpp::R_386_TLS_IE_32:
466 case elfcpp::R_386_TLS_GOTDESC:
467 case elfcpp::R_386_TLS_DESC_CALL:
469 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
470 program_name, object->name().c_str(), r_type, gsym->name());
475 case elfcpp::R_386_32PLT:
476 case elfcpp::R_386_TLS_GD_32:
477 case elfcpp::R_386_TLS_GD_PUSH:
478 case elfcpp::R_386_TLS_GD_CALL:
479 case elfcpp::R_386_TLS_GD_POP:
480 case elfcpp::R_386_TLS_LDM_32:
481 case elfcpp::R_386_TLS_LDM_PUSH:
482 case elfcpp::R_386_TLS_LDM_CALL:
483 case elfcpp::R_386_TLS_LDM_POP:
484 case elfcpp::R_386_USED_BY_INTEL_200:
487 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
488 program_name, object->name().c_str(), r_type, gsym->name());
493 // Scan relocations for a section.
496 Target_i386::scan_relocs(const General_options& options,
497 Symbol_table* symtab,
499 Sized_relobj<32, false>* object,
500 unsigned int sh_type,
501 const unsigned char* prelocs,
503 size_t local_symbol_count,
504 const unsigned char* plocal_symbols,
505 Symbol** global_symbols)
507 if (sh_type == elfcpp::SHT_RELA)
509 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
510 program_name, object->name().c_str());
514 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
528 // Perform a relocation.
531 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
534 const elfcpp::Rel<32, false>& rel,
536 Sized_symbol<32>* gsym,
537 elfcpp::Elf_types<32>::Elf_Addr value,
539 elfcpp::Elf_types<32>::Elf_Addr address,
542 if (this->skip_call_tls_get_addr_)
544 if (r_type != elfcpp::R_386_PLT32
546 || strcmp(gsym->name(), "___tls_get_addr") != 0)
548 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
550 relinfo->location(relnum, rel.get_r_offset()).c_str());
554 this->skip_call_tls_get_addr_ = false;
561 case elfcpp::R_386_NONE:
562 case elfcpp::R_386_GNU_VTINHERIT:
563 case elfcpp::R_386_GNU_VTENTRY:
566 case elfcpp::R_386_32:
567 Relocate_functions<32, false>::rel32(view, value);
570 case elfcpp::R_386_PC32:
571 Relocate_functions<32, false>::pcrel32(view, value, address);
574 case elfcpp::R_386_16:
575 Relocate_functions<32, false>::rel16(view, value);
578 case elfcpp::R_386_PC16:
579 Relocate_functions<32, false>::pcrel16(view, value, address);
582 case elfcpp::R_386_8:
583 Relocate_functions<32, false>::rel8(view, value);
586 case elfcpp::R_386_PC8:
587 Relocate_functions<32, false>::pcrel8(view, value, address);
590 case elfcpp::R_386_PLT32:
591 if (gsym->is_resolved_locally())
592 Relocate_functions<32, false>::pcrel32(view, value, address);
594 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
596 relinfo->location(relnum, rel.get_r_offset()).c_str(),
600 case elfcpp::R_386_GOT32:
601 // Local GOT offsets not yet supported.
603 assert(gsym->has_got_offset());
604 value = gsym->got_offset();
605 Relocate_functions<32, false>::rel32(view, value);
608 case elfcpp::R_386_GOTOFF:
609 value -= target->got_section(NULL, NULL)->address();
610 Relocate_functions<32, false>::rel32(view, value);
613 case elfcpp::R_386_GOTPC:
614 value = target->got_section(NULL, NULL)->address();
615 Relocate_functions<32, false>::pcrel32(view, value, address);
618 case elfcpp::R_386_COPY:
619 case elfcpp::R_386_GLOB_DAT:
620 case elfcpp::R_386_JUMP_SLOT:
621 case elfcpp::R_386_RELATIVE:
622 case elfcpp::R_386_TLS_TPOFF:
623 case elfcpp::R_386_TLS_DTPMOD32:
624 case elfcpp::R_386_TLS_DTPOFF32:
625 case elfcpp::R_386_TLS_TPOFF32:
626 case elfcpp::R_386_TLS_DESC:
627 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
629 relinfo->location(relnum, rel.get_r_offset()).c_str(),
634 case elfcpp::R_386_TLS_IE:
635 case elfcpp::R_386_TLS_GOTIE:
636 case elfcpp::R_386_TLS_LE:
637 case elfcpp::R_386_TLS_GD:
638 case elfcpp::R_386_TLS_LDM:
639 case elfcpp::R_386_TLS_LDO_32:
640 case elfcpp::R_386_TLS_IE_32:
641 case elfcpp::R_386_TLS_LE_32:
642 case elfcpp::R_386_TLS_GOTDESC:
643 case elfcpp::R_386_TLS_DESC_CALL:
644 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
648 case elfcpp::R_386_32PLT:
649 case elfcpp::R_386_TLS_GD_32:
650 case elfcpp::R_386_TLS_GD_PUSH:
651 case elfcpp::R_386_TLS_GD_CALL:
652 case elfcpp::R_386_TLS_GD_POP:
653 case elfcpp::R_386_TLS_LDM_32:
654 case elfcpp::R_386_TLS_LDM_PUSH:
655 case elfcpp::R_386_TLS_LDM_CALL:
656 case elfcpp::R_386_TLS_LDM_POP:
657 case elfcpp::R_386_USED_BY_INTEL_200:
659 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
661 relinfo->location(relnum, rel.get_r_offset()).c_str(),
670 // Perform a TLS relocation.
673 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
675 const elfcpp::Rel<32, false>& rel,
677 Sized_symbol<32>* gsym,
678 elfcpp::Elf_types<32>::Elf_Addr value,
680 elfcpp::Elf_types<32>::Elf_Addr,
683 Output_segment* tls_segment = relinfo->layout->tls_segment();
684 if (tls_segment == NULL)
686 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
688 relinfo->location(relnum, rel.get_r_offset()).c_str());
692 const bool is_local = gsym == NULL || gsym->is_resolved_locally();
693 const unsigned int opt_r_type =
694 Target_i386::optimize_tls_reloc(relinfo->options, is_local, r_type);
697 case elfcpp::R_386_TLS_LE_32:
698 value = tls_segment->vaddr() + tls_segment->memsz() - value;
699 Relocate_functions<32, false>::rel32(view, value);
702 case elfcpp::R_386_TLS_LE:
703 value = value - (tls_segment->vaddr() + tls_segment->memsz());
704 Relocate_functions<32, false>::rel32(view, value);
707 case elfcpp::R_386_TLS_IE:
708 case elfcpp::R_386_TLS_GOTIE:
709 case elfcpp::R_386_TLS_IE_32:
710 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
712 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
713 rel, r_type, value, view,
717 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
719 relinfo->location(relnum, rel.get_r_offset()).c_str(),
724 case elfcpp::R_386_TLS_GD:
725 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
727 this->tls_gd_to_le(relinfo, relnum, tls_segment,
728 rel, r_type, value, view,
732 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
734 relinfo->location(relnum, rel.get_r_offset()).c_str(),
739 case elfcpp::R_386_TLS_LDM:
740 case elfcpp::R_386_TLS_LDO_32:
741 case elfcpp::R_386_TLS_GOTDESC:
742 case elfcpp::R_386_TLS_DESC_CALL:
743 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
745 relinfo->location(relnum, rel.get_r_offset()).c_str(),
752 // Do a relocation in which we convert a TLS Initial-Exec to a
756 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
758 Output_segment* tls_segment,
759 const elfcpp::Rel<32, false>& rel,
761 elfcpp::Elf_types<32>::Elf_Addr value,
765 // We have to actually change the instructions, which means that we
766 // need to examine the opcodes to figure out which instruction we
768 if (r_type == elfcpp::R_386_TLS_IE)
770 // movl %gs:XX,%eax ==> movl $YY,%eax
771 // movl %gs:XX,%reg ==> movl $YY,%reg
772 // addl %gs:XX,%reg ==> addl $YY,%reg
773 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
774 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
776 unsigned char op1 = view[-1];
779 // movl XX,%eax ==> movl $YY,%eax
784 Target_i386::Relocate::check_range(relinfo, relnum, rel,
787 unsigned char op2 = view[-2];
790 // movl XX,%reg ==> movl $YY,%reg
791 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
792 (op1 & 0xc7) == 0x05);
794 view[-1] = 0xc0 | ((op1 >> 3) & 7);
796 else if (op2 == 0x03)
798 // addl XX,%reg ==> addl $YY,%reg
799 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
800 (op1 & 0xc7) == 0x05);
802 view[-1] = 0xc0 | ((op1 >> 3) & 7);
805 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
810 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
811 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
812 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
813 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
814 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
816 unsigned char op1 = view[-1];
817 unsigned char op2 = view[-2];
818 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
819 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
822 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
824 view[-1] = 0xc0 | ((op1 >> 3) & 7);
826 else if (op2 == 0x2b)
828 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
830 view[-1] = 0xe8 | ((op1 >> 3) & 7);
832 else if (op2 == 0x03)
834 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
836 view[-1] = 0xc0 | ((op1 >> 3) & 7);
839 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
842 value = tls_segment->vaddr() + tls_segment->memsz() - value;
843 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
846 Relocate_functions<32, false>::rel32(view, value);
849 // Do a relocation in which we convert a TLS Global-Dynamic to a
853 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
855 Output_segment* tls_segment,
856 const elfcpp::Rel<32, false>& rel,
858 elfcpp::Elf_types<32>::Elf_Addr value,
862 // leal foo(,%reg,1),%eax; call ___tls_get_addr
863 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
864 // leal foo(%reg),%eax; call ___tls_get_addr
865 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
867 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
868 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
870 unsigned char op1 = view[-1];
871 unsigned char op2 = view[-2];
873 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
874 op2 == 0x8d || op2 == 0x04);
875 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
882 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
883 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
885 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
886 ((op1 & 0xc7) == 0x05
887 && op1 != (4 << 3)));
888 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
892 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
893 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
894 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
896 // There is a trailing nop. Use the size byte subl.
897 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
902 // Use the five byte subl.
903 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
907 value = tls_segment->vaddr() + tls_segment->memsz() - value;
908 Relocate_functions<32, false>::rel32(view + roff, value);
910 // The next reloc should be a PLT32 reloc against __tls_get_addr.
912 this->skip_call_tls_get_addr_ = true;
915 // Check the range for a TLS relocation.
918 Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
920 const elfcpp::Rel<32, false>& rel,
921 off_t view_size, off_t off)
923 off_t offset = rel.get_r_offset() + off;
924 if (offset < 0 || offset > view_size)
926 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
928 relinfo->location(relnum, rel.get_r_offset()).c_str());
933 // Check the validity of a TLS relocation. This is like assert.
936 Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
938 const elfcpp::Rel<32, false>& rel,
944 _("%s: %s: TLS relocation against invalid instruction\n"),
946 relinfo->location(relnum, rel.get_r_offset()).c_str());
951 // Relocate section data.
954 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
955 unsigned int sh_type,
956 const unsigned char* prelocs,
959 elfcpp::Elf_types<32>::Elf_Addr address,
962 assert(sh_type == elfcpp::SHT_REL);
964 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
965 Target_i386::Relocate>(
975 // The selector for i386 object files.
977 class Target_selector_i386 : public Target_selector
980 Target_selector_i386()
981 : Target_selector(elfcpp::EM_386, 32, false)
985 recognize(int machine, int osabi, int abiversion);
988 Target_i386* target_;
991 // Recognize an i386 object file when we already know that the machine
995 Target_selector_i386::recognize(int, int, int)
997 if (this->target_ == NULL)
998 this->target_ = new Target_i386();
999 return this->target_;
1002 Target_selector_i386 target_selector_i386;
1004 } // End anonymous namespace.