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