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