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