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