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