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