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