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