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