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