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