e033d34382a589dfd2bfca8c87b3fbcbd90ad3a4
[deliverable/binutils-gdb.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009 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 "copy-relocs.h"
36 #include "target.h"
37 #include "target-reloc.h"
38 #include "target-select.h"
39 #include "tls.h"
40
41 namespace
42 {
43
44 using namespace gold;
45
46 class Output_data_plt_i386;
47
48 // The i386 target class.
49 // TLS info comes from
50 // http://people.redhat.com/drepper/tls.pdf
51 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
52
53 class Target_i386 : public Sized_target<32, false>
54 {
55 public:
56 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
57
58 Target_i386()
59 : Sized_target<32, false>(&i386_info),
60 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
61 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
62 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
63 { }
64
65 // Process the relocations to determine unreferenced sections for
66 // garbage collection.
67 void
68 gc_process_relocs(const General_options& options,
69 Symbol_table* symtab,
70 Layout* layout,
71 Sized_relobj<32, false>* object,
72 unsigned int data_shndx,
73 unsigned int sh_type,
74 const unsigned char* prelocs,
75 size_t reloc_count,
76 Output_section* output_section,
77 bool needs_special_offset_handling,
78 size_t local_symbol_count,
79 const unsigned char* plocal_symbols);
80
81 // Scan the relocations to look for symbol adjustments.
82 void
83 scan_relocs(const General_options& options,
84 Symbol_table* symtab,
85 Layout* layout,
86 Sized_relobj<32, false>* object,
87 unsigned int data_shndx,
88 unsigned int sh_type,
89 const unsigned char* prelocs,
90 size_t reloc_count,
91 Output_section* output_section,
92 bool needs_special_offset_handling,
93 size_t local_symbol_count,
94 const unsigned char* plocal_symbols);
95
96 // Finalize the sections.
97 void
98 do_finalize_sections(Layout*);
99
100 // Return the value to use for a dynamic which requires special
101 // treatment.
102 uint64_t
103 do_dynsym_value(const Symbol*) const;
104
105 // Relocate a section.
106 void
107 relocate_section(const Relocate_info<32, false>*,
108 unsigned int sh_type,
109 const unsigned char* prelocs,
110 size_t reloc_count,
111 Output_section* output_section,
112 bool needs_special_offset_handling,
113 unsigned char* view,
114 elfcpp::Elf_types<32>::Elf_Addr view_address,
115 section_size_type view_size);
116
117 // Scan the relocs during a relocatable link.
118 void
119 scan_relocatable_relocs(const General_options& options,
120 Symbol_table* symtab,
121 Layout* layout,
122 Sized_relobj<32, false>* object,
123 unsigned int data_shndx,
124 unsigned int sh_type,
125 const unsigned char* prelocs,
126 size_t reloc_count,
127 Output_section* output_section,
128 bool needs_special_offset_handling,
129 size_t local_symbol_count,
130 const unsigned char* plocal_symbols,
131 Relocatable_relocs*);
132
133 // Relocate a section during a relocatable link.
134 void
135 relocate_for_relocatable(const Relocate_info<32, false>*,
136 unsigned int sh_type,
137 const unsigned char* prelocs,
138 size_t reloc_count,
139 Output_section* output_section,
140 off_t offset_in_output_section,
141 const Relocatable_relocs*,
142 unsigned char* view,
143 elfcpp::Elf_types<32>::Elf_Addr view_address,
144 section_size_type view_size,
145 unsigned char* reloc_view,
146 section_size_type reloc_view_size);
147
148 // Return a string used to fill a code section with nops.
149 std::string
150 do_code_fill(section_size_type length) const;
151
152 // Return whether SYM is defined by the ABI.
153 bool
154 do_is_defined_by_abi(const Symbol* sym) const
155 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
156
157 // Return the size of the GOT section.
158 section_size_type
159 got_size()
160 {
161 gold_assert(this->got_ != NULL);
162 return this->got_->data_size();
163 }
164
165 private:
166 // The class which scans relocations.
167 struct Scan
168 {
169 inline void
170 local(const General_options& options, Symbol_table* symtab,
171 Layout* layout, Target_i386* target,
172 Sized_relobj<32, false>* object,
173 unsigned int data_shndx,
174 Output_section* output_section,
175 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
176 const elfcpp::Sym<32, false>& lsym);
177
178 inline void
179 global(const General_options& options, Symbol_table* symtab,
180 Layout* layout, Target_i386* target,
181 Sized_relobj<32, false>* object,
182 unsigned int data_shndx,
183 Output_section* output_section,
184 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
185 Symbol* gsym);
186
187 static void
188 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
189
190 static void
191 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
192 Symbol*);
193 };
194
195 // The class which implements relocation.
196 class Relocate
197 {
198 public:
199 Relocate()
200 : skip_call_tls_get_addr_(false),
201 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
202 { }
203
204 ~Relocate()
205 {
206 if (this->skip_call_tls_get_addr_)
207 {
208 // FIXME: This needs to specify the location somehow.
209 gold_error(_("missing expected TLS relocation"));
210 }
211 }
212
213 // Return whether the static relocation needs to be applied.
214 inline bool
215 should_apply_static_reloc(const Sized_symbol<32>* gsym,
216 int ref_flags,
217 bool is_32bit);
218
219 // Do a relocation. Return false if the caller should not issue
220 // any warnings about this relocation.
221 inline bool
222 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
223 const elfcpp::Rel<32, false>&,
224 unsigned int r_type, const Sized_symbol<32>*,
225 const Symbol_value<32>*,
226 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
227 section_size_type);
228
229 private:
230 // Do a TLS relocation.
231 inline void
232 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
233 size_t relnum, const elfcpp::Rel<32, false>&,
234 unsigned int r_type, const Sized_symbol<32>*,
235 const Symbol_value<32>*,
236 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
237 section_size_type);
238
239 // Do a TLS General-Dynamic to Initial-Exec transition.
240 inline void
241 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
242 Output_segment* tls_segment,
243 const elfcpp::Rel<32, false>&, unsigned int r_type,
244 elfcpp::Elf_types<32>::Elf_Addr value,
245 unsigned char* view,
246 section_size_type view_size);
247
248 // Do a TLS General-Dynamic to Local-Exec transition.
249 inline void
250 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
251 Output_segment* tls_segment,
252 const elfcpp::Rel<32, false>&, unsigned int r_type,
253 elfcpp::Elf_types<32>::Elf_Addr value,
254 unsigned char* view,
255 section_size_type view_size);
256
257 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
258 // transition.
259 inline void
260 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
261 Output_segment* tls_segment,
262 const elfcpp::Rel<32, false>&, unsigned int r_type,
263 elfcpp::Elf_types<32>::Elf_Addr value,
264 unsigned char* view,
265 section_size_type view_size);
266
267 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
268 // transition.
269 inline void
270 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
271 Output_segment* tls_segment,
272 const elfcpp::Rel<32, false>&, unsigned int r_type,
273 elfcpp::Elf_types<32>::Elf_Addr value,
274 unsigned char* view,
275 section_size_type view_size);
276
277 // Do a TLS Local-Dynamic to Local-Exec transition.
278 inline void
279 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
280 Output_segment* tls_segment,
281 const elfcpp::Rel<32, false>&, unsigned int r_type,
282 elfcpp::Elf_types<32>::Elf_Addr value,
283 unsigned char* view,
284 section_size_type view_size);
285
286 // Do a TLS Initial-Exec to Local-Exec transition.
287 static inline void
288 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
289 Output_segment* tls_segment,
290 const elfcpp::Rel<32, false>&, unsigned int r_type,
291 elfcpp::Elf_types<32>::Elf_Addr value,
292 unsigned char* view,
293 section_size_type view_size);
294
295 // We need to keep track of which type of local dynamic relocation
296 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
297 enum Local_dynamic_type
298 {
299 LOCAL_DYNAMIC_NONE,
300 LOCAL_DYNAMIC_SUN,
301 LOCAL_DYNAMIC_GNU
302 };
303
304 // This is set if we should skip the next reloc, which should be a
305 // PLT32 reloc against ___tls_get_addr.
306 bool skip_call_tls_get_addr_;
307 // The type of local dynamic relocation we have seen in the section
308 // being relocated, if any.
309 Local_dynamic_type local_dynamic_type_;
310 };
311
312 // A class which returns the size required for a relocation type,
313 // used while scanning relocs during a relocatable link.
314 class Relocatable_size_for_reloc
315 {
316 public:
317 unsigned int
318 get_size_for_reloc(unsigned int, Relobj*);
319 };
320
321 // Adjust TLS relocation type based on the options and whether this
322 // is a local symbol.
323 static tls::Tls_optimization
324 optimize_tls_reloc(bool is_final, int r_type);
325
326 // Get the GOT section, creating it if necessary.
327 Output_data_got<32, false>*
328 got_section(Symbol_table*, Layout*);
329
330 // Get the GOT PLT section.
331 Output_data_space*
332 got_plt_section() const
333 {
334 gold_assert(this->got_plt_ != NULL);
335 return this->got_plt_;
336 }
337
338 // Create a PLT entry for a global symbol.
339 void
340 make_plt_entry(Symbol_table*, Layout*, Symbol*);
341
342 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
343 void
344 define_tls_base_symbol(Symbol_table*, Layout*);
345
346 // Create a GOT entry for the TLS module index.
347 unsigned int
348 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
349 Sized_relobj<32, false>* object);
350
351 // Get the PLT section.
352 const Output_data_plt_i386*
353 plt_section() const
354 {
355 gold_assert(this->plt_ != NULL);
356 return this->plt_;
357 }
358
359 // Get the dynamic reloc section, creating it if necessary.
360 Reloc_section*
361 rel_dyn_section(Layout*);
362
363 // Return true if the symbol may need a COPY relocation.
364 // References from an executable object to non-function symbols
365 // defined in a dynamic object may need a COPY relocation.
366 bool
367 may_need_copy_reloc(Symbol* gsym)
368 {
369 return (!parameters->options().shared()
370 && gsym->is_from_dynobj()
371 && gsym->type() != elfcpp::STT_FUNC);
372 }
373
374 // Add a potential copy relocation.
375 void
376 copy_reloc(Symbol_table* symtab, Layout* layout,
377 Sized_relobj<32, false>* object,
378 unsigned int shndx, Output_section* output_section,
379 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
380 {
381 this->copy_relocs_.copy_reloc(symtab, layout,
382 symtab->get_sized_symbol<32>(sym),
383 object, shndx, output_section, reloc,
384 this->rel_dyn_section(layout));
385 }
386
387 // Information about this specific target which we pass to the
388 // general Target structure.
389 static const Target::Target_info i386_info;
390
391 // The types of GOT entries needed for this platform.
392 enum Got_type
393 {
394 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
395 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
396 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
397 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
398 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
399 };
400
401 // The GOT section.
402 Output_data_got<32, false>* got_;
403 // The PLT section.
404 Output_data_plt_i386* plt_;
405 // The GOT PLT section.
406 Output_data_space* got_plt_;
407 // The dynamic reloc section.
408 Reloc_section* rel_dyn_;
409 // Relocs saved to avoid a COPY reloc.
410 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
411 // Space for variables copied with a COPY reloc.
412 Output_data_space* dynbss_;
413 // Offset of the GOT entry for the TLS module index.
414 unsigned int got_mod_index_offset_;
415 // True if the _TLS_MODULE_BASE_ symbol has been defined.
416 bool tls_base_symbol_defined_;
417 };
418
419 const Target::Target_info Target_i386::i386_info =
420 {
421 32, // size
422 false, // is_big_endian
423 elfcpp::EM_386, // machine_code
424 false, // has_make_symbol
425 false, // has_resolve
426 true, // has_code_fill
427 true, // is_default_stack_executable
428 '\0', // wrap_char
429 "/usr/lib/libc.so.1", // dynamic_linker
430 0x08048000, // default_text_segment_address
431 0x1000, // abi_pagesize (overridable by -z max-page-size)
432 0x1000 // common_pagesize (overridable by -z common-page-size)
433 };
434
435 // Get the GOT section, creating it if necessary.
436
437 Output_data_got<32, false>*
438 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
439 {
440 if (this->got_ == NULL)
441 {
442 gold_assert(symtab != NULL && layout != NULL);
443
444 this->got_ = new Output_data_got<32, false>();
445
446 Output_section* os;
447 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
448 (elfcpp::SHF_ALLOC
449 | elfcpp::SHF_WRITE),
450 this->got_);
451 os->set_is_relro();
452
453 // The old GNU linker creates a .got.plt section. We just
454 // create another set of data in the .got section. Note that we
455 // always create a PLT if we create a GOT, although the PLT
456 // might be empty.
457 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
458 os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
459 (elfcpp::SHF_ALLOC
460 | elfcpp::SHF_WRITE),
461 this->got_plt_);
462 os->set_is_relro();
463
464 // The first three entries are reserved.
465 this->got_plt_->set_current_data_size(3 * 4);
466
467 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
468 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
469 this->got_plt_,
470 0, 0, elfcpp::STT_OBJECT,
471 elfcpp::STB_LOCAL,
472 elfcpp::STV_HIDDEN, 0,
473 false, false);
474 }
475
476 return this->got_;
477 }
478
479 // Get the dynamic reloc section, creating it if necessary.
480
481 Target_i386::Reloc_section*
482 Target_i386::rel_dyn_section(Layout* layout)
483 {
484 if (this->rel_dyn_ == NULL)
485 {
486 gold_assert(layout != NULL);
487 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
488 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
489 elfcpp::SHF_ALLOC, this->rel_dyn_);
490 }
491 return this->rel_dyn_;
492 }
493
494 // A class to handle the PLT data.
495
496 class Output_data_plt_i386 : public Output_section_data
497 {
498 public:
499 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
500
501 Output_data_plt_i386(Layout*, Output_data_space*);
502
503 // Add an entry to the PLT.
504 void
505 add_entry(Symbol* gsym);
506
507 // Return the .rel.plt section data.
508 const Reloc_section*
509 rel_plt() const
510 { return this->rel_; }
511
512 protected:
513 void
514 do_adjust_output_section(Output_section* os);
515
516 // Write to a map file.
517 void
518 do_print_to_mapfile(Mapfile* mapfile) const
519 { mapfile->print_output_data(this, _("** PLT")); }
520
521 private:
522 // The size of an entry in the PLT.
523 static const int plt_entry_size = 16;
524
525 // The first entry in the PLT for an executable.
526 static unsigned char exec_first_plt_entry[plt_entry_size];
527
528 // The first entry in the PLT for a shared object.
529 static unsigned char dyn_first_plt_entry[plt_entry_size];
530
531 // Other entries in the PLT for an executable.
532 static unsigned char exec_plt_entry[plt_entry_size];
533
534 // Other entries in the PLT for a shared object.
535 static unsigned char dyn_plt_entry[plt_entry_size];
536
537 // Set the final size.
538 void
539 set_final_data_size()
540 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
541
542 // Write out the PLT data.
543 void
544 do_write(Output_file*);
545
546 // The reloc section.
547 Reloc_section* rel_;
548 // The .got.plt section.
549 Output_data_space* got_plt_;
550 // The number of PLT entries.
551 unsigned int count_;
552 };
553
554 // Create the PLT section. The ordinary .got section is an argument,
555 // since we need to refer to the start. We also create our own .got
556 // section just for PLT entries.
557
558 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
559 Output_data_space* got_plt)
560 : Output_section_data(4), got_plt_(got_plt), count_(0)
561 {
562 this->rel_ = new Reloc_section(false);
563 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
564 elfcpp::SHF_ALLOC, this->rel_);
565 }
566
567 void
568 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
569 {
570 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
571 // linker, and so do we.
572 os->set_entsize(4);
573 }
574
575 // Add an entry to the PLT.
576
577 void
578 Output_data_plt_i386::add_entry(Symbol* gsym)
579 {
580 gold_assert(!gsym->has_plt_offset());
581
582 // Note that when setting the PLT offset we skip the initial
583 // reserved PLT entry.
584 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
585
586 ++this->count_;
587
588 section_offset_type got_offset = this->got_plt_->current_data_size();
589
590 // Every PLT entry needs a GOT entry which points back to the PLT
591 // entry (this will be changed by the dynamic linker, normally
592 // lazily when the function is called).
593 this->got_plt_->set_current_data_size(got_offset + 4);
594
595 // Every PLT entry needs a reloc.
596 gsym->set_needs_dynsym_entry();
597 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
598 got_offset);
599
600 // Note that we don't need to save the symbol. The contents of the
601 // PLT are independent of which symbols are used. The symbols only
602 // appear in the relocations.
603 }
604
605 // The first entry in the PLT for an executable.
606
607 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
608 {
609 0xff, 0x35, // pushl contents of memory address
610 0, 0, 0, 0, // replaced with address of .got + 4
611 0xff, 0x25, // jmp indirect
612 0, 0, 0, 0, // replaced with address of .got + 8
613 0, 0, 0, 0 // unused
614 };
615
616 // The first entry in the PLT for a shared object.
617
618 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
619 {
620 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
621 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
622 0, 0, 0, 0 // unused
623 };
624
625 // Subsequent entries in the PLT for an executable.
626
627 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
628 {
629 0xff, 0x25, // jmp indirect
630 0, 0, 0, 0, // replaced with address of symbol in .got
631 0x68, // pushl immediate
632 0, 0, 0, 0, // replaced with offset into relocation table
633 0xe9, // jmp relative
634 0, 0, 0, 0 // replaced with offset to start of .plt
635 };
636
637 // Subsequent entries in the PLT for a shared object.
638
639 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
640 {
641 0xff, 0xa3, // jmp *offset(%ebx)
642 0, 0, 0, 0, // replaced with offset of symbol in .got
643 0x68, // pushl immediate
644 0, 0, 0, 0, // replaced with offset into relocation table
645 0xe9, // jmp relative
646 0, 0, 0, 0 // replaced with offset to start of .plt
647 };
648
649 // Write out the PLT. This uses the hand-coded instructions above,
650 // and adjusts them as needed. This is all specified by the i386 ELF
651 // Processor Supplement.
652
653 void
654 Output_data_plt_i386::do_write(Output_file* of)
655 {
656 const off_t offset = this->offset();
657 const section_size_type oview_size =
658 convert_to_section_size_type(this->data_size());
659 unsigned char* const oview = of->get_output_view(offset, oview_size);
660
661 const off_t got_file_offset = this->got_plt_->offset();
662 const section_size_type got_size =
663 convert_to_section_size_type(this->got_plt_->data_size());
664 unsigned char* const got_view = of->get_output_view(got_file_offset,
665 got_size);
666
667 unsigned char* pov = oview;
668
669 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
670 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
671
672 if (parameters->options().shared())
673 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
674 else
675 {
676 memcpy(pov, exec_first_plt_entry, plt_entry_size);
677 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
678 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
679 }
680 pov += plt_entry_size;
681
682 unsigned char* got_pov = got_view;
683
684 memset(got_pov, 0, 12);
685 got_pov += 12;
686
687 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
688
689 unsigned int plt_offset = plt_entry_size;
690 unsigned int plt_rel_offset = 0;
691 unsigned int got_offset = 12;
692 const unsigned int count = this->count_;
693 for (unsigned int i = 0;
694 i < count;
695 ++i,
696 pov += plt_entry_size,
697 got_pov += 4,
698 plt_offset += plt_entry_size,
699 plt_rel_offset += rel_size,
700 got_offset += 4)
701 {
702 // Set and adjust the PLT entry itself.
703
704 if (parameters->options().shared())
705 {
706 memcpy(pov, dyn_plt_entry, plt_entry_size);
707 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
708 }
709 else
710 {
711 memcpy(pov, exec_plt_entry, plt_entry_size);
712 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
713 (got_address
714 + got_offset));
715 }
716
717 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
718 elfcpp::Swap<32, false>::writeval(pov + 12,
719 - (plt_offset + plt_entry_size));
720
721 // Set the entry in the GOT.
722 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
723 }
724
725 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
726 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
727
728 of->write_output_view(offset, oview_size, oview);
729 of->write_output_view(got_file_offset, got_size, got_view);
730 }
731
732 // Create a PLT entry for a global symbol.
733
734 void
735 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
736 {
737 if (gsym->has_plt_offset())
738 return;
739
740 if (this->plt_ == NULL)
741 {
742 // Create the GOT sections first.
743 this->got_section(symtab, layout);
744
745 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
746 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
747 (elfcpp::SHF_ALLOC
748 | elfcpp::SHF_EXECINSTR),
749 this->plt_);
750 }
751
752 this->plt_->add_entry(gsym);
753 }
754
755 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
756
757 void
758 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
759 {
760 if (this->tls_base_symbol_defined_)
761 return;
762
763 Output_segment* tls_segment = layout->tls_segment();
764 if (tls_segment != NULL)
765 {
766 bool is_exec = parameters->options().output_is_executable();
767 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
768 tls_segment, 0, 0,
769 elfcpp::STT_TLS,
770 elfcpp::STB_LOCAL,
771 elfcpp::STV_HIDDEN, 0,
772 (is_exec
773 ? Symbol::SEGMENT_END
774 : Symbol::SEGMENT_START),
775 true);
776 }
777 this->tls_base_symbol_defined_ = true;
778 }
779
780 // Create a GOT entry for the TLS module index.
781
782 unsigned int
783 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
784 Sized_relobj<32, false>* object)
785 {
786 if (this->got_mod_index_offset_ == -1U)
787 {
788 gold_assert(symtab != NULL && layout != NULL && object != NULL);
789 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
790 Output_data_got<32, false>* got = this->got_section(symtab, layout);
791 unsigned int got_offset = got->add_constant(0);
792 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
793 got_offset);
794 got->add_constant(0);
795 this->got_mod_index_offset_ = got_offset;
796 }
797 return this->got_mod_index_offset_;
798 }
799
800 // Optimize the TLS relocation type based on what we know about the
801 // symbol. IS_FINAL is true if the final address of this symbol is
802 // known at link time.
803
804 tls::Tls_optimization
805 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
806 {
807 // If we are generating a shared library, then we can't do anything
808 // in the linker.
809 if (parameters->options().shared())
810 return tls::TLSOPT_NONE;
811
812 switch (r_type)
813 {
814 case elfcpp::R_386_TLS_GD:
815 case elfcpp::R_386_TLS_GOTDESC:
816 case elfcpp::R_386_TLS_DESC_CALL:
817 // These are General-Dynamic which permits fully general TLS
818 // access. Since we know that we are generating an executable,
819 // we can convert this to Initial-Exec. If we also know that
820 // this is a local symbol, we can further switch to Local-Exec.
821 if (is_final)
822 return tls::TLSOPT_TO_LE;
823 return tls::TLSOPT_TO_IE;
824
825 case elfcpp::R_386_TLS_LDM:
826 // This is Local-Dynamic, which refers to a local symbol in the
827 // dynamic TLS block. Since we know that we generating an
828 // executable, we can switch to Local-Exec.
829 return tls::TLSOPT_TO_LE;
830
831 case elfcpp::R_386_TLS_LDO_32:
832 // Another type of Local-Dynamic relocation.
833 return tls::TLSOPT_TO_LE;
834
835 case elfcpp::R_386_TLS_IE:
836 case elfcpp::R_386_TLS_GOTIE:
837 case elfcpp::R_386_TLS_IE_32:
838 // These are Initial-Exec relocs which get the thread offset
839 // from the GOT. If we know that we are linking against the
840 // local symbol, we can switch to Local-Exec, which links the
841 // thread offset into the instruction.
842 if (is_final)
843 return tls::TLSOPT_TO_LE;
844 return tls::TLSOPT_NONE;
845
846 case elfcpp::R_386_TLS_LE:
847 case elfcpp::R_386_TLS_LE_32:
848 // When we already have Local-Exec, there is nothing further we
849 // can do.
850 return tls::TLSOPT_NONE;
851
852 default:
853 gold_unreachable();
854 }
855 }
856
857 // Report an unsupported relocation against a local symbol.
858
859 void
860 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
861 unsigned int r_type)
862 {
863 gold_error(_("%s: unsupported reloc %u against local symbol"),
864 object->name().c_str(), r_type);
865 }
866
867 // Scan a relocation for a local symbol.
868
869 inline void
870 Target_i386::Scan::local(const General_options&,
871 Symbol_table* symtab,
872 Layout* layout,
873 Target_i386* target,
874 Sized_relobj<32, false>* object,
875 unsigned int data_shndx,
876 Output_section* output_section,
877 const elfcpp::Rel<32, false>& reloc,
878 unsigned int r_type,
879 const elfcpp::Sym<32, false>& lsym)
880 {
881 switch (r_type)
882 {
883 case elfcpp::R_386_NONE:
884 case elfcpp::R_386_GNU_VTINHERIT:
885 case elfcpp::R_386_GNU_VTENTRY:
886 break;
887
888 case elfcpp::R_386_32:
889 // If building a shared library (or a position-independent
890 // executable), we need to create a dynamic relocation for
891 // this location. The relocation applied at link time will
892 // apply the link-time value, so we flag the location with
893 // an R_386_RELATIVE relocation so the dynamic loader can
894 // relocate it easily.
895 if (parameters->options().output_is_position_independent())
896 {
897 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
898 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
899 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
900 output_section, data_shndx,
901 reloc.get_r_offset());
902 }
903 break;
904
905 case elfcpp::R_386_16:
906 case elfcpp::R_386_8:
907 // If building a shared library (or a position-independent
908 // executable), we need to create a dynamic relocation for
909 // this location. Because the addend needs to remain in the
910 // data section, we need to be careful not to apply this
911 // relocation statically.
912 if (parameters->options().output_is_position_independent())
913 {
914 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
915 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
916 if (lsym.get_st_type() != elfcpp::STT_SECTION)
917 rel_dyn->add_local(object, r_sym, r_type, output_section,
918 data_shndx, reloc.get_r_offset());
919 else
920 {
921 gold_assert(lsym.get_st_value() == 0);
922 unsigned int shndx = lsym.get_st_shndx();
923 bool is_ordinary;
924 shndx = object->adjust_sym_shndx(r_sym, shndx,
925 &is_ordinary);
926 if (!is_ordinary)
927 object->error(_("section symbol %u has bad shndx %u"),
928 r_sym, shndx);
929 else
930 rel_dyn->add_local_section(object, shndx,
931 r_type, output_section,
932 data_shndx, reloc.get_r_offset());
933 }
934 }
935 break;
936
937 case elfcpp::R_386_PC32:
938 case elfcpp::R_386_PC16:
939 case elfcpp::R_386_PC8:
940 break;
941
942 case elfcpp::R_386_PLT32:
943 // Since we know this is a local symbol, we can handle this as a
944 // PC32 reloc.
945 break;
946
947 case elfcpp::R_386_GOTOFF:
948 case elfcpp::R_386_GOTPC:
949 // We need a GOT section.
950 target->got_section(symtab, layout);
951 break;
952
953 case elfcpp::R_386_GOT32:
954 {
955 // The symbol requires a GOT entry.
956 Output_data_got<32, false>* got = target->got_section(symtab, layout);
957 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
958 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
959 {
960 // If we are generating a shared object, we need to add a
961 // dynamic RELATIVE relocation for this symbol's GOT entry.
962 if (parameters->options().output_is_position_independent())
963 {
964 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
965 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
966 rel_dyn->add_local_relative(
967 object, r_sym, elfcpp::R_386_RELATIVE, got,
968 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
969 }
970 }
971 }
972 break;
973
974 // These are relocations which should only be seen by the
975 // dynamic linker, and should never be seen here.
976 case elfcpp::R_386_COPY:
977 case elfcpp::R_386_GLOB_DAT:
978 case elfcpp::R_386_JUMP_SLOT:
979 case elfcpp::R_386_RELATIVE:
980 case elfcpp::R_386_TLS_TPOFF:
981 case elfcpp::R_386_TLS_DTPMOD32:
982 case elfcpp::R_386_TLS_DTPOFF32:
983 case elfcpp::R_386_TLS_TPOFF32:
984 case elfcpp::R_386_TLS_DESC:
985 gold_error(_("%s: unexpected reloc %u in object file"),
986 object->name().c_str(), r_type);
987 break;
988
989 // These are initial TLS relocs, which are expected when
990 // linking.
991 case elfcpp::R_386_TLS_GD: // Global-dynamic
992 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
993 case elfcpp::R_386_TLS_DESC_CALL:
994 case elfcpp::R_386_TLS_LDM: // Local-dynamic
995 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
996 case elfcpp::R_386_TLS_IE: // Initial-exec
997 case elfcpp::R_386_TLS_IE_32:
998 case elfcpp::R_386_TLS_GOTIE:
999 case elfcpp::R_386_TLS_LE: // Local-exec
1000 case elfcpp::R_386_TLS_LE_32:
1001 {
1002 bool output_is_shared = parameters->options().shared();
1003 const tls::Tls_optimization optimized_type
1004 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1005 switch (r_type)
1006 {
1007 case elfcpp::R_386_TLS_GD: // Global-dynamic
1008 if (optimized_type == tls::TLSOPT_NONE)
1009 {
1010 // Create a pair of GOT entries for the module index and
1011 // dtv-relative offset.
1012 Output_data_got<32, false>* got
1013 = target->got_section(symtab, layout);
1014 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1015 unsigned int shndx = lsym.get_st_shndx();
1016 bool is_ordinary;
1017 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1018 if (!is_ordinary)
1019 object->error(_("local symbol %u has bad shndx %u"),
1020 r_sym, shndx);
1021 else
1022 got->add_local_pair_with_rel(object, r_sym, shndx,
1023 GOT_TYPE_TLS_PAIR,
1024 target->rel_dyn_section(layout),
1025 elfcpp::R_386_TLS_DTPMOD32, 0);
1026 }
1027 else if (optimized_type != tls::TLSOPT_TO_LE)
1028 unsupported_reloc_local(object, r_type);
1029 break;
1030
1031 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1032 target->define_tls_base_symbol(symtab, layout);
1033 if (optimized_type == tls::TLSOPT_NONE)
1034 {
1035 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1036 Output_data_got<32, false>* got
1037 = target->got_section(symtab, layout);
1038 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1039 unsigned int shndx = lsym.get_st_shndx();
1040 bool is_ordinary;
1041 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1042 if (!is_ordinary)
1043 object->error(_("local symbol %u has bad shndx %u"),
1044 r_sym, shndx);
1045 else
1046 got->add_local_pair_with_rel(object, r_sym, shndx,
1047 GOT_TYPE_TLS_DESC,
1048 target->rel_dyn_section(layout),
1049 elfcpp::R_386_TLS_DESC, 0);
1050 }
1051 else if (optimized_type != tls::TLSOPT_TO_LE)
1052 unsupported_reloc_local(object, r_type);
1053 break;
1054
1055 case elfcpp::R_386_TLS_DESC_CALL:
1056 break;
1057
1058 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1059 if (optimized_type == tls::TLSOPT_NONE)
1060 {
1061 // Create a GOT entry for the module index.
1062 target->got_mod_index_entry(symtab, layout, object);
1063 }
1064 else if (optimized_type != tls::TLSOPT_TO_LE)
1065 unsupported_reloc_local(object, r_type);
1066 break;
1067
1068 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1069 break;
1070
1071 case elfcpp::R_386_TLS_IE: // Initial-exec
1072 case elfcpp::R_386_TLS_IE_32:
1073 case elfcpp::R_386_TLS_GOTIE:
1074 layout->set_has_static_tls();
1075 if (optimized_type == tls::TLSOPT_NONE)
1076 {
1077 // For the R_386_TLS_IE relocation, we need to create a
1078 // dynamic relocation when building a shared library.
1079 if (r_type == elfcpp::R_386_TLS_IE
1080 && parameters->options().shared())
1081 {
1082 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1083 unsigned int r_sym
1084 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1085 rel_dyn->add_local_relative(object, r_sym,
1086 elfcpp::R_386_RELATIVE,
1087 output_section, data_shndx,
1088 reloc.get_r_offset());
1089 }
1090 // Create a GOT entry for the tp-relative offset.
1091 Output_data_got<32, false>* got
1092 = target->got_section(symtab, layout);
1093 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1094 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1095 ? elfcpp::R_386_TLS_TPOFF32
1096 : elfcpp::R_386_TLS_TPOFF);
1097 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1098 ? GOT_TYPE_TLS_OFFSET
1099 : GOT_TYPE_TLS_NOFFSET);
1100 got->add_local_with_rel(object, r_sym, got_type,
1101 target->rel_dyn_section(layout),
1102 dyn_r_type);
1103 }
1104 else if (optimized_type != tls::TLSOPT_TO_LE)
1105 unsupported_reloc_local(object, r_type);
1106 break;
1107
1108 case elfcpp::R_386_TLS_LE: // Local-exec
1109 case elfcpp::R_386_TLS_LE_32:
1110 layout->set_has_static_tls();
1111 if (output_is_shared)
1112 {
1113 // We need to create a dynamic relocation.
1114 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1115 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1116 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1117 ? elfcpp::R_386_TLS_TPOFF32
1118 : elfcpp::R_386_TLS_TPOFF);
1119 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1120 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1121 data_shndx, reloc.get_r_offset());
1122 }
1123 break;
1124
1125 default:
1126 gold_unreachable();
1127 }
1128 }
1129 break;
1130
1131 case elfcpp::R_386_32PLT:
1132 case elfcpp::R_386_TLS_GD_32:
1133 case elfcpp::R_386_TLS_GD_PUSH:
1134 case elfcpp::R_386_TLS_GD_CALL:
1135 case elfcpp::R_386_TLS_GD_POP:
1136 case elfcpp::R_386_TLS_LDM_32:
1137 case elfcpp::R_386_TLS_LDM_PUSH:
1138 case elfcpp::R_386_TLS_LDM_CALL:
1139 case elfcpp::R_386_TLS_LDM_POP:
1140 case elfcpp::R_386_USED_BY_INTEL_200:
1141 default:
1142 unsupported_reloc_local(object, r_type);
1143 break;
1144 }
1145 }
1146
1147 // Report an unsupported relocation against a global symbol.
1148
1149 void
1150 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1151 unsigned int r_type,
1152 Symbol* gsym)
1153 {
1154 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1155 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1156 }
1157
1158 // Scan a relocation for a global symbol.
1159
1160 inline void
1161 Target_i386::Scan::global(const General_options&,
1162 Symbol_table* symtab,
1163 Layout* layout,
1164 Target_i386* target,
1165 Sized_relobj<32, false>* object,
1166 unsigned int data_shndx,
1167 Output_section* output_section,
1168 const elfcpp::Rel<32, false>& reloc,
1169 unsigned int r_type,
1170 Symbol* gsym)
1171 {
1172 switch (r_type)
1173 {
1174 case elfcpp::R_386_NONE:
1175 case elfcpp::R_386_GNU_VTINHERIT:
1176 case elfcpp::R_386_GNU_VTENTRY:
1177 break;
1178
1179 case elfcpp::R_386_32:
1180 case elfcpp::R_386_16:
1181 case elfcpp::R_386_8:
1182 {
1183 // Make a PLT entry if necessary.
1184 if (gsym->needs_plt_entry())
1185 {
1186 target->make_plt_entry(symtab, layout, gsym);
1187 // Since this is not a PC-relative relocation, we may be
1188 // taking the address of a function. In that case we need to
1189 // set the entry in the dynamic symbol table to the address of
1190 // the PLT entry.
1191 if (gsym->is_from_dynobj() && !parameters->options().shared())
1192 gsym->set_needs_dynsym_value();
1193 }
1194 // Make a dynamic relocation if necessary.
1195 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1196 {
1197 if (target->may_need_copy_reloc(gsym))
1198 {
1199 target->copy_reloc(symtab, layout, object,
1200 data_shndx, output_section, gsym, reloc);
1201 }
1202 else if (r_type == elfcpp::R_386_32
1203 && gsym->can_use_relative_reloc(false))
1204 {
1205 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1206 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1207 output_section, object,
1208 data_shndx, reloc.get_r_offset());
1209 }
1210 else
1211 {
1212 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1213 rel_dyn->add_global(gsym, r_type, output_section, object,
1214 data_shndx, reloc.get_r_offset());
1215 }
1216 }
1217 }
1218 break;
1219
1220 case elfcpp::R_386_PC32:
1221 case elfcpp::R_386_PC16:
1222 case elfcpp::R_386_PC8:
1223 {
1224 // Make a PLT entry if necessary.
1225 if (gsym->needs_plt_entry())
1226 {
1227 // These relocations are used for function calls only in
1228 // non-PIC code. For a 32-bit relocation in a shared library,
1229 // we'll need a text relocation anyway, so we can skip the
1230 // PLT entry and let the dynamic linker bind the call directly
1231 // to the target. For smaller relocations, we should use a
1232 // PLT entry to ensure that the call can reach.
1233 if (!parameters->options().shared()
1234 || r_type != elfcpp::R_386_PC32)
1235 target->make_plt_entry(symtab, layout, gsym);
1236 }
1237 // Make a dynamic relocation if necessary.
1238 int flags = Symbol::NON_PIC_REF;
1239 if (gsym->type() == elfcpp::STT_FUNC)
1240 flags |= Symbol::FUNCTION_CALL;
1241 if (gsym->needs_dynamic_reloc(flags))
1242 {
1243 if (target->may_need_copy_reloc(gsym))
1244 {
1245 target->copy_reloc(symtab, layout, object,
1246 data_shndx, output_section, gsym, reloc);
1247 }
1248 else
1249 {
1250 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1251 rel_dyn->add_global(gsym, r_type, output_section, object,
1252 data_shndx, reloc.get_r_offset());
1253 }
1254 }
1255 }
1256 break;
1257
1258 case elfcpp::R_386_GOT32:
1259 {
1260 // The symbol requires a GOT entry.
1261 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1262 if (gsym->final_value_is_known())
1263 got->add_global(gsym, GOT_TYPE_STANDARD);
1264 else
1265 {
1266 // If this symbol is not fully resolved, we need to add a
1267 // GOT entry with a dynamic relocation.
1268 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1269 if (gsym->is_from_dynobj()
1270 || gsym->is_undefined()
1271 || gsym->is_preemptible())
1272 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1273 rel_dyn, elfcpp::R_386_GLOB_DAT);
1274 else
1275 {
1276 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1277 rel_dyn->add_global_relative(
1278 gsym, elfcpp::R_386_RELATIVE, got,
1279 gsym->got_offset(GOT_TYPE_STANDARD));
1280 }
1281 }
1282 }
1283 break;
1284
1285 case elfcpp::R_386_PLT32:
1286 // If the symbol is fully resolved, this is just a PC32 reloc.
1287 // Otherwise we need a PLT entry.
1288 if (gsym->final_value_is_known())
1289 break;
1290 // If building a shared library, we can also skip the PLT entry
1291 // if the symbol is defined in the output file and is protected
1292 // or hidden.
1293 if (gsym->is_defined()
1294 && !gsym->is_from_dynobj()
1295 && !gsym->is_preemptible())
1296 break;
1297 target->make_plt_entry(symtab, layout, gsym);
1298 break;
1299
1300 case elfcpp::R_386_GOTOFF:
1301 case elfcpp::R_386_GOTPC:
1302 // We need a GOT section.
1303 target->got_section(symtab, layout);
1304 break;
1305
1306 // These are relocations which should only be seen by the
1307 // dynamic linker, and should never be seen here.
1308 case elfcpp::R_386_COPY:
1309 case elfcpp::R_386_GLOB_DAT:
1310 case elfcpp::R_386_JUMP_SLOT:
1311 case elfcpp::R_386_RELATIVE:
1312 case elfcpp::R_386_TLS_TPOFF:
1313 case elfcpp::R_386_TLS_DTPMOD32:
1314 case elfcpp::R_386_TLS_DTPOFF32:
1315 case elfcpp::R_386_TLS_TPOFF32:
1316 case elfcpp::R_386_TLS_DESC:
1317 gold_error(_("%s: unexpected reloc %u in object file"),
1318 object->name().c_str(), r_type);
1319 break;
1320
1321 // These are initial tls relocs, which are expected when
1322 // linking.
1323 case elfcpp::R_386_TLS_GD: // Global-dynamic
1324 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1325 case elfcpp::R_386_TLS_DESC_CALL:
1326 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1327 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1328 case elfcpp::R_386_TLS_IE: // Initial-exec
1329 case elfcpp::R_386_TLS_IE_32:
1330 case elfcpp::R_386_TLS_GOTIE:
1331 case elfcpp::R_386_TLS_LE: // Local-exec
1332 case elfcpp::R_386_TLS_LE_32:
1333 {
1334 const bool is_final = gsym->final_value_is_known();
1335 const tls::Tls_optimization optimized_type
1336 = Target_i386::optimize_tls_reloc(is_final, r_type);
1337 switch (r_type)
1338 {
1339 case elfcpp::R_386_TLS_GD: // Global-dynamic
1340 if (optimized_type == tls::TLSOPT_NONE)
1341 {
1342 // Create a pair of GOT entries for the module index and
1343 // dtv-relative offset.
1344 Output_data_got<32, false>* got
1345 = target->got_section(symtab, layout);
1346 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1347 target->rel_dyn_section(layout),
1348 elfcpp::R_386_TLS_DTPMOD32,
1349 elfcpp::R_386_TLS_DTPOFF32);
1350 }
1351 else if (optimized_type == tls::TLSOPT_TO_IE)
1352 {
1353 // Create a GOT entry for the tp-relative offset.
1354 Output_data_got<32, false>* got
1355 = target->got_section(symtab, layout);
1356 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1357 target->rel_dyn_section(layout),
1358 elfcpp::R_386_TLS_TPOFF);
1359 }
1360 else if (optimized_type != tls::TLSOPT_TO_LE)
1361 unsupported_reloc_global(object, r_type, gsym);
1362 break;
1363
1364 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1365 target->define_tls_base_symbol(symtab, layout);
1366 if (optimized_type == tls::TLSOPT_NONE)
1367 {
1368 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1369 Output_data_got<32, false>* got
1370 = target->got_section(symtab, layout);
1371 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1372 target->rel_dyn_section(layout),
1373 elfcpp::R_386_TLS_DESC, 0);
1374 }
1375 else if (optimized_type == tls::TLSOPT_TO_IE)
1376 {
1377 // Create a GOT entry for the tp-relative offset.
1378 Output_data_got<32, false>* got
1379 = target->got_section(symtab, layout);
1380 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1381 target->rel_dyn_section(layout),
1382 elfcpp::R_386_TLS_TPOFF);
1383 }
1384 else if (optimized_type != tls::TLSOPT_TO_LE)
1385 unsupported_reloc_global(object, r_type, gsym);
1386 break;
1387
1388 case elfcpp::R_386_TLS_DESC_CALL:
1389 break;
1390
1391 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1392 if (optimized_type == tls::TLSOPT_NONE)
1393 {
1394 // Create a GOT entry for the module index.
1395 target->got_mod_index_entry(symtab, layout, object);
1396 }
1397 else if (optimized_type != tls::TLSOPT_TO_LE)
1398 unsupported_reloc_global(object, r_type, gsym);
1399 break;
1400
1401 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1402 break;
1403
1404 case elfcpp::R_386_TLS_IE: // Initial-exec
1405 case elfcpp::R_386_TLS_IE_32:
1406 case elfcpp::R_386_TLS_GOTIE:
1407 layout->set_has_static_tls();
1408 if (optimized_type == tls::TLSOPT_NONE)
1409 {
1410 // For the R_386_TLS_IE relocation, we need to create a
1411 // dynamic relocation when building a shared library.
1412 if (r_type == elfcpp::R_386_TLS_IE
1413 && parameters->options().shared())
1414 {
1415 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1416 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1417 output_section, object,
1418 data_shndx,
1419 reloc.get_r_offset());
1420 }
1421 // Create a GOT entry for the tp-relative offset.
1422 Output_data_got<32, false>* got
1423 = target->got_section(symtab, layout);
1424 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1425 ? elfcpp::R_386_TLS_TPOFF32
1426 : elfcpp::R_386_TLS_TPOFF);
1427 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1428 ? GOT_TYPE_TLS_OFFSET
1429 : GOT_TYPE_TLS_NOFFSET);
1430 got->add_global_with_rel(gsym, got_type,
1431 target->rel_dyn_section(layout),
1432 dyn_r_type);
1433 }
1434 else if (optimized_type != tls::TLSOPT_TO_LE)
1435 unsupported_reloc_global(object, r_type, gsym);
1436 break;
1437
1438 case elfcpp::R_386_TLS_LE: // Local-exec
1439 case elfcpp::R_386_TLS_LE_32:
1440 layout->set_has_static_tls();
1441 if (parameters->options().shared())
1442 {
1443 // We need to create a dynamic relocation.
1444 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1445 ? elfcpp::R_386_TLS_TPOFF32
1446 : elfcpp::R_386_TLS_TPOFF);
1447 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1448 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1449 data_shndx, reloc.get_r_offset());
1450 }
1451 break;
1452
1453 default:
1454 gold_unreachable();
1455 }
1456 }
1457 break;
1458
1459 case elfcpp::R_386_32PLT:
1460 case elfcpp::R_386_TLS_GD_32:
1461 case elfcpp::R_386_TLS_GD_PUSH:
1462 case elfcpp::R_386_TLS_GD_CALL:
1463 case elfcpp::R_386_TLS_GD_POP:
1464 case elfcpp::R_386_TLS_LDM_32:
1465 case elfcpp::R_386_TLS_LDM_PUSH:
1466 case elfcpp::R_386_TLS_LDM_CALL:
1467 case elfcpp::R_386_TLS_LDM_POP:
1468 case elfcpp::R_386_USED_BY_INTEL_200:
1469 default:
1470 unsupported_reloc_global(object, r_type, gsym);
1471 break;
1472 }
1473 }
1474
1475 // Process relocations for gc.
1476
1477 void
1478 Target_i386::gc_process_relocs(const General_options& options,
1479 Symbol_table* symtab,
1480 Layout* layout,
1481 Sized_relobj<32, false>* object,
1482 unsigned int data_shndx,
1483 unsigned int,
1484 const unsigned char* prelocs,
1485 size_t reloc_count,
1486 Output_section* output_section,
1487 bool needs_special_offset_handling,
1488 size_t local_symbol_count,
1489 const unsigned char* plocal_symbols)
1490 {
1491 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1492 Target_i386::Scan>(
1493 options,
1494 symtab,
1495 layout,
1496 this,
1497 object,
1498 data_shndx,
1499 prelocs,
1500 reloc_count,
1501 output_section,
1502 needs_special_offset_handling,
1503 local_symbol_count,
1504 plocal_symbols);
1505 }
1506
1507 // Scan relocations for a section.
1508
1509 void
1510 Target_i386::scan_relocs(const General_options& options,
1511 Symbol_table* symtab,
1512 Layout* layout,
1513 Sized_relobj<32, false>* object,
1514 unsigned int data_shndx,
1515 unsigned int sh_type,
1516 const unsigned char* prelocs,
1517 size_t reloc_count,
1518 Output_section* output_section,
1519 bool needs_special_offset_handling,
1520 size_t local_symbol_count,
1521 const unsigned char* plocal_symbols)
1522 {
1523 if (sh_type == elfcpp::SHT_RELA)
1524 {
1525 gold_error(_("%s: unsupported RELA reloc section"),
1526 object->name().c_str());
1527 return;
1528 }
1529
1530 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1531 Target_i386::Scan>(
1532 options,
1533 symtab,
1534 layout,
1535 this,
1536 object,
1537 data_shndx,
1538 prelocs,
1539 reloc_count,
1540 output_section,
1541 needs_special_offset_handling,
1542 local_symbol_count,
1543 plocal_symbols);
1544 }
1545
1546 // Finalize the sections.
1547
1548 void
1549 Target_i386::do_finalize_sections(Layout* layout)
1550 {
1551 // Fill in some more dynamic tags.
1552 Output_data_dynamic* const odyn = layout->dynamic_data();
1553 if (odyn != NULL)
1554 {
1555 if (this->got_plt_ != NULL)
1556 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1557
1558 if (this->plt_ != NULL)
1559 {
1560 const Output_data* od = this->plt_->rel_plt();
1561 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1562 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1563 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1564 }
1565
1566 if (this->rel_dyn_ != NULL)
1567 {
1568 const Output_data* od = this->rel_dyn_;
1569 odyn->add_section_address(elfcpp::DT_REL, od);
1570 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1571 odyn->add_constant(elfcpp::DT_RELENT,
1572 elfcpp::Elf_sizes<32>::rel_size);
1573 }
1574
1575 if (!parameters->options().shared())
1576 {
1577 // The value of the DT_DEBUG tag is filled in by the dynamic
1578 // linker at run time, and used by the debugger.
1579 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1580 }
1581 }
1582
1583 // Emit any relocs we saved in an attempt to avoid generating COPY
1584 // relocs.
1585 if (this->copy_relocs_.any_saved_relocs())
1586 this->copy_relocs_.emit(this->rel_dyn_section(layout));
1587 }
1588
1589 // Return whether a direct absolute static relocation needs to be applied.
1590 // In cases where Scan::local() or Scan::global() has created
1591 // a dynamic relocation other than R_386_RELATIVE, the addend
1592 // of the relocation is carried in the data, and we must not
1593 // apply the static relocation.
1594
1595 inline bool
1596 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
1597 int ref_flags,
1598 bool is_32bit)
1599 {
1600 // For local symbols, we will have created a non-RELATIVE dynamic
1601 // relocation only if (a) the output is position independent,
1602 // (b) the relocation is absolute (not pc- or segment-relative), and
1603 // (c) the relocation is not 32 bits wide.
1604 if (gsym == NULL)
1605 return !(parameters->options().output_is_position_independent()
1606 && (ref_flags & Symbol::ABSOLUTE_REF)
1607 && !is_32bit);
1608
1609 // For global symbols, we use the same helper routines used in the
1610 // scan pass. If we did not create a dynamic relocation, or if we
1611 // created a RELATIVE dynamic relocation, we should apply the static
1612 // relocation.
1613 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1614 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1615 && gsym->can_use_relative_reloc(ref_flags
1616 & Symbol::FUNCTION_CALL);
1617 return !has_dyn || is_rel;
1618 }
1619
1620 // Perform a relocation.
1621
1622 inline bool
1623 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1624 Target_i386* target,
1625 size_t relnum,
1626 const elfcpp::Rel<32, false>& rel,
1627 unsigned int r_type,
1628 const Sized_symbol<32>* gsym,
1629 const Symbol_value<32>* psymval,
1630 unsigned char* view,
1631 elfcpp::Elf_types<32>::Elf_Addr address,
1632 section_size_type view_size)
1633 {
1634 if (this->skip_call_tls_get_addr_)
1635 {
1636 if ((r_type != elfcpp::R_386_PLT32
1637 && r_type != elfcpp::R_386_PC32)
1638 || gsym == NULL
1639 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1640 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1641 _("missing expected TLS relocation"));
1642 else
1643 {
1644 this->skip_call_tls_get_addr_ = false;
1645 return false;
1646 }
1647 }
1648
1649 // Pick the value to use for symbols defined in shared objects.
1650 Symbol_value<32> symval;
1651 if (gsym != NULL
1652 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
1653 || r_type == elfcpp::R_386_PC16
1654 || r_type == elfcpp::R_386_PC32))
1655 {
1656 symval.set_output_value(target->plt_section()->address()
1657 + gsym->plt_offset());
1658 psymval = &symval;
1659 }
1660
1661 const Sized_relobj<32, false>* object = relinfo->object;
1662
1663 // Get the GOT offset if needed.
1664 // The GOT pointer points to the end of the GOT section.
1665 // We need to subtract the size of the GOT section to get
1666 // the actual offset to use in the relocation.
1667 bool have_got_offset = false;
1668 unsigned int got_offset = 0;
1669 switch (r_type)
1670 {
1671 case elfcpp::R_386_GOT32:
1672 if (gsym != NULL)
1673 {
1674 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1675 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1676 - target->got_size());
1677 }
1678 else
1679 {
1680 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1681 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1682 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1683 - target->got_size());
1684 }
1685 have_got_offset = true;
1686 break;
1687
1688 default:
1689 break;
1690 }
1691
1692 switch (r_type)
1693 {
1694 case elfcpp::R_386_NONE:
1695 case elfcpp::R_386_GNU_VTINHERIT:
1696 case elfcpp::R_386_GNU_VTENTRY:
1697 break;
1698
1699 case elfcpp::R_386_32:
1700 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
1701 Relocate_functions<32, false>::rel32(view, object, psymval);
1702 break;
1703
1704 case elfcpp::R_386_PC32:
1705 {
1706 int ref_flags = Symbol::NON_PIC_REF;
1707 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1708 ref_flags |= Symbol::FUNCTION_CALL;
1709 if (should_apply_static_reloc(gsym, ref_flags, true))
1710 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1711 }
1712 break;
1713
1714 case elfcpp::R_386_16:
1715 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1716 Relocate_functions<32, false>::rel16(view, object, psymval);
1717 break;
1718
1719 case elfcpp::R_386_PC16:
1720 {
1721 int ref_flags = Symbol::NON_PIC_REF;
1722 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1723 ref_flags |= Symbol::FUNCTION_CALL;
1724 if (should_apply_static_reloc(gsym, ref_flags, false))
1725 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1726 }
1727 break;
1728
1729 case elfcpp::R_386_8:
1730 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
1731 Relocate_functions<32, false>::rel8(view, object, psymval);
1732 break;
1733
1734 case elfcpp::R_386_PC8:
1735 {
1736 int ref_flags = Symbol::NON_PIC_REF;
1737 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1738 ref_flags |= Symbol::FUNCTION_CALL;
1739 if (should_apply_static_reloc(gsym, ref_flags, false))
1740 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1741 }
1742 break;
1743
1744 case elfcpp::R_386_PLT32:
1745 gold_assert(gsym == NULL
1746 || gsym->has_plt_offset()
1747 || gsym->final_value_is_known()
1748 || (gsym->is_defined()
1749 && !gsym->is_from_dynobj()
1750 && !gsym->is_preemptible()));
1751 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1752 break;
1753
1754 case elfcpp::R_386_GOT32:
1755 gold_assert(have_got_offset);
1756 Relocate_functions<32, false>::rel32(view, got_offset);
1757 break;
1758
1759 case elfcpp::R_386_GOTOFF:
1760 {
1761 elfcpp::Elf_types<32>::Elf_Addr value;
1762 value = (psymval->value(object, 0)
1763 - target->got_plt_section()->address());
1764 Relocate_functions<32, false>::rel32(view, value);
1765 }
1766 break;
1767
1768 case elfcpp::R_386_GOTPC:
1769 {
1770 elfcpp::Elf_types<32>::Elf_Addr value;
1771 value = target->got_plt_section()->address();
1772 Relocate_functions<32, false>::pcrel32(view, value, address);
1773 }
1774 break;
1775
1776 case elfcpp::R_386_COPY:
1777 case elfcpp::R_386_GLOB_DAT:
1778 case elfcpp::R_386_JUMP_SLOT:
1779 case elfcpp::R_386_RELATIVE:
1780 // These are outstanding tls relocs, which are unexpected when
1781 // linking.
1782 case elfcpp::R_386_TLS_TPOFF:
1783 case elfcpp::R_386_TLS_DTPMOD32:
1784 case elfcpp::R_386_TLS_DTPOFF32:
1785 case elfcpp::R_386_TLS_TPOFF32:
1786 case elfcpp::R_386_TLS_DESC:
1787 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1788 _("unexpected reloc %u in object file"),
1789 r_type);
1790 break;
1791
1792 // These are initial tls relocs, which are expected when
1793 // linking.
1794 case elfcpp::R_386_TLS_GD: // Global-dynamic
1795 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1796 case elfcpp::R_386_TLS_DESC_CALL:
1797 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1798 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1799 case elfcpp::R_386_TLS_IE: // Initial-exec
1800 case elfcpp::R_386_TLS_IE_32:
1801 case elfcpp::R_386_TLS_GOTIE:
1802 case elfcpp::R_386_TLS_LE: // Local-exec
1803 case elfcpp::R_386_TLS_LE_32:
1804 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1805 view, address, view_size);
1806 break;
1807
1808 case elfcpp::R_386_32PLT:
1809 case elfcpp::R_386_TLS_GD_32:
1810 case elfcpp::R_386_TLS_GD_PUSH:
1811 case elfcpp::R_386_TLS_GD_CALL:
1812 case elfcpp::R_386_TLS_GD_POP:
1813 case elfcpp::R_386_TLS_LDM_32:
1814 case elfcpp::R_386_TLS_LDM_PUSH:
1815 case elfcpp::R_386_TLS_LDM_CALL:
1816 case elfcpp::R_386_TLS_LDM_POP:
1817 case elfcpp::R_386_USED_BY_INTEL_200:
1818 default:
1819 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1820 _("unsupported reloc %u"),
1821 r_type);
1822 break;
1823 }
1824
1825 return true;
1826 }
1827
1828 // Perform a TLS relocation.
1829
1830 inline void
1831 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1832 Target_i386* target,
1833 size_t relnum,
1834 const elfcpp::Rel<32, false>& rel,
1835 unsigned int r_type,
1836 const Sized_symbol<32>* gsym,
1837 const Symbol_value<32>* psymval,
1838 unsigned char* view,
1839 elfcpp::Elf_types<32>::Elf_Addr,
1840 section_size_type view_size)
1841 {
1842 Output_segment* tls_segment = relinfo->layout->tls_segment();
1843
1844 const Sized_relobj<32, false>* object = relinfo->object;
1845
1846 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
1847
1848 const bool is_final =
1849 (gsym == NULL
1850 ? !parameters->options().output_is_position_independent()
1851 : gsym->final_value_is_known());
1852 const tls::Tls_optimization optimized_type
1853 = Target_i386::optimize_tls_reloc(is_final, r_type);
1854 switch (r_type)
1855 {
1856 case elfcpp::R_386_TLS_GD: // Global-dynamic
1857 if (optimized_type == tls::TLSOPT_TO_LE)
1858 {
1859 gold_assert(tls_segment != NULL);
1860 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1861 rel, r_type, value, view,
1862 view_size);
1863 break;
1864 }
1865 else
1866 {
1867 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1868 ? GOT_TYPE_TLS_NOFFSET
1869 : GOT_TYPE_TLS_PAIR);
1870 unsigned int got_offset;
1871 if (gsym != NULL)
1872 {
1873 gold_assert(gsym->has_got_offset(got_type));
1874 got_offset = gsym->got_offset(got_type) - target->got_size();
1875 }
1876 else
1877 {
1878 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1879 gold_assert(object->local_has_got_offset(r_sym, got_type));
1880 got_offset = (object->local_got_offset(r_sym, got_type)
1881 - target->got_size());
1882 }
1883 if (optimized_type == tls::TLSOPT_TO_IE)
1884 {
1885 gold_assert(tls_segment != NULL);
1886 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1887 got_offset, view, view_size);
1888 break;
1889 }
1890 else if (optimized_type == tls::TLSOPT_NONE)
1891 {
1892 // Relocate the field with the offset of the pair of GOT
1893 // entries.
1894 Relocate_functions<32, false>::rel32(view, got_offset);
1895 break;
1896 }
1897 }
1898 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1899 _("unsupported reloc %u"),
1900 r_type);
1901 break;
1902
1903 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1904 case elfcpp::R_386_TLS_DESC_CALL:
1905 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1906 if (optimized_type == tls::TLSOPT_TO_LE)
1907 {
1908 gold_assert(tls_segment != NULL);
1909 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1910 rel, r_type, value, view,
1911 view_size);
1912 break;
1913 }
1914 else
1915 {
1916 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1917 ? GOT_TYPE_TLS_NOFFSET
1918 : GOT_TYPE_TLS_DESC);
1919 unsigned int got_offset;
1920 if (gsym != NULL)
1921 {
1922 gold_assert(gsym->has_got_offset(got_type));
1923 got_offset = gsym->got_offset(got_type) - target->got_size();
1924 }
1925 else
1926 {
1927 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1928 gold_assert(object->local_has_got_offset(r_sym, got_type));
1929 got_offset = (object->local_got_offset(r_sym, got_type)
1930 - target->got_size());
1931 }
1932 if (optimized_type == tls::TLSOPT_TO_IE)
1933 {
1934 gold_assert(tls_segment != NULL);
1935 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1936 got_offset, view, view_size);
1937 break;
1938 }
1939 else if (optimized_type == tls::TLSOPT_NONE)
1940 {
1941 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1942 {
1943 // Relocate the field with the offset of the pair of GOT
1944 // entries.
1945 Relocate_functions<32, false>::rel32(view, got_offset);
1946 }
1947 break;
1948 }
1949 }
1950 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1951 _("unsupported reloc %u"),
1952 r_type);
1953 break;
1954
1955 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1956 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1957 {
1958 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1959 _("both SUN and GNU model "
1960 "TLS relocations"));
1961 break;
1962 }
1963 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1964 if (optimized_type == tls::TLSOPT_TO_LE)
1965 {
1966 gold_assert(tls_segment != NULL);
1967 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1968 value, view, view_size);
1969 break;
1970 }
1971 else if (optimized_type == tls::TLSOPT_NONE)
1972 {
1973 // Relocate the field with the offset of the GOT entry for
1974 // the module index.
1975 unsigned int got_offset;
1976 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1977 - target->got_size());
1978 Relocate_functions<32, false>::rel32(view, got_offset);
1979 break;
1980 }
1981 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1982 _("unsupported reloc %u"),
1983 r_type);
1984 break;
1985
1986 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1987 // This reloc can appear in debugging sections, in which case we
1988 // won't see the TLS_LDM reloc. The local_dynamic_type field
1989 // tells us this.
1990 if (optimized_type == tls::TLSOPT_TO_LE
1991 && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
1992 {
1993 gold_assert(tls_segment != NULL);
1994 value -= tls_segment->memsz();
1995 }
1996 Relocate_functions<32, false>::rel32(view, value);
1997 break;
1998
1999 case elfcpp::R_386_TLS_IE: // Initial-exec
2000 case elfcpp::R_386_TLS_GOTIE:
2001 case elfcpp::R_386_TLS_IE_32:
2002 if (optimized_type == tls::TLSOPT_TO_LE)
2003 {
2004 gold_assert(tls_segment != NULL);
2005 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2006 rel, r_type, value, view,
2007 view_size);
2008 break;
2009 }
2010 else if (optimized_type == tls::TLSOPT_NONE)
2011 {
2012 // Relocate the field with the offset of the GOT entry for
2013 // the tp-relative offset of the symbol.
2014 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2015 ? GOT_TYPE_TLS_OFFSET
2016 : GOT_TYPE_TLS_NOFFSET);
2017 unsigned int got_offset;
2018 if (gsym != NULL)
2019 {
2020 gold_assert(gsym->has_got_offset(got_type));
2021 got_offset = gsym->got_offset(got_type);
2022 }
2023 else
2024 {
2025 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2026 gold_assert(object->local_has_got_offset(r_sym, got_type));
2027 got_offset = object->local_got_offset(r_sym, got_type);
2028 }
2029 // For the R_386_TLS_IE relocation, we need to apply the
2030 // absolute address of the GOT entry.
2031 if (r_type == elfcpp::R_386_TLS_IE)
2032 got_offset += target->got_plt_section()->address();
2033 // All GOT offsets are relative to the end of the GOT.
2034 got_offset -= target->got_size();
2035 Relocate_functions<32, false>::rel32(view, got_offset);
2036 break;
2037 }
2038 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2039 _("unsupported reloc %u"),
2040 r_type);
2041 break;
2042
2043 case elfcpp::R_386_TLS_LE: // Local-exec
2044 // If we're creating a shared library, a dynamic relocation will
2045 // have been created for this location, so do not apply it now.
2046 if (!parameters->options().shared())
2047 {
2048 gold_assert(tls_segment != NULL);
2049 value -= tls_segment->memsz();
2050 Relocate_functions<32, false>::rel32(view, value);
2051 }
2052 break;
2053
2054 case elfcpp::R_386_TLS_LE_32:
2055 // If we're creating a shared library, a dynamic relocation will
2056 // have been created for this location, so do not apply it now.
2057 if (!parameters->options().shared())
2058 {
2059 gold_assert(tls_segment != NULL);
2060 value = tls_segment->memsz() - value;
2061 Relocate_functions<32, false>::rel32(view, value);
2062 }
2063 break;
2064 }
2065 }
2066
2067 // Do a relocation in which we convert a TLS General-Dynamic to a
2068 // Local-Exec.
2069
2070 inline void
2071 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2072 size_t relnum,
2073 Output_segment* tls_segment,
2074 const elfcpp::Rel<32, false>& rel,
2075 unsigned int,
2076 elfcpp::Elf_types<32>::Elf_Addr value,
2077 unsigned char* view,
2078 section_size_type view_size)
2079 {
2080 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2081 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2082 // leal foo(%reg),%eax; call ___tls_get_addr
2083 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2084
2085 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2086 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2087
2088 unsigned char op1 = view[-1];
2089 unsigned char op2 = view[-2];
2090
2091 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2092 op2 == 0x8d || op2 == 0x04);
2093 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2094
2095 int roff = 5;
2096
2097 if (op2 == 0x04)
2098 {
2099 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2100 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2101 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2102 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2103 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2104 }
2105 else
2106 {
2107 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2108 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2109 if (rel.get_r_offset() + 9 < view_size
2110 && view[9] == 0x90)
2111 {
2112 // There is a trailing nop. Use the size byte subl.
2113 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2114 roff = 6;
2115 }
2116 else
2117 {
2118 // Use the five byte subl.
2119 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2120 }
2121 }
2122
2123 value = tls_segment->memsz() - value;
2124 Relocate_functions<32, false>::rel32(view + roff, value);
2125
2126 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2127 // We can skip it.
2128 this->skip_call_tls_get_addr_ = true;
2129 }
2130
2131 // Do a relocation in which we convert a TLS General-Dynamic to an
2132 // Initial-Exec.
2133
2134 inline void
2135 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2136 size_t relnum,
2137 Output_segment*,
2138 const elfcpp::Rel<32, false>& rel,
2139 unsigned int,
2140 elfcpp::Elf_types<32>::Elf_Addr value,
2141 unsigned char* view,
2142 section_size_type view_size)
2143 {
2144 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2145 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2146
2147 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2148 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2149
2150 unsigned char op1 = view[-1];
2151 unsigned char op2 = view[-2];
2152
2153 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2154 op2 == 0x8d || op2 == 0x04);
2155 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2156
2157 int roff = 5;
2158
2159 // FIXME: For now, support only the first (SIB) form.
2160 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2161
2162 if (op2 == 0x04)
2163 {
2164 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2165 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2166 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2167 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2168 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2169 }
2170 else
2171 {
2172 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2173 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2174 if (rel.get_r_offset() + 9 < view_size
2175 && view[9] == 0x90)
2176 {
2177 // FIXME: This is not the right instruction sequence.
2178 // There is a trailing nop. Use the size byte subl.
2179 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2180 roff = 6;
2181 }
2182 else
2183 {
2184 // FIXME: This is not the right instruction sequence.
2185 // Use the five byte subl.
2186 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2187 }
2188 }
2189
2190 Relocate_functions<32, false>::rel32(view + roff, value);
2191
2192 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2193 // We can skip it.
2194 this->skip_call_tls_get_addr_ = true;
2195 }
2196
2197 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2198 // General-Dynamic to a Local-Exec.
2199
2200 inline void
2201 Target_i386::Relocate::tls_desc_gd_to_le(
2202 const Relocate_info<32, false>* relinfo,
2203 size_t relnum,
2204 Output_segment* tls_segment,
2205 const elfcpp::Rel<32, false>& rel,
2206 unsigned int r_type,
2207 elfcpp::Elf_types<32>::Elf_Addr value,
2208 unsigned char* view,
2209 section_size_type view_size)
2210 {
2211 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2212 {
2213 // leal foo@TLSDESC(%ebx), %eax
2214 // ==> leal foo@NTPOFF, %eax
2215 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2216 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2217 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2218 view[-2] == 0x8d && view[-1] == 0x83);
2219 view[-1] = 0x05;
2220 value -= tls_segment->memsz();
2221 Relocate_functions<32, false>::rel32(view, value);
2222 }
2223 else
2224 {
2225 // call *foo@TLSCALL(%eax)
2226 // ==> nop; nop
2227 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2228 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2229 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2230 view[0] == 0xff && view[1] == 0x10);
2231 view[0] = 0x66;
2232 view[1] = 0x90;
2233 }
2234 }
2235
2236 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2237 // General-Dynamic to an Initial-Exec.
2238
2239 inline void
2240 Target_i386::Relocate::tls_desc_gd_to_ie(
2241 const Relocate_info<32, false>* relinfo,
2242 size_t relnum,
2243 Output_segment*,
2244 const elfcpp::Rel<32, false>& rel,
2245 unsigned int r_type,
2246 elfcpp::Elf_types<32>::Elf_Addr value,
2247 unsigned char* view,
2248 section_size_type view_size)
2249 {
2250 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2251 {
2252 // leal foo@TLSDESC(%ebx), %eax
2253 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2254 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2255 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2256 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2257 view[-2] == 0x8d && view[-1] == 0x83);
2258 view[-2] = 0x8b;
2259 Relocate_functions<32, false>::rel32(view, value);
2260 }
2261 else
2262 {
2263 // call *foo@TLSCALL(%eax)
2264 // ==> nop; nop
2265 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2266 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2267 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2268 view[0] == 0xff && view[1] == 0x10);
2269 view[0] = 0x66;
2270 view[1] = 0x90;
2271 }
2272 }
2273
2274 // Do a relocation in which we convert a TLS Local-Dynamic to a
2275 // Local-Exec.
2276
2277 inline void
2278 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2279 size_t relnum,
2280 Output_segment*,
2281 const elfcpp::Rel<32, false>& rel,
2282 unsigned int,
2283 elfcpp::Elf_types<32>::Elf_Addr,
2284 unsigned char* view,
2285 section_size_type view_size)
2286 {
2287 // leal foo(%reg), %eax; call ___tls_get_addr
2288 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2289
2290 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2291 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2292
2293 // FIXME: Does this test really always pass?
2294 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2295 view[-2] == 0x8d && view[-1] == 0x83);
2296
2297 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2298
2299 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2300
2301 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2302 // We can skip it.
2303 this->skip_call_tls_get_addr_ = true;
2304 }
2305
2306 // Do a relocation in which we convert a TLS Initial-Exec to a
2307 // Local-Exec.
2308
2309 inline void
2310 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2311 size_t relnum,
2312 Output_segment* tls_segment,
2313 const elfcpp::Rel<32, false>& rel,
2314 unsigned int r_type,
2315 elfcpp::Elf_types<32>::Elf_Addr value,
2316 unsigned char* view,
2317 section_size_type view_size)
2318 {
2319 // We have to actually change the instructions, which means that we
2320 // need to examine the opcodes to figure out which instruction we
2321 // are looking at.
2322 if (r_type == elfcpp::R_386_TLS_IE)
2323 {
2324 // movl %gs:XX,%eax ==> movl $YY,%eax
2325 // movl %gs:XX,%reg ==> movl $YY,%reg
2326 // addl %gs:XX,%reg ==> addl $YY,%reg
2327 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2328 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2329
2330 unsigned char op1 = view[-1];
2331 if (op1 == 0xa1)
2332 {
2333 // movl XX,%eax ==> movl $YY,%eax
2334 view[-1] = 0xb8;
2335 }
2336 else
2337 {
2338 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2339
2340 unsigned char op2 = view[-2];
2341 if (op2 == 0x8b)
2342 {
2343 // movl XX,%reg ==> movl $YY,%reg
2344 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2345 (op1 & 0xc7) == 0x05);
2346 view[-2] = 0xc7;
2347 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2348 }
2349 else if (op2 == 0x03)
2350 {
2351 // addl XX,%reg ==> addl $YY,%reg
2352 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2353 (op1 & 0xc7) == 0x05);
2354 view[-2] = 0x81;
2355 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2356 }
2357 else
2358 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2359 }
2360 }
2361 else
2362 {
2363 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2364 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2365 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2366 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2367 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2368
2369 unsigned char op1 = view[-1];
2370 unsigned char op2 = view[-2];
2371 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2372 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2373 if (op2 == 0x8b)
2374 {
2375 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2376 view[-2] = 0xc7;
2377 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2378 }
2379 else if (op2 == 0x2b)
2380 {
2381 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2382 view[-2] = 0x81;
2383 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2384 }
2385 else if (op2 == 0x03)
2386 {
2387 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2388 view[-2] = 0x81;
2389 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2390 }
2391 else
2392 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2393 }
2394
2395 value = tls_segment->memsz() - value;
2396 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2397 value = - value;
2398
2399 Relocate_functions<32, false>::rel32(view, value);
2400 }
2401
2402 // Relocate section data.
2403
2404 void
2405 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2406 unsigned int sh_type,
2407 const unsigned char* prelocs,
2408 size_t reloc_count,
2409 Output_section* output_section,
2410 bool needs_special_offset_handling,
2411 unsigned char* view,
2412 elfcpp::Elf_types<32>::Elf_Addr address,
2413 section_size_type view_size)
2414 {
2415 gold_assert(sh_type == elfcpp::SHT_REL);
2416
2417 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2418 Target_i386::Relocate>(
2419 relinfo,
2420 this,
2421 prelocs,
2422 reloc_count,
2423 output_section,
2424 needs_special_offset_handling,
2425 view,
2426 address,
2427 view_size);
2428 }
2429
2430 // Return the size of a relocation while scanning during a relocatable
2431 // link.
2432
2433 unsigned int
2434 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2435 unsigned int r_type,
2436 Relobj* object)
2437 {
2438 switch (r_type)
2439 {
2440 case elfcpp::R_386_NONE:
2441 case elfcpp::R_386_GNU_VTINHERIT:
2442 case elfcpp::R_386_GNU_VTENTRY:
2443 case elfcpp::R_386_TLS_GD: // Global-dynamic
2444 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2445 case elfcpp::R_386_TLS_DESC_CALL:
2446 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2447 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2448 case elfcpp::R_386_TLS_IE: // Initial-exec
2449 case elfcpp::R_386_TLS_IE_32:
2450 case elfcpp::R_386_TLS_GOTIE:
2451 case elfcpp::R_386_TLS_LE: // Local-exec
2452 case elfcpp::R_386_TLS_LE_32:
2453 return 0;
2454
2455 case elfcpp::R_386_32:
2456 case elfcpp::R_386_PC32:
2457 case elfcpp::R_386_GOT32:
2458 case elfcpp::R_386_PLT32:
2459 case elfcpp::R_386_GOTOFF:
2460 case elfcpp::R_386_GOTPC:
2461 return 4;
2462
2463 case elfcpp::R_386_16:
2464 case elfcpp::R_386_PC16:
2465 return 2;
2466
2467 case elfcpp::R_386_8:
2468 case elfcpp::R_386_PC8:
2469 return 1;
2470
2471 // These are relocations which should only be seen by the
2472 // dynamic linker, and should never be seen here.
2473 case elfcpp::R_386_COPY:
2474 case elfcpp::R_386_GLOB_DAT:
2475 case elfcpp::R_386_JUMP_SLOT:
2476 case elfcpp::R_386_RELATIVE:
2477 case elfcpp::R_386_TLS_TPOFF:
2478 case elfcpp::R_386_TLS_DTPMOD32:
2479 case elfcpp::R_386_TLS_DTPOFF32:
2480 case elfcpp::R_386_TLS_TPOFF32:
2481 case elfcpp::R_386_TLS_DESC:
2482 object->error(_("unexpected reloc %u in object file"), r_type);
2483 return 0;
2484
2485 case elfcpp::R_386_32PLT:
2486 case elfcpp::R_386_TLS_GD_32:
2487 case elfcpp::R_386_TLS_GD_PUSH:
2488 case elfcpp::R_386_TLS_GD_CALL:
2489 case elfcpp::R_386_TLS_GD_POP:
2490 case elfcpp::R_386_TLS_LDM_32:
2491 case elfcpp::R_386_TLS_LDM_PUSH:
2492 case elfcpp::R_386_TLS_LDM_CALL:
2493 case elfcpp::R_386_TLS_LDM_POP:
2494 case elfcpp::R_386_USED_BY_INTEL_200:
2495 default:
2496 object->error(_("unsupported reloc %u in object file"), r_type);
2497 return 0;
2498 }
2499 }
2500
2501 // Scan the relocs during a relocatable link.
2502
2503 void
2504 Target_i386::scan_relocatable_relocs(const General_options& options,
2505 Symbol_table* symtab,
2506 Layout* layout,
2507 Sized_relobj<32, false>* object,
2508 unsigned int data_shndx,
2509 unsigned int sh_type,
2510 const unsigned char* prelocs,
2511 size_t reloc_count,
2512 Output_section* output_section,
2513 bool needs_special_offset_handling,
2514 size_t local_symbol_count,
2515 const unsigned char* plocal_symbols,
2516 Relocatable_relocs* rr)
2517 {
2518 gold_assert(sh_type == elfcpp::SHT_REL);
2519
2520 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2521 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2522
2523 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
2524 Scan_relocatable_relocs>(
2525 options,
2526 symtab,
2527 layout,
2528 object,
2529 data_shndx,
2530 prelocs,
2531 reloc_count,
2532 output_section,
2533 needs_special_offset_handling,
2534 local_symbol_count,
2535 plocal_symbols,
2536 rr);
2537 }
2538
2539 // Relocate a section during a relocatable link.
2540
2541 void
2542 Target_i386::relocate_for_relocatable(
2543 const Relocate_info<32, false>* relinfo,
2544 unsigned int sh_type,
2545 const unsigned char* prelocs,
2546 size_t reloc_count,
2547 Output_section* output_section,
2548 off_t offset_in_output_section,
2549 const Relocatable_relocs* rr,
2550 unsigned char* view,
2551 elfcpp::Elf_types<32>::Elf_Addr view_address,
2552 section_size_type view_size,
2553 unsigned char* reloc_view,
2554 section_size_type reloc_view_size)
2555 {
2556 gold_assert(sh_type == elfcpp::SHT_REL);
2557
2558 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
2559 relinfo,
2560 prelocs,
2561 reloc_count,
2562 output_section,
2563 offset_in_output_section,
2564 rr,
2565 view,
2566 view_address,
2567 view_size,
2568 reloc_view,
2569 reloc_view_size);
2570 }
2571
2572 // Return the value to use for a dynamic which requires special
2573 // treatment. This is how we support equality comparisons of function
2574 // pointers across shared library boundaries, as described in the
2575 // processor specific ABI supplement.
2576
2577 uint64_t
2578 Target_i386::do_dynsym_value(const Symbol* gsym) const
2579 {
2580 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2581 return this->plt_section()->address() + gsym->plt_offset();
2582 }
2583
2584 // Return a string used to fill a code section with nops to take up
2585 // the specified length.
2586
2587 std::string
2588 Target_i386::do_code_fill(section_size_type length) const
2589 {
2590 if (length >= 16)
2591 {
2592 // Build a jmp instruction to skip over the bytes.
2593 unsigned char jmp[5];
2594 jmp[0] = 0xe9;
2595 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2596 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2597 + std::string(length - 5, '\0'));
2598 }
2599
2600 // Nop sequences of various lengths.
2601 const char nop1[1] = { 0x90 }; // nop
2602 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2603 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2604 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2605 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2606 0x00 }; // leal 0(%esi,1),%esi
2607 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2608 0x00, 0x00 };
2609 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2610 0x00, 0x00, 0x00 };
2611 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2612 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2613 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2614 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2615 0x00 };
2616 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2617 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2618 0x00, 0x00 };
2619 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2620 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2621 0x00, 0x00, 0x00 };
2622 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2623 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2624 0x00, 0x00, 0x00, 0x00 };
2625 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2626 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2627 0x27, 0x00, 0x00, 0x00,
2628 0x00 };
2629 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2630 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2631 0xbc, 0x27, 0x00, 0x00,
2632 0x00, 0x00 };
2633 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2634 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2635 0x90, 0x90, 0x90, 0x90,
2636 0x90, 0x90, 0x90 };
2637
2638 const char* nops[16] = {
2639 NULL,
2640 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2641 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2642 };
2643
2644 return std::string(nops[length], length);
2645 }
2646
2647 // The selector for i386 object files.
2648
2649 class Target_selector_i386 : public Target_selector
2650 {
2651 public:
2652 Target_selector_i386()
2653 : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
2654 { }
2655
2656 Target*
2657 do_instantiate_target()
2658 { return new Target_i386(); }
2659 };
2660
2661 Target_selector_i386 target_selector_i386;
2662
2663 } // End anonymous namespace.
This page took 0.128829 seconds and 4 git commands to generate.