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