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