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