Remove unnecessary elfcpp_config.h file.
[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"
7e1edb90 8#include "parameters.h"
92e059d8 9#include "reloc.h"
61ba1cf9
ILT
10#include "i386.h"
11#include "object.h"
ead1e424 12#include "symtab.h"
92e059d8
ILT
13#include "layout.h"
14#include "output.h"
14bfc3f5 15#include "target.h"
61ba1cf9 16#include "target-reloc.h"
14bfc3f5
ILT
17#include "target-select.h"
18
19namespace
20{
21
22using namespace gold;
23
a3ad94ed
ILT
24class Output_data_plt_i386;
25
14bfc3f5
ILT
26// The i386 target class.
27
28class Target_i386 : public Sized_target<32, false>
29{
30 public:
5a6f7e2d
ILT
31 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
32
14bfc3f5 33 Target_i386()
ead1e424 34 : Sized_target<32, false>(&i386_info),
5a6f7e2d
ILT
35 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
36 copy_relocs_(NULL), dynbss_(NULL)
14bfc3f5 37 { }
75f65a3e 38
92e059d8 39 // Scan the relocations to look for symbol adjustments.
61ba1cf9 40 void
92e059d8
ILT
41 scan_relocs(const General_options& options,
42 Symbol_table* symtab,
ead1e424 43 Layout* layout,
f6ce93d6 44 Sized_relobj<32, false>* object,
a3ad94ed 45 unsigned int data_shndx,
92e059d8
ILT
46 unsigned int sh_type,
47 const unsigned char* prelocs,
48 size_t reloc_count,
49 size_t local_symbol_count,
50 const unsigned char* plocal_symbols,
51 Symbol** global_symbols);
61ba1cf9 52
5a6f7e2d
ILT
53 // Finalize the sections.
54 void
7e1edb90 55 do_finalize_sections(Layout*);
5a6f7e2d 56
92e059d8
ILT
57 // Relocate a section.
58 void
59 relocate_section(const Relocate_info<32, false>*,
60 unsigned int sh_type,
61 const unsigned char* prelocs,
62 size_t reloc_count,
63 unsigned char* view,
64 elfcpp::Elf_types<32>::Elf_Addr view_address,
65 off_t view_size);
66
c51e6221
ILT
67 // Return a string used to fill a code section with nops.
68 std::string
69 do_code_fill(off_t length);
70
92e059d8
ILT
71 private:
72 // The class which scans relocations.
73 struct Scan
61ba1cf9
ILT
74 {
75 inline void
ead1e424
ILT
76 local(const General_options& options, Symbol_table* symtab,
77 Layout* layout, Target_i386* target,
f6ce93d6 78 Sized_relobj<32, false>* object,
a3ad94ed 79 unsigned int data_shndx,
92e059d8
ILT
80 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
81 const elfcpp::Sym<32, false>& lsym);
61ba1cf9 82
92e059d8 83 inline void
ead1e424
ILT
84 global(const General_options& options, Symbol_table* symtab,
85 Layout* layout, Target_i386* target,
f6ce93d6 86 Sized_relobj<32, false>* object,
a3ad94ed 87 unsigned int data_shndx,
92e059d8
ILT
88 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
89 Symbol* gsym);
61ba1cf9
ILT
90 };
91
92e059d8
ILT
92 // The class which implements relocation.
93 class Relocate
94 {
95 public:
ead1e424
ILT
96 Relocate()
97 : skip_call_tls_get_addr_(false)
98 { }
99
100 ~Relocate()
101 {
102 if (this->skip_call_tls_get_addr_)
103 {
104 // FIXME: This needs to specify the location somehow.
105 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
106 program_name);
107 gold_exit(false);
108 }
109 }
110
111 // Do a relocation. Return false if the caller should not issue
112 // any warnings about this relocation.
113 inline bool
114 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
92e059d8 115 const elfcpp::Rel<32, false>&,
c06b7b0b 116 unsigned int r_type, const Sized_symbol<32>*,
b8e6aad9 117 const Symbol_value<32>*,
92e059d8
ILT
118 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
119 off_t);
120
121 private:
122 // Do a TLS relocation.
ead1e424 123 inline void
92e059d8
ILT
124 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
125 const elfcpp::Rel<32, false>&,
c06b7b0b 126 unsigned int r_type, const Sized_symbol<32>*,
b8e6aad9 127 const Symbol_value<32>*,
92e059d8
ILT
128 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
129
130 // Do a TLS Initial-Exec to Local-Exec transition.
131 static inline void
132 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
133 Output_segment* tls_segment,
134 const elfcpp::Rel<32, false>&, unsigned int r_type,
135 elfcpp::Elf_types<32>::Elf_Addr value,
136 unsigned char* view,
137 off_t view_size);
138
ead1e424
ILT
139 // Do a TLS Global-Dynamic to Local-Exec transition.
140 inline void
141 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
142 Output_segment* tls_segment,
143 const elfcpp::Rel<32, false>&, unsigned int r_type,
144 elfcpp::Elf_types<32>::Elf_Addr value,
145 unsigned char* view,
146 off_t view_size);
147
92e059d8
ILT
148 // Check the range for a TLS relocation.
149 static inline void
150 check_range(const Relocate_info<32, false>*, size_t relnum,
151 const elfcpp::Rel<32, false>&, off_t, off_t);
152
153 // Check the validity of a TLS relocation. This is like assert.
154 static inline void
155 check_tls(const Relocate_info<32, false>*, size_t relnum,
156 const elfcpp::Rel<32, false>&, bool);
ead1e424
ILT
157
158 // This is set if we should skip the next reloc, which should be a
159 // PLT32 reloc against ___tls_get_addr.
160 bool skip_call_tls_get_addr_;
92e059d8
ILT
161 };
162
163 // Adjust TLS relocation type based on the options and whether this
164 // is a local symbol.
165 static unsigned int
7e1edb90 166 optimize_tls_reloc(bool is_final, int r_type);
92e059d8 167
ead1e424 168 // Get the GOT section, creating it if necessary.
dbe717ef 169 Output_data_got<32, false>*
7e1edb90 170 got_section(Symbol_table*, Layout*);
a3ad94ed
ILT
171
172 // Create a PLT entry for a global symbol.
173 void
7e1edb90 174 make_plt_entry(Symbol_table*, Layout*, Symbol*);
a3ad94ed
ILT
175
176 // Get the PLT section.
177 Output_data_plt_i386*
178 plt_section() const
179 {
180 gold_assert(this->plt_ != NULL);
181 return this->plt_;
182 }
183
5a6f7e2d
ILT
184 // Get the dynamic reloc section, creating it if necessary.
185 Reloc_section*
186 rel_dyn_section(Layout*);
187
a3ad94ed
ILT
188 // Copy a relocation against a global symbol.
189 void
5a6f7e2d
ILT
190 copy_reloc(const General_options*, Symbol_table*, Layout*,
191 Sized_relobj<32, false>*, unsigned int,
a3ad94ed 192 Symbol*, const elfcpp::Rel<32, false>&);
ead1e424 193
92e059d8
ILT
194 // Information about this specific target which we pass to the
195 // general Target structure.
75f65a3e 196 static const Target::Target_info i386_info;
ead1e424
ILT
197
198 // The GOT section.
dbe717ef 199 Output_data_got<32, false>* got_;
a3ad94ed
ILT
200 // The PLT section.
201 Output_data_plt_i386* plt_;
202 // The GOT PLT section.
203 Output_data_space* got_plt_;
5a6f7e2d
ILT
204 // The dynamic reloc section.
205 Reloc_section* rel_dyn_;
206 // Relocs saved to avoid a COPY reloc.
207 Copy_relocs<32, false>* copy_relocs_;
208 // Space for variables copied with a COPY reloc.
209 Output_data_space* dynbss_;
75f65a3e
ILT
210};
211
212const Target::Target_info Target_i386::i386_info =
213{
61ba1cf9
ILT
214 32, // size
215 false, // is_big_endian
216 elfcpp::EM_386, // machine_code
217 false, // has_make_symbol
dbe717ef 218 false, // has_resolve
c51e6221 219 true, // has_code_fill
dbe717ef
ILT
220 "/usr/lib/libc.so.1", // dynamic_linker
221 0x08048000, // text_segment_address
61ba1cf9
ILT
222 0x1000, // abi_pagesize
223 0x1000 // common_pagesize
14bfc3f5
ILT
224};
225
ead1e424
ILT
226// Get the GOT section, creating it if necessary.
227
dbe717ef 228Output_data_got<32, false>*
7e1edb90 229Target_i386::got_section(Symbol_table* symtab, Layout* layout)
ead1e424
ILT
230{
231 if (this->got_ == NULL)
232 {
7e1edb90 233 gold_assert(symtab != NULL && layout != NULL);
a3ad94ed 234
7e1edb90 235 this->got_ = new Output_data_got<32, false>();
ead1e424 236
ead1e424 237 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
16649710
ILT
238 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
239 this->got_);
ead1e424 240
a3ad94ed
ILT
241 // The old GNU linker creates a .got.plt section. We just
242 // create another set of data in the .got section. Note that we
243 // always create a PLT if we create a GOT, although the PLT
244 // might be empty.
245 this->got_plt_ = new Output_data_space(4);
246 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
16649710
ILT
247 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
248 this->got_plt_);
a3ad94ed 249
ead1e424 250 // The first three entries are reserved.
a3ad94ed 251 this->got_plt_->set_space_size(3 * 4);
ead1e424 252
a3ad94ed 253 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
14b31740 254 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
a3ad94ed 255 this->got_plt_,
ead1e424 256 0, 0, elfcpp::STT_OBJECT,
16649710 257 elfcpp::STB_LOCAL,
ead1e424
ILT
258 elfcpp::STV_HIDDEN, 0,
259 false, false);
260 }
a3ad94ed 261
ead1e424
ILT
262 return this->got_;
263}
264
5a6f7e2d
ILT
265// Get the dynamic reloc section, creating it if necessary.
266
267Target_i386::Reloc_section*
268Target_i386::rel_dyn_section(Layout* layout)
269{
270 if (this->rel_dyn_ == NULL)
271 {
272 gold_assert(layout != NULL);
273 this->rel_dyn_ = new Reloc_section();
274 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
275 elfcpp::SHF_ALLOC, this->rel_dyn_);
276 }
277 return this->rel_dyn_;
278}
279
a3ad94ed
ILT
280// A class to handle the PLT data.
281
282class Output_data_plt_i386 : public Output_section_data
283{
284 public:
285 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
286
7e1edb90 287 Output_data_plt_i386(Layout*, Output_data_space*);
a3ad94ed
ILT
288
289 // Add an entry to the PLT.
290 void
291 add_entry(Symbol* gsym);
292
16649710
ILT
293 // Return the .rel.plt section data.
294 const Reloc_section*
295 rel_plt() const
296 { return this->rel_; }
297
298 protected:
299 void
300 do_adjust_output_section(Output_section* os);
301
a3ad94ed
ILT
302 private:
303 // The size of an entry in the PLT.
304 static const int plt_entry_size = 16;
305
306 // The first entry in the PLT for an executable.
307 static unsigned char exec_first_plt_entry[plt_entry_size];
308
309 // The first entry in the PLT for a shared object.
310 static unsigned char dyn_first_plt_entry[plt_entry_size];
311
312 // Other entries in the PLT for an executable.
313 static unsigned char exec_plt_entry[plt_entry_size];
314
315 // Other entries in the PLT for a shared object.
316 static unsigned char dyn_plt_entry[plt_entry_size];
317
318 // Set the final size.
319 void
320 do_set_address(uint64_t, off_t)
321 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
322
323 // Write out the PLT data.
324 void
325 do_write(Output_file*);
326
327 // The reloc section.
328 Reloc_section* rel_;
329 // The .got.plt section.
330 Output_data_space* got_plt_;
331 // The number of PLT entries.
332 unsigned int count_;
a3ad94ed
ILT
333};
334
335// Create the PLT section. The ordinary .got section is an argument,
336// since we need to refer to the start. We also create our own .got
337// section just for PLT entries.
338
339Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
7e1edb90 340 Output_data_space* got_plt)
80576242 341 : Output_section_data(4), got_plt_(got_plt), count_(0)
a3ad94ed
ILT
342{
343 this->rel_ = new Reloc_section();
344 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
345 elfcpp::SHF_ALLOC, this->rel_);
346}
347
16649710
ILT
348// For some reason
349
350void
351Output_data_plt_i386::do_adjust_output_section(Output_section* os)
352{
353 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
354 // linker, and so do we.
355 os->set_entsize(4);
356}
357
a3ad94ed
ILT
358// Add an entry to the PLT.
359
360void
361Output_data_plt_i386::add_entry(Symbol* gsym)
362{
363 gold_assert(!gsym->has_plt_offset());
364
365 // Note that when setting the PLT offset we skip the initial
366 // reserved PLT entry.
367 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
368
369 ++this->count_;
370
371 off_t got_offset = this->got_plt_->data_size();
372
373 // Every PLT entry needs a GOT entry which points back to the PLT
374 // entry (this will be changed by the dynamic linker, normally
375 // lazily when the function is called).
376 this->got_plt_->set_space_size(got_offset + 4);
377
378 // Every PLT entry needs a reloc.
16649710 379 gsym->set_needs_dynsym_entry();
a3ad94ed
ILT
380 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
381 got_offset);
382
383 // Note that we don't need to save the symbol. The contents of the
384 // PLT are independent of which symbols are used. The symbols only
385 // appear in the relocations.
386}
387
388// The first entry in the PLT for an executable.
389
390unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
391{
392 0xff, 0x35, // pushl contents of memory address
393 0, 0, 0, 0, // replaced with address of .got + 4
394 0xff, 0x25, // jmp indirect
395 0, 0, 0, 0, // replaced with address of .got + 8
396 0, 0, 0, 0 // unused
397};
398
399// The first entry in the PLT for a shared object.
400
401unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
402{
403 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
404 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
405 0, 0, 0, 0 // unused
406};
407
408// Subsequent entries in the PLT for an executable.
409
410unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
411{
412 0xff, 0x25, // jmp indirect
413 0, 0, 0, 0, // replaced with address of symbol in .got
414 0x68, // pushl immediate
415 0, 0, 0, 0, // replaced with offset into relocation table
416 0xe9, // jmp relative
417 0, 0, 0, 0 // replaced with offset to start of .plt
418};
419
420// Subsequent entries in the PLT for a shared object.
421
422unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
423{
424 0xff, 0xa3, // jmp *offset(%ebx)
425 0, 0, 0, 0, // replaced with offset of symbol in .got
426 0x68, // pushl immediate
427 0, 0, 0, 0, // replaced with offset into relocation table
428 0xe9, // jmp relative
429 0, 0, 0, 0 // replaced with offset to start of .plt
430};
431
432// Write out the PLT. This uses the hand-coded instructions above,
433// and adjusts them as needed. This is all specified by the i386 ELF
434// Processor Supplement.
435
436void
437Output_data_plt_i386::do_write(Output_file* of)
438{
439 const off_t offset = this->offset();
440 const off_t oview_size = this->data_size();
441 unsigned char* const oview = of->get_output_view(offset, oview_size);
442
443 const off_t got_file_offset = this->got_plt_->offset();
444 const off_t got_size = this->got_plt_->data_size();
445 unsigned char* const got_view = of->get_output_view(got_file_offset,
446 got_size);
447
448 unsigned char* pov = oview;
449
450 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
451 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
452
7e1edb90 453 if (parameters->output_is_shared())
a3ad94ed
ILT
454 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
455 else
456 {
457 memcpy(pov, exec_first_plt_entry, plt_entry_size);
458 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
459 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
460 }
461 pov += plt_entry_size;
462
463 unsigned char* got_pov = got_view;
464
465 memset(got_pov, 0, 12);
466 got_pov += 12;
467
468 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
469
470 unsigned int plt_offset = plt_entry_size;
471 unsigned int plt_rel_offset = 0;
472 unsigned int got_offset = 12;
473 const unsigned int count = this->count_;
474 for (unsigned int i = 0;
475 i < count;
476 ++i,
477 pov += plt_entry_size,
478 got_pov += 4,
479 plt_offset += plt_entry_size,
480 plt_rel_offset += rel_size,
481 got_offset += 4)
482 {
483 // Set and adjust the PLT entry itself.
484
7e1edb90 485 if (parameters->output_is_shared())
a3ad94ed
ILT
486 {
487 memcpy(pov, dyn_plt_entry, plt_entry_size);
488 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
489 }
490 else
491 {
492 memcpy(pov, exec_plt_entry, plt_entry_size);
493 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
494 (got_address
495 + got_offset));
496 }
497
498 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
499 elfcpp::Swap<32, false>::writeval(pov + 12,
500 - (plt_offset + plt_entry_size));
501
502 // Set the entry in the GOT.
503 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
504 }
505
506 gold_assert(pov - oview == oview_size);
507 gold_assert(got_pov - got_view == got_size);
508
509 of->write_output_view(offset, oview_size, oview);
510 of->write_output_view(got_file_offset, got_size, got_view);
511}
512
513// Create a PLT entry for a global symbol.
514
515void
7e1edb90 516Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
a3ad94ed
ILT
517{
518 if (gsym->has_plt_offset())
519 return;
520
521 if (this->plt_ == NULL)
522 {
523 // Create the GOT sections first.
7e1edb90 524 this->got_section(symtab, layout);
a3ad94ed 525
7e1edb90 526 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
16649710
ILT
527 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
528 (elfcpp::SHF_ALLOC
529 | elfcpp::SHF_EXECINSTR),
530 this->plt_);
a3ad94ed
ILT
531 }
532
533 this->plt_->add_entry(gsym);
534}
535
536// Handle a relocation against a non-function symbol defined in a
537// dynamic object. The traditional way to handle this is to generate
538// a COPY relocation to copy the variable at runtime from the shared
539// object into the executable's data segment. However, this is
540// undesirable in general, as if the size of the object changes in the
541// dynamic object, the executable will no longer work correctly. If
542// this relocation is in a writable section, then we can create a
543// dynamic reloc and the dynamic linker will resolve it to the correct
544// address at runtime. However, we do not want do that if the
545// relocation is in a read-only section, as it would prevent the
546// readonly segment from being shared. And if we have to eventually
547// generate a COPY reloc, then any dynamic relocations will be
548// useless. So this means that if this is a writable section, we need
549// to save the relocation until we see whether we have to create a
550// COPY relocation for this symbol for any other relocation.
551
552void
553Target_i386::copy_reloc(const General_options* options,
5a6f7e2d
ILT
554 Symbol_table* symtab,
555 Layout* layout,
a3ad94ed
ILT
556 Sized_relobj<32, false>* object,
557 unsigned int data_shndx, Symbol* gsym,
5a6f7e2d 558 const elfcpp::Rel<32, false>& rel)
a3ad94ed 559{
5a6f7e2d
ILT
560 Sized_symbol<32>* ssym;
561 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
562 SELECT_SIZE(32));
563
564 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
565 data_shndx, ssym))
a3ad94ed
ILT
566 {
567 // So far we do not need a COPY reloc. Save this relocation.
5a6f7e2d
ILT
568 // If it turns out that we never need a COPY reloc for this
569 // symbol, then we will emit the relocation.
570 if (this->copy_relocs_ == NULL)
571 this->copy_relocs_ = new Copy_relocs<32, false>();
572 this->copy_relocs_->save(ssym, object, data_shndx, rel);
a3ad94ed 573 }
5a6f7e2d
ILT
574 else
575 {
576 // Allocate space for this symbol in the .bss section.
577
578 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
579
580 // There is no defined way to determine the required alignment
581 // of the symbol. We pick the alignment based on the size. We
582 // set an arbitrary maximum of 256.
583 unsigned int align;
584 for (align = 1; align < 512; align <<= 1)
585 if ((symsize & align) != 0)
586 break;
a3ad94ed 587
5a6f7e2d
ILT
588 if (this->dynbss_ == NULL)
589 {
590 this->dynbss_ = new Output_data_space(align);
591 layout->add_output_section_data(".bss",
592 elfcpp::SHT_NOBITS,
593 (elfcpp::SHF_ALLOC
594 | elfcpp::SHF_WRITE),
595 this->dynbss_);
596 }
597
598 Output_data_space* dynbss = this->dynbss_;
599
600 if (align > dynbss->addralign())
601 dynbss->set_space_alignment(align);
602
603 off_t dynbss_size = dynbss->data_size();
604 dynbss_size = align_address(dynbss_size, align);
605 off_t offset = dynbss_size;
606 dynbss->set_space_size(dynbss_size + symsize);
607
608 // Define the symbol in the .dynbss section.
14b31740
ILT
609 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
610 dynbss, offset, symsize, ssym->type(),
611 ssym->binding(), ssym->visibility(),
612 ssym->nonvis(), false, false);
5a6f7e2d
ILT
613
614 // Add the COPY reloc.
16649710 615 ssym->set_needs_dynsym_entry();
5a6f7e2d
ILT
616 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
617 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
618 }
a3ad94ed
ILT
619}
620
92e059d8 621// Optimize the TLS relocation type based on what we know about the
a3ad94ed
ILT
622// symbol. IS_FINAL is true if the final address of this symbol is
623// known at link time.
92e059d8
ILT
624
625unsigned int
7e1edb90 626Target_i386::optimize_tls_reloc(bool is_final, int r_type)
92e059d8
ILT
627{
628 // If we are generating a shared library, then we can't do anything
629 // in the linker.
7e1edb90 630 if (parameters->output_is_shared())
92e059d8
ILT
631 return r_type;
632
633 switch (r_type)
634 {
635 case elfcpp::R_386_TLS_GD:
636 case elfcpp::R_386_TLS_GOTDESC:
637 case elfcpp::R_386_TLS_DESC_CALL:
638 // These are Global-Dynamic which permits fully general TLS
639 // access. Since we know that we are generating an executable,
640 // we can convert this to Initial-Exec. If we also know that
641 // this is a local symbol, we can further switch to Local-Exec.
a3ad94ed 642 if (is_final)
92e059d8
ILT
643 return elfcpp::R_386_TLS_LE_32;
644 return elfcpp::R_386_TLS_IE_32;
645
646 case elfcpp::R_386_TLS_LDM:
647 // This is Local-Dynamic, which refers to a local symbol in the
648 // dynamic TLS block. Since we know that we generating an
649 // executable, we can switch to Local-Exec.
650 return elfcpp::R_386_TLS_LE_32;
651
652 case elfcpp::R_386_TLS_LDO_32:
653 // Another type of Local-Dynamic relocation.
654 return elfcpp::R_386_TLS_LE;
655
656 case elfcpp::R_386_TLS_IE:
657 case elfcpp::R_386_TLS_GOTIE:
658 case elfcpp::R_386_TLS_IE_32:
659 // These are Initial-Exec relocs which get the thread offset
660 // from the GOT. If we know that we are linking against the
661 // local symbol, we can switch to Local-Exec, which links the
662 // thread offset into the instruction.
a3ad94ed 663 if (is_final)
92e059d8
ILT
664 return elfcpp::R_386_TLS_LE_32;
665 return r_type;
8462ae85 666
92e059d8
ILT
667 case elfcpp::R_386_TLS_LE:
668 case elfcpp::R_386_TLS_LE_32:
669 // When we already have Local-Exec, there is nothing further we
670 // can do.
671 return r_type;
672
673 default:
a3ad94ed 674 gold_unreachable();
92e059d8
ILT
675 }
676}
677
678// Scan a relocation for a local symbol.
679
680inline void
7e1edb90 681Target_i386::Scan::local(const General_options&,
ead1e424
ILT
682 Symbol_table* symtab,
683 Layout* layout,
684 Target_i386* target,
f6ce93d6 685 Sized_relobj<32, false>* object,
a3ad94ed
ILT
686 unsigned int,
687 const elfcpp::Rel<32, false>&,
688 unsigned int r_type,
92e059d8
ILT
689 const elfcpp::Sym<32, false>&)
690{
691 switch (r_type)
692 {
693 case elfcpp::R_386_NONE:
694 case elfcpp::R_386_GNU_VTINHERIT:
695 case elfcpp::R_386_GNU_VTENTRY:
696 break;
697
698 case elfcpp::R_386_32:
699 case elfcpp::R_386_16:
700 case elfcpp::R_386_8:
701 // FIXME: If we are generating a shared object we need to copy
702 // this relocation into the object.
7e1edb90 703 gold_assert(!parameters->output_is_shared());
92e059d8
ILT
704 break;
705
706 case elfcpp::R_386_PC32:
707 case elfcpp::R_386_PC16:
708 case elfcpp::R_386_PC8:
709 break;
710
ead1e424
ILT
711 case elfcpp::R_386_GOTOFF:
712 case elfcpp::R_386_GOTPC:
713 // We need a GOT section.
7e1edb90 714 target->got_section(symtab, layout);
ead1e424
ILT
715 break;
716
92e059d8
ILT
717 case elfcpp::R_386_COPY:
718 case elfcpp::R_386_GLOB_DAT:
719 case elfcpp::R_386_JUMP_SLOT:
720 case elfcpp::R_386_RELATIVE:
721 case elfcpp::R_386_TLS_TPOFF:
722 case elfcpp::R_386_TLS_DTPMOD32:
723 case elfcpp::R_386_TLS_DTPOFF32:
724 case elfcpp::R_386_TLS_TPOFF32:
725 case elfcpp::R_386_TLS_DESC:
726 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
727 program_name, object->name().c_str(), r_type);
728 gold_exit(false);
729 break;
730
731 case elfcpp::R_386_TLS_IE:
732 case elfcpp::R_386_TLS_GOTIE:
733 case elfcpp::R_386_TLS_LE:
734 case elfcpp::R_386_TLS_GD:
735 case elfcpp::R_386_TLS_LDM:
736 case elfcpp::R_386_TLS_LDO_32:
737 case elfcpp::R_386_TLS_IE_32:
738 case elfcpp::R_386_TLS_LE_32:
739 case elfcpp::R_386_TLS_GOTDESC:
740 case elfcpp::R_386_TLS_DESC_CALL:
7e1edb90 741 {
80576242
ILT
742 bool output_is_shared = parameters->output_is_shared();
743 r_type = Target_i386::optimize_tls_reloc(!output_is_shared,
7e1edb90
ILT
744 r_type);
745 switch (r_type)
746 {
747 case elfcpp::R_386_TLS_LE:
748 case elfcpp::R_386_TLS_LE_32:
749 // FIXME: If generating a shared object, we need to copy
750 // this relocation into the object.
80576242 751 gold_assert(!output_is_shared);
7e1edb90 752 break;
92e059d8 753
7e1edb90
ILT
754 case elfcpp::R_386_TLS_IE:
755 case elfcpp::R_386_TLS_GOTIE:
756 case elfcpp::R_386_TLS_GD:
757 case elfcpp::R_386_TLS_LDM:
758 case elfcpp::R_386_TLS_LDO_32:
759 case elfcpp::R_386_TLS_IE_32:
760 case elfcpp::R_386_TLS_GOTDESC:
761 case elfcpp::R_386_TLS_DESC_CALL:
762 fprintf(stderr,
763 _("%s: %s: unsupported reloc %u against local symbol\n"),
764 program_name, object->name().c_str(), r_type);
765 break;
766 }
767 }
92e059d8
ILT
768 break;
769
770 case elfcpp::R_386_GOT32:
771 case elfcpp::R_386_PLT32:
92e059d8
ILT
772 case elfcpp::R_386_32PLT:
773 case elfcpp::R_386_TLS_GD_32:
774 case elfcpp::R_386_TLS_GD_PUSH:
775 case elfcpp::R_386_TLS_GD_CALL:
776 case elfcpp::R_386_TLS_GD_POP:
777 case elfcpp::R_386_TLS_LDM_32:
778 case elfcpp::R_386_TLS_LDM_PUSH:
779 case elfcpp::R_386_TLS_LDM_CALL:
780 case elfcpp::R_386_TLS_LDM_POP:
781 case elfcpp::R_386_USED_BY_INTEL_200:
782 default:
783 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
784 program_name, object->name().c_str(), r_type);
785 break;
786 }
787}
788
789// Scan a relocation for a global symbol.
790
791inline void
792Target_i386::Scan::global(const General_options& options,
ead1e424
ILT
793 Symbol_table* symtab,
794 Layout* layout,
795 Target_i386* target,
f6ce93d6 796 Sized_relobj<32, false>* object,
a3ad94ed
ILT
797 unsigned int data_shndx,
798 const elfcpp::Rel<32, false>& reloc,
799 unsigned int r_type,
92e059d8
ILT
800 Symbol* gsym)
801{
802 switch (r_type)
803 {
804 case elfcpp::R_386_NONE:
805 case elfcpp::R_386_GNU_VTINHERIT:
8462ae85 806 case elfcpp::R_386_GNU_VTENTRY:
92e059d8
ILT
807 break;
808
809 case elfcpp::R_386_32:
810 case elfcpp::R_386_PC32:
811 case elfcpp::R_386_16:
812 case elfcpp::R_386_PC16:
813 case elfcpp::R_386_8:
814 case elfcpp::R_386_PC8:
815 // FIXME: If we are generating a shared object we may need to
816 // copy this relocation into the object. If this symbol is
817 // defined in a shared object, we may need to copy this
818 // relocation in order to avoid a COPY relocation.
7e1edb90 819 gold_assert(!parameters->output_is_shared());
a3ad94ed 820
14b31740 821 if (gsym->is_from_dynobj())
a3ad94ed
ILT
822 {
823 // This symbol is defined in a dynamic object. If it is a
824 // function, we make a PLT entry. Otherwise we need to
825 // either generate a COPY reloc or copy this reloc.
826 if (gsym->type() == elfcpp::STT_FUNC)
7e1edb90 827 target->make_plt_entry(symtab, layout, gsym);
a3ad94ed 828 else
5a6f7e2d
ILT
829 target->copy_reloc(&options, symtab, layout, object, data_shndx,
830 gsym, reloc);
a3ad94ed
ILT
831 }
832
92e059d8
ILT
833 break;
834
ead1e424 835 case elfcpp::R_386_GOT32:
8462ae85
ILT
836 {
837 // The symbol requires a GOT entry.
7e1edb90 838 Output_data_got<32, false>* got = target->got_section(symtab, layout);
8462ae85
ILT
839 if (got->add_global(gsym))
840 {
841 // If this symbol is not fully resolved, we need to add a
842 // dynamic relocation for it.
7e1edb90 843 if (!gsym->final_value_is_known())
8462ae85
ILT
844 {
845 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
846 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
847 gsym->got_offset());
848 }
849 }
850 }
ead1e424
ILT
851 break;
852
853 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
854 // If the symbol is fully resolved, this is just a PC32 reloc.
855 // Otherwise we need a PLT entry.
7e1edb90 856 if (gsym->final_value_is_known())
ead1e424 857 break;
7e1edb90 858 target->make_plt_entry(symtab, layout, gsym);
ead1e424
ILT
859 break;
860
861 case elfcpp::R_386_GOTOFF:
862 case elfcpp::R_386_GOTPC:
863 // We need a GOT section.
7e1edb90 864 target->got_section(symtab, layout);
ead1e424
ILT
865 break;
866
92e059d8
ILT
867 case elfcpp::R_386_COPY:
868 case elfcpp::R_386_GLOB_DAT:
869 case elfcpp::R_386_JUMP_SLOT:
870 case elfcpp::R_386_RELATIVE:
871 case elfcpp::R_386_TLS_TPOFF:
872 case elfcpp::R_386_TLS_DTPMOD32:
873 case elfcpp::R_386_TLS_DTPOFF32:
874 case elfcpp::R_386_TLS_TPOFF32:
875 case elfcpp::R_386_TLS_DESC:
876 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
877 program_name, object->name().c_str(), r_type);
878 gold_exit(false);
879 break;
880
881 case elfcpp::R_386_TLS_IE:
882 case elfcpp::R_386_TLS_GOTIE:
883 case elfcpp::R_386_TLS_LE:
884 case elfcpp::R_386_TLS_GD:
885 case elfcpp::R_386_TLS_LDM:
886 case elfcpp::R_386_TLS_LDO_32:
887 case elfcpp::R_386_TLS_IE_32:
888 case elfcpp::R_386_TLS_LE_32:
889 case elfcpp::R_386_TLS_GOTDESC:
890 case elfcpp::R_386_TLS_DESC_CALL:
a3ad94ed 891 {
7e1edb90
ILT
892 const bool is_final = gsym->final_value_is_known();
893 r_type = Target_i386::optimize_tls_reloc(is_final, r_type);
a3ad94ed
ILT
894 switch (r_type)
895 {
896 case elfcpp::R_386_TLS_LE:
897 case elfcpp::R_386_TLS_LE_32:
898 // FIXME: If generating a shared object, we need to copy
899 // this relocation into the object.
7e1edb90 900 gold_assert(!parameters->output_is_shared());
a3ad94ed
ILT
901 break;
902
903 case elfcpp::R_386_TLS_IE:
904 case elfcpp::R_386_TLS_GOTIE:
905 case elfcpp::R_386_TLS_GD:
906 case elfcpp::R_386_TLS_LDM:
907 case elfcpp::R_386_TLS_LDO_32:
908 case elfcpp::R_386_TLS_IE_32:
909 case elfcpp::R_386_TLS_GOTDESC:
910 case elfcpp::R_386_TLS_DESC_CALL:
911 fprintf(stderr,
912 _("%s: %s: unsupported reloc %u "
913 "against global symbol %s\n"),
914 program_name, object->name().c_str(), r_type,
915 gsym->name());
916 break;
917 }
918 }
92e059d8
ILT
919 break;
920
92e059d8
ILT
921 case elfcpp::R_386_32PLT:
922 case elfcpp::R_386_TLS_GD_32:
923 case elfcpp::R_386_TLS_GD_PUSH:
924 case elfcpp::R_386_TLS_GD_CALL:
925 case elfcpp::R_386_TLS_GD_POP:
926 case elfcpp::R_386_TLS_LDM_32:
927 case elfcpp::R_386_TLS_LDM_PUSH:
928 case elfcpp::R_386_TLS_LDM_CALL:
929 case elfcpp::R_386_TLS_LDM_POP:
930 case elfcpp::R_386_USED_BY_INTEL_200:
931 default:
932 fprintf(stderr,
933 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
934 program_name, object->name().c_str(), r_type, gsym->name());
935 break;
936 }
937}
938
939// Scan relocations for a section.
940
941void
942Target_i386::scan_relocs(const General_options& options,
943 Symbol_table* symtab,
ead1e424 944 Layout* layout,
f6ce93d6 945 Sized_relobj<32, false>* object,
a3ad94ed 946 unsigned int data_shndx,
92e059d8
ILT
947 unsigned int sh_type,
948 const unsigned char* prelocs,
949 size_t reloc_count,
950 size_t local_symbol_count,
951 const unsigned char* plocal_symbols,
952 Symbol** global_symbols)
953{
954 if (sh_type == elfcpp::SHT_RELA)
955 {
956 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
957 program_name, object->name().c_str());
958 gold_exit(false);
959 }
960
ead1e424
ILT
961 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
962 Target_i386::Scan>(
92e059d8
ILT
963 options,
964 symtab,
ead1e424
ILT
965 layout,
966 this,
92e059d8 967 object,
a3ad94ed 968 data_shndx,
92e059d8
ILT
969 prelocs,
970 reloc_count,
971 local_symbol_count,
972 plocal_symbols,
973 global_symbols);
974}
975
16649710 976// Finalize the sections.
5a6f7e2d
ILT
977
978void
7e1edb90 979Target_i386::do_finalize_sections(Layout* layout)
5a6f7e2d 980{
16649710
ILT
981 // Fill in some more dynamic tags.
982 Output_data_dynamic* const odyn = layout->dynamic_data();
983 if (odyn != NULL)
984 {
985 if (this->got_plt_ != NULL)
986 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
987
988 if (this->plt_ != NULL)
989 {
990 const Output_data* od = this->plt_->rel_plt();
991 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
992 odyn->add_section_address(elfcpp::DT_JMPREL, od);
993 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
994 }
995
996 if (this->rel_dyn_ != NULL)
997 {
998 const Output_data* od = this->rel_dyn_;
999 odyn->add_section_address(elfcpp::DT_REL, od);
1000 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1001 odyn->add_constant(elfcpp::DT_RELENT,
1002 elfcpp::Elf_sizes<32>::rel_size);
1003 }
1004
7e1edb90 1005 if (!parameters->output_is_shared())
16649710
ILT
1006 {
1007 // The value of the DT_DEBUG tag is filled in by the dynamic
1008 // linker at run time, and used by the debugger.
1009 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1010 }
1011 }
1012
1013 // Emit any relocs we saved in an attempt to avoid generating COPY
1014 // relocs.
5a6f7e2d
ILT
1015 if (this->copy_relocs_ == NULL)
1016 return;
1017 if (this->copy_relocs_->any_to_emit())
1018 {
1019 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1020 this->copy_relocs_->emit(rel_dyn);
1021 }
1022 delete this->copy_relocs_;
1023 this->copy_relocs_ = NULL;
1024}
1025
61ba1cf9
ILT
1026// Perform a relocation.
1027
ead1e424 1028inline bool
92e059d8 1029Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
ead1e424 1030 Target_i386* target,
92e059d8
ILT
1031 size_t relnum,
1032 const elfcpp::Rel<32, false>& rel,
1033 unsigned int r_type,
c06b7b0b 1034 const Sized_symbol<32>* gsym,
b8e6aad9 1035 const Symbol_value<32>* psymval,
92e059d8
ILT
1036 unsigned char* view,
1037 elfcpp::Elf_types<32>::Elf_Addr address,
1038 off_t view_size)
61ba1cf9 1039{
ead1e424
ILT
1040 if (this->skip_call_tls_get_addr_)
1041 {
1042 if (r_type != elfcpp::R_386_PLT32
1043 || gsym == NULL
1044 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1045 {
1046 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1047 program_name,
1048 relinfo->location(relnum, rel.get_r_offset()).c_str());
1049 gold_exit(false);
1050 }
1051
1052 this->skip_call_tls_get_addr_ = false;
1053
1054 return false;
1055 }
1056
a3ad94ed 1057 // Pick the value to use for symbols defined in shared objects.
b8e6aad9 1058 Symbol_value<32> symval;
8462ae85 1059 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
a3ad94ed 1060 {
b8e6aad9
ILT
1061 symval.set_output_value(target->plt_section()->address()
1062 + gsym->plt_offset());
1063 psymval = &symval;
a3ad94ed
ILT
1064 }
1065
b8e6aad9
ILT
1066 const Sized_relobj<32, false>* object = relinfo->object;
1067
61ba1cf9
ILT
1068 switch (r_type)
1069 {
1070 case elfcpp::R_386_NONE:
92e059d8
ILT
1071 case elfcpp::R_386_GNU_VTINHERIT:
1072 case elfcpp::R_386_GNU_VTENTRY:
61ba1cf9
ILT
1073 break;
1074
1075 case elfcpp::R_386_32:
b8e6aad9 1076 Relocate_functions<32, false>::rel32(view, object, psymval);
61ba1cf9
ILT
1077 break;
1078
1079 case elfcpp::R_386_PC32:
b8e6aad9 1080 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
92e059d8
ILT
1081 break;
1082
1083 case elfcpp::R_386_16:
b8e6aad9 1084 Relocate_functions<32, false>::rel16(view, object, psymval);
92e059d8
ILT
1085 break;
1086
1087 case elfcpp::R_386_PC16:
b8e6aad9 1088 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
61ba1cf9
ILT
1089 break;
1090
92e059d8 1091 case elfcpp::R_386_8:
b8e6aad9 1092 Relocate_functions<32, false>::rel8(view, object, psymval);
92e059d8
ILT
1093 break;
1094
1095 case elfcpp::R_386_PC8:
b8e6aad9 1096 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
92e059d8
ILT
1097 break;
1098
ead1e424 1099 case elfcpp::R_386_PLT32:
a3ad94ed 1100 gold_assert(gsym->has_plt_offset()
7e1edb90 1101 || gsym->final_value_is_known());
b8e6aad9 1102 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
ead1e424
ILT
1103 break;
1104
1105 case elfcpp::R_386_GOT32:
1106 // Local GOT offsets not yet supported.
a3ad94ed
ILT
1107 gold_assert(gsym);
1108 gold_assert(gsym->has_got_offset());
b8e6aad9 1109 Relocate_functions<32, false>::rel32(view, gsym->got_offset());
ead1e424
ILT
1110 break;
1111
1112 case elfcpp::R_386_GOTOFF:
b8e6aad9
ILT
1113 {
1114 elfcpp::Elf_types<32>::Elf_Addr value;
1115 value = (psymval->value(object, 0)
7e1edb90 1116 - target->got_section(NULL, NULL)->address());
b8e6aad9
ILT
1117 Relocate_functions<32, false>::rel32(view, value);
1118 }
ead1e424
ILT
1119 break;
1120
1121 case elfcpp::R_386_GOTPC:
b8e6aad9
ILT
1122 {
1123 elfcpp::Elf_types<32>::Elf_Addr value;
7e1edb90 1124 value = target->got_section(NULL, NULL)->address();
b8e6aad9
ILT
1125 Relocate_functions<32, false>::pcrel32(view, value, address);
1126 }
ead1e424
ILT
1127 break;
1128
92e059d8
ILT
1129 case elfcpp::R_386_COPY:
1130 case elfcpp::R_386_GLOB_DAT:
1131 case elfcpp::R_386_JUMP_SLOT:
1132 case elfcpp::R_386_RELATIVE:
1133 case elfcpp::R_386_TLS_TPOFF:
1134 case elfcpp::R_386_TLS_DTPMOD32:
1135 case elfcpp::R_386_TLS_DTPOFF32:
1136 case elfcpp::R_386_TLS_TPOFF32:
1137 case elfcpp::R_386_TLS_DESC:
1138 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1139 program_name,
1140 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1141 r_type);
1142 gold_exit(false);
1143 break;
1144
1145 case elfcpp::R_386_TLS_IE:
1146 case elfcpp::R_386_TLS_GOTIE:
1147 case elfcpp::R_386_TLS_LE:
1148 case elfcpp::R_386_TLS_GD:
1149 case elfcpp::R_386_TLS_LDM:
1150 case elfcpp::R_386_TLS_LDO_32:
1151 case elfcpp::R_386_TLS_IE_32:
1152 case elfcpp::R_386_TLS_LE_32:
1153 case elfcpp::R_386_TLS_GOTDESC:
1154 case elfcpp::R_386_TLS_DESC_CALL:
b8e6aad9 1155 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
ead1e424 1156 address, view_size);
92e059d8
ILT
1157 break;
1158
92e059d8
ILT
1159 case elfcpp::R_386_32PLT:
1160 case elfcpp::R_386_TLS_GD_32:
1161 case elfcpp::R_386_TLS_GD_PUSH:
1162 case elfcpp::R_386_TLS_GD_CALL:
1163 case elfcpp::R_386_TLS_GD_POP:
1164 case elfcpp::R_386_TLS_LDM_32:
1165 case elfcpp::R_386_TLS_LDM_PUSH:
1166 case elfcpp::R_386_TLS_LDM_CALL:
1167 case elfcpp::R_386_TLS_LDM_POP:
1168 case elfcpp::R_386_USED_BY_INTEL_200:
61ba1cf9
ILT
1169 default:
1170 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
92e059d8
ILT
1171 program_name,
1172 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1173 r_type);
61ba1cf9 1174 // gold_exit(false);
92e059d8
ILT
1175 break;
1176 }
ead1e424
ILT
1177
1178 return true;
92e059d8
ILT
1179}
1180
1181// Perform a TLS relocation.
1182
1183inline void
1184Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1185 size_t relnum,
1186 const elfcpp::Rel<32, false>& rel,
1187 unsigned int r_type,
c06b7b0b 1188 const Sized_symbol<32>* gsym,
b8e6aad9 1189 const Symbol_value<32>* psymval,
92e059d8
ILT
1190 unsigned char* view,
1191 elfcpp::Elf_types<32>::Elf_Addr,
1192 off_t view_size)
1193{
1194 Output_segment* tls_segment = relinfo->layout->tls_segment();
1195 if (tls_segment == NULL)
1196 {
1197 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1198 program_name,
1199 relinfo->location(relnum, rel.get_r_offset()).c_str());
1200 gold_exit(false);
1201 }
1202
b8e6aad9
ILT
1203 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1204
a3ad94ed 1205 const bool is_final = (gsym == NULL
7e1edb90
ILT
1206 ? !parameters->output_is_shared()
1207 : gsym->final_value_is_known());
92e059d8 1208 const unsigned int opt_r_type =
7e1edb90 1209 Target_i386::optimize_tls_reloc(is_final, r_type);
92e059d8
ILT
1210 switch (r_type)
1211 {
1212 case elfcpp::R_386_TLS_LE_32:
1213 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1214 Relocate_functions<32, false>::rel32(view, value);
1215 break;
1216
1217 case elfcpp::R_386_TLS_LE:
1218 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1219 Relocate_functions<32, false>::rel32(view, value);
1220 break;
1221
1222 case elfcpp::R_386_TLS_IE:
1223 case elfcpp::R_386_TLS_GOTIE:
1224 case elfcpp::R_386_TLS_IE_32:
1225 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1226 {
1227 Target_i386::Relocate::tls_ie_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 type %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
1239 case elfcpp::R_386_TLS_GD:
ead1e424
ILT
1240 if (opt_r_type == elfcpp::R_386_TLS_LE_32)
1241 {
1242 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1243 rel, r_type, value, view,
1244 view_size);
1245 break;
1246 }
1247 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1248 program_name,
1249 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1250 r_type);
1251 // gold_exit(false);
1252 break;
1253
92e059d8
ILT
1254 case elfcpp::R_386_TLS_LDM:
1255 case elfcpp::R_386_TLS_LDO_32:
1256 case elfcpp::R_386_TLS_GOTDESC:
1257 case elfcpp::R_386_TLS_DESC_CALL:
1258 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1259 program_name,
1260 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1261 r_type);
1262 // gold_exit(false);
1263 break;
1264 }
1265}
1266
1267// Do a relocation in which we convert a TLS Initial-Exec to a
1268// Local-Exec.
1269
1270inline void
1271Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1272 size_t relnum,
1273 Output_segment* tls_segment,
1274 const elfcpp::Rel<32, false>& rel,
1275 unsigned int r_type,
1276 elfcpp::Elf_types<32>::Elf_Addr value,
1277 unsigned char* view,
1278 off_t view_size)
1279{
1280 // We have to actually change the instructions, which means that we
1281 // need to examine the opcodes to figure out which instruction we
1282 // are looking at.
1283 if (r_type == elfcpp::R_386_TLS_IE)
1284 {
1285 // movl %gs:XX,%eax ==> movl $YY,%eax
1286 // movl %gs:XX,%reg ==> movl $YY,%reg
1287 // addl %gs:XX,%reg ==> addl $YY,%reg
1288 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -1);
1289 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1290
1291 unsigned char op1 = view[-1];
1292 if (op1 == 0xa1)
1293 {
1294 // movl XX,%eax ==> movl $YY,%eax
1295 view[-1] = 0xb8;
1296 }
1297 else
1298 {
1299 Target_i386::Relocate::check_range(relinfo, relnum, rel,
1300 view_size, -2);
1301
1302 unsigned char op2 = view[-2];
1303 if (op2 == 0x8b)
1304 {
1305 // movl XX,%reg ==> movl $YY,%reg
1306 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1307 (op1 & 0xc7) == 0x05);
1308 view[-2] = 0xc7;
1309 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1310 }
1311 else if (op2 == 0x03)
1312 {
1313 // addl XX,%reg ==> addl $YY,%reg
1314 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1315 (op1 & 0xc7) == 0x05);
1316 view[-2] = 0x81;
1317 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1318 }
1319 else
1320 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1321 }
1322 }
1323 else
1324 {
1325 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1326 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1327 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1328 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1329 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 4);
1330
1331 unsigned char op1 = view[-1];
1332 unsigned char op2 = view[-2];
1333 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1334 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1335 if (op2 == 0x8b)
1336 {
1337 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1338 view[-2] = 0xc7;
1339 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1340 }
1341 else if (op2 == 0x2b)
1342 {
1343 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1344 view[-2] = 0x81;
1345 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1346 }
1347 else if (op2 == 0x03)
1348 {
1349 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1350 view[-2] = 0x81;
1351 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1352 }
1353 else
1354 Target_i386::Relocate::check_tls(relinfo, relnum, rel, 0);
1355 }
1356
ead1e424
ILT
1357 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1358 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1359 value = - value;
92e059d8
ILT
1360
1361 Relocate_functions<32, false>::rel32(view, value);
1362}
1363
ead1e424
ILT
1364// Do a relocation in which we convert a TLS Global-Dynamic to a
1365// Local-Exec.
1366
1367inline void
1368Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1369 size_t relnum,
1370 Output_segment* tls_segment,
1371 const elfcpp::Rel<32, false>& rel,
1372 unsigned int,
1373 elfcpp::Elf_types<32>::Elf_Addr value,
1374 unsigned char* view,
1375 off_t view_size)
1376{
1377 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1378 // ==> movl %gs,0,%eax; subl $foo@tpoff,%eax
1379 // leal foo(%reg),%eax; call ___tls_get_addr
1380 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1381
1382 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -2);
1383 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, 9);
1384
1385 unsigned char op1 = view[-1];
1386 unsigned char op2 = view[-2];
1387
1388 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1389 op2 == 0x8d || op2 == 0x04);
1390 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1391 view[4] == 0xe8);
1392
1393 int roff = 5;
1394
1395 if (op2 == 0x04)
1396 {
1397 Target_i386::Relocate::check_range(relinfo, relnum, rel, view_size, -3);
1398 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1399 view[-3] == 0x8d);
1400 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1401 ((op1 & 0xc7) == 0x05
1402 && op1 != (4 << 3)));
1403 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1404 }
1405 else
1406 {
1407 Target_i386::Relocate::check_tls(relinfo, relnum, rel,
1408 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1409 if (rel.get_r_offset() + 9 < view_size && view[9] == 0x90)
1410 {
1411 // There is a trailing nop. Use the size byte subl.
1412 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1413 roff = 6;
1414 }
1415 else
1416 {
1417 // Use the five byte subl.
1418 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1419 }
1420 }
1421
1422 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1423 Relocate_functions<32, false>::rel32(view + roff, value);
1424
1425 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1426 // We can skip it.
1427 this->skip_call_tls_get_addr_ = true;
1428}
1429
92e059d8
ILT
1430// Check the range for a TLS relocation.
1431
1432inline void
1433Target_i386::Relocate::check_range(const Relocate_info<32, false>* relinfo,
1434 size_t relnum,
1435 const elfcpp::Rel<32, false>& rel,
1436 off_t view_size, off_t off)
1437{
1438 off_t offset = rel.get_r_offset() + off;
1439 if (offset < 0 || offset > view_size)
1440 {
1441 fprintf(stderr, _("%s: %s: TLS relocation out of range\n"),
1442 program_name,
1443 relinfo->location(relnum, rel.get_r_offset()).c_str());
1444 gold_exit(false);
1445 }
1446}
1447
1448// Check the validity of a TLS relocation. This is like assert.
1449
1450inline void
1451Target_i386::Relocate::check_tls(const Relocate_info<32, false>* relinfo,
1452 size_t relnum,
1453 const elfcpp::Rel<32, false>& rel,
1454 bool valid)
1455{
1456 if (!valid)
1457 {
1458 fprintf(stderr,
1459 _("%s: %s: TLS relocation against invalid instruction\n"),
1460 program_name,
1461 relinfo->location(relnum, rel.get_r_offset()).c_str());
1462 gold_exit(false);
61ba1cf9
ILT
1463 }
1464}
1465
1466// Relocate section data.
1467
1468void
92e059d8 1469Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
1470 unsigned int sh_type,
1471 const unsigned char* prelocs,
1472 size_t reloc_count,
61ba1cf9
ILT
1473 unsigned char* view,
1474 elfcpp::Elf_types<32>::Elf_Addr address,
1475 off_t view_size)
1476{
a3ad94ed 1477 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 1478
ead1e424
ILT
1479 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1480 Target_i386::Relocate>(
92e059d8 1481 relinfo,
ead1e424 1482 this,
61ba1cf9
ILT
1483 prelocs,
1484 reloc_count,
61ba1cf9
ILT
1485 view,
1486 address,
1487 view_size);
1488}
1489
c51e6221
ILT
1490// Return a string used to fill a code section with nops to take up
1491// the specified length.
1492
1493std::string
1494Target_i386::do_code_fill(off_t length)
1495{
1496 if (length >= 16)
1497 {
1498 // Build a jmp instruction to skip over the bytes.
1499 unsigned char jmp[5];
1500 jmp[0] = 0xe9;
1501 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1502 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1503 + std::string(length - 5, '\0'));
1504 }
1505
1506 // Nop sequences of various lengths.
1507 const char nop1[1] = { 0x90 }; // nop
1508 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1509 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1510 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1511 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1512 0x00 }; // leal 0(%esi,1),%esi
1513 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1514 0x00, 0x00 };
1515 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1516 0x00, 0x00, 0x00 };
1517 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1518 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1519 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1520 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1521 0x00 };
1522 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1523 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1524 0x00, 0x00 };
1525 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1526 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1527 0x00, 0x00, 0x00 };
1528 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1529 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1530 0x00, 0x00, 0x00, 0x00 };
1531 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1532 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1533 0x27, 0x00, 0x00, 0x00,
1534 0x00 };
1535 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1536 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1537 0xbc, 0x27, 0x00, 0x00,
1538 0x00, 0x00 };
1539 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1540 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1541 0x90, 0x90, 0x90, 0x90,
1542 0x90, 0x90, 0x90 };
1543
1544 const char* nops[16] = {
1545 NULL,
1546 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1547 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1548 };
1549
1550 return std::string(nops[length], length);
1551}
1552
14bfc3f5
ILT
1553// The selector for i386 object files.
1554
1555class Target_selector_i386 : public Target_selector
1556{
1557public:
1558 Target_selector_i386()
1559 : Target_selector(elfcpp::EM_386, 32, false)
1560 { }
1561
1562 Target*
ead1e424
ILT
1563 recognize(int machine, int osabi, int abiversion);
1564
1565 private:
1566 Target_i386* target_;
14bfc3f5
ILT
1567};
1568
1569// Recognize an i386 object file when we already know that the machine
1570// number is EM_386.
1571
1572Target*
ead1e424 1573Target_selector_i386::recognize(int, int, int)
14bfc3f5 1574{
ead1e424
ILT
1575 if (this->target_ == NULL)
1576 this->target_ = new Target_i386();
1577 return this->target_;
14bfc3f5
ILT
1578}
1579
1580Target_selector_i386 target_selector_i386;
1581
1582} // End anonymous namespace.
This page took 0.142523 seconds and 4 git commands to generate.