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