* cp-name-parser.y (cp_merge_demangle_parse_infos): Don't use
[deliverable/binutils-gdb.git] / gold / i386.cc
CommitLineData
14bfc3f5
ILT
1// i386.cc -- i386 target support for gold.
2
b3ce541e 3// Copyright 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
6cb15b7f
ILT
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
14bfc3f5 23#include "gold.h"
ead1e424
ILT
24
25#include <cstring>
26
14bfc3f5 27#include "elfcpp.h"
07a60597 28#include "dwarf.h"
7e1edb90 29#include "parameters.h"
92e059d8 30#include "reloc.h"
61ba1cf9
ILT
31#include "i386.h"
32#include "object.h"
ead1e424 33#include "symtab.h"
92e059d8
ILT
34#include "layout.h"
35#include "output.h"
12c0daef 36#include "copy-relocs.h"
14bfc3f5 37#include "target.h"
61ba1cf9 38#include "target-reloc.h"
14bfc3f5 39#include "target-select.h"
af6359d5 40#include "tls.h"
36959681 41#include "freebsd.h"
f345227a 42#include "gc.h"
14bfc3f5
ILT
43
44namespace
45{
46
47using namespace gold;
48
7223e9ca
ILT
49// A class to handle the PLT data.
50
51class Output_data_plt_i386 : public Output_section_data
52{
53 public:
54 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
55
67181c72 56 Output_data_plt_i386(Layout*, Output_data_space*, Output_data_space*);
7223e9ca
ILT
57
58 // Add an entry to the PLT.
59 void
67181c72 60 add_entry(Symbol_table*, Layout*, Symbol* gsym);
7223e9ca
ILT
61
62 // Add an entry to the PLT for a local STT_GNU_IFUNC symbol.
63 unsigned int
67181c72
ILT
64 add_local_ifunc_entry(Symbol_table*, Layout*,
65 Sized_relobj_file<32, false>* relobj,
7223e9ca
ILT
66 unsigned int local_sym_index);
67
68 // Return the .rel.plt section data.
69 Reloc_section*
70 rel_plt() const
71 { return this->rel_; }
72
73 // Return where the TLS_DESC relocations should go.
74 Reloc_section*
75 rel_tls_desc(Layout*);
76
67181c72
ILT
77 // Return where the IRELATIVE relocations should go.
78 Reloc_section*
79 rel_irelative(Symbol_table*, Layout*);
80
81 // Return whether we created a section for IRELATIVE relocations.
82 bool
83 has_irelative_section() const
84 { return this->irelative_rel_ != NULL; }
85
7223e9ca
ILT
86 // Return the number of PLT entries.
87 unsigned int
88 entry_count() const
67181c72 89 { return this->count_ + this->irelative_count_; }
7223e9ca
ILT
90
91 // Return the offset of the first non-reserved PLT entry.
92 static unsigned int
93 first_plt_entry_offset()
94 { return plt_entry_size; }
95
96 // Return the size of a PLT entry.
97 static unsigned int
98 get_plt_entry_size()
99 { return plt_entry_size; }
100
67181c72
ILT
101 // Return the PLT address to use for a global symbol.
102 uint64_t
103 address_for_global(const Symbol*);
104
105 // Return the PLT address to use for a local symbol.
106 uint64_t
107 address_for_local(const Relobj*, unsigned int symndx);
108
7223e9ca
ILT
109 protected:
110 void
111 do_adjust_output_section(Output_section* os);
112
113 // Write to a map file.
114 void
115 do_print_to_mapfile(Mapfile* mapfile) const
116 { mapfile->print_output_data(this, _("** PLT")); }
117
118 private:
119 // The size of an entry in the PLT.
120 static const int plt_entry_size = 16;
121
122 // The first entry in the PLT for an executable.
07a60597 123 static const unsigned char exec_first_plt_entry[plt_entry_size];
7223e9ca
ILT
124
125 // The first entry in the PLT for a shared object.
07a60597 126 static const unsigned char dyn_first_plt_entry[plt_entry_size];
7223e9ca
ILT
127
128 // Other entries in the PLT for an executable.
07a60597 129 static const unsigned char exec_plt_entry[plt_entry_size];
7223e9ca
ILT
130
131 // Other entries in the PLT for a shared object.
07a60597
ILT
132 static const unsigned char dyn_plt_entry[plt_entry_size];
133
134 // The .eh_frame unwind information for the PLT.
135 static const int plt_eh_frame_cie_size = 16;
136 static const int plt_eh_frame_fde_size = 32;
137 static const unsigned char plt_eh_frame_cie[plt_eh_frame_cie_size];
138 static const unsigned char plt_eh_frame_fde[plt_eh_frame_fde_size];
7223e9ca
ILT
139
140 // Set the final size.
141 void
142 set_final_data_size()
67181c72
ILT
143 {
144 this->set_data_size((this->count_ + this->irelative_count_ + 1)
145 * plt_entry_size);
146 }
7223e9ca
ILT
147
148 // Write out the PLT data.
149 void
150 do_write(Output_file*);
151
152 // We keep a list of global STT_GNU_IFUNC symbols, each with its
153 // offset in the GOT.
154 struct Global_ifunc
155 {
156 Symbol* sym;
157 unsigned int got_offset;
158 };
159
160 // We keep a list of local STT_GNU_IFUNC symbols, each with its
161 // offset in the GOT.
162 struct Local_ifunc
163 {
6fa2a40b 164 Sized_relobj_file<32, false>* object;
7223e9ca
ILT
165 unsigned int local_sym_index;
166 unsigned int got_offset;
167 };
168
7d172687
ILT
169 // A pointer to the Layout class, so that we can find the .dynamic
170 // section when we write out the GOT PLT section.
171 Layout* layout_;
7223e9ca
ILT
172 // The reloc section.
173 Reloc_section* rel_;
174 // The TLS_DESC relocations, if necessary. These must follow the
175 // regular PLT relocs.
176 Reloc_section* tls_desc_rel_;
67181c72
ILT
177 // The IRELATIVE relocations, if necessary. These must follow the
178 // regular relocatoins and the TLS_DESC relocations.
179 Reloc_section* irelative_rel_;
7223e9ca
ILT
180 // The .got.plt section.
181 Output_data_space* got_plt_;
67181c72
ILT
182 // The part of the .got.plt section used for IRELATIVE relocs.
183 Output_data_space* got_irelative_;
7223e9ca
ILT
184 // The number of PLT entries.
185 unsigned int count_;
67181c72
ILT
186 // Number of PLT entries with R_386_IRELATIVE relocs. These follow
187 // the regular PLT entries.
188 unsigned int irelative_count_;
7223e9ca
ILT
189 // Global STT_GNU_IFUNC symbols.
190 std::vector<Global_ifunc> global_ifuncs_;
191 // Local STT_GNU_IFUNC symbols.
192 std::vector<Local_ifunc> local_ifuncs_;
193};
a3ad94ed 194
14bfc3f5 195// The i386 target class.
e041f13d
ILT
196// TLS info comes from
197// http://people.redhat.com/drepper/tls.pdf
198// http://www.lsd.ic.unicamp.br/~oliva/writeups/TLS/RFC-TLSDESC-x86.txt
14bfc3f5 199
200b2bb9 200class Target_i386 : public Sized_target<32, false>
14bfc3f5
ILT
201{
202 public:
5a6f7e2d
ILT
203 typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, false> Reloc_section;
204
14bfc3f5 205 Target_i386()
200b2bb9 206 : Sized_target<32, false>(&i386_info),
67181c72
ILT
207 got_(NULL), plt_(NULL), got_plt_(NULL), got_irelative_(NULL),
208 got_tlsdesc_(NULL), global_offset_table_(NULL), rel_dyn_(NULL),
209 rel_irelative_(NULL), copy_relocs_(elfcpp::R_386_COPY), dynbss_(NULL),
edfbb029 210 got_mod_index_offset_(-1U), tls_base_symbol_defined_(false)
14bfc3f5 211 { }
75f65a3e 212
6d03d481
ST
213 // Process the relocations to determine unreferenced sections for
214 // garbage collection.
215 void
ad0f2072 216 gc_process_relocs(Symbol_table* symtab,
6d03d481 217 Layout* layout,
6fa2a40b 218 Sized_relobj_file<32, false>* object,
6d03d481
ST
219 unsigned int data_shndx,
220 unsigned int sh_type,
221 const unsigned char* prelocs,
222 size_t reloc_count,
223 Output_section* output_section,
224 bool needs_special_offset_handling,
225 size_t local_symbol_count,
226 const unsigned char* plocal_symbols);
227
92e059d8 228 // Scan the relocations to look for symbol adjustments.
61ba1cf9 229 void
ad0f2072 230 scan_relocs(Symbol_table* symtab,
ead1e424 231 Layout* layout,
6fa2a40b 232 Sized_relobj_file<32, false>* object,
a3ad94ed 233 unsigned int data_shndx,
92e059d8
ILT
234 unsigned int sh_type,
235 const unsigned char* prelocs,
236 size_t reloc_count,
730cdc88
ILT
237 Output_section* output_section,
238 bool needs_special_offset_handling,
92e059d8 239 size_t local_symbol_count,
730cdc88 240 const unsigned char* plocal_symbols);
61ba1cf9 241
5a6f7e2d
ILT
242 // Finalize the sections.
243 void
f59f41f3 244 do_finalize_sections(Layout*, const Input_objects*, Symbol_table*);
5a6f7e2d 245
ab5c9e90
ILT
246 // Return the value to use for a dynamic which requires special
247 // treatment.
248 uint64_t
249 do_dynsym_value(const Symbol*) const;
250
92e059d8
ILT
251 // Relocate a section.
252 void
253 relocate_section(const Relocate_info<32, false>*,
254 unsigned int sh_type,
255 const unsigned char* prelocs,
256 size_t reloc_count,
730cdc88
ILT
257 Output_section* output_section,
258 bool needs_special_offset_handling,
92e059d8
ILT
259 unsigned char* view,
260 elfcpp::Elf_types<32>::Elf_Addr view_address,
364c7fa5
ILT
261 section_size_type view_size,
262 const Reloc_symbol_changes*);
92e059d8 263
6a74a719
ILT
264 // Scan the relocs during a relocatable link.
265 void
ad0f2072 266 scan_relocatable_relocs(Symbol_table* symtab,
6a74a719 267 Layout* layout,
6fa2a40b 268 Sized_relobj_file<32, false>* object,
6a74a719
ILT
269 unsigned int data_shndx,
270 unsigned int sh_type,
271 const unsigned char* prelocs,
272 size_t reloc_count,
273 Output_section* output_section,
274 bool needs_special_offset_handling,
275 size_t local_symbol_count,
276 const unsigned char* plocal_symbols,
277 Relocatable_relocs*);
278
279 // Relocate a section during a relocatable link.
280 void
281 relocate_for_relocatable(const Relocate_info<32, false>*,
282 unsigned int sh_type,
283 const unsigned char* prelocs,
284 size_t reloc_count,
285 Output_section* output_section,
286 off_t offset_in_output_section,
287 const Relocatable_relocs*,
288 unsigned char* view,
289 elfcpp::Elf_types<32>::Elf_Addr view_address,
290 section_size_type view_size,
291 unsigned char* reloc_view,
292 section_size_type reloc_view_size);
293
c51e6221
ILT
294 // Return a string used to fill a code section with nops.
295 std::string
8851ecca 296 do_code_fill(section_size_type length) const;
c51e6221 297
9a2d6984
ILT
298 // Return whether SYM is defined by the ABI.
299 bool
9c2d0ef9 300 do_is_defined_by_abi(const Symbol* sym) const
9a2d6984
ILT
301 { return strcmp(sym->name(), "___tls_get_addr") == 0; }
302
bb04269c
DK
303 // Return whether a symbol name implies a local label. The UnixWare
304 // 2.1 cc generates temporary symbols that start with .X, so we
305 // recognize them here. FIXME: do other SVR4 compilers also use .X?.
306 // If so, we should move the .X recognition into
307 // Target::do_is_local_label_name.
308 bool
309 do_is_local_label_name(const char* name) const
310 {
311 if (name[0] == '.' && name[1] == 'X')
312 return true;
313 return Target::do_is_local_label_name(name);
314 }
315
67181c72
ILT
316 // Return the PLT address to use for a global symbol.
317 uint64_t
318 do_plt_address_for_global(const Symbol* gsym) const
319 { return this->plt_section()->address_for_global(gsym); }
7223e9ca 320
67181c72
ILT
321 uint64_t
322 do_plt_address_for_local(const Relobj* relobj, unsigned int symndx) const
323 { return this->plt_section()->address_for_local(relobj, symndx); }
7223e9ca 324
b3ce541e
ILT
325 // We can tell whether we take the address of a function.
326 inline bool
327 do_can_check_for_function_pointers() const
328 { return true; }
329
02d7cd44
ILT
330 // Return the base for a DW_EH_PE_datarel encoding.
331 uint64_t
332 do_ehframe_datarel_base() const;
333
b6848d3c
ILT
334 // Return whether SYM is call to a non-split function.
335 bool
336 do_is_call_to_non_split(const Symbol* sym, unsigned int) const;
337
9b547ce6 338 // Adjust -fsplit-stack code which calls non-split-stack code.
364c7fa5
ILT
339 void
340 do_calls_non_split(Relobj* object, unsigned int shndx,
341 section_offset_type fnoffset, section_size_type fnsize,
342 unsigned char* view, section_size_type view_size,
343 std::string* from, std::string* to) const;
344
96f2030e 345 // Return the size of the GOT section.
fe8718a4 346 section_size_type
0e70b911 347 got_size() const
96f2030e
ILT
348 {
349 gold_assert(this->got_ != NULL);
350 return this->got_->data_size();
351 }
352
0e70b911
CC
353 // Return the number of entries in the GOT.
354 unsigned int
355 got_entry_count() const
356 {
357 if (this->got_ == NULL)
358 return 0;
359 return this->got_size() / 4;
360 }
361
362 // Return the number of entries in the PLT.
363 unsigned int
364 plt_entry_count() const;
365
366 // Return the offset of the first non-reserved PLT entry.
367 unsigned int
368 first_plt_entry_offset() const;
369
370 // Return the size of each PLT entry.
371 unsigned int
372 plt_entry_size() const;
373
92e059d8
ILT
374 private:
375 // The class which scans relocations.
376 struct Scan
61ba1cf9 377 {
95a2c8d6
RS
378 static inline int
379
380 get_reference_flags(unsigned int r_type);
381
61ba1cf9 382 inline void
ad0f2072 383 local(Symbol_table* symtab, Layout* layout, Target_i386* target,
6fa2a40b 384 Sized_relobj_file<32, false>* object,
a3ad94ed 385 unsigned int data_shndx,
07f397ab 386 Output_section* output_section,
92e059d8
ILT
387 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
388 const elfcpp::Sym<32, false>& lsym);
61ba1cf9 389
92e059d8 390 inline void
ad0f2072 391 global(Symbol_table* symtab, Layout* layout, Target_i386* target,
6fa2a40b 392 Sized_relobj_file<32, false>* object,
a3ad94ed 393 unsigned int data_shndx,
07f397ab 394 Output_section* output_section,
92e059d8
ILT
395 const elfcpp::Rel<32, false>& reloc, unsigned int r_type,
396 Symbol* gsym);
af6359d5 397
21bb3914 398 inline bool
0897ed3b
ST
399 local_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
400 Target_i386* target,
6fa2a40b 401 Sized_relobj_file<32, false>* object,
0897ed3b
ST
402 unsigned int data_shndx,
403 Output_section* output_section,
404 const elfcpp::Rel<32, false>& reloc,
405 unsigned int r_type,
406 const elfcpp::Sym<32, false>& lsym);
407
408 inline bool
409 global_reloc_may_be_function_pointer(Symbol_table* symtab, Layout* layout,
410 Target_i386* target,
6fa2a40b 411 Sized_relobj_file<32, false>* object,
0897ed3b
ST
412 unsigned int data_shndx,
413 Output_section* output_section,
414 const elfcpp::Rel<32, false>& reloc,
415 unsigned int r_type,
416 Symbol* gsym);
21bb3914
ST
417
418 inline bool
0897ed3b 419 possible_function_pointer_reloc(unsigned int r_type);
21bb3914 420
7223e9ca 421 bool
6fa2a40b
CC
422 reloc_needs_plt_for_ifunc(Sized_relobj_file<32, false>*,
423 unsigned int r_type);
7223e9ca 424
af6359d5 425 static void
6fa2a40b 426 unsupported_reloc_local(Sized_relobj_file<32, false>*, unsigned int r_type);
af6359d5
ILT
427
428 static void
6fa2a40b 429 unsupported_reloc_global(Sized_relobj_file<32, false>*, unsigned int r_type,
af6359d5 430 Symbol*);
61ba1cf9
ILT
431 };
432
92e059d8
ILT
433 // The class which implements relocation.
434 class Relocate
435 {
436 public:
ead1e424 437 Relocate()
46cf9fa2 438 : skip_call_tls_get_addr_(false),
82bb573a 439 local_dynamic_type_(LOCAL_DYNAMIC_NONE)
ead1e424
ILT
440 { }
441
442 ~Relocate()
443 {
444 if (this->skip_call_tls_get_addr_)
445 {
446 // FIXME: This needs to specify the location somehow.
75f2446e 447 gold_error(_("missing expected TLS relocation"));
ead1e424
ILT
448 }
449 }
450
86849f1f
ILT
451 // Return whether the static relocation needs to be applied.
452 inline bool
453 should_apply_static_reloc(const Sized_symbol<32>* gsym,
95a2c8d6 454 unsigned int r_type,
031cdbed
ILT
455 bool is_32bit,
456 Output_section* output_section);
86849f1f 457
ead1e424
ILT
458 // Do a relocation. Return false if the caller should not issue
459 // any warnings about this relocation.
460 inline bool
031cdbed
ILT
461 relocate(const Relocate_info<32, false>*, Target_i386*, Output_section*,
462 size_t relnum, const elfcpp::Rel<32, false>&,
c06b7b0b 463 unsigned int r_type, const Sized_symbol<32>*,
b8e6aad9 464 const Symbol_value<32>*,
92e059d8 465 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
fe8718a4 466 section_size_type);
92e059d8
ILT
467
468 private:
469 // Do a TLS relocation.
ead1e424 470 inline void
07f397ab
ILT
471 relocate_tls(const Relocate_info<32, false>*, Target_i386* target,
472 size_t relnum, const elfcpp::Rel<32, false>&,
c06b7b0b 473 unsigned int r_type, const Sized_symbol<32>*,
b8e6aad9 474 const Symbol_value<32>*,
fe8718a4
ILT
475 unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
476 section_size_type);
92e059d8 477
07f397ab
ILT
478 // Do a TLS General-Dynamic to Initial-Exec transition.
479 inline void
480 tls_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
481 Output_segment* tls_segment,
482 const elfcpp::Rel<32, false>&, unsigned int r_type,
483 elfcpp::Elf_types<32>::Elf_Addr value,
484 unsigned char* view,
fe8718a4 485 section_size_type view_size);
07f397ab 486
56622147
ILT
487 // Do a TLS General-Dynamic to Local-Exec transition.
488 inline void
489 tls_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
92e059d8
ILT
490 Output_segment* tls_segment,
491 const elfcpp::Rel<32, false>&, unsigned int r_type,
492 elfcpp::Elf_types<32>::Elf_Addr value,
493 unsigned char* view,
fe8718a4 494 section_size_type view_size);
92e059d8 495
c2b45e22
CC
496 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Initial-Exec
497 // transition.
498 inline void
499 tls_desc_gd_to_ie(const Relocate_info<32, false>*, size_t relnum,
500 Output_segment* tls_segment,
501 const elfcpp::Rel<32, false>&, unsigned int r_type,
502 elfcpp::Elf_types<32>::Elf_Addr value,
503 unsigned char* view,
504 section_size_type view_size);
505
506 // Do a TLS_GOTDESC or TLS_DESC_CALL General-Dynamic to Local-Exec
507 // transition.
508 inline void
509 tls_desc_gd_to_le(const Relocate_info<32, false>*, size_t relnum,
510 Output_segment* tls_segment,
511 const elfcpp::Rel<32, false>&, unsigned int r_type,
512 elfcpp::Elf_types<32>::Elf_Addr value,
513 unsigned char* view,
514 section_size_type view_size);
515
56622147 516 // Do a TLS Local-Dynamic to Local-Exec transition.
ead1e424 517 inline void
56622147 518 tls_ld_to_le(const Relocate_info<32, false>*, size_t relnum,
ead1e424
ILT
519 Output_segment* tls_segment,
520 const elfcpp::Rel<32, false>&, unsigned int r_type,
521 elfcpp::Elf_types<32>::Elf_Addr value,
522 unsigned char* view,
fe8718a4 523 section_size_type view_size);
ead1e424 524
56622147
ILT
525 // Do a TLS Initial-Exec to Local-Exec transition.
526 static inline void
527 tls_ie_to_le(const Relocate_info<32, false>*, size_t relnum,
46cf9fa2
ILT
528 Output_segment* tls_segment,
529 const elfcpp::Rel<32, false>&, unsigned int r_type,
530 elfcpp::Elf_types<32>::Elf_Addr value,
531 unsigned char* view,
fe8718a4 532 section_size_type view_size);
46cf9fa2 533
46cf9fa2
ILT
534 // We need to keep track of which type of local dynamic relocation
535 // we have seen, so that we can optimize R_386_TLS_LDO_32 correctly.
536 enum Local_dynamic_type
537 {
538 LOCAL_DYNAMIC_NONE,
539 LOCAL_DYNAMIC_SUN,
540 LOCAL_DYNAMIC_GNU
541 };
542
ead1e424
ILT
543 // This is set if we should skip the next reloc, which should be a
544 // PLT32 reloc against ___tls_get_addr.
545 bool skip_call_tls_get_addr_;
46cf9fa2
ILT
546 // The type of local dynamic relocation we have seen in the section
547 // being relocated, if any.
548 Local_dynamic_type local_dynamic_type_;
92e059d8
ILT
549 };
550
6a74a719
ILT
551 // A class which returns the size required for a relocation type,
552 // used while scanning relocs during a relocatable link.
553 class Relocatable_size_for_reloc
554 {
555 public:
556 unsigned int
557 get_size_for_reloc(unsigned int, Relobj*);
558 };
559
92e059d8
ILT
560 // Adjust TLS relocation type based on the options and whether this
561 // is a local symbol.
af6359d5 562 static tls::Tls_optimization
7e1edb90 563 optimize_tls_reloc(bool is_final, int r_type);
92e059d8 564
ead1e424 565 // Get the GOT section, creating it if necessary.
dbe717ef 566 Output_data_got<32, false>*
7e1edb90 567 got_section(Symbol_table*, Layout*);
a3ad94ed 568
96f2030e
ILT
569 // Get the GOT PLT section.
570 Output_data_space*
571 got_plt_section() const
572 {
573 gold_assert(this->got_plt_ != NULL);
574 return this->got_plt_;
575 }
576
a8df5856
ILT
577 // Get the GOT section for TLSDESC entries.
578 Output_data_got<32, false>*
579 got_tlsdesc_section() const
580 {
581 gold_assert(this->got_tlsdesc_ != NULL);
582 return this->got_tlsdesc_;
583 }
584
7223e9ca
ILT
585 // Create the PLT section.
586 void
587 make_plt_section(Symbol_table* symtab, Layout* layout);
588
a3ad94ed
ILT
589 // Create a PLT entry for a global symbol.
590 void
7e1edb90 591 make_plt_entry(Symbol_table*, Layout*, Symbol*);
a3ad94ed 592
7223e9ca
ILT
593 // Create a PLT entry for a local STT_GNU_IFUNC symbol.
594 void
595 make_local_ifunc_plt_entry(Symbol_table*, Layout*,
6fa2a40b 596 Sized_relobj_file<32, false>* relobj,
7223e9ca
ILT
597 unsigned int local_sym_index);
598
9fa33bee 599 // Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
600 void
601 define_tls_base_symbol(Symbol_table*, Layout*);
602
94c4710f
ILT
603 // Create a GOT entry for the TLS module index.
604 unsigned int
605 got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 606 Sized_relobj_file<32, false>* object);
94c4710f 607
a3ad94ed 608 // Get the PLT section.
e291e7b9 609 Output_data_plt_i386*
a3ad94ed
ILT
610 plt_section() const
611 {
612 gold_assert(this->plt_ != NULL);
613 return this->plt_;
614 }
615
5a6f7e2d
ILT
616 // Get the dynamic reloc section, creating it if necessary.
617 Reloc_section*
618 rel_dyn_section(Layout*);
619
e291e7b9
ILT
620 // Get the section to use for TLS_DESC relocations.
621 Reloc_section*
622 rel_tls_desc_section(Layout*) const;
623
67181c72
ILT
624 // Get the section to use for IRELATIVE relocations.
625 Reloc_section*
626 rel_irelative_section(Layout*);
627
12c0daef 628 // Add a potential copy relocation.
a3ad94ed 629 void
ef9beddf 630 copy_reloc(Symbol_table* symtab, Layout* layout,
6fa2a40b 631 Sized_relobj_file<32, false>* object,
12c0daef
ILT
632 unsigned int shndx, Output_section* output_section,
633 Symbol* sym, const elfcpp::Rel<32, false>& reloc)
634 {
635 this->copy_relocs_.copy_reloc(symtab, layout,
636 symtab->get_sized_symbol<32>(sym),
637 object, shndx, output_section, reloc,
638 this->rel_dyn_section(layout));
639 }
ead1e424 640
92e059d8
ILT
641 // Information about this specific target which we pass to the
642 // general Target structure.
75f65a3e 643 static const Target::Target_info i386_info;
ead1e424 644
0a65a3a7 645 // The types of GOT entries needed for this platform.
0e70b911
CC
646 // These values are exposed to the ABI in an incremental link.
647 // Do not renumber existing values without changing the version
648 // number of the .gnu_incremental_inputs section.
0a65a3a7
CC
649 enum Got_type
650 {
651 GOT_TYPE_STANDARD = 0, // GOT entry for a regular symbol
c2b45e22
CC
652 GOT_TYPE_TLS_NOFFSET = 1, // GOT entry for negative TLS offset
653 GOT_TYPE_TLS_OFFSET = 2, // GOT entry for positive TLS offset
654 GOT_TYPE_TLS_PAIR = 3, // GOT entry for TLS module/offset pair
655 GOT_TYPE_TLS_DESC = 4 // GOT entry for TLS_DESC pair
0a65a3a7
CC
656 };
657
ead1e424 658 // The GOT section.
dbe717ef 659 Output_data_got<32, false>* got_;
a3ad94ed
ILT
660 // The PLT section.
661 Output_data_plt_i386* plt_;
662 // The GOT PLT section.
663 Output_data_space* got_plt_;
67181c72
ILT
664 // The GOT section for IRELATIVE relocations.
665 Output_data_space* got_irelative_;
a8df5856
ILT
666 // The GOT section for TLSDESC relocations.
667 Output_data_got<32, false>* got_tlsdesc_;
e785ec03
ILT
668 // The _GLOBAL_OFFSET_TABLE_ symbol.
669 Symbol* global_offset_table_;
5a6f7e2d
ILT
670 // The dynamic reloc section.
671 Reloc_section* rel_dyn_;
67181c72
ILT
672 // The section to use for IRELATIVE relocs.
673 Reloc_section* rel_irelative_;
5a6f7e2d 674 // Relocs saved to avoid a COPY reloc.
12c0daef 675 Copy_relocs<elfcpp::SHT_REL, 32, false> copy_relocs_;
5a6f7e2d
ILT
676 // Space for variables copied with a COPY reloc.
677 Output_data_space* dynbss_;
c2b45e22 678 // Offset of the GOT entry for the TLS module index.
94c4710f 679 unsigned int got_mod_index_offset_;
edfbb029
CC
680 // True if the _TLS_MODULE_BASE_ symbol has been defined.
681 bool tls_base_symbol_defined_;
75f65a3e
ILT
682};
683
684const Target::Target_info Target_i386::i386_info =
685{
61ba1cf9
ILT
686 32, // size
687 false, // is_big_endian
688 elfcpp::EM_386, // machine_code
689 false, // has_make_symbol
dbe717ef 690 false, // has_resolve
c51e6221 691 true, // has_code_fill
35cdfc9a 692 true, // is_default_stack_executable
b3ce541e 693 true, // can_icf_inline_merge_sections
0864d551 694 '\0', // wrap_char
dbe717ef 695 "/usr/lib/libc.so.1", // dynamic_linker
0c5e9c22 696 0x08048000, // default_text_segment_address
cd72c291 697 0x1000, // abi_pagesize (overridable by -z max-page-size)
8a5e3e08
ILT
698 0x1000, // common_pagesize (overridable by -z common-page-size)
699 elfcpp::SHN_UNDEF, // small_common_shndx
700 elfcpp::SHN_UNDEF, // large_common_shndx
701 0, // small_common_section_flags
05a352e6
DK
702 0, // large_common_section_flags
703 NULL, // attributes_section
704 NULL // attributes_vendor
14bfc3f5
ILT
705};
706
ead1e424
ILT
707// Get the GOT section, creating it if necessary.
708
dbe717ef 709Output_data_got<32, false>*
7e1edb90 710Target_i386::got_section(Symbol_table* symtab, Layout* layout)
ead1e424
ILT
711{
712 if (this->got_ == NULL)
713 {
7e1edb90 714 gold_assert(symtab != NULL && layout != NULL);
a3ad94ed 715
7e1edb90 716 this->got_ = new Output_data_got<32, false>();
ead1e424 717
9446efde
ILT
718 // When using -z now, we can treat .got.plt as a relro section.
719 // Without -z now, it is modified after program startup by lazy
720 // PLT relocations.
721 bool is_got_plt_relro = parameters->options().now();
722 Output_section_order got_order = (is_got_plt_relro
723 ? ORDER_RELRO
724 : ORDER_RELRO_LAST);
725 Output_section_order got_plt_order = (is_got_plt_relro
726 ? ORDER_RELRO
727 : ORDER_NON_RELRO_FIRST);
728
82742395
ILT
729 layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
730 (elfcpp::SHF_ALLOC
731 | elfcpp::SHF_WRITE),
9446efde 732 this->got_, got_order, true);
ead1e424 733
7d9e3d98 734 this->got_plt_ = new Output_data_space(4, "** GOT PLT");
82742395
ILT
735 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
736 (elfcpp::SHF_ALLOC
737 | elfcpp::SHF_WRITE),
9446efde
ILT
738 this->got_plt_, got_plt_order,
739 is_got_plt_relro);
a3ad94ed 740
ead1e424 741 // The first three entries are reserved.
27bc2bce 742 this->got_plt_->set_current_data_size(3 * 4);
ead1e424 743
9446efde
ILT
744 if (!is_got_plt_relro)
745 {
746 // Those bytes can go into the relro segment.
747 layout->increase_relro(3 * 4);
748 }
1a2dff53 749
a3ad94ed 750 // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
e785ec03
ILT
751 this->global_offset_table_ =
752 symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
753 Symbol_table::PREDEFINED,
754 this->got_plt_,
755 0, 0, elfcpp::STT_OBJECT,
756 elfcpp::STB_LOCAL,
757 elfcpp::STV_HIDDEN, 0,
758 false, false);
a8df5856 759
67181c72
ILT
760 // If there are any IRELATIVE relocations, they get GOT entries
761 // in .got.plt after the jump slot relocations.
762 this->got_irelative_ = new Output_data_space(4, "** GOT IRELATIVE PLT");
763 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
764 (elfcpp::SHF_ALLOC
765 | elfcpp::SHF_WRITE),
766 this->got_irelative_,
9446efde 767 got_plt_order, is_got_plt_relro);
67181c72 768
a8df5856
ILT
769 // If there are any TLSDESC relocations, they get GOT entries in
770 // .got.plt after the jump slot entries.
771 this->got_tlsdesc_ = new Output_data_got<32, false>();
772 layout->add_output_section_data(".got.plt", elfcpp::SHT_PROGBITS,
773 (elfcpp::SHF_ALLOC
774 | elfcpp::SHF_WRITE),
22f0da72 775 this->got_tlsdesc_,
9446efde 776 got_plt_order, is_got_plt_relro);
ead1e424 777 }
a3ad94ed 778
ead1e424
ILT
779 return this->got_;
780}
781
5a6f7e2d
ILT
782// Get the dynamic reloc section, creating it if necessary.
783
784Target_i386::Reloc_section*
785Target_i386::rel_dyn_section(Layout* layout)
786{
787 if (this->rel_dyn_ == NULL)
788 {
789 gold_assert(layout != NULL);
d98bc257 790 this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
5a6f7e2d 791 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
22f0da72
ILT
792 elfcpp::SHF_ALLOC, this->rel_dyn_,
793 ORDER_DYNAMIC_RELOCS, false);
5a6f7e2d
ILT
794 }
795 return this->rel_dyn_;
796}
797
67181c72
ILT
798// Get the section to use for IRELATIVE relocs, creating it if
799// necessary. These go in .rel.dyn, but only after all other dynamic
800// relocations. They need to follow the other dynamic relocations so
801// that they can refer to global variables initialized by those
802// relocs.
803
804Target_i386::Reloc_section*
805Target_i386::rel_irelative_section(Layout* layout)
806{
807 if (this->rel_irelative_ == NULL)
808 {
809 // Make sure we have already create the dynamic reloc section.
810 this->rel_dyn_section(layout);
811 this->rel_irelative_ = new Reloc_section(false);
812 layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
813 elfcpp::SHF_ALLOC, this->rel_irelative_,
814 ORDER_DYNAMIC_RELOCS, false);
815 gold_assert(this->rel_dyn_->output_section()
816 == this->rel_irelative_->output_section());
817 }
818 return this->rel_irelative_;
819}
820
a3ad94ed
ILT
821// Create the PLT section. The ordinary .got section is an argument,
822// since we need to refer to the start. We also create our own .got
823// section just for PLT entries.
824
67181c72
ILT
825Output_data_plt_i386::Output_data_plt_i386(Layout* layout,
826 Output_data_space* got_plt,
827 Output_data_space* got_irelative)
7d172687
ILT
828 : Output_section_data(16), layout_(layout), tls_desc_rel_(NULL),
829 irelative_rel_(NULL), got_plt_(got_plt), got_irelative_(got_irelative),
830 count_(0), irelative_count_(0), global_ifuncs_(), local_ifuncs_()
a3ad94ed 831{
d98bc257 832 this->rel_ = new Reloc_section(false);
a3ad94ed 833 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
22f0da72
ILT
834 elfcpp::SHF_ALLOC, this->rel_,
835 ORDER_DYNAMIC_PLT_RELOCS, false);
7223e9ca 836
07a60597
ILT
837 // Add unwind information if requested.
838 if (parameters->options().ld_generated_unwind_info())
839 layout->add_eh_frame_for_plt(this, plt_eh_frame_cie, plt_eh_frame_cie_size,
840 plt_eh_frame_fde, plt_eh_frame_fde_size);
a3ad94ed
ILT
841}
842
16649710
ILT
843void
844Output_data_plt_i386::do_adjust_output_section(Output_section* os)
845{
846 // UnixWare sets the entsize of .plt to 4, and so does the old GNU
847 // linker, and so do we.
848 os->set_entsize(4);
849}
850
a3ad94ed
ILT
851// Add an entry to the PLT.
852
853void
67181c72
ILT
854Output_data_plt_i386::add_entry(Symbol_table* symtab, Layout* layout,
855 Symbol* gsym)
a3ad94ed
ILT
856{
857 gold_assert(!gsym->has_plt_offset());
858
a3ad94ed 859 // Every PLT entry needs a reloc.
7223e9ca
ILT
860 if (gsym->type() == elfcpp::STT_GNU_IFUNC
861 && gsym->can_use_relative_reloc(false))
862 {
67181c72
ILT
863 gsym->set_plt_offset(this->irelative_count_ * plt_entry_size);
864 ++this->irelative_count_;
865 section_offset_type got_offset =
866 this->got_irelative_->current_data_size();
867 this->got_irelative_->set_current_data_size(got_offset + 4);
868 Reloc_section* rel = this->rel_irelative(symtab, layout);
869 rel->add_symbolless_global_addend(gsym, elfcpp::R_386_IRELATIVE,
870 this->got_irelative_, got_offset);
7223e9ca
ILT
871 struct Global_ifunc gi;
872 gi.sym = gsym;
873 gi.got_offset = got_offset;
874 this->global_ifuncs_.push_back(gi);
875 }
876 else
877 {
67181c72
ILT
878 // When setting the PLT offset we skip the initial reserved PLT
879 // entry.
880 gsym->set_plt_offset((this->count_ + 1) * plt_entry_size);
881
882 ++this->count_;
883
884 section_offset_type got_offset = this->got_plt_->current_data_size();
885
886 // Every PLT entry needs a GOT entry which points back to the
887 // PLT entry (this will be changed by the dynamic linker,
888 // normally lazily when the function is called).
889 this->got_plt_->set_current_data_size(got_offset + 4);
890
7223e9ca
ILT
891 gsym->set_needs_dynsym_entry();
892 this->rel_->add_global(gsym, elfcpp::R_386_JUMP_SLOT, this->got_plt_,
893 got_offset);
894 }
a3ad94ed
ILT
895
896 // Note that we don't need to save the symbol. The contents of the
897 // PLT are independent of which symbols are used. The symbols only
898 // appear in the relocations.
899}
900
7223e9ca
ILT
901// Add an entry to the PLT for a local STT_GNU_IFUNC symbol. Return
902// the PLT offset.
903
904unsigned int
6fa2a40b 905Output_data_plt_i386::add_local_ifunc_entry(
67181c72
ILT
906 Symbol_table* symtab,
907 Layout* layout,
6fa2a40b
CC
908 Sized_relobj_file<32, false>* relobj,
909 unsigned int local_sym_index)
7223e9ca 910{
67181c72
ILT
911 unsigned int plt_offset = this->irelative_count_ * plt_entry_size;
912 ++this->irelative_count_;
7223e9ca 913
67181c72 914 section_offset_type got_offset = this->got_irelative_->current_data_size();
7223e9ca
ILT
915
916 // Every PLT entry needs a GOT entry which points back to the PLT
917 // entry.
67181c72 918 this->got_irelative_->set_current_data_size(got_offset + 4);
7223e9ca
ILT
919
920 // Every PLT entry needs a reloc.
67181c72
ILT
921 Reloc_section* rel = this->rel_irelative(symtab, layout);
922 rel->add_symbolless_local_addend(relobj, local_sym_index,
923 elfcpp::R_386_IRELATIVE,
924 this->got_irelative_, got_offset);
7223e9ca
ILT
925
926 struct Local_ifunc li;
927 li.object = relobj;
928 li.local_sym_index = local_sym_index;
929 li.got_offset = got_offset;
930 this->local_ifuncs_.push_back(li);
931
932 return plt_offset;
933}
934
e291e7b9
ILT
935// Return where the TLS_DESC relocations should go, creating it if
936// necessary. These follow the JUMP_SLOT relocations.
937
938Output_data_plt_i386::Reloc_section*
939Output_data_plt_i386::rel_tls_desc(Layout* layout)
940{
941 if (this->tls_desc_rel_ == NULL)
942 {
943 this->tls_desc_rel_ = new Reloc_section(false);
944 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
945 elfcpp::SHF_ALLOC, this->tls_desc_rel_,
22f0da72 946 ORDER_DYNAMIC_PLT_RELOCS, false);
67181c72
ILT
947 gold_assert(this->tls_desc_rel_->output_section()
948 == this->rel_->output_section());
e291e7b9
ILT
949 }
950 return this->tls_desc_rel_;
951}
952
67181c72
ILT
953// Return where the IRELATIVE relocations should go in the PLT. These
954// follow the JUMP_SLOT and TLS_DESC relocations.
955
956Output_data_plt_i386::Reloc_section*
957Output_data_plt_i386::rel_irelative(Symbol_table* symtab, Layout* layout)
958{
959 if (this->irelative_rel_ == NULL)
960 {
961 // Make sure we have a place for the TLS_DESC relocations, in
962 // case we see any later on.
963 this->rel_tls_desc(layout);
964 this->irelative_rel_ = new Reloc_section(false);
965 layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
966 elfcpp::SHF_ALLOC, this->irelative_rel_,
967 ORDER_DYNAMIC_PLT_RELOCS, false);
968 gold_assert(this->irelative_rel_->output_section()
969 == this->rel_->output_section());
970
971 if (parameters->doing_static_link())
972 {
973 // A statically linked executable will only have a .rel.plt
974 // section to hold R_386_IRELATIVE relocs for STT_GNU_IFUNC
975 // symbols. The library will use these symbols to locate
976 // the IRELATIVE relocs at program startup time.
977 symtab->define_in_output_data("__rel_iplt_start", NULL,
978 Symbol_table::PREDEFINED,
979 this->irelative_rel_, 0, 0,
980 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
981 elfcpp::STV_HIDDEN, 0, false, true);
982 symtab->define_in_output_data("__rel_iplt_end", NULL,
983 Symbol_table::PREDEFINED,
984 this->irelative_rel_, 0, 0,
985 elfcpp::STT_NOTYPE, elfcpp::STB_GLOBAL,
986 elfcpp::STV_HIDDEN, 0, true, true);
987 }
988 }
989 return this->irelative_rel_;
990}
991
992// Return the PLT address to use for a global symbol.
993
994uint64_t
995Output_data_plt_i386::address_for_global(const Symbol* gsym)
996{
997 uint64_t offset = 0;
998 if (gsym->type() == elfcpp::STT_GNU_IFUNC
999 && gsym->can_use_relative_reloc(false))
1000 offset = (this->count_ + 1) * plt_entry_size;
1001 return this->address() + offset;
1002}
1003
1004// Return the PLT address to use for a local symbol. These are always
1005// IRELATIVE relocs.
1006
1007uint64_t
1008Output_data_plt_i386::address_for_local(const Relobj*, unsigned int)
1009{
1010 return this->address() + (this->count_ + 1) * plt_entry_size;
1011}
1012
a3ad94ed
ILT
1013// The first entry in the PLT for an executable.
1014
07a60597 1015const unsigned char Output_data_plt_i386::exec_first_plt_entry[plt_entry_size] =
a3ad94ed
ILT
1016{
1017 0xff, 0x35, // pushl contents of memory address
1018 0, 0, 0, 0, // replaced with address of .got + 4
1019 0xff, 0x25, // jmp indirect
1020 0, 0, 0, 0, // replaced with address of .got + 8
1021 0, 0, 0, 0 // unused
1022};
1023
1024// The first entry in the PLT for a shared object.
1025
07a60597 1026const unsigned char Output_data_plt_i386::dyn_first_plt_entry[plt_entry_size] =
a3ad94ed
ILT
1027{
1028 0xff, 0xb3, 4, 0, 0, 0, // pushl 4(%ebx)
1029 0xff, 0xa3, 8, 0, 0, 0, // jmp *8(%ebx)
1030 0, 0, 0, 0 // unused
1031};
1032
1033// Subsequent entries in the PLT for an executable.
1034
07a60597 1035const unsigned char Output_data_plt_i386::exec_plt_entry[plt_entry_size] =
a3ad94ed
ILT
1036{
1037 0xff, 0x25, // jmp indirect
1038 0, 0, 0, 0, // replaced with address of symbol in .got
1039 0x68, // pushl immediate
1040 0, 0, 0, 0, // replaced with offset into relocation table
1041 0xe9, // jmp relative
1042 0, 0, 0, 0 // replaced with offset to start of .plt
1043};
1044
1045// Subsequent entries in the PLT for a shared object.
1046
07a60597 1047const unsigned char Output_data_plt_i386::dyn_plt_entry[plt_entry_size] =
a3ad94ed
ILT
1048{
1049 0xff, 0xa3, // jmp *offset(%ebx)
1050 0, 0, 0, 0, // replaced with offset of symbol in .got
1051 0x68, // pushl immediate
1052 0, 0, 0, 0, // replaced with offset into relocation table
1053 0xe9, // jmp relative
1054 0, 0, 0, 0 // replaced with offset to start of .plt
1055};
1056
07a60597
ILT
1057// The .eh_frame unwind information for the PLT.
1058
1059const unsigned char
1060Output_data_plt_i386::plt_eh_frame_cie[plt_eh_frame_cie_size] =
1061{
1062 1, // CIE version.
1063 'z', // Augmentation: augmentation size included.
1064 'R', // Augmentation: FDE encoding included.
1065 '\0', // End of augmentation string.
1066 1, // Code alignment factor.
1067 0x7c, // Data alignment factor.
1068 8, // Return address column.
1069 1, // Augmentation size.
1070 (elfcpp::DW_EH_PE_pcrel // FDE encoding.
1071 | elfcpp::DW_EH_PE_sdata4),
1072 elfcpp::DW_CFA_def_cfa, 4, 4, // DW_CFA_def_cfa: r4 (esp) ofs 4.
1073 elfcpp::DW_CFA_offset + 8, 1, // DW_CFA_offset: r8 (eip) at cfa-4.
1074 elfcpp::DW_CFA_nop, // Align to 16 bytes.
1075 elfcpp::DW_CFA_nop
1076};
1077
1078const unsigned char
1079Output_data_plt_i386::plt_eh_frame_fde[plt_eh_frame_fde_size] =
1080{
1081 0, 0, 0, 0, // Replaced with offset to .plt.
1082 0, 0, 0, 0, // Replaced with size of .plt.
1083 0, // Augmentation size.
1084 elfcpp::DW_CFA_def_cfa_offset, 8, // DW_CFA_def_cfa_offset: 8.
1085 elfcpp::DW_CFA_advance_loc + 6, // Advance 6 to __PLT__ + 6.
1086 elfcpp::DW_CFA_def_cfa_offset, 12, // DW_CFA_def_cfa_offset: 12.
1087 elfcpp::DW_CFA_advance_loc + 10, // Advance 10 to __PLT__ + 16.
1088 elfcpp::DW_CFA_def_cfa_expression, // DW_CFA_def_cfa_expression.
1089 11, // Block length.
1090 elfcpp::DW_OP_breg4, 4, // Push %esp + 4.
1091 elfcpp::DW_OP_breg8, 0, // Push %eip.
1092 elfcpp::DW_OP_lit15, // Push 0xf.
1093 elfcpp::DW_OP_and, // & (%eip & 0xf).
1094 elfcpp::DW_OP_lit11, // Push 0xb.
1095 elfcpp::DW_OP_ge, // >= ((%eip & 0xf) >= 0xb)
1096 elfcpp::DW_OP_lit2, // Push 2.
1097 elfcpp::DW_OP_shl, // << (((%eip & 0xf) >= 0xb) << 2)
491f21ca 1098 elfcpp::DW_OP_plus, // + ((((%eip&0xf)>=0xb)<<2)+%esp+4
07a60597
ILT
1099 elfcpp::DW_CFA_nop, // Align to 32 bytes.
1100 elfcpp::DW_CFA_nop,
1101 elfcpp::DW_CFA_nop,
1102 elfcpp::DW_CFA_nop
1103};
1104
a3ad94ed
ILT
1105// Write out the PLT. This uses the hand-coded instructions above,
1106// and adjusts them as needed. This is all specified by the i386 ELF
1107// Processor Supplement.
1108
1109void
1110Output_data_plt_i386::do_write(Output_file* of)
1111{
2ea97941 1112 const off_t offset = this->offset();
fe8718a4
ILT
1113 const section_size_type oview_size =
1114 convert_to_section_size_type(this->data_size());
2ea97941 1115 unsigned char* const oview = of->get_output_view(offset, oview_size);
a3ad94ed
ILT
1116
1117 const off_t got_file_offset = this->got_plt_->offset();
67181c72
ILT
1118 gold_assert(parameters->incremental_update()
1119 || (got_file_offset + this->got_plt_->data_size()
1120 == this->got_irelative_->offset()));
fe8718a4 1121 const section_size_type got_size =
67181c72
ILT
1122 convert_to_section_size_type(this->got_plt_->data_size()
1123 + this->got_irelative_->data_size());
a3ad94ed
ILT
1124 unsigned char* const got_view = of->get_output_view(got_file_offset,
1125 got_size);
1126
1127 unsigned char* pov = oview;
1128
1129 elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1130 elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1131
374ad285 1132 if (parameters->options().output_is_position_independent())
a3ad94ed
ILT
1133 memcpy(pov, dyn_first_plt_entry, plt_entry_size);
1134 else
1135 {
1136 memcpy(pov, exec_first_plt_entry, plt_entry_size);
1137 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_address + 4);
1138 elfcpp::Swap<32, false>::writeval(pov + 8, got_address + 8);
1139 }
1140 pov += plt_entry_size;
1141
1142 unsigned char* got_pov = got_view;
1143
7d172687
ILT
1144 // The first entry in the GOT is the address of the .dynamic section
1145 // aka the PT_DYNAMIC segment. The next two entries are reserved.
1146 // We saved space for them when we created the section in
1147 // Target_i386::got_section.
1148 Output_section* dynamic = this->layout_->dynamic_section();
1149 uint32_t dynamic_addr = dynamic == NULL ? 0 : dynamic->address();
1150 elfcpp::Swap<32, false>::writeval(got_pov, dynamic_addr);
1151 got_pov += 4;
1152 memset(got_pov, 0, 8);
1153 got_pov += 8;
a3ad94ed
ILT
1154
1155 const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1156
1157 unsigned int plt_offset = plt_entry_size;
1158 unsigned int plt_rel_offset = 0;
1159 unsigned int got_offset = 12;
67181c72 1160 const unsigned int count = this->count_ + this->irelative_count_;
a3ad94ed
ILT
1161 for (unsigned int i = 0;
1162 i < count;
1163 ++i,
1164 pov += plt_entry_size,
1165 got_pov += 4,
1166 plt_offset += plt_entry_size,
1167 plt_rel_offset += rel_size,
1168 got_offset += 4)
1169 {
1170 // Set and adjust the PLT entry itself.
1171
374ad285 1172 if (parameters->options().output_is_position_independent())
a3ad94ed
ILT
1173 {
1174 memcpy(pov, dyn_plt_entry, plt_entry_size);
1175 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2, got_offset);
1176 }
1177 else
1178 {
1179 memcpy(pov, exec_plt_entry, plt_entry_size);
1180 elfcpp::Swap_unaligned<32, false>::writeval(pov + 2,
1181 (got_address
1182 + got_offset));
1183 }
1184
1185 elfcpp::Swap_unaligned<32, false>::writeval(pov + 7, plt_rel_offset);
1186 elfcpp::Swap<32, false>::writeval(pov + 12,
1187 - (plt_offset + plt_entry_size));
1188
1189 // Set the entry in the GOT.
1190 elfcpp::Swap<32, false>::writeval(got_pov, plt_address + plt_offset + 6);
1191 }
1192
7223e9ca
ILT
1193 // If any STT_GNU_IFUNC symbols have PLT entries, we need to change
1194 // the GOT to point to the actual symbol value, rather than point to
1195 // the PLT entry. That will let the dynamic linker call the right
1196 // function when resolving IRELATIVE relocations.
67181c72 1197 unsigned char* got_irelative_view = got_view + this->got_plt_->data_size();
7223e9ca
ILT
1198 for (std::vector<Global_ifunc>::const_iterator p =
1199 this->global_ifuncs_.begin();
1200 p != this->global_ifuncs_.end();
1201 ++p)
1202 {
1203 const Sized_symbol<32>* ssym =
1204 static_cast<const Sized_symbol<32>*>(p->sym);
67181c72 1205 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
7223e9ca
ILT
1206 ssym->value());
1207 }
1208
1209 for (std::vector<Local_ifunc>::const_iterator p =
1210 this->local_ifuncs_.begin();
1211 p != this->local_ifuncs_.end();
1212 ++p)
1213 {
1214 const Symbol_value<32>* psymval =
1215 p->object->local_symbol(p->local_sym_index);
67181c72 1216 elfcpp::Swap<32, false>::writeval(got_irelative_view + p->got_offset,
7223e9ca
ILT
1217 psymval->value(p->object, 0));
1218 }
1219
fe8718a4
ILT
1220 gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1221 gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
a3ad94ed 1222
2ea97941 1223 of->write_output_view(offset, oview_size, oview);
a3ad94ed
ILT
1224 of->write_output_view(got_file_offset, got_size, got_view);
1225}
1226
7223e9ca 1227// Create the PLT section.
a3ad94ed
ILT
1228
1229void
7223e9ca 1230Target_i386::make_plt_section(Symbol_table* symtab, Layout* layout)
a3ad94ed 1231{
a3ad94ed
ILT
1232 if (this->plt_ == NULL)
1233 {
1234 // Create the GOT sections first.
7e1edb90 1235 this->got_section(symtab, layout);
a3ad94ed 1236
67181c72
ILT
1237 this->plt_ = new Output_data_plt_i386(layout, this->got_plt_,
1238 this->got_irelative_);
16649710
ILT
1239 layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1240 (elfcpp::SHF_ALLOC
1241 | elfcpp::SHF_EXECINSTR),
22f0da72 1242 this->plt_, ORDER_PLT, false);
7223e9ca
ILT
1243
1244 // Make the sh_info field of .rel.plt point to .plt.
1245 Output_section* rel_plt_os = this->plt_->rel_plt()->output_section();
1246 rel_plt_os->set_info_section(this->plt_->output_section());
a3ad94ed 1247 }
7223e9ca 1248}
a3ad94ed 1249
7223e9ca
ILT
1250// Create a PLT entry for a global symbol.
1251
1252void
1253Target_i386::make_plt_entry(Symbol_table* symtab, Layout* layout, Symbol* gsym)
1254{
1255 if (gsym->has_plt_offset())
1256 return;
1257 if (this->plt_ == NULL)
1258 this->make_plt_section(symtab, layout);
67181c72 1259 this->plt_->add_entry(symtab, layout, gsym);
a3ad94ed
ILT
1260}
1261
7223e9ca
ILT
1262// Make a PLT entry for a local STT_GNU_IFUNC symbol.
1263
1264void
1265Target_i386::make_local_ifunc_plt_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 1266 Sized_relobj_file<32, false>* relobj,
7223e9ca
ILT
1267 unsigned int local_sym_index)
1268{
1269 if (relobj->local_has_plt_offset(local_sym_index))
1270 return;
1271 if (this->plt_ == NULL)
1272 this->make_plt_section(symtab, layout);
67181c72
ILT
1273 unsigned int plt_offset = this->plt_->add_local_ifunc_entry(symtab, layout,
1274 relobj,
7223e9ca
ILT
1275 local_sym_index);
1276 relobj->set_local_plt_offset(local_sym_index, plt_offset);
1277}
1278
0e70b911
CC
1279// Return the number of entries in the PLT.
1280
1281unsigned int
1282Target_i386::plt_entry_count() const
1283{
1284 if (this->plt_ == NULL)
1285 return 0;
1286 return this->plt_->entry_count();
1287}
1288
1289// Return the offset of the first non-reserved PLT entry.
1290
1291unsigned int
1292Target_i386::first_plt_entry_offset() const
1293{
1294 return Output_data_plt_i386::first_plt_entry_offset();
1295}
1296
1297// Return the size of each PLT entry.
1298
1299unsigned int
1300Target_i386::plt_entry_size() const
1301{
1302 return Output_data_plt_i386::get_plt_entry_size();
1303}
1304
e291e7b9
ILT
1305// Get the section to use for TLS_DESC relocations.
1306
1307Target_i386::Reloc_section*
1308Target_i386::rel_tls_desc_section(Layout* layout) const
1309{
1310 return this->plt_section()->rel_tls_desc(layout);
1311}
1312
9fa33bee 1313// Define the _TLS_MODULE_BASE_ symbol in the TLS segment.
edfbb029
CC
1314
1315void
1316Target_i386::define_tls_base_symbol(Symbol_table* symtab, Layout* layout)
1317{
1318 if (this->tls_base_symbol_defined_)
1319 return;
1320
1321 Output_segment* tls_segment = layout->tls_segment();
1322 if (tls_segment != NULL)
1323 {
183fd0e3 1324 bool is_exec = parameters->options().output_is_executable();
edfbb029 1325 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
99fff23b 1326 Symbol_table::PREDEFINED,
edfbb029
CC
1327 tls_segment, 0, 0,
1328 elfcpp::STT_TLS,
1329 elfcpp::STB_LOCAL,
1330 elfcpp::STV_HIDDEN, 0,
183fd0e3
AO
1331 (is_exec
1332 ? Symbol::SEGMENT_END
1333 : Symbol::SEGMENT_START),
1334 true);
edfbb029
CC
1335 }
1336 this->tls_base_symbol_defined_ = true;
1337}
1338
94c4710f
ILT
1339// Create a GOT entry for the TLS module index.
1340
1341unsigned int
1342Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
6fa2a40b 1343 Sized_relobj_file<32, false>* object)
94c4710f
ILT
1344{
1345 if (this->got_mod_index_offset_ == -1U)
1346 {
1347 gold_assert(symtab != NULL && layout != NULL && object != NULL);
1348 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
1349 Output_data_got<32, false>* got = this->got_section(symtab, layout);
1350 unsigned int got_offset = got->add_constant(0);
1351 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
1352 got_offset);
009a67a2 1353 got->add_constant(0);
94c4710f
ILT
1354 this->got_mod_index_offset_ = got_offset;
1355 }
1356 return this->got_mod_index_offset_;
1357}
1358
92e059d8 1359// Optimize the TLS relocation type based on what we know about the
a3ad94ed
ILT
1360// symbol. IS_FINAL is true if the final address of this symbol is
1361// known at link time.
92e059d8 1362
af6359d5 1363tls::Tls_optimization
7e1edb90 1364Target_i386::optimize_tls_reloc(bool is_final, int r_type)
92e059d8
ILT
1365{
1366 // If we are generating a shared library, then we can't do anything
1367 // in the linker.
8851ecca 1368 if (parameters->options().shared())
af6359d5 1369 return tls::TLSOPT_NONE;
92e059d8
ILT
1370
1371 switch (r_type)
1372 {
1373 case elfcpp::R_386_TLS_GD:
1374 case elfcpp::R_386_TLS_GOTDESC:
1375 case elfcpp::R_386_TLS_DESC_CALL:
e041f13d 1376 // These are General-Dynamic which permits fully general TLS
92e059d8
ILT
1377 // access. Since we know that we are generating an executable,
1378 // we can convert this to Initial-Exec. If we also know that
1379 // this is a local symbol, we can further switch to Local-Exec.
a3ad94ed 1380 if (is_final)
af6359d5
ILT
1381 return tls::TLSOPT_TO_LE;
1382 return tls::TLSOPT_TO_IE;
92e059d8
ILT
1383
1384 case elfcpp::R_386_TLS_LDM:
1385 // This is Local-Dynamic, which refers to a local symbol in the
1386 // dynamic TLS block. Since we know that we generating an
1387 // executable, we can switch to Local-Exec.
af6359d5 1388 return tls::TLSOPT_TO_LE;
92e059d8
ILT
1389
1390 case elfcpp::R_386_TLS_LDO_32:
af6359d5
ILT
1391 // Another type of Local-Dynamic relocation.
1392 return tls::TLSOPT_TO_LE;
92e059d8
ILT
1393
1394 case elfcpp::R_386_TLS_IE:
1395 case elfcpp::R_386_TLS_GOTIE:
1396 case elfcpp::R_386_TLS_IE_32:
1397 // These are Initial-Exec relocs which get the thread offset
1398 // from the GOT. If we know that we are linking against the
1399 // local symbol, we can switch to Local-Exec, which links the
1400 // thread offset into the instruction.
a3ad94ed 1401 if (is_final)
af6359d5
ILT
1402 return tls::TLSOPT_TO_LE;
1403 return tls::TLSOPT_NONE;
8462ae85 1404
92e059d8
ILT
1405 case elfcpp::R_386_TLS_LE:
1406 case elfcpp::R_386_TLS_LE_32:
1407 // When we already have Local-Exec, there is nothing further we
1408 // can do.
af6359d5 1409 return tls::TLSOPT_NONE;
92e059d8
ILT
1410
1411 default:
a3ad94ed 1412 gold_unreachable();
92e059d8
ILT
1413 }
1414}
1415
95a2c8d6 1416// Get the Reference_flags for a particular relocation.
af6359d5 1417
95a2c8d6
RS
1418int
1419Target_i386::Scan::get_reference_flags(unsigned int r_type)
7223e9ca
ILT
1420{
1421 switch (r_type)
1422 {
1423 case elfcpp::R_386_NONE:
1424 case elfcpp::R_386_GNU_VTINHERIT:
1425 case elfcpp::R_386_GNU_VTENTRY:
95a2c8d6
RS
1426 case elfcpp::R_386_GOTPC:
1427 // No symbol reference.
1428 return 0;
7223e9ca
ILT
1429
1430 case elfcpp::R_386_32:
1431 case elfcpp::R_386_16:
1432 case elfcpp::R_386_8:
95a2c8d6
RS
1433 return Symbol::ABSOLUTE_REF;
1434
7223e9ca
ILT
1435 case elfcpp::R_386_PC32:
1436 case elfcpp::R_386_PC16:
1437 case elfcpp::R_386_PC8:
7223e9ca 1438 case elfcpp::R_386_GOTOFF:
95a2c8d6
RS
1439 return Symbol::RELATIVE_REF;
1440
1441 case elfcpp::R_386_PLT32:
1442 return Symbol::FUNCTION_CALL | Symbol::RELATIVE_REF;
1443
7223e9ca 1444 case elfcpp::R_386_GOT32:
95a2c8d6
RS
1445 // Absolute in GOT.
1446 return Symbol::ABSOLUTE_REF;
1447
1448 case elfcpp::R_386_TLS_GD: // Global-dynamic
1449 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1450 case elfcpp::R_386_TLS_DESC_CALL:
1451 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1452 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1453 case elfcpp::R_386_TLS_IE: // Initial-exec
1454 case elfcpp::R_386_TLS_IE_32:
1455 case elfcpp::R_386_TLS_GOTIE:
1456 case elfcpp::R_386_TLS_LE: // Local-exec
1457 case elfcpp::R_386_TLS_LE_32:
1458 return Symbol::TLS_REF;
7223e9ca
ILT
1459
1460 case elfcpp::R_386_COPY:
1461 case elfcpp::R_386_GLOB_DAT:
1462 case elfcpp::R_386_JUMP_SLOT:
1463 case elfcpp::R_386_RELATIVE:
1464 case elfcpp::R_386_IRELATIVE:
1465 case elfcpp::R_386_TLS_TPOFF:
1466 case elfcpp::R_386_TLS_DTPMOD32:
1467 case elfcpp::R_386_TLS_DTPOFF32:
1468 case elfcpp::R_386_TLS_TPOFF32:
1469 case elfcpp::R_386_TLS_DESC:
7223e9ca
ILT
1470 case elfcpp::R_386_32PLT:
1471 case elfcpp::R_386_TLS_GD_32:
1472 case elfcpp::R_386_TLS_GD_PUSH:
1473 case elfcpp::R_386_TLS_GD_CALL:
1474 case elfcpp::R_386_TLS_GD_POP:
1475 case elfcpp::R_386_TLS_LDM_32:
1476 case elfcpp::R_386_TLS_LDM_PUSH:
1477 case elfcpp::R_386_TLS_LDM_CALL:
1478 case elfcpp::R_386_TLS_LDM_POP:
1479 case elfcpp::R_386_USED_BY_INTEL_200:
1480 default:
95a2c8d6
RS
1481 // Not expected. We will give an error later.
1482 return 0;
7223e9ca
ILT
1483 }
1484}
1485
95a2c8d6
RS
1486// Report an unsupported relocation against a local symbol.
1487
1488void
6fa2a40b 1489Target_i386::Scan::unsupported_reloc_local(Sized_relobj_file<32, false>* object,
95a2c8d6
RS
1490 unsigned int r_type)
1491{
1492 gold_error(_("%s: unsupported reloc %u against local symbol"),
1493 object->name().c_str(), r_type);
1494}
1495
1496// Return whether we need to make a PLT entry for a relocation of a
1497// given type against a STT_GNU_IFUNC symbol.
1498
1499bool
6fa2a40b
CC
1500Target_i386::Scan::reloc_needs_plt_for_ifunc(
1501 Sized_relobj_file<32, false>* object,
1502 unsigned int r_type)
95a2c8d6
RS
1503{
1504 int flags = Scan::get_reference_flags(r_type);
1505 if (flags & Symbol::TLS_REF)
1506 gold_error(_("%s: unsupported TLS reloc %u for IFUNC symbol"),
1507 object->name().c_str(), r_type);
1508 return flags != 0;
1509}
1510
92e059d8
ILT
1511// Scan a relocation for a local symbol.
1512
1513inline void
ad0f2072 1514Target_i386::Scan::local(Symbol_table* symtab,
ead1e424
ILT
1515 Layout* layout,
1516 Target_i386* target,
6fa2a40b 1517 Sized_relobj_file<32, false>* object,
1b64748b 1518 unsigned int data_shndx,
07f397ab 1519 Output_section* output_section,
1b64748b 1520 const elfcpp::Rel<32, false>& reloc,
a3ad94ed 1521 unsigned int r_type,
7bf1f802 1522 const elfcpp::Sym<32, false>& lsym)
92e059d8 1523{
7223e9ca
ILT
1524 // A local STT_GNU_IFUNC symbol may require a PLT entry.
1525 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC
1526 && this->reloc_needs_plt_for_ifunc(object, r_type))
1527 {
1528 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1529 target->make_local_ifunc_plt_entry(symtab, layout, object, r_sym);
1530 }
1531
92e059d8
ILT
1532 switch (r_type)
1533 {
1534 case elfcpp::R_386_NONE:
1535 case elfcpp::R_386_GNU_VTINHERIT:
1536 case elfcpp::R_386_GNU_VTENTRY:
1537 break;
1538
1539 case elfcpp::R_386_32:
436ca963
ILT
1540 // If building a shared library (or a position-independent
1541 // executable), we need to create a dynamic relocation for
1542 // this location. The relocation applied at link time will
1543 // apply the link-time value, so we flag the location with
1544 // an R_386_RELATIVE relocation so the dynamic loader can
1545 // relocate it easily.
8851ecca 1546 if (parameters->options().output_is_position_independent())
436ca963
ILT
1547 {
1548 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3 1549 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7223e9ca
ILT
1550 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
1551 output_section, data_shndx,
1552 reloc.get_r_offset());
d61c6bd4
ILT
1553 }
1554 break;
1555
1556 case elfcpp::R_386_16:
1557 case elfcpp::R_386_8:
1558 // If building a shared library (or a position-independent
1559 // executable), we need to create a dynamic relocation for
1560 // this location. Because the addend needs to remain in the
1561 // data section, we need to be careful not to apply this
1562 // relocation statically.
8851ecca 1563 if (parameters->options().output_is_position_independent())
d61c6bd4
ILT
1564 {
1565 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
d491d34e 1566 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
dceae3c1 1567 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
1568 rel_dyn->add_local(object, r_sym, r_type, output_section,
1569 data_shndx, reloc.get_r_offset());
dceae3c1
ILT
1570 else
1571 {
1572 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
1573 unsigned int shndx = lsym.get_st_shndx();
1574 bool is_ordinary;
1575 shndx = object->adjust_sym_shndx(r_sym, shndx,
1576 &is_ordinary);
1577 if (!is_ordinary)
1578 object->error(_("section symbol %u has bad shndx %u"),
1579 r_sym, shndx);
1580 else
1581 rel_dyn->add_local_section(object, shndx,
1582 r_type, output_section,
1583 data_shndx, reloc.get_r_offset());
dceae3c1 1584 }
436ca963 1585 }
92e059d8
ILT
1586 break;
1587
1588 case elfcpp::R_386_PC32:
1589 case elfcpp::R_386_PC16:
1590 case elfcpp::R_386_PC8:
1591 break;
1592
df2efe71
ILT
1593 case elfcpp::R_386_PLT32:
1594 // Since we know this is a local symbol, we can handle this as a
1595 // PC32 reloc.
1596 break;
1597
ead1e424
ILT
1598 case elfcpp::R_386_GOTOFF:
1599 case elfcpp::R_386_GOTPC:
1600 // We need a GOT section.
7e1edb90 1601 target->got_section(symtab, layout);
ead1e424
ILT
1602 break;
1603
1b64748b
ILT
1604 case elfcpp::R_386_GOT32:
1605 {
1606 // The symbol requires a GOT entry.
1607 Output_data_got<32, false>* got = target->got_section(symtab, layout);
1608 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7223e9ca
ILT
1609
1610 // For a STT_GNU_IFUNC symbol we want the PLT offset. That
1611 // lets function pointers compare correctly with shared
1612 // libraries. Otherwise we would need an IRELATIVE reloc.
1613 bool is_new;
1614 if (lsym.get_st_type() == elfcpp::STT_GNU_IFUNC)
1615 is_new = got->add_local_plt(object, r_sym, GOT_TYPE_STANDARD);
1616 else
1617 is_new = got->add_local(object, r_sym, GOT_TYPE_STANDARD);
1618 if (is_new)
1b64748b
ILT
1619 {
1620 // If we are generating a shared object, we need to add a
7bf1f802 1621 // dynamic RELATIVE relocation for this symbol's GOT entry.
8851ecca 1622 if (parameters->options().output_is_position_independent())
1b64748b
ILT
1623 {
1624 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7223e9ca
ILT
1625 unsigned int got_offset =
1626 object->local_got_offset(r_sym, GOT_TYPE_STANDARD);
1627 rel_dyn->add_local_relative(object, r_sym,
1628 elfcpp::R_386_RELATIVE,
1629 got, got_offset);
1b64748b
ILT
1630 }
1631 }
1632 }
1633 break;
1634
af6359d5
ILT
1635 // These are relocations which should only be seen by the
1636 // dynamic linker, and should never be seen here.
92e059d8
ILT
1637 case elfcpp::R_386_COPY:
1638 case elfcpp::R_386_GLOB_DAT:
1639 case elfcpp::R_386_JUMP_SLOT:
1640 case elfcpp::R_386_RELATIVE:
7223e9ca 1641 case elfcpp::R_386_IRELATIVE:
92e059d8
ILT
1642 case elfcpp::R_386_TLS_TPOFF:
1643 case elfcpp::R_386_TLS_DTPMOD32:
1644 case elfcpp::R_386_TLS_DTPOFF32:
1645 case elfcpp::R_386_TLS_TPOFF32:
1646 case elfcpp::R_386_TLS_DESC:
a0c4fb0a 1647 gold_error(_("%s: unexpected reloc %u in object file"),
75f2446e 1648 object->name().c_str(), r_type);
92e059d8
ILT
1649 break;
1650
af6359d5 1651 // These are initial TLS relocs, which are expected when
d61c17ea 1652 // linking.
56622147
ILT
1653 case elfcpp::R_386_TLS_GD: // Global-dynamic
1654 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1655 case elfcpp::R_386_TLS_DESC_CALL:
1656 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1657 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1658 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 1659 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
1660 case elfcpp::R_386_TLS_GOTIE:
1661 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 1662 case elfcpp::R_386_TLS_LE_32:
7e1edb90 1663 {
8851ecca 1664 bool output_is_shared = parameters->options().shared();
af6359d5
ILT
1665 const tls::Tls_optimization optimized_type
1666 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
7e1edb90
ILT
1667 switch (r_type)
1668 {
56622147 1669 case elfcpp::R_386_TLS_GD: // Global-dynamic
07f397ab
ILT
1670 if (optimized_type == tls::TLSOPT_NONE)
1671 {
1672 // Create a pair of GOT entries for the module index and
1673 // dtv-relative offset.
1674 Output_data_got<32, false>* got
1675 = target->got_section(symtab, layout);
1676 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
d491d34e
ILT
1677 unsigned int shndx = lsym.get_st_shndx();
1678 bool is_ordinary;
1679 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1680 if (!is_ordinary)
1681 object->error(_("local symbol %u has bad shndx %u"),
1682 r_sym, shndx);
1683 else
1684 got->add_local_pair_with_rel(object, r_sym, shndx,
1685 GOT_TYPE_TLS_PAIR,
1686 target->rel_dyn_section(layout),
1687 elfcpp::R_386_TLS_DTPMOD32, 0);
07f397ab
ILT
1688 }
1689 else if (optimized_type != tls::TLSOPT_TO_LE)
1690 unsupported_reloc_local(object, r_type);
1691 break;
1692
56622147 1693 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
edfbb029 1694 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1695 if (optimized_type == tls::TLSOPT_NONE)
1696 {
a8df5856
ILT
1697 // Create a double GOT entry with an R_386_TLS_DESC
1698 // reloc. The R_386_TLS_DESC reloc is resolved
1699 // lazily, so the GOT entry needs to be in an area in
1700 // .got.plt, not .got. Call got_section to make sure
1701 // the section has been created.
1702 target->got_section(symtab, layout);
1703 Output_data_got<32, false>* got = target->got_tlsdesc_section();
c2b45e22 1704 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
e291e7b9
ILT
1705 if (!object->local_has_got_offset(r_sym, GOT_TYPE_TLS_DESC))
1706 {
1707 unsigned int got_offset = got->add_constant(0);
1708 // The local symbol value is stored in the second
1709 // GOT entry.
1710 got->add_local(object, r_sym, GOT_TYPE_TLS_DESC);
1711 // That set the GOT offset of the local symbol to
1712 // point to the second entry, but we want it to
1713 // point to the first.
1714 object->set_local_got_offset(r_sym, GOT_TYPE_TLS_DESC,
1715 got_offset);
1716 Reloc_section* rt = target->rel_tls_desc_section(layout);
1717 rt->add_absolute(elfcpp::R_386_TLS_DESC, got, got_offset);
1718 }
c2b45e22
CC
1719 }
1720 else if (optimized_type != tls::TLSOPT_TO_LE)
7bf1f802 1721 unsupported_reloc_local(object, r_type);
af6359d5
ILT
1722 break;
1723
c2b45e22
CC
1724 case elfcpp::R_386_TLS_DESC_CALL:
1725 break;
1726
56622147 1727 case elfcpp::R_386_TLS_LDM: // Local-dynamic
07f397ab
ILT
1728 if (optimized_type == tls::TLSOPT_NONE)
1729 {
1730 // Create a GOT entry for the module index.
94c4710f 1731 target->got_mod_index_entry(symtab, layout, object);
07f397ab
ILT
1732 }
1733 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5
ILT
1734 unsupported_reloc_local(object, r_type);
1735 break;
1736
56622147 1737 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
46cf9fa2
ILT
1738 break;
1739
56622147
ILT
1740 case elfcpp::R_386_TLS_IE: // Initial-exec
1741 case elfcpp::R_386_TLS_IE_32:
1742 case elfcpp::R_386_TLS_GOTIE:
535890bb 1743 layout->set_has_static_tls();
07f397ab
ILT
1744 if (optimized_type == tls::TLSOPT_NONE)
1745 {
7bf1f802
ILT
1746 // For the R_386_TLS_IE relocation, we need to create a
1747 // dynamic relocation when building a shared library.
1748 if (r_type == elfcpp::R_386_TLS_IE
8851ecca 1749 && parameters->options().shared())
7bf1f802
ILT
1750 {
1751 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
1752 unsigned int r_sym
1753 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1754 rel_dyn->add_local_relative(object, r_sym,
1755 elfcpp::R_386_RELATIVE,
1756 output_section, data_shndx,
1757 reloc.get_r_offset());
7bf1f802 1758 }
07f397ab
ILT
1759 // Create a GOT entry for the tp-relative offset.
1760 Output_data_got<32, false>* got
1761 = target->got_section(symtab, layout);
1762 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7bf1f802
ILT
1763 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1764 ? elfcpp::R_386_TLS_TPOFF32
1765 : elfcpp::R_386_TLS_TPOFF);
c2b45e22
CC
1766 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1767 ? GOT_TYPE_TLS_OFFSET
1768 : GOT_TYPE_TLS_NOFFSET);
1769 got->add_local_with_rel(object, r_sym, got_type,
7bf1f802
ILT
1770 target->rel_dyn_section(layout),
1771 dyn_r_type);
07f397ab
ILT
1772 }
1773 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5 1774 unsupported_reloc_local(object, r_type);
7e1edb90 1775 break;
af6359d5 1776
56622147
ILT
1777 case elfcpp::R_386_TLS_LE: // Local-exec
1778 case elfcpp::R_386_TLS_LE_32:
535890bb 1779 layout->set_has_static_tls();
07f397ab 1780 if (output_is_shared)
7bf1f802
ILT
1781 {
1782 // We need to create a dynamic relocation.
dceae3c1 1783 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7bf1f802
ILT
1784 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1785 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1786 ? elfcpp::R_386_TLS_TPOFF32
1787 : elfcpp::R_386_TLS_TPOFF);
1788 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1789 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1790 data_shndx, reloc.get_r_offset());
1791 }
56622147
ILT
1792 break;
1793
af6359d5
ILT
1794 default:
1795 gold_unreachable();
7e1edb90
ILT
1796 }
1797 }
92e059d8
ILT
1798 break;
1799
92e059d8
ILT
1800 case elfcpp::R_386_32PLT:
1801 case elfcpp::R_386_TLS_GD_32:
1802 case elfcpp::R_386_TLS_GD_PUSH:
1803 case elfcpp::R_386_TLS_GD_CALL:
1804 case elfcpp::R_386_TLS_GD_POP:
1805 case elfcpp::R_386_TLS_LDM_32:
1806 case elfcpp::R_386_TLS_LDM_PUSH:
1807 case elfcpp::R_386_TLS_LDM_CALL:
1808 case elfcpp::R_386_TLS_LDM_POP:
1809 case elfcpp::R_386_USED_BY_INTEL_200:
1810 default:
af6359d5 1811 unsupported_reloc_local(object, r_type);
92e059d8
ILT
1812 break;
1813 }
1814}
1815
af6359d5
ILT
1816// Report an unsupported relocation against a global symbol.
1817
1818void
6fa2a40b
CC
1819Target_i386::Scan::unsupported_reloc_global(
1820 Sized_relobj_file<32, false>* object,
1821 unsigned int r_type,
1822 Symbol* gsym)
af6359d5 1823{
75f2446e 1824 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1825 object->name().c_str(), r_type, gsym->demangled_name().c_str());
af6359d5
ILT
1826}
1827
0897ed3b
ST
1828inline bool
1829Target_i386::Scan::possible_function_pointer_reloc(unsigned int r_type)
1830{
1831 switch (r_type)
1832 {
1833 case elfcpp::R_386_32:
1834 case elfcpp::R_386_16:
1835 case elfcpp::R_386_8:
1836 case elfcpp::R_386_GOTOFF:
1837 case elfcpp::R_386_GOT32:
1838 {
1839 return true;
1840 }
1841 default:
1842 return false;
1843 }
1844 return false;
1845}
1846
1847inline bool
1848Target_i386::Scan::local_reloc_may_be_function_pointer(
1849 Symbol_table* ,
1850 Layout* ,
1851 Target_i386* ,
6fa2a40b 1852 Sized_relobj_file<32, false>* ,
0897ed3b
ST
1853 unsigned int ,
1854 Output_section* ,
1855 const elfcpp::Rel<32, false>& ,
1856 unsigned int r_type,
1857 const elfcpp::Sym<32, false>&)
1858{
1859 return possible_function_pointer_reloc(r_type);
1860}
1861
1862inline bool
1863Target_i386::Scan::global_reloc_may_be_function_pointer(
1864 Symbol_table* ,
1865 Layout* ,
1866 Target_i386* ,
6fa2a40b 1867 Sized_relobj_file<32, false>* ,
0897ed3b
ST
1868 unsigned int ,
1869 Output_section* ,
1870 const elfcpp::Rel<32, false>& ,
1871 unsigned int r_type,
1872 Symbol*)
1873{
1874 return possible_function_pointer_reloc(r_type);
1875}
1876
92e059d8
ILT
1877// Scan a relocation for a global symbol.
1878
1879inline void
ad0f2072 1880Target_i386::Scan::global(Symbol_table* symtab,
ead1e424
ILT
1881 Layout* layout,
1882 Target_i386* target,
6fa2a40b 1883 Sized_relobj_file<32, false>* object,
a3ad94ed 1884 unsigned int data_shndx,
07f397ab 1885 Output_section* output_section,
a3ad94ed
ILT
1886 const elfcpp::Rel<32, false>& reloc,
1887 unsigned int r_type,
92e059d8
ILT
1888 Symbol* gsym)
1889{
7223e9ca
ILT
1890 // A STT_GNU_IFUNC symbol may require a PLT entry.
1891 if (gsym->type() == elfcpp::STT_GNU_IFUNC
1892 && this->reloc_needs_plt_for_ifunc(object, r_type))
1893 target->make_plt_entry(symtab, layout, gsym);
1894
92e059d8
ILT
1895 switch (r_type)
1896 {
1897 case elfcpp::R_386_NONE:
1898 case elfcpp::R_386_GNU_VTINHERIT:
8462ae85 1899 case elfcpp::R_386_GNU_VTENTRY:
92e059d8
ILT
1900 break;
1901
1902 case elfcpp::R_386_32:
92e059d8 1903 case elfcpp::R_386_16:
92e059d8 1904 case elfcpp::R_386_8:
96f2030e 1905 {
d61c6bd4
ILT
1906 // Make a PLT entry if necessary.
1907 if (gsym->needs_plt_entry())
1908 {
1909 target->make_plt_entry(symtab, layout, gsym);
1910 // Since this is not a PC-relative relocation, we may be
1911 // taking the address of a function. In that case we need to
1912 // set the entry in the dynamic symbol table to the address of
1913 // the PLT entry.
8851ecca 1914 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1915 gsym->set_needs_dynsym_value();
1916 }
1917 // Make a dynamic relocation if necessary.
95a2c8d6 1918 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
d61c6bd4 1919 {
966d4097 1920 if (gsym->may_need_copy_reloc())
d61c6bd4 1921 {
12c0daef 1922 target->copy_reloc(symtab, layout, object,
4f4c5f80 1923 data_shndx, output_section, gsym, reloc);
d61c6bd4 1924 }
7223e9ca
ILT
1925 else if (r_type == elfcpp::R_386_32
1926 && gsym->type() == elfcpp::STT_GNU_IFUNC
1927 && gsym->can_use_relative_reloc(false)
1928 && !gsym->is_from_dynobj()
1929 && !gsym->is_undefined()
1930 && !gsym->is_preemptible())
1931 {
1932 // Use an IRELATIVE reloc for a locally defined
1933 // STT_GNU_IFUNC symbol. This makes a function
1934 // address in a PIE executable match the address in a
1935 // shared library that it links against.
67181c72 1936 Reloc_section* rel_dyn = target->rel_irelative_section(layout);
7223e9ca
ILT
1937 rel_dyn->add_symbolless_global_addend(gsym,
1938 elfcpp::R_386_IRELATIVE,
1939 output_section,
1940 object, data_shndx,
1941 reloc.get_r_offset());
1942 }
d61c6bd4
ILT
1943 else if (r_type == elfcpp::R_386_32
1944 && gsym->can_use_relative_reloc(false))
1945 {
1946 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
7223e9ca
ILT
1947 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1948 output_section, object,
1949 data_shndx, reloc.get_r_offset());
d61c6bd4
ILT
1950 }
1951 else
1952 {
96f2030e 1953 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4f4c5f80
ILT
1954 rel_dyn->add_global(gsym, r_type, output_section, object,
1955 data_shndx, reloc.get_r_offset());
d61c6bd4
ILT
1956 }
1957 }
1958 }
1959 break;
1960
1961 case elfcpp::R_386_PC32:
1962 case elfcpp::R_386_PC16:
1963 case elfcpp::R_386_PC8:
1964 {
1965 // Make a PLT entry if necessary.
1966 if (gsym->needs_plt_entry())
7bf1f802
ILT
1967 {
1968 // These relocations are used for function calls only in
1969 // non-PIC code. For a 32-bit relocation in a shared library,
1970 // we'll need a text relocation anyway, so we can skip the
1971 // PLT entry and let the dynamic linker bind the call directly
1972 // to the target. For smaller relocations, we should use a
1973 // PLT entry to ensure that the call can reach.
8851ecca 1974 if (!parameters->options().shared()
7bf1f802
ILT
1975 || r_type != elfcpp::R_386_PC32)
1976 target->make_plt_entry(symtab, layout, gsym);
1977 }
d61c6bd4 1978 // Make a dynamic relocation if necessary.
95a2c8d6 1979 if (gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type)))
96f2030e 1980 {
966d4097 1981 if (gsym->may_need_copy_reloc())
d61c6bd4 1982 {
12c0daef 1983 target->copy_reloc(symtab, layout, object,
4f4c5f80 1984 data_shndx, output_section, gsym, reloc);
d61c6bd4 1985 }
86849f1f 1986 else
d61c6bd4
ILT
1987 {
1988 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4f4c5f80
ILT
1989 rel_dyn->add_global(gsym, r_type, output_section, object,
1990 data_shndx, reloc.get_r_offset());
d61c6bd4 1991 }
96f2030e
ILT
1992 }
1993 }
92e059d8
ILT
1994 break;
1995
ead1e424 1996 case elfcpp::R_386_GOT32:
8462ae85
ILT
1997 {
1998 // The symbol requires a GOT entry.
7e1edb90 1999 Output_data_got<32, false>* got = target->got_section(symtab, layout);
7bf1f802 2000 if (gsym->final_value_is_known())
7223e9ca
ILT
2001 {
2002 // For a STT_GNU_IFUNC symbol we want the PLT address.
2003 if (gsym->type() == elfcpp::STT_GNU_IFUNC)
2004 got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2005 else
2006 got->add_global(gsym, GOT_TYPE_STANDARD);
2007 }
7bf1f802
ILT
2008 else
2009 {
8462ae85 2010 // If this symbol is not fully resolved, we need to add a
7bf1f802
ILT
2011 // GOT entry with a dynamic relocation.
2012 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
07aa62f2
ILT
2013
2014 // Use a GLOB_DAT rather than a RELATIVE reloc if:
2015 //
2016 // 1) The symbol may be defined in some other module.
2017 //
2018 // 2) We are building a shared library and this is a
2019 // protected symbol; using GLOB_DAT means that the dynamic
2020 // linker can use the address of the PLT in the main
2021 // executable when appropriate so that function address
2022 // comparisons work.
2023 //
2024 // 3) This is a STT_GNU_IFUNC symbol in position dependent
2025 // code, again so that function address comparisons work.
8fc19601
ILT
2026 if (gsym->is_from_dynobj()
2027 || gsym->is_undefined()
7223e9ca 2028 || gsym->is_preemptible()
07aa62f2
ILT
2029 || (gsym->visibility() == elfcpp::STV_PROTECTED
2030 && parameters->options().shared())
7223e9ca
ILT
2031 || (gsym->type() == elfcpp::STT_GNU_IFUNC
2032 && parameters->options().output_is_position_independent()))
0a65a3a7
CC
2033 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
2034 rel_dyn, elfcpp::R_386_GLOB_DAT);
7bf1f802 2035 else
8462ae85 2036 {
7223e9ca
ILT
2037 // For a STT_GNU_IFUNC symbol we want to write the PLT
2038 // offset into the GOT, so that function pointer
2039 // comparisons work correctly.
2040 bool is_new;
2041 if (gsym->type() != elfcpp::STT_GNU_IFUNC)
2042 is_new = got->add_global(gsym, GOT_TYPE_STANDARD);
2043 else
2044 {
2045 is_new = got->add_global_plt(gsym, GOT_TYPE_STANDARD);
2046 // Tell the dynamic linker to use the PLT address
2047 // when resolving relocations.
2048 if (gsym->is_from_dynobj()
2049 && !parameters->options().shared())
2050 gsym->set_needs_dynsym_value();
2051 }
2052 if (is_new)
2053 {
2054 unsigned int got_off = gsym->got_offset(GOT_TYPE_STANDARD);
2055 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2056 got, got_off);
2057 }
8462ae85
ILT
2058 }
2059 }
2060 }
ead1e424
ILT
2061 break;
2062
2063 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
2064 // If the symbol is fully resolved, this is just a PC32 reloc.
2065 // Otherwise we need a PLT entry.
7e1edb90 2066 if (gsym->final_value_is_known())
ead1e424 2067 break;
436ca963
ILT
2068 // If building a shared library, we can also skip the PLT entry
2069 // if the symbol is defined in the output file and is protected
2070 // or hidden.
2071 if (gsym->is_defined()
2072 && !gsym->is_from_dynobj()
2073 && !gsym->is_preemptible())
2074 break;
7e1edb90 2075 target->make_plt_entry(symtab, layout, gsym);
ead1e424
ILT
2076 break;
2077
2078 case elfcpp::R_386_GOTOFF:
2079 case elfcpp::R_386_GOTPC:
2080 // We need a GOT section.
7e1edb90 2081 target->got_section(symtab, layout);
ead1e424
ILT
2082 break;
2083
af6359d5
ILT
2084 // These are relocations which should only be seen by the
2085 // dynamic linker, and should never be seen here.
92e059d8
ILT
2086 case elfcpp::R_386_COPY:
2087 case elfcpp::R_386_GLOB_DAT:
2088 case elfcpp::R_386_JUMP_SLOT:
2089 case elfcpp::R_386_RELATIVE:
7223e9ca 2090 case elfcpp::R_386_IRELATIVE:
92e059d8
ILT
2091 case elfcpp::R_386_TLS_TPOFF:
2092 case elfcpp::R_386_TLS_DTPMOD32:
2093 case elfcpp::R_386_TLS_DTPOFF32:
2094 case elfcpp::R_386_TLS_TPOFF32:
2095 case elfcpp::R_386_TLS_DESC:
75f2446e
ILT
2096 gold_error(_("%s: unexpected reloc %u in object file"),
2097 object->name().c_str(), r_type);
92e059d8
ILT
2098 break;
2099
d61c17ea
ILT
2100 // These are initial tls relocs, which are expected when
2101 // linking.
56622147
ILT
2102 case elfcpp::R_386_TLS_GD: // Global-dynamic
2103 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2104 case elfcpp::R_386_TLS_DESC_CALL:
2105 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2106 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2107 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 2108 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
2109 case elfcpp::R_386_TLS_GOTIE:
2110 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 2111 case elfcpp::R_386_TLS_LE_32:
a3ad94ed 2112 {
7e1edb90 2113 const bool is_final = gsym->final_value_is_known();
af6359d5
ILT
2114 const tls::Tls_optimization optimized_type
2115 = Target_i386::optimize_tls_reloc(is_final, r_type);
a3ad94ed
ILT
2116 switch (r_type)
2117 {
56622147 2118 case elfcpp::R_386_TLS_GD: // Global-dynamic
07f397ab
ILT
2119 if (optimized_type == tls::TLSOPT_NONE)
2120 {
2121 // Create a pair of GOT entries for the module index and
2122 // dtv-relative offset.
2123 Output_data_got<32, false>* got
2124 = target->got_section(symtab, layout);
0a65a3a7 2125 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7bf1f802
ILT
2126 target->rel_dyn_section(layout),
2127 elfcpp::R_386_TLS_DTPMOD32,
2128 elfcpp::R_386_TLS_DTPOFF32);
07f397ab
ILT
2129 }
2130 else if (optimized_type == tls::TLSOPT_TO_IE)
2131 {
2132 // Create a GOT entry for the tp-relative offset.
2133 Output_data_got<32, false>* got
2134 = target->got_section(symtab, layout);
c2b45e22 2135 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
0a65a3a7 2136 target->rel_dyn_section(layout),
c2b45e22 2137 elfcpp::R_386_TLS_TPOFF);
07f397ab
ILT
2138 }
2139 else if (optimized_type != tls::TLSOPT_TO_LE)
2140 unsupported_reloc_global(object, r_type, gsym);
2141 break;
2142
56622147 2143 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
edfbb029 2144 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
2145 if (optimized_type == tls::TLSOPT_NONE)
2146 {
a8df5856
ILT
2147 // Create a double GOT entry with an R_386_TLS_DESC
2148 // reloc. The R_386_TLS_DESC reloc is resolved
2149 // lazily, so the GOT entry needs to be in an area in
2150 // .got.plt, not .got. Call got_section to make sure
2151 // the section has been created.
2152 target->got_section(symtab, layout);
2153 Output_data_got<32, false>* got = target->got_tlsdesc_section();
e291e7b9
ILT
2154 Reloc_section* rt = target->rel_tls_desc_section(layout);
2155 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC, rt,
c2b45e22
CC
2156 elfcpp::R_386_TLS_DESC, 0);
2157 }
2158 else if (optimized_type == tls::TLSOPT_TO_IE)
2159 {
2160 // Create a GOT entry for the tp-relative offset.
2161 Output_data_got<32, false>* got
2162 = target->got_section(symtab, layout);
2163 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
2164 target->rel_dyn_section(layout),
2165 elfcpp::R_386_TLS_TPOFF);
2166 }
2167 else if (optimized_type != tls::TLSOPT_TO_LE)
7bf1f802 2168 unsupported_reloc_global(object, r_type, gsym);
c2b45e22
CC
2169 break;
2170
2171 case elfcpp::R_386_TLS_DESC_CALL:
af6359d5
ILT
2172 break;
2173
56622147 2174 case elfcpp::R_386_TLS_LDM: // Local-dynamic
07f397ab
ILT
2175 if (optimized_type == tls::TLSOPT_NONE)
2176 {
2177 // Create a GOT entry for the module index.
94c4710f 2178 target->got_mod_index_entry(symtab, layout, object);
07f397ab
ILT
2179 }
2180 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5
ILT
2181 unsupported_reloc_global(object, r_type, gsym);
2182 break;
2183
56622147 2184 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
46cf9fa2
ILT
2185 break;
2186
56622147
ILT
2187 case elfcpp::R_386_TLS_IE: // Initial-exec
2188 case elfcpp::R_386_TLS_IE_32:
2189 case elfcpp::R_386_TLS_GOTIE:
535890bb 2190 layout->set_has_static_tls();
07f397ab
ILT
2191 if (optimized_type == tls::TLSOPT_NONE)
2192 {
7bf1f802
ILT
2193 // For the R_386_TLS_IE relocation, we need to create a
2194 // dynamic relocation when building a shared library.
2195 if (r_type == elfcpp::R_386_TLS_IE
8851ecca 2196 && parameters->options().shared())
07f397ab 2197 {
07f397ab 2198 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
2199 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
2200 output_section, object,
2201 data_shndx,
2202 reloc.get_r_offset());
07f397ab 2203 }
7bf1f802
ILT
2204 // Create a GOT entry for the tp-relative offset.
2205 Output_data_got<32, false>* got
2206 = target->got_section(symtab, layout);
2207 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
2208 ? elfcpp::R_386_TLS_TPOFF32
2209 : elfcpp::R_386_TLS_TPOFF);
c2b45e22
CC
2210 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2211 ? GOT_TYPE_TLS_OFFSET
2212 : GOT_TYPE_TLS_NOFFSET);
2213 got->add_global_with_rel(gsym, got_type,
7bf1f802
ILT
2214 target->rel_dyn_section(layout),
2215 dyn_r_type);
07f397ab
ILT
2216 }
2217 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5 2218 unsupported_reloc_global(object, r_type, gsym);
a3ad94ed 2219 break;
af6359d5 2220
56622147
ILT
2221 case elfcpp::R_386_TLS_LE: // Local-exec
2222 case elfcpp::R_386_TLS_LE_32:
535890bb 2223 layout->set_has_static_tls();
8851ecca 2224 if (parameters->options().shared())
7bf1f802
ILT
2225 {
2226 // We need to create a dynamic relocation.
2227 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
2228 ? elfcpp::R_386_TLS_TPOFF32
2229 : elfcpp::R_386_TLS_TPOFF);
2230 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
2231 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
2232 data_shndx, reloc.get_r_offset());
2233 }
56622147
ILT
2234 break;
2235
af6359d5
ILT
2236 default:
2237 gold_unreachable();
a3ad94ed
ILT
2238 }
2239 }
92e059d8
ILT
2240 break;
2241
92e059d8
ILT
2242 case elfcpp::R_386_32PLT:
2243 case elfcpp::R_386_TLS_GD_32:
2244 case elfcpp::R_386_TLS_GD_PUSH:
2245 case elfcpp::R_386_TLS_GD_CALL:
2246 case elfcpp::R_386_TLS_GD_POP:
2247 case elfcpp::R_386_TLS_LDM_32:
2248 case elfcpp::R_386_TLS_LDM_PUSH:
2249 case elfcpp::R_386_TLS_LDM_CALL:
2250 case elfcpp::R_386_TLS_LDM_POP:
2251 case elfcpp::R_386_USED_BY_INTEL_200:
2252 default:
af6359d5 2253 unsupported_reloc_global(object, r_type, gsym);
92e059d8
ILT
2254 break;
2255 }
2256}
2257
6d03d481
ST
2258// Process relocations for gc.
2259
2260void
ad0f2072 2261Target_i386::gc_process_relocs(Symbol_table* symtab,
6d03d481 2262 Layout* layout,
6fa2a40b 2263 Sized_relobj_file<32, false>* object,
6d03d481
ST
2264 unsigned int data_shndx,
2265 unsigned int,
2266 const unsigned char* prelocs,
2267 size_t reloc_count,
2268 Output_section* output_section,
2269 bool needs_special_offset_handling,
2270 size_t local_symbol_count,
2271 const unsigned char* plocal_symbols)
2272{
2273 gold::gc_process_relocs<32, false, Target_i386, elfcpp::SHT_REL,
41cbeecc
ST
2274 Target_i386::Scan,
2275 Target_i386::Relocatable_size_for_reloc>(
6d03d481
ST
2276 symtab,
2277 layout,
2278 this,
2279 object,
2280 data_shndx,
2281 prelocs,
2282 reloc_count,
2283 output_section,
2284 needs_special_offset_handling,
2285 local_symbol_count,
2286 plocal_symbols);
2287}
2288
92e059d8
ILT
2289// Scan relocations for a section.
2290
2291void
ad0f2072 2292Target_i386::scan_relocs(Symbol_table* symtab,
ead1e424 2293 Layout* layout,
6fa2a40b 2294 Sized_relobj_file<32, false>* object,
a3ad94ed 2295 unsigned int data_shndx,
92e059d8
ILT
2296 unsigned int sh_type,
2297 const unsigned char* prelocs,
2298 size_t reloc_count,
730cdc88
ILT
2299 Output_section* output_section,
2300 bool needs_special_offset_handling,
92e059d8 2301 size_t local_symbol_count,
730cdc88 2302 const unsigned char* plocal_symbols)
92e059d8
ILT
2303{
2304 if (sh_type == elfcpp::SHT_RELA)
2305 {
75f2446e
ILT
2306 gold_error(_("%s: unsupported RELA reloc section"),
2307 object->name().c_str());
2308 return;
92e059d8
ILT
2309 }
2310
ead1e424
ILT
2311 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
2312 Target_i386::Scan>(
92e059d8 2313 symtab,
ead1e424
ILT
2314 layout,
2315 this,
92e059d8 2316 object,
a3ad94ed 2317 data_shndx,
92e059d8
ILT
2318 prelocs,
2319 reloc_count,
730cdc88
ILT
2320 output_section,
2321 needs_special_offset_handling,
92e059d8 2322 local_symbol_count,
730cdc88 2323 plocal_symbols);
92e059d8
ILT
2324}
2325
16649710 2326// Finalize the sections.
5a6f7e2d
ILT
2327
2328void
f59f41f3
DK
2329Target_i386::do_finalize_sections(
2330 Layout* layout,
2331 const Input_objects*,
e785ec03 2332 Symbol_table* symtab)
5a6f7e2d 2333{
ea715a34
ILT
2334 const Reloc_section* rel_plt = (this->plt_ == NULL
2335 ? NULL
2336 : this->plt_->rel_plt());
2337 layout->add_target_dynamic_tags(true, this->got_plt_, rel_plt,
612a8d3d 2338 this->rel_dyn_, true, false);
16649710
ILT
2339
2340 // Emit any relocs we saved in an attempt to avoid generating COPY
2341 // relocs.
12c0daef
ILT
2342 if (this->copy_relocs_.any_saved_relocs())
2343 this->copy_relocs_.emit(this->rel_dyn_section(layout));
e785ec03
ILT
2344
2345 // Set the size of the _GLOBAL_OFFSET_TABLE_ symbol to the size of
2346 // the .got.plt section.
2347 Symbol* sym = this->global_offset_table_;
2348 if (sym != NULL)
2349 {
2350 uint32_t data_size = this->got_plt_->current_data_size();
2351 symtab->get_sized_symbol<32>(sym)->set_symsize(data_size);
2352 }
28a13fec 2353
67181c72
ILT
2354 if (parameters->doing_static_link()
2355 && (this->plt_ == NULL || !this->plt_->has_irelative_section()))
28a13fec
ILT
2356 {
2357 // If linking statically, make sure that the __rel_iplt symbols
2358 // were defined if necessary, even if we didn't create a PLT.
2359 static const Define_symbol_in_segment syms[] =
2360 {
2361 {
2362 "__rel_iplt_start", // name
2363 elfcpp::PT_LOAD, // segment_type
2364 elfcpp::PF_W, // segment_flags_set
2365 elfcpp::PF(0), // segment_flags_clear
2366 0, // value
2367 0, // size
2368 elfcpp::STT_NOTYPE, // type
2369 elfcpp::STB_GLOBAL, // binding
2370 elfcpp::STV_HIDDEN, // visibility
2371 0, // nonvis
2372 Symbol::SEGMENT_START, // offset_from_base
2373 true // only_if_ref
2374 },
2375 {
2376 "__rel_iplt_end", // name
2377 elfcpp::PT_LOAD, // segment_type
2378 elfcpp::PF_W, // segment_flags_set
2379 elfcpp::PF(0), // segment_flags_clear
2380 0, // value
2381 0, // size
2382 elfcpp::STT_NOTYPE, // type
2383 elfcpp::STB_GLOBAL, // binding
2384 elfcpp::STV_HIDDEN, // visibility
2385 0, // nonvis
2386 Symbol::SEGMENT_START, // offset_from_base
2387 true // only_if_ref
2388 }
2389 };
2390
2391 symtab->define_symbols(layout, 2, syms,
2392 layout->script_options()->saw_sections_clause());
2393 }
5a6f7e2d
ILT
2394}
2395
86849f1f
ILT
2396// Return whether a direct absolute static relocation needs to be applied.
2397// In cases where Scan::local() or Scan::global() has created
2398// a dynamic relocation other than R_386_RELATIVE, the addend
2399// of the relocation is carried in the data, and we must not
2400// apply the static relocation.
2401
2402inline bool
2403Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
95a2c8d6 2404 unsigned int r_type,
031cdbed
ILT
2405 bool is_32bit,
2406 Output_section* output_section)
86849f1f 2407{
031cdbed
ILT
2408 // If the output section is not allocated, then we didn't call
2409 // scan_relocs, we didn't create a dynamic reloc, and we must apply
2410 // the reloc here.
2411 if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
2412 return true;
2413
95a2c8d6
RS
2414 int ref_flags = Scan::get_reference_flags(r_type);
2415
d61c6bd4
ILT
2416 // For local symbols, we will have created a non-RELATIVE dynamic
2417 // relocation only if (a) the output is position independent,
2418 // (b) the relocation is absolute (not pc- or segment-relative), and
2419 // (c) the relocation is not 32 bits wide.
86849f1f 2420 if (gsym == NULL)
8851ecca 2421 return !(parameters->options().output_is_position_independent()
0700cf32 2422 && (ref_flags & Symbol::ABSOLUTE_REF)
d61c6bd4 2423 && !is_32bit);
86849f1f 2424
0700cf32
ILT
2425 // For global symbols, we use the same helper routines used in the
2426 // scan pass. If we did not create a dynamic relocation, or if we
2427 // created a RELATIVE dynamic relocation, we should apply the static
2428 // relocation.
2429 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
2430 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
2431 && gsym->can_use_relative_reloc(ref_flags
2432 & Symbol::FUNCTION_CALL);
2433 return !has_dyn || is_rel;
86849f1f
ILT
2434}
2435
61ba1cf9
ILT
2436// Perform a relocation.
2437
ead1e424 2438inline bool
92e059d8 2439Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
ead1e424 2440 Target_i386* target,
ca09d69a 2441 Output_section* output_section,
92e059d8
ILT
2442 size_t relnum,
2443 const elfcpp::Rel<32, false>& rel,
2444 unsigned int r_type,
c06b7b0b 2445 const Sized_symbol<32>* gsym,
b8e6aad9 2446 const Symbol_value<32>* psymval,
92e059d8
ILT
2447 unsigned char* view,
2448 elfcpp::Elf_types<32>::Elf_Addr address,
fe8718a4 2449 section_size_type view_size)
61ba1cf9 2450{
ead1e424
ILT
2451 if (this->skip_call_tls_get_addr_)
2452 {
5efc7cd2
CC
2453 if ((r_type != elfcpp::R_386_PLT32
2454 && r_type != elfcpp::R_386_PC32)
ead1e424
ILT
2455 || gsym == NULL
2456 || strcmp(gsym->name(), "___tls_get_addr") != 0)
75f2446e
ILT
2457 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2458 _("missing expected TLS relocation"));
2459 else
ead1e424 2460 {
75f2446e
ILT
2461 this->skip_call_tls_get_addr_ = false;
2462 return false;
ead1e424 2463 }
ead1e424
ILT
2464 }
2465
6fa2a40b 2466 const Sized_relobj_file<32, false>* object = relinfo->object;
7223e9ca 2467
a3ad94ed 2468 // Pick the value to use for symbols defined in shared objects.
b8e6aad9 2469 Symbol_value<32> symval;
436ca963 2470 if (gsym != NULL
7223e9ca
ILT
2471 && gsym->type() == elfcpp::STT_GNU_IFUNC
2472 && r_type == elfcpp::R_386_32
95a2c8d6 2473 && gsym->needs_dynamic_reloc(Scan::get_reference_flags(r_type))
7223e9ca
ILT
2474 && gsym->can_use_relative_reloc(false)
2475 && !gsym->is_from_dynobj()
2476 && !gsym->is_undefined()
2477 && !gsym->is_preemptible())
2478 {
2479 // In this case we are generating a R_386_IRELATIVE reloc. We
2480 // want to use the real value of the symbol, not the PLT offset.
2481 }
2482 else if (gsym != NULL
95a2c8d6 2483 && gsym->use_plt_offset(Scan::get_reference_flags(r_type)))
a3ad94ed 2484 {
67181c72 2485 symval.set_output_value(target->plt_address_for_global(gsym)
b8e6aad9
ILT
2486 + gsym->plt_offset());
2487 psymval = &symval;
a3ad94ed 2488 }
7223e9ca
ILT
2489 else if (gsym == NULL && psymval->is_ifunc_symbol())
2490 {
2491 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2492 if (object->local_has_plt_offset(r_sym))
2493 {
67181c72 2494 symval.set_output_value(target->plt_address_for_local(object, r_sym)
7223e9ca
ILT
2495 + object->local_plt_offset(r_sym));
2496 psymval = &symval;
2497 }
2498 }
b8e6aad9 2499
1b64748b 2500 // Get the GOT offset if needed.
96f2030e
ILT
2501 // The GOT pointer points to the end of the GOT section.
2502 // We need to subtract the size of the GOT section to get
2503 // the actual offset to use in the relocation.
1b64748b
ILT
2504 bool have_got_offset = false;
2505 unsigned int got_offset = 0;
2506 switch (r_type)
2507 {
2508 case elfcpp::R_386_GOT32:
2509 if (gsym != NULL)
2510 {
0a65a3a7
CC
2511 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
2512 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
2513 - target->got_size());
1b64748b
ILT
2514 }
2515 else
2516 {
2517 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
0a65a3a7
CC
2518 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
2519 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
2520 - target->got_size());
1b64748b
ILT
2521 }
2522 have_got_offset = true;
2523 break;
2524
2525 default:
2526 break;
2527 }
2528
61ba1cf9
ILT
2529 switch (r_type)
2530 {
2531 case elfcpp::R_386_NONE:
92e059d8
ILT
2532 case elfcpp::R_386_GNU_VTINHERIT:
2533 case elfcpp::R_386_GNU_VTENTRY:
61ba1cf9
ILT
2534 break;
2535
2536 case elfcpp::R_386_32:
95a2c8d6 2537 if (should_apply_static_reloc(gsym, r_type, true, output_section))
86849f1f 2538 Relocate_functions<32, false>::rel32(view, object, psymval);
61ba1cf9
ILT
2539 break;
2540
2541 case elfcpp::R_386_PC32:
95a2c8d6
RS
2542 if (should_apply_static_reloc(gsym, r_type, true, output_section))
2543 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
92e059d8
ILT
2544 break;
2545
2546 case elfcpp::R_386_16:
95a2c8d6 2547 if (should_apply_static_reloc(gsym, r_type, false, output_section))
86849f1f 2548 Relocate_functions<32, false>::rel16(view, object, psymval);
92e059d8
ILT
2549 break;
2550
2551 case elfcpp::R_386_PC16:
95a2c8d6
RS
2552 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2553 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
61ba1cf9
ILT
2554 break;
2555
92e059d8 2556 case elfcpp::R_386_8:
95a2c8d6 2557 if (should_apply_static_reloc(gsym, r_type, false, output_section))
86849f1f 2558 Relocate_functions<32, false>::rel8(view, object, psymval);
92e059d8
ILT
2559 break;
2560
2561 case elfcpp::R_386_PC8:
95a2c8d6
RS
2562 if (should_apply_static_reloc(gsym, r_type, false, output_section))
2563 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
92e059d8
ILT
2564 break;
2565
ead1e424 2566 case elfcpp::R_386_PLT32:
df2efe71
ILT
2567 gold_assert(gsym == NULL
2568 || gsym->has_plt_offset()
99f8faca
ILT
2569 || gsym->final_value_is_known()
2570 || (gsym->is_defined()
2571 && !gsym->is_from_dynobj()
2572 && !gsym->is_preemptible()));
b8e6aad9 2573 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
ead1e424
ILT
2574 break;
2575
2576 case elfcpp::R_386_GOT32:
1b64748b
ILT
2577 gold_assert(have_got_offset);
2578 Relocate_functions<32, false>::rel32(view, got_offset);
ead1e424
ILT
2579 break;
2580
2581 case elfcpp::R_386_GOTOFF:
b8e6aad9
ILT
2582 {
2583 elfcpp::Elf_types<32>::Elf_Addr value;
2584 value = (psymval->value(object, 0)
96f2030e 2585 - target->got_plt_section()->address());
b8e6aad9
ILT
2586 Relocate_functions<32, false>::rel32(view, value);
2587 }
ead1e424
ILT
2588 break;
2589
2590 case elfcpp::R_386_GOTPC:
b8e6aad9
ILT
2591 {
2592 elfcpp::Elf_types<32>::Elf_Addr value;
96f2030e 2593 value = target->got_plt_section()->address();
b8e6aad9
ILT
2594 Relocate_functions<32, false>::pcrel32(view, value, address);
2595 }
ead1e424
ILT
2596 break;
2597
92e059d8
ILT
2598 case elfcpp::R_386_COPY:
2599 case elfcpp::R_386_GLOB_DAT:
2600 case elfcpp::R_386_JUMP_SLOT:
2601 case elfcpp::R_386_RELATIVE:
7223e9ca 2602 case elfcpp::R_386_IRELATIVE:
d61c17ea
ILT
2603 // These are outstanding tls relocs, which are unexpected when
2604 // linking.
92e059d8
ILT
2605 case elfcpp::R_386_TLS_TPOFF:
2606 case elfcpp::R_386_TLS_DTPMOD32:
2607 case elfcpp::R_386_TLS_DTPOFF32:
2608 case elfcpp::R_386_TLS_TPOFF32:
2609 case elfcpp::R_386_TLS_DESC:
75f2446e
ILT
2610 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2611 _("unexpected reloc %u in object file"),
2612 r_type);
92e059d8
ILT
2613 break;
2614
d61c17ea
ILT
2615 // These are initial tls relocs, which are expected when
2616 // linking.
56622147
ILT
2617 case elfcpp::R_386_TLS_GD: // Global-dynamic
2618 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2619 case elfcpp::R_386_TLS_DESC_CALL:
2620 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2621 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2622 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 2623 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
2624 case elfcpp::R_386_TLS_GOTIE:
2625 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 2626 case elfcpp::R_386_TLS_LE_32:
07f397ab
ILT
2627 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
2628 view, address, view_size);
92e059d8
ILT
2629 break;
2630
92e059d8
ILT
2631 case elfcpp::R_386_32PLT:
2632 case elfcpp::R_386_TLS_GD_32:
2633 case elfcpp::R_386_TLS_GD_PUSH:
2634 case elfcpp::R_386_TLS_GD_CALL:
2635 case elfcpp::R_386_TLS_GD_POP:
2636 case elfcpp::R_386_TLS_LDM_32:
2637 case elfcpp::R_386_TLS_LDM_PUSH:
2638 case elfcpp::R_386_TLS_LDM_CALL:
2639 case elfcpp::R_386_TLS_LDM_POP:
2640 case elfcpp::R_386_USED_BY_INTEL_200:
61ba1cf9 2641 default:
75f2446e
ILT
2642 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2643 _("unsupported reloc %u"),
2644 r_type);
92e059d8
ILT
2645 break;
2646 }
ead1e424
ILT
2647
2648 return true;
92e059d8
ILT
2649}
2650
2651// Perform a TLS relocation.
2652
2653inline void
2654Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
07f397ab 2655 Target_i386* target,
92e059d8
ILT
2656 size_t relnum,
2657 const elfcpp::Rel<32, false>& rel,
2658 unsigned int r_type,
c06b7b0b 2659 const Sized_symbol<32>* gsym,
b8e6aad9 2660 const Symbol_value<32>* psymval,
92e059d8
ILT
2661 unsigned char* view,
2662 elfcpp::Elf_types<32>::Elf_Addr,
fe8718a4 2663 section_size_type view_size)
92e059d8
ILT
2664{
2665 Output_segment* tls_segment = relinfo->layout->tls_segment();
92e059d8 2666
6fa2a40b 2667 const Sized_relobj_file<32, false>* object = relinfo->object;
07f397ab
ILT
2668
2669 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
b8e6aad9 2670
b3705d2a
ILT
2671 const bool is_final = (gsym == NULL
2672 ? !parameters->options().shared()
2673 : gsym->final_value_is_known());
af6359d5
ILT
2674 const tls::Tls_optimization optimized_type
2675 = Target_i386::optimize_tls_reloc(is_final, r_type);
92e059d8
ILT
2676 switch (r_type)
2677 {
56622147 2678 case elfcpp::R_386_TLS_GD: // Global-dynamic
af6359d5 2679 if (optimized_type == tls::TLSOPT_TO_LE)
92e059d8 2680 {
62855347
ILT
2681 if (tls_segment == NULL)
2682 {
191f1a2d
ILT
2683 gold_assert(parameters->errors()->error_count() > 0
2684 || issue_undefined_symbol_error(gsym));
62855347
ILT
2685 return;
2686 }
56622147
ILT
2687 this->tls_gd_to_le(relinfo, relnum, tls_segment,
2688 rel, r_type, value, view,
2689 view_size);
92e059d8
ILT
2690 break;
2691 }
07f397ab
ILT
2692 else
2693 {
c2b45e22
CC
2694 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2695 ? GOT_TYPE_TLS_NOFFSET
2696 : GOT_TYPE_TLS_PAIR);
07f397ab
ILT
2697 unsigned int got_offset;
2698 if (gsym != NULL)
2699 {
c2b45e22
CC
2700 gold_assert(gsym->has_got_offset(got_type));
2701 got_offset = gsym->got_offset(got_type) - target->got_size();
07f397ab
ILT
2702 }
2703 else
2704 {
2705 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
2706 gold_assert(object->local_has_got_offset(r_sym, got_type));
2707 got_offset = (object->local_got_offset(r_sym, got_type)
07f397ab
ILT
2708 - target->got_size());
2709 }
2710 if (optimized_type == tls::TLSOPT_TO_IE)
2711 {
7bf1f802
ILT
2712 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2713 got_offset, view, view_size);
07f397ab
ILT
2714 break;
2715 }
2716 else if (optimized_type == tls::TLSOPT_NONE)
2717 {
2718 // Relocate the field with the offset of the pair of GOT
2719 // entries.
2720 Relocate_functions<32, false>::rel32(view, got_offset);
2721 break;
2722 }
2723 }
75f2446e
ILT
2724 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2725 _("unsupported reloc %u"),
2726 r_type);
92e059d8
ILT
2727 break;
2728
56622147
ILT
2729 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2730 case elfcpp::R_386_TLS_DESC_CALL:
497897f9 2731 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
c2b45e22
CC
2732 if (optimized_type == tls::TLSOPT_TO_LE)
2733 {
62855347
ILT
2734 if (tls_segment == NULL)
2735 {
191f1a2d
ILT
2736 gold_assert(parameters->errors()->error_count() > 0
2737 || issue_undefined_symbol_error(gsym));
62855347
ILT
2738 return;
2739 }
c2b45e22
CC
2740 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
2741 rel, r_type, value, view,
2742 view_size);
2743 break;
2744 }
2745 else
2746 {
2747 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
2748 ? GOT_TYPE_TLS_NOFFSET
2749 : GOT_TYPE_TLS_DESC);
a8df5856
ILT
2750 unsigned int got_offset = 0;
2751 if (r_type == elfcpp::R_386_TLS_GOTDESC
2752 && optimized_type == tls::TLSOPT_NONE)
2753 {
2754 // We created GOT entries in the .got.tlsdesc portion of
2755 // the .got.plt section, but the offset stored in the
2756 // symbol is the offset within .got.tlsdesc.
2757 got_offset = (target->got_size()
2758 + target->got_plt_section()->data_size());
2759 }
c2b45e22
CC
2760 if (gsym != NULL)
2761 {
2762 gold_assert(gsym->has_got_offset(got_type));
a8df5856 2763 got_offset += gsym->got_offset(got_type) - target->got_size();
c2b45e22
CC
2764 }
2765 else
2766 {
2767 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
2768 gold_assert(object->local_has_got_offset(r_sym, got_type));
a8df5856
ILT
2769 got_offset += (object->local_got_offset(r_sym, got_type)
2770 - target->got_size());
c2b45e22
CC
2771 }
2772 if (optimized_type == tls::TLSOPT_TO_IE)
2773 {
62855347
ILT
2774 if (tls_segment == NULL)
2775 {
191f1a2d
ILT
2776 gold_assert(parameters->errors()->error_count() > 0
2777 || issue_undefined_symbol_error(gsym));
62855347
ILT
2778 return;
2779 }
c2b45e22
CC
2780 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
2781 got_offset, view, view_size);
2782 break;
2783 }
2784 else if (optimized_type == tls::TLSOPT_NONE)
2785 {
2786 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2787 {
2788 // Relocate the field with the offset of the pair of GOT
2789 // entries.
2790 Relocate_functions<32, false>::rel32(view, got_offset);
2791 }
2792 break;
2793 }
2794 }
75f2446e
ILT
2795 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2796 _("unsupported reloc %u"),
2797 r_type);
ead1e424
ILT
2798 break;
2799
56622147 2800 case elfcpp::R_386_TLS_LDM: // Local-dynamic
46cf9fa2
ILT
2801 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2802 {
75f2446e
ILT
2803 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2804 _("both SUN and GNU model "
2805 "TLS relocations"));
2806 break;
46cf9fa2
ILT
2807 }
2808 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
af6359d5 2809 if (optimized_type == tls::TLSOPT_TO_LE)
46cf9fa2 2810 {
62855347
ILT
2811 if (tls_segment == NULL)
2812 {
191f1a2d
ILT
2813 gold_assert(parameters->errors()->error_count() > 0
2814 || issue_undefined_symbol_error(gsym));
62855347
ILT
2815 return;
2816 }
46cf9fa2
ILT
2817 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2818 value, view, view_size);
2819 break;
2820 }
07f397ab
ILT
2821 else if (optimized_type == tls::TLSOPT_NONE)
2822 {
2823 // Relocate the field with the offset of the GOT entry for
2824 // the module index.
2825 unsigned int got_offset;
94c4710f
ILT
2826 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2827 - target->got_size());
07f397ab
ILT
2828 Relocate_functions<32, false>::rel32(view, got_offset);
2829 break;
2830 }
75f2446e
ILT
2831 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2832 _("unsupported reloc %u"),
2833 r_type);
46cf9fa2
ILT
2834 break;
2835
56622147 2836 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
d6f22b98 2837 if (optimized_type == tls::TLSOPT_TO_LE)
e8a9fcda 2838 {
82bb573a
ILT
2839 // This reloc can appear in debugging sections, in which
2840 // case we must not convert to local-exec. We decide what
2841 // to do based on whether the section is marked as
2842 // containing executable code. That is what the GNU linker
2843 // does as well.
2844 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2845 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
d6f22b98 2846 {
62855347
ILT
2847 if (tls_segment == NULL)
2848 {
191f1a2d
ILT
2849 gold_assert(parameters->errors()->error_count() > 0
2850 || issue_undefined_symbol_error(gsym));
62855347
ILT
2851 return;
2852 }
d6f22b98
ILT
2853 value -= tls_segment->memsz();
2854 }
e8a9fcda 2855 }
46cf9fa2
ILT
2856 Relocate_functions<32, false>::rel32(view, value);
2857 break;
2858
56622147
ILT
2859 case elfcpp::R_386_TLS_IE: // Initial-exec
2860 case elfcpp::R_386_TLS_GOTIE:
2861 case elfcpp::R_386_TLS_IE_32:
2862 if (optimized_type == tls::TLSOPT_TO_LE)
2863 {
62855347
ILT
2864 if (tls_segment == NULL)
2865 {
191f1a2d
ILT
2866 gold_assert(parameters->errors()->error_count() > 0
2867 || issue_undefined_symbol_error(gsym));
62855347
ILT
2868 return;
2869 }
56622147
ILT
2870 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2871 rel, r_type, value, view,
2872 view_size);
2873 break;
2874 }
07f397ab
ILT
2875 else if (optimized_type == tls::TLSOPT_NONE)
2876 {
2877 // Relocate the field with the offset of the GOT entry for
2878 // the tp-relative offset of the symbol.
c2b45e22
CC
2879 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2880 ? GOT_TYPE_TLS_OFFSET
2881 : GOT_TYPE_TLS_NOFFSET);
07f397ab
ILT
2882 unsigned int got_offset;
2883 if (gsym != NULL)
2884 {
c2b45e22
CC
2885 gold_assert(gsym->has_got_offset(got_type));
2886 got_offset = gsym->got_offset(got_type);
07f397ab
ILT
2887 }
2888 else
2889 {
2890 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
2891 gold_assert(object->local_has_got_offset(r_sym, got_type));
2892 got_offset = object->local_got_offset(r_sym, got_type);
07f397ab
ILT
2893 }
2894 // For the R_386_TLS_IE relocation, we need to apply the
2895 // absolute address of the GOT entry.
2896 if (r_type == elfcpp::R_386_TLS_IE)
2897 got_offset += target->got_plt_section()->address();
2898 // All GOT offsets are relative to the end of the GOT.
2899 got_offset -= target->got_size();
2900 Relocate_functions<32, false>::rel32(view, got_offset);
2901 break;
2902 }
75f2446e
ILT
2903 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2904 _("unsupported reloc %u"),
2905 r_type);
92e059d8 2906 break;
92e059d8 2907
56622147 2908 case elfcpp::R_386_TLS_LE: // Local-exec
7bf1f802
ILT
2909 // If we're creating a shared library, a dynamic relocation will
2910 // have been created for this location, so do not apply it now.
8851ecca 2911 if (!parameters->options().shared())
7bf1f802 2912 {
62855347
ILT
2913 if (tls_segment == NULL)
2914 {
191f1a2d
ILT
2915 gold_assert(parameters->errors()->error_count() > 0
2916 || issue_undefined_symbol_error(gsym));
62855347
ILT
2917 return;
2918 }
7bf1f802
ILT
2919 value -= tls_segment->memsz();
2920 Relocate_functions<32, false>::rel32(view, value);
2921 }
56622147 2922 break;
92e059d8 2923
56622147 2924 case elfcpp::R_386_TLS_LE_32:
7bf1f802
ILT
2925 // If we're creating a shared library, a dynamic relocation will
2926 // have been created for this location, so do not apply it now.
8851ecca 2927 if (!parameters->options().shared())
7bf1f802 2928 {
62855347
ILT
2929 if (tls_segment == NULL)
2930 {
191f1a2d
ILT
2931 gold_assert(parameters->errors()->error_count() > 0
2932 || issue_undefined_symbol_error(gsym));
62855347
ILT
2933 return;
2934 }
7bf1f802
ILT
2935 value = tls_segment->memsz() - value;
2936 Relocate_functions<32, false>::rel32(view, value);
2937 }
56622147 2938 break;
92e059d8 2939 }
92e059d8
ILT
2940}
2941
e041f13d 2942// Do a relocation in which we convert a TLS General-Dynamic to a
ead1e424
ILT
2943// Local-Exec.
2944
2945inline void
2946Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2947 size_t relnum,
2948 Output_segment* tls_segment,
2949 const elfcpp::Rel<32, false>& rel,
2950 unsigned int,
2951 elfcpp::Elf_types<32>::Elf_Addr value,
2952 unsigned char* view,
fe8718a4 2953 section_size_type view_size)
ead1e424
ILT
2954{
2955 // leal foo(,%reg,1),%eax; call ___tls_get_addr
46cf9fa2 2956 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
ead1e424
ILT
2957 // leal foo(%reg),%eax; call ___tls_get_addr
2958 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2959
af6359d5
ILT
2960 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2961 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
ead1e424
ILT
2962
2963 unsigned char op1 = view[-1];
2964 unsigned char op2 = view[-2];
2965
af6359d5
ILT
2966 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2967 op2 == 0x8d || op2 == 0x04);
2968 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
ead1e424
ILT
2969
2970 int roff = 5;
2971
2972 if (op2 == 0x04)
2973 {
af6359d5
ILT
2974 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2975 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2976 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2977 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
ead1e424
ILT
2978 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2979 }
2980 else
2981 {
af6359d5
ILT
2982 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2983 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 2984 if (rel.get_r_offset() + 9 < view_size
d61c17ea 2985 && view[9] == 0x90)
ead1e424
ILT
2986 {
2987 // There is a trailing nop. Use the size byte subl.
2988 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2989 roff = 6;
2990 }
2991 else
2992 {
2993 // Use the five byte subl.
2994 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2995 }
2996 }
2997
7bf1f802 2998 value = tls_segment->memsz() - value;
ead1e424
ILT
2999 Relocate_functions<32, false>::rel32(view + roff, value);
3000
3001 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3002 // We can skip it.
3003 this->skip_call_tls_get_addr_ = true;
3004}
3005
7bf1f802 3006// Do a relocation in which we convert a TLS General-Dynamic to an
07f397ab
ILT
3007// Initial-Exec.
3008
3009inline void
3010Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
3011 size_t relnum,
c2b45e22 3012 Output_segment*,
07f397ab
ILT
3013 const elfcpp::Rel<32, false>& rel,
3014 unsigned int,
3015 elfcpp::Elf_types<32>::Elf_Addr value,
3016 unsigned char* view,
fe8718a4 3017 section_size_type view_size)
07f397ab
ILT
3018{
3019 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
3020 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
3021
3022 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3023 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
3024
3025 unsigned char op1 = view[-1];
3026 unsigned char op2 = view[-2];
3027
3028 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3029 op2 == 0x8d || op2 == 0x04);
3030 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
3031
3032 int roff = 5;
3033
c2b45e22
CC
3034 // FIXME: For now, support only the first (SIB) form.
3035 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
07f397ab
ILT
3036
3037 if (op2 == 0x04)
3038 {
3039 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
3040 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
3041 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3042 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
3043 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
3044 }
3045 else
3046 {
3047 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3048 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 3049 if (rel.get_r_offset() + 9 < view_size
07f397ab
ILT
3050 && view[9] == 0x90)
3051 {
3052 // FIXME: This is not the right instruction sequence.
3053 // There is a trailing nop. Use the size byte subl.
3054 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
3055 roff = 6;
3056 }
3057 else
3058 {
3059 // FIXME: This is not the right instruction sequence.
3060 // Use the five byte subl.
3061 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
3062 }
3063 }
3064
07f397ab
ILT
3065 Relocate_functions<32, false>::rel32(view + roff, value);
3066
3067 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3068 // We can skip it.
3069 this->skip_call_tls_get_addr_ = true;
3070}
3071
c2b45e22
CC
3072// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3073// General-Dynamic to a Local-Exec.
3074
3075inline void
3076Target_i386::Relocate::tls_desc_gd_to_le(
3077 const Relocate_info<32, false>* relinfo,
3078 size_t relnum,
3079 Output_segment* tls_segment,
3080 const elfcpp::Rel<32, false>& rel,
3081 unsigned int r_type,
3082 elfcpp::Elf_types<32>::Elf_Addr value,
3083 unsigned char* view,
3084 section_size_type view_size)
3085{
3086 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3087 {
3088 // leal foo@TLSDESC(%ebx), %eax
3089 // ==> leal foo@NTPOFF, %eax
3090 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3091 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3092 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3093 view[-2] == 0x8d && view[-1] == 0x83);
3094 view[-1] = 0x05;
3095 value -= tls_segment->memsz();
3096 Relocate_functions<32, false>::rel32(view, value);
3097 }
3098 else
3099 {
3100 // call *foo@TLSCALL(%eax)
3101 // ==> nop; nop
3102 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3103 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3104 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3105 view[0] == 0xff && view[1] == 0x10);
3106 view[0] = 0x66;
3107 view[1] = 0x90;
3108 }
3109}
3110
3111// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
3112// General-Dynamic to an Initial-Exec.
3113
3114inline void
3115Target_i386::Relocate::tls_desc_gd_to_ie(
3116 const Relocate_info<32, false>* relinfo,
3117 size_t relnum,
3118 Output_segment*,
3119 const elfcpp::Rel<32, false>& rel,
3120 unsigned int r_type,
3121 elfcpp::Elf_types<32>::Elf_Addr value,
3122 unsigned char* view,
3123 section_size_type view_size)
3124{
3125 if (r_type == elfcpp::R_386_TLS_GOTDESC)
3126 {
3127 // leal foo@TLSDESC(%ebx), %eax
3128 // ==> movl foo@GOTNTPOFF(%ebx), %eax
3129 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3130 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3131 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3132 view[-2] == 0x8d && view[-1] == 0x83);
3133 view[-2] = 0x8b;
3134 Relocate_functions<32, false>::rel32(view, value);
3135 }
3136 else
3137 {
3138 // call *foo@TLSCALL(%eax)
3139 // ==> nop; nop
3140 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
3141 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
3142 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3143 view[0] == 0xff && view[1] == 0x10);
3144 view[0] = 0x66;
3145 view[1] = 0x90;
3146 }
3147}
3148
46cf9fa2
ILT
3149// Do a relocation in which we convert a TLS Local-Dynamic to a
3150// Local-Exec.
3151
3152inline void
3153Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
3154 size_t relnum,
3155 Output_segment*,
3156 const elfcpp::Rel<32, false>& rel,
3157 unsigned int,
3158 elfcpp::Elf_types<32>::Elf_Addr,
3159 unsigned char* view,
fe8718a4 3160 section_size_type view_size)
46cf9fa2
ILT
3161{
3162 // leal foo(%reg), %eax; call ___tls_get_addr
3163 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
3164
af6359d5
ILT
3165 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3166 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
46cf9fa2
ILT
3167
3168 // FIXME: Does this test really always pass?
af6359d5
ILT
3169 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3170 view[-2] == 0x8d && view[-1] == 0x83);
46cf9fa2 3171
af6359d5 3172 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
46cf9fa2
ILT
3173
3174 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
3175
3176 // The next reloc should be a PLT32 reloc against __tls_get_addr.
3177 // We can skip it.
3178 this->skip_call_tls_get_addr_ = true;
3179}
3180
56622147
ILT
3181// Do a relocation in which we convert a TLS Initial-Exec to a
3182// Local-Exec.
3183
3184inline void
3185Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
3186 size_t relnum,
3187 Output_segment* tls_segment,
3188 const elfcpp::Rel<32, false>& rel,
3189 unsigned int r_type,
3190 elfcpp::Elf_types<32>::Elf_Addr value,
3191 unsigned char* view,
fe8718a4 3192 section_size_type view_size)
56622147
ILT
3193{
3194 // We have to actually change the instructions, which means that we
3195 // need to examine the opcodes to figure out which instruction we
3196 // are looking at.
3197 if (r_type == elfcpp::R_386_TLS_IE)
3198 {
3199 // movl %gs:XX,%eax ==> movl $YY,%eax
3200 // movl %gs:XX,%reg ==> movl $YY,%reg
3201 // addl %gs:XX,%reg ==> addl $YY,%reg
3202 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
3203 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3204
3205 unsigned char op1 = view[-1];
3206 if (op1 == 0xa1)
3207 {
3208 // movl XX,%eax ==> movl $YY,%eax
3209 view[-1] = 0xb8;
3210 }
3211 else
3212 {
3213 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3214
3215 unsigned char op2 = view[-2];
3216 if (op2 == 0x8b)
3217 {
3218 // movl XX,%reg ==> movl $YY,%reg
3219 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3220 (op1 & 0xc7) == 0x05);
3221 view[-2] = 0xc7;
3222 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3223 }
3224 else if (op2 == 0x03)
3225 {
3226 // addl XX,%reg ==> addl $YY,%reg
3227 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3228 (op1 & 0xc7) == 0x05);
3229 view[-2] = 0x81;
3230 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3231 }
3232 else
3233 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3234 }
3235 }
3236 else
3237 {
3238 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3239 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3240 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3241 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
3242 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
3243
3244 unsigned char op1 = view[-1];
3245 unsigned char op2 = view[-2];
3246 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
3247 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
3248 if (op2 == 0x8b)
3249 {
3250 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
3251 view[-2] = 0xc7;
3252 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3253 }
3254 else if (op2 == 0x2b)
3255 {
3256 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
3257 view[-2] = 0x81;
3258 view[-1] = 0xe8 | ((op1 >> 3) & 7);
3259 }
3260 else if (op2 == 0x03)
3261 {
3262 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
3263 view[-2] = 0x81;
3264 view[-1] = 0xc0 | ((op1 >> 3) & 7);
3265 }
3266 else
3267 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
3268 }
3269
7bf1f802 3270 value = tls_segment->memsz() - value;
56622147
ILT
3271 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
3272 value = - value;
3273
3274 Relocate_functions<32, false>::rel32(view, value);
3275}
3276
61ba1cf9
ILT
3277// Relocate section data.
3278
3279void
92e059d8 3280Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
3281 unsigned int sh_type,
3282 const unsigned char* prelocs,
3283 size_t reloc_count,
730cdc88
ILT
3284 Output_section* output_section,
3285 bool needs_special_offset_handling,
61ba1cf9
ILT
3286 unsigned char* view,
3287 elfcpp::Elf_types<32>::Elf_Addr address,
364c7fa5
ILT
3288 section_size_type view_size,
3289 const Reloc_symbol_changes* reloc_symbol_changes)
61ba1cf9 3290{
a3ad94ed 3291 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 3292
ead1e424
ILT
3293 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
3294 Target_i386::Relocate>(
92e059d8 3295 relinfo,
ead1e424 3296 this,
61ba1cf9
ILT
3297 prelocs,
3298 reloc_count,
730cdc88
ILT
3299 output_section,
3300 needs_special_offset_handling,
61ba1cf9
ILT
3301 view,
3302 address,
364c7fa5
ILT
3303 view_size,
3304 reloc_symbol_changes);
61ba1cf9
ILT
3305}
3306
6a74a719
ILT
3307// Return the size of a relocation while scanning during a relocatable
3308// link.
3309
3310unsigned int
3311Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
3312 unsigned int r_type,
3313 Relobj* object)
3314{
3315 switch (r_type)
3316 {
3317 case elfcpp::R_386_NONE:
3318 case elfcpp::R_386_GNU_VTINHERIT:
3319 case elfcpp::R_386_GNU_VTENTRY:
3320 case elfcpp::R_386_TLS_GD: // Global-dynamic
3321 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
3322 case elfcpp::R_386_TLS_DESC_CALL:
3323 case elfcpp::R_386_TLS_LDM: // Local-dynamic
3324 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
3325 case elfcpp::R_386_TLS_IE: // Initial-exec
3326 case elfcpp::R_386_TLS_IE_32:
3327 case elfcpp::R_386_TLS_GOTIE:
3328 case elfcpp::R_386_TLS_LE: // Local-exec
3329 case elfcpp::R_386_TLS_LE_32:
3330 return 0;
3331
3332 case elfcpp::R_386_32:
3333 case elfcpp::R_386_PC32:
3334 case elfcpp::R_386_GOT32:
3335 case elfcpp::R_386_PLT32:
3336 case elfcpp::R_386_GOTOFF:
3337 case elfcpp::R_386_GOTPC:
3338 return 4;
3339
3340 case elfcpp::R_386_16:
3341 case elfcpp::R_386_PC16:
3342 return 2;
3343
3344 case elfcpp::R_386_8:
3345 case elfcpp::R_386_PC8:
3346 return 1;
3347
3348 // These are relocations which should only be seen by the
3349 // dynamic linker, and should never be seen here.
3350 case elfcpp::R_386_COPY:
3351 case elfcpp::R_386_GLOB_DAT:
3352 case elfcpp::R_386_JUMP_SLOT:
3353 case elfcpp::R_386_RELATIVE:
7223e9ca 3354 case elfcpp::R_386_IRELATIVE:
6a74a719
ILT
3355 case elfcpp::R_386_TLS_TPOFF:
3356 case elfcpp::R_386_TLS_DTPMOD32:
3357 case elfcpp::R_386_TLS_DTPOFF32:
3358 case elfcpp::R_386_TLS_TPOFF32:
3359 case elfcpp::R_386_TLS_DESC:
3360 object->error(_("unexpected reloc %u in object file"), r_type);
3361 return 0;
3362
3363 case elfcpp::R_386_32PLT:
3364 case elfcpp::R_386_TLS_GD_32:
3365 case elfcpp::R_386_TLS_GD_PUSH:
3366 case elfcpp::R_386_TLS_GD_CALL:
3367 case elfcpp::R_386_TLS_GD_POP:
3368 case elfcpp::R_386_TLS_LDM_32:
3369 case elfcpp::R_386_TLS_LDM_PUSH:
3370 case elfcpp::R_386_TLS_LDM_CALL:
3371 case elfcpp::R_386_TLS_LDM_POP:
3372 case elfcpp::R_386_USED_BY_INTEL_200:
3373 default:
3374 object->error(_("unsupported reloc %u in object file"), r_type);
3375 return 0;
3376 }
3377}
3378
3379// Scan the relocs during a relocatable link.
3380
3381void
ad0f2072 3382Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719 3383 Layout* layout,
6fa2a40b 3384 Sized_relobj_file<32, false>* object,
6a74a719
ILT
3385 unsigned int data_shndx,
3386 unsigned int sh_type,
3387 const unsigned char* prelocs,
3388 size_t reloc_count,
3389 Output_section* output_section,
3390 bool needs_special_offset_handling,
3391 size_t local_symbol_count,
3392 const unsigned char* plocal_symbols,
3393 Relocatable_relocs* rr)
3394{
3395 gold_assert(sh_type == elfcpp::SHT_REL);
3396
3397 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
3398 Relocatable_size_for_reloc> Scan_relocatable_relocs;
3399
7019cd25 3400 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
6a74a719 3401 Scan_relocatable_relocs>(
6a74a719
ILT
3402 symtab,
3403 layout,
3404 object,
3405 data_shndx,
3406 prelocs,
3407 reloc_count,
3408 output_section,
3409 needs_special_offset_handling,
3410 local_symbol_count,
3411 plocal_symbols,
3412 rr);
3413}
3414
3415// Relocate a section during a relocatable link.
3416
3417void
3418Target_i386::relocate_for_relocatable(
3419 const Relocate_info<32, false>* relinfo,
3420 unsigned int sh_type,
3421 const unsigned char* prelocs,
3422 size_t reloc_count,
3423 Output_section* output_section,
3424 off_t offset_in_output_section,
3425 const Relocatable_relocs* rr,
3426 unsigned char* view,
3427 elfcpp::Elf_types<32>::Elf_Addr view_address,
3428 section_size_type view_size,
3429 unsigned char* reloc_view,
3430 section_size_type reloc_view_size)
3431{
3432 gold_assert(sh_type == elfcpp::SHT_REL);
3433
7019cd25 3434 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
6a74a719
ILT
3435 relinfo,
3436 prelocs,
3437 reloc_count,
3438 output_section,
3439 offset_in_output_section,
3440 rr,
3441 view,
3442 view_address,
3443 view_size,
3444 reloc_view,
3445 reloc_view_size);
3446}
3447
ab5c9e90
ILT
3448// Return the value to use for a dynamic which requires special
3449// treatment. This is how we support equality comparisons of function
3450// pointers across shared library boundaries, as described in the
3451// processor specific ABI supplement.
3452
3453uint64_t
3454Target_i386::do_dynsym_value(const Symbol* gsym) const
3455{
3456 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
67181c72 3457 return this->plt_address_for_global(gsym) + gsym->plt_offset();
ab5c9e90
ILT
3458}
3459
c51e6221
ILT
3460// Return a string used to fill a code section with nops to take up
3461// the specified length.
3462
3463std::string
8851ecca 3464Target_i386::do_code_fill(section_size_type length) const
c51e6221
ILT
3465{
3466 if (length >= 16)
3467 {
3468 // Build a jmp instruction to skip over the bytes.
3469 unsigned char jmp[5];
3470 jmp[0] = 0xe9;
3471 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
3472 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
3473 + std::string(length - 5, '\0'));
3474 }
3475
3476 // Nop sequences of various lengths.
3477 const char nop1[1] = { 0x90 }; // nop
3478 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
3479 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
3480 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
3481 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
3482 0x00 }; // leal 0(%esi,1),%esi
3483 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3484 0x00, 0x00 };
3485 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3486 0x00, 0x00, 0x00 };
3487 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
3488 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
3489 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
3490 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
3491 0x00 };
3492 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
3493 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
3494 0x00, 0x00 };
3495 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
3496 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
3497 0x00, 0x00, 0x00 };
3498 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3499 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
3500 0x00, 0x00, 0x00, 0x00 };
3501 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
3502 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
3503 0x27, 0x00, 0x00, 0x00,
3504 0x00 };
3505 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
3506 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
3507 0xbc, 0x27, 0x00, 0x00,
3508 0x00, 0x00 };
3509 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
3510 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
3511 0x90, 0x90, 0x90, 0x90,
3512 0x90, 0x90, 0x90 };
3513
3514 const char* nops[16] = {
3515 NULL,
3516 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
3517 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
3518 };
3519
3520 return std::string(nops[length], length);
3521}
3522
02d7cd44
ILT
3523// Return the value to use for the base of a DW_EH_PE_datarel offset
3524// in an FDE. Solaris and SVR4 use DW_EH_PE_datarel because their
3525// assembler can not write out the difference between two labels in
3526// different sections, so instead of using a pc-relative value they
3527// use an offset from the GOT.
3528
3529uint64_t
3530Target_i386::do_ehframe_datarel_base() const
3531{
3532 gold_assert(this->global_offset_table_ != NULL);
3533 Symbol* sym = this->global_offset_table_;
3534 Sized_symbol<32>* ssym = static_cast<Sized_symbol<32>*>(sym);
3535 return ssym->value();
3536}
3537
b6848d3c
ILT
3538// Return whether SYM should be treated as a call to a non-split
3539// function. We don't want that to be true of a call to a
3540// get_pc_thunk function.
3541
3542bool
3543Target_i386::do_is_call_to_non_split(const Symbol* sym, unsigned int) const
3544{
3545 return (sym->type() == elfcpp::STT_FUNC
b6848d3c
ILT
3546 && !is_prefix_of("__i686.get_pc_thunk.", sym->name()));
3547}
3548
364c7fa5 3549// FNOFFSET in section SHNDX in OBJECT is the start of a function
9b547ce6 3550// compiled with -fsplit-stack. The function calls non-split-stack
364c7fa5
ILT
3551// code. We have to change the function so that it always ensures
3552// that it has enough stack space to run some random function.
3553
3554void
3555Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
3556 section_offset_type fnoffset,
3557 section_size_type fnsize,
3558 unsigned char* view,
3559 section_size_type view_size,
3560 std::string* from,
3561 std::string* to) const
3562{
3563 // The function starts with a comparison of the stack pointer and a
3564 // field in the TCB. This is followed by a jump.
3565
3566 // cmp %gs:NN,%esp
3567 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
3568 && fnsize > 7)
3569 {
3570 // We will call __morestack if the carry flag is set after this
3571 // comparison. We turn the comparison into an stc instruction
3572 // and some nops.
3573 view[fnoffset] = '\xf9';
3574 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
3575 }
3576 // lea NN(%esp),%ecx
1782c879
ILT
3577 // lea NN(%esp),%edx
3578 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
3579 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
364c7fa5
ILT
3580 && fnsize > 7)
3581 {
3582 // This is loading an offset from the stack pointer for a
3583 // comparison. The offset is negative, so we decrease the
3584 // offset by the amount of space we need for the stack. This
3585 // means we will avoid calling __morestack if there happens to
3586 // be plenty of space on the stack already.
3587 unsigned char* pval = view + fnoffset + 3;
3588 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
3589 val -= parameters->options().split_stack_adjust_size();
3590 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
3591 }
3592 else
3593 {
3594 if (!object->has_no_split_stack())
3595 object->error(_("failed to match split-stack sequence at "
3596 "section %u offset %0zx"),
ac33a407 3597 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
3598 return;
3599 }
3600
3601 // We have to change the function so that it calls
3602 // __morestack_non_split instead of __morestack. The former will
3603 // allocate additional stack space.
3604 *from = "__morestack";
3605 *to = "__morestack_non_split";
3606}
3607
14bfc3f5
ILT
3608// The selector for i386 object files.
3609
36959681 3610class Target_selector_i386 : public Target_selector_freebsd
14bfc3f5
ILT
3611{
3612public:
3613 Target_selector_i386()
36959681 3614 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
03ef7571
ILT
3615 "elf32-i386", "elf32-i386-freebsd",
3616 "elf_i386")
14bfc3f5
ILT
3617 { }
3618
3619 Target*
e96caa79
ILT
3620 do_instantiate_target()
3621 { return new Target_i386(); }
14bfc3f5
ILT
3622};
3623
14bfc3f5
ILT
3624Target_selector_i386 target_selector_i386;
3625
3626} // End anonymous namespace.
This page took 0.442516 seconds and 4 git commands to generate.