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