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