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