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