daily update
[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>*,
b8e6aad9 112 const Symbol_value<32>*,
92e059d8
ILT
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>*,
b8e6aad9 122 const Symbol_value<32>*,
92e059d8
ILT
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;
8462ae85 669
92e059d8
ILT
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:
8462ae85 807 case elfcpp::R_386_GNU_VTENTRY:
92e059d8
ILT
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 836 case elfcpp::R_386_GOT32:
8462ae85
ILT
837 {
838 // The symbol requires a GOT entry.
839 Output_data_got<32, false>* got = target->got_section(&options, symtab,
840 layout);
841 if (got->add_global(gsym))
842 {
843 // If this symbol is not fully resolved, we need to add a
844 // dynamic relocation for it.
845 if (!gsym->final_value_is_known(&options))
846 {
847 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
848 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
849 gsym->got_offset());
850 }
851 }
852 }
ead1e424
ILT
853 break;
854
855 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
856 // If the symbol is fully resolved, this is just a PC32 reloc.
857 // Otherwise we need a PLT entry.
858 if (gsym->final_value_is_known(&options))
ead1e424 859 break;
a3ad94ed 860 target->make_plt_entry(&options, symtab, layout, gsym);
ead1e424
ILT
861 break;
862
863 case elfcpp::R_386_GOTOFF:
864 case elfcpp::R_386_GOTPC:
865 // We need a GOT section.
a3ad94ed 866 target->got_section(&options, symtab, layout);
ead1e424
ILT
867 break;
868
92e059d8
ILT
869 case elfcpp::R_386_COPY:
870 case elfcpp::R_386_GLOB_DAT:
871 case elfcpp::R_386_JUMP_SLOT:
872 case elfcpp::R_386_RELATIVE:
873 case elfcpp::R_386_TLS_TPOFF:
874 case elfcpp::R_386_TLS_DTPMOD32:
875 case elfcpp::R_386_TLS_DTPOFF32:
876 case elfcpp::R_386_TLS_TPOFF32:
877 case elfcpp::R_386_TLS_DESC:
878 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
879 program_name, object->name().c_str(), r_type);
880 gold_exit(false);
881 break;
882
883 case elfcpp::R_386_TLS_IE:
884 case elfcpp::R_386_TLS_GOTIE:
885 case elfcpp::R_386_TLS_LE:
886 case elfcpp::R_386_TLS_GD:
887 case elfcpp::R_386_TLS_LDM:
888 case elfcpp::R_386_TLS_LDO_32:
889 case elfcpp::R_386_TLS_IE_32:
890 case elfcpp::R_386_TLS_LE_32:
891 case elfcpp::R_386_TLS_GOTDESC:
892 case elfcpp::R_386_TLS_DESC_CALL:
a3ad94ed
ILT
893 {
894 const bool is_final = gsym->final_value_is_known(&options);
895 r_type = Target_i386::optimize_tls_reloc(&options, is_final, r_type);
896 switch (r_type)
897 {
898 case elfcpp::R_386_TLS_LE:
899 case elfcpp::R_386_TLS_LE_32:
900 // FIXME: If generating a shared object, we need to copy
901 // this relocation into the object.
902 gold_assert(!options.is_shared());
903 break;
904
905 case elfcpp::R_386_TLS_IE:
906 case elfcpp::R_386_TLS_GOTIE:
907 case elfcpp::R_386_TLS_GD:
908 case elfcpp::R_386_TLS_LDM:
909 case elfcpp::R_386_TLS_LDO_32:
910 case elfcpp::R_386_TLS_IE_32:
911 case elfcpp::R_386_TLS_GOTDESC:
912 case elfcpp::R_386_TLS_DESC_CALL:
913 fprintf(stderr,
914 _("%s: %s: unsupported reloc %u "
915 "against global symbol %s\n"),
916 program_name, object->name().c_str(), r_type,
917 gsym->name());
918 break;
919 }
920 }
92e059d8
ILT
921 break;
922
92e059d8
ILT
923 case elfcpp::R_386_32PLT:
924 case elfcpp::R_386_TLS_GD_32:
925 case elfcpp::R_386_TLS_GD_PUSH:
926 case elfcpp::R_386_TLS_GD_CALL:
927 case elfcpp::R_386_TLS_GD_POP:
928 case elfcpp::R_386_TLS_LDM_32:
929 case elfcpp::R_386_TLS_LDM_PUSH:
930 case elfcpp::R_386_TLS_LDM_CALL:
931 case elfcpp::R_386_TLS_LDM_POP:
932 case elfcpp::R_386_USED_BY_INTEL_200:
933 default:
934 fprintf(stderr,
935 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
936 program_name, object->name().c_str(), r_type, gsym->name());
937 break;
938 }
939}
940
941// Scan relocations for a section.
942
943void
944Target_i386::scan_relocs(const General_options& options,
945 Symbol_table* symtab,
ead1e424 946 Layout* layout,
f6ce93d6 947 Sized_relobj<32, false>* object,
a3ad94ed 948 unsigned int data_shndx,
92e059d8
ILT
949 unsigned int sh_type,
950 const unsigned char* prelocs,
951 size_t reloc_count,
952 size_t local_symbol_count,
953 const unsigned char* plocal_symbols,
954 Symbol** global_symbols)
955{
956 if (sh_type == elfcpp::SHT_RELA)
957 {
958 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
959 program_name, object->name().c_str());
960 gold_exit(false);
961 }
962
ead1e424
ILT
963 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
964 Target_i386::Scan>(
92e059d8
ILT
965 options,
966 symtab,
ead1e424
ILT
967 layout,
968 this,
92e059d8 969 object,
a3ad94ed 970 data_shndx,
92e059d8
ILT
971 prelocs,
972 reloc_count,
973 local_symbol_count,
974 plocal_symbols,
975 global_symbols);
976}
977
16649710 978// Finalize the sections.
5a6f7e2d
ILT
979
980void
16649710
ILT
981Target_i386::do_finalize_sections(const General_options* options,
982 Layout* layout)
5a6f7e2d 983{
16649710
ILT
984 // Fill in some more dynamic tags.
985 Output_data_dynamic* const odyn = layout->dynamic_data();
986 if (odyn != NULL)
987 {
988 if (this->got_plt_ != NULL)
989 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
990
991 if (this->plt_ != NULL)
992 {
993 const Output_data* od = this->plt_->rel_plt();
994 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
995 odyn->add_section_address(elfcpp::DT_JMPREL, od);
996 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
997 }
998
999 if (this->rel_dyn_ != NULL)
1000 {
1001 const Output_data* od = this->rel_dyn_;
1002 odyn->add_section_address(elfcpp::DT_REL, od);
1003 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1004 odyn->add_constant(elfcpp::DT_RELENT,
1005 elfcpp::Elf_sizes<32>::rel_size);
1006 }
1007
1008 if (!options->is_shared())
1009 {
1010 // The value of the DT_DEBUG tag is filled in by the dynamic
1011 // linker at run time, and used by the debugger.
1012 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1013 }
1014 }
1015
1016 // Emit any relocs we saved in an attempt to avoid generating COPY
1017 // relocs.
5a6f7e2d
ILT
1018 if (this->copy_relocs_ == NULL)
1019 return;
1020 if (this->copy_relocs_->any_to_emit())
1021 {
1022 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1023 this->copy_relocs_->emit(rel_dyn);
1024 }
1025 delete this->copy_relocs_;
1026 this->copy_relocs_ = NULL;
1027}
1028
61ba1cf9
ILT
1029// Perform a relocation.
1030
ead1e424 1031inline bool
92e059d8 1032Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
ead1e424 1033 Target_i386* target,
92e059d8
ILT
1034 size_t relnum,
1035 const elfcpp::Rel<32, false>& rel,
1036 unsigned int r_type,
c06b7b0b 1037 const Sized_symbol<32>* gsym,
b8e6aad9 1038 const Symbol_value<32>* psymval,
92e059d8
ILT
1039 unsigned char* view,
1040 elfcpp::Elf_types<32>::Elf_Addr address,
1041 off_t view_size)
61ba1cf9 1042{
ead1e424
ILT
1043 if (this->skip_call_tls_get_addr_)
1044 {
1045 if (r_type != elfcpp::R_386_PLT32
1046 || gsym == NULL
1047 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1048 {
1049 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1050 program_name,
1051 relinfo->location(relnum, rel.get_r_offset()).c_str());
1052 gold_exit(false);
1053 }
1054
1055 this->skip_call_tls_get_addr_ = false;
1056
1057 return false;
1058 }
1059
a3ad94ed 1060 // Pick the value to use for symbols defined in shared objects.
b8e6aad9 1061 Symbol_value<32> symval;
8462ae85 1062 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
a3ad94ed 1063 {
b8e6aad9
ILT
1064 symval.set_output_value(target->plt_section()->address()
1065 + gsym->plt_offset());
1066 psymval = &symval;
a3ad94ed
ILT
1067 }
1068
b8e6aad9
ILT
1069 const Sized_relobj<32, false>* object = relinfo->object;
1070
61ba1cf9
ILT
1071 switch (r_type)
1072 {
1073 case elfcpp::R_386_NONE:
92e059d8
ILT
1074 case elfcpp::R_386_GNU_VTINHERIT:
1075 case elfcpp::R_386_GNU_VTENTRY:
61ba1cf9
ILT
1076 break;
1077
1078 case elfcpp::R_386_32:
b8e6aad9 1079 Relocate_functions<32, false>::rel32(view, object, psymval);
61ba1cf9
ILT
1080 break;
1081
1082 case elfcpp::R_386_PC32:
b8e6aad9 1083 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
92e059d8
ILT
1084 break;
1085
1086 case elfcpp::R_386_16:
b8e6aad9 1087 Relocate_functions<32, false>::rel16(view, object, psymval);
92e059d8
ILT
1088 break;
1089
1090 case elfcpp::R_386_PC16:
b8e6aad9 1091 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
61ba1cf9
ILT
1092 break;
1093
92e059d8 1094 case elfcpp::R_386_8:
b8e6aad9 1095 Relocate_functions<32, false>::rel8(view, object, psymval);
92e059d8
ILT
1096 break;
1097
1098 case elfcpp::R_386_PC8:
b8e6aad9 1099 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
92e059d8
ILT
1100 break;
1101
ead1e424 1102 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
1103 gold_assert(gsym->has_plt_offset()
1104 || gsym->final_value_is_known(relinfo->options));
b8e6aad9 1105 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
ead1e424
ILT
1106 break;
1107
1108 case elfcpp::R_386_GOT32:
1109 // Local GOT offsets not yet supported.
a3ad94ed
ILT
1110 gold_assert(gsym);
1111 gold_assert(gsym->has_got_offset());
b8e6aad9 1112 Relocate_functions<32, false>::rel32(view, gsym->got_offset());
ead1e424
ILT
1113 break;
1114
1115 case elfcpp::R_386_GOTOFF:
b8e6aad9
ILT
1116 {
1117 elfcpp::Elf_types<32>::Elf_Addr value;
1118 value = (psymval->value(object, 0)
1119 - target->got_section(NULL, NULL, NULL)->address());
1120 Relocate_functions<32, false>::rel32(view, value);
1121 }
ead1e424
ILT
1122 break;
1123
1124 case elfcpp::R_386_GOTPC:
b8e6aad9
ILT
1125 {
1126 elfcpp::Elf_types<32>::Elf_Addr value;
1127 value = target->got_section(NULL, NULL, NULL)->address();
1128 Relocate_functions<32, false>::pcrel32(view, value, address);
1129 }
ead1e424
ILT
1130 break;
1131
92e059d8
ILT
1132 case elfcpp::R_386_COPY:
1133 case elfcpp::R_386_GLOB_DAT:
1134 case elfcpp::R_386_JUMP_SLOT:
1135 case elfcpp::R_386_RELATIVE:
1136 case elfcpp::R_386_TLS_TPOFF:
1137 case elfcpp::R_386_TLS_DTPMOD32:
1138 case elfcpp::R_386_TLS_DTPOFF32:
1139 case elfcpp::R_386_TLS_TPOFF32:
1140 case elfcpp::R_386_TLS_DESC:
1141 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1142 program_name,
1143 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1144 r_type);
1145 gold_exit(false);
1146 break;
1147
1148 case elfcpp::R_386_TLS_IE:
1149 case elfcpp::R_386_TLS_GOTIE:
1150 case elfcpp::R_386_TLS_LE:
1151 case elfcpp::R_386_TLS_GD:
1152 case elfcpp::R_386_TLS_LDM:
1153 case elfcpp::R_386_TLS_LDO_32:
1154 case elfcpp::R_386_TLS_IE_32:
1155 case elfcpp::R_386_TLS_LE_32:
1156 case elfcpp::R_386_TLS_GOTDESC:
1157 case elfcpp::R_386_TLS_DESC_CALL:
b8e6aad9 1158 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
ead1e424 1159 address, view_size);
92e059d8
ILT
1160 break;
1161
92e059d8
ILT
1162 case elfcpp::R_386_32PLT:
1163 case elfcpp::R_386_TLS_GD_32:
1164 case elfcpp::R_386_TLS_GD_PUSH:
1165 case elfcpp::R_386_TLS_GD_CALL:
1166 case elfcpp::R_386_TLS_GD_POP:
1167 case elfcpp::R_386_TLS_LDM_32:
1168 case elfcpp::R_386_TLS_LDM_PUSH:
1169 case elfcpp::R_386_TLS_LDM_CALL:
1170 case elfcpp::R_386_TLS_LDM_POP:
1171 case elfcpp::R_386_USED_BY_INTEL_200:
61ba1cf9
ILT
1172 default:
1173 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
92e059d8
ILT
1174 program_name,
1175 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1176 r_type);
61ba1cf9 1177 // gold_exit(false);
92e059d8
ILT
1178 break;
1179 }
ead1e424
ILT
1180
1181 return true;
92e059d8
ILT
1182}
1183
1184// Perform a TLS relocation.
1185
1186inline void
1187Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1188 size_t relnum,
1189 const elfcpp::Rel<32, false>& rel,
1190 unsigned int r_type,
c06b7b0b 1191 const Sized_symbol<32>* gsym,
b8e6aad9 1192 const Symbol_value<32>* psymval,
92e059d8
ILT
1193 unsigned char* view,
1194 elfcpp::Elf_types<32>::Elf_Addr,
1195 off_t view_size)
1196{
1197 Output_segment* tls_segment = relinfo->layout->tls_segment();
1198 if (tls_segment == NULL)
1199 {
1200 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1201 program_name,
1202 relinfo->location(relnum, rel.get_r_offset()).c_str());
1203 gold_exit(false);
1204 }
1205
b8e6aad9
ILT
1206 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1207
a3ad94ed
ILT
1208 const bool is_final = (gsym == NULL
1209 ? !relinfo->options->is_shared()
1210 : gsym->final_value_is_known(relinfo->options));
92e059d8 1211 const unsigned int opt_r_type =
a3ad94ed 1212 Target_i386::optimize_tls_reloc(relinfo->options, is_final, r_type);
92e059d8
ILT
1213 switch (r_type)
1214 {
1215 case elfcpp::R_386_TLS_LE_32:
1216 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1217 Relocate_functions<32, false>::rel32(view, value);
1218 break;
1219
1220 case elfcpp::R_386_TLS_LE:
1221 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1222 Relocate_functions<32, false>::rel32(view, value);
1223 break;
1224
1225 case elfcpp::R_386_TLS_IE:
1226 case elfcpp::R_386_TLS_GOTIE:
1227 case elfcpp::R_386_TLS_IE_32:
1228 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1229 {
1230 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1231 rel, r_type, value, view,
1232 view_size);
1233 break;
1234 }
1235 fprintf(stderr, _("%s: %s: unsupported reloc type %u\n"),
1236 program_name,
1237 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1238 r_type);
1239 // gold_exit(false);
1240 break;
1241
1242 case elfcpp::R_386_TLS_GD:
ead1e424
ILT
1243 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1244 {
1245 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1246 rel, r_type, value, view,
1247 view_size);
1248 break;
1249 }
1250 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1251 program_name,
1252 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1253 r_type);
1254 // gold_exit(false);
1255 break;
1256
92e059d8
ILT
1257 case elfcpp::R_386_TLS_LDM:
1258 case elfcpp::R_386_TLS_LDO_32:
1259 case elfcpp::R_386_TLS_GOTDESC:
1260 case elfcpp::R_386_TLS_DESC_CALL:
1261 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1262 program_name,
1263 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1264 r_type);
1265 // gold_exit(false);
1266 break;
1267 }
1268}
1269
1270// Do a relocation in which we convert a TLS Initial-Exec to a
1271// Local-Exec.
1272
1273inline void
1274Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1275 size_t relnum,
1276 Output_segment* tls_segment,
1277 const elfcpp::Rel<32, false>& rel,
1278 unsigned int r_type,
1279 elfcpp::Elf_types<32>::Elf_Addr value,
1280 unsigned char* view,
1281 off_t view_size)
1282{
1283 // We have to actually change the instructions, which means that we
1284 // need to examine the opcodes to figure out which instruction we
1285 // are looking at.
1286 if (r_type == elfcpp::R_386_TLS_IE)
1287 {
1288 // movl %gs:XX,%eax ==> movl $YY,%eax
1289 // movl %gs:XX,%reg ==> movl $YY,%reg
1290 // addl %gs:XX,%reg ==> addl $YY,%reg
1291 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1292 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1293
1294 unsigned char op1 = view[-1];
1295 if (op1 == 0xa1)
1296 {
1297 // movl XX,%eax ==> movl $YY,%eax
1298 view[-1] = 0xb8;
1299 }
1300 else
1301 {
1302 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1303 view_size, -2);
1304
1305 unsigned char op2 = view[-2];
1306 if (op2 == 0x8b)
1307 {
1308 // movl XX,%reg ==> movl $YY,%reg
1309 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1310 (op1 & 0xc7) == 0x05);
1311 view[-2] = 0xc7;
1312 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1313 }
1314 else if (op2 == 0x03)
1315 {
1316 // addl XX,%reg ==> addl $YY,%reg
1317 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1318 (op1 & 0xc7) == 0x05);
1319 view[-2] = 0x81;
1320 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1321 }
1322 else
1323 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1324 }
1325 }
1326 else
1327 {
1328 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1329 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1330 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1331 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1332 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1333
1334 unsigned char op1 = view[-1];
1335 unsigned char op2 = view[-2];
1336 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1337 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1338 if (op2 == 0x8b)
1339 {
1340 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1341 view[-2] = 0xc7;
1342 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1343 }
1344 else if (op2 == 0x2b)
1345 {
1346 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1347 view[-2] = 0x81;
1348 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1349 }
1350 else if (op2 == 0x03)
1351 {
1352 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1353 view[-2] = 0x81;
1354 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1355 }
1356 else
1357 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1358 }
1359
ead1e424
ILT
1360 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1361 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1362 value = - value;
92e059d8
ILT
1363
1364 Relocate_functions<32, false>::rel32(view, value);
1365}
1366
ead1e424
ILT
1367// Do a relocation in which we convert a TLS Global-Dynamic to a
1368// Local-Exec.
1369
1370inline void
1371Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1372 size_t relnum,
1373 Output_segment* tls_segment,
1374 const elfcpp::Rel<32, false>& rel,
1375 unsigned int,
1376 elfcpp::Elf_types<32>::Elf_Addr value,
1377 unsigned char* view,
1378 off_t view_size)
1379{
1380 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1381 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1382 // leal foo(%reg),%eax; call ___tls_get_addr
1383 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1384
1385 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1386 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1387
1388 unsigned char op1 = view[-1];
1389 unsigned char op2 = view[-2];
1390
1391 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1392 op2 == 0x8d || op2 == 0x04);
1393 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1394 view[4] == 0xe8);
1395
1396 int roff = 5;
1397
1398 if (op2 == 0x04)
1399 {
1400 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1401 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1402 view[-3] == 0x8d);
1403 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1404 ((op1 & 0xc7) == 0x05
1405 && op1 != (4 << 3)));
1406 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1407 }
1408 else
1409 {
1410 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1411 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1412 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1413 {
1414 // There is a trailing nop. Use the size byte subl.
1415 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1416 roff = 6;
1417 }
1418 else
1419 {
1420 // Use the five byte subl.
1421 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1422 }
1423 }
1424
1425 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1426 Relocate_functions<32, false>::rel32(view + roff, value);
1427
1428 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1429 // We can skip it.
1430 this->skip_call_tls_get_addr_ = true;
1431}
1432
92e059d8
ILT
1433// Check the range for a TLS relocation.
1434
1435inline void
1436Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1437 size_t relnum,
1438 const elfcpp::Rel<32, false>& rel,
1439 off_t view_size, off_t off)
1440{
1441 off_t offset = rel.get_r_offset() + off;
1442 if (offset < 0 || offset > view_size)
1443 {
1444 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1445 program_name,
1446 relinfo->location(relnum, rel.get_r_offset()).c_str());
1447 gold_exit(false);
1448 }
1449}
1450
1451// Check the validity of a TLS relocation. This is like assert.
1452
1453inline void
1454Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1455 size_t relnum,
1456 const elfcpp::Rel<32, false>& rel,
1457 bool valid)
1458{
1459 if (!valid)
1460 {
1461 fprintf(stderr,
1462 _("%s: %s: TLS relocation against invalid instruction\n"),
1463 program_name,
1464 relinfo->location(relnum, rel.get_r_offset()).c_str());
1465 gold_exit(false);
61ba1cf9
ILT
1466 }
1467}
1468
1469// Relocate section data.
1470
1471void
92e059d8 1472Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
1473 unsigned int sh_type,
1474 const unsigned char* prelocs,
1475 size_t reloc_count,
61ba1cf9
ILT
1476 unsigned char* view,
1477 elfcpp::Elf_types<32>::Elf_Addr address,
1478 off_t view_size)
1479{
a3ad94ed 1480 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 1481
ead1e424
ILT
1482 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1483 Target_i386::Relocate>(
92e059d8 1484 relinfo,
ead1e424 1485 this,
61ba1cf9
ILT
1486 prelocs,
1487 reloc_count,
61ba1cf9
ILT
1488 view,
1489 address,
1490 view_size);
1491}
1492
14bfc3f5
ILT
1493// The selector for i386 object files.
1494
1495class Target_selector_i386 : public Target_selector
1496{
1497public:
1498 Target_selector_i386()
1499 : Target_selector(elfcpp::EM_386, 32, false)
1500 { }
1501
1502 Target*
ead1e424
ILT
1503 recognize(int machine, int osabi, int abiversion);
1504
1505 private:
1506 Target_i386* target_;
14bfc3f5
ILT
1507};
1508
1509// Recognize an i386 object file when we already know that the machine
1510// number is EM_386.
1511
1512Target*
ead1e424 1513Target_selector_i386::recognize(int, int, int)
14bfc3f5 1514{
ead1e424
ILT
1515 if (this->target_ == NULL)
1516 this->target_ = new Target_i386();
1517 return this->target_;
14bfc3f5
ILT
1518}
1519
1520Target_selector_i386 target_selector_i386;
1521
1522} // End anonymous namespace.
This page took 0.145555 seconds and 4 git commands to generate.