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