Fix soname for library found in search path.
[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
49 class Target_i386 : public Sized_target<32, false>
50 {
51 public:
52 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
53
54 Target_i386()
55 : Sized_target<32, false>(&i386_info),
56 got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
57 copy_relocs_(NULL), dynbss_(NULL)
58 { }
59
60 // Scan the relocations to look for symbol adjustments.
61 void
62 scan_relocs(const General_options& options,
63 Symbol_table* symtab,
64 Layout* layout,
65 Sized_relobj<32, false>* object,
66 unsigned int data_shndx,
67 unsigned int sh_type,
68 const unsigned char* prelocs,
69 size_t reloc_count,
70 size_t local_symbol_count,
71 const unsigned char* plocal_symbols,
72 Symbol** global_symbols);
73
74 // Finalize the sections.
75 void
76 do_finalize_sections(Layout*);
77
78 // Return the value to use for a dynamic which requires special
79 // treatment.
80 uint64_t
81 do_dynsym_value(const Symbol*) const;
82
83 // Relocate a section.
84 void
85 relocate_section(const Relocate_info<32, false>*,
86 unsigned int sh_type,
87 const unsigned char* prelocs,
88 size_t reloc_count,
89 unsigned char* view,
90 elfcpp::Elf_types<32>::Elf_Addr view_address,
91 off_t view_size);
92
93 // Return a string used to fill a code section with nops.
94 std::string
95 do_code_fill(off_t length);
96
97 private:
98 // The class which scans relocations.
99 struct Scan
100 {
101 inline void
102 local(const General_options& options, Symbol_table* symtab,
103 Layout* layout, Target_i386* target,
104 Sized_relobj<32, false>* object,
105 unsigned int data_shndx,
106 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
107 const elfcpp::Sym<32, false>& lsym);
108
109 inline void
110 global(const General_options& options, Symbol_table* symtab,
111 Layout* layout, Target_i386* target,
112 Sized_relobj<32, false>* object,
113 unsigned int data_shndx,
114 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
115 Symbol* gsym);
116
117 static void
118 unsupported_reloc_local(Sized_relobj<32, false>*, unsigned int r_type);
119
120 static void
121 unsupported_reloc_global(Sized_relobj<32, false>*, unsigned int r_type,
122 Symbol*);
123 };
124
125 // The class which implements relocation.
126 class Relocate
127 {
128 public:
129 Relocate()
130 : skip_call_tls_get_addr_(false),
131 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
132 { }
133
134 ~Relocate()
135 {
136 if (this->skip_call_tls_get_addr_)
137 {
138 // FIXME: This needs to specify the location somehow.
139 fprintf(stderr, _("%s: missing expected TLS relocation\n"),
140 program_name);
141 gold_exit(false);
142 }
143 }
144
145 // Do a relocation. Return false if the caller should not issue
146 // any warnings about this relocation.
147 inline bool
148 relocate(const Relocate_info<32, false>*, Target_i386*, size_t relnum,
149 const elfcpp::Rel<32, false>&,
150 unsigned int r_type, const Sized_symbol<32>*,
151 const Symbol_value<32>*,
152 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
153 off_t);
154
155 private:
156 // Do a TLS relocation.
157 inline void
158 relocate_tls(const Relocate_info<32, false>*, size_t relnum,
159 const elfcpp::Rel<32, false>&,
160 unsigned int r_type, const Sized_symbol<32>*,
161 const Symbol_value<32>*,
162 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr, off_t);
163
164 // Do a TLS Initial-Exec to Local-Exec transition.
165 static inline void
166 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
167 Output_segment* tls_segment,
168 const elfcpp::Rel<32, false>&, unsigned int r_type,
169 elfcpp::Elf_types<32>::Elf_Addr value,
170 unsigned char* view,
171 off_t view_size);
172
173 // Do a TLS Global-Dynamic to Local-Exec transition.
174 inline void
175 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
176 Output_segment* tls_segment,
177 const elfcpp::Rel<32, false>&, unsigned int r_type,
178 elfcpp::Elf_types<32>::Elf_Addr value,
179 unsigned char* view,
180 off_t view_size);
181
182 // Do a TLS Local-Dynamic to Local-Exec transition.
183 inline void
184 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
185 Output_segment* tls_segment,
186 const elfcpp::Rel<32, false>&, unsigned int r_type,
187 elfcpp::Elf_types<32>::Elf_Addr value,
188 unsigned char* view,
189 off_t view_size);
190
191 // We need to keep track of which type of local dynamic relocation
192 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
193 enum Local_dynamic_type
194 {
195 LOCAL_DYNAMIC_NONE,
196 LOCAL_DYNAMIC_SUN,
197 LOCAL_DYNAMIC_GNU
198 };
199
200 // This is set if we should skip the next reloc, which should be a
201 // PLT32 reloc against ___tls_get_addr.
202 bool skip_call_tls_get_addr_;
203 // The type of local dynamic relocation we have seen in the section
204 // being relocated, if any.
205 Local_dynamic_type local_dynamic_type_;
206 };
207
208 // Adjust TLS relocation type based on the options and whether this
209 // is a local symbol.
210 static tls::Tls_optimization
211 optimize_tls_reloc(bool is_final, int r_type);
212
213 // Get the GOT section, creating it if necessary.
214 Output_data_got<32, false>*
215 got_section(Symbol_table*, Layout*);
216
217 // Create a PLT entry for a global symbol.
218 void
219 make_plt_entry(Symbol_table*, Layout*, Symbol*);
220
221 // Get the PLT section.
222 const Output_data_plt_i386*
223 plt_section() const
224 {
225 gold_assert(this->plt_ != NULL);
226 return this->plt_;
227 }
228
229 // Get the dynamic reloc section, creating it if necessary.
230 Reloc_section*
231 rel_dyn_section(Layout*);
232
233 // Copy a relocation against a global symbol.
234 void
235 copy_reloc(const General_options*, Symbol_table*, Layout*,
236 Sized_relobj<32, false>*, unsigned int,
237 Symbol*, const elfcpp::Rel<32, false>&);
238
239 // Information about this specific target which we pass to the
240 // general Target structure.
241 static const Target::Target_info i386_info;
242
243 // The GOT section.
244 Output_data_got<32, false>* got_;
245 // The PLT section.
246 Output_data_plt_i386* plt_;
247 // The GOT PLT section.
248 Output_data_space* got_plt_;
249 // The dynamic reloc section.
250 Reloc_section* rel_dyn_;
251 // Relocs saved to avoid a COPY reloc.
252 Copy_relocs<32, false>* copy_relocs_;
253 // Space for variables copied with a COPY reloc.
254 Output_data_space* dynbss_;
255 };
256
257 const Target::Target_info Target_i386::i386_info =
258 {
259 32, // size
260 false, // is_big_endian
261 elfcpp::EM_386, // machine_code
262 false, // has_make_symbol
263 false, // has_resolve
264 true, // has_code_fill
265 "/usr/lib/libc.so.1", // dynamic_linker
266 0x08048000, // text_segment_address
267 0x1000, // abi_pagesize
268 0x1000 // common_pagesize
269 };
270
271 // Get the GOT section, creating it if necessary.
272
273 Output_data_got<32, false>*
274 Target_i386::got_section(Symbol_table* symtab, Layout* layout)
275 {
276 if (this->got_ == NULL)
277 {
278 gold_assert(symtab != NULL && layout != NULL);
279
280 this->got_ = new Output_data_got<32, false>();
281
282 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
283 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
284 this->got_);
285
286 // The old GNU linker creates a .got.plt section. We just
287 // create another set of data in the .got section. Note that we
288 // always create a PLT if we create a GOT, although the PLT
289 // might be empty.
290 this->got_plt_ = new Output_data_space(4);
291 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
292 elfcpp::SHF_ALLOC | elfcpp::SHF_WRITE,
293 this->got_plt_);
294
295 // The first three entries are reserved.
296 this->got_plt_->set_space_size(3 * 4);
297
298 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
299 symtab->define_in_output_data(this, "_GLOBAL_OFFSET_TABLE_", NULL,
300 this->got_plt_,
301 0, 0, elfcpp::STT_OBJECT,
302 elfcpp::STB_LOCAL,
303 elfcpp::STV_HIDDEN, 0,
304 false, false);
305 }
306
307 return this->got_;
308 }
309
310 // Get the dynamic reloc section, creating it if necessary.
311
312 Target_i386::Reloc_section*
313 Target_i386::rel_dyn_section(Layout* layout)
314 {
315 if (this->rel_dyn_ == NULL)
316 {
317 gold_assert(layout != NULL);
318 this->rel_dyn_ = new Reloc_section();
319 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
320 elfcpp::SHF_ALLOC, this->rel_dyn_);
321 }
322 return this->rel_dyn_;
323 }
324
325 // A class to handle the PLT data.
326
327 class Output_data_plt_i386 : public Output_section_data
328 {
329 public:
330 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
331
332 Output_data_plt_i386(Layout*, Output_data_space*);
333
334 // Add an entry to the PLT.
335 void
336 add_entry(Symbol* gsym);
337
338 // Return the .rel.plt section data.
339 const Reloc_section*
340 rel_plt() const
341 { return this->rel_; }
342
343 protected:
344 void
345 do_adjust_output_section(Output_section* os);
346
347 private:
348 // The size of an entry in the PLT.
349 static const int plt_entry_size = 16;
350
351 // The first entry in the PLT for an executable.
352 static unsigned char exec_first_plt_entry[plt_entry_size];
353
354 // The first entry in the PLT for a shared object.
355 static unsigned char dyn_first_plt_entry[plt_entry_size];
356
357 // Other entries in the PLT for an executable.
358 static unsigned char exec_plt_entry[plt_entry_size];
359
360 // Other entries in the PLT for a shared object.
361 static unsigned char dyn_plt_entry[plt_entry_size];
362
363 // Set the final size.
364 void
365 do_set_address(uint64_t, off_t)
366 { this->set_data_size((this->count_ + 1) * plt_entry_size); }
367
368 // Write out the PLT data.
369 void
370 do_write(Output_file*);
371
372 // The reloc section.
373 Reloc_section* rel_;
374 // The .got.plt section.
375 Output_data_space* got_plt_;
376 // The number of PLT entries.
377 unsigned int count_;
378 };
379
380 // Create the PLT section. The ordinary .got section is an argument,
381 // since we need to refer to the start. We also create our own .got
382 // section just for PLT entries.
383
384 Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
385 Output_data_space* got_plt)
386 : Output_section_data(4), got_plt_(got_plt), count_(0)
387 {
388 this->rel_ = new Reloc_section();
389 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
390 elfcpp::SHF_ALLOC, this->rel_);
391 }
392
393 void
394 Output_data_plt_i386::do_adjust_output_section(Output_section* os)
395 {
396 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
397 // linker, and so do we.
398 os->set_entsize(4);
399 }
400
401 // Add an entry to the PLT.
402
403 void
404 Output_data_plt_i386::add_entry(Symbol* gsym)
405 {
406 gold_assert(!gsym->has_plt_offset());
407
408 // Note that when setting the PLT offset we skip the initial
409 // reserved PLT entry.
410 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
411
412 ++this->count_;
413
414 off_t got_offset = this->got_plt_->data_size();
415
416 // Every PLT entry needs a GOT entry which points back to the PLT
417 // entry (this will be changed by the dynamic linker, normally
418 // lazily when the function is called).
419 this->got_plt_->set_space_size(got_offset + 4);
420
421 // Every PLT entry needs a reloc.
422 gsym->set_needs_dynsym_entry();
423 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
424 got_offset);
425
426 // Note that we don't need to save the symbol. The contents of the
427 // PLT are independent of which symbols are used. The symbols only
428 // appear in the relocations.
429 }
430
431 // The first entry in the PLT for an executable.
432
433 unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
434 {
435 0xff, 0x35, // pushl contents of memory address
436 0, 0, 0, 0, // replaced with address of .got + 4
437 0xff, 0x25, // jmp indirect
438 0, 0, 0, 0, // replaced with address of .got + 8
439 0, 0, 0, 0 // unused
440 };
441
442 // The first entry in the PLT for a shared object.
443
444 unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
445 {
446 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
447 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
448 0, 0, 0, 0 // unused
449 };
450
451 // Subsequent entries in the PLT for an executable.
452
453 unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
454 {
455 0xff, 0x25, // jmp indirect
456 0, 0, 0, 0, // replaced with address of symbol in .got
457 0x68, // pushl immediate
458 0, 0, 0, 0, // replaced with offset into relocation table
459 0xe9, // jmp relative
460 0, 0, 0, 0 // replaced with offset to start of .plt
461 };
462
463 // Subsequent entries in the PLT for a shared object.
464
465 unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
466 {
467 0xff, 0xa3, // jmp *offset(%ebx)
468 0, 0, 0, 0, // replaced with offset of symbol in .got
469 0x68, // pushl immediate
470 0, 0, 0, 0, // replaced with offset into relocation table
471 0xe9, // jmp relative
472 0, 0, 0, 0 // replaced with offset to start of .plt
473 };
474
475 // Write out the PLT. This uses the hand-coded instructions above,
476 // and adjusts them as needed. This is all specified by the i386 ELF
477 // Processor Supplement.
478
479 void
480 Output_data_plt_i386::do_write(Output_file* of)
481 {
482 const off_t offset = this->offset();
483 const off_t oview_size = this->data_size();
484 unsigned char* const oview = of->get_output_view(offset, oview_size);
485
486 const off_t got_file_offset = this->got_plt_->offset();
487 const off_t got_size = this->got_plt_->data_size();
488 unsigned char* const got_view = of->get_output_view(got_file_offset,
489 got_size);
490
491 unsigned char* pov = oview;
492
493 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
494 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
495
496 if (parameters->output_is_shared())
497 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
498 else
499 {
500 memcpy(pov, exec_first_plt_entry, plt_entry_size);
501 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
502 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
503 }
504 pov += plt_entry_size;
505
506 unsigned char* got_pov = got_view;
507
508 memset(got_pov, 0, 12);
509 got_pov += 12;
510
511 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
512
513 unsigned int plt_offset = plt_entry_size;
514 unsigned int plt_rel_offset = 0;
515 unsigned int got_offset = 12;
516 const unsigned int count = this->count_;
517 for (unsigned int i = 0;
518 i < count;
519 ++i,
520 pov += plt_entry_size,
521 got_pov += 4,
522 plt_offset += plt_entry_size,
523 plt_rel_offset += rel_size,
524 got_offset += 4)
525 {
526 // Set and adjust the PLT entry itself.
527
528 if (parameters->output_is_shared())
529 {
530 memcpy(pov, dyn_plt_entry, plt_entry_size);
531 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
532 }
533 else
534 {
535 memcpy(pov, exec_plt_entry, plt_entry_size);
536 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
537 (got_address
538 + got_offset));
539 }
540
541 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
542 elfcpp::Swap<32, false>::writeval(pov + 12,
543 - (plt_offset + plt_entry_size));
544
545 // Set the entry in the GOT.
546 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
547 }
548
549 gold_assert(pov - oview == oview_size);
550 gold_assert(got_pov - got_view == got_size);
551
552 of->write_output_view(offset, oview_size, oview);
553 of->write_output_view(got_file_offset, got_size, got_view);
554 }
555
556 // Create a PLT entry for a global symbol.
557
558 void
559 Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
560 {
561 if (gsym->has_plt_offset())
562 return;
563
564 if (this->plt_ == NULL)
565 {
566 // Create the GOT sections first.
567 this->got_section(symtab, layout);
568
569 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_);
570 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
571 (elfcpp::SHF_ALLOC
572 | elfcpp::SHF_EXECINSTR),
573 this->plt_);
574 }
575
576 this->plt_->add_entry(gsym);
577 }
578
579 // Handle a relocation against a non-function symbol defined in a
580 // dynamic object. The traditional way to handle this is to generate
581 // a COPY relocation to copy the variable at runtime from the shared
582 // object into the executable's data segment. However, this is
583 // undesirable in general, as if the size of the object changes in the
584 // dynamic object, the executable will no longer work correctly. If
585 // this relocation is in a writable section, then we can create a
586 // dynamic reloc and the dynamic linker will resolve it to the correct
587 // address at runtime. However, we do not want do that if the
588 // relocation is in a read-only section, as it would prevent the
589 // readonly segment from being shared. And if we have to eventually
590 // generate a COPY reloc, then any dynamic relocations will be
591 // useless. So this means that if this is a writable section, we need
592 // to save the relocation until we see whether we have to create a
593 // COPY relocation for this symbol for any other relocation.
594
595 void
596 Target_i386::copy_reloc(const General_options* options,
597 Symbol_table* symtab,
598 Layout* layout,
599 Sized_relobj<32, false>* object,
600 unsigned int data_shndx, Symbol* gsym,
601 const elfcpp::Rel<32, false>& rel)
602 {
603 Sized_symbol<32>* ssym;
604 ssym = symtab->get_sized_symbol SELECT_SIZE_NAME(32) (gsym
605 SELECT_SIZE(32));
606
607 if (!Copy_relocs<32, false>::need_copy_reloc(options, object,
608 data_shndx, ssym))
609 {
610 // So far we do not need a COPY reloc. Save this relocation.
611 // If it turns out that we never need a COPY reloc for this
612 // symbol, then we will emit the relocation.
613 if (this->copy_relocs_ == NULL)
614 this->copy_relocs_ = new Copy_relocs<32, false>();
615 this->copy_relocs_->save(ssym, object, data_shndx, rel);
616 }
617 else
618 {
619 // Allocate space for this symbol in the .bss section.
620
621 elfcpp::Elf_types<32>::Elf_WXword symsize = ssym->symsize();
622
623 // There is no defined way to determine the required alignment
624 // of the symbol. We pick the alignment based on the size. We
625 // set an arbitrary maximum of 256.
626 unsigned int align;
627 for (align = 1; align < 512; align <<= 1)
628 if ((symsize & align) != 0)
629 break;
630
631 if (this->dynbss_ == NULL)
632 {
633 this->dynbss_ = new Output_data_space(align);
634 layout->add_output_section_data(".bss",
635 elfcpp::SHT_NOBITS,
636 (elfcpp::SHF_ALLOC
637 | elfcpp::SHF_WRITE),
638 this->dynbss_);
639 }
640
641 Output_data_space* dynbss = this->dynbss_;
642
643 if (align > dynbss->addralign())
644 dynbss->set_space_alignment(align);
645
646 off_t dynbss_size = dynbss->data_size();
647 dynbss_size = align_address(dynbss_size, align);
648 off_t offset = dynbss_size;
649 dynbss->set_space_size(dynbss_size + symsize);
650
651 // Define the symbol in the .dynbss section.
652 symtab->define_in_output_data(this, ssym->name(), ssym->version(),
653 dynbss, offset, symsize, ssym->type(),
654 ssym->binding(), ssym->visibility(),
655 ssym->nonvis(), false, false);
656
657 // Add the COPY reloc.
658 ssym->set_needs_dynsym_entry();
659 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
660 rel_dyn->add_global(ssym, elfcpp::R_386_COPY, dynbss, offset);
661 }
662 }
663
664 // Optimize the TLS relocation type based on what we know about the
665 // symbol. IS_FINAL is true if the final address of this symbol is
666 // known at link time.
667
668 tls::Tls_optimization
669 Target_i386::optimize_tls_reloc(bool is_final, int r_type)
670 {
671 // If we are generating a shared library, then we can't do anything
672 // in the linker.
673 if (parameters->output_is_shared())
674 return tls::TLSOPT_NONE;
675
676 switch (r_type)
677 {
678 case elfcpp::R_386_TLS_GD:
679 case elfcpp::R_386_TLS_GOTDESC:
680 case elfcpp::R_386_TLS_DESC_CALL:
681 // These are Global-Dynamic which permits fully general TLS
682 // access. Since we know that we are generating an executable,
683 // we can convert this to Initial-Exec. If we also know that
684 // this is a local symbol, we can further switch to Local-Exec.
685 if (is_final)
686 return tls::TLSOPT_TO_LE;
687 return tls::TLSOPT_TO_IE;
688
689 case elfcpp::R_386_TLS_LDM:
690 // This is Local-Dynamic, which refers to a local symbol in the
691 // dynamic TLS block. Since we know that we generating an
692 // executable, we can switch to Local-Exec.
693 return tls::TLSOPT_TO_LE;
694
695 case elfcpp::R_386_TLS_LDO_32:
696 // Another type of Local-Dynamic relocation.
697 return tls::TLSOPT_TO_LE;
698
699 case elfcpp::R_386_TLS_IE:
700 case elfcpp::R_386_TLS_GOTIE:
701 case elfcpp::R_386_TLS_IE_32:
702 // These are Initial-Exec relocs which get the thread offset
703 // from the GOT. If we know that we are linking against the
704 // local symbol, we can switch to Local-Exec, which links the
705 // thread offset into the instruction.
706 if (is_final)
707 return tls::TLSOPT_TO_LE;
708 return tls::TLSOPT_NONE;
709
710 case elfcpp::R_386_TLS_LE:
711 case elfcpp::R_386_TLS_LE_32:
712 // When we already have Local-Exec, there is nothing further we
713 // can do.
714 return tls::TLSOPT_NONE;
715
716 default:
717 gold_unreachable();
718 }
719 }
720
721 // Report an unsupported relocation against a local symbol.
722
723 void
724 Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
725 unsigned int r_type)
726 {
727 fprintf(stderr, _("%s: %s: unsupported reloc %u against local symbol\n"),
728 program_name, object->name().c_str(), r_type);
729 }
730
731 // Scan a relocation for a local symbol.
732
733 inline void
734 Target_i386::Scan::local(const General_options&,
735 Symbol_table* symtab,
736 Layout* layout,
737 Target_i386* target,
738 Sized_relobj<32, false>* object,
739 unsigned int,
740 const elfcpp::Rel<32, false>&,
741 unsigned int r_type,
742 const elfcpp::Sym<32, false>&)
743 {
744 switch (r_type)
745 {
746 case elfcpp::R_386_NONE:
747 case elfcpp::R_386_GNU_VTINHERIT:
748 case elfcpp::R_386_GNU_VTENTRY:
749 break;
750
751 case elfcpp::R_386_32:
752 case elfcpp::R_386_16:
753 case elfcpp::R_386_8:
754 // FIXME: If we are generating a shared object we need to copy
755 // this relocation into the object.
756 gold_assert(!parameters->output_is_shared());
757 break;
758
759 case elfcpp::R_386_PC32:
760 case elfcpp::R_386_PC16:
761 case elfcpp::R_386_PC8:
762 break;
763
764 case elfcpp::R_386_GOTOFF:
765 case elfcpp::R_386_GOTPC:
766 // We need a GOT section.
767 target->got_section(symtab, layout);
768 break;
769
770 // These are relocations which should only be seen by the
771 // dynamic linker, and should never be seen here.
772 case elfcpp::R_386_COPY:
773 case elfcpp::R_386_GLOB_DAT:
774 case elfcpp::R_386_JUMP_SLOT:
775 case elfcpp::R_386_RELATIVE:
776 case elfcpp::R_386_TLS_TPOFF:
777 case elfcpp::R_386_TLS_DTPMOD32:
778 case elfcpp::R_386_TLS_DTPOFF32:
779 case elfcpp::R_386_TLS_TPOFF32:
780 case elfcpp::R_386_TLS_DESC:
781 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
782 program_name, object->name().c_str(), r_type);
783 gold_exit(false);
784 break;
785
786 // These are initial TLS relocs, which are expected when
787 // linking.
788 case elfcpp::R_386_TLS_IE:
789 case elfcpp::R_386_TLS_GOTIE:
790 case elfcpp::R_386_TLS_LE:
791 case elfcpp::R_386_TLS_GD:
792 case elfcpp::R_386_TLS_LDM:
793 case elfcpp::R_386_TLS_LDO_32:
794 case elfcpp::R_386_TLS_IE_32:
795 case elfcpp::R_386_TLS_LE_32:
796 case elfcpp::R_386_TLS_GOTDESC:
797 case elfcpp::R_386_TLS_DESC_CALL:
798 {
799 bool output_is_shared = parameters->output_is_shared();
800 const tls::Tls_optimization optimized_type
801 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
802 switch (r_type)
803 {
804 case elfcpp::R_386_TLS_LE:
805 case elfcpp::R_386_TLS_LE_32:
806 // FIXME: If generating a shared object, we need to copy
807 // this relocation into the object.
808 gold_assert(!output_is_shared);
809 break;
810
811 case elfcpp::R_386_TLS_IE:
812 case elfcpp::R_386_TLS_IE_32:
813 case elfcpp::R_386_TLS_GOTIE:
814 // FIXME: If not relaxing to LE, we need to generate a
815 // TPOFF or TPOFF32 reloc.
816 if (optimized_type != tls::TLSOPT_TO_LE)
817 unsupported_reloc_local(object, r_type);
818 break;
819
820 case elfcpp::R_386_TLS_LDM:
821 // FIXME: If not relaxing to LE, we need to generate a
822 // DTPMOD32 reloc.
823 if (optimized_type != tls::TLSOPT_TO_LE)
824 unsupported_reloc_local(object, r_type);
825 break;
826
827 case elfcpp::R_386_TLS_LDO_32:
828 break;
829
830 case elfcpp::R_386_TLS_GD:
831 case elfcpp::R_386_TLS_GOTDESC:
832 case elfcpp::R_386_TLS_DESC_CALL:
833 // FIXME: If not relaxing to LE, we need to generate
834 // DTPMOD32 and DTPOFF32 relocs.
835 if (optimized_type != tls::TLSOPT_TO_LE)
836 unsupported_reloc_local(object, r_type);
837 break;
838
839 default:
840 gold_unreachable();
841 }
842 }
843 break;
844
845 case elfcpp::R_386_GOT32:
846 case elfcpp::R_386_PLT32:
847 case elfcpp::R_386_32PLT:
848 case elfcpp::R_386_TLS_GD_32:
849 case elfcpp::R_386_TLS_GD_PUSH:
850 case elfcpp::R_386_TLS_GD_CALL:
851 case elfcpp::R_386_TLS_GD_POP:
852 case elfcpp::R_386_TLS_LDM_32:
853 case elfcpp::R_386_TLS_LDM_PUSH:
854 case elfcpp::R_386_TLS_LDM_CALL:
855 case elfcpp::R_386_TLS_LDM_POP:
856 case elfcpp::R_386_USED_BY_INTEL_200:
857 default:
858 unsupported_reloc_local(object, r_type);
859 break;
860 }
861 }
862
863 // Report an unsupported relocation against a global symbol.
864
865 void
866 Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
867 unsigned int r_type,
868 Symbol* gsym)
869 {
870 fprintf(stderr,
871 _("%s: %s: unsupported reloc %u against global symbol %s\n"),
872 program_name, object->name().c_str(), r_type, gsym->name());
873 }
874
875 // Scan a relocation for a global symbol.
876
877 inline void
878 Target_i386::Scan::global(const General_options& options,
879 Symbol_table* symtab,
880 Layout* layout,
881 Target_i386* target,
882 Sized_relobj<32, false>* object,
883 unsigned int data_shndx,
884 const elfcpp::Rel<32, false>& reloc,
885 unsigned int r_type,
886 Symbol* gsym)
887 {
888 switch (r_type)
889 {
890 case elfcpp::R_386_NONE:
891 case elfcpp::R_386_GNU_VTINHERIT:
892 case elfcpp::R_386_GNU_VTENTRY:
893 break;
894
895 case elfcpp::R_386_32:
896 case elfcpp::R_386_PC32:
897 case elfcpp::R_386_16:
898 case elfcpp::R_386_PC16:
899 case elfcpp::R_386_8:
900 case elfcpp::R_386_PC8:
901 // FIXME: If we are generating a shared object we may need to
902 // copy this relocation into the object. If this symbol is
903 // defined in a shared object, we may need to copy this
904 // relocation in order to avoid a COPY relocation.
905 gold_assert(!parameters->output_is_shared());
906
907 if (gsym->is_from_dynobj())
908 {
909 // This symbol is defined in a dynamic object. If it is a
910 // function, we make a PLT entry. Otherwise we need to
911 // either generate a COPY reloc or copy this reloc.
912 if (gsym->type() == elfcpp::STT_FUNC)
913 {
914 target->make_plt_entry(symtab, layout, gsym);
915
916 // If this is not a PC relative reference, then we may
917 // be taking the address of the function. In that case
918 // we need to set the entry in the dynamic symbol table
919 // to the address of the PLT entry.
920 if (r_type != elfcpp::R_386_PC32
921 && r_type != elfcpp::R_386_PC16
922 && r_type != elfcpp::R_386_PC8)
923 gsym->set_needs_dynsym_value();
924 }
925 else
926 target->copy_reloc(&options, symtab, layout, object, data_shndx,
927 gsym, reloc);
928 }
929
930 break;
931
932 case elfcpp::R_386_GOT32:
933 {
934 // The symbol requires a GOT entry.
935 Output_data_got<32, false>* got = target->got_section(symtab, layout);
936 if (got->add_global(gsym))
937 {
938 // If this symbol is not fully resolved, we need to add a
939 // dynamic relocation for it.
940 if (!gsym->final_value_is_known())
941 {
942 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
943 rel_dyn->add_global(gsym, elfcpp::R_386_GLOB_DAT, got,
944 gsym->got_offset());
945 }
946 }
947 }
948 break;
949
950 case elfcpp::R_386_PLT32:
951 // If the symbol is fully resolved, this is just a PC32 reloc.
952 // Otherwise we need a PLT entry.
953 if (gsym->final_value_is_known())
954 break;
955 target->make_plt_entry(symtab, layout, gsym);
956 break;
957
958 case elfcpp::R_386_GOTOFF:
959 case elfcpp::R_386_GOTPC:
960 // We need a GOT section.
961 target->got_section(symtab, layout);
962 break;
963
964 // These are relocations which should only be seen by the
965 // dynamic linker, and should never be seen here.
966 case elfcpp::R_386_COPY:
967 case elfcpp::R_386_GLOB_DAT:
968 case elfcpp::R_386_JUMP_SLOT:
969 case elfcpp::R_386_RELATIVE:
970 case elfcpp::R_386_TLS_TPOFF:
971 case elfcpp::R_386_TLS_DTPMOD32:
972 case elfcpp::R_386_TLS_DTPOFF32:
973 case elfcpp::R_386_TLS_TPOFF32:
974 case elfcpp::R_386_TLS_DESC:
975 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
976 program_name, object->name().c_str(), r_type);
977 gold_exit(false);
978 break;
979
980 // These are initial tls relocs, which are expected when
981 // linking.
982 case elfcpp::R_386_TLS_IE:
983 case elfcpp::R_386_TLS_GOTIE:
984 case elfcpp::R_386_TLS_LE:
985 case elfcpp::R_386_TLS_GD:
986 case elfcpp::R_386_TLS_LDM:
987 case elfcpp::R_386_TLS_LDO_32:
988 case elfcpp::R_386_TLS_IE_32:
989 case elfcpp::R_386_TLS_LE_32:
990 case elfcpp::R_386_TLS_GOTDESC:
991 case elfcpp::R_386_TLS_DESC_CALL:
992 {
993 const bool is_final = gsym->final_value_is_known();
994 const tls::Tls_optimization optimized_type
995 = Target_i386::optimize_tls_reloc(is_final, r_type);
996 switch (r_type)
997 {
998 case elfcpp::R_386_TLS_LE:
999 case elfcpp::R_386_TLS_LE_32:
1000 // FIXME: If generating a shared object, we need to copy
1001 // this relocation into the object.
1002 gold_assert(!parameters->output_is_shared());
1003 break;
1004
1005 case elfcpp::R_386_TLS_IE:
1006 case elfcpp::R_386_TLS_IE_32:
1007 case elfcpp::R_386_TLS_GOTIE:
1008 // FIXME: If not relaxing to LE, we need to generate a
1009 // TPOFF or TPOFF32 reloc.
1010 if (optimized_type != tls::TLSOPT_TO_LE)
1011 unsupported_reloc_global(object, r_type, gsym);
1012 break;
1013
1014 case elfcpp::R_386_TLS_LDM:
1015 // FIXME: If not relaxing to LE, we need to generate a
1016 // DTPMOD32 reloc.
1017 if (optimized_type != tls::TLSOPT_TO_LE)
1018 unsupported_reloc_global(object, r_type, gsym);
1019 break;
1020
1021 case elfcpp::R_386_TLS_LDO_32:
1022 break;
1023
1024 case elfcpp::R_386_TLS_GD:
1025 case elfcpp::R_386_TLS_GOTDESC:
1026 case elfcpp::R_386_TLS_DESC_CALL:
1027 // FIXME: If not relaxing to LE, we need to generate
1028 // DTPMOD32 and DTPOFF32 relocs.
1029 if (optimized_type != tls::TLSOPT_TO_LE)
1030 unsupported_reloc_global(object, r_type, gsym);
1031 break;
1032
1033 default:
1034 gold_unreachable();
1035 }
1036 }
1037 break;
1038
1039 case elfcpp::R_386_32PLT:
1040 case elfcpp::R_386_TLS_GD_32:
1041 case elfcpp::R_386_TLS_GD_PUSH:
1042 case elfcpp::R_386_TLS_GD_CALL:
1043 case elfcpp::R_386_TLS_GD_POP:
1044 case elfcpp::R_386_TLS_LDM_32:
1045 case elfcpp::R_386_TLS_LDM_PUSH:
1046 case elfcpp::R_386_TLS_LDM_CALL:
1047 case elfcpp::R_386_TLS_LDM_POP:
1048 case elfcpp::R_386_USED_BY_INTEL_200:
1049 default:
1050 unsupported_reloc_global(object, r_type, gsym);
1051 break;
1052 }
1053 }
1054
1055 // Scan relocations for a section.
1056
1057 void
1058 Target_i386::scan_relocs(const General_options& options,
1059 Symbol_table* symtab,
1060 Layout* layout,
1061 Sized_relobj<32, false>* object,
1062 unsigned int data_shndx,
1063 unsigned int sh_type,
1064 const unsigned char* prelocs,
1065 size_t reloc_count,
1066 size_t local_symbol_count,
1067 const unsigned char* plocal_symbols,
1068 Symbol** global_symbols)
1069 {
1070 if (sh_type == elfcpp::SHT_RELA)
1071 {
1072 fprintf(stderr, _("%s: %s: unsupported RELA reloc section\n"),
1073 program_name, object->name().c_str());
1074 gold_exit(false);
1075 }
1076
1077 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1078 Target_i386::Scan>(
1079 options,
1080 symtab,
1081 layout,
1082 this,
1083 object,
1084 data_shndx,
1085 prelocs,
1086 reloc_count,
1087 local_symbol_count,
1088 plocal_symbols,
1089 global_symbols);
1090 }
1091
1092 // Finalize the sections.
1093
1094 void
1095 Target_i386::do_finalize_sections(Layout* layout)
1096 {
1097 // Fill in some more dynamic tags.
1098 Output_data_dynamic* const odyn = layout->dynamic_data();
1099 if (odyn != NULL)
1100 {
1101 if (this->got_plt_ != NULL)
1102 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1103
1104 if (this->plt_ != NULL)
1105 {
1106 const Output_data* od = this->plt_->rel_plt();
1107 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1108 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1109 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1110 }
1111
1112 if (this->rel_dyn_ != NULL)
1113 {
1114 const Output_data* od = this->rel_dyn_;
1115 odyn->add_section_address(elfcpp::DT_REL, od);
1116 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1117 odyn->add_constant(elfcpp::DT_RELENT,
1118 elfcpp::Elf_sizes<32>::rel_size);
1119 }
1120
1121 if (!parameters->output_is_shared())
1122 {
1123 // The value of the DT_DEBUG tag is filled in by the dynamic
1124 // linker at run time, and used by the debugger.
1125 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1126 }
1127 }
1128
1129 // Emit any relocs we saved in an attempt to avoid generating COPY
1130 // relocs.
1131 if (this->copy_relocs_ == NULL)
1132 return;
1133 if (this->copy_relocs_->any_to_emit())
1134 {
1135 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1136 this->copy_relocs_->emit(rel_dyn);
1137 }
1138 delete this->copy_relocs_;
1139 this->copy_relocs_ = NULL;
1140 }
1141
1142 // Perform a relocation.
1143
1144 inline bool
1145 Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
1146 Target_i386* target,
1147 size_t relnum,
1148 const elfcpp::Rel<32, false>& rel,
1149 unsigned int r_type,
1150 const Sized_symbol<32>* gsym,
1151 const Symbol_value<32>* psymval,
1152 unsigned char* view,
1153 elfcpp::Elf_types<32>::Elf_Addr address,
1154 off_t view_size)
1155 {
1156 if (this->skip_call_tls_get_addr_)
1157 {
1158 if (r_type != elfcpp::R_386_PLT32
1159 || gsym == NULL
1160 || strcmp(gsym->name(), "___tls_get_addr") != 0)
1161 {
1162 fprintf(stderr, _("%s: %s: missing expected TLS relocation\n"),
1163 program_name,
1164 relinfo->location(relnum, rel.get_r_offset()).c_str());
1165 gold_exit(false);
1166 }
1167
1168 this->skip_call_tls_get_addr_ = false;
1169
1170 return false;
1171 }
1172
1173 // Pick the value to use for symbols defined in shared objects.
1174 Symbol_value<32> symval;
1175 if (gsym != NULL && gsym->is_from_dynobj() && gsym->has_plt_offset())
1176 {
1177 symval.set_output_value(target->plt_section()->address()
1178 + gsym->plt_offset());
1179 psymval = &symval;
1180 }
1181
1182 const Sized_relobj<32, false>* object = relinfo->object;
1183
1184 switch (r_type)
1185 {
1186 case elfcpp::R_386_NONE:
1187 case elfcpp::R_386_GNU_VTINHERIT:
1188 case elfcpp::R_386_GNU_VTENTRY:
1189 break;
1190
1191 case elfcpp::R_386_32:
1192 Relocate_functions<32, false>::rel32(view, object, psymval);
1193 break;
1194
1195 case elfcpp::R_386_PC32:
1196 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1197 break;
1198
1199 case elfcpp::R_386_16:
1200 Relocate_functions<32, false>::rel16(view, object, psymval);
1201 break;
1202
1203 case elfcpp::R_386_PC16:
1204 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
1205 break;
1206
1207 case elfcpp::R_386_8:
1208 Relocate_functions<32, false>::rel8(view, object, psymval);
1209 break;
1210
1211 case elfcpp::R_386_PC8:
1212 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
1213 break;
1214
1215 case elfcpp::R_386_PLT32:
1216 gold_assert(gsym->has_plt_offset()
1217 || gsym->final_value_is_known());
1218 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1219 break;
1220
1221 case elfcpp::R_386_GOT32:
1222 // Local GOT offsets not yet supported.
1223 gold_assert(gsym);
1224 gold_assert(gsym->has_got_offset());
1225 Relocate_functions<32, false>::rel32(view, gsym->got_offset());
1226 break;
1227
1228 case elfcpp::R_386_GOTOFF:
1229 {
1230 elfcpp::Elf_types<32>::Elf_Addr value;
1231 value = (psymval->value(object, 0)
1232 - target->got_section(NULL, NULL)->address());
1233 Relocate_functions<32, false>::rel32(view, value);
1234 }
1235 break;
1236
1237 case elfcpp::R_386_GOTPC:
1238 {
1239 elfcpp::Elf_types<32>::Elf_Addr value;
1240 value = target->got_section(NULL, NULL)->address();
1241 Relocate_functions<32, false>::pcrel32(view, value, address);
1242 }
1243 break;
1244
1245 case elfcpp::R_386_COPY:
1246 case elfcpp::R_386_GLOB_DAT:
1247 case elfcpp::R_386_JUMP_SLOT:
1248 case elfcpp::R_386_RELATIVE:
1249 // These are outstanding tls relocs, which are unexpected when
1250 // linking.
1251 case elfcpp::R_386_TLS_TPOFF:
1252 case elfcpp::R_386_TLS_DTPMOD32:
1253 case elfcpp::R_386_TLS_DTPOFF32:
1254 case elfcpp::R_386_TLS_TPOFF32:
1255 case elfcpp::R_386_TLS_DESC:
1256 fprintf(stderr, _("%s: %s: unexpected reloc %u in object file\n"),
1257 program_name,
1258 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1259 r_type);
1260 gold_exit(false);
1261 break;
1262
1263 // These are initial tls relocs, which are expected when
1264 // linking.
1265 case elfcpp::R_386_TLS_IE:
1266 case elfcpp::R_386_TLS_GOTIE:
1267 case elfcpp::R_386_TLS_LE:
1268 case elfcpp::R_386_TLS_GD:
1269 case elfcpp::R_386_TLS_LDM:
1270 case elfcpp::R_386_TLS_LDO_32:
1271 case elfcpp::R_386_TLS_IE_32:
1272 case elfcpp::R_386_TLS_LE_32:
1273 case elfcpp::R_386_TLS_GOTDESC:
1274 case elfcpp::R_386_TLS_DESC_CALL:
1275 this->relocate_tls(relinfo, relnum, rel, r_type, gsym, psymval, view,
1276 address, view_size);
1277 break;
1278
1279 case elfcpp::R_386_32PLT:
1280 case elfcpp::R_386_TLS_GD_32:
1281 case elfcpp::R_386_TLS_GD_PUSH:
1282 case elfcpp::R_386_TLS_GD_CALL:
1283 case elfcpp::R_386_TLS_GD_POP:
1284 case elfcpp::R_386_TLS_LDM_32:
1285 case elfcpp::R_386_TLS_LDM_PUSH:
1286 case elfcpp::R_386_TLS_LDM_CALL:
1287 case elfcpp::R_386_TLS_LDM_POP:
1288 case elfcpp::R_386_USED_BY_INTEL_200:
1289 default:
1290 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1291 program_name,
1292 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1293 r_type);
1294 // gold_exit(false);
1295 break;
1296 }
1297
1298 return true;
1299 }
1300
1301 // Perform a TLS relocation.
1302
1303 inline void
1304 Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
1305 size_t relnum,
1306 const elfcpp::Rel<32, false>& rel,
1307 unsigned int r_type,
1308 const Sized_symbol<32>* gsym,
1309 const Symbol_value<32>* psymval,
1310 unsigned char* view,
1311 elfcpp::Elf_types<32>::Elf_Addr,
1312 off_t view_size)
1313 {
1314 Output_segment* tls_segment = relinfo->layout->tls_segment();
1315 if (tls_segment == NULL)
1316 {
1317 fprintf(stderr, _("%s: %s: TLS reloc but no TLS segment\n"),
1318 program_name,
1319 relinfo->location(relnum, rel.get_r_offset()).c_str());
1320 gold_exit(false);
1321 }
1322
1323 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(relinfo->object, 0);
1324
1325 const bool is_final = (gsym == NULL
1326 ? !parameters->output_is_shared()
1327 : gsym->final_value_is_known());
1328 const tls::Tls_optimization optimized_type
1329 = Target_i386::optimize_tls_reloc(is_final, r_type);
1330 switch (r_type)
1331 {
1332 case elfcpp::R_386_TLS_LE_32:
1333 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1334 Relocate_functions<32, false>::rel32(view, value);
1335 break;
1336
1337 case elfcpp::R_386_TLS_LE:
1338 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1339 Relocate_functions<32, false>::rel32(view, value);
1340 break;
1341
1342 case elfcpp::R_386_TLS_IE:
1343 case elfcpp::R_386_TLS_GOTIE:
1344 case elfcpp::R_386_TLS_IE_32:
1345 if (optimized_type == tls::TLSOPT_TO_LE)
1346 {
1347 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1348 rel, r_type, value, view,
1349 view_size);
1350 break;
1351 }
1352 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1353 program_name,
1354 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1355 r_type);
1356 // gold_exit(false);
1357 break;
1358
1359 case elfcpp::R_386_TLS_GD:
1360 if (optimized_type == tls::TLSOPT_TO_LE)
1361 {
1362 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1363 rel, r_type, value, view,
1364 view_size);
1365 break;
1366 }
1367 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1368 program_name,
1369 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1370 r_type);
1371 // gold_exit(false);
1372 break;
1373
1374 case elfcpp::R_386_TLS_LDM:
1375 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1376 {
1377 fprintf(stderr,
1378 _("%s: %s: both SUN and GNU model TLS relocations\n"),
1379 program_name,
1380 relinfo->location(relnum, rel.get_r_offset()).c_str());
1381 gold_exit(false);
1382 }
1383 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
1384 if (optimized_type == tls::TLSOPT_TO_LE)
1385 {
1386 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1387 value, view, view_size);
1388 break;
1389 }
1390 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1391 program_name,
1392 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1393 r_type);
1394 // gold_exit(false);
1395 break;
1396
1397 case elfcpp::R_386_TLS_LDO_32:
1398 // This reloc can appear in debugging sections, in which case we
1399 // won't see the TLS_LDM reloc. The local_dynamic_type field
1400 // tells us this.
1401 if (optimized_type != tls::TLSOPT_TO_LE
1402 || this->local_dynamic_type_ == LOCAL_DYNAMIC_NONE)
1403 value = value - tls_segment->vaddr();
1404 else if (this->local_dynamic_type_ == LOCAL_DYNAMIC_GNU)
1405 value = value - (tls_segment->vaddr() + tls_segment->memsz());
1406 else
1407 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1408 Relocate_functions<32, false>::rel32(view, value);
1409 break;
1410
1411 case elfcpp::R_386_TLS_GOTDESC:
1412 case elfcpp::R_386_TLS_DESC_CALL:
1413 fprintf(stderr, _("%s: %s: unsupported reloc %u\n"),
1414 program_name,
1415 relinfo->location(relnum, rel.get_r_offset()).c_str(),
1416 r_type);
1417 // gold_exit(false);
1418 break;
1419 }
1420 }
1421
1422 // Do a relocation in which we convert a TLS Initial-Exec to a
1423 // Local-Exec.
1424
1425 inline void
1426 Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
1427 size_t relnum,
1428 Output_segment* tls_segment,
1429 const elfcpp::Rel<32, false>& rel,
1430 unsigned int r_type,
1431 elfcpp::Elf_types<32>::Elf_Addr value,
1432 unsigned char* view,
1433 off_t view_size)
1434 {
1435 // We have to actually change the instructions, which means that we
1436 // need to examine the opcodes to figure out which instruction we
1437 // are looking at.
1438 if (r_type == elfcpp::R_386_TLS_IE)
1439 {
1440 // movl %gs:XX,%eax ==> movl $YY,%eax
1441 // movl %gs:XX,%reg ==> movl $YY,%reg
1442 // addl %gs:XX,%reg ==> addl $YY,%reg
1443 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
1444 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1445
1446 unsigned char op1 = view[-1];
1447 if (op1 == 0xa1)
1448 {
1449 // movl XX,%eax ==> movl $YY,%eax
1450 view[-1] = 0xb8;
1451 }
1452 else
1453 {
1454 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1455
1456 unsigned char op2 = view[-2];
1457 if (op2 == 0x8b)
1458 {
1459 // movl XX,%reg ==> movl $YY,%reg
1460 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1461 (op1 & 0xc7) == 0x05);
1462 view[-2] = 0xc7;
1463 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1464 }
1465 else if (op2 == 0x03)
1466 {
1467 // addl XX,%reg ==> addl $YY,%reg
1468 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1469 (op1 & 0xc7) == 0x05);
1470 view[-2] = 0x81;
1471 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1472 }
1473 else
1474 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1475 }
1476 }
1477 else
1478 {
1479 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1480 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1481 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1482 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1483 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
1484
1485 unsigned char op1 = view[-1];
1486 unsigned char op2 = view[-2];
1487 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1488 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
1489 if (op2 == 0x8b)
1490 {
1491 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
1492 view[-2] = 0xc7;
1493 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1494 }
1495 else if (op2 == 0x2b)
1496 {
1497 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
1498 view[-2] = 0x81;
1499 view[-1] = 0xe8 | ((op1 >> 3) & 7);
1500 }
1501 else if (op2 == 0x03)
1502 {
1503 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
1504 view[-2] = 0x81;
1505 view[-1] = 0xc0 | ((op1 >> 3) & 7);
1506 }
1507 else
1508 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
1509 }
1510
1511 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1512 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
1513 value = - value;
1514
1515 Relocate_functions<32, false>::rel32(view, value);
1516 }
1517
1518 // Do a relocation in which we convert a TLS Global-Dynamic to a
1519 // Local-Exec.
1520
1521 inline void
1522 Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
1523 size_t relnum,
1524 Output_segment* tls_segment,
1525 const elfcpp::Rel<32, false>& rel,
1526 unsigned int,
1527 elfcpp::Elf_types<32>::Elf_Addr value,
1528 unsigned char* view,
1529 off_t view_size)
1530 {
1531 // leal foo(,%reg,1),%eax; call ___tls_get_addr
1532 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1533 // leal foo(%reg),%eax; call ___tls_get_addr
1534 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
1535
1536 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1537 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1538
1539 unsigned char op1 = view[-1];
1540 unsigned char op2 = view[-2];
1541
1542 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1543 op2 == 0x8d || op2 == 0x04);
1544 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1545
1546 int roff = 5;
1547
1548 if (op2 == 0x04)
1549 {
1550 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
1551 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
1552 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1553 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
1554 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1555 }
1556 else
1557 {
1558 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1559 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
1560 if (static_cast<off_t>(rel.get_r_offset() + 9) < view_size
1561 && view[9] == 0x90)
1562 {
1563 // There is a trailing nop. Use the size byte subl.
1564 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
1565 roff = 6;
1566 }
1567 else
1568 {
1569 // Use the five byte subl.
1570 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
1571 }
1572 }
1573
1574 value = tls_segment->vaddr() + tls_segment->memsz() - value;
1575 Relocate_functions<32, false>::rel32(view + roff, value);
1576
1577 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1578 // We can skip it.
1579 this->skip_call_tls_get_addr_ = true;
1580 }
1581
1582 // Do a relocation in which we convert a TLS Local-Dynamic to a
1583 // Local-Exec.
1584
1585 inline void
1586 Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
1587 size_t relnum,
1588 Output_segment*,
1589 const elfcpp::Rel<32, false>& rel,
1590 unsigned int,
1591 elfcpp::Elf_types<32>::Elf_Addr,
1592 unsigned char* view,
1593 off_t view_size)
1594 {
1595 // leal foo(%reg), %eax; call ___tls_get_addr
1596 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
1597
1598 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
1599 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
1600
1601 // FIXME: Does this test really always pass?
1602 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
1603 view[-2] == 0x8d && view[-1] == 0x83);
1604
1605 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
1606
1607 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
1608
1609 // The next reloc should be a PLT32 reloc against __tls_get_addr.
1610 // We can skip it.
1611 this->skip_call_tls_get_addr_ = true;
1612 }
1613
1614 // Relocate section data.
1615
1616 void
1617 Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
1618 unsigned int sh_type,
1619 const unsigned char* prelocs,
1620 size_t reloc_count,
1621 unsigned char* view,
1622 elfcpp::Elf_types<32>::Elf_Addr address,
1623 off_t view_size)
1624 {
1625 gold_assert(sh_type == elfcpp::SHT_REL);
1626
1627 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
1628 Target_i386::Relocate>(
1629 relinfo,
1630 this,
1631 prelocs,
1632 reloc_count,
1633 view,
1634 address,
1635 view_size);
1636 }
1637
1638 // Return the value to use for a dynamic which requires special
1639 // treatment. This is how we support equality comparisons of function
1640 // pointers across shared library boundaries, as described in the
1641 // processor specific ABI supplement.
1642
1643 uint64_t
1644 Target_i386::do_dynsym_value(const Symbol* gsym) const
1645 {
1646 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
1647 return this->plt_section()->address() + gsym->plt_offset();
1648 }
1649
1650 // Return a string used to fill a code section with nops to take up
1651 // the specified length.
1652
1653 std::string
1654 Target_i386::do_code_fill(off_t length)
1655 {
1656 if (length >= 16)
1657 {
1658 // Build a jmp instruction to skip over the bytes.
1659 unsigned char jmp[5];
1660 jmp[0] = 0xe9;
1661 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
1662 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
1663 + std::string(length - 5, '\0'));
1664 }
1665
1666 // Nop sequences of various lengths.
1667 const char nop1[1] = { 0x90 }; // nop
1668 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
1669 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
1670 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
1671 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
1672 0x00 }; // leal 0(%esi,1),%esi
1673 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1674 0x00, 0x00 };
1675 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1676 0x00, 0x00, 0x00 };
1677 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
1678 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
1679 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
1680 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
1681 0x00 };
1682 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
1683 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
1684 0x00, 0x00 };
1685 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
1686 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
1687 0x00, 0x00, 0x00 };
1688 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1689 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
1690 0x00, 0x00, 0x00, 0x00 };
1691 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
1692 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
1693 0x27, 0x00, 0x00, 0x00,
1694 0x00 };
1695 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
1696 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
1697 0xbc, 0x27, 0x00, 0x00,
1698 0x00, 0x00 };
1699 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
1700 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
1701 0x90, 0x90, 0x90, 0x90,
1702 0x90, 0x90, 0x90 };
1703
1704 const char* nops[16] = {
1705 NULL,
1706 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
1707 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
1708 };
1709
1710 return std::string(nops[length], length);
1711 }
1712
1713 // The selector for i386 object files.
1714
1715 class Target_selector_i386 : public Target_selector
1716 {
1717 public:
1718 Target_selector_i386()
1719 : Target_selector(elfcpp::EM_386, 32, false)
1720 { }
1721
1722 Target*
1723 recognize(int machine, int osabi, int abiversion);
1724
1725 private:
1726 Target_i386* target_;
1727 };
1728
1729 // Recognize an i386 object file when we already know that the machine
1730 // number is EM_386.
1731
1732 Target*
1733 Target_selector_i386::recognize(int, int, int)
1734 {
1735 if (this->target_ == NULL)
1736 this->target_ = new Target_i386();
1737 return this->target_;
1738 }
1739
1740 Target_selector_i386 target_selector_i386;
1741
1742 } // End anonymous namespace.