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