* configure: Regenerate to track ../common/common.m4 changes.
[deliverable/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
138 do_is_defined_by_abi(Symbol* sym) const
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
edfbb029
CC
326 // Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
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
edfbb029
CC
739// Define the _TLS_MODULE_BASE_ symbol at the end of the TLS segment.
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 {
750 symtab->define_in_output_segment("_TLS_MODULE_BASE_", NULL,
751 tls_segment, 0, 0,
752 elfcpp::STT_TLS,
753 elfcpp::STB_LOCAL,
754 elfcpp::STV_HIDDEN, 0,
755 Symbol::SEGMENT_END, true);
756 }
757 this->tls_base_symbol_defined_ = true;
758}
759
94c4710f
ILT
760// Create a GOT entry for the TLS module index.
761
762unsigned int
763Target_i386::got_mod_index_entry(Symbol_table* symtab, Layout* layout,
764 Sized_relobj<32, false>* object)
765{
766 if (this->got_mod_index_offset_ == -1U)
767 {
768 gold_assert(symtab != NULL && layout != NULL && object != NULL);
769 Reloc_section* rel_dyn = this->rel_dyn_section(layout);
770 Output_data_got<32, false>* got = this->got_section(symtab, layout);
771 unsigned int got_offset = got->add_constant(0);
772 rel_dyn->add_local(object, 0, elfcpp::R_386_TLS_DTPMOD32, got,
773 got_offset);
009a67a2 774 got->add_constant(0);
94c4710f
ILT
775 this->got_mod_index_offset_ = got_offset;
776 }
777 return this->got_mod_index_offset_;
778}
779
92e059d8 780// Optimize the TLS relocation type based on what we know about the
a3ad94ed
ILT
781// symbol. IS_FINAL is true if the final address of this symbol is
782// known at link time.
92e059d8 783
af6359d5 784tls::Tls_optimization
7e1edb90 785Target_i386::optimize_tls_reloc(bool is_final, int r_type)
92e059d8
ILT
786{
787 // If we are generating a shared library, then we can't do anything
788 // in the linker.
8851ecca 789 if (parameters->options().shared())
af6359d5 790 return tls::TLSOPT_NONE;
92e059d8
ILT
791
792 switch (r_type)
793 {
794 case elfcpp::R_386_TLS_GD:
795 case elfcpp::R_386_TLS_GOTDESC:
796 case elfcpp::R_386_TLS_DESC_CALL:
e041f13d 797 // These are General-Dynamic which permits fully general TLS
92e059d8
ILT
798 // access. Since we know that we are generating an executable,
799 // we can convert this to Initial-Exec. If we also know that
800 // this is a local symbol, we can further switch to Local-Exec.
a3ad94ed 801 if (is_final)
af6359d5
ILT
802 return tls::TLSOPT_TO_LE;
803 return tls::TLSOPT_TO_IE;
92e059d8
ILT
804
805 case elfcpp::R_386_TLS_LDM:
806 // This is Local-Dynamic, which refers to a local symbol in the
807 // dynamic TLS block. Since we know that we generating an
808 // executable, we can switch to Local-Exec.
af6359d5 809 return tls::TLSOPT_TO_LE;
92e059d8
ILT
810
811 case elfcpp::R_386_TLS_LDO_32:
af6359d5
ILT
812 // Another type of Local-Dynamic relocation.
813 return tls::TLSOPT_TO_LE;
92e059d8
ILT
814
815 case elfcpp::R_386_TLS_IE:
816 case elfcpp::R_386_TLS_GOTIE:
817 case elfcpp::R_386_TLS_IE_32:
818 // These are Initial-Exec relocs which get the thread offset
819 // from the GOT. If we know that we are linking against the
820 // local symbol, we can switch to Local-Exec, which links the
821 // thread offset into the instruction.
a3ad94ed 822 if (is_final)
af6359d5
ILT
823 return tls::TLSOPT_TO_LE;
824 return tls::TLSOPT_NONE;
8462ae85 825
92e059d8
ILT
826 case elfcpp::R_386_TLS_LE:
827 case elfcpp::R_386_TLS_LE_32:
828 // When we already have Local-Exec, there is nothing further we
829 // can do.
af6359d5 830 return tls::TLSOPT_NONE;
92e059d8
ILT
831
832 default:
a3ad94ed 833 gold_unreachable();
92e059d8
ILT
834 }
835}
836
af6359d5
ILT
837// Report an unsupported relocation against a local symbol.
838
839void
840Target_i386::Scan::unsupported_reloc_local(Sized_relobj<32, false>* object,
841 unsigned int r_type)
842{
75f2446e
ILT
843 gold_error(_("%s: unsupported reloc %u against local symbol"),
844 object->name().c_str(), r_type);
af6359d5
ILT
845}
846
92e059d8
ILT
847// Scan a relocation for a local symbol.
848
849inline void
7e1edb90 850Target_i386::Scan::local(const General_options&,
ead1e424
ILT
851 Symbol_table* symtab,
852 Layout* layout,
853 Target_i386* target,
f6ce93d6 854 Sized_relobj<32, false>* object,
1b64748b 855 unsigned int data_shndx,
07f397ab 856 Output_section* output_section,
1b64748b 857 const elfcpp::Rel<32, false>& reloc,
a3ad94ed 858 unsigned int r_type,
7bf1f802 859 const elfcpp::Sym<32, false>& lsym)
92e059d8
ILT
860{
861 switch (r_type)
862 {
863 case elfcpp::R_386_NONE:
864 case elfcpp::R_386_GNU_VTINHERIT:
865 case elfcpp::R_386_GNU_VTENTRY:
866 break;
867
868 case elfcpp::R_386_32:
436ca963
ILT
869 // If building a shared library (or a position-independent
870 // executable), we need to create a dynamic relocation for
871 // this location. The relocation applied at link time will
872 // apply the link-time value, so we flag the location with
873 // an R_386_RELATIVE relocation so the dynamic loader can
874 // relocate it easily.
8851ecca 875 if (parameters->options().output_is_position_independent())
436ca963
ILT
876 {
877 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
878 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
879 rel_dyn->add_local_relative(object, r_sym, elfcpp::R_386_RELATIVE,
880 output_section, data_shndx,
881 reloc.get_r_offset());
d61c6bd4
ILT
882 }
883 break;
884
885 case elfcpp::R_386_16:
886 case elfcpp::R_386_8:
887 // If building a shared library (or a position-independent
888 // executable), we need to create a dynamic relocation for
889 // this location. Because the addend needs to remain in the
890 // data section, we need to be careful not to apply this
891 // relocation statically.
8851ecca 892 if (parameters->options().output_is_position_independent())
d61c6bd4
ILT
893 {
894 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
d491d34e 895 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
dceae3c1 896 if (lsym.get_st_type() != elfcpp::STT_SECTION)
d491d34e
ILT
897 rel_dyn->add_local(object, r_sym, r_type, output_section,
898 data_shndx, reloc.get_r_offset());
dceae3c1
ILT
899 else
900 {
901 gold_assert(lsym.get_st_value() == 0);
d491d34e
ILT
902 unsigned int shndx = lsym.get_st_shndx();
903 bool is_ordinary;
904 shndx = object->adjust_sym_shndx(r_sym, shndx,
905 &is_ordinary);
906 if (!is_ordinary)
907 object->error(_("section symbol %u has bad shndx %u"),
908 r_sym, shndx);
909 else
910 rel_dyn->add_local_section(object, shndx,
911 r_type, output_section,
912 data_shndx, reloc.get_r_offset());
dceae3c1 913 }
436ca963 914 }
92e059d8
ILT
915 break;
916
917 case elfcpp::R_386_PC32:
918 case elfcpp::R_386_PC16:
919 case elfcpp::R_386_PC8:
920 break;
921
df2efe71
ILT
922 case elfcpp::R_386_PLT32:
923 // Since we know this is a local symbol, we can handle this as a
924 // PC32 reloc.
925 break;
926
ead1e424
ILT
927 case elfcpp::R_386_GOTOFF:
928 case elfcpp::R_386_GOTPC:
929 // We need a GOT section.
7e1edb90 930 target->got_section(symtab, layout);
ead1e424
ILT
931 break;
932
1b64748b
ILT
933 case elfcpp::R_386_GOT32:
934 {
935 // The symbol requires a GOT entry.
936 Output_data_got<32, false>* got = target->got_section(symtab, layout);
937 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
0a65a3a7 938 if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1b64748b
ILT
939 {
940 // If we are generating a shared object, we need to add a
7bf1f802 941 // dynamic RELATIVE relocation for this symbol's GOT entry.
8851ecca 942 if (parameters->options().output_is_position_independent())
1b64748b
ILT
943 {
944 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3 945 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
0a65a3a7
CC
946 rel_dyn->add_local_relative(
947 object, r_sym, elfcpp::R_386_RELATIVE, got,
948 object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1b64748b
ILT
949 }
950 }
951 }
952 break;
953
af6359d5
ILT
954 // These are relocations which should only be seen by the
955 // dynamic linker, and should never be seen here.
92e059d8
ILT
956 case elfcpp::R_386_COPY:
957 case elfcpp::R_386_GLOB_DAT:
958 case elfcpp::R_386_JUMP_SLOT:
959 case elfcpp::R_386_RELATIVE:
960 case elfcpp::R_386_TLS_TPOFF:
961 case elfcpp::R_386_TLS_DTPMOD32:
962 case elfcpp::R_386_TLS_DTPOFF32:
963 case elfcpp::R_386_TLS_TPOFF32:
964 case elfcpp::R_386_TLS_DESC:
a0c4fb0a 965 gold_error(_("%s: unexpected reloc %u in object file"),
75f2446e 966 object->name().c_str(), r_type);
92e059d8
ILT
967 break;
968
af6359d5 969 // These are initial TLS relocs, which are expected when
d61c17ea 970 // linking.
56622147
ILT
971 case elfcpp::R_386_TLS_GD: // Global-dynamic
972 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
973 case elfcpp::R_386_TLS_DESC_CALL:
974 case elfcpp::R_386_TLS_LDM: // Local-dynamic
975 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
976 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 977 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
978 case elfcpp::R_386_TLS_GOTIE:
979 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 980 case elfcpp::R_386_TLS_LE_32:
7e1edb90 981 {
8851ecca 982 bool output_is_shared = parameters->options().shared();
af6359d5
ILT
983 const tls::Tls_optimization optimized_type
984 = Target_i386::optimize_tls_reloc(!output_is_shared, r_type);
7e1edb90
ILT
985 switch (r_type)
986 {
56622147 987 case elfcpp::R_386_TLS_GD: // Global-dynamic
07f397ab
ILT
988 if (optimized_type == tls::TLSOPT_NONE)
989 {
990 // Create a pair of GOT entries for the module index and
991 // dtv-relative offset.
992 Output_data_got<32, false>* got
993 = target->got_section(symtab, layout);
994 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
d491d34e
ILT
995 unsigned int shndx = lsym.get_st_shndx();
996 bool is_ordinary;
997 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
998 if (!is_ordinary)
999 object->error(_("local symbol %u has bad shndx %u"),
1000 r_sym, shndx);
1001 else
1002 got->add_local_pair_with_rel(object, r_sym, shndx,
1003 GOT_TYPE_TLS_PAIR,
1004 target->rel_dyn_section(layout),
1005 elfcpp::R_386_TLS_DTPMOD32, 0);
07f397ab
ILT
1006 }
1007 else if (optimized_type != tls::TLSOPT_TO_LE)
1008 unsupported_reloc_local(object, r_type);
1009 break;
1010
56622147 1011 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva)
edfbb029 1012 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1013 if (optimized_type == tls::TLSOPT_NONE)
1014 {
1015 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1016 Output_data_got<32, false>* got
1017 = target->got_section(symtab, layout);
1018 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
d491d34e
ILT
1019 unsigned int shndx = lsym.get_st_shndx();
1020 bool is_ordinary;
1021 shndx = object->adjust_sym_shndx(r_sym, shndx, &is_ordinary);
1022 if (!is_ordinary)
1023 object->error(_("local symbol %u has bad shndx %u"),
1024 r_sym, shndx);
1025 else
1026 got->add_local_pair_with_rel(object, r_sym, shndx,
1027 GOT_TYPE_TLS_DESC,
1028 target->rel_dyn_section(layout),
1029 elfcpp::R_386_TLS_DESC, 0);
c2b45e22
CC
1030 }
1031 else if (optimized_type != tls::TLSOPT_TO_LE)
7bf1f802 1032 unsupported_reloc_local(object, r_type);
af6359d5
ILT
1033 break;
1034
c2b45e22
CC
1035 case elfcpp::R_386_TLS_DESC_CALL:
1036 break;
1037
56622147 1038 case elfcpp::R_386_TLS_LDM: // Local-dynamic
07f397ab
ILT
1039 if (optimized_type == tls::TLSOPT_NONE)
1040 {
1041 // Create a GOT entry for the module index.
94c4710f 1042 target->got_mod_index_entry(symtab, layout, object);
07f397ab
ILT
1043 }
1044 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5
ILT
1045 unsupported_reloc_local(object, r_type);
1046 break;
1047
56622147 1048 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
46cf9fa2
ILT
1049 break;
1050
56622147
ILT
1051 case elfcpp::R_386_TLS_IE: // Initial-exec
1052 case elfcpp::R_386_TLS_IE_32:
1053 case elfcpp::R_386_TLS_GOTIE:
535890bb 1054 layout->set_has_static_tls();
07f397ab
ILT
1055 if (optimized_type == tls::TLSOPT_NONE)
1056 {
7bf1f802
ILT
1057 // For the R_386_TLS_IE relocation, we need to create a
1058 // dynamic relocation when building a shared library.
1059 if (r_type == elfcpp::R_386_TLS_IE
8851ecca 1060 && parameters->options().shared())
7bf1f802
ILT
1061 {
1062 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
1063 unsigned int r_sym
1064 = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1065 rel_dyn->add_local_relative(object, r_sym,
1066 elfcpp::R_386_RELATIVE,
1067 output_section, data_shndx,
1068 reloc.get_r_offset());
7bf1f802 1069 }
07f397ab
ILT
1070 // Create a GOT entry for the tp-relative offset.
1071 Output_data_got<32, false>* got
1072 = target->got_section(symtab, layout);
1073 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
7bf1f802
ILT
1074 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1075 ? elfcpp::R_386_TLS_TPOFF32
1076 : elfcpp::R_386_TLS_TPOFF);
c2b45e22
CC
1077 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1078 ? GOT_TYPE_TLS_OFFSET
1079 : GOT_TYPE_TLS_NOFFSET);
1080 got->add_local_with_rel(object, r_sym, got_type,
7bf1f802
ILT
1081 target->rel_dyn_section(layout),
1082 dyn_r_type);
07f397ab
ILT
1083 }
1084 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5 1085 unsupported_reloc_local(object, r_type);
7e1edb90 1086 break;
af6359d5 1087
56622147
ILT
1088 case elfcpp::R_386_TLS_LE: // Local-exec
1089 case elfcpp::R_386_TLS_LE_32:
535890bb 1090 layout->set_has_static_tls();
07f397ab 1091 if (output_is_shared)
7bf1f802
ILT
1092 {
1093 // We need to create a dynamic relocation.
dceae3c1 1094 gold_assert(lsym.get_st_type() != elfcpp::STT_SECTION);
7bf1f802
ILT
1095 unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1096 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1097 ? elfcpp::R_386_TLS_TPOFF32
1098 : elfcpp::R_386_TLS_TPOFF);
1099 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1100 rel_dyn->add_local(object, r_sym, dyn_r_type, output_section,
1101 data_shndx, reloc.get_r_offset());
1102 }
56622147
ILT
1103 break;
1104
af6359d5
ILT
1105 default:
1106 gold_unreachable();
7e1edb90
ILT
1107 }
1108 }
92e059d8
ILT
1109 break;
1110
92e059d8
ILT
1111 case elfcpp::R_386_32PLT:
1112 case elfcpp::R_386_TLS_GD_32:
1113 case elfcpp::R_386_TLS_GD_PUSH:
1114 case elfcpp::R_386_TLS_GD_CALL:
1115 case elfcpp::R_386_TLS_GD_POP:
1116 case elfcpp::R_386_TLS_LDM_32:
1117 case elfcpp::R_386_TLS_LDM_PUSH:
1118 case elfcpp::R_386_TLS_LDM_CALL:
1119 case elfcpp::R_386_TLS_LDM_POP:
1120 case elfcpp::R_386_USED_BY_INTEL_200:
1121 default:
af6359d5 1122 unsupported_reloc_local(object, r_type);
92e059d8
ILT
1123 break;
1124 }
1125}
1126
af6359d5
ILT
1127// Report an unsupported relocation against a global symbol.
1128
1129void
1130Target_i386::Scan::unsupported_reloc_global(Sized_relobj<32, false>* object,
1131 unsigned int r_type,
1132 Symbol* gsym)
1133{
75f2446e 1134 gold_error(_("%s: unsupported reloc %u against global symbol %s"),
a2b1aa12 1135 object->name().c_str(), r_type, gsym->demangled_name().c_str());
af6359d5
ILT
1136}
1137
92e059d8
ILT
1138// Scan a relocation for a global symbol.
1139
1140inline void
12c0daef 1141Target_i386::Scan::global(const General_options&,
ead1e424
ILT
1142 Symbol_table* symtab,
1143 Layout* layout,
1144 Target_i386* target,
f6ce93d6 1145 Sized_relobj<32, false>* object,
a3ad94ed 1146 unsigned int data_shndx,
07f397ab 1147 Output_section* output_section,
a3ad94ed
ILT
1148 const elfcpp::Rel<32, false>& reloc,
1149 unsigned int r_type,
92e059d8
ILT
1150 Symbol* gsym)
1151{
1152 switch (r_type)
1153 {
1154 case elfcpp::R_386_NONE:
1155 case elfcpp::R_386_GNU_VTINHERIT:
8462ae85 1156 case elfcpp::R_386_GNU_VTENTRY:
92e059d8
ILT
1157 break;
1158
1159 case elfcpp::R_386_32:
92e059d8 1160 case elfcpp::R_386_16:
92e059d8 1161 case elfcpp::R_386_8:
96f2030e 1162 {
d61c6bd4
ILT
1163 // Make a PLT entry if necessary.
1164 if (gsym->needs_plt_entry())
1165 {
1166 target->make_plt_entry(symtab, layout, gsym);
1167 // Since this is not a PC-relative relocation, we may be
1168 // taking the address of a function. In that case we need to
1169 // set the entry in the dynamic symbol table to the address of
1170 // the PLT entry.
8851ecca 1171 if (gsym->is_from_dynobj() && !parameters->options().shared())
d61c6bd4
ILT
1172 gsym->set_needs_dynsym_value();
1173 }
1174 // Make a dynamic relocation if necessary.
0700cf32 1175 if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
d61c6bd4
ILT
1176 {
1177 if (target->may_need_copy_reloc(gsym))
1178 {
12c0daef 1179 target->copy_reloc(symtab, layout, object,
4f4c5f80 1180 data_shndx, output_section, gsym, reloc);
d61c6bd4
ILT
1181 }
1182 else if (r_type == elfcpp::R_386_32
1183 && gsym->can_use_relative_reloc(false))
1184 {
1185 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
1186 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1187 output_section, object,
1188 data_shndx, reloc.get_r_offset());
d61c6bd4
ILT
1189 }
1190 else
1191 {
96f2030e 1192 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4f4c5f80
ILT
1193 rel_dyn->add_global(gsym, r_type, output_section, object,
1194 data_shndx, reloc.get_r_offset());
d61c6bd4
ILT
1195 }
1196 }
1197 }
1198 break;
1199
1200 case elfcpp::R_386_PC32:
1201 case elfcpp::R_386_PC16:
1202 case elfcpp::R_386_PC8:
1203 {
1204 // Make a PLT entry if necessary.
1205 if (gsym->needs_plt_entry())
7bf1f802
ILT
1206 {
1207 // These relocations are used for function calls only in
1208 // non-PIC code. For a 32-bit relocation in a shared library,
1209 // we'll need a text relocation anyway, so we can skip the
1210 // PLT entry and let the dynamic linker bind the call directly
1211 // to the target. For smaller relocations, we should use a
1212 // PLT entry to ensure that the call can reach.
8851ecca 1213 if (!parameters->options().shared()
7bf1f802
ILT
1214 || r_type != elfcpp::R_386_PC32)
1215 target->make_plt_entry(symtab, layout, gsym);
1216 }
d61c6bd4 1217 // Make a dynamic relocation if necessary.
0700cf32
ILT
1218 int flags = Symbol::NON_PIC_REF;
1219 if (gsym->type() == elfcpp::STT_FUNC)
1220 flags |= Symbol::FUNCTION_CALL;
1221 if (gsym->needs_dynamic_reloc(flags))
96f2030e 1222 {
d61c6bd4
ILT
1223 if (target->may_need_copy_reloc(gsym))
1224 {
12c0daef 1225 target->copy_reloc(symtab, layout, object,
4f4c5f80 1226 data_shndx, output_section, gsym, reloc);
d61c6bd4 1227 }
86849f1f 1228 else
d61c6bd4
ILT
1229 {
1230 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
4f4c5f80
ILT
1231 rel_dyn->add_global(gsym, r_type, output_section, object,
1232 data_shndx, reloc.get_r_offset());
d61c6bd4 1233 }
96f2030e
ILT
1234 }
1235 }
92e059d8
ILT
1236 break;
1237
ead1e424 1238 case elfcpp::R_386_GOT32:
8462ae85
ILT
1239 {
1240 // The symbol requires a GOT entry.
7e1edb90 1241 Output_data_got<32, false>* got = target->got_section(symtab, layout);
7bf1f802 1242 if (gsym->final_value_is_known())
0a65a3a7 1243 got->add_global(gsym, GOT_TYPE_STANDARD);
7bf1f802
ILT
1244 else
1245 {
8462ae85 1246 // If this symbol is not fully resolved, we need to add a
7bf1f802
ILT
1247 // GOT entry with a dynamic relocation.
1248 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
8fc19601
ILT
1249 if (gsym->is_from_dynobj()
1250 || gsym->is_undefined()
1251 || gsym->is_preemptible())
0a65a3a7
CC
1252 got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1253 rel_dyn, elfcpp::R_386_GLOB_DAT);
7bf1f802 1254 else
8462ae85 1255 {
0a65a3a7
CC
1256 if (got->add_global(gsym, GOT_TYPE_STANDARD))
1257 rel_dyn->add_global_relative(
1258 gsym, elfcpp::R_386_RELATIVE, got,
1259 gsym->got_offset(GOT_TYPE_STANDARD));
8462ae85
ILT
1260 }
1261 }
1262 }
ead1e424
ILT
1263 break;
1264
1265 case elfcpp::R_386_PLT32:
a3ad94ed
ILT
1266 // If the symbol is fully resolved, this is just a PC32 reloc.
1267 // Otherwise we need a PLT entry.
7e1edb90 1268 if (gsym->final_value_is_known())
ead1e424 1269 break;
436ca963
ILT
1270 // If building a shared library, we can also skip the PLT entry
1271 // if the symbol is defined in the output file and is protected
1272 // or hidden.
1273 if (gsym->is_defined()
1274 && !gsym->is_from_dynobj()
1275 && !gsym->is_preemptible())
1276 break;
7e1edb90 1277 target->make_plt_entry(symtab, layout, gsym);
ead1e424
ILT
1278 break;
1279
1280 case elfcpp::R_386_GOTOFF:
1281 case elfcpp::R_386_GOTPC:
1282 // We need a GOT section.
7e1edb90 1283 target->got_section(symtab, layout);
ead1e424
ILT
1284 break;
1285
af6359d5
ILT
1286 // These are relocations which should only be seen by the
1287 // dynamic linker, and should never be seen here.
92e059d8
ILT
1288 case elfcpp::R_386_COPY:
1289 case elfcpp::R_386_GLOB_DAT:
1290 case elfcpp::R_386_JUMP_SLOT:
1291 case elfcpp::R_386_RELATIVE:
1292 case elfcpp::R_386_TLS_TPOFF:
1293 case elfcpp::R_386_TLS_DTPMOD32:
1294 case elfcpp::R_386_TLS_DTPOFF32:
1295 case elfcpp::R_386_TLS_TPOFF32:
1296 case elfcpp::R_386_TLS_DESC:
75f2446e
ILT
1297 gold_error(_("%s: unexpected reloc %u in object file"),
1298 object->name().c_str(), r_type);
92e059d8
ILT
1299 break;
1300
d61c17ea
ILT
1301 // These are initial tls relocs, which are expected when
1302 // linking.
56622147
ILT
1303 case elfcpp::R_386_TLS_GD: // Global-dynamic
1304 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1305 case elfcpp::R_386_TLS_DESC_CALL:
1306 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1307 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1308 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 1309 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
1310 case elfcpp::R_386_TLS_GOTIE:
1311 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 1312 case elfcpp::R_386_TLS_LE_32:
a3ad94ed 1313 {
7e1edb90 1314 const bool is_final = gsym->final_value_is_known();
af6359d5
ILT
1315 const tls::Tls_optimization optimized_type
1316 = Target_i386::optimize_tls_reloc(is_final, r_type);
a3ad94ed
ILT
1317 switch (r_type)
1318 {
56622147 1319 case elfcpp::R_386_TLS_GD: // Global-dynamic
07f397ab
ILT
1320 if (optimized_type == tls::TLSOPT_NONE)
1321 {
1322 // Create a pair of GOT entries for the module index and
1323 // dtv-relative offset.
1324 Output_data_got<32, false>* got
1325 = target->got_section(symtab, layout);
0a65a3a7 1326 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_PAIR,
7bf1f802
ILT
1327 target->rel_dyn_section(layout),
1328 elfcpp::R_386_TLS_DTPMOD32,
1329 elfcpp::R_386_TLS_DTPOFF32);
07f397ab
ILT
1330 }
1331 else if (optimized_type == tls::TLSOPT_TO_IE)
1332 {
1333 // Create a GOT entry for the tp-relative offset.
1334 Output_data_got<32, false>* got
1335 = target->got_section(symtab, layout);
c2b45e22 1336 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
0a65a3a7 1337 target->rel_dyn_section(layout),
c2b45e22 1338 elfcpp::R_386_TLS_TPOFF);
07f397ab
ILT
1339 }
1340 else if (optimized_type != tls::TLSOPT_TO_LE)
1341 unsupported_reloc_global(object, r_type, gsym);
1342 break;
1343
56622147 1344 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (~oliva url)
edfbb029 1345 target->define_tls_base_symbol(symtab, layout);
c2b45e22
CC
1346 if (optimized_type == tls::TLSOPT_NONE)
1347 {
1348 // Create a double GOT entry with an R_386_TLS_DESC reloc.
1349 Output_data_got<32, false>* got
1350 = target->got_section(symtab, layout);
1351 got->add_global_pair_with_rel(gsym, GOT_TYPE_TLS_DESC,
1352 target->rel_dyn_section(layout),
1353 elfcpp::R_386_TLS_DESC, 0);
1354 }
1355 else if (optimized_type == tls::TLSOPT_TO_IE)
1356 {
1357 // Create a GOT entry for the tp-relative offset.
1358 Output_data_got<32, false>* got
1359 = target->got_section(symtab, layout);
1360 got->add_global_with_rel(gsym, GOT_TYPE_TLS_NOFFSET,
1361 target->rel_dyn_section(layout),
1362 elfcpp::R_386_TLS_TPOFF);
1363 }
1364 else if (optimized_type != tls::TLSOPT_TO_LE)
7bf1f802 1365 unsupported_reloc_global(object, r_type, gsym);
c2b45e22
CC
1366 break;
1367
1368 case elfcpp::R_386_TLS_DESC_CALL:
af6359d5
ILT
1369 break;
1370
56622147 1371 case elfcpp::R_386_TLS_LDM: // Local-dynamic
07f397ab
ILT
1372 if (optimized_type == tls::TLSOPT_NONE)
1373 {
1374 // Create a GOT entry for the module index.
94c4710f 1375 target->got_mod_index_entry(symtab, layout, object);
07f397ab
ILT
1376 }
1377 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5
ILT
1378 unsupported_reloc_global(object, r_type, gsym);
1379 break;
1380
56622147 1381 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
46cf9fa2
ILT
1382 break;
1383
56622147
ILT
1384 case elfcpp::R_386_TLS_IE: // Initial-exec
1385 case elfcpp::R_386_TLS_IE_32:
1386 case elfcpp::R_386_TLS_GOTIE:
535890bb 1387 layout->set_has_static_tls();
07f397ab
ILT
1388 if (optimized_type == tls::TLSOPT_NONE)
1389 {
7bf1f802
ILT
1390 // For the R_386_TLS_IE relocation, we need to create a
1391 // dynamic relocation when building a shared library.
1392 if (r_type == elfcpp::R_386_TLS_IE
8851ecca 1393 && parameters->options().shared())
07f397ab 1394 {
07f397ab 1395 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
e8c846c3
ILT
1396 rel_dyn->add_global_relative(gsym, elfcpp::R_386_RELATIVE,
1397 output_section, object,
1398 data_shndx,
1399 reloc.get_r_offset());
07f397ab 1400 }
7bf1f802
ILT
1401 // Create a GOT entry for the tp-relative offset.
1402 Output_data_got<32, false>* got
1403 = target->got_section(symtab, layout);
1404 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_IE_32
1405 ? elfcpp::R_386_TLS_TPOFF32
1406 : elfcpp::R_386_TLS_TPOFF);
c2b45e22
CC
1407 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1408 ? GOT_TYPE_TLS_OFFSET
1409 : GOT_TYPE_TLS_NOFFSET);
1410 got->add_global_with_rel(gsym, got_type,
7bf1f802
ILT
1411 target->rel_dyn_section(layout),
1412 dyn_r_type);
07f397ab
ILT
1413 }
1414 else if (optimized_type != tls::TLSOPT_TO_LE)
af6359d5 1415 unsupported_reloc_global(object, r_type, gsym);
a3ad94ed 1416 break;
af6359d5 1417
56622147
ILT
1418 case elfcpp::R_386_TLS_LE: // Local-exec
1419 case elfcpp::R_386_TLS_LE_32:
535890bb 1420 layout->set_has_static_tls();
8851ecca 1421 if (parameters->options().shared())
7bf1f802
ILT
1422 {
1423 // We need to create a dynamic relocation.
1424 unsigned int dyn_r_type = (r_type == elfcpp::R_386_TLS_LE_32
1425 ? elfcpp::R_386_TLS_TPOFF32
1426 : elfcpp::R_386_TLS_TPOFF);
1427 Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1428 rel_dyn->add_global(gsym, dyn_r_type, output_section, object,
1429 data_shndx, reloc.get_r_offset());
1430 }
56622147
ILT
1431 break;
1432
af6359d5
ILT
1433 default:
1434 gold_unreachable();
a3ad94ed
ILT
1435 }
1436 }
92e059d8
ILT
1437 break;
1438
92e059d8
ILT
1439 case elfcpp::R_386_32PLT:
1440 case elfcpp::R_386_TLS_GD_32:
1441 case elfcpp::R_386_TLS_GD_PUSH:
1442 case elfcpp::R_386_TLS_GD_CALL:
1443 case elfcpp::R_386_TLS_GD_POP:
1444 case elfcpp::R_386_TLS_LDM_32:
1445 case elfcpp::R_386_TLS_LDM_PUSH:
1446 case elfcpp::R_386_TLS_LDM_CALL:
1447 case elfcpp::R_386_TLS_LDM_POP:
1448 case elfcpp::R_386_USED_BY_INTEL_200:
1449 default:
af6359d5 1450 unsupported_reloc_global(object, r_type, gsym);
92e059d8
ILT
1451 break;
1452 }
1453}
1454
1455// Scan relocations for a section.
1456
1457void
1458Target_i386::scan_relocs(const General_options& options,
1459 Symbol_table* symtab,
ead1e424 1460 Layout* layout,
f6ce93d6 1461 Sized_relobj<32, false>* object,
a3ad94ed 1462 unsigned int data_shndx,
92e059d8
ILT
1463 unsigned int sh_type,
1464 const unsigned char* prelocs,
1465 size_t reloc_count,
730cdc88
ILT
1466 Output_section* output_section,
1467 bool needs_special_offset_handling,
92e059d8 1468 size_t local_symbol_count,
730cdc88 1469 const unsigned char* plocal_symbols)
92e059d8
ILT
1470{
1471 if (sh_type == elfcpp::SHT_RELA)
1472 {
75f2446e
ILT
1473 gold_error(_("%s: unsupported RELA reloc section"),
1474 object->name().c_str());
1475 return;
92e059d8
ILT
1476 }
1477
ead1e424
ILT
1478 gold::scan_relocs<32, false, Target_i386, elfcpp::SHT_REL,
1479 Target_i386::Scan>(
92e059d8
ILT
1480 options,
1481 symtab,
ead1e424
ILT
1482 layout,
1483 this,
92e059d8 1484 object,
a3ad94ed 1485 data_shndx,
92e059d8
ILT
1486 prelocs,
1487 reloc_count,
730cdc88
ILT
1488 output_section,
1489 needs_special_offset_handling,
92e059d8 1490 local_symbol_count,
730cdc88 1491 plocal_symbols);
92e059d8
ILT
1492}
1493
16649710 1494// Finalize the sections.
5a6f7e2d
ILT
1495
1496void
7e1edb90 1497Target_i386::do_finalize_sections(Layout* layout)
5a6f7e2d 1498{
16649710
ILT
1499 // Fill in some more dynamic tags.
1500 Output_data_dynamic* const odyn = layout->dynamic_data();
1501 if (odyn != NULL)
1502 {
1503 if (this->got_plt_ != NULL)
1504 odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1505
1506 if (this->plt_ != NULL)
1507 {
1508 const Output_data* od = this->plt_->rel_plt();
1509 odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1510 odyn->add_section_address(elfcpp::DT_JMPREL, od);
1511 odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1512 }
1513
1514 if (this->rel_dyn_ != NULL)
1515 {
1516 const Output_data* od = this->rel_dyn_;
1517 odyn->add_section_address(elfcpp::DT_REL, od);
1518 odyn->add_section_size(elfcpp::DT_RELSZ, od);
1519 odyn->add_constant(elfcpp::DT_RELENT,
1520 elfcpp::Elf_sizes<32>::rel_size);
1521 }
1522
8851ecca 1523 if (!parameters->options().shared())
16649710
ILT
1524 {
1525 // The value of the DT_DEBUG tag is filled in by the dynamic
1526 // linker at run time, and used by the debugger.
1527 odyn->add_constant(elfcpp::DT_DEBUG, 0);
1528 }
1529 }
1530
1531 // Emit any relocs we saved in an attempt to avoid generating COPY
1532 // relocs.
12c0daef
ILT
1533 if (this->copy_relocs_.any_saved_relocs())
1534 this->copy_relocs_.emit(this->rel_dyn_section(layout));
5a6f7e2d
ILT
1535}
1536
86849f1f
ILT
1537// Return whether a direct absolute static relocation needs to be applied.
1538// In cases where Scan::local() or Scan::global() has created
1539// a dynamic relocation other than R_386_RELATIVE, the addend
1540// of the relocation is carried in the data, and we must not
1541// apply the static relocation.
1542
1543inline bool
1544Target_i386::Relocate::should_apply_static_reloc(const Sized_symbol<32>* gsym,
0700cf32 1545 int ref_flags,
86849f1f
ILT
1546 bool is_32bit)
1547{
d61c6bd4
ILT
1548 // For local symbols, we will have created a non-RELATIVE dynamic
1549 // relocation only if (a) the output is position independent,
1550 // (b) the relocation is absolute (not pc- or segment-relative), and
1551 // (c) the relocation is not 32 bits wide.
86849f1f 1552 if (gsym == NULL)
8851ecca 1553 return !(parameters->options().output_is_position_independent()
0700cf32 1554 && (ref_flags & Symbol::ABSOLUTE_REF)
d61c6bd4 1555 && !is_32bit);
86849f1f 1556
0700cf32
ILT
1557 // For global symbols, we use the same helper routines used in the
1558 // scan pass. If we did not create a dynamic relocation, or if we
1559 // created a RELATIVE dynamic relocation, we should apply the static
1560 // relocation.
1561 bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1562 bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1563 && gsym->can_use_relative_reloc(ref_flags
1564 & Symbol::FUNCTION_CALL);
1565 return !has_dyn || is_rel;
86849f1f
ILT
1566}
1567
61ba1cf9
ILT
1568// Perform a relocation.
1569
ead1e424 1570inline bool
92e059d8 1571Target_i386::Relocate::relocate(const Relocate_info<32, false>* relinfo,
ead1e424 1572 Target_i386* target,
92e059d8
ILT
1573 size_t relnum,
1574 const elfcpp::Rel<32, false>& rel,
1575 unsigned int r_type,
c06b7b0b 1576 const Sized_symbol<32>* gsym,
b8e6aad9 1577 const Symbol_value<32>* psymval,
92e059d8
ILT
1578 unsigned char* view,
1579 elfcpp::Elf_types<32>::Elf_Addr address,
fe8718a4 1580 section_size_type view_size)
61ba1cf9 1581{
ead1e424
ILT
1582 if (this->skip_call_tls_get_addr_)
1583 {
1584 if (r_type != elfcpp::R_386_PLT32
1585 || gsym == NULL
1586 || strcmp(gsym->name(), "___tls_get_addr") != 0)
75f2446e
ILT
1587 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1588 _("missing expected TLS relocation"));
1589 else
ead1e424 1590 {
75f2446e
ILT
1591 this->skip_call_tls_get_addr_ = false;
1592 return false;
ead1e424 1593 }
ead1e424
ILT
1594 }
1595
a3ad94ed 1596 // Pick the value to use for symbols defined in shared objects.
b8e6aad9 1597 Symbol_value<32> symval;
0700cf32
ILT
1598 bool is_nonpic = (r_type == elfcpp::R_386_PC8
1599 || r_type == elfcpp::R_386_PC16
1600 || r_type == elfcpp::R_386_PC32);
436ca963
ILT
1601 if (gsym != NULL
1602 && (gsym->is_from_dynobj()
8851ecca 1603 || (parameters->options().shared()
8fc19601 1604 && (gsym->is_undefined() || gsym->is_preemptible())))
0700cf32 1605 && gsym->has_plt_offset()
8851ecca 1606 && (!is_nonpic || !parameters->options().shared()))
a3ad94ed 1607 {
b8e6aad9
ILT
1608 symval.set_output_value(target->plt_section()->address()
1609 + gsym->plt_offset());
1610 psymval = &symval;
a3ad94ed
ILT
1611 }
1612
b8e6aad9
ILT
1613 const Sized_relobj<32, false>* object = relinfo->object;
1614
1b64748b 1615 // Get the GOT offset if needed.
96f2030e
ILT
1616 // The GOT pointer points to the end of the GOT section.
1617 // We need to subtract the size of the GOT section to get
1618 // the actual offset to use in the relocation.
1b64748b
ILT
1619 bool have_got_offset = false;
1620 unsigned int got_offset = 0;
1621 switch (r_type)
1622 {
1623 case elfcpp::R_386_GOT32:
1624 if (gsym != NULL)
1625 {
0a65a3a7
CC
1626 gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1627 got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1628 - target->got_size());
1b64748b
ILT
1629 }
1630 else
1631 {
1632 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
0a65a3a7
CC
1633 gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1634 got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1635 - target->got_size());
1b64748b
ILT
1636 }
1637 have_got_offset = true;
1638 break;
1639
1640 default:
1641 break;
1642 }
1643
61ba1cf9
ILT
1644 switch (r_type)
1645 {
1646 case elfcpp::R_386_NONE:
92e059d8
ILT
1647 case elfcpp::R_386_GNU_VTINHERIT:
1648 case elfcpp::R_386_GNU_VTENTRY:
61ba1cf9
ILT
1649 break;
1650
1651 case elfcpp::R_386_32:
0700cf32 1652 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true))
86849f1f 1653 Relocate_functions<32, false>::rel32(view, object, psymval);
61ba1cf9
ILT
1654 break;
1655
1656 case elfcpp::R_386_PC32:
d61c6bd4 1657 {
0700cf32
ILT
1658 int ref_flags = Symbol::NON_PIC_REF;
1659 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1660 ref_flags |= Symbol::FUNCTION_CALL;
1661 if (should_apply_static_reloc(gsym, ref_flags, true))
d61c6bd4
ILT
1662 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
1663 }
92e059d8
ILT
1664 break;
1665
1666 case elfcpp::R_386_16:
0700cf32 1667 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
86849f1f 1668 Relocate_functions<32, false>::rel16(view, object, psymval);
92e059d8
ILT
1669 break;
1670
1671 case elfcpp::R_386_PC16:
d61c6bd4 1672 {
0700cf32
ILT
1673 int ref_flags = Symbol::NON_PIC_REF;
1674 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1675 ref_flags |= Symbol::FUNCTION_CALL;
1676 if (should_apply_static_reloc(gsym, ref_flags, false))
d09e9154 1677 Relocate_functions<32, false>::pcrel16(view, object, psymval, address);
d61c6bd4 1678 }
61ba1cf9
ILT
1679 break;
1680
92e059d8 1681 case elfcpp::R_386_8:
0700cf32 1682 if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false))
86849f1f 1683 Relocate_functions<32, false>::rel8(view, object, psymval);
92e059d8
ILT
1684 break;
1685
1686 case elfcpp::R_386_PC8:
d61c6bd4 1687 {
0700cf32
ILT
1688 int ref_flags = Symbol::NON_PIC_REF;
1689 if (gsym != NULL && gsym->type() == elfcpp::STT_FUNC)
1690 ref_flags |= Symbol::FUNCTION_CALL;
1691 if (should_apply_static_reloc(gsym, ref_flags, false))
d09e9154 1692 Relocate_functions<32, false>::pcrel8(view, object, psymval, address);
d61c6bd4 1693 }
92e059d8
ILT
1694 break;
1695
ead1e424 1696 case elfcpp::R_386_PLT32:
df2efe71
ILT
1697 gold_assert(gsym == NULL
1698 || gsym->has_plt_offset()
99f8faca
ILT
1699 || gsym->final_value_is_known()
1700 || (gsym->is_defined()
1701 && !gsym->is_from_dynobj()
1702 && !gsym->is_preemptible()));
b8e6aad9 1703 Relocate_functions<32, false>::pcrel32(view, object, psymval, address);
ead1e424
ILT
1704 break;
1705
1706 case elfcpp::R_386_GOT32:
1b64748b
ILT
1707 gold_assert(have_got_offset);
1708 Relocate_functions<32, false>::rel32(view, got_offset);
ead1e424
ILT
1709 break;
1710
1711 case elfcpp::R_386_GOTOFF:
b8e6aad9
ILT
1712 {
1713 elfcpp::Elf_types<32>::Elf_Addr value;
1714 value = (psymval->value(object, 0)
96f2030e 1715 - target->got_plt_section()->address());
b8e6aad9
ILT
1716 Relocate_functions<32, false>::rel32(view, value);
1717 }
ead1e424
ILT
1718 break;
1719
1720 case elfcpp::R_386_GOTPC:
b8e6aad9
ILT
1721 {
1722 elfcpp::Elf_types<32>::Elf_Addr value;
96f2030e 1723 value = target->got_plt_section()->address();
b8e6aad9
ILT
1724 Relocate_functions<32, false>::pcrel32(view, value, address);
1725 }
ead1e424
ILT
1726 break;
1727
92e059d8
ILT
1728 case elfcpp::R_386_COPY:
1729 case elfcpp::R_386_GLOB_DAT:
1730 case elfcpp::R_386_JUMP_SLOT:
1731 case elfcpp::R_386_RELATIVE:
d61c17ea
ILT
1732 // These are outstanding tls relocs, which are unexpected when
1733 // linking.
92e059d8
ILT
1734 case elfcpp::R_386_TLS_TPOFF:
1735 case elfcpp::R_386_TLS_DTPMOD32:
1736 case elfcpp::R_386_TLS_DTPOFF32:
1737 case elfcpp::R_386_TLS_TPOFF32:
1738 case elfcpp::R_386_TLS_DESC:
75f2446e
ILT
1739 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1740 _("unexpected reloc %u in object file"),
1741 r_type);
92e059d8
ILT
1742 break;
1743
d61c17ea
ILT
1744 // These are initial tls relocs, which are expected when
1745 // linking.
56622147
ILT
1746 case elfcpp::R_386_TLS_GD: // Global-dynamic
1747 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1748 case elfcpp::R_386_TLS_DESC_CALL:
1749 case elfcpp::R_386_TLS_LDM: // Local-dynamic
1750 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
1751 case elfcpp::R_386_TLS_IE: // Initial-exec
92e059d8 1752 case elfcpp::R_386_TLS_IE_32:
56622147
ILT
1753 case elfcpp::R_386_TLS_GOTIE:
1754 case elfcpp::R_386_TLS_LE: // Local-exec
92e059d8 1755 case elfcpp::R_386_TLS_LE_32:
07f397ab
ILT
1756 this->relocate_tls(relinfo, target, relnum, rel, r_type, gsym, psymval,
1757 view, address, view_size);
92e059d8
ILT
1758 break;
1759
92e059d8
ILT
1760 case elfcpp::R_386_32PLT:
1761 case elfcpp::R_386_TLS_GD_32:
1762 case elfcpp::R_386_TLS_GD_PUSH:
1763 case elfcpp::R_386_TLS_GD_CALL:
1764 case elfcpp::R_386_TLS_GD_POP:
1765 case elfcpp::R_386_TLS_LDM_32:
1766 case elfcpp::R_386_TLS_LDM_PUSH:
1767 case elfcpp::R_386_TLS_LDM_CALL:
1768 case elfcpp::R_386_TLS_LDM_POP:
1769 case elfcpp::R_386_USED_BY_INTEL_200:
61ba1cf9 1770 default:
75f2446e
ILT
1771 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1772 _("unsupported reloc %u"),
1773 r_type);
92e059d8
ILT
1774 break;
1775 }
ead1e424
ILT
1776
1777 return true;
92e059d8
ILT
1778}
1779
1780// Perform a TLS relocation.
1781
1782inline void
1783Target_i386::Relocate::relocate_tls(const Relocate_info<32, false>* relinfo,
07f397ab 1784 Target_i386* target,
92e059d8
ILT
1785 size_t relnum,
1786 const elfcpp::Rel<32, false>& rel,
1787 unsigned int r_type,
c06b7b0b 1788 const Sized_symbol<32>* gsym,
b8e6aad9 1789 const Symbol_value<32>* psymval,
92e059d8
ILT
1790 unsigned char* view,
1791 elfcpp::Elf_types<32>::Elf_Addr,
fe8718a4 1792 section_size_type view_size)
92e059d8
ILT
1793{
1794 Output_segment* tls_segment = relinfo->layout->tls_segment();
92e059d8 1795
07f397ab
ILT
1796 const Sized_relobj<32, false>* object = relinfo->object;
1797
1798 elfcpp::Elf_types<32>::Elf_Addr value = psymval->value(object, 0);
b8e6aad9 1799
8851ecca
ILT
1800 const bool is_final =
1801 (gsym == NULL
1802 ? !parameters->options().output_is_position_independent()
1803 : gsym->final_value_is_known());
af6359d5
ILT
1804 const tls::Tls_optimization optimized_type
1805 = Target_i386::optimize_tls_reloc(is_final, r_type);
92e059d8
ILT
1806 switch (r_type)
1807 {
56622147 1808 case elfcpp::R_386_TLS_GD: // Global-dynamic
af6359d5 1809 if (optimized_type == tls::TLSOPT_TO_LE)
92e059d8 1810 {
07f397ab 1811 gold_assert(tls_segment != NULL);
56622147
ILT
1812 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1813 rel, r_type, value, view,
1814 view_size);
92e059d8
ILT
1815 break;
1816 }
07f397ab
ILT
1817 else
1818 {
c2b45e22
CC
1819 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1820 ? GOT_TYPE_TLS_NOFFSET
1821 : GOT_TYPE_TLS_PAIR);
07f397ab
ILT
1822 unsigned int got_offset;
1823 if (gsym != NULL)
1824 {
c2b45e22
CC
1825 gold_assert(gsym->has_got_offset(got_type));
1826 got_offset = gsym->got_offset(got_type) - target->got_size();
07f397ab
ILT
1827 }
1828 else
1829 {
1830 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
1831 gold_assert(object->local_has_got_offset(r_sym, got_type));
1832 got_offset = (object->local_got_offset(r_sym, got_type)
07f397ab
ILT
1833 - target->got_size());
1834 }
1835 if (optimized_type == tls::TLSOPT_TO_IE)
1836 {
1837 gold_assert(tls_segment != NULL);
7bf1f802
ILT
1838 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1839 got_offset, view, view_size);
07f397ab
ILT
1840 break;
1841 }
1842 else if (optimized_type == tls::TLSOPT_NONE)
1843 {
1844 // Relocate the field with the offset of the pair of GOT
1845 // entries.
1846 Relocate_functions<32, false>::rel32(view, got_offset);
1847 break;
1848 }
1849 }
75f2446e
ILT
1850 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1851 _("unsupported reloc %u"),
1852 r_type);
92e059d8
ILT
1853 break;
1854
56622147
ILT
1855 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1856 case elfcpp::R_386_TLS_DESC_CALL:
497897f9 1857 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
c2b45e22
CC
1858 if (optimized_type == tls::TLSOPT_TO_LE)
1859 {
1860 gold_assert(tls_segment != NULL);
1861 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1862 rel, r_type, value, view,
1863 view_size);
1864 break;
1865 }
1866 else
1867 {
1868 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1869 ? GOT_TYPE_TLS_NOFFSET
1870 : GOT_TYPE_TLS_DESC);
1871 unsigned int got_offset;
1872 if (gsym != NULL)
1873 {
1874 gold_assert(gsym->has_got_offset(got_type));
1875 got_offset = gsym->got_offset(got_type) - target->got_size();
1876 }
1877 else
1878 {
1879 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1880 gold_assert(object->local_has_got_offset(r_sym, got_type));
1881 got_offset = (object->local_got_offset(r_sym, got_type)
1882 - target->got_size());
1883 }
1884 if (optimized_type == tls::TLSOPT_TO_IE)
1885 {
1886 gold_assert(tls_segment != NULL);
1887 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1888 got_offset, view, view_size);
1889 break;
1890 }
1891 else if (optimized_type == tls::TLSOPT_NONE)
1892 {
1893 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1894 {
1895 // Relocate the field with the offset of the pair of GOT
1896 // entries.
1897 Relocate_functions<32, false>::rel32(view, got_offset);
1898 }
1899 break;
1900 }
1901 }
75f2446e
ILT
1902 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1903 _("unsupported reloc %u"),
1904 r_type);
ead1e424
ILT
1905 break;
1906
56622147 1907 case elfcpp::R_386_TLS_LDM: // Local-dynamic
46cf9fa2
ILT
1908 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
1909 {
75f2446e
ILT
1910 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1911 _("both SUN and GNU model "
1912 "TLS relocations"));
1913 break;
46cf9fa2
ILT
1914 }
1915 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
af6359d5 1916 if (optimized_type == tls::TLSOPT_TO_LE)
46cf9fa2 1917 {
07f397ab 1918 gold_assert(tls_segment != NULL);
46cf9fa2
ILT
1919 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
1920 value, view, view_size);
1921 break;
1922 }
07f397ab
ILT
1923 else if (optimized_type == tls::TLSOPT_NONE)
1924 {
1925 // Relocate the field with the offset of the GOT entry for
1926 // the module index.
1927 unsigned int got_offset;
94c4710f
ILT
1928 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
1929 - target->got_size());
07f397ab
ILT
1930 Relocate_functions<32, false>::rel32(view, got_offset);
1931 break;
1932 }
75f2446e
ILT
1933 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1934 _("unsupported reloc %u"),
1935 r_type);
46cf9fa2
ILT
1936 break;
1937
56622147 1938 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
46cf9fa2
ILT
1939 // This reloc can appear in debugging sections, in which case we
1940 // won't see the TLS_LDM reloc. The local_dynamic_type field
1941 // tells us this.
497897f9
ILT
1942 if (optimized_type == tls::TLSOPT_TO_LE
1943 && this->local_dynamic_type_ != LOCAL_DYNAMIC_NONE)
94c4710f
ILT
1944 {
1945 gold_assert(tls_segment != NULL);
1946 value -= tls_segment->memsz();
1947 }
46cf9fa2
ILT
1948 Relocate_functions<32, false>::rel32(view, value);
1949 break;
1950
56622147
ILT
1951 case elfcpp::R_386_TLS_IE: // Initial-exec
1952 case elfcpp::R_386_TLS_GOTIE:
1953 case elfcpp::R_386_TLS_IE_32:
1954 if (optimized_type == tls::TLSOPT_TO_LE)
1955 {
07f397ab 1956 gold_assert(tls_segment != NULL);
56622147
ILT
1957 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
1958 rel, r_type, value, view,
1959 view_size);
1960 break;
1961 }
07f397ab
ILT
1962 else if (optimized_type == tls::TLSOPT_NONE)
1963 {
1964 // Relocate the field with the offset of the GOT entry for
1965 // the tp-relative offset of the symbol.
c2b45e22
CC
1966 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
1967 ? GOT_TYPE_TLS_OFFSET
1968 : GOT_TYPE_TLS_NOFFSET);
07f397ab
ILT
1969 unsigned int got_offset;
1970 if (gsym != NULL)
1971 {
c2b45e22
CC
1972 gold_assert(gsym->has_got_offset(got_type));
1973 got_offset = gsym->got_offset(got_type);
07f397ab
ILT
1974 }
1975 else
1976 {
1977 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
1978 gold_assert(object->local_has_got_offset(r_sym, got_type));
1979 got_offset = object->local_got_offset(r_sym, got_type);
07f397ab
ILT
1980 }
1981 // For the R_386_TLS_IE relocation, we need to apply the
1982 // absolute address of the GOT entry.
1983 if (r_type == elfcpp::R_386_TLS_IE)
1984 got_offset += target->got_plt_section()->address();
1985 // All GOT offsets are relative to the end of the GOT.
1986 got_offset -= target->got_size();
1987 Relocate_functions<32, false>::rel32(view, got_offset);
1988 break;
1989 }
75f2446e
ILT
1990 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1991 _("unsupported reloc %u"),
1992 r_type);
92e059d8 1993 break;
92e059d8 1994
56622147 1995 case elfcpp::R_386_TLS_LE: // Local-exec
7bf1f802
ILT
1996 // If we're creating a shared library, a dynamic relocation will
1997 // have been created for this location, so do not apply it now.
8851ecca 1998 if (!parameters->options().shared())
7bf1f802
ILT
1999 {
2000 gold_assert(tls_segment != NULL);
2001 value -= tls_segment->memsz();
2002 Relocate_functions<32, false>::rel32(view, value);
2003 }
56622147 2004 break;
92e059d8 2005
56622147 2006 case elfcpp::R_386_TLS_LE_32:
7bf1f802
ILT
2007 // If we're creating a shared library, a dynamic relocation will
2008 // have been created for this location, so do not apply it now.
8851ecca 2009 if (!parameters->options().shared())
7bf1f802
ILT
2010 {
2011 gold_assert(tls_segment != NULL);
2012 value = tls_segment->memsz() - value;
2013 Relocate_functions<32, false>::rel32(view, value);
2014 }
56622147 2015 break;
92e059d8 2016 }
92e059d8
ILT
2017}
2018
e041f13d 2019// Do a relocation in which we convert a TLS General-Dynamic to a
ead1e424
ILT
2020// Local-Exec.
2021
2022inline void
2023Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2024 size_t relnum,
2025 Output_segment* tls_segment,
2026 const elfcpp::Rel<32, false>& rel,
2027 unsigned int,
2028 elfcpp::Elf_types<32>::Elf_Addr value,
2029 unsigned char* view,
fe8718a4 2030 section_size_type view_size)
ead1e424
ILT
2031{
2032 // leal foo(,%reg,1),%eax; call ___tls_get_addr
46cf9fa2 2033 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
ead1e424
ILT
2034 // leal foo(%reg),%eax; call ___tls_get_addr
2035 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2036
af6359d5
ILT
2037 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2038 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
ead1e424
ILT
2039
2040 unsigned char op1 = view[-1];
2041 unsigned char op2 = view[-2];
2042
af6359d5
ILT
2043 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2044 op2 == 0x8d || op2 == 0x04);
2045 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
ead1e424
ILT
2046
2047 int roff = 5;
2048
2049 if (op2 == 0x04)
2050 {
af6359d5
ILT
2051 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2052 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2053 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2054 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
ead1e424
ILT
2055 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2056 }
2057 else
2058 {
af6359d5
ILT
2059 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2060 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 2061 if (rel.get_r_offset() + 9 < view_size
d61c17ea 2062 && view[9] == 0x90)
ead1e424
ILT
2063 {
2064 // There is a trailing nop. Use the size byte subl.
2065 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2066 roff = 6;
2067 }
2068 else
2069 {
2070 // Use the five byte subl.
2071 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2072 }
2073 }
2074
7bf1f802 2075 value = tls_segment->memsz() - value;
ead1e424
ILT
2076 Relocate_functions<32, false>::rel32(view + roff, value);
2077
2078 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2079 // We can skip it.
2080 this->skip_call_tls_get_addr_ = true;
2081}
2082
7bf1f802 2083// Do a relocation in which we convert a TLS General-Dynamic to an
07f397ab
ILT
2084// Initial-Exec.
2085
2086inline void
2087Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2088 size_t relnum,
c2b45e22 2089 Output_segment*,
07f397ab
ILT
2090 const elfcpp::Rel<32, false>& rel,
2091 unsigned int,
2092 elfcpp::Elf_types<32>::Elf_Addr value,
2093 unsigned char* view,
fe8718a4 2094 section_size_type view_size)
07f397ab
ILT
2095{
2096 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2097 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2098
2099 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2100 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2101
2102 unsigned char op1 = view[-1];
2103 unsigned char op2 = view[-2];
2104
2105 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2106 op2 == 0x8d || op2 == 0x04);
2107 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2108
2109 int roff = 5;
2110
c2b45e22
CC
2111 // FIXME: For now, support only the first (SIB) form.
2112 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
07f397ab
ILT
2113
2114 if (op2 == 0x04)
2115 {
2116 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2117 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2118 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2119 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2120 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2121 }
2122 else
2123 {
2124 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2125 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 2126 if (rel.get_r_offset() + 9 < view_size
07f397ab
ILT
2127 && view[9] == 0x90)
2128 {
2129 // FIXME: This is not the right instruction sequence.
2130 // There is a trailing nop. Use the size byte subl.
2131 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2132 roff = 6;
2133 }
2134 else
2135 {
2136 // FIXME: This is not the right instruction sequence.
2137 // Use the five byte subl.
2138 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2139 }
2140 }
2141
07f397ab
ILT
2142 Relocate_functions<32, false>::rel32(view + roff, value);
2143
2144 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2145 // We can skip it.
2146 this->skip_call_tls_get_addr_ = true;
2147}
2148
c2b45e22
CC
2149// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2150// General-Dynamic to a Local-Exec.
2151
2152inline void
2153Target_i386::Relocate::tls_desc_gd_to_le(
2154 const Relocate_info<32, false>* relinfo,
2155 size_t relnum,
2156 Output_segment* tls_segment,
2157 const elfcpp::Rel<32, false>& rel,
2158 unsigned int r_type,
2159 elfcpp::Elf_types<32>::Elf_Addr value,
2160 unsigned char* view,
2161 section_size_type view_size)
2162{
2163 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2164 {
2165 // leal foo@TLSDESC(%ebx), %eax
2166 // ==> leal foo@NTPOFF, %eax
2167 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2168 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2169 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2170 view[-2] == 0x8d && view[-1] == 0x83);
2171 view[-1] = 0x05;
2172 value -= tls_segment->memsz();
2173 Relocate_functions<32, false>::rel32(view, value);
2174 }
2175 else
2176 {
2177 // call *foo@TLSCALL(%eax)
2178 // ==> nop; nop
2179 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2180 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2181 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2182 view[0] == 0xff && view[1] == 0x10);
2183 view[0] = 0x66;
2184 view[1] = 0x90;
2185 }
2186}
2187
2188// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2189// General-Dynamic to an Initial-Exec.
2190
2191inline void
2192Target_i386::Relocate::tls_desc_gd_to_ie(
2193 const Relocate_info<32, false>* relinfo,
2194 size_t relnum,
2195 Output_segment*,
2196 const elfcpp::Rel<32, false>& rel,
2197 unsigned int r_type,
2198 elfcpp::Elf_types<32>::Elf_Addr value,
2199 unsigned char* view,
2200 section_size_type view_size)
2201{
2202 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2203 {
2204 // leal foo@TLSDESC(%ebx), %eax
2205 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2206 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2207 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2208 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2209 view[-2] == 0x8d && view[-1] == 0x83);
2210 view[-2] = 0x8b;
2211 Relocate_functions<32, false>::rel32(view, value);
2212 }
2213 else
2214 {
2215 // call *foo@TLSCALL(%eax)
2216 // ==> nop; nop
2217 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2218 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2219 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2220 view[0] == 0xff && view[1] == 0x10);
2221 view[0] = 0x66;
2222 view[1] = 0x90;
2223 }
2224}
2225
46cf9fa2
ILT
2226// Do a relocation in which we convert a TLS Local-Dynamic to a
2227// Local-Exec.
2228
2229inline void
2230Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2231 size_t relnum,
2232 Output_segment*,
2233 const elfcpp::Rel<32, false>& rel,
2234 unsigned int,
2235 elfcpp::Elf_types<32>::Elf_Addr,
2236 unsigned char* view,
fe8718a4 2237 section_size_type view_size)
46cf9fa2
ILT
2238{
2239 // leal foo(%reg), %eax; call ___tls_get_addr
2240 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2241
af6359d5
ILT
2242 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2243 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
46cf9fa2
ILT
2244
2245 // FIXME: Does this test really always pass?
af6359d5
ILT
2246 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2247 view[-2] == 0x8d && view[-1] == 0x83);
46cf9fa2 2248
af6359d5 2249 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
46cf9fa2
ILT
2250
2251 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2252
2253 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2254 // We can skip it.
2255 this->skip_call_tls_get_addr_ = true;
2256}
2257
56622147
ILT
2258// Do a relocation in which we convert a TLS Initial-Exec to a
2259// Local-Exec.
2260
2261inline void
2262Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2263 size_t relnum,
2264 Output_segment* tls_segment,
2265 const elfcpp::Rel<32, false>& rel,
2266 unsigned int r_type,
2267 elfcpp::Elf_types<32>::Elf_Addr value,
2268 unsigned char* view,
fe8718a4 2269 section_size_type view_size)
56622147
ILT
2270{
2271 // We have to actually change the instructions, which means that we
2272 // need to examine the opcodes to figure out which instruction we
2273 // are looking at.
2274 if (r_type == elfcpp::R_386_TLS_IE)
2275 {
2276 // movl %gs:XX,%eax ==> movl $YY,%eax
2277 // movl %gs:XX,%reg ==> movl $YY,%reg
2278 // addl %gs:XX,%reg ==> addl $YY,%reg
2279 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2280 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2281
2282 unsigned char op1 = view[-1];
2283 if (op1 == 0xa1)
2284 {
2285 // movl XX,%eax ==> movl $YY,%eax
2286 view[-1] = 0xb8;
2287 }
2288 else
2289 {
2290 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2291
2292 unsigned char op2 = view[-2];
2293 if (op2 == 0x8b)
2294 {
2295 // movl XX,%reg ==> movl $YY,%reg
2296 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2297 (op1 & 0xc7) == 0x05);
2298 view[-2] = 0xc7;
2299 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2300 }
2301 else if (op2 == 0x03)
2302 {
2303 // addl XX,%reg ==> addl $YY,%reg
2304 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2305 (op1 & 0xc7) == 0x05);
2306 view[-2] = 0x81;
2307 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2308 }
2309 else
2310 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2311 }
2312 }
2313 else
2314 {
2315 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2316 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2317 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2318 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2319 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2320
2321 unsigned char op1 = view[-1];
2322 unsigned char op2 = view[-2];
2323 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2324 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2325 if (op2 == 0x8b)
2326 {
2327 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2328 view[-2] = 0xc7;
2329 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2330 }
2331 else if (op2 == 0x2b)
2332 {
2333 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2334 view[-2] = 0x81;
2335 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2336 }
2337 else if (op2 == 0x03)
2338 {
2339 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2340 view[-2] = 0x81;
2341 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2342 }
2343 else
2344 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2345 }
2346
7bf1f802 2347 value = tls_segment->memsz() - value;
56622147
ILT
2348 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2349 value = - value;
2350
2351 Relocate_functions<32, false>::rel32(view, value);
2352}
2353
61ba1cf9
ILT
2354// Relocate section data.
2355
2356void
92e059d8 2357Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
2358 unsigned int sh_type,
2359 const unsigned char* prelocs,
2360 size_t reloc_count,
730cdc88
ILT
2361 Output_section* output_section,
2362 bool needs_special_offset_handling,
61ba1cf9
ILT
2363 unsigned char* view,
2364 elfcpp::Elf_types<32>::Elf_Addr address,
fe8718a4 2365 section_size_type view_size)
61ba1cf9 2366{
a3ad94ed 2367 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 2368
ead1e424
ILT
2369 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2370 Target_i386::Relocate>(
92e059d8 2371 relinfo,
ead1e424 2372 this,
61ba1cf9
ILT
2373 prelocs,
2374 reloc_count,
730cdc88
ILT
2375 output_section,
2376 needs_special_offset_handling,
61ba1cf9
ILT
2377 view,
2378 address,
2379 view_size);
2380}
2381
6a74a719
ILT
2382// Return the size of a relocation while scanning during a relocatable
2383// link.
2384
2385unsigned int
2386Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2387 unsigned int r_type,
2388 Relobj* object)
2389{
2390 switch (r_type)
2391 {
2392 case elfcpp::R_386_NONE:
2393 case elfcpp::R_386_GNU_VTINHERIT:
2394 case elfcpp::R_386_GNU_VTENTRY:
2395 case elfcpp::R_386_TLS_GD: // Global-dynamic
2396 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2397 case elfcpp::R_386_TLS_DESC_CALL:
2398 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2399 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2400 case elfcpp::R_386_TLS_IE: // Initial-exec
2401 case elfcpp::R_386_TLS_IE_32:
2402 case elfcpp::R_386_TLS_GOTIE:
2403 case elfcpp::R_386_TLS_LE: // Local-exec
2404 case elfcpp::R_386_TLS_LE_32:
2405 return 0;
2406
2407 case elfcpp::R_386_32:
2408 case elfcpp::R_386_PC32:
2409 case elfcpp::R_386_GOT32:
2410 case elfcpp::R_386_PLT32:
2411 case elfcpp::R_386_GOTOFF:
2412 case elfcpp::R_386_GOTPC:
2413 return 4;
2414
2415 case elfcpp::R_386_16:
2416 case elfcpp::R_386_PC16:
2417 return 2;
2418
2419 case elfcpp::R_386_8:
2420 case elfcpp::R_386_PC8:
2421 return 1;
2422
2423 // These are relocations which should only be seen by the
2424 // dynamic linker, and should never be seen here.
2425 case elfcpp::R_386_COPY:
2426 case elfcpp::R_386_GLOB_DAT:
2427 case elfcpp::R_386_JUMP_SLOT:
2428 case elfcpp::R_386_RELATIVE:
2429 case elfcpp::R_386_TLS_TPOFF:
2430 case elfcpp::R_386_TLS_DTPMOD32:
2431 case elfcpp::R_386_TLS_DTPOFF32:
2432 case elfcpp::R_386_TLS_TPOFF32:
2433 case elfcpp::R_386_TLS_DESC:
2434 object->error(_("unexpected reloc %u in object file"), r_type);
2435 return 0;
2436
2437 case elfcpp::R_386_32PLT:
2438 case elfcpp::R_386_TLS_GD_32:
2439 case elfcpp::R_386_TLS_GD_PUSH:
2440 case elfcpp::R_386_TLS_GD_CALL:
2441 case elfcpp::R_386_TLS_GD_POP:
2442 case elfcpp::R_386_TLS_LDM_32:
2443 case elfcpp::R_386_TLS_LDM_PUSH:
2444 case elfcpp::R_386_TLS_LDM_CALL:
2445 case elfcpp::R_386_TLS_LDM_POP:
2446 case elfcpp::R_386_USED_BY_INTEL_200:
2447 default:
2448 object->error(_("unsupported reloc %u in object file"), r_type);
2449 return 0;
2450 }
2451}
2452
2453// Scan the relocs during a relocatable link.
2454
2455void
2456Target_i386::scan_relocatable_relocs(const General_options& options,
2457 Symbol_table* symtab,
2458 Layout* layout,
2459 Sized_relobj<32, false>* object,
2460 unsigned int data_shndx,
2461 unsigned int sh_type,
2462 const unsigned char* prelocs,
2463 size_t reloc_count,
2464 Output_section* output_section,
2465 bool needs_special_offset_handling,
2466 size_t local_symbol_count,
2467 const unsigned char* plocal_symbols,
2468 Relocatable_relocs* rr)
2469{
2470 gold_assert(sh_type == elfcpp::SHT_REL);
2471
2472 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2473 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2474
7019cd25 2475 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
6a74a719
ILT
2476 Scan_relocatable_relocs>(
2477 options,
2478 symtab,
2479 layout,
2480 object,
2481 data_shndx,
2482 prelocs,
2483 reloc_count,
2484 output_section,
2485 needs_special_offset_handling,
2486 local_symbol_count,
2487 plocal_symbols,
2488 rr);
2489}
2490
2491// Relocate a section during a relocatable link.
2492
2493void
2494Target_i386::relocate_for_relocatable(
2495 const Relocate_info<32, false>* relinfo,
2496 unsigned int sh_type,
2497 const unsigned char* prelocs,
2498 size_t reloc_count,
2499 Output_section* output_section,
2500 off_t offset_in_output_section,
2501 const Relocatable_relocs* rr,
2502 unsigned char* view,
2503 elfcpp::Elf_types<32>::Elf_Addr view_address,
2504 section_size_type view_size,
2505 unsigned char* reloc_view,
2506 section_size_type reloc_view_size)
2507{
2508 gold_assert(sh_type == elfcpp::SHT_REL);
2509
7019cd25 2510 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
6a74a719
ILT
2511 relinfo,
2512 prelocs,
2513 reloc_count,
2514 output_section,
2515 offset_in_output_section,
2516 rr,
2517 view,
2518 view_address,
2519 view_size,
2520 reloc_view,
2521 reloc_view_size);
2522}
2523
ab5c9e90
ILT
2524// Return the value to use for a dynamic which requires special
2525// treatment. This is how we support equality comparisons of function
2526// pointers across shared library boundaries, as described in the
2527// processor specific ABI supplement.
2528
2529uint64_t
2530Target_i386::do_dynsym_value(const Symbol* gsym) const
2531{
2532 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2533 return this->plt_section()->address() + gsym->plt_offset();
2534}
2535
c51e6221
ILT
2536// Return a string used to fill a code section with nops to take up
2537// the specified length.
2538
2539std::string
8851ecca 2540Target_i386::do_code_fill(section_size_type length) const
c51e6221
ILT
2541{
2542 if (length >= 16)
2543 {
2544 // Build a jmp instruction to skip over the bytes.
2545 unsigned char jmp[5];
2546 jmp[0] = 0xe9;
2547 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2548 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2549 + std::string(length - 5, '\0'));
2550 }
2551
2552 // Nop sequences of various lengths.
2553 const char nop1[1] = { 0x90 }; // nop
2554 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2555 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2556 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2557 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2558 0x00 }; // leal 0(%esi,1),%esi
2559 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2560 0x00, 0x00 };
2561 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2562 0x00, 0x00, 0x00 };
2563 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2564 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2565 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2566 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2567 0x00 };
2568 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2569 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2570 0x00, 0x00 };
2571 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2572 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2573 0x00, 0x00, 0x00 };
2574 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2575 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2576 0x00, 0x00, 0x00, 0x00 };
2577 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2578 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2579 0x27, 0x00, 0x00, 0x00,
2580 0x00 };
2581 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2582 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2583 0xbc, 0x27, 0x00, 0x00,
2584 0x00, 0x00 };
2585 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2586 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2587 0x90, 0x90, 0x90, 0x90,
2588 0x90, 0x90, 0x90 };
2589
2590 const char* nops[16] = {
2591 NULL,
2592 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2593 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2594 };
2595
2596 return std::string(nops[length], length);
2597}
2598
14bfc3f5
ILT
2599// The selector for i386 object files.
2600
2601class Target_selector_i386 : public Target_selector
2602{
2603public:
2604 Target_selector_i386()
e96caa79 2605 : Target_selector(elfcpp::EM_386, 32, false, "elf32-i386")
14bfc3f5
ILT
2606 { }
2607
2608 Target*
e96caa79
ILT
2609 do_instantiate_target()
2610 { return new Target_i386(); }
14bfc3f5
ILT
2611};
2612
14bfc3f5
ILT
2613Target_selector_i386 target_selector_i386;
2614
2615} // End anonymous namespace.
This page took 0.219227 seconds and 4 git commands to generate.