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