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