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