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