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