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
, const 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
, const 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_data_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_data_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 "/usr/lib/libc.so.1", // dynamic_linker
171 0x08048000, // text_segment_address
172 0x1000, // abi_pagesize
173 0x1000 // common_pagesize
176 // Get the GOT section, creating it if necessary.
178 Output_data_got
<32, false>*
179 Target_i386::got_section(Symbol_table
* symtab
, Layout
* layout
)
181 if (this->got_
== NULL
)
183 this->got_
= new Output_data_got
<32, false>();
185 assert(symtab
!= NULL
&& layout
!= NULL
);
186 layout
->add_output_section_data(".got", elfcpp::SHT_PROGBITS
,
187 elfcpp::SHF_ALLOC
, this->got_
);
189 // The first three entries are reserved.
190 this->got_
->add_constant(0);
191 this->got_
->add_constant(0);
192 this->got_
->add_constant(0);
194 // Define _GLOBAL_OFFSET_TABLE_ at the start of the section.
195 symtab
->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", this->got_
,
196 0, 0, elfcpp::STT_OBJECT
,
198 elfcpp::STV_HIDDEN
, 0,
204 // Optimize the TLS relocation type based on what we know about the
205 // symbol. IS_LOCAL is true if this symbol can be resolved entirely
206 // locally--i.e., does not have to be in the dynamic symbol table.
209 Target_i386::optimize_tls_reloc(const General_options
* options
, bool is_local
,
212 // If we are generating a shared library, then we can't do anything
214 if (options
->is_shared())
219 case elfcpp::R_386_TLS_GD
:
220 case elfcpp::R_386_TLS_GOTDESC
:
221 case elfcpp::R_386_TLS_DESC_CALL
:
222 // These are Global-Dynamic which permits fully general TLS
223 // access. Since we know that we are generating an executable,
224 // we can convert this to Initial-Exec. If we also know that
225 // this is a local symbol, we can further switch to Local-Exec.
227 return elfcpp::R_386_TLS_LE_32
;
228 return elfcpp::R_386_TLS_IE_32
;
230 case elfcpp::R_386_TLS_LDM
:
231 // This is Local-Dynamic, which refers to a local symbol in the
232 // dynamic TLS block. Since we know that we generating an
233 // executable, we can switch to Local-Exec.
234 return elfcpp::R_386_TLS_LE_32
;
236 case elfcpp::R_386_TLS_LDO_32
:
237 // Another type of Local-Dynamic relocation.
238 return elfcpp::R_386_TLS_LE
;
240 case elfcpp::R_386_TLS_IE
:
241 case elfcpp::R_386_TLS_GOTIE
:
242 case elfcpp::R_386_TLS_IE_32
:
243 // These are Initial-Exec relocs which get the thread offset
244 // from the GOT. If we know that we are linking against the
245 // local symbol, we can switch to Local-Exec, which links the
246 // thread offset into the instruction.
248 return elfcpp::R_386_TLS_LE_32
;
251 case elfcpp::R_386_TLS_LE
:
252 case elfcpp::R_386_TLS_LE_32
:
253 // When we already have Local-Exec, there is nothing further we
262 // Scan a relocation for a local symbol.
265 Target_i386::Scan::local(const General_options
& options
,
266 Symbol_table
* symtab
,
269 Sized_relobj
<32, false>* object
,
270 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
271 const elfcpp::Sym
<32, false>&)
275 case elfcpp::R_386_NONE
:
276 case elfcpp::R_386_GNU_VTINHERIT
:
277 case elfcpp::R_386_GNU_VTENTRY
:
280 case elfcpp::R_386_32
:
281 case elfcpp::R_386_16
:
282 case elfcpp::R_386_8
:
283 // FIXME: If we are generating a shared object we need to copy
284 // this relocation into the object.
287 case elfcpp::R_386_PC32
:
288 case elfcpp::R_386_PC16
:
289 case elfcpp::R_386_PC8
:
292 case elfcpp::R_386_GOTOFF
:
293 case elfcpp::R_386_GOTPC
:
294 // We need a GOT section.
295 target
->got_section(symtab
, layout
);
298 case elfcpp::R_386_COPY
:
299 case elfcpp::R_386_GLOB_DAT
:
300 case elfcpp::R_386_JUMP_SLOT
:
301 case elfcpp::R_386_RELATIVE
:
302 case elfcpp::R_386_TLS_TPOFF
:
303 case elfcpp::R_386_TLS_DTPMOD32
:
304 case elfcpp::R_386_TLS_DTPOFF32
:
305 case elfcpp::R_386_TLS_TPOFF32
:
306 case elfcpp::R_386_TLS_DESC
:
307 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
308 program_name
, object
->name().c_str(), r_type
);
312 case elfcpp::R_386_TLS_IE
:
313 case elfcpp::R_386_TLS_GOTIE
:
314 case elfcpp::R_386_TLS_LE
:
315 case elfcpp::R_386_TLS_GD
:
316 case elfcpp::R_386_TLS_LDM
:
317 case elfcpp::R_386_TLS_LDO_32
:
318 case elfcpp::R_386_TLS_IE_32
:
319 case elfcpp::R_386_TLS_LE_32
:
320 case elfcpp::R_386_TLS_GOTDESC
:
321 case elfcpp::R_386_TLS_DESC_CALL
:
322 r_type
= Target_i386::optimize_tls_reloc(&options
, true, r_type
);
325 case elfcpp::R_386_TLS_LE
:
326 case elfcpp::R_386_TLS_LE_32
:
327 // FIXME: If generating a shared object, we need to copy
328 // this relocation into the object.
331 case elfcpp::R_386_TLS_IE
:
332 case elfcpp::R_386_TLS_GOTIE
:
333 case elfcpp::R_386_TLS_GD
:
334 case elfcpp::R_386_TLS_LDM
:
335 case elfcpp::R_386_TLS_LDO_32
:
336 case elfcpp::R_386_TLS_IE_32
:
337 case elfcpp::R_386_TLS_GOTDESC
:
338 case elfcpp::R_386_TLS_DESC_CALL
:
340 _("%s: %s: unsupported reloc %u against local symbol\n"),
341 program_name
, object
->name().c_str(), r_type
);
346 case elfcpp::R_386_GOT32
:
347 case elfcpp::R_386_PLT32
:
348 case elfcpp::R_386_32PLT
:
349 case elfcpp::R_386_TLS_GD_32
:
350 case elfcpp::R_386_TLS_GD_PUSH
:
351 case elfcpp::R_386_TLS_GD_CALL
:
352 case elfcpp::R_386_TLS_GD_POP
:
353 case elfcpp::R_386_TLS_LDM_32
:
354 case elfcpp::R_386_TLS_LDM_PUSH
:
355 case elfcpp::R_386_TLS_LDM_CALL
:
356 case elfcpp::R_386_TLS_LDM_POP
:
357 case elfcpp::R_386_USED_BY_INTEL_200
:
359 fprintf(stderr
, _("%s: %s: unsupported reloc %u against local symbol\n"),
360 program_name
, object
->name().c_str(), r_type
);
365 // Scan a relocation for a global symbol.
368 Target_i386::Scan::global(const General_options
& options
,
369 Symbol_table
* symtab
,
372 Sized_relobj
<32, false>* object
,
373 const elfcpp::Rel
<32, false>&, unsigned int r_type
,
378 case elfcpp::R_386_NONE
:
379 case elfcpp::R_386_GNU_VTINHERIT
:
380 case elfcpp::R_386_GNU_VTENTRY
:
383 case elfcpp::R_386_32
:
384 case elfcpp::R_386_PC32
:
385 case elfcpp::R_386_16
:
386 case elfcpp::R_386_PC16
:
387 case elfcpp::R_386_8
:
388 case elfcpp::R_386_PC8
:
389 // FIXME: If we are generating a shared object we may need to
390 // copy this relocation into the object. If this symbol is
391 // defined in a shared object, we may need to copy this
392 // relocation in order to avoid a COPY relocation.
395 case elfcpp::R_386_GOT32
:
396 // The symbol requires a GOT entry.
397 if (target
->got_section(symtab
, layout
)->add_global(gsym
))
399 // If this symbol is not resolved locally, we need to add a
400 // dynamic relocation for it.
401 if (!gsym
->is_resolved_locally())
406 case elfcpp::R_386_PLT32
:
407 // If the symbol is resolved locally, this is just a PC32 reloc.
408 if (gsym
->is_resolved_locally())
411 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
412 program_name
, object
->name().c_str(), r_type
, gsym
->name());
415 case elfcpp::R_386_GOTOFF
:
416 case elfcpp::R_386_GOTPC
:
417 // We need a GOT section.
418 target
->got_section(symtab
, layout
);
421 case elfcpp::R_386_COPY
:
422 case elfcpp::R_386_GLOB_DAT
:
423 case elfcpp::R_386_JUMP_SLOT
:
424 case elfcpp::R_386_RELATIVE
:
425 case elfcpp::R_386_TLS_TPOFF
:
426 case elfcpp::R_386_TLS_DTPMOD32
:
427 case elfcpp::R_386_TLS_DTPOFF32
:
428 case elfcpp::R_386_TLS_TPOFF32
:
429 case elfcpp::R_386_TLS_DESC
:
430 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
431 program_name
, object
->name().c_str(), r_type
);
435 case elfcpp::R_386_TLS_IE
:
436 case elfcpp::R_386_TLS_GOTIE
:
437 case elfcpp::R_386_TLS_LE
:
438 case elfcpp::R_386_TLS_GD
:
439 case elfcpp::R_386_TLS_LDM
:
440 case elfcpp::R_386_TLS_LDO_32
:
441 case elfcpp::R_386_TLS_IE_32
:
442 case elfcpp::R_386_TLS_LE_32
:
443 case elfcpp::R_386_TLS_GOTDESC
:
444 case elfcpp::R_386_TLS_DESC_CALL
:
445 r_type
= Target_i386::optimize_tls_reloc(&options
,
446 gsym
->is_resolved_locally(),
450 case elfcpp::R_386_TLS_LE
:
451 case elfcpp::R_386_TLS_LE_32
:
452 // FIXME: If generating a shared object, we need to copy
453 // this relocation into the object.
456 case elfcpp::R_386_TLS_IE
:
457 case elfcpp::R_386_TLS_GOTIE
:
458 case elfcpp::R_386_TLS_GD
:
459 case elfcpp::R_386_TLS_LDM
:
460 case elfcpp::R_386_TLS_LDO_32
:
461 case elfcpp::R_386_TLS_IE_32
:
462 case elfcpp::R_386_TLS_GOTDESC
:
463 case elfcpp::R_386_TLS_DESC_CALL
:
465 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
466 program_name
, object
->name().c_str(), r_type
, gsym
->name());
471 case elfcpp::R_386_32PLT
:
472 case elfcpp::R_386_TLS_GD_32
:
473 case elfcpp::R_386_TLS_GD_PUSH
:
474 case elfcpp::R_386_TLS_GD_CALL
:
475 case elfcpp::R_386_TLS_GD_POP
:
476 case elfcpp::R_386_TLS_LDM_32
:
477 case elfcpp::R_386_TLS_LDM_PUSH
:
478 case elfcpp::R_386_TLS_LDM_CALL
:
479 case elfcpp::R_386_TLS_LDM_POP
:
480 case elfcpp::R_386_USED_BY_INTEL_200
:
483 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
484 program_name
, object
->name().c_str(), r_type
, gsym
->name());
489 // Scan relocations for a section.
492 Target_i386::scan_relocs(const General_options
& options
,
493 Symbol_table
* symtab
,
495 Sized_relobj
<32, false>* object
,
496 unsigned int sh_type
,
497 const unsigned char* prelocs
,
499 size_t local_symbol_count
,
500 const unsigned char* plocal_symbols
,
501 Symbol
** global_symbols
)
503 if (sh_type
== elfcpp::SHT_RELA
)
505 fprintf(stderr
, _("%s: %s: unsupported RELA reloc section\n"),
506 program_name
, object
->name().c_str());
510 gold::scan_relocs
<32, false, Target_i386
, elfcpp::SHT_REL
,
524 // Perform a relocation.
527 Target_i386::Relocate::relocate(const Relocate_info
<32, false>* relinfo
,
530 const elfcpp::Rel
<32, false>& rel
,
532 const Sized_symbol
<32>* gsym
,
533 elfcpp::Elf_types
<32>::Elf_Addr value
,
535 elfcpp::Elf_types
<32>::Elf_Addr address
,
538 if (this->skip_call_tls_get_addr_
)
540 if (r_type
!= elfcpp::R_386_PLT32
542 || strcmp(gsym
->name(), "___tls_get_addr") != 0)
544 fprintf(stderr
, _("%s: %s: missing expected TLS relocation\n"),
546 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
550 this->skip_call_tls_get_addr_
= false;
557 case elfcpp::R_386_NONE
:
558 case elfcpp::R_386_GNU_VTINHERIT
:
559 case elfcpp::R_386_GNU_VTENTRY
:
562 case elfcpp::R_386_32
:
563 Relocate_functions
<32, false>::rel32(view
, value
);
566 case elfcpp::R_386_PC32
:
567 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
570 case elfcpp::R_386_16
:
571 Relocate_functions
<32, false>::rel16(view
, value
);
574 case elfcpp::R_386_PC16
:
575 Relocate_functions
<32, false>::pcrel16(view
, value
, address
);
578 case elfcpp::R_386_8
:
579 Relocate_functions
<32, false>::rel8(view
, value
);
582 case elfcpp::R_386_PC8
:
583 Relocate_functions
<32, false>::pcrel8(view
, value
, address
);
586 case elfcpp::R_386_PLT32
:
587 if (gsym
->is_resolved_locally())
588 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
590 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
592 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
596 case elfcpp::R_386_GOT32
:
597 // Local GOT offsets not yet supported.
599 assert(gsym
->has_got_offset());
600 value
= gsym
->got_offset();
601 Relocate_functions
<32, false>::rel32(view
, value
);
604 case elfcpp::R_386_GOTOFF
:
605 value
-= target
->got_section(NULL
, NULL
)->address();
606 Relocate_functions
<32, false>::rel32(view
, value
);
609 case elfcpp::R_386_GOTPC
:
610 value
= target
->got_section(NULL
, NULL
)->address();
611 Relocate_functions
<32, false>::pcrel32(view
, value
, address
);
614 case elfcpp::R_386_COPY
:
615 case elfcpp::R_386_GLOB_DAT
:
616 case elfcpp::R_386_JUMP_SLOT
:
617 case elfcpp::R_386_RELATIVE
:
618 case elfcpp::R_386_TLS_TPOFF
:
619 case elfcpp::R_386_TLS_DTPMOD32
:
620 case elfcpp::R_386_TLS_DTPOFF32
:
621 case elfcpp::R_386_TLS_TPOFF32
:
622 case elfcpp::R_386_TLS_DESC
:
623 fprintf(stderr
, _("%s: %s: unexpected reloc %u in object file\n"),
625 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
630 case elfcpp::R_386_TLS_IE
:
631 case elfcpp::R_386_TLS_GOTIE
:
632 case elfcpp::R_386_TLS_LE
:
633 case elfcpp::R_386_TLS_GD
:
634 case elfcpp::R_386_TLS_LDM
:
635 case elfcpp::R_386_TLS_LDO_32
:
636 case elfcpp::R_386_TLS_IE_32
:
637 case elfcpp::R_386_TLS_LE_32
:
638 case elfcpp::R_386_TLS_GOTDESC
:
639 case elfcpp::R_386_TLS_DESC_CALL
:
640 this->relocate_tls(relinfo
, relnum
, rel
, r_type
, gsym
, value
, view
,
644 case elfcpp::R_386_32PLT
:
645 case elfcpp::R_386_TLS_GD_32
:
646 case elfcpp::R_386_TLS_GD_PUSH
:
647 case elfcpp::R_386_TLS_GD_CALL
:
648 case elfcpp::R_386_TLS_GD_POP
:
649 case elfcpp::R_386_TLS_LDM_32
:
650 case elfcpp::R_386_TLS_LDM_PUSH
:
651 case elfcpp::R_386_TLS_LDM_CALL
:
652 case elfcpp::R_386_TLS_LDM_POP
:
653 case elfcpp::R_386_USED_BY_INTEL_200
:
655 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
657 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
666 // Perform a TLS relocation.
669 Target_i386::Relocate::relocate_tls(const Relocate_info
<32, false>* relinfo
,
671 const elfcpp::Rel
<32, false>& rel
,
673 const Sized_symbol
<32>* gsym
,
674 elfcpp::Elf_types
<32>::Elf_Addr value
,
676 elfcpp::Elf_types
<32>::Elf_Addr
,
679 Output_segment
* tls_segment
= relinfo
->layout
->tls_segment();
680 if (tls_segment
== NULL
)
682 fprintf(stderr
, _("%s: %s: TLS reloc but no TLS segment\n"),
684 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
688 const bool is_local
= gsym
== NULL
|| gsym
->is_resolved_locally();
689 const unsigned int opt_r_type
=
690 Target_i386::optimize_tls_reloc(relinfo
->options
, is_local
, r_type
);
693 case elfcpp::R_386_TLS_LE_32
:
694 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
695 Relocate_functions
<32, false>::rel32(view
, value
);
698 case elfcpp::R_386_TLS_LE
:
699 value
= value
- (tls_segment
->vaddr() + tls_segment
->memsz());
700 Relocate_functions
<32, false>::rel32(view
, value
);
703 case elfcpp::R_386_TLS_IE
:
704 case elfcpp::R_386_TLS_GOTIE
:
705 case elfcpp::R_386_TLS_IE_32
:
706 if (opt_r_type
== elfcpp::R_386_TLS_LE_32
)
708 Target_i386::Relocate::tls_ie_to_le(relinfo
, relnum
, tls_segment
,
709 rel
, r_type
, value
, view
,
713 fprintf(stderr
, _("%s: %s: unsupported reloc type %u\n"),
715 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
720 case elfcpp::R_386_TLS_GD
:
721 if (opt_r_type
== elfcpp::R_386_TLS_LE_32
)
723 this->tls_gd_to_le(relinfo
, relnum
, tls_segment
,
724 rel
, r_type
, value
, view
,
728 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
730 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
735 case elfcpp::R_386_TLS_LDM
:
736 case elfcpp::R_386_TLS_LDO_32
:
737 case elfcpp::R_386_TLS_GOTDESC
:
738 case elfcpp::R_386_TLS_DESC_CALL
:
739 fprintf(stderr
, _("%s: %s: unsupported reloc %u\n"),
741 relinfo
->location(relnum
, rel
.get_r_offset()).c_str(),
748 // Do a relocation in which we convert a TLS Initial-Exec to a
752 Target_i386::Relocate::tls_ie_to_le(const Relocate_info
<32, false>* relinfo
,
754 Output_segment
* tls_segment
,
755 const elfcpp::Rel
<32, false>& rel
,
757 elfcpp::Elf_types
<32>::Elf_Addr value
,
761 // We have to actually change the instructions, which means that we
762 // need to examine the opcodes to figure out which instruction we
764 if (r_type
== elfcpp::R_386_TLS_IE
)
766 // movl %gs:XX,%eax ==> movl $YY,%eax
767 // movl %gs:XX,%reg ==> movl $YY,%reg
768 // addl %gs:XX,%reg ==> addl $YY,%reg
769 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -1);
770 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
772 unsigned char op1
= view
[-1];
775 // movl XX,%eax ==> movl $YY,%eax
780 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
,
783 unsigned char op2
= view
[-2];
786 // movl XX,%reg ==> movl $YY,%reg
787 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
788 (op1
& 0xc7) == 0x05);
790 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
792 else if (op2
== 0x03)
794 // addl XX,%reg ==> addl $YY,%reg
795 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
796 (op1
& 0xc7) == 0x05);
798 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
801 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
806 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
807 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
808 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
809 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
810 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 4);
812 unsigned char op1
= view
[-1];
813 unsigned char op2
= view
[-2];
814 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
815 (op1
& 0xc0) == 0x80 && (op1
& 7) != 4);
818 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
820 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
822 else if (op2
== 0x2b)
824 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
826 view
[-1] = 0xe8 | ((op1
>> 3) & 7);
828 else if (op2
== 0x03)
830 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
832 view
[-1] = 0xc0 | ((op1
>> 3) & 7);
835 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
, 0);
838 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
839 if (r_type
== elfcpp::R_386_TLS_IE
|| r_type
== elfcpp::R_386_TLS_GOTIE
)
842 Relocate_functions
<32, false>::rel32(view
, value
);
845 // Do a relocation in which we convert a TLS Global-Dynamic to a
849 Target_i386::Relocate::tls_gd_to_le(const Relocate_info
<32, false>* relinfo
,
851 Output_segment
* tls_segment
,
852 const elfcpp::Rel
<32, false>& rel
,
854 elfcpp::Elf_types
<32>::Elf_Addr value
,
858 // leal foo(,%reg,1),%eax; call ___tls_get_addr
859 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
860 // leal foo(%reg),%eax; call ___tls_get_addr
861 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
863 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -2);
864 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, 9);
866 unsigned char op1
= view
[-1];
867 unsigned char op2
= view
[-2];
869 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
870 op2
== 0x8d || op2
== 0x04);
871 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
878 Target_i386::Relocate::check_range(relinfo
, relnum
, rel
, view_size
, -3);
879 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
881 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
882 ((op1
& 0xc7) == 0x05
883 && op1
!= (4 << 3)));
884 memcpy(view
- 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
888 Target_i386::Relocate::check_tls(relinfo
, relnum
, rel
,
889 (op1
& 0xf8) == 0x80 && (op1
& 7) != 4);
890 if (rel
.get_r_offset() + 9 < view_size
&& view
[9] == 0x90)
892 // There is a trailing nop. Use the size byte subl.
893 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
898 // Use the five byte subl.
899 memcpy(view
- 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
903 value
= tls_segment
->vaddr() + tls_segment
->memsz() - value
;
904 Relocate_functions
<32, false>::rel32(view
+ roff
, value
);
906 // The next reloc should be a PLT32 reloc against __tls_get_addr.
908 this->skip_call_tls_get_addr_
= true;
911 // Check the range for a TLS relocation.
914 Target_i386::Relocate::check_range(const Relocate_info
<32, false>* relinfo
,
916 const elfcpp::Rel
<32, false>& rel
,
917 off_t view_size
, off_t off
)
919 off_t offset
= rel
.get_r_offset() + off
;
920 if (offset
< 0 || offset
> view_size
)
922 fprintf(stderr
, _("%s: %s: TLS relocation out of range\n"),
924 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
929 // Check the validity of a TLS relocation. This is like assert.
932 Target_i386::Relocate::check_tls(const Relocate_info
<32, false>* relinfo
,
934 const elfcpp::Rel
<32, false>& rel
,
940 _("%s: %s: TLS relocation against invalid instruction\n"),
942 relinfo
->location(relnum
, rel
.get_r_offset()).c_str());
947 // Relocate section data.
950 Target_i386::relocate_section(const Relocate_info
<32, false>* relinfo
,
951 unsigned int sh_type
,
952 const unsigned char* prelocs
,
955 elfcpp::Elf_types
<32>::Elf_Addr address
,
958 assert(sh_type
== elfcpp::SHT_REL
);
960 gold::relocate_section
<32, false, Target_i386
, elfcpp::SHT_REL
,
961 Target_i386::Relocate
>(
971 // The selector for i386 object files.
973 class Target_selector_i386
: public Target_selector
976 Target_selector_i386()
977 : Target_selector(elfcpp::EM_386
, 32, false)
981 recognize(int machine
, int osabi
, int abiversion
);
984 Target_i386
* target_
;
987 // Recognize an i386 object file when we already know that the machine
991 Target_selector_i386::recognize(int, int, int)
993 if (this->target_
== NULL
)
994 this->target_
= new Target_i386();
995 return this->target_
;
998 Target_selector_i386 target_selector_i386
;
1000 } // End anonymous namespace.