* reply_mig_hack.awk: Check for `auto const mach_msg_type_t'
[deliverable/binutils-gdb.git] / gold / i386.cc
CommitLineData
14bfc3f5
ILT
1// i386.cc -- i386 target support for gold.
2
3#include "gold.h"
ead1e424
ILT
4
5#include <cstring>
6
14bfc3f5 7#include "elfcpp.h"
92e059d8 8#include "reloc.h"
61ba1cf9
ILT
9#include "i386.h"
10#include "object.h"
ead1e424 11#include "symtab.h"
92e059d8
ILT
12#include "layout.h"
13#include "output.h"
14bfc3f5 14#include "target.h"
61ba1cf9 15#include "target-reloc.h"
14bfc3f5
ILT
16#include "target-select.h"
17
18namespace
19{
20
21using namespace gold;
22
a3ad94ed
ILT
23class Output_data_plt_i386;
24
14bfc3f5
ILT
25// The i386 target class.
26
27class Target_i386 : public Sized_target<32, false>
28{
29 public:
5a6f7e2d
ILT
30 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
31
14bfc3f5 32 Target_i386()
ead1e424 33 : Sized_target<32, false>(&i386_info),
5a6f7e2d
ILT
34 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
35 copy_relocs_(NULL), dynbss_(NULL)
14bfc3f5 36 { }
75f65a3e 37
92e059d8 38 // Scan the relocations to look for symbol adjustments.
61ba1cf9 39 void
92e059d8
ILT
40 scan_relocs(const General_options& options,
41 Symbol_table* symtab,
ead1e424 42 Layout* layout,
f6ce93d6 43 Sized_relobj<32, false>* object,
a3ad94ed 44 unsigned int data_shndx,
92e059d8
ILT
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);
61ba1cf9 51
5a6f7e2d
ILT
52 // Finalize the sections.
53 void
16649710 54 do_finalize_sections(const General_options*, Layout*);
5a6f7e2d 55
92e059d8
ILT
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);
65
66 private:
67 // The class which scans relocations.
68 struct Scan
61ba1cf9
ILT
69 {
70 inline void
ead1e424
ILT
71 local(const General_options& options, Symbol_table* symtab,
72 Layout* layout, Target_i386* target,
f6ce93d6 73 Sized_relobj<32, false>* object,
a3ad94ed 74 unsigned int data_shndx,
92e059d8
ILT
75 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
76 const elfcpp::Sym<32, false>& lsym);
61ba1cf9 77
92e059d8 78 inline void
ead1e424
ILT
79 global(const General_options& options, Symbol_table* symtab,
80 Layout* layout, Target_i386* target,
f6ce93d6 81 Sized_relobj<32, false>* object,
a3ad94ed 82 unsigned int data_shndx,
92e059d8
ILT
83 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
84 Symbol* gsym);
61ba1cf9
ILT
85 };
86
92e059d8
ILT
87 // The class which implements relocation.
88 class Relocate
89 {
90 public:
ead1e424
ILT
91 Relocate()
92 : skip_call_tls_get_addr_(false)
93 { }
94
95 ~Relocate()
96 {
97 if (this->skip_call_tls_get_addr_)
98 {
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);
103 }
104 }
105
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,
92e059d8 110 const elfcpp::Rel<32, false>&,
c06b7b0b 111 unsigned int r_type, const Sized_symbol<32>*,
92e059d8
ILT
112 elfcpp::Elf_types<32>::Elf_Addr,
113 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
114 off_t);
115
116 private:
117 // Do a TLS relocation.
ead1e424 118 inline void
92e059d8
ILT
119 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
120 const elfcpp::Rel<32, false>&,
c06b7b0b 121 unsigned int r_type, const Sized_symbol<32>*,
92e059d8
ILT
122 elfcpp::Elf_types<32>::Elf_Addr,
123 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
124
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);
133
ead1e424
ILT
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);
142
92e059d8
ILT
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);
147
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);
ead1e424
ILT
152
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_;
92e059d8
ILT
156 };
157
158 // Adjust TLS relocation type based on the options and whether this
159 // is a local symbol.
160 static unsigned int
a3ad94ed 161 optimize_tls_reloc(const General_options*, bool is_final, int r_type);
92e059d8 162
ead1e424 163 // Get the GOT section, creating it if necessary.
dbe717ef 164 Output_data_got<32, false>*
a3ad94ed
ILT
165 got_section(const General_options*, Symbol_table*, Layout*);
166
167 // Create a PLT entry for a global symbol.
168 void
169 make_plt_entry(const General_options* options, Symbol_table*,
170 Layout*, Symbol*);
171
172 // Get the PLT section.
173 Output_data_plt_i386*
174 plt_section() const
175 {
176 gold_assert(this->plt_ != NULL);
177 return this->plt_;
178 }
179
5a6f7e2d
ILT
180 // Get the dynamic reloc section, creating it if necessary.
181 Reloc_section*
182 rel_dyn_section(Layout*);
183
a3ad94ed
ILT
184 // Copy a relocation against a global symbol.
185 void
5a6f7e2d
ILT
186 copy_reloc(const General_options*, Symbol_table*, Layout*,
187 Sized_relobj<32, false>*, unsigned int,
a3ad94ed 188 Symbol*, const elfcpp::Rel<32, false>&);
ead1e424 189
92e059d8
ILT
190 // Information about this specific target which we pass to the
191 // general Target structure.
75f65a3e 192 static const Target::Target_info i386_info;
ead1e424
ILT
193
194 // The GOT section.
dbe717ef 195 Output_data_got<32, false>* got_;
a3ad94ed
ILT
196 // The PLT section.
197 Output_data_plt_i386* plt_;
198 // The GOT PLT section.
199 Output_data_space* got_plt_;
5a6f7e2d
ILT
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_;
75f65a3e
ILT
206};
207
208const Target::Target_info Target_i386::i386_info =
209{
61ba1cf9
ILT
210 32, // size
211 false, // is_big_endian
212 elfcpp::EM_386, // machine_code
213 false, // has_make_symbol
dbe717ef
ILT
214 false, // has_resolve
215 "/usr/lib/libc.so.1", // dynamic_linker
216 0x08048000, // text_segment_address
61ba1cf9
ILT
217 0x1000, // abi_pagesize
218 0x1000 // common_pagesize
14bfc3f5
ILT
219};
220
ead1e424
ILT
221// Get the GOT section, creating it if necessary.
222
dbe717ef 223Output_data_got<32, false>*
a3ad94ed
ILT
224Target_i386::got_section(const General_options* options, Symbol_table* symtab,
225 Layout* layout)
ead1e424
ILT
226{
227 if (this->got_ == NULL)
228 {
a3ad94ed
ILT
229 gold_assert(options != NULL && symtab != NULL && layout != NULL);
230
231 this->got_ = new Output_data_got<32, false>(options);
ead1e424 232
ead1e424 233 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
16649710
ILT
234 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
235 this->got_);
ead1e424 236
a3ad94ed
ILT
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,
16649710
ILT
243 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
244 this->got_plt_);
a3ad94ed 245
ead1e424 246 // The first three entries are reserved.
a3ad94ed 247 this->got_plt_->set_space_size(3 * 4);
ead1e424 248
a3ad94ed 249 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
14b31740 250 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
a3ad94ed 251 this->got_plt_,
ead1e424 252 0, 0, elfcpp::STT_OBJECT,
16649710 253 elfcpp::STB_LOCAL,
ead1e424
ILT
254 elfcpp::STV_HIDDEN, 0,
255 false, false);
256 }
a3ad94ed 257
ead1e424
ILT
258 return this->got_;
259}
260
5a6f7e2d
ILT
261// Get the dynamic reloc section, creating it if necessary.
262
263Target_i386::Reloc_section*
264Target_i386::rel_dyn_section(Layout* layout)
265{
266 if (this->rel_dyn_ == NULL)
267 {
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_);
272 }
273 return this->rel_dyn_;
274}
275
a3ad94ed
ILT
276// A class to handle the PLT data.
277
278class Output_data_plt_i386 : public Output_section_data
279{
280 public:
281 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
282
283 Output_data_plt_i386(Layout*, Output_data_space*, bool is_shared);
284
285 // Add an entry to the PLT.
286 void
287 add_entry(Symbol* gsym);
288
16649710
ILT
289 // Return the .rel.plt section data.
290 const Reloc_section*
291 rel_plt() const
292 { return this->rel_; }
293
294 protected:
295 void
296 do_adjust_output_section(Output_section* os);
297
a3ad94ed
ILT
298 private:
299 // The size of an entry in the PLT.
300 static const int plt_entry_size = 16;
301
302 // The first entry in the PLT for an executable.
303 static unsigned char exec_first_plt_entry[plt_entry_size];
304
305 // The first entry in the PLT for a shared object.
306 static unsigned char dyn_first_plt_entry[plt_entry_size];
307
308 // Other entries in the PLT for an executable.
309 static unsigned char exec_plt_entry[plt_entry_size];
310
311 // Other entries in the PLT for a shared object.
312 static unsigned char dyn_plt_entry[plt_entry_size];
313
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); }
318
319 // Write out the PLT data.
320 void
321 do_write(Output_file*);
322
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_;
331};
332
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.
336
337Output_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)
341{
342 this->rel_ = new Reloc_section();
343 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
344 elfcpp::SHF_ALLOC, this->rel_);
345}
346
16649710
ILT
347// For some reason
348
349void
350Output_data_plt_i386::do_adjust_output_section(Output_section* os)
351{
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);
355}
356
a3ad94ed
ILT
357// Add an entry to the PLT.
358
359void
360Output_data_plt_i386::add_entry(Symbol* gsym)
361{
362 gold_assert(!gsym->has_plt_offset());
363
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);
367
368 ++this->count_;
369
370 off_t got_offset = this->got_plt_->data_size();
371
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);
376
377 // Every PLT entry needs a reloc.
16649710 378 gsym->set_needs_dynsym_entry();
a3ad94ed
ILT
379 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
380 got_offset);
381
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.
385}
386
387// The first entry in the PLT for an executable.
388
389unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
390{
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
396};
397
398// The first entry in the PLT for a shared object.
399
400unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
401{
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
405};
406
407// Subsequent entries in the PLT for an executable.
408
409unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
410{
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
417};
418
419// Subsequent entries in the PLT for a shared object.
420
421unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
422{
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
429};
430
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.
434
435void
436Output_data_plt_i386::do_write(Output_file* of)
437{
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);
441
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);
446
447 unsigned char* pov = oview;
448
449 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
450 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
451
452 if (this->is_shared_)
453 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
454 else
455 {
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);
459 }
460 pov += plt_entry_size;
461
462 unsigned char* got_pov = got_view;
463
464 memset(got_pov, 0, 12);
465 got_pov += 12;
466
467 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
468
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)
481 {
482 // Set and adjust the PLT entry itself.
483
484 if (this->is_shared_)
485 {
486 memcpy(pov, dyn_plt_entry, plt_entry_size);
487 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
488 }
489 else
490 {
491 memcpy(pov, exec_plt_entry, plt_entry_size);
492 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
493 (got_address
494 + got_offset));
495 }
496
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));
500
501 // Set the entry in the GOT.
502 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
503 }
504
505 gold_assert(pov - oview == oview_size);
506 gold_assert(got_pov - got_view == got_size);
507
508 of->write_output_view(offset, oview_size, oview);
509 of->write_output_view(got_file_offset, got_size, got_view);
510}
511
512// Create a PLT entry for a global symbol.
513
514void
515Target_i386::make_plt_entry(const General_options* options,
516 Symbol_table* symtab, Layout* layout, Symbol* gsym)
517{
518 if (gsym->has_plt_offset())
519 return;
520
521 if (this->plt_ == NULL)
522 {
523 // Create the GOT sections first.
524 this->got_section(options, symtab, layout);
525
526 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
527 options->is_shared());
16649710
ILT
528 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
529 (elfcpp::SHF_ALLOC
530 | elfcpp::SHF_EXECINSTR),
531 this->plt_);
a3ad94ed
ILT
532 }
533
534 this->plt_->add_entry(gsym);
535}
536
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.
552
553void
554Target_i386::copy_reloc(const General_options* options,
5a6f7e2d
ILT
555 Symbol_table* symtab,
556 Layout* layout,
a3ad94ed
ILT
557 Sized_relobj<32, false>* object,
558 unsigned int data_shndx, Symbol* gsym,
5a6f7e2d 559 const elfcpp::Rel<32, false>& rel)
a3ad94ed 560{
5a6f7e2d
ILT
561 Sized_symbol<32>* ssym;
562 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
563 SELECT_SIZE(32));
564
565 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
566 data_shndx, ssym))
a3ad94ed
ILT
567 {
568 // So far we do not need a COPY reloc. Save this relocation.
5a6f7e2d
ILT
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);
a3ad94ed 574 }
5a6f7e2d
ILT
575 else
576 {
577 // Allocate space for this symbol in the .bss section.
578
579 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
580
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;
a3ad94ed 588
5a6f7e2d
ILT
589 if (this->dynbss_ == NULL)
590 {
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_);
597 }
598
599 Output_data_space* dynbss = this->dynbss_;
600
601 if (align > dynbss->addralign())
602 dynbss->set_space_alignment(align);
603
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);
608
609 // Define the symbol in the .dynbss section.
14b31740
ILT
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);
5a6f7e2d
ILT
614
615 // Add the COPY reloc.
16649710 616 ssym->set_needs_dynsym_entry();
5a6f7e2d
ILT
617 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
618 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
619 }
a3ad94ed
ILT
620}
621
92e059d8 622// Optimize the TLS relocation type based on what we know about the
a3ad94ed
ILT
623// symbol. IS_FINAL is true if the final address of this symbol is
624// known at link time.
92e059d8
ILT
625
626unsigned int
a3ad94ed
ILT
627Target_i386::optimize_tls_reloc(const General_options* options,
628 bool is_final,
92e059d8
ILT
629 int r_type)
630{
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;
635
636 switch (r_type)
637 {
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.
a3ad94ed 645 if (is_final)
92e059d8
ILT
646 return elfcpp::R_386_TLS_LE_32;
647 return elfcpp::R_386_TLS_IE_32;
648
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;
654
655 case elfcpp::R_386_TLS_LDO_32:
656 // Another type of Local-Dynamic relocation.
657 return elfcpp::R_386_TLS_LE;
658
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.
a3ad94ed 666 if (is_final)
92e059d8
ILT
667 return elfcpp::R_386_TLS_LE_32;
668 return r_type;
669
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;
675
676 default:
a3ad94ed 677 gold_unreachable();
92e059d8
ILT
678 }
679}
680
681// Scan a relocation for a local symbol.
682
683inline void
684Target_i386::Scan::local(const General_options& options,
ead1e424
ILT
685 Symbol_table* symtab,
686 Layout* layout,
687 Target_i386* target,
f6ce93d6 688 Sized_relobj<32, false>* object,
a3ad94ed
ILT
689 unsigned int,
690 const elfcpp::Rel<32, false>&,
691 unsigned int r_type,
92e059d8
ILT
692 const elfcpp::Sym<32, false>&)
693{
694 switch (r_type)
695 {
696 case elfcpp::R_386_NONE:
697 case elfcpp::R_386_GNU_VTINHERIT:
698 case elfcpp::R_386_GNU_VTENTRY:
699 break;
700
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.
a3ad94ed 706 gold_assert(!options.is_shared());
92e059d8
ILT
707 break;
708
709 case elfcpp::R_386_PC32:
710 case elfcpp::R_386_PC16:
711 case elfcpp::R_386_PC8:
712 break;
713
ead1e424
ILT
714 case elfcpp::R_386_GOTOFF:
715 case elfcpp::R_386_GOTPC:
716 // We need a GOT section.
a3ad94ed 717 target->got_section(&options, symtab, layout);
ead1e424
ILT
718 break;
719
92e059d8
ILT
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;
733
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:
a3ad94ed
ILT
744 r_type = Target_i386::optimize_tls_reloc(&options,
745 !options.is_shared(),
746 r_type);
92e059d8
ILT
747 switch (r_type)
748 {
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.
a3ad94ed 753 gold_assert(!options.is_shared());
92e059d8
ILT
754 break;
755
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;
768 }
769 break;
770
771 case elfcpp::R_386_GOT32:
772 case elfcpp::R_386_PLT32:
92e059d8
ILT
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;
787 }
788}
789
790// Scan a relocation for a global symbol.
791
792inline void
793Target_i386::Scan::global(const General_options& options,
ead1e424
ILT
794 Symbol_table* symtab,
795 Layout* layout,
796 Target_i386* target,
f6ce93d6 797 Sized_relobj<32, false>* object,
a3ad94ed
ILT
798 unsigned int data_shndx,
799 const elfcpp::Rel<32, false>& reloc,
800 unsigned int r_type,
92e059d8
ILT
801 Symbol* gsym)
802{
803 switch (r_type)
804 {
805 case elfcpp::R_386_NONE:
806 case elfcpp::R_386_GNU_VTINHERIT:
807 case elfcpp::R_386_GNU_VTENTRY:
808 break;
809
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.
a3ad94ed
ILT
820 gold_assert(!options.is_shared());
821
14b31740 822 if (gsym->is_from_dynobj())
a3ad94ed
ILT
823 {
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
5a6f7e2d
ILT
830 target->copy_reloc(&options, symtab, layout, object, data_shndx,
831 gsym, reloc);
a3ad94ed
ILT
832 }
833
92e059d8
ILT
834 break;
835
ead1e424
ILT
836 case elfcpp::R_386_GOT32:
837 // The symbol requires a GOT entry.
a3ad94ed 838 if (target->got_section(&options, symtab, layout)->add_global(gsym))
ead1e424 839 {
a3ad94ed 840 // If this symbol is not fully resolved, we need to add a
ead1e424 841 // dynamic relocation for it.
a3ad94ed
ILT
842 if (!gsym->final_value_is_known(&options))
843 gold_unreachable();
ead1e424
ILT
844 }
845 break;
846
847 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
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))
ead1e424 851 break;
a3ad94ed 852 target->make_plt_entry(&options, symtab, layout, gsym);
ead1e424
ILT
853 break;
854
855 case elfcpp::R_386_GOTOFF:
856 case elfcpp::R_386_GOTPC:
857 // We need a GOT section.
a3ad94ed 858 target->got_section(&options, symtab, layout);
ead1e424
ILT
859 break;
860
92e059d8
ILT
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;
874
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:
a3ad94ed
ILT
885 {
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)
889 {
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;
896
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;
911 }
912 }
92e059d8
ILT
913 break;
914
92e059d8
ILT
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;
930 }
931}
932
933// Scan relocations for a section.
934
935void
936Target_i386::scan_relocs(const General_options& options,
937 Symbol_table* symtab,
ead1e424 938 Layout* layout,
f6ce93d6 939 Sized_relobj<32, false>* object,
a3ad94ed 940 unsigned int data_shndx,
92e059d8
ILT
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)
947{
948 if (sh_type == elfcpp::SHT_RELA)
949 {
950 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
951 program_name, object->name().c_str());
952 gold_exit(false);
953 }
954
ead1e424
ILT
955 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
956 Target_i386::Scan>(
92e059d8
ILT
957 options,
958 symtab,
ead1e424
ILT
959 layout,
960 this,
92e059d8 961 object,
a3ad94ed 962 data_shndx,
92e059d8
ILT
963 prelocs,
964 reloc_count,
965 local_symbol_count,
966 plocal_symbols,
967 global_symbols);
968}
969
16649710 970// Finalize the sections.
5a6f7e2d
ILT
971
972void
16649710
ILT
973Target_i386::do_finalize_sections(const General_options* options,
974 Layout* layout)
5a6f7e2d 975{
16649710
ILT
976 // Fill in some more dynamic tags.
977 Output_data_dynamic* const odyn = layout->dynamic_data();
978 if (odyn != NULL)
979 {
980 if (this->got_plt_ != NULL)
981 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
982
983 if (this->plt_ != NULL)
984 {
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);
989 }
990
991 if (this->rel_dyn_ != NULL)
992 {
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);
998 }
999
1000 if (!options->is_shared())
1001 {
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);
1005 }
1006 }
1007
1008 // Emit any relocs we saved in an attempt to avoid generating COPY
1009 // relocs.
5a6f7e2d
ILT
1010 if (this->copy_relocs_ == NULL)
1011 return;
1012 if (this->copy_relocs_->any_to_emit())
1013 {
1014 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1015 this->copy_relocs_->emit(rel_dyn);
1016 }
1017 delete this->copy_relocs_;
1018 this->copy_relocs_ = NULL;
1019}
1020
61ba1cf9
ILT
1021// Perform a relocation.
1022
ead1e424 1023inline bool
92e059d8 1024Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
ead1e424 1025 Target_i386* target,
92e059d8
ILT
1026 size_t relnum,
1027 const elfcpp::Rel<32, false>& rel,
1028 unsigned int r_type,
c06b7b0b 1029 const Sized_symbol<32>* gsym,
92e059d8
ILT
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)
61ba1cf9 1034{
ead1e424
ILT
1035 if (this->skip_call_tls_get_addr_)
1036 {
1037 if (r_type != elfcpp::R_386_PLT32
1038 || gsym == NULL
1039 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1040 {
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);
1045 }
1046
1047 this->skip_call_tls_get_addr_ = false;
1048
1049 return false;
1050 }
1051
a3ad94ed 1052 // Pick the value to use for symbols defined in shared objects.
14b31740 1053 if (gsym != NULL && gsym->is_from_dynobj())
a3ad94ed
ILT
1054 {
1055 if (gsym->has_plt_offset())
16649710 1056 value = target->plt_section()->address() + gsym->plt_offset();
a3ad94ed
ILT
1057 else
1058 gold_unreachable();
1059 }
1060
61ba1cf9
ILT
1061 switch (r_type)
1062 {
1063 case elfcpp::R_386_NONE:
92e059d8
ILT
1064 case elfcpp::R_386_GNU_VTINHERIT:
1065 case elfcpp::R_386_GNU_VTENTRY:
61ba1cf9
ILT
1066 break;
1067
1068 case elfcpp::R_386_32:
92e059d8 1069 Relocate_functions<32, false>::rel32(view, value);
61ba1cf9
ILT
1070 break;
1071
1072 case elfcpp::R_386_PC32:
92e059d8
ILT
1073 Relocate_functions<32, false>::pcrel32(view, value, address);
1074 break;
1075
1076 case elfcpp::R_386_16:
1077 Relocate_functions<32, false>::rel16(view, value);
1078 break;
1079
1080 case elfcpp::R_386_PC16:
1081 Relocate_functions<32, false>::pcrel16(view, value, address);
61ba1cf9
ILT
1082 break;
1083
92e059d8
ILT
1084 case elfcpp::R_386_8:
1085 Relocate_functions<32, false>::rel8(view, value);
1086 break;
1087
1088 case elfcpp::R_386_PC8:
1089 Relocate_functions<32, false>::pcrel8(view, value, address);
1090 break;
1091
ead1e424 1092 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
1093 gold_assert(gsym->has_plt_offset()
1094 || gsym->final_value_is_known(relinfo->options));
1095 Relocate_functions<32, false>::pcrel32(view, value, address);
ead1e424
ILT
1096 break;
1097
1098 case elfcpp::R_386_GOT32:
1099 // Local GOT offsets not yet supported.
a3ad94ed
ILT
1100 gold_assert(gsym);
1101 gold_assert(gsym->has_got_offset());
ead1e424
ILT
1102 value = gsym->got_offset();
1103 Relocate_functions<32, false>::rel32(view, value);
1104 break;
1105
1106 case elfcpp::R_386_GOTOFF:
a3ad94ed 1107 value -= target->got_section(NULL, NULL, NULL)->address();
ead1e424
ILT
1108 Relocate_functions<32, false>::rel32(view, value);
1109 break;
1110
1111 case elfcpp::R_386_GOTPC:
a3ad94ed 1112 value = target->got_section(NULL, NULL, NULL)->address();
ead1e424
ILT
1113 Relocate_functions<32, false>::pcrel32(view, value, address);
1114 break;
1115
92e059d8
ILT
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;
1131
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:
ead1e424
ILT
1142 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, value, view,
1143 address, view_size);
92e059d8
ILT
1144 break;
1145
92e059d8
ILT
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:
61ba1cf9
ILT
1156 default:
1157 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
92e059d8
ILT
1158 program_name,
1159 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1160 r_type);
61ba1cf9 1161 // gold_exit(false);
92e059d8
ILT
1162 break;
1163 }
ead1e424
ILT
1164
1165 return true;
92e059d8
ILT
1166}
1167
1168// Perform a TLS relocation.
1169
1170inline void
1171Target_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,
c06b7b0b 1175 const Sized_symbol<32>* gsym,
92e059d8
ILT
1176 elfcpp::Elf_types<32>::Elf_Addr value,
1177 unsigned char* view,
1178 elfcpp::Elf_types<32>::Elf_Addr,
1179 off_t view_size)
1180{
1181 Output_segment* tls_segment = relinfo->layout->tls_segment();
1182 if (tls_segment == NULL)
1183 {
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);
1188 }
1189
a3ad94ed
ILT
1190 const bool is_final = (gsym == NULL
1191 ? !relinfo->options->is_shared()
1192 : gsym->final_value_is_known(relinfo->options));
92e059d8 1193 const unsigned int opt_r_type =
a3ad94ed 1194 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
92e059d8
ILT
1195 switch (r_type)
1196 {
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;
1201
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;
1206
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)
1211 {
1212 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1213 rel, r_type, value, view,
1214 view_size);
1215 break;
1216 }
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;
1223
1224 case elfcpp::R_386_TLS_GD:
ead1e424
ILT
1225 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1226 {
1227 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1228 rel, r_type, value, view,
1229 view_size);
1230 break;
1231 }
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;
1238
92e059d8
ILT
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;
1249 }
1250}
1251
1252// Do a relocation in which we convert a TLS Initial-Exec to a
1253// Local-Exec.
1254
1255inline void
1256Target_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)
1264{
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)
1269 {
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);
1275
1276 unsigned char op1 = view[-1];
1277 if (op1 == 0xa1)
1278 {
1279 // movl XX,%eax ==> movl $YY,%eax
1280 view[-1] = 0xb8;
1281 }
1282 else
1283 {
1284 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1285 view_size, -2);
1286
1287 unsigned char op2 = view[-2];
1288 if (op2 == 0x8b)
1289 {
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);
1295 }
1296 else if (op2 == 0x03)
1297 {
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);
1303 }
1304 else
1305 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1306 }
1307 }
1308 else
1309 {
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);
1315
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)
1321 {
1322 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1323 view[-2] = 0xc7;
1324 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1325 }
1326 else if (op2 == 0x2b)
1327 {
1328 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1329 view[-2] = 0x81;
1330 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1331 }
1332 else if (op2 == 0x03)
1333 {
1334 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1335 view[-2] = 0x81;
1336 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1337 }
1338 else
1339 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1340 }
1341
ead1e424
ILT
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;
92e059d8
ILT
1345
1346 Relocate_functions<32, false>::rel32(view, value);
1347}
1348
ead1e424
ILT
1349// Do a relocation in which we convert a TLS Global-Dynamic to a
1350// Local-Exec.
1351
1352inline void
1353Target_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)
1361{
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
1366
1367 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1368 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1369
1370 unsigned char op1 = view[-1];
1371 unsigned char op2 = view[-2];
1372
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);
1377
1378 int roff = 5;
1379
1380 if (op2 == 0x04)
1381 {
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);
1389 }
1390 else
1391 {
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)
1395 {
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;
1399 }
1400 else
1401 {
1402 // Use the five byte subl.
1403 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1404 }
1405 }
1406
1407 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1408 Relocate_functions<32, false>::rel32(view + roff, value);
1409
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;
1413}
1414
92e059d8
ILT
1415// Check the range for a TLS relocation.
1416
1417inline void
1418Target_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)
1422{
1423 off_t offset = rel.get_r_offset() + off;
1424 if (offset < 0 || offset > view_size)
1425 {
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);
1430 }
1431}
1432
1433// Check the validity of a TLS relocation. This is like assert.
1434
1435inline void
1436Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1437 size_t relnum,
1438 const elfcpp::Rel<32, false>& rel,
1439 bool valid)
1440{
1441 if (!valid)
1442 {
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);
61ba1cf9
ILT
1448 }
1449}
1450
1451// Relocate section data.
1452
1453void
92e059d8 1454Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
1455 unsigned int sh_type,
1456 const unsigned char* prelocs,
1457 size_t reloc_count,
61ba1cf9
ILT
1458 unsigned char* view,
1459 elfcpp::Elf_types<32>::Elf_Addr address,
1460 off_t view_size)
1461{
a3ad94ed 1462 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 1463
ead1e424
ILT
1464 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1465 Target_i386::Relocate>(
92e059d8 1466 relinfo,
ead1e424 1467 this,
61ba1cf9
ILT
1468 prelocs,
1469 reloc_count,
61ba1cf9
ILT
1470 view,
1471 address,
1472 view_size);
1473}
1474
14bfc3f5
ILT
1475// The selector for i386 object files.
1476
1477class Target_selector_i386 : public Target_selector
1478{
1479public:
1480 Target_selector_i386()
1481 : Target_selector(elfcpp::EM_386, 32, false)
1482 { }
1483
1484 Target*
ead1e424
ILT
1485 recognize(int machine, int osabi, int abiversion);
1486
1487 private:
1488 Target_i386* target_;
14bfc3f5
ILT
1489};
1490
1491// Recognize an i386 object file when we already know that the machine
1492// number is EM_386.
1493
1494Target*
ead1e424 1495Target_selector_i386::recognize(int, int, int)
14bfc3f5 1496{
ead1e424
ILT
1497 if (this->target_ == NULL)
1498 this->target_ = new Target_i386();
1499 return this->target_;
14bfc3f5
ILT
1500}
1501
1502Target_selector_i386 target_selector_i386;
1503
1504} // End anonymous namespace.
This page took 0.111301 seconds and 4 git commands to generate.