2010-09-28 Sriraman Tallam <tmsriram@google.com>
[deliverable/binutils-gdb.git] / gold / i386.cc
1 // i386.cc -- i386 target support for gold.
2
3 // Copyright 2006, 2007, 2008, 2009, 2010 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 #include "freebsd.h"
41 #include "gc.h"
42
43 namespace
44 {
45
46 using namespace gold;
47
48 // A class to handle the PLT data.
49
50 class Output_data_plt_i386 : public Output_section_data
51 {
52 public:
53 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
54
55 Output_data_plt_i386(Symbol_table*, Layout*, Output_data_space*);
56
57 // Add an entry to the PLT.
58 void
59 add_entry(Symbol* gsym);
60
61 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
62 unsigned int
63 add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
64 unsigned int local_sym_index);
65
66 // Return the .rel.plt section data.
67 Reloc_section*
68 rel_plt() const
69 { return this->rel_; }
70
71 // Return where the TLS_DESC relocations should go.
72 Reloc_section*
73 rel_tls_desc(Layout*);
74
75 // Return the number of PLT entries.
76 unsigned int
77 entry_count() const
78 { return this->count_; }
79
80 // Return the offset of the first non-reserved PLT entry.
81 static unsigned int
82 first_plt_entry_offset()
83 { return plt_entry_size; }
84
85 // Return the size of a PLT entry.
86 static unsigned int
87 get_plt_entry_size()
88 { return plt_entry_size; }
89
90 protected:
91 void
92 do_adjust_output_section(Output_section* os);
93
94 // Write to a map file.
95 void
96 do_print_to_mapfile(Mapfile* mapfile) const
97 { mapfile->print_output_data(this, _("** PLT")); }
98
99 private:
100 // The size of an entry in the PLT.
101 static const int plt_entry_size = 16;
102
103 // The first entry in the PLT for an executable.
104 static unsigned char exec_first_plt_entry[plt_entry_size];
105
106 // The first entry in the PLT for a shared object.
107 static unsigned char dyn_first_plt_entry[plt_entry_size];
108
109 // Other entries in the PLT for an executable.
110 static unsigned char exec_plt_entry[plt_entry_size];
111
112 // Other entries in the PLT for a shared object.
113 static unsigned char dyn_plt_entry[plt_entry_size];
114
115 // Set the final size.
116 void
117 set_final_data_size()
118 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
119
120 // Write out the PLT data.
121 void
122 do_write(Output_file*);
123
124 // We keep a list of global STT_GNU_IFUNC symbols, each with its
125 // offset in the GOT.
126 struct Global_ifunc
127 {
128 Symbol* sym;
129 unsigned int got_offset;
130 };
131
132 // We keep a list of local STT_GNU_IFUNC symbols, each with its
133 // offset in the GOT.
134 struct Local_ifunc
135 {
136 Sized_relobj<32, false>* object;
137 unsigned int local_sym_index;
138 unsigned int got_offset;
139 };
140
141 // The reloc section.
142 Reloc_section* rel_;
143 // The TLS_DESC relocations, if necessary. These must follow the
144 // regular PLT relocs.
145 Reloc_section* tls_desc_rel_;
146 // The .got.plt section.
147 Output_data_space* got_plt_;
148 // The number of PLT entries.
149 unsigned int count_;
150 // Global STT_GNU_IFUNC symbols.
151 std::vector<Global_ifunc> global_ifuncs_;
152 // Local STT_GNU_IFUNC symbols.
153 std::vector<Local_ifunc> local_ifuncs_;
154 };
155
156 // The i386 target class.
157 // TLS info comes from
158 // http://people.redhat.com/drepper/tls.pdf
159 // http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
160
161 class Target_i386 : public Target_freebsd<32, false>
162 {
163 public:
164 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
165
166 Target_i386()
167 : Target_freebsd<32, false>(&i386_info),
168 got_(NULL), plt_(NULL), got_plt_(NULL), got_tlsdesc_(NULL),
169 global_offset_table_(NULL), rel_dyn_(NULL),
170 copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
171 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
172 { }
173
174 inline bool
175 can_check_for_function_pointers() const
176 { return true; }
177
178 virtual bool
179 can_icf_inline_merge_sections () const
180 { return true; }
181
182 // Process the relocations to determine unreferenced sections for
183 // garbage collection.
184 void
185 gc_process_relocs(Symbol_table* symtab,
186 Layout* layout,
187 Sized_relobj<32, false>* object,
188 unsigned int data_shndx,
189 unsigned int sh_type,
190 const unsigned char* prelocs,
191 size_t reloc_count,
192 Output_section* output_section,
193 bool needs_special_offset_handling,
194 size_t local_symbol_count,
195 const unsigned char* plocal_symbols);
196
197 // Scan the relocations to look for symbol adjustments.
198 void
199 scan_relocs(Symbol_table* symtab,
200 Layout* layout,
201 Sized_relobj<32, false>* object,
202 unsigned int data_shndx,
203 unsigned int sh_type,
204 const unsigned char* prelocs,
205 size_t reloc_count,
206 Output_section* output_section,
207 bool needs_special_offset_handling,
208 size_t local_symbol_count,
209 const unsigned char* plocal_symbols);
210
211 // Finalize the sections.
212 void
213 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
214
215 // Return the value to use for a dynamic which requires special
216 // treatment.
217 uint64_t
218 do_dynsym_value(const Symbol*) const;
219
220 // Relocate a section.
221 void
222 relocate_section(const Relocate_info<32, false>*,
223 unsigned int sh_type,
224 const unsigned char* prelocs,
225 size_t reloc_count,
226 Output_section* output_section,
227 bool needs_special_offset_handling,
228 unsigned char* view,
229 elfcpp::Elf_types<32>::Elf_Addr view_address,
230 section_size_type view_size,
231 const Reloc_symbol_changes*);
232
233 // Scan the relocs during a relocatable link.
234 void
235 scan_relocatable_relocs(Symbol_table* symtab,
236 Layout* layout,
237 Sized_relobj<32, false>* object,
238 unsigned int data_shndx,
239 unsigned int sh_type,
240 const unsigned char* prelocs,
241 size_t reloc_count,
242 Output_section* output_section,
243 bool needs_special_offset_handling,
244 size_t local_symbol_count,
245 const unsigned char* plocal_symbols,
246 Relocatable_relocs*);
247
248 // Relocate a section during a relocatable link.
249 void
250 relocate_for_relocatable(const Relocate_info<32, false>*,
251 unsigned int sh_type,
252 const unsigned char* prelocs,
253 size_t reloc_count,
254 Output_section* output_section,
255 off_t offset_in_output_section,
256 const Relocatable_relocs*,
257 unsigned char* view,
258 elfcpp::Elf_types<32>::Elf_Addr view_address,
259 section_size_type view_size,
260 unsigned char* reloc_view,
261 section_size_type reloc_view_size);
262
263 // Return a string used to fill a code section with nops.
264 std::string
265 do_code_fill(section_size_type length) const;
266
267 // Return whether SYM is defined by the ABI.
268 bool
269 do_is_defined_by_abi(const Symbol* sym) const
270 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
271
272 // Return whether a symbol name implies a local label. The UnixWare
273 // 2.1 cc generates temporary symbols that start with .X, so we
274 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
275 // If so, we should move the .X recognition into
276 // Target::do_is_local_label_name.
277 bool
278 do_is_local_label_name(const char* name) const
279 {
280 if (name[0] == '.' && name[1] == 'X')
281 return true;
282 return Target::do_is_local_label_name(name);
283 }
284
285 // Return the PLT section.
286 Output_data*
287 do_plt_section_for_global(const Symbol*) const
288 { return this->plt_section(); }
289
290 Output_data*
291 do_plt_section_for_local(const Relobj*, unsigned int) const
292 { return this->plt_section(); }
293
294 // Return whether SYM is call to a non-split function.
295 bool
296 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
297
298 // Adjust -fstack-split code which calls non-stack-split code.
299 void
300 do_calls_non_split(Relobj* object, unsigned int shndx,
301 section_offset_type fnoffset, section_size_type fnsize,
302 unsigned char* view, section_size_type view_size,
303 std::string* from, std::string* to) const;
304
305 // Return the size of the GOT section.
306 section_size_type
307 got_size() const
308 {
309 gold_assert(this->got_ != NULL);
310 return this->got_->data_size();
311 }
312
313 // Return the number of entries in the GOT.
314 unsigned int
315 got_entry_count() const
316 {
317 if (this->got_ == NULL)
318 return 0;
319 return this->got_size() / 4;
320 }
321
322 // Return the number of entries in the PLT.
323 unsigned int
324 plt_entry_count() const;
325
326 // Return the offset of the first non-reserved PLT entry.
327 unsigned int
328 first_plt_entry_offset() const;
329
330 // Return the size of each PLT entry.
331 unsigned int
332 plt_entry_size() const;
333
334 private:
335 // The class which scans relocations.
336 struct Scan
337 {
338 inline void
339 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
340 Sized_relobj<32, false>* object,
341 unsigned int data_shndx,
342 Output_section* output_section,
343 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
344 const elfcpp::Sym<32, false>& lsym);
345
346 inline void
347 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
348 Sized_relobj<32, false>* object,
349 unsigned int data_shndx,
350 Output_section* output_section,
351 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
352 Symbol* gsym);
353
354 inline bool
355 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
356 Target_i386* target,
357 Sized_relobj<32, false>* object,
358 unsigned int data_shndx,
359 Output_section* output_section,
360 const elfcpp::Rel<32, false>& reloc,
361 unsigned int r_type,
362 const elfcpp::Sym<32, false>& lsym);
363
364 inline bool
365 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
366 Target_i386* target,
367 Sized_relobj<32, false>* object,
368 unsigned int data_shndx,
369 Output_section* output_section,
370 const elfcpp::Rel<32, false>& reloc,
371 unsigned int r_type,
372 Symbol* gsym);
373
374 inline bool
375 possible_function_pointer_reloc(unsigned int r_type);
376
377 bool
378 reloc_needs_plt_for_ifunc(Sized_relobj<32, false>*, unsigned int r_type);
379
380 static void
381 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
382
383 static void
384 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
385 Symbol*);
386 };
387
388 // The class which implements relocation.
389 class Relocate
390 {
391 public:
392 Relocate()
393 : skip_call_tls_get_addr_(false),
394 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
395 { }
396
397 ~Relocate()
398 {
399 if (this->skip_call_tls_get_addr_)
400 {
401 // FIXME: This needs to specify the location somehow.
402 gold_error(_("missing expected TLS relocation"));
403 }
404 }
405
406 // Return whether the static relocation needs to be applied.
407 inline bool
408 should_apply_static_reloc(const Sized_symbol<32>* gsym,
409 int ref_flags,
410 bool is_32bit,
411 Output_section* output_section);
412
413 // Do a relocation. Return false if the caller should not issue
414 // any warnings about this relocation.
415 inline bool
416 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
417 size_t relnum, const elfcpp::Rel<32, false>&,
418 unsigned int r_type, const Sized_symbol<32>*,
419 const Symbol_value<32>*,
420 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
421 section_size_type);
422
423 private:
424 // Do a TLS relocation.
425 inline void
426 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
427 size_t relnum, const elfcpp::Rel<32, false>&,
428 unsigned int r_type, const Sized_symbol<32>*,
429 const Symbol_value<32>*,
430 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
431 section_size_type);
432
433 // Do a TLS General-Dynamic to Initial-Exec transition.
434 inline void
435 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
436 Output_segment* tls_segment,
437 const elfcpp::Rel<32, false>&, unsigned int r_type,
438 elfcpp::Elf_types<32>::Elf_Addr value,
439 unsigned char* view,
440 section_size_type view_size);
441
442 // Do a TLS General-Dynamic to Local-Exec transition.
443 inline void
444 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
445 Output_segment* tls_segment,
446 const elfcpp::Rel<32, false>&, unsigned int r_type,
447 elfcpp::Elf_types<32>::Elf_Addr value,
448 unsigned char* view,
449 section_size_type view_size);
450
451 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
452 // transition.
453 inline void
454 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
455 Output_segment* tls_segment,
456 const elfcpp::Rel<32, false>&, unsigned int r_type,
457 elfcpp::Elf_types<32>::Elf_Addr value,
458 unsigned char* view,
459 section_size_type view_size);
460
461 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
462 // transition.
463 inline void
464 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
465 Output_segment* tls_segment,
466 const elfcpp::Rel<32, false>&, unsigned int r_type,
467 elfcpp::Elf_types<32>::Elf_Addr value,
468 unsigned char* view,
469 section_size_type view_size);
470
471 // Do a TLS Local-Dynamic to Local-Exec transition.
472 inline void
473 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
474 Output_segment* tls_segment,
475 const elfcpp::Rel<32, false>&, unsigned int r_type,
476 elfcpp::Elf_types<32>::Elf_Addr value,
477 unsigned char* view,
478 section_size_type view_size);
479
480 // Do a TLS Initial-Exec to Local-Exec transition.
481 static inline void
482 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
483 Output_segment* tls_segment,
484 const elfcpp::Rel<32, false>&, unsigned int r_type,
485 elfcpp::Elf_types<32>::Elf_Addr value,
486 unsigned char* view,
487 section_size_type view_size);
488
489 // We need to keep track of which type of local dynamic relocation
490 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
491 enum Local_dynamic_type
492 {
493 LOCAL_DYNAMIC_NONE,
494 LOCAL_DYNAMIC_SUN,
495 LOCAL_DYNAMIC_GNU
496 };
497
498 // This is set if we should skip the next reloc, which should be a
499 // PLT32 reloc against ___tls_get_addr.
500 bool skip_call_tls_get_addr_;
501 // The type of local dynamic relocation we have seen in the section
502 // being relocated, if any.
503 Local_dynamic_type local_dynamic_type_;
504 };
505
506 // A class which returns the size required for a relocation type,
507 // used while scanning relocs during a relocatable link.
508 class Relocatable_size_for_reloc
509 {
510 public:
511 unsigned int
512 get_size_for_reloc(unsigned int, Relobj*);
513 };
514
515 // Adjust TLS relocation type based on the options and whether this
516 // is a local symbol.
517 static tls::Tls_optimization
518 optimize_tls_reloc(bool is_final, int r_type);
519
520 // Get the GOT section, creating it if necessary.
521 Output_data_got<32, false>*
522 got_section(Symbol_table*, Layout*);
523
524 // Get the GOT PLT section.
525 Output_data_space*
526 got_plt_section() const
527 {
528 gold_assert(this->got_plt_ != NULL);
529 return this->got_plt_;
530 }
531
532 // Get the GOT section for TLSDESC entries.
533 Output_data_got<32, false>*
534 got_tlsdesc_section() const
535 {
536 gold_assert(this->got_tlsdesc_ != NULL);
537 return this->got_tlsdesc_;
538 }
539
540 // Create the PLT section.
541 void
542 make_plt_section(Symbol_table* symtab, Layout* layout);
543
544 // Create a PLT entry for a global symbol.
545 void
546 make_plt_entry(Symbol_table*, Layout*, Symbol*);
547
548 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
549 void
550 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
551 Sized_relobj<32, false>* relobj,
552 unsigned int local_sym_index);
553
554 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
555 void
556 define_tls_base_symbol(Symbol_table*, Layout*);
557
558 // Create a GOT entry for the TLS module index.
559 unsigned int
560 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
561 Sized_relobj<32, false>* object);
562
563 // Get the PLT section.
564 Output_data_plt_i386*
565 plt_section() const
566 {
567 gold_assert(this->plt_ != NULL);
568 return this->plt_;
569 }
570
571 // Get the dynamic reloc section, creating it if necessary.
572 Reloc_section*
573 rel_dyn_section(Layout*);
574
575 // Get the section to use for TLS_DESC relocations.
576 Reloc_section*
577 rel_tls_desc_section(Layout*) const;
578
579 // Add a potential copy relocation.
580 void
581 copy_reloc(Symbol_table* symtab, Layout* layout,
582 Sized_relobj<32, false>* object,
583 unsigned int shndx, Output_section* output_section,
584 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
585 {
586 this->copy_relocs_.copy_reloc(symtab, layout,
587 symtab->get_sized_symbol<32>(sym),
588 object, shndx, output_section, reloc,
589 this->rel_dyn_section(layout));
590 }
591
592 // Information about this specific target which we pass to the
593 // general Target structure.
594 static const Target::Target_info i386_info;
595
596 // The types of GOT entries needed for this platform.
597 // These values are exposed to the ABI in an incremental link.
598 // Do not renumber existing values without changing the version
599 // number of the .gnu_incremental_inputs section.
600 enum Got_type
601 {
602 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
603 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
604 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
605 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
606 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
607 };
608
609 // The GOT section.
610 Output_data_got<32, false>* got_;
611 // The PLT section.
612 Output_data_plt_i386* plt_;
613 // The GOT PLT section.
614 Output_data_space* got_plt_;
615 // The GOT section for TLSDESC relocations.
616 Output_data_got<32, false>* got_tlsdesc_;
617 // The _GLOBAL_OFFSET_TABLE_ symbol.
618 Symbol* global_offset_table_;
619 // The dynamic reloc section.
620 Reloc_section* rel_dyn_;
621 // Relocs saved to avoid a COPY reloc.
622 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
623 // Space for variables copied with a COPY reloc.
624 Output_data_space* dynbss_;
625 // Offset of the GOT entry for the TLS module index.
626 unsigned int got_mod_index_offset_;
627 // True if the _TLS_MODULE_BASE_ symbol has been defined.
628 bool tls_base_symbol_defined_;
629 };
630
631 const Target::Target_info Target_i386::i386_info =
632 {
633 32, // size
634 false, // is_big_endian
635 elfcpp::EM_386, // machine_code
636 false, // has_make_symbol
637 false, // has_resolve
638 true, // has_code_fill
639 true, // is_default_stack_executable
640 '\0', // wrap_char
641 "/usr/lib/libc.so.1", // dynamic_linker
642 0x08048000, // default_text_segment_address
643 0x1000, // abi_pagesize (overridable by -z max-page-size)
644 0x1000, // common_pagesize (overridable by -z common-page-size)
645 elfcpp::SHN_UNDEF, // small_common_shndx
646 elfcpp::SHN_UNDEF, // large_common_shndx
647 0, // small_common_section_flags
648 0, // large_common_section_flags
649 NULL, // attributes_section
650 NULL // attributes_vendor
651 };
652
653 // Get the GOT section, creating it if necessary.
654
655 Output_data_got<32, false>*
656 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
657 {
658 if (this->got_ == NULL)
659 {
660 gold_assert(symtab != NULL && layout != NULL);
661
662 this->got_ = new Output_data_got<32, false>();
663
664 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
665 (elfcpp::SHF_ALLOC
666 | elfcpp::SHF_WRITE),
667 this->got_, ORDER_RELRO_LAST, true);
668
669 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
670 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
671 (elfcpp::SHF_ALLOC
672 | elfcpp::SHF_WRITE),
673 this->got_plt_, ORDER_NON_RELRO_FIRST,
674 false);
675
676 // The first three entries are reserved.
677 this->got_plt_->set_current_data_size(3 * 4);
678
679 // Those bytes can go into the relro segment.
680 layout->increase_relro(3 * 4);
681
682 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
683 this->global_offset_table_ =
684 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
685 Symbol_table::PREDEFINED,
686 this->got_plt_,
687 0, 0, elfcpp::STT_OBJECT,
688 elfcpp::STB_LOCAL,
689 elfcpp::STV_HIDDEN, 0,
690 false, false);
691
692 // If there are any TLSDESC relocations, they get GOT entries in
693 // .got.plt after the jump slot entries.
694 this->got_tlsdesc_ = new Output_data_got<32, false>();
695 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
696 (elfcpp::SHF_ALLOC
697 | elfcpp::SHF_WRITE),
698 this->got_tlsdesc_,
699 ORDER_NON_RELRO_FIRST, false);
700 }
701
702 return this->got_;
703 }
704
705 // Get the dynamic reloc section, creating it if necessary.
706
707 Target_i386::Reloc_section*
708 Target_i386::rel_dyn_section(Layout* layout)
709 {
710 if (this->rel_dyn_ == NULL)
711 {
712 gold_assert(layout != NULL);
713 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
714 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
715 elfcpp::SHF_ALLOC, this->rel_dyn_,
716 ORDER_DYNAMIC_RELOCS, false);
717 }
718 return this->rel_dyn_;
719 }
720
721 // Create the PLT section. The ordinary .got section is an argument,
722 // since we need to refer to the start. We also create our own .got
723 // section just for PLT entries.
724
725 Output_data_plt_i386::Output_data_plt_i386(Symbol_table* symtab,
726 Layout* layout,
727 Output_data_space* got_plt)
728 : Output_section_data(4), tls_desc_rel_(NULL), got_plt_(got_plt), count_(0),
729 global_ifuncs_(), local_ifuncs_()
730 {
731 this->rel_ = new Reloc_section(false);
732 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
733 elfcpp::SHF_ALLOC, this->rel_,
734 ORDER_DYNAMIC_PLT_RELOCS, false);
735
736 if (parameters->doing_static_link())
737 {
738 // A statically linked executable will only have a .rel.plt
739 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
740 // symbols. The library will use these symbols to locate the
741 // IRELATIVE relocs at program startup time.
742 symtab->define_in_output_data("__rel_iplt_start", NULL,
743 Symbol_table::PREDEFINED,
744 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
745 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
746 0, false, true);
747 symtab->define_in_output_data("__rel_iplt_end", NULL,
748 Symbol_table::PREDEFINED,
749 this->rel_, 0, 0, elfcpp::STT_NOTYPE,
750 elfcpp::STB_GLOBAL, elfcpp::STV_HIDDEN,
751 0, true, true);
752 }
753 }
754
755 void
756 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
757 {
758 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
759 // linker, and so do we.
760 os->set_entsize(4);
761 }
762
763 // Add an entry to the PLT.
764
765 void
766 Output_data_plt_i386::add_entry(Symbol* gsym)
767 {
768 gold_assert(!gsym->has_plt_offset());
769
770 // Note that when setting the PLT offset we skip the initial
771 // reserved PLT entry.
772 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
773
774 ++this->count_;
775
776 section_offset_type got_offset = this->got_plt_->current_data_size();
777
778 // Every PLT entry needs a GOT entry which points back to the PLT
779 // entry (this will be changed by the dynamic linker, normally
780 // lazily when the function is called).
781 this->got_plt_->set_current_data_size(got_offset + 4);
782
783 // Every PLT entry needs a reloc.
784 if (gsym->type() == elfcpp::STT_GNU_IFUNC
785 && gsym->can_use_relative_reloc(false))
786 {
787 this->rel_->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
788 this->got_plt_, got_offset);
789 struct Global_ifunc gi;
790 gi.sym = gsym;
791 gi.got_offset = got_offset;
792 this->global_ifuncs_.push_back(gi);
793 }
794 else
795 {
796 gsym->set_needs_dynsym_entry();
797 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
798 got_offset);
799 }
800
801 // Note that we don't need to save the symbol. The contents of the
802 // PLT are independent of which symbols are used. The symbols only
803 // appear in the relocations.
804 }
805
806 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
807 // the PLT offset.
808
809 unsigned int
810 Output_data_plt_i386::add_local_ifunc_entry(Sized_relobj<32, false>* relobj,
811 unsigned int local_sym_index)
812 {
813 unsigned int plt_offset = (this->count_ + 1) * plt_entry_size;
814 ++this->count_;
815
816 section_offset_type got_offset = this->got_plt_->current_data_size();
817
818 // Every PLT entry needs a GOT entry which points back to the PLT
819 // entry.
820 this->got_plt_->set_current_data_size(got_offset + 4);
821
822 // Every PLT entry needs a reloc.
823 this->rel_->add_symbolless_local_addend(relobj, local_sym_index,
824 elfcpp::R_386_IRELATIVE,
825 this->got_plt_, got_offset);
826
827 struct Local_ifunc li;
828 li.object = relobj;
829 li.local_sym_index = local_sym_index;
830 li.got_offset = got_offset;
831 this->local_ifuncs_.push_back(li);
832
833 return plt_offset;
834 }
835
836 // Return where the TLS_DESC relocations should go, creating it if
837 // necessary. These follow the JUMP_SLOT relocations.
838
839 Output_data_plt_i386::Reloc_section*
840 Output_data_plt_i386::rel_tls_desc(Layout* layout)
841 {
842 if (this->tls_desc_rel_ == NULL)
843 {
844 this->tls_desc_rel_ = new Reloc_section(false);
845 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
846 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
847 ORDER_DYNAMIC_PLT_RELOCS, false);
848 gold_assert(this->tls_desc_rel_->output_section() ==
849 this->rel_->output_section());
850 }
851 return this->tls_desc_rel_;
852 }
853
854 // The first entry in the PLT for an executable.
855
856 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
857 {
858 0xff, 0x35, // pushl contents of memory address
859 0, 0, 0, 0, // replaced with address of .got + 4
860 0xff, 0x25, // jmp indirect
861 0, 0, 0, 0, // replaced with address of .got + 8
862 0, 0, 0, 0 // unused
863 };
864
865 // The first entry in the PLT for a shared object.
866
867 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
868 {
869 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
870 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
871 0, 0, 0, 0 // unused
872 };
873
874 // Subsequent entries in the PLT for an executable.
875
876 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
877 {
878 0xff, 0x25, // jmp indirect
879 0, 0, 0, 0, // replaced with address of symbol in .got
880 0x68, // pushl immediate
881 0, 0, 0, 0, // replaced with offset into relocation table
882 0xe9, // jmp relative
883 0, 0, 0, 0 // replaced with offset to start of .plt
884 };
885
886 // Subsequent entries in the PLT for a shared object.
887
888 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
889 {
890 0xff, 0xa3, // jmp *offset(%ebx)
891 0, 0, 0, 0, // replaced with offset of symbol in .got
892 0x68, // pushl immediate
893 0, 0, 0, 0, // replaced with offset into relocation table
894 0xe9, // jmp relative
895 0, 0, 0, 0 // replaced with offset to start of .plt
896 };
897
898 // Write out the PLT. This uses the hand-coded instructions above,
899 // and adjusts them as needed. This is all specified by the i386 ELF
900 // Processor Supplement.
901
902 void
903 Output_data_plt_i386::do_write(Output_file* of)
904 {
905 const off_t offset = this->offset();
906 const section_size_type oview_size =
907 convert_to_section_size_type(this->data_size());
908 unsigned char* const oview = of->get_output_view(offset, oview_size);
909
910 const off_t got_file_offset = this->got_plt_->offset();
911 const section_size_type got_size =
912 convert_to_section_size_type(this->got_plt_->data_size());
913 unsigned char* const got_view = of->get_output_view(got_file_offset,
914 got_size);
915
916 unsigned char* pov = oview;
917
918 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
919 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
920
921 if (parameters->options().output_is_position_independent())
922 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
923 else
924 {
925 memcpy(pov, exec_first_plt_entry, plt_entry_size);
926 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
927 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
928 }
929 pov += plt_entry_size;
930
931 unsigned char* got_pov = got_view;
932
933 memset(got_pov, 0, 12);
934 got_pov += 12;
935
936 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
937
938 unsigned int plt_offset = plt_entry_size;
939 unsigned int plt_rel_offset = 0;
940 unsigned int got_offset = 12;
941 const unsigned int count = this->count_;
942 for (unsigned int i = 0;
943 i < count;
944 ++i,
945 pov += plt_entry_size,
946 got_pov += 4,
947 plt_offset += plt_entry_size,
948 plt_rel_offset += rel_size,
949 got_offset += 4)
950 {
951 // Set and adjust the PLT entry itself.
952
953 if (parameters->options().output_is_position_independent())
954 {
955 memcpy(pov, dyn_plt_entry, plt_entry_size);
956 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
957 }
958 else
959 {
960 memcpy(pov, exec_plt_entry, plt_entry_size);
961 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
962 (got_address
963 + got_offset));
964 }
965
966 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
967 elfcpp::Swap<32, false>::writeval(pov + 12,
968 - (plt_offset + plt_entry_size));
969
970 // Set the entry in the GOT.
971 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
972 }
973
974 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
975 // the GOT to point to the actual symbol value, rather than point to
976 // the PLT entry. That will let the dynamic linker call the right
977 // function when resolving IRELATIVE relocations.
978 for (std::vector<Global_ifunc>::const_iterator p =
979 this->global_ifuncs_.begin();
980 p != this->global_ifuncs_.end();
981 ++p)
982 {
983 const Sized_symbol<32>* ssym =
984 static_cast<const Sized_symbol<32>*>(p->sym);
985 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
986 ssym->value());
987 }
988
989 for (std::vector<Local_ifunc>::const_iterator p =
990 this->local_ifuncs_.begin();
991 p != this->local_ifuncs_.end();
992 ++p)
993 {
994 const Symbol_value<32>* psymval =
995 p->object->local_symbol(p->local_sym_index);
996 elfcpp::Swap<32, false>::writeval(got_view + p->got_offset,
997 psymval->value(p->object, 0));
998 }
999
1000 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1001 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1002
1003 of->write_output_view(offset, oview_size, oview);
1004 of->write_output_view(got_file_offset, got_size, got_view);
1005 }
1006
1007 // Create the PLT section.
1008
1009 void
1010 Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
1011 {
1012 if (this->plt_ == NULL)
1013 {
1014 // Create the GOT sections first.
1015 this->got_section(symtab, layout);
1016
1017 this->plt_ = new Output_data_plt_i386(symtab, layout, this->got_plt_);
1018 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1019 (elfcpp::SHF_ALLOC
1020 | elfcpp::SHF_EXECINSTR),
1021 this->plt_, ORDER_PLT, false);
1022
1023 // Make the sh_info field of .rel.plt point to .plt.
1024 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1025 rel_plt_os->set_info_section(this->plt_->output_section());
1026 }
1027 }
1028
1029 // Create a PLT entry for a global symbol.
1030
1031 void
1032 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1033 {
1034 if (gsym->has_plt_offset())
1035 return;
1036 if (this->plt_ == NULL)
1037 this->make_plt_section(symtab, layout);
1038 this->plt_->add_entry(gsym);
1039 }
1040
1041 // Make a PLT entry for a local STT_GNU_IFUNC symbol.
1042
1043 void
1044 Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
1045 Sized_relobj<32, false>* relobj,
1046 unsigned int local_sym_index)
1047 {
1048 if (relobj->local_has_plt_offset(local_sym_index))
1049 return;
1050 if (this->plt_ == NULL)
1051 this->make_plt_section(symtab, layout);
1052 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(relobj,
1053 local_sym_index);
1054 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1055 }
1056
1057 // Return the number of entries in the PLT.
1058
1059 unsigned int
1060 Target_i386::plt_entry_count() const
1061 {
1062 if (this->plt_ == NULL)
1063 return 0;
1064 return this->plt_->entry_count();
1065 }
1066
1067 // Return the offset of the first non-reserved PLT entry.
1068
1069 unsigned int
1070 Target_i386::first_plt_entry_offset() const
1071 {
1072 return Output_data_plt_i386::first_plt_entry_offset();
1073 }
1074
1075 // Return the size of each PLT entry.
1076
1077 unsigned int
1078 Target_i386::plt_entry_size() const
1079 {
1080 return Output_data_plt_i386::get_plt_entry_size();
1081 }
1082
1083 // Get the section to use for TLS_DESC relocations.
1084
1085 Target_i386::Reloc_section*
1086 Target_i386::rel_tls_desc_section(Layout* layout) const
1087 {
1088 return this->plt_section()->rel_tls_desc(layout);
1089 }
1090
1091 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
1092
1093 void
1094 Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1095 {
1096 if (this->tls_base_symbol_defined_)
1097 return;
1098
1099 Output_segment* tls_segment = layout->tls_segment();
1100 if (tls_segment != NULL)
1101 {
1102 bool is_exec = parameters->options().output_is_executable();
1103 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
1104 Symbol_table::PREDEFINED,
1105 tls_segment, 0, 0,
1106 elfcpp::STT_TLS,
1107 elfcpp::STB_LOCAL,
1108 elfcpp::STV_HIDDEN, 0,
1109 (is_exec
1110 ? Symbol::SEGMENT_END
1111 : Symbol::SEGMENT_START),
1112 true);
1113 }
1114 this->tls_base_symbol_defined_ = true;
1115 }
1116
1117 // Create a GOT entry for the TLS module index.
1118
1119 unsigned int
1120 Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
1121 Sized_relobj<32, false>* object)
1122 {
1123 if (this->got_mod_index_offset_ == -1U)
1124 {
1125 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1126 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1127 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1128 unsigned int got_offset = got->add_constant(0);
1129 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1130 got_offset);
1131 got->add_constant(0);
1132 this->got_mod_index_offset_ = got_offset;
1133 }
1134 return this->got_mod_index_offset_;
1135 }
1136
1137 // Optimize the TLS relocation type based on what we know about the
1138 // symbol. IS_FINAL is true if the final address of this symbol is
1139 // known at link time.
1140
1141 tls::Tls_optimization
1142 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
1143 {
1144 // If we are generating a shared library, then we can't do anything
1145 // in the linker.
1146 if (parameters->options().shared())
1147 return tls::TLSOPT_NONE;
1148
1149 switch (r_type)
1150 {
1151 case elfcpp::R_386_TLS_GD:
1152 case elfcpp::R_386_TLS_GOTDESC:
1153 case elfcpp::R_386_TLS_DESC_CALL:
1154 // These are General-Dynamic which permits fully general TLS
1155 // access. Since we know that we are generating an executable,
1156 // we can convert this to Initial-Exec. If we also know that
1157 // this is a local symbol, we can further switch to Local-Exec.
1158 if (is_final)
1159 return tls::TLSOPT_TO_LE;
1160 return tls::TLSOPT_TO_IE;
1161
1162 case elfcpp::R_386_TLS_LDM:
1163 // This is Local-Dynamic, which refers to a local symbol in the
1164 // dynamic TLS block. Since we know that we generating an
1165 // executable, we can switch to Local-Exec.
1166 return tls::TLSOPT_TO_LE;
1167
1168 case elfcpp::R_386_TLS_LDO_32:
1169 // Another type of Local-Dynamic relocation.
1170 return tls::TLSOPT_TO_LE;
1171
1172 case elfcpp::R_386_TLS_IE:
1173 case elfcpp::R_386_TLS_GOTIE:
1174 case elfcpp::R_386_TLS_IE_32:
1175 // These are Initial-Exec relocs which get the thread offset
1176 // from the GOT. If we know that we are linking against the
1177 // local symbol, we can switch to Local-Exec, which links the
1178 // thread offset into the instruction.
1179 if (is_final)
1180 return tls::TLSOPT_TO_LE;
1181 return tls::TLSOPT_NONE;
1182
1183 case elfcpp::R_386_TLS_LE:
1184 case elfcpp::R_386_TLS_LE_32:
1185 // When we already have Local-Exec, there is nothing further we
1186 // can do.
1187 return tls::TLSOPT_NONE;
1188
1189 default:
1190 gold_unreachable();
1191 }
1192 }
1193
1194 // Report an unsupported relocation against a local symbol.
1195
1196 void
1197 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
1198 unsigned int r_type)
1199 {
1200 gold_error(_("%s: unsupported reloc %u against local symbol"),
1201 object->name().c_str(), r_type);
1202 }
1203
1204 // Return whether we need to make a PLT entry for a relocation of a
1205 // given type against a STT_GNU_IFUNC symbol.
1206
1207 bool
1208 Target_i386::Scan::reloc_needs_plt_for_ifunc(Sized_relobj<32, false>* object,
1209 unsigned int r_type)
1210 {
1211 switch (r_type)
1212 {
1213 case elfcpp::R_386_NONE:
1214 case elfcpp::R_386_GNU_VTINHERIT:
1215 case elfcpp::R_386_GNU_VTENTRY:
1216 return false;
1217
1218 case elfcpp::R_386_32:
1219 case elfcpp::R_386_16:
1220 case elfcpp::R_386_8:
1221 case elfcpp::R_386_PC32:
1222 case elfcpp::R_386_PC16:
1223 case elfcpp::R_386_PC8:
1224 case elfcpp::R_386_PLT32:
1225 case elfcpp::R_386_GOTOFF:
1226 case elfcpp::R_386_GOTPC:
1227 case elfcpp::R_386_GOT32:
1228 return true;
1229
1230 case elfcpp::R_386_COPY:
1231 case elfcpp::R_386_GLOB_DAT:
1232 case elfcpp::R_386_JUMP_SLOT:
1233 case elfcpp::R_386_RELATIVE:
1234 case elfcpp::R_386_IRELATIVE:
1235 case elfcpp::R_386_TLS_TPOFF:
1236 case elfcpp::R_386_TLS_DTPMOD32:
1237 case elfcpp::R_386_TLS_DTPOFF32:
1238 case elfcpp::R_386_TLS_TPOFF32:
1239 case elfcpp::R_386_TLS_DESC:
1240 // We will give an error later.
1241 return false;
1242
1243 case elfcpp::R_386_TLS_GD:
1244 case elfcpp::R_386_TLS_GOTDESC:
1245 case elfcpp::R_386_TLS_DESC_CALL:
1246 case elfcpp::R_386_TLS_LDM:
1247 case elfcpp::R_386_TLS_LDO_32:
1248 case elfcpp::R_386_TLS_IE:
1249 case elfcpp::R_386_TLS_IE_32:
1250 case elfcpp::R_386_TLS_GOTIE:
1251 case elfcpp::R_386_TLS_LE:
1252 case elfcpp::R_386_TLS_LE_32:
1253 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1254 object->name().c_str(), r_type);
1255 return false;
1256
1257 case elfcpp::R_386_32PLT:
1258 case elfcpp::R_386_TLS_GD_32:
1259 case elfcpp::R_386_TLS_GD_PUSH:
1260 case elfcpp::R_386_TLS_GD_CALL:
1261 case elfcpp::R_386_TLS_GD_POP:
1262 case elfcpp::R_386_TLS_LDM_32:
1263 case elfcpp::R_386_TLS_LDM_PUSH:
1264 case elfcpp::R_386_TLS_LDM_CALL:
1265 case elfcpp::R_386_TLS_LDM_POP:
1266 case elfcpp::R_386_USED_BY_INTEL_200:
1267 default:
1268 // We will give an error later.
1269 return false;
1270 }
1271 }
1272
1273 // Scan a relocation for a local symbol.
1274
1275 inline void
1276 Target_i386::Scan::local(Symbol_table* symtab,
1277 Layout* layout,
1278 Target_i386* target,
1279 Sized_relobj<32, false>* object,
1280 unsigned int data_shndx,
1281 Output_section* output_section,
1282 const elfcpp::Rel<32, false>& reloc,
1283 unsigned int r_type,
1284 const elfcpp::Sym<32, false>& lsym)
1285 {
1286 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1287 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1288 && this->reloc_needs_plt_for_ifunc(object, r_type))
1289 {
1290 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1291 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1292 }
1293
1294 switch (r_type)
1295 {
1296 case elfcpp::R_386_NONE:
1297 case elfcpp::R_386_GNU_VTINHERIT:
1298 case elfcpp::R_386_GNU_VTENTRY:
1299 break;
1300
1301 case elfcpp::R_386_32:
1302 // If building a shared library (or a position-independent
1303 // executable), we need to create a dynamic relocation for
1304 // this location. The relocation applied at link time will
1305 // apply the link-time value, so we flag the location with
1306 // an R_386_RELATIVE relocation so the dynamic loader can
1307 // relocate it easily.
1308 if (parameters->options().output_is_position_independent())
1309 {
1310 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1311 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1312 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1313 output_section, data_shndx,
1314 reloc.get_r_offset());
1315 }
1316 break;
1317
1318 case elfcpp::R_386_16:
1319 case elfcpp::R_386_8:
1320 // If building a shared library (or a position-independent
1321 // executable), we need to create a dynamic relocation for
1322 // this location. Because the addend needs to remain in the
1323 // data section, we need to be careful not to apply this
1324 // relocation statically.
1325 if (parameters->options().output_is_position_independent())
1326 {
1327 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1328 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1329 if (lsym.get_st_type() != elfcpp::STT_SECTION)
1330 rel_dyn->add_local(object, r_sym, r_type, output_section,
1331 data_shndx, reloc.get_r_offset());
1332 else
1333 {
1334 gold_assert(lsym.get_st_value() == 0);
1335 unsigned int shndx = lsym.get_st_shndx();
1336 bool is_ordinary;
1337 shndx = object->adjust_sym_shndx(r_sym, shndx,
1338 &is_ordinary);
1339 if (!is_ordinary)
1340 object->error(_("section symbol %u has bad shndx %u"),
1341 r_sym, shndx);
1342 else
1343 rel_dyn->add_local_section(object, shndx,
1344 r_type, output_section,
1345 data_shndx, reloc.get_r_offset());
1346 }
1347 }
1348 break;
1349
1350 case elfcpp::R_386_PC32:
1351 case elfcpp::R_386_PC16:
1352 case elfcpp::R_386_PC8:
1353 break;
1354
1355 case elfcpp::R_386_PLT32:
1356 // Since we know this is a local symbol, we can handle this as a
1357 // PC32 reloc.
1358 break;
1359
1360 case elfcpp::R_386_GOTOFF:
1361 case elfcpp::R_386_GOTPC:
1362 // We need a GOT section.
1363 target->got_section(symtab, layout);
1364 break;
1365
1366 case elfcpp::R_386_GOT32:
1367 {
1368 // The symbol requires a GOT entry.
1369 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1370 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1371
1372 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1373 // lets function pointers compare correctly with shared
1374 // libraries. Otherwise we would need an IRELATIVE reloc.
1375 bool is_new;
1376 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1377 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1378 else
1379 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1380 if (is_new)
1381 {
1382 // If we are generating a shared object, we need to add a
1383 // dynamic RELATIVE relocation for this symbol's GOT entry.
1384 if (parameters->options().output_is_position_independent())
1385 {
1386 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1387 unsigned int got_offset =
1388 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1389 rel_dyn->add_local_relative(object, r_sym,
1390 elfcpp::R_386_RELATIVE,
1391 got, got_offset);
1392 }
1393 }
1394 }
1395 break;
1396
1397 // These are relocations which should only be seen by the
1398 // dynamic linker, and should never be seen here.
1399 case elfcpp::R_386_COPY:
1400 case elfcpp::R_386_GLOB_DAT:
1401 case elfcpp::R_386_JUMP_SLOT:
1402 case elfcpp::R_386_RELATIVE:
1403 case elfcpp::R_386_IRELATIVE:
1404 case elfcpp::R_386_TLS_TPOFF:
1405 case elfcpp::R_386_TLS_DTPMOD32:
1406 case elfcpp::R_386_TLS_DTPOFF32:
1407 case elfcpp::R_386_TLS_TPOFF32:
1408 case elfcpp::R_386_TLS_DESC:
1409 gold_error(_("%s: unexpected reloc %u in object file"),
1410 object->name().c_str(), r_type);
1411 break;
1412
1413 // These are initial TLS relocs, which are expected when
1414 // linking.
1415 case elfcpp::R_386_TLS_GD: // Global-dynamic
1416 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1417 case elfcpp::R_386_TLS_DESC_CALL:
1418 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1419 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1420 case elfcpp::R_386_TLS_IE: // Initial-exec
1421 case elfcpp::R_386_TLS_IE_32:
1422 case elfcpp::R_386_TLS_GOTIE:
1423 case elfcpp::R_386_TLS_LE: // Local-exec
1424 case elfcpp::R_386_TLS_LE_32:
1425 {
1426 bool output_is_shared = parameters->options().shared();
1427 const tls::Tls_optimization optimized_type
1428 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
1429 switch (r_type)
1430 {
1431 case elfcpp::R_386_TLS_GD: // Global-dynamic
1432 if (optimized_type == tls::TLSOPT_NONE)
1433 {
1434 // Create a pair of GOT entries for the module index and
1435 // dtv-relative offset.
1436 Output_data_got<32, false>* got
1437 = target->got_section(symtab, layout);
1438 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1439 unsigned int shndx = lsym.get_st_shndx();
1440 bool is_ordinary;
1441 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1442 if (!is_ordinary)
1443 object->error(_("local symbol %u has bad shndx %u"),
1444 r_sym, shndx);
1445 else
1446 got->add_local_pair_with_rel(object, r_sym, shndx,
1447 GOT_TYPE_TLS_PAIR,
1448 target->rel_dyn_section(layout),
1449 elfcpp::R_386_TLS_DTPMOD32, 0);
1450 }
1451 else if (optimized_type != tls::TLSOPT_TO_LE)
1452 unsupported_reloc_local(object, r_type);
1453 break;
1454
1455 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
1456 target->define_tls_base_symbol(symtab, layout);
1457 if (optimized_type == tls::TLSOPT_NONE)
1458 {
1459 // Create a double GOT entry with an R_386_TLS_DESC
1460 // reloc. The R_386_TLS_DESC reloc is resolved
1461 // lazily, so the GOT entry needs to be in an area in
1462 // .got.plt, not .got. Call got_section to make sure
1463 // the section has been created.
1464 target->got_section(symtab, layout);
1465 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1466 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1467 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1468 {
1469 unsigned int got_offset = got->add_constant(0);
1470 // The local symbol value is stored in the second
1471 // GOT entry.
1472 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1473 // That set the GOT offset of the local symbol to
1474 // point to the second entry, but we want it to
1475 // point to the first.
1476 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1477 got_offset);
1478 Reloc_section* rt = target->rel_tls_desc_section(layout);
1479 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1480 }
1481 }
1482 else if (optimized_type != tls::TLSOPT_TO_LE)
1483 unsupported_reloc_local(object, r_type);
1484 break;
1485
1486 case elfcpp::R_386_TLS_DESC_CALL:
1487 break;
1488
1489 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1490 if (optimized_type == tls::TLSOPT_NONE)
1491 {
1492 // Create a GOT entry for the module index.
1493 target->got_mod_index_entry(symtab, layout, object);
1494 }
1495 else if (optimized_type != tls::TLSOPT_TO_LE)
1496 unsupported_reloc_local(object, r_type);
1497 break;
1498
1499 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1500 break;
1501
1502 case elfcpp::R_386_TLS_IE: // Initial-exec
1503 case elfcpp::R_386_TLS_IE_32:
1504 case elfcpp::R_386_TLS_GOTIE:
1505 layout->set_has_static_tls();
1506 if (optimized_type == tls::TLSOPT_NONE)
1507 {
1508 // For the R_386_TLS_IE relocation, we need to create a
1509 // dynamic relocation when building a shared library.
1510 if (r_type == elfcpp::R_386_TLS_IE
1511 && parameters->options().shared())
1512 {
1513 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1514 unsigned int r_sym
1515 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1516 rel_dyn->add_local_relative(object, r_sym,
1517 elfcpp::R_386_RELATIVE,
1518 output_section, data_shndx,
1519 reloc.get_r_offset());
1520 }
1521 // Create a GOT entry for the tp-relative offset.
1522 Output_data_got<32, false>* got
1523 = target->got_section(symtab, layout);
1524 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1525 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1526 ? elfcpp::R_386_TLS_TPOFF32
1527 : elfcpp::R_386_TLS_TPOFF);
1528 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1529 ? GOT_TYPE_TLS_OFFSET
1530 : GOT_TYPE_TLS_NOFFSET);
1531 got->add_local_with_rel(object, r_sym, got_type,
1532 target->rel_dyn_section(layout),
1533 dyn_r_type);
1534 }
1535 else if (optimized_type != tls::TLSOPT_TO_LE)
1536 unsupported_reloc_local(object, r_type);
1537 break;
1538
1539 case elfcpp::R_386_TLS_LE: // Local-exec
1540 case elfcpp::R_386_TLS_LE_32:
1541 layout->set_has_static_tls();
1542 if (output_is_shared)
1543 {
1544 // We need to create a dynamic relocation.
1545 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
1546 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1547 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1548 ? elfcpp::R_386_TLS_TPOFF32
1549 : elfcpp::R_386_TLS_TPOFF);
1550 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1551 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1552 data_shndx, reloc.get_r_offset());
1553 }
1554 break;
1555
1556 default:
1557 gold_unreachable();
1558 }
1559 }
1560 break;
1561
1562 case elfcpp::R_386_32PLT:
1563 case elfcpp::R_386_TLS_GD_32:
1564 case elfcpp::R_386_TLS_GD_PUSH:
1565 case elfcpp::R_386_TLS_GD_CALL:
1566 case elfcpp::R_386_TLS_GD_POP:
1567 case elfcpp::R_386_TLS_LDM_32:
1568 case elfcpp::R_386_TLS_LDM_PUSH:
1569 case elfcpp::R_386_TLS_LDM_CALL:
1570 case elfcpp::R_386_TLS_LDM_POP:
1571 case elfcpp::R_386_USED_BY_INTEL_200:
1572 default:
1573 unsupported_reloc_local(object, r_type);
1574 break;
1575 }
1576 }
1577
1578 // Report an unsupported relocation against a global symbol.
1579
1580 void
1581 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1582 unsigned int r_type,
1583 Symbol* gsym)
1584 {
1585 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1586 object->name().c_str(), r_type, gsym->demangled_name().c_str());
1587 }
1588
1589 inline bool
1590 Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1591 {
1592 switch (r_type)
1593 {
1594 case elfcpp::R_386_32:
1595 case elfcpp::R_386_16:
1596 case elfcpp::R_386_8:
1597 case elfcpp::R_386_GOTOFF:
1598 case elfcpp::R_386_GOT32:
1599 {
1600 return true;
1601 }
1602 default:
1603 return false;
1604 }
1605 return false;
1606 }
1607
1608 inline bool
1609 Target_i386::Scan::local_reloc_may_be_function_pointer(
1610 Symbol_table* ,
1611 Layout* ,
1612 Target_i386* ,
1613 Sized_relobj<32, false>* ,
1614 unsigned int ,
1615 Output_section* ,
1616 const elfcpp::Rel<32, false>& ,
1617 unsigned int r_type,
1618 const elfcpp::Sym<32, false>&)
1619 {
1620 return possible_function_pointer_reloc(r_type);
1621 }
1622
1623 inline bool
1624 Target_i386::Scan::global_reloc_may_be_function_pointer(
1625 Symbol_table* ,
1626 Layout* ,
1627 Target_i386* ,
1628 Sized_relobj<32, false>* ,
1629 unsigned int ,
1630 Output_section* ,
1631 const elfcpp::Rel<32, false>& ,
1632 unsigned int r_type,
1633 Symbol*)
1634 {
1635 return possible_function_pointer_reloc(r_type);
1636 }
1637
1638 // Scan a relocation for a global symbol.
1639
1640 inline void
1641 Target_i386::Scan::global(Symbol_table* symtab,
1642 Layout* layout,
1643 Target_i386* target,
1644 Sized_relobj<32, false>* object,
1645 unsigned int data_shndx,
1646 Output_section* output_section,
1647 const elfcpp::Rel<32, false>& reloc,
1648 unsigned int r_type,
1649 Symbol* gsym)
1650 {
1651 // A STT_GNU_IFUNC symbol may require a PLT entry.
1652 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1653 && this->reloc_needs_plt_for_ifunc(object, r_type))
1654 target->make_plt_entry(symtab, layout, gsym);
1655
1656 switch (r_type)
1657 {
1658 case elfcpp::R_386_NONE:
1659 case elfcpp::R_386_GNU_VTINHERIT:
1660 case elfcpp::R_386_GNU_VTENTRY:
1661 break;
1662
1663 case elfcpp::R_386_32:
1664 case elfcpp::R_386_16:
1665 case elfcpp::R_386_8:
1666 {
1667 // Make a PLT entry if necessary.
1668 if (gsym->needs_plt_entry())
1669 {
1670 target->make_plt_entry(symtab, layout, gsym);
1671 // Since this is not a PC-relative relocation, we may be
1672 // taking the address of a function. In that case we need to
1673 // set the entry in the dynamic symbol table to the address of
1674 // the PLT entry.
1675 if (gsym->is_from_dynobj() && !parameters->options().shared())
1676 gsym->set_needs_dynsym_value();
1677 }
1678 // Make a dynamic relocation if necessary.
1679 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1680 {
1681 if (gsym->may_need_copy_reloc())
1682 {
1683 target->copy_reloc(symtab, layout, object,
1684 data_shndx, output_section, gsym, reloc);
1685 }
1686 else if (r_type == elfcpp::R_386_32
1687 && gsym->type() == elfcpp::STT_GNU_IFUNC
1688 && gsym->can_use_relative_reloc(false)
1689 && !gsym->is_from_dynobj()
1690 && !gsym->is_undefined()
1691 && !gsym->is_preemptible())
1692 {
1693 // Use an IRELATIVE reloc for a locally defined
1694 // STT_GNU_IFUNC symbol. This makes a function
1695 // address in a PIE executable match the address in a
1696 // shared library that it links against.
1697 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1698 rel_dyn->add_symbolless_global_addend(gsym,
1699 elfcpp::R_386_IRELATIVE,
1700 output_section,
1701 object, data_shndx,
1702 reloc.get_r_offset());
1703 }
1704 else if (r_type == elfcpp::R_386_32
1705 && gsym->can_use_relative_reloc(false))
1706 {
1707 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1708 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1709 output_section, object,
1710 data_shndx, reloc.get_r_offset());
1711 }
1712 else
1713 {
1714 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1715 rel_dyn->add_global(gsym, r_type, output_section, object,
1716 data_shndx, reloc.get_r_offset());
1717 }
1718 }
1719 }
1720 break;
1721
1722 case elfcpp::R_386_PC32:
1723 case elfcpp::R_386_PC16:
1724 case elfcpp::R_386_PC8:
1725 {
1726 // Make a PLT entry if necessary.
1727 if (gsym->needs_plt_entry())
1728 {
1729 // These relocations are used for function calls only in
1730 // non-PIC code. For a 32-bit relocation in a shared library,
1731 // we'll need a text relocation anyway, so we can skip the
1732 // PLT entry and let the dynamic linker bind the call directly
1733 // to the target. For smaller relocations, we should use a
1734 // PLT entry to ensure that the call can reach.
1735 if (!parameters->options().shared()
1736 || r_type != elfcpp::R_386_PC32)
1737 target->make_plt_entry(symtab, layout, gsym);
1738 }
1739 // Make a dynamic relocation if necessary.
1740 int flags = Symbol::NON_PIC_REF;
1741 if (gsym->is_func())
1742 flags |= Symbol::FUNCTION_CALL;
1743 if (gsym->needs_dynamic_reloc(flags))
1744 {
1745 if (gsym->may_need_copy_reloc())
1746 {
1747 target->copy_reloc(symtab, layout, object,
1748 data_shndx, output_section, gsym, reloc);
1749 }
1750 else
1751 {
1752 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1753 rel_dyn->add_global(gsym, r_type, output_section, object,
1754 data_shndx, reloc.get_r_offset());
1755 }
1756 }
1757 }
1758 break;
1759
1760 case elfcpp::R_386_GOT32:
1761 {
1762 // The symbol requires a GOT entry.
1763 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1764 if (gsym->final_value_is_known())
1765 {
1766 // For a STT_GNU_IFUNC symbol we want the PLT address.
1767 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
1768 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1769 else
1770 got->add_global(gsym, GOT_TYPE_STANDARD);
1771 }
1772 else
1773 {
1774 // If this symbol is not fully resolved, we need to add a
1775 // GOT entry with a dynamic relocation.
1776 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1777 if (gsym->is_from_dynobj()
1778 || gsym->is_undefined()
1779 || gsym->is_preemptible()
1780 || (gsym->type() == elfcpp::STT_GNU_IFUNC
1781 && parameters->options().output_is_position_independent()))
1782 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1783 rel_dyn, elfcpp::R_386_GLOB_DAT);
1784 else
1785 {
1786 // For a STT_GNU_IFUNC symbol we want to write the PLT
1787 // offset into the GOT, so that function pointer
1788 // comparisons work correctly.
1789 bool is_new;
1790 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
1791 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
1792 else
1793 {
1794 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
1795 // Tell the dynamic linker to use the PLT address
1796 // when resolving relocations.
1797 if (gsym->is_from_dynobj()
1798 && !parameters->options().shared())
1799 gsym->set_needs_dynsym_value();
1800 }
1801 if (is_new)
1802 {
1803 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
1804 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1805 got, got_off);
1806 }
1807 }
1808 }
1809 }
1810 break;
1811
1812 case elfcpp::R_386_PLT32:
1813 // If the symbol is fully resolved, this is just a PC32 reloc.
1814 // Otherwise we need a PLT entry.
1815 if (gsym->final_value_is_known())
1816 break;
1817 // If building a shared library, we can also skip the PLT entry
1818 // if the symbol is defined in the output file and is protected
1819 // or hidden.
1820 if (gsym->is_defined()
1821 && !gsym->is_from_dynobj()
1822 && !gsym->is_preemptible())
1823 break;
1824 target->make_plt_entry(symtab, layout, gsym);
1825 break;
1826
1827 case elfcpp::R_386_GOTOFF:
1828 case elfcpp::R_386_GOTPC:
1829 // We need a GOT section.
1830 target->got_section(symtab, layout);
1831 break;
1832
1833 // These are relocations which should only be seen by the
1834 // dynamic linker, and should never be seen here.
1835 case elfcpp::R_386_COPY:
1836 case elfcpp::R_386_GLOB_DAT:
1837 case elfcpp::R_386_JUMP_SLOT:
1838 case elfcpp::R_386_RELATIVE:
1839 case elfcpp::R_386_IRELATIVE:
1840 case elfcpp::R_386_TLS_TPOFF:
1841 case elfcpp::R_386_TLS_DTPMOD32:
1842 case elfcpp::R_386_TLS_DTPOFF32:
1843 case elfcpp::R_386_TLS_TPOFF32:
1844 case elfcpp::R_386_TLS_DESC:
1845 gold_error(_("%s: unexpected reloc %u in object file"),
1846 object->name().c_str(), r_type);
1847 break;
1848
1849 // These are initial tls relocs, which are expected when
1850 // linking.
1851 case elfcpp::R_386_TLS_GD: // Global-dynamic
1852 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1853 case elfcpp::R_386_TLS_DESC_CALL:
1854 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1855 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1856 case elfcpp::R_386_TLS_IE: // Initial-exec
1857 case elfcpp::R_386_TLS_IE_32:
1858 case elfcpp::R_386_TLS_GOTIE:
1859 case elfcpp::R_386_TLS_LE: // Local-exec
1860 case elfcpp::R_386_TLS_LE_32:
1861 {
1862 const bool is_final = gsym->final_value_is_known();
1863 const tls::Tls_optimization optimized_type
1864 = Target_i386::optimize_tls_reloc(is_final, r_type);
1865 switch (r_type)
1866 {
1867 case elfcpp::R_386_TLS_GD: // Global-dynamic
1868 if (optimized_type == tls::TLSOPT_NONE)
1869 {
1870 // Create a pair of GOT entries for the module index and
1871 // dtv-relative offset.
1872 Output_data_got<32, false>* got
1873 = target->got_section(symtab, layout);
1874 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
1875 target->rel_dyn_section(layout),
1876 elfcpp::R_386_TLS_DTPMOD32,
1877 elfcpp::R_386_TLS_DTPOFF32);
1878 }
1879 else if (optimized_type == tls::TLSOPT_TO_IE)
1880 {
1881 // Create a GOT entry for the tp-relative offset.
1882 Output_data_got<32, false>* got
1883 = target->got_section(symtab, layout);
1884 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1885 target->rel_dyn_section(layout),
1886 elfcpp::R_386_TLS_TPOFF);
1887 }
1888 else if (optimized_type != tls::TLSOPT_TO_LE)
1889 unsupported_reloc_global(object, r_type, gsym);
1890 break;
1891
1892 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
1893 target->define_tls_base_symbol(symtab, layout);
1894 if (optimized_type == tls::TLSOPT_NONE)
1895 {
1896 // Create a double GOT entry with an R_386_TLS_DESC
1897 // reloc. The R_386_TLS_DESC reloc is resolved
1898 // lazily, so the GOT entry needs to be in an area in
1899 // .got.plt, not .got. Call got_section to make sure
1900 // the section has been created.
1901 target->got_section(symtab, layout);
1902 Output_data_got<32, false>* got = target->got_tlsdesc_section();
1903 Reloc_section* rt = target->rel_tls_desc_section(layout);
1904 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
1905 elfcpp::R_386_TLS_DESC, 0);
1906 }
1907 else if (optimized_type == tls::TLSOPT_TO_IE)
1908 {
1909 // Create a GOT entry for the tp-relative offset.
1910 Output_data_got<32, false>* got
1911 = target->got_section(symtab, layout);
1912 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1913 target->rel_dyn_section(layout),
1914 elfcpp::R_386_TLS_TPOFF);
1915 }
1916 else if (optimized_type != tls::TLSOPT_TO_LE)
1917 unsupported_reloc_global(object, r_type, gsym);
1918 break;
1919
1920 case elfcpp::R_386_TLS_DESC_CALL:
1921 break;
1922
1923 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1924 if (optimized_type == tls::TLSOPT_NONE)
1925 {
1926 // Create a GOT entry for the module index.
1927 target->got_mod_index_entry(symtab, layout, object);
1928 }
1929 else if (optimized_type != tls::TLSOPT_TO_LE)
1930 unsupported_reloc_global(object, r_type, gsym);
1931 break;
1932
1933 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1934 break;
1935
1936 case elfcpp::R_386_TLS_IE: // Initial-exec
1937 case elfcpp::R_386_TLS_IE_32:
1938 case elfcpp::R_386_TLS_GOTIE:
1939 layout->set_has_static_tls();
1940 if (optimized_type == tls::TLSOPT_NONE)
1941 {
1942 // For the R_386_TLS_IE relocation, we need to create a
1943 // dynamic relocation when building a shared library.
1944 if (r_type == elfcpp::R_386_TLS_IE
1945 && parameters->options().shared())
1946 {
1947 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1948 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1949 output_section, object,
1950 data_shndx,
1951 reloc.get_r_offset());
1952 }
1953 // Create a GOT entry for the tp-relative offset.
1954 Output_data_got<32, false>* got
1955 = target->got_section(symtab, layout);
1956 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1957 ? elfcpp::R_386_TLS_TPOFF32
1958 : elfcpp::R_386_TLS_TPOFF);
1959 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1960 ? GOT_TYPE_TLS_OFFSET
1961 : GOT_TYPE_TLS_NOFFSET);
1962 got->add_global_with_rel(gsym, got_type,
1963 target->rel_dyn_section(layout),
1964 dyn_r_type);
1965 }
1966 else if (optimized_type != tls::TLSOPT_TO_LE)
1967 unsupported_reloc_global(object, r_type, gsym);
1968 break;
1969
1970 case elfcpp::R_386_TLS_LE: // Local-exec
1971 case elfcpp::R_386_TLS_LE_32:
1972 layout->set_has_static_tls();
1973 if (parameters->options().shared())
1974 {
1975 // We need to create a dynamic relocation.
1976 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1977 ? elfcpp::R_386_TLS_TPOFF32
1978 : elfcpp::R_386_TLS_TPOFF);
1979 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1980 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1981 data_shndx, reloc.get_r_offset());
1982 }
1983 break;
1984
1985 default:
1986 gold_unreachable();
1987 }
1988 }
1989 break;
1990
1991 case elfcpp::R_386_32PLT:
1992 case elfcpp::R_386_TLS_GD_32:
1993 case elfcpp::R_386_TLS_GD_PUSH:
1994 case elfcpp::R_386_TLS_GD_CALL:
1995 case elfcpp::R_386_TLS_GD_POP:
1996 case elfcpp::R_386_TLS_LDM_32:
1997 case elfcpp::R_386_TLS_LDM_PUSH:
1998 case elfcpp::R_386_TLS_LDM_CALL:
1999 case elfcpp::R_386_TLS_LDM_POP:
2000 case elfcpp::R_386_USED_BY_INTEL_200:
2001 default:
2002 unsupported_reloc_global(object, r_type, gsym);
2003 break;
2004 }
2005 }
2006
2007 // Process relocations for gc.
2008
2009 void
2010 Target_i386::gc_process_relocs(Symbol_table* symtab,
2011 Layout* layout,
2012 Sized_relobj<32, false>* object,
2013 unsigned int data_shndx,
2014 unsigned int,
2015 const unsigned char* prelocs,
2016 size_t reloc_count,
2017 Output_section* output_section,
2018 bool needs_special_offset_handling,
2019 size_t local_symbol_count,
2020 const unsigned char* plocal_symbols)
2021 {
2022 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2023 Target_i386::Scan,
2024 Target_i386::Relocatable_size_for_reloc>(
2025 symtab,
2026 layout,
2027 this,
2028 object,
2029 data_shndx,
2030 prelocs,
2031 reloc_count,
2032 output_section,
2033 needs_special_offset_handling,
2034 local_symbol_count,
2035 plocal_symbols);
2036 }
2037
2038 // Scan relocations for a section.
2039
2040 void
2041 Target_i386::scan_relocs(Symbol_table* symtab,
2042 Layout* layout,
2043 Sized_relobj<32, false>* object,
2044 unsigned int data_shndx,
2045 unsigned int sh_type,
2046 const unsigned char* prelocs,
2047 size_t reloc_count,
2048 Output_section* output_section,
2049 bool needs_special_offset_handling,
2050 size_t local_symbol_count,
2051 const unsigned char* plocal_symbols)
2052 {
2053 if (sh_type == elfcpp::SHT_RELA)
2054 {
2055 gold_error(_("%s: unsupported RELA reloc section"),
2056 object->name().c_str());
2057 return;
2058 }
2059
2060 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2061 Target_i386::Scan>(
2062 symtab,
2063 layout,
2064 this,
2065 object,
2066 data_shndx,
2067 prelocs,
2068 reloc_count,
2069 output_section,
2070 needs_special_offset_handling,
2071 local_symbol_count,
2072 plocal_symbols);
2073 }
2074
2075 // Finalize the sections.
2076
2077 void
2078 Target_i386::do_finalize_sections(
2079 Layout* layout,
2080 const Input_objects*,
2081 Symbol_table* symtab)
2082 {
2083 const Reloc_section* rel_plt = (this->plt_ == NULL
2084 ? NULL
2085 : this->plt_->rel_plt());
2086 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
2087 this->rel_dyn_, true, false);
2088
2089 // Emit any relocs we saved in an attempt to avoid generating COPY
2090 // relocs.
2091 if (this->copy_relocs_.any_saved_relocs())
2092 this->copy_relocs_.emit(this->rel_dyn_section(layout));
2093
2094 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2095 // the .got.plt section.
2096 Symbol* sym = this->global_offset_table_;
2097 if (sym != NULL)
2098 {
2099 uint32_t data_size = this->got_plt_->current_data_size();
2100 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2101 }
2102 }
2103
2104 // Return whether a direct absolute static relocation needs to be applied.
2105 // In cases where Scan::local() or Scan::global() has created
2106 // a dynamic relocation other than R_386_RELATIVE, the addend
2107 // of the relocation is carried in the data, and we must not
2108 // apply the static relocation.
2109
2110 inline bool
2111 Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
2112 int ref_flags,
2113 bool is_32bit,
2114 Output_section* output_section)
2115 {
2116 // If the output section is not allocated, then we didn't call
2117 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2118 // the reloc here.
2119 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2120 return true;
2121
2122 // For local symbols, we will have created a non-RELATIVE dynamic
2123 // relocation only if (a) the output is position independent,
2124 // (b) the relocation is absolute (not pc- or segment-relative), and
2125 // (c) the relocation is not 32 bits wide.
2126 if (gsym == NULL)
2127 return !(parameters->options().output_is_position_independent()
2128 && (ref_flags & Symbol::ABSOLUTE_REF)
2129 && !is_32bit);
2130
2131 // For global symbols, we use the same helper routines used in the
2132 // scan pass. If we did not create a dynamic relocation, or if we
2133 // created a RELATIVE dynamic relocation, we should apply the static
2134 // relocation.
2135 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2136 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2137 && gsym->can_use_relative_reloc(ref_flags
2138 & Symbol::FUNCTION_CALL);
2139 return !has_dyn || is_rel;
2140 }
2141
2142 // Perform a relocation.
2143
2144 inline bool
2145 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
2146 Target_i386* target,
2147 Output_section* output_section,
2148 size_t relnum,
2149 const elfcpp::Rel<32, false>& rel,
2150 unsigned int r_type,
2151 const Sized_symbol<32>* gsym,
2152 const Symbol_value<32>* psymval,
2153 unsigned char* view,
2154 elfcpp::Elf_types<32>::Elf_Addr address,
2155 section_size_type view_size)
2156 {
2157 if (this->skip_call_tls_get_addr_)
2158 {
2159 if ((r_type != elfcpp::R_386_PLT32
2160 && r_type != elfcpp::R_386_PC32)
2161 || gsym == NULL
2162 || strcmp(gsym->name(), "___tls_get_addr") != 0)
2163 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2164 _("missing expected TLS relocation"));
2165 else
2166 {
2167 this->skip_call_tls_get_addr_ = false;
2168 return false;
2169 }
2170 }
2171
2172 const Sized_relobj<32, false>* object = relinfo->object;
2173
2174 // Pick the value to use for symbols defined in shared objects.
2175 Symbol_value<32> symval;
2176 if (gsym != NULL
2177 && gsym->type() == elfcpp::STT_GNU_IFUNC
2178 && r_type == elfcpp::R_386_32
2179 && gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF)
2180 && gsym->can_use_relative_reloc(false)
2181 && !gsym->is_from_dynobj()
2182 && !gsym->is_undefined()
2183 && !gsym->is_preemptible())
2184 {
2185 // In this case we are generating a R_386_IRELATIVE reloc. We
2186 // want to use the real value of the symbol, not the PLT offset.
2187 }
2188 else if (gsym != NULL
2189 && gsym->use_plt_offset(r_type == elfcpp::R_386_PC8
2190 || r_type == elfcpp::R_386_PC16
2191 || r_type == elfcpp::R_386_PC32))
2192 {
2193 symval.set_output_value(target->plt_section()->address()
2194 + gsym->plt_offset());
2195 psymval = &symval;
2196 }
2197 else if (gsym == NULL && psymval->is_ifunc_symbol())
2198 {
2199 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2200 if (object->local_has_plt_offset(r_sym))
2201 {
2202 symval.set_output_value(target->plt_section()->address()
2203 + object->local_plt_offset(r_sym));
2204 psymval = &symval;
2205 }
2206 }
2207
2208 // Get the GOT offset if needed.
2209 // The GOT pointer points to the end of the GOT section.
2210 // We need to subtract the size of the GOT section to get
2211 // the actual offset to use in the relocation.
2212 bool have_got_offset = false;
2213 unsigned int got_offset = 0;
2214 switch (r_type)
2215 {
2216 case elfcpp::R_386_GOT32:
2217 if (gsym != NULL)
2218 {
2219 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2220 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2221 - target->got_size());
2222 }
2223 else
2224 {
2225 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2226 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2227 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2228 - target->got_size());
2229 }
2230 have_got_offset = true;
2231 break;
2232
2233 default:
2234 break;
2235 }
2236
2237 switch (r_type)
2238 {
2239 case elfcpp::R_386_NONE:
2240 case elfcpp::R_386_GNU_VTINHERIT:
2241 case elfcpp::R_386_GNU_VTENTRY:
2242 break;
2243
2244 case elfcpp::R_386_32:
2245 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2246 output_section))
2247 Relocate_functions<32, false>::rel32(view, object, psymval);
2248 break;
2249
2250 case elfcpp::R_386_PC32:
2251 {
2252 int ref_flags = Symbol::NON_PIC_REF;
2253 if (gsym != NULL && gsym->is_func())
2254 ref_flags |= Symbol::FUNCTION_CALL;
2255 if (should_apply_static_reloc(gsym, ref_flags, true, output_section))
2256 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2257 }
2258 break;
2259
2260 case elfcpp::R_386_16:
2261 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2262 output_section))
2263 Relocate_functions<32, false>::rel16(view, object, psymval);
2264 break;
2265
2266 case elfcpp::R_386_PC16:
2267 {
2268 int ref_flags = Symbol::NON_PIC_REF;
2269 if (gsym != NULL && gsym->is_func())
2270 ref_flags |= Symbol::FUNCTION_CALL;
2271 if (should_apply_static_reloc(gsym, ref_flags, false, output_section))
2272 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
2273 }
2274 break;
2275
2276 case elfcpp::R_386_8:
2277 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2278 output_section))
2279 Relocate_functions<32, false>::rel8(view, object, psymval);
2280 break;
2281
2282 case elfcpp::R_386_PC8:
2283 {
2284 int ref_flags = Symbol::NON_PIC_REF;
2285 if (gsym != NULL && gsym->is_func())
2286 ref_flags |= Symbol::FUNCTION_CALL;
2287 if (should_apply_static_reloc(gsym, ref_flags, false,
2288 output_section))
2289 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
2290 }
2291 break;
2292
2293 case elfcpp::R_386_PLT32:
2294 gold_assert(gsym == NULL
2295 || gsym->has_plt_offset()
2296 || gsym->final_value_is_known()
2297 || (gsym->is_defined()
2298 && !gsym->is_from_dynobj()
2299 && !gsym->is_preemptible()));
2300 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
2301 break;
2302
2303 case elfcpp::R_386_GOT32:
2304 gold_assert(have_got_offset);
2305 Relocate_functions<32, false>::rel32(view, got_offset);
2306 break;
2307
2308 case elfcpp::R_386_GOTOFF:
2309 {
2310 elfcpp::Elf_types<32>::Elf_Addr value;
2311 value = (psymval->value(object, 0)
2312 - target->got_plt_section()->address());
2313 Relocate_functions<32, false>::rel32(view, value);
2314 }
2315 break;
2316
2317 case elfcpp::R_386_GOTPC:
2318 {
2319 elfcpp::Elf_types<32>::Elf_Addr value;
2320 value = target->got_plt_section()->address();
2321 Relocate_functions<32, false>::pcrel32(view, value, address);
2322 }
2323 break;
2324
2325 case elfcpp::R_386_COPY:
2326 case elfcpp::R_386_GLOB_DAT:
2327 case elfcpp::R_386_JUMP_SLOT:
2328 case elfcpp::R_386_RELATIVE:
2329 case elfcpp::R_386_IRELATIVE:
2330 // These are outstanding tls relocs, which are unexpected when
2331 // linking.
2332 case elfcpp::R_386_TLS_TPOFF:
2333 case elfcpp::R_386_TLS_DTPMOD32:
2334 case elfcpp::R_386_TLS_DTPOFF32:
2335 case elfcpp::R_386_TLS_TPOFF32:
2336 case elfcpp::R_386_TLS_DESC:
2337 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2338 _("unexpected reloc %u in object file"),
2339 r_type);
2340 break;
2341
2342 // These are initial tls relocs, which are expected when
2343 // linking.
2344 case elfcpp::R_386_TLS_GD: // Global-dynamic
2345 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2346 case elfcpp::R_386_TLS_DESC_CALL:
2347 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2348 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2349 case elfcpp::R_386_TLS_IE: // Initial-exec
2350 case elfcpp::R_386_TLS_IE_32:
2351 case elfcpp::R_386_TLS_GOTIE:
2352 case elfcpp::R_386_TLS_LE: // Local-exec
2353 case elfcpp::R_386_TLS_LE_32:
2354 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2355 view, address, view_size);
2356 break;
2357
2358 case elfcpp::R_386_32PLT:
2359 case elfcpp::R_386_TLS_GD_32:
2360 case elfcpp::R_386_TLS_GD_PUSH:
2361 case elfcpp::R_386_TLS_GD_CALL:
2362 case elfcpp::R_386_TLS_GD_POP:
2363 case elfcpp::R_386_TLS_LDM_32:
2364 case elfcpp::R_386_TLS_LDM_PUSH:
2365 case elfcpp::R_386_TLS_LDM_CALL:
2366 case elfcpp::R_386_TLS_LDM_POP:
2367 case elfcpp::R_386_USED_BY_INTEL_200:
2368 default:
2369 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2370 _("unsupported reloc %u"),
2371 r_type);
2372 break;
2373 }
2374
2375 return true;
2376 }
2377
2378 // Perform a TLS relocation.
2379
2380 inline void
2381 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
2382 Target_i386* target,
2383 size_t relnum,
2384 const elfcpp::Rel<32, false>& rel,
2385 unsigned int r_type,
2386 const Sized_symbol<32>* gsym,
2387 const Symbol_value<32>* psymval,
2388 unsigned char* view,
2389 elfcpp::Elf_types<32>::Elf_Addr,
2390 section_size_type view_size)
2391 {
2392 Output_segment* tls_segment = relinfo->layout->tls_segment();
2393
2394 const Sized_relobj<32, false>* object = relinfo->object;
2395
2396 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
2397
2398 const bool is_final = (gsym == NULL
2399 ? !parameters->options().shared()
2400 : gsym->final_value_is_known());
2401 const tls::Tls_optimization optimized_type
2402 = Target_i386::optimize_tls_reloc(is_final, r_type);
2403 switch (r_type)
2404 {
2405 case elfcpp::R_386_TLS_GD: // Global-dynamic
2406 if (optimized_type == tls::TLSOPT_TO_LE)
2407 {
2408 gold_assert(tls_segment != NULL);
2409 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2410 rel, r_type, value, view,
2411 view_size);
2412 break;
2413 }
2414 else
2415 {
2416 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2417 ? GOT_TYPE_TLS_NOFFSET
2418 : GOT_TYPE_TLS_PAIR);
2419 unsigned int got_offset;
2420 if (gsym != NULL)
2421 {
2422 gold_assert(gsym->has_got_offset(got_type));
2423 got_offset = gsym->got_offset(got_type) - target->got_size();
2424 }
2425 else
2426 {
2427 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2428 gold_assert(object->local_has_got_offset(r_sym, got_type));
2429 got_offset = (object->local_got_offset(r_sym, got_type)
2430 - target->got_size());
2431 }
2432 if (optimized_type == tls::TLSOPT_TO_IE)
2433 {
2434 gold_assert(tls_segment != NULL);
2435 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2436 got_offset, view, view_size);
2437 break;
2438 }
2439 else if (optimized_type == tls::TLSOPT_NONE)
2440 {
2441 // Relocate the field with the offset of the pair of GOT
2442 // entries.
2443 Relocate_functions<32, false>::rel32(view, got_offset);
2444 break;
2445 }
2446 }
2447 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2448 _("unsupported reloc %u"),
2449 r_type);
2450 break;
2451
2452 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2453 case elfcpp::R_386_TLS_DESC_CALL:
2454 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2455 if (optimized_type == tls::TLSOPT_TO_LE)
2456 {
2457 gold_assert(tls_segment != NULL);
2458 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2459 rel, r_type, value, view,
2460 view_size);
2461 break;
2462 }
2463 else
2464 {
2465 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2466 ? GOT_TYPE_TLS_NOFFSET
2467 : GOT_TYPE_TLS_DESC);
2468 unsigned int got_offset = 0;
2469 if (r_type == elfcpp::R_386_TLS_GOTDESC
2470 && optimized_type == tls::TLSOPT_NONE)
2471 {
2472 // We created GOT entries in the .got.tlsdesc portion of
2473 // the .got.plt section, but the offset stored in the
2474 // symbol is the offset within .got.tlsdesc.
2475 got_offset = (target->got_size()
2476 + target->got_plt_section()->data_size());
2477 }
2478 if (gsym != NULL)
2479 {
2480 gold_assert(gsym->has_got_offset(got_type));
2481 got_offset += gsym->got_offset(got_type) - target->got_size();
2482 }
2483 else
2484 {
2485 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2486 gold_assert(object->local_has_got_offset(r_sym, got_type));
2487 got_offset += (object->local_got_offset(r_sym, got_type)
2488 - target->got_size());
2489 }
2490 if (optimized_type == tls::TLSOPT_TO_IE)
2491 {
2492 gold_assert(tls_segment != NULL);
2493 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2494 got_offset, view, view_size);
2495 break;
2496 }
2497 else if (optimized_type == tls::TLSOPT_NONE)
2498 {
2499 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2500 {
2501 // Relocate the field with the offset of the pair of GOT
2502 // entries.
2503 Relocate_functions<32, false>::rel32(view, got_offset);
2504 }
2505 break;
2506 }
2507 }
2508 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2509 _("unsupported reloc %u"),
2510 r_type);
2511 break;
2512
2513 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2514 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2515 {
2516 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2517 _("both SUN and GNU model "
2518 "TLS relocations"));
2519 break;
2520 }
2521 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
2522 if (optimized_type == tls::TLSOPT_TO_LE)
2523 {
2524 gold_assert(tls_segment != NULL);
2525 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2526 value, view, view_size);
2527 break;
2528 }
2529 else if (optimized_type == tls::TLSOPT_NONE)
2530 {
2531 // Relocate the field with the offset of the GOT entry for
2532 // the module index.
2533 unsigned int got_offset;
2534 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2535 - target->got_size());
2536 Relocate_functions<32, false>::rel32(view, got_offset);
2537 break;
2538 }
2539 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2540 _("unsupported reloc %u"),
2541 r_type);
2542 break;
2543
2544 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2545 if (optimized_type == tls::TLSOPT_TO_LE)
2546 {
2547 // This reloc can appear in debugging sections, in which
2548 // case we must not convert to local-exec. We decide what
2549 // to do based on whether the section is marked as
2550 // containing executable code. That is what the GNU linker
2551 // does as well.
2552 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2553 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
2554 {
2555 gold_assert(tls_segment != NULL);
2556 value -= tls_segment->memsz();
2557 }
2558 }
2559 Relocate_functions<32, false>::rel32(view, value);
2560 break;
2561
2562 case elfcpp::R_386_TLS_IE: // Initial-exec
2563 case elfcpp::R_386_TLS_GOTIE:
2564 case elfcpp::R_386_TLS_IE_32:
2565 if (optimized_type == tls::TLSOPT_TO_LE)
2566 {
2567 gold_assert(tls_segment != NULL);
2568 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2569 rel, r_type, value, view,
2570 view_size);
2571 break;
2572 }
2573 else if (optimized_type == tls::TLSOPT_NONE)
2574 {
2575 // Relocate the field with the offset of the GOT entry for
2576 // the tp-relative offset of the symbol.
2577 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2578 ? GOT_TYPE_TLS_OFFSET
2579 : GOT_TYPE_TLS_NOFFSET);
2580 unsigned int got_offset;
2581 if (gsym != NULL)
2582 {
2583 gold_assert(gsym->has_got_offset(got_type));
2584 got_offset = gsym->got_offset(got_type);
2585 }
2586 else
2587 {
2588 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2589 gold_assert(object->local_has_got_offset(r_sym, got_type));
2590 got_offset = object->local_got_offset(r_sym, got_type);
2591 }
2592 // For the R_386_TLS_IE relocation, we need to apply the
2593 // absolute address of the GOT entry.
2594 if (r_type == elfcpp::R_386_TLS_IE)
2595 got_offset += target->got_plt_section()->address();
2596 // All GOT offsets are relative to the end of the GOT.
2597 got_offset -= target->got_size();
2598 Relocate_functions<32, false>::rel32(view, got_offset);
2599 break;
2600 }
2601 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2602 _("unsupported reloc %u"),
2603 r_type);
2604 break;
2605
2606 case elfcpp::R_386_TLS_LE: // Local-exec
2607 // If we're creating a shared library, a dynamic relocation will
2608 // have been created for this location, so do not apply it now.
2609 if (!parameters->options().shared())
2610 {
2611 gold_assert(tls_segment != NULL);
2612 value -= tls_segment->memsz();
2613 Relocate_functions<32, false>::rel32(view, value);
2614 }
2615 break;
2616
2617 case elfcpp::R_386_TLS_LE_32:
2618 // If we're creating a shared library, a dynamic relocation will
2619 // have been created for this location, so do not apply it now.
2620 if (!parameters->options().shared())
2621 {
2622 gold_assert(tls_segment != NULL);
2623 value = tls_segment->memsz() - value;
2624 Relocate_functions<32, false>::rel32(view, value);
2625 }
2626 break;
2627 }
2628 }
2629
2630 // Do a relocation in which we convert a TLS General-Dynamic to a
2631 // Local-Exec.
2632
2633 inline void
2634 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2635 size_t relnum,
2636 Output_segment* tls_segment,
2637 const elfcpp::Rel<32, false>& rel,
2638 unsigned int,
2639 elfcpp::Elf_types<32>::Elf_Addr value,
2640 unsigned char* view,
2641 section_size_type view_size)
2642 {
2643 // leal foo(,%reg,1),%eax; call ___tls_get_addr
2644 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2645 // leal foo(%reg),%eax; call ___tls_get_addr
2646 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2647
2648 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2649 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2650
2651 unsigned char op1 = view[-1];
2652 unsigned char op2 = view[-2];
2653
2654 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2655 op2 == 0x8d || op2 == 0x04);
2656 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2657
2658 int roff = 5;
2659
2660 if (op2 == 0x04)
2661 {
2662 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2663 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2664 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2665 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2666 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2667 }
2668 else
2669 {
2670 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2671 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2672 if (rel.get_r_offset() + 9 < view_size
2673 && view[9] == 0x90)
2674 {
2675 // There is a trailing nop. Use the size byte subl.
2676 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2677 roff = 6;
2678 }
2679 else
2680 {
2681 // Use the five byte subl.
2682 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2683 }
2684 }
2685
2686 value = tls_segment->memsz() - value;
2687 Relocate_functions<32, false>::rel32(view + roff, value);
2688
2689 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2690 // We can skip it.
2691 this->skip_call_tls_get_addr_ = true;
2692 }
2693
2694 // Do a relocation in which we convert a TLS General-Dynamic to an
2695 // Initial-Exec.
2696
2697 inline void
2698 Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2699 size_t relnum,
2700 Output_segment*,
2701 const elfcpp::Rel<32, false>& rel,
2702 unsigned int,
2703 elfcpp::Elf_types<32>::Elf_Addr value,
2704 unsigned char* view,
2705 section_size_type view_size)
2706 {
2707 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2708 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2709
2710 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2711 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2712
2713 unsigned char op1 = view[-1];
2714 unsigned char op2 = view[-2];
2715
2716 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2717 op2 == 0x8d || op2 == 0x04);
2718 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2719
2720 int roff = 5;
2721
2722 // FIXME: For now, support only the first (SIB) form.
2723 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
2724
2725 if (op2 == 0x04)
2726 {
2727 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2728 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2729 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2730 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2731 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2732 }
2733 else
2734 {
2735 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2736 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
2737 if (rel.get_r_offset() + 9 < view_size
2738 && view[9] == 0x90)
2739 {
2740 // FIXME: This is not the right instruction sequence.
2741 // There is a trailing nop. Use the size byte subl.
2742 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2743 roff = 6;
2744 }
2745 else
2746 {
2747 // FIXME: This is not the right instruction sequence.
2748 // Use the five byte subl.
2749 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2750 }
2751 }
2752
2753 Relocate_functions<32, false>::rel32(view + roff, value);
2754
2755 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2756 // We can skip it.
2757 this->skip_call_tls_get_addr_ = true;
2758 }
2759
2760 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2761 // General-Dynamic to a Local-Exec.
2762
2763 inline void
2764 Target_i386::Relocate::tls_desc_gd_to_le(
2765 const Relocate_info<32, false>* relinfo,
2766 size_t relnum,
2767 Output_segment* tls_segment,
2768 const elfcpp::Rel<32, false>& rel,
2769 unsigned int r_type,
2770 elfcpp::Elf_types<32>::Elf_Addr value,
2771 unsigned char* view,
2772 section_size_type view_size)
2773 {
2774 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2775 {
2776 // leal foo@TLSDESC(%ebx), %eax
2777 // ==> leal foo@NTPOFF, %eax
2778 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2779 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2780 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2781 view[-2] == 0x8d && view[-1] == 0x83);
2782 view[-1] = 0x05;
2783 value -= tls_segment->memsz();
2784 Relocate_functions<32, false>::rel32(view, value);
2785 }
2786 else
2787 {
2788 // call *foo@TLSCALL(%eax)
2789 // ==> nop; nop
2790 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2791 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2792 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2793 view[0] == 0xff && view[1] == 0x10);
2794 view[0] = 0x66;
2795 view[1] = 0x90;
2796 }
2797 }
2798
2799 // Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2800 // General-Dynamic to an Initial-Exec.
2801
2802 inline void
2803 Target_i386::Relocate::tls_desc_gd_to_ie(
2804 const Relocate_info<32, false>* relinfo,
2805 size_t relnum,
2806 Output_segment*,
2807 const elfcpp::Rel<32, false>& rel,
2808 unsigned int r_type,
2809 elfcpp::Elf_types<32>::Elf_Addr value,
2810 unsigned char* view,
2811 section_size_type view_size)
2812 {
2813 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2814 {
2815 // leal foo@TLSDESC(%ebx), %eax
2816 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2817 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2818 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2819 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2820 view[-2] == 0x8d && view[-1] == 0x83);
2821 view[-2] = 0x8b;
2822 Relocate_functions<32, false>::rel32(view, value);
2823 }
2824 else
2825 {
2826 // call *foo@TLSCALL(%eax)
2827 // ==> nop; nop
2828 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2829 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2830 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2831 view[0] == 0xff && view[1] == 0x10);
2832 view[0] = 0x66;
2833 view[1] = 0x90;
2834 }
2835 }
2836
2837 // Do a relocation in which we convert a TLS Local-Dynamic to a
2838 // Local-Exec.
2839
2840 inline void
2841 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2842 size_t relnum,
2843 Output_segment*,
2844 const elfcpp::Rel<32, false>& rel,
2845 unsigned int,
2846 elfcpp::Elf_types<32>::Elf_Addr,
2847 unsigned char* view,
2848 section_size_type view_size)
2849 {
2850 // leal foo(%reg), %eax; call ___tls_get_addr
2851 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2852
2853 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2854 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2855
2856 // FIXME: Does this test really always pass?
2857 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2858 view[-2] == 0x8d && view[-1] == 0x83);
2859
2860 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2861
2862 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2863
2864 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2865 // We can skip it.
2866 this->skip_call_tls_get_addr_ = true;
2867 }
2868
2869 // Do a relocation in which we convert a TLS Initial-Exec to a
2870 // Local-Exec.
2871
2872 inline void
2873 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2874 size_t relnum,
2875 Output_segment* tls_segment,
2876 const elfcpp::Rel<32, false>& rel,
2877 unsigned int r_type,
2878 elfcpp::Elf_types<32>::Elf_Addr value,
2879 unsigned char* view,
2880 section_size_type view_size)
2881 {
2882 // We have to actually change the instructions, which means that we
2883 // need to examine the opcodes to figure out which instruction we
2884 // are looking at.
2885 if (r_type == elfcpp::R_386_TLS_IE)
2886 {
2887 // movl %gs:XX,%eax ==> movl $YY,%eax
2888 // movl %gs:XX,%reg ==> movl $YY,%reg
2889 // addl %gs:XX,%reg ==> addl $YY,%reg
2890 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2891 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2892
2893 unsigned char op1 = view[-1];
2894 if (op1 == 0xa1)
2895 {
2896 // movl XX,%eax ==> movl $YY,%eax
2897 view[-1] = 0xb8;
2898 }
2899 else
2900 {
2901 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2902
2903 unsigned char op2 = view[-2];
2904 if (op2 == 0x8b)
2905 {
2906 // movl XX,%reg ==> movl $YY,%reg
2907 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2908 (op1 & 0xc7) == 0x05);
2909 view[-2] = 0xc7;
2910 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2911 }
2912 else if (op2 == 0x03)
2913 {
2914 // addl XX,%reg ==> addl $YY,%reg
2915 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2916 (op1 & 0xc7) == 0x05);
2917 view[-2] = 0x81;
2918 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2919 }
2920 else
2921 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2922 }
2923 }
2924 else
2925 {
2926 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2927 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2928 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2929 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2930 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2931
2932 unsigned char op1 = view[-1];
2933 unsigned char op2 = view[-2];
2934 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2935 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2936 if (op2 == 0x8b)
2937 {
2938 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2939 view[-2] = 0xc7;
2940 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2941 }
2942 else if (op2 == 0x2b)
2943 {
2944 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2945 view[-2] = 0x81;
2946 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2947 }
2948 else if (op2 == 0x03)
2949 {
2950 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2951 view[-2] = 0x81;
2952 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2953 }
2954 else
2955 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2956 }
2957
2958 value = tls_segment->memsz() - value;
2959 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2960 value = - value;
2961
2962 Relocate_functions<32, false>::rel32(view, value);
2963 }
2964
2965 // Relocate section data.
2966
2967 void
2968 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
2969 unsigned int sh_type,
2970 const unsigned char* prelocs,
2971 size_t reloc_count,
2972 Output_section* output_section,
2973 bool needs_special_offset_handling,
2974 unsigned char* view,
2975 elfcpp::Elf_types<32>::Elf_Addr address,
2976 section_size_type view_size,
2977 const Reloc_symbol_changes* reloc_symbol_changes)
2978 {
2979 gold_assert(sh_type == elfcpp::SHT_REL);
2980
2981 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2982 Target_i386::Relocate>(
2983 relinfo,
2984 this,
2985 prelocs,
2986 reloc_count,
2987 output_section,
2988 needs_special_offset_handling,
2989 view,
2990 address,
2991 view_size,
2992 reloc_symbol_changes);
2993 }
2994
2995 // Return the size of a relocation while scanning during a relocatable
2996 // link.
2997
2998 unsigned int
2999 Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3000 unsigned int r_type,
3001 Relobj* object)
3002 {
3003 switch (r_type)
3004 {
3005 case elfcpp::R_386_NONE:
3006 case elfcpp::R_386_GNU_VTINHERIT:
3007 case elfcpp::R_386_GNU_VTENTRY:
3008 case elfcpp::R_386_TLS_GD: // Global-dynamic
3009 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3010 case elfcpp::R_386_TLS_DESC_CALL:
3011 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3012 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3013 case elfcpp::R_386_TLS_IE: // Initial-exec
3014 case elfcpp::R_386_TLS_IE_32:
3015 case elfcpp::R_386_TLS_GOTIE:
3016 case elfcpp::R_386_TLS_LE: // Local-exec
3017 case elfcpp::R_386_TLS_LE_32:
3018 return 0;
3019
3020 case elfcpp::R_386_32:
3021 case elfcpp::R_386_PC32:
3022 case elfcpp::R_386_GOT32:
3023 case elfcpp::R_386_PLT32:
3024 case elfcpp::R_386_GOTOFF:
3025 case elfcpp::R_386_GOTPC:
3026 return 4;
3027
3028 case elfcpp::R_386_16:
3029 case elfcpp::R_386_PC16:
3030 return 2;
3031
3032 case elfcpp::R_386_8:
3033 case elfcpp::R_386_PC8:
3034 return 1;
3035
3036 // These are relocations which should only be seen by the
3037 // dynamic linker, and should never be seen here.
3038 case elfcpp::R_386_COPY:
3039 case elfcpp::R_386_GLOB_DAT:
3040 case elfcpp::R_386_JUMP_SLOT:
3041 case elfcpp::R_386_RELATIVE:
3042 case elfcpp::R_386_IRELATIVE:
3043 case elfcpp::R_386_TLS_TPOFF:
3044 case elfcpp::R_386_TLS_DTPMOD32:
3045 case elfcpp::R_386_TLS_DTPOFF32:
3046 case elfcpp::R_386_TLS_TPOFF32:
3047 case elfcpp::R_386_TLS_DESC:
3048 object->error(_("unexpected reloc %u in object file"), r_type);
3049 return 0;
3050
3051 case elfcpp::R_386_32PLT:
3052 case elfcpp::R_386_TLS_GD_32:
3053 case elfcpp::R_386_TLS_GD_PUSH:
3054 case elfcpp::R_386_TLS_GD_CALL:
3055 case elfcpp::R_386_TLS_GD_POP:
3056 case elfcpp::R_386_TLS_LDM_32:
3057 case elfcpp::R_386_TLS_LDM_PUSH:
3058 case elfcpp::R_386_TLS_LDM_CALL:
3059 case elfcpp::R_386_TLS_LDM_POP:
3060 case elfcpp::R_386_USED_BY_INTEL_200:
3061 default:
3062 object->error(_("unsupported reloc %u in object file"), r_type);
3063 return 0;
3064 }
3065 }
3066
3067 // Scan the relocs during a relocatable link.
3068
3069 void
3070 Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
3071 Layout* layout,
3072 Sized_relobj<32, false>* object,
3073 unsigned int data_shndx,
3074 unsigned int sh_type,
3075 const unsigned char* prelocs,
3076 size_t reloc_count,
3077 Output_section* output_section,
3078 bool needs_special_offset_handling,
3079 size_t local_symbol_count,
3080 const unsigned char* plocal_symbols,
3081 Relocatable_relocs* rr)
3082 {
3083 gold_assert(sh_type == elfcpp::SHT_REL);
3084
3085 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3086 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3087
3088 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
3089 Scan_relocatable_relocs>(
3090 symtab,
3091 layout,
3092 object,
3093 data_shndx,
3094 prelocs,
3095 reloc_count,
3096 output_section,
3097 needs_special_offset_handling,
3098 local_symbol_count,
3099 plocal_symbols,
3100 rr);
3101 }
3102
3103 // Relocate a section during a relocatable link.
3104
3105 void
3106 Target_i386::relocate_for_relocatable(
3107 const Relocate_info<32, false>* relinfo,
3108 unsigned int sh_type,
3109 const unsigned char* prelocs,
3110 size_t reloc_count,
3111 Output_section* output_section,
3112 off_t offset_in_output_section,
3113 const Relocatable_relocs* rr,
3114 unsigned char* view,
3115 elfcpp::Elf_types<32>::Elf_Addr view_address,
3116 section_size_type view_size,
3117 unsigned char* reloc_view,
3118 section_size_type reloc_view_size)
3119 {
3120 gold_assert(sh_type == elfcpp::SHT_REL);
3121
3122 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
3123 relinfo,
3124 prelocs,
3125 reloc_count,
3126 output_section,
3127 offset_in_output_section,
3128 rr,
3129 view,
3130 view_address,
3131 view_size,
3132 reloc_view,
3133 reloc_view_size);
3134 }
3135
3136 // Return the value to use for a dynamic which requires special
3137 // treatment. This is how we support equality comparisons of function
3138 // pointers across shared library boundaries, as described in the
3139 // processor specific ABI supplement.
3140
3141 uint64_t
3142 Target_i386::do_dynsym_value(const Symbol* gsym) const
3143 {
3144 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
3145 return this->plt_section()->address() + gsym->plt_offset();
3146 }
3147
3148 // Return a string used to fill a code section with nops to take up
3149 // the specified length.
3150
3151 std::string
3152 Target_i386::do_code_fill(section_size_type length) const
3153 {
3154 if (length >= 16)
3155 {
3156 // Build a jmp instruction to skip over the bytes.
3157 unsigned char jmp[5];
3158 jmp[0] = 0xe9;
3159 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3160 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3161 + std::string(length - 5, '\0'));
3162 }
3163
3164 // Nop sequences of various lengths.
3165 const char nop1[1] = { 0x90 }; // nop
3166 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3167 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3168 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3169 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3170 0x00 }; // leal 0(%esi,1),%esi
3171 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3172 0x00, 0x00 };
3173 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3174 0x00, 0x00, 0x00 };
3175 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3176 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3177 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3178 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3179 0x00 };
3180 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3181 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3182 0x00, 0x00 };
3183 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3184 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3185 0x00, 0x00, 0x00 };
3186 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3187 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3188 0x00, 0x00, 0x00, 0x00 };
3189 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3190 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3191 0x27, 0x00, 0x00, 0x00,
3192 0x00 };
3193 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3194 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3195 0xbc, 0x27, 0x00, 0x00,
3196 0x00, 0x00 };
3197 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3198 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3199 0x90, 0x90, 0x90, 0x90,
3200 0x90, 0x90, 0x90 };
3201
3202 const char* nops[16] = {
3203 NULL,
3204 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3205 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3206 };
3207
3208 return std::string(nops[length], length);
3209 }
3210
3211 // Return whether SYM should be treated as a call to a non-split
3212 // function. We don't want that to be true of a call to a
3213 // get_pc_thunk function.
3214
3215 bool
3216 Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3217 {
3218 return (sym->type() == elfcpp::STT_FUNC
3219 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3220 }
3221
3222 // FNOFFSET in section SHNDX in OBJECT is the start of a function
3223 // compiled with -fstack-split. The function calls non-stack-split
3224 // code. We have to change the function so that it always ensures
3225 // that it has enough stack space to run some random function.
3226
3227 void
3228 Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3229 section_offset_type fnoffset,
3230 section_size_type fnsize,
3231 unsigned char* view,
3232 section_size_type view_size,
3233 std::string* from,
3234 std::string* to) const
3235 {
3236 // The function starts with a comparison of the stack pointer and a
3237 // field in the TCB. This is followed by a jump.
3238
3239 // cmp %gs:NN,%esp
3240 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3241 && fnsize > 7)
3242 {
3243 // We will call __morestack if the carry flag is set after this
3244 // comparison. We turn the comparison into an stc instruction
3245 // and some nops.
3246 view[fnoffset] = '\xf9';
3247 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3248 }
3249 // lea NN(%esp),%ecx
3250 // lea NN(%esp),%edx
3251 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3252 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
3253 && fnsize > 7)
3254 {
3255 // This is loading an offset from the stack pointer for a
3256 // comparison. The offset is negative, so we decrease the
3257 // offset by the amount of space we need for the stack. This
3258 // means we will avoid calling __morestack if there happens to
3259 // be plenty of space on the stack already.
3260 unsigned char* pval = view + fnoffset + 3;
3261 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3262 val -= parameters->options().split_stack_adjust_size();
3263 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3264 }
3265 else
3266 {
3267 if (!object->has_no_split_stack())
3268 object->error(_("failed to match split-stack sequence at "
3269 "section %u offset %0zx"),
3270 shndx, static_cast<size_t>(fnoffset));
3271 return;
3272 }
3273
3274 // We have to change the function so that it calls
3275 // __morestack_non_split instead of __morestack. The former will
3276 // allocate additional stack space.
3277 *from = "__morestack";
3278 *to = "__morestack_non_split";
3279 }
3280
3281 // The selector for i386 object files.
3282
3283 class Target_selector_i386 : public Target_selector_freebsd
3284 {
3285 public:
3286 Target_selector_i386()
3287 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
3288 "elf32-i386", "elf32-i386-freebsd")
3289 { }
3290
3291 Target*
3292 do_instantiate_target()
3293 { return new Target_i386(); }
3294 };
3295
3296 Target_selector_i386 target_selector_i386;
3297
3298 } // End anonymous namespace.
This page took 0.119165 seconds and 5 git commands to generate.