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