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