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