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