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