* sparc.cc (Target_sparc::Scan::local): Do not emit relocs other than
[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,
1612 this->rel_dyn_, true);
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
8851ecca
ILT
1900 const bool is_final =
1901 (gsym == NULL
1902 ? !parameters->options().output_is_position_independent()
1903 : gsym->final_value_is_known());
af6359d5
ILT
1904 const tls::Tls_optimization optimized_type
1905 = Target_i386::optimize_tls_reloc(is_final, r_type);
92e059d8
ILT
1906 switch (r_type)
1907 {
56622147 1908 case elfcpp::R_386_TLS_GD: // Global-dynamic
af6359d5 1909 if (optimized_type == tls::TLSOPT_TO_LE)
92e059d8 1910 {
07f397ab 1911 gold_assert(tls_segment != NULL);
56622147
ILT
1912 this->tls_gd_to_le(relinfo, relnum, tls_segment,
1913 rel, r_type, value, view,
1914 view_size);
92e059d8
ILT
1915 break;
1916 }
07f397ab
ILT
1917 else
1918 {
c2b45e22
CC
1919 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1920 ? GOT_TYPE_TLS_NOFFSET
1921 : GOT_TYPE_TLS_PAIR);
07f397ab
ILT
1922 unsigned int got_offset;
1923 if (gsym != NULL)
1924 {
c2b45e22
CC
1925 gold_assert(gsym->has_got_offset(got_type));
1926 got_offset = gsym->got_offset(got_type) - target->got_size();
07f397ab
ILT
1927 }
1928 else
1929 {
1930 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
1931 gold_assert(object->local_has_got_offset(r_sym, got_type));
1932 got_offset = (object->local_got_offset(r_sym, got_type)
07f397ab
ILT
1933 - target->got_size());
1934 }
1935 if (optimized_type == tls::TLSOPT_TO_IE)
1936 {
1937 gold_assert(tls_segment != NULL);
7bf1f802
ILT
1938 this->tls_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1939 got_offset, view, view_size);
07f397ab
ILT
1940 break;
1941 }
1942 else if (optimized_type == tls::TLSOPT_NONE)
1943 {
1944 // Relocate the field with the offset of the pair of GOT
1945 // entries.
1946 Relocate_functions<32, false>::rel32(view, got_offset);
1947 break;
1948 }
1949 }
75f2446e
ILT
1950 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
1951 _("unsupported reloc %u"),
1952 r_type);
92e059d8
ILT
1953 break;
1954
56622147
ILT
1955 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
1956 case elfcpp::R_386_TLS_DESC_CALL:
497897f9 1957 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
c2b45e22
CC
1958 if (optimized_type == tls::TLSOPT_TO_LE)
1959 {
1960 gold_assert(tls_segment != NULL);
1961 this->tls_desc_gd_to_le(relinfo, relnum, tls_segment,
1962 rel, r_type, value, view,
1963 view_size);
1964 break;
1965 }
1966 else
1967 {
1968 unsigned int got_type = (optimized_type == tls::TLSOPT_TO_IE
1969 ? GOT_TYPE_TLS_NOFFSET
1970 : GOT_TYPE_TLS_DESC);
1971 unsigned int got_offset;
1972 if (gsym != NULL)
1973 {
1974 gold_assert(gsym->has_got_offset(got_type));
1975 got_offset = gsym->got_offset(got_type) - target->got_size();
1976 }
1977 else
1978 {
1979 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1980 gold_assert(object->local_has_got_offset(r_sym, got_type));
1981 got_offset = (object->local_got_offset(r_sym, got_type)
1982 - target->got_size());
1983 }
1984 if (optimized_type == tls::TLSOPT_TO_IE)
1985 {
1986 gold_assert(tls_segment != NULL);
1987 this->tls_desc_gd_to_ie(relinfo, relnum, tls_segment, rel, r_type,
1988 got_offset, view, view_size);
1989 break;
1990 }
1991 else if (optimized_type == tls::TLSOPT_NONE)
1992 {
1993 if (r_type == elfcpp::R_386_TLS_GOTDESC)
1994 {
1995 // Relocate the field with the offset of the pair of GOT
1996 // entries.
1997 Relocate_functions<32, false>::rel32(view, got_offset);
1998 }
1999 break;
2000 }
2001 }
75f2446e
ILT
2002 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2003 _("unsupported reloc %u"),
2004 r_type);
ead1e424
ILT
2005 break;
2006
56622147 2007 case elfcpp::R_386_TLS_LDM: // Local-dynamic
46cf9fa2
ILT
2008 if (this->local_dynamic_type_ == LOCAL_DYNAMIC_SUN)
2009 {
75f2446e
ILT
2010 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2011 _("both SUN and GNU model "
2012 "TLS relocations"));
2013 break;
46cf9fa2
ILT
2014 }
2015 this->local_dynamic_type_ = LOCAL_DYNAMIC_GNU;
af6359d5 2016 if (optimized_type == tls::TLSOPT_TO_LE)
46cf9fa2 2017 {
07f397ab 2018 gold_assert(tls_segment != NULL);
46cf9fa2
ILT
2019 this->tls_ld_to_le(relinfo, relnum, tls_segment, rel, r_type,
2020 value, view, view_size);
2021 break;
2022 }
07f397ab
ILT
2023 else if (optimized_type == tls::TLSOPT_NONE)
2024 {
2025 // Relocate the field with the offset of the GOT entry for
2026 // the module index.
2027 unsigned int got_offset;
94c4710f
ILT
2028 got_offset = (target->got_mod_index_entry(NULL, NULL, NULL)
2029 - target->got_size());
07f397ab
ILT
2030 Relocate_functions<32, false>::rel32(view, got_offset);
2031 break;
2032 }
75f2446e
ILT
2033 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2034 _("unsupported reloc %u"),
2035 r_type);
46cf9fa2
ILT
2036 break;
2037
56622147 2038 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
d6f22b98 2039 if (optimized_type == tls::TLSOPT_TO_LE)
e8a9fcda 2040 {
82bb573a
ILT
2041 // This reloc can appear in debugging sections, in which
2042 // case we must not convert to local-exec. We decide what
2043 // to do based on whether the section is marked as
2044 // containing executable code. That is what the GNU linker
2045 // does as well.
2046 elfcpp::Shdr<32, false> shdr(relinfo->data_shdr);
2047 if ((shdr.get_sh_flags() & elfcpp::SHF_EXECINSTR) != 0)
d6f22b98
ILT
2048 {
2049 gold_assert(tls_segment != NULL);
2050 value -= tls_segment->memsz();
2051 }
e8a9fcda 2052 }
46cf9fa2
ILT
2053 Relocate_functions<32, false>::rel32(view, value);
2054 break;
2055
56622147
ILT
2056 case elfcpp::R_386_TLS_IE: // Initial-exec
2057 case elfcpp::R_386_TLS_GOTIE:
2058 case elfcpp::R_386_TLS_IE_32:
2059 if (optimized_type == tls::TLSOPT_TO_LE)
2060 {
07f397ab 2061 gold_assert(tls_segment != NULL);
56622147
ILT
2062 Target_i386::Relocate::tls_ie_to_le(relinfo, relnum, tls_segment,
2063 rel, r_type, value, view,
2064 view_size);
2065 break;
2066 }
07f397ab
ILT
2067 else if (optimized_type == tls::TLSOPT_NONE)
2068 {
2069 // Relocate the field with the offset of the GOT entry for
2070 // the tp-relative offset of the symbol.
c2b45e22
CC
2071 unsigned int got_type = (r_type == elfcpp::R_386_TLS_IE_32
2072 ? GOT_TYPE_TLS_OFFSET
2073 : GOT_TYPE_TLS_NOFFSET);
07f397ab
ILT
2074 unsigned int got_offset;
2075 if (gsym != NULL)
2076 {
c2b45e22
CC
2077 gold_assert(gsym->has_got_offset(got_type));
2078 got_offset = gsym->got_offset(got_type);
07f397ab
ILT
2079 }
2080 else
2081 {
2082 unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
c2b45e22
CC
2083 gold_assert(object->local_has_got_offset(r_sym, got_type));
2084 got_offset = object->local_got_offset(r_sym, got_type);
07f397ab
ILT
2085 }
2086 // For the R_386_TLS_IE relocation, we need to apply the
2087 // absolute address of the GOT entry.
2088 if (r_type == elfcpp::R_386_TLS_IE)
2089 got_offset += target->got_plt_section()->address();
2090 // All GOT offsets are relative to the end of the GOT.
2091 got_offset -= target->got_size();
2092 Relocate_functions<32, false>::rel32(view, got_offset);
2093 break;
2094 }
75f2446e
ILT
2095 gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2096 _("unsupported reloc %u"),
2097 r_type);
92e059d8 2098 break;
92e059d8 2099
56622147 2100 case elfcpp::R_386_TLS_LE: // Local-exec
7bf1f802
ILT
2101 // If we're creating a shared library, a dynamic relocation will
2102 // have been created for this location, so do not apply it now.
8851ecca 2103 if (!parameters->options().shared())
7bf1f802
ILT
2104 {
2105 gold_assert(tls_segment != NULL);
2106 value -= tls_segment->memsz();
2107 Relocate_functions<32, false>::rel32(view, value);
2108 }
56622147 2109 break;
92e059d8 2110
56622147 2111 case elfcpp::R_386_TLS_LE_32:
7bf1f802
ILT
2112 // If we're creating a shared library, a dynamic relocation will
2113 // have been created for this location, so do not apply it now.
8851ecca 2114 if (!parameters->options().shared())
7bf1f802
ILT
2115 {
2116 gold_assert(tls_segment != NULL);
2117 value = tls_segment->memsz() - value;
2118 Relocate_functions<32, false>::rel32(view, value);
2119 }
56622147 2120 break;
92e059d8 2121 }
92e059d8
ILT
2122}
2123
e041f13d 2124// Do a relocation in which we convert a TLS General-Dynamic to a
ead1e424
ILT
2125// Local-Exec.
2126
2127inline void
2128Target_i386::Relocate::tls_gd_to_le(const Relocate_info<32, false>* relinfo,
2129 size_t relnum,
2130 Output_segment* tls_segment,
2131 const elfcpp::Rel<32, false>& rel,
2132 unsigned int,
2133 elfcpp::Elf_types<32>::Elf_Addr value,
2134 unsigned char* view,
fe8718a4 2135 section_size_type view_size)
ead1e424
ILT
2136{
2137 // leal foo(,%reg,1),%eax; call ___tls_get_addr
46cf9fa2 2138 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
ead1e424
ILT
2139 // leal foo(%reg),%eax; call ___tls_get_addr
2140 // ==> movl %gs:0,%eax; subl $foo@tpoff,%eax
2141
af6359d5
ILT
2142 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2143 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
ead1e424
ILT
2144
2145 unsigned char op1 = view[-1];
2146 unsigned char op2 = view[-2];
2147
af6359d5
ILT
2148 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2149 op2 == 0x8d || op2 == 0x04);
2150 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
ead1e424
ILT
2151
2152 int roff = 5;
2153
2154 if (op2 == 0x04)
2155 {
af6359d5
ILT
2156 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2157 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2158 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2159 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
ead1e424
ILT
2160 memcpy(view - 3, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2161 }
2162 else
2163 {
af6359d5
ILT
2164 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2165 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 2166 if (rel.get_r_offset() + 9 < view_size
d61c17ea 2167 && view[9] == 0x90)
ead1e424
ILT
2168 {
2169 // There is a trailing nop. Use the size byte subl.
2170 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2171 roff = 6;
2172 }
2173 else
2174 {
2175 // Use the five byte subl.
2176 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2177 }
2178 }
2179
7bf1f802 2180 value = tls_segment->memsz() - value;
ead1e424
ILT
2181 Relocate_functions<32, false>::rel32(view + roff, value);
2182
2183 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2184 // We can skip it.
2185 this->skip_call_tls_get_addr_ = true;
2186}
2187
7bf1f802 2188// Do a relocation in which we convert a TLS General-Dynamic to an
07f397ab
ILT
2189// Initial-Exec.
2190
2191inline void
2192Target_i386::Relocate::tls_gd_to_ie(const Relocate_info<32, false>* relinfo,
2193 size_t relnum,
c2b45e22 2194 Output_segment*,
07f397ab
ILT
2195 const elfcpp::Rel<32, false>& rel,
2196 unsigned int,
2197 elfcpp::Elf_types<32>::Elf_Addr value,
2198 unsigned char* view,
fe8718a4 2199 section_size_type view_size)
07f397ab
ILT
2200{
2201 // leal foo(,%ebx,1),%eax; call ___tls_get_addr
2202 // ==> movl %gs:0,%eax; addl foo@gotntpoff(%ebx),%eax
2203
2204 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2205 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
2206
2207 unsigned char op1 = view[-1];
2208 unsigned char op2 = view[-2];
2209
2210 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2211 op2 == 0x8d || op2 == 0x04);
2212 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
2213
2214 int roff = 5;
2215
c2b45e22
CC
2216 // FIXME: For now, support only the first (SIB) form.
2217 tls::check_tls(relinfo, relnum, rel.get_r_offset(), op2 == 0x04);
07f397ab
ILT
2218
2219 if (op2 == 0x04)
2220 {
2221 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -3);
2222 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[-3] == 0x8d);
2223 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2224 ((op1 & 0xc7) == 0x05 && op1 != (4 << 3)));
2225 memcpy(view - 3, "\x65\xa1\0\0\0\0\x03\x83\0\0\0", 12);
2226 }
2227 else
2228 {
2229 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2230 (op1 & 0xf8) == 0x80 && (op1 & 7) != 4);
fe8718a4 2231 if (rel.get_r_offset() + 9 < view_size
07f397ab
ILT
2232 && view[9] == 0x90)
2233 {
2234 // FIXME: This is not the right instruction sequence.
2235 // There is a trailing nop. Use the size byte subl.
2236 memcpy(view - 2, "\x65\xa1\0\0\0\0\x81\xe8\0\0\0", 12);
2237 roff = 6;
2238 }
2239 else
2240 {
2241 // FIXME: This is not the right instruction sequence.
2242 // Use the five byte subl.
2243 memcpy(view - 2, "\x65\xa1\0\0\0\0\x2d\0\0\0", 11);
2244 }
2245 }
2246
07f397ab
ILT
2247 Relocate_functions<32, false>::rel32(view + roff, value);
2248
2249 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2250 // We can skip it.
2251 this->skip_call_tls_get_addr_ = true;
2252}
2253
c2b45e22
CC
2254// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2255// General-Dynamic to a Local-Exec.
2256
2257inline void
2258Target_i386::Relocate::tls_desc_gd_to_le(
2259 const Relocate_info<32, false>* relinfo,
2260 size_t relnum,
2261 Output_segment* tls_segment,
2262 const elfcpp::Rel<32, false>& rel,
2263 unsigned int r_type,
2264 elfcpp::Elf_types<32>::Elf_Addr value,
2265 unsigned char* view,
2266 section_size_type view_size)
2267{
2268 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2269 {
2270 // leal foo@TLSDESC(%ebx), %eax
2271 // ==> leal foo@NTPOFF, %eax
2272 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2273 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2274 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2275 view[-2] == 0x8d && view[-1] == 0x83);
2276 view[-1] = 0x05;
2277 value -= tls_segment->memsz();
2278 Relocate_functions<32, false>::rel32(view, value);
2279 }
2280 else
2281 {
2282 // call *foo@TLSCALL(%eax)
2283 // ==> nop; nop
2284 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2285 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2286 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2287 view[0] == 0xff && view[1] == 0x10);
2288 view[0] = 0x66;
2289 view[1] = 0x90;
2290 }
2291}
2292
2293// Do a relocation in which we convert a TLS_GOTDESC or TLS_DESC_CALL
2294// General-Dynamic to an Initial-Exec.
2295
2296inline void
2297Target_i386::Relocate::tls_desc_gd_to_ie(
2298 const Relocate_info<32, false>* relinfo,
2299 size_t relnum,
2300 Output_segment*,
2301 const elfcpp::Rel<32, false>& rel,
2302 unsigned int r_type,
2303 elfcpp::Elf_types<32>::Elf_Addr value,
2304 unsigned char* view,
2305 section_size_type view_size)
2306{
2307 if (r_type == elfcpp::R_386_TLS_GOTDESC)
2308 {
2309 // leal foo@TLSDESC(%ebx), %eax
2310 // ==> movl foo@GOTNTPOFF(%ebx), %eax
2311 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2312 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2313 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2314 view[-2] == 0x8d && view[-1] == 0x83);
2315 view[-2] = 0x8b;
2316 Relocate_functions<32, false>::rel32(view, value);
2317 }
2318 else
2319 {
2320 // call *foo@TLSCALL(%eax)
2321 // ==> nop; nop
2322 gold_assert(r_type == elfcpp::R_386_TLS_DESC_CALL);
2323 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 2);
2324 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2325 view[0] == 0xff && view[1] == 0x10);
2326 view[0] = 0x66;
2327 view[1] = 0x90;
2328 }
2329}
2330
46cf9fa2
ILT
2331// Do a relocation in which we convert a TLS Local-Dynamic to a
2332// Local-Exec.
2333
2334inline void
2335Target_i386::Relocate::tls_ld_to_le(const Relocate_info<32, false>* relinfo,
2336 size_t relnum,
2337 Output_segment*,
2338 const elfcpp::Rel<32, false>& rel,
2339 unsigned int,
2340 elfcpp::Elf_types<32>::Elf_Addr,
2341 unsigned char* view,
fe8718a4 2342 section_size_type view_size)
46cf9fa2
ILT
2343{
2344 // leal foo(%reg), %eax; call ___tls_get_addr
2345 // ==> movl %gs:0,%eax; nop; leal 0(%esi,1),%esi
2346
af6359d5
ILT
2347 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2348 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 9);
46cf9fa2
ILT
2349
2350 // FIXME: Does this test really always pass?
af6359d5
ILT
2351 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2352 view[-2] == 0x8d && view[-1] == 0x83);
46cf9fa2 2353
af6359d5 2354 tls::check_tls(relinfo, relnum, rel.get_r_offset(), view[4] == 0xe8);
46cf9fa2
ILT
2355
2356 memcpy(view - 2, "\x65\xa1\0\0\0\0\x90\x8d\x74\x26\0", 11);
2357
2358 // The next reloc should be a PLT32 reloc against __tls_get_addr.
2359 // We can skip it.
2360 this->skip_call_tls_get_addr_ = true;
2361}
2362
56622147
ILT
2363// Do a relocation in which we convert a TLS Initial-Exec to a
2364// Local-Exec.
2365
2366inline void
2367Target_i386::Relocate::tls_ie_to_le(const Relocate_info<32, false>* relinfo,
2368 size_t relnum,
2369 Output_segment* tls_segment,
2370 const elfcpp::Rel<32, false>& rel,
2371 unsigned int r_type,
2372 elfcpp::Elf_types<32>::Elf_Addr value,
2373 unsigned char* view,
fe8718a4 2374 section_size_type view_size)
56622147
ILT
2375{
2376 // We have to actually change the instructions, which means that we
2377 // need to examine the opcodes to figure out which instruction we
2378 // are looking at.
2379 if (r_type == elfcpp::R_386_TLS_IE)
2380 {
2381 // movl %gs:XX,%eax ==> movl $YY,%eax
2382 // movl %gs:XX,%reg ==> movl $YY,%reg
2383 // addl %gs:XX,%reg ==> addl $YY,%reg
2384 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -1);
2385 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2386
2387 unsigned char op1 = view[-1];
2388 if (op1 == 0xa1)
2389 {
2390 // movl XX,%eax ==> movl $YY,%eax
2391 view[-1] = 0xb8;
2392 }
2393 else
2394 {
2395 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2396
2397 unsigned char op2 = view[-2];
2398 if (op2 == 0x8b)
2399 {
2400 // movl XX,%reg ==> movl $YY,%reg
2401 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2402 (op1 & 0xc7) == 0x05);
2403 view[-2] = 0xc7;
2404 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2405 }
2406 else if (op2 == 0x03)
2407 {
2408 // addl XX,%reg ==> addl $YY,%reg
2409 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2410 (op1 & 0xc7) == 0x05);
2411 view[-2] = 0x81;
2412 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2413 }
2414 else
2415 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2416 }
2417 }
2418 else
2419 {
2420 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2421 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2422 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2423 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, -2);
2424 tls::check_range(relinfo, relnum, rel.get_r_offset(), view_size, 4);
2425
2426 unsigned char op1 = view[-1];
2427 unsigned char op2 = view[-2];
2428 tls::check_tls(relinfo, relnum, rel.get_r_offset(),
2429 (op1 & 0xc0) == 0x80 && (op1 & 7) != 4);
2430 if (op2 == 0x8b)
2431 {
2432 // movl %gs:XX(%reg1),%reg2 ==> movl $YY,%reg2
2433 view[-2] = 0xc7;
2434 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2435 }
2436 else if (op2 == 0x2b)
2437 {
2438 // subl %gs:XX(%reg1),%reg2 ==> subl $YY,%reg2
2439 view[-2] = 0x81;
2440 view[-1] = 0xe8 | ((op1 >> 3) & 7);
2441 }
2442 else if (op2 == 0x03)
2443 {
2444 // addl %gs:XX(%reg1),%reg2 ==> addl $YY,$reg2
2445 view[-2] = 0x81;
2446 view[-1] = 0xc0 | ((op1 >> 3) & 7);
2447 }
2448 else
2449 tls::check_tls(relinfo, relnum, rel.get_r_offset(), 0);
2450 }
2451
7bf1f802 2452 value = tls_segment->memsz() - value;
56622147
ILT
2453 if (r_type == elfcpp::R_386_TLS_IE || r_type == elfcpp::R_386_TLS_GOTIE)
2454 value = - value;
2455
2456 Relocate_functions<32, false>::rel32(view, value);
2457}
2458
61ba1cf9
ILT
2459// Relocate section data.
2460
2461void
92e059d8 2462Target_i386::relocate_section(const Relocate_info<32, false>* relinfo,
61ba1cf9
ILT
2463 unsigned int sh_type,
2464 const unsigned char* prelocs,
2465 size_t reloc_count,
730cdc88
ILT
2466 Output_section* output_section,
2467 bool needs_special_offset_handling,
61ba1cf9
ILT
2468 unsigned char* view,
2469 elfcpp::Elf_types<32>::Elf_Addr address,
364c7fa5
ILT
2470 section_size_type view_size,
2471 const Reloc_symbol_changes* reloc_symbol_changes)
61ba1cf9 2472{
a3ad94ed 2473 gold_assert(sh_type == elfcpp::SHT_REL);
61ba1cf9 2474
ead1e424
ILT
2475 gold::relocate_section<32, false, Target_i386, elfcpp::SHT_REL,
2476 Target_i386::Relocate>(
92e059d8 2477 relinfo,
ead1e424 2478 this,
61ba1cf9
ILT
2479 prelocs,
2480 reloc_count,
730cdc88
ILT
2481 output_section,
2482 needs_special_offset_handling,
61ba1cf9
ILT
2483 view,
2484 address,
364c7fa5
ILT
2485 view_size,
2486 reloc_symbol_changes);
61ba1cf9
ILT
2487}
2488
6a74a719
ILT
2489// Return the size of a relocation while scanning during a relocatable
2490// link.
2491
2492unsigned int
2493Target_i386::Relocatable_size_for_reloc::get_size_for_reloc(
2494 unsigned int r_type,
2495 Relobj* object)
2496{
2497 switch (r_type)
2498 {
2499 case elfcpp::R_386_NONE:
2500 case elfcpp::R_386_GNU_VTINHERIT:
2501 case elfcpp::R_386_GNU_VTENTRY:
2502 case elfcpp::R_386_TLS_GD: // Global-dynamic
2503 case elfcpp::R_386_TLS_GOTDESC: // Global-dynamic (from ~oliva url)
2504 case elfcpp::R_386_TLS_DESC_CALL:
2505 case elfcpp::R_386_TLS_LDM: // Local-dynamic
2506 case elfcpp::R_386_TLS_LDO_32: // Alternate local-dynamic
2507 case elfcpp::R_386_TLS_IE: // Initial-exec
2508 case elfcpp::R_386_TLS_IE_32:
2509 case elfcpp::R_386_TLS_GOTIE:
2510 case elfcpp::R_386_TLS_LE: // Local-exec
2511 case elfcpp::R_386_TLS_LE_32:
2512 return 0;
2513
2514 case elfcpp::R_386_32:
2515 case elfcpp::R_386_PC32:
2516 case elfcpp::R_386_GOT32:
2517 case elfcpp::R_386_PLT32:
2518 case elfcpp::R_386_GOTOFF:
2519 case elfcpp::R_386_GOTPC:
2520 return 4;
2521
2522 case elfcpp::R_386_16:
2523 case elfcpp::R_386_PC16:
2524 return 2;
2525
2526 case elfcpp::R_386_8:
2527 case elfcpp::R_386_PC8:
2528 return 1;
2529
2530 // These are relocations which should only be seen by the
2531 // dynamic linker, and should never be seen here.
2532 case elfcpp::R_386_COPY:
2533 case elfcpp::R_386_GLOB_DAT:
2534 case elfcpp::R_386_JUMP_SLOT:
2535 case elfcpp::R_386_RELATIVE:
2536 case elfcpp::R_386_TLS_TPOFF:
2537 case elfcpp::R_386_TLS_DTPMOD32:
2538 case elfcpp::R_386_TLS_DTPOFF32:
2539 case elfcpp::R_386_TLS_TPOFF32:
2540 case elfcpp::R_386_TLS_DESC:
2541 object->error(_("unexpected reloc %u in object file"), r_type);
2542 return 0;
2543
2544 case elfcpp::R_386_32PLT:
2545 case elfcpp::R_386_TLS_GD_32:
2546 case elfcpp::R_386_TLS_GD_PUSH:
2547 case elfcpp::R_386_TLS_GD_CALL:
2548 case elfcpp::R_386_TLS_GD_POP:
2549 case elfcpp::R_386_TLS_LDM_32:
2550 case elfcpp::R_386_TLS_LDM_PUSH:
2551 case elfcpp::R_386_TLS_LDM_CALL:
2552 case elfcpp::R_386_TLS_LDM_POP:
2553 case elfcpp::R_386_USED_BY_INTEL_200:
2554 default:
2555 object->error(_("unsupported reloc %u in object file"), r_type);
2556 return 0;
2557 }
2558}
2559
2560// Scan the relocs during a relocatable link.
2561
2562void
ad0f2072 2563Target_i386::scan_relocatable_relocs(Symbol_table* symtab,
6a74a719
ILT
2564 Layout* layout,
2565 Sized_relobj<32, false>* object,
2566 unsigned int data_shndx,
2567 unsigned int sh_type,
2568 const unsigned char* prelocs,
2569 size_t reloc_count,
2570 Output_section* output_section,
2571 bool needs_special_offset_handling,
2572 size_t local_symbol_count,
2573 const unsigned char* plocal_symbols,
2574 Relocatable_relocs* rr)
2575{
2576 gold_assert(sh_type == elfcpp::SHT_REL);
2577
2578 typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2579 Relocatable_size_for_reloc> Scan_relocatable_relocs;
2580
7019cd25 2581 gold::scan_relocatable_relocs<32, false, elfcpp::SHT_REL,
6a74a719 2582 Scan_relocatable_relocs>(
6a74a719
ILT
2583 symtab,
2584 layout,
2585 object,
2586 data_shndx,
2587 prelocs,
2588 reloc_count,
2589 output_section,
2590 needs_special_offset_handling,
2591 local_symbol_count,
2592 plocal_symbols,
2593 rr);
2594}
2595
2596// Relocate a section during a relocatable link.
2597
2598void
2599Target_i386::relocate_for_relocatable(
2600 const Relocate_info<32, false>* relinfo,
2601 unsigned int sh_type,
2602 const unsigned char* prelocs,
2603 size_t reloc_count,
2604 Output_section* output_section,
2605 off_t offset_in_output_section,
2606 const Relocatable_relocs* rr,
2607 unsigned char* view,
2608 elfcpp::Elf_types<32>::Elf_Addr view_address,
2609 section_size_type view_size,
2610 unsigned char* reloc_view,
2611 section_size_type reloc_view_size)
2612{
2613 gold_assert(sh_type == elfcpp::SHT_REL);
2614
7019cd25 2615 gold::relocate_for_relocatable<32, false, elfcpp::SHT_REL>(
6a74a719
ILT
2616 relinfo,
2617 prelocs,
2618 reloc_count,
2619 output_section,
2620 offset_in_output_section,
2621 rr,
2622 view,
2623 view_address,
2624 view_size,
2625 reloc_view,
2626 reloc_view_size);
2627}
2628
ab5c9e90
ILT
2629// Return the value to use for a dynamic which requires special
2630// treatment. This is how we support equality comparisons of function
2631// pointers across shared library boundaries, as described in the
2632// processor specific ABI supplement.
2633
2634uint64_t
2635Target_i386::do_dynsym_value(const Symbol* gsym) const
2636{
2637 gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2638 return this->plt_section()->address() + gsym->plt_offset();
2639}
2640
c51e6221
ILT
2641// Return a string used to fill a code section with nops to take up
2642// the specified length.
2643
2644std::string
8851ecca 2645Target_i386::do_code_fill(section_size_type length) const
c51e6221
ILT
2646{
2647 if (length >= 16)
2648 {
2649 // Build a jmp instruction to skip over the bytes.
2650 unsigned char jmp[5];
2651 jmp[0] = 0xe9;
2652 elfcpp::Swap_unaligned<32, false>::writeval(jmp + 1, length - 5);
2653 return (std::string(reinterpret_cast<char*>(&jmp[0]), 5)
2654 + std::string(length - 5, '\0'));
2655 }
2656
2657 // Nop sequences of various lengths.
2658 const char nop1[1] = { 0x90 }; // nop
2659 const char nop2[2] = { 0x66, 0x90 }; // xchg %ax %ax
2660 const char nop3[3] = { 0x8d, 0x76, 0x00 }; // leal 0(%esi),%esi
2661 const char nop4[4] = { 0x8d, 0x74, 0x26, 0x00}; // leal 0(%esi,1),%esi
2662 const char nop5[5] = { 0x90, 0x8d, 0x74, 0x26, // nop
2663 0x00 }; // leal 0(%esi,1),%esi
2664 const char nop6[6] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2665 0x00, 0x00 };
2666 const char nop7[7] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2667 0x00, 0x00, 0x00 };
2668 const char nop8[8] = { 0x90, 0x8d, 0xb4, 0x26, // nop
2669 0x00, 0x00, 0x00, 0x00 }; // leal 0L(%esi,1),%esi
2670 const char nop9[9] = { 0x89, 0xf6, 0x8d, 0xbc, // movl %esi,%esi
2671 0x27, 0x00, 0x00, 0x00, // leal 0L(%edi,1),%edi
2672 0x00 };
2673 const char nop10[10] = { 0x8d, 0x76, 0x00, 0x8d, // leal 0(%esi),%esi
2674 0xbc, 0x27, 0x00, 0x00, // leal 0L(%edi,1),%edi
2675 0x00, 0x00 };
2676 const char nop11[11] = { 0x8d, 0x74, 0x26, 0x00, // leal 0(%esi,1),%esi
2677 0x8d, 0xbc, 0x27, 0x00, // leal 0L(%edi,1),%edi
2678 0x00, 0x00, 0x00 };
2679 const char nop12[12] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2680 0x00, 0x00, 0x8d, 0xbf, // leal 0L(%edi),%edi
2681 0x00, 0x00, 0x00, 0x00 };
2682 const char nop13[13] = { 0x8d, 0xb6, 0x00, 0x00, // leal 0L(%esi),%esi
2683 0x00, 0x00, 0x8d, 0xbc, // leal 0L(%edi,1),%edi
2684 0x27, 0x00, 0x00, 0x00,
2685 0x00 };
2686 const char nop14[14] = { 0x8d, 0xb4, 0x26, 0x00, // leal 0L(%esi,1),%esi
2687 0x00, 0x00, 0x00, 0x8d, // leal 0L(%edi,1),%edi
2688 0xbc, 0x27, 0x00, 0x00,
2689 0x00, 0x00 };
2690 const char nop15[15] = { 0xeb, 0x0d, 0x90, 0x90, // jmp .+15
2691 0x90, 0x90, 0x90, 0x90, // nop,nop,nop,...
2692 0x90, 0x90, 0x90, 0x90,
2693 0x90, 0x90, 0x90 };
2694
2695 const char* nops[16] = {
2696 NULL,
2697 nop1, nop2, nop3, nop4, nop5, nop6, nop7,
2698 nop8, nop9, nop10, nop11, nop12, nop13, nop14, nop15
2699 };
2700
2701 return std::string(nops[length], length);
2702}
2703
364c7fa5
ILT
2704// FNOFFSET in section SHNDX in OBJECT is the start of a function
2705// compiled with -fstack-split. The function calls non-stack-split
2706// code. We have to change the function so that it always ensures
2707// that it has enough stack space to run some random function.
2708
2709void
2710Target_i386::do_calls_non_split(Relobj* object, unsigned int shndx,
2711 section_offset_type fnoffset,
2712 section_size_type fnsize,
2713 unsigned char* view,
2714 section_size_type view_size,
2715 std::string* from,
2716 std::string* to) const
2717{
2718 // The function starts with a comparison of the stack pointer and a
2719 // field in the TCB. This is followed by a jump.
2720
2721 // cmp %gs:NN,%esp
2722 if (this->match_view(view, view_size, fnoffset, "\x65\x3b\x25", 3)
2723 && fnsize > 7)
2724 {
2725 // We will call __morestack if the carry flag is set after this
2726 // comparison. We turn the comparison into an stc instruction
2727 // and some nops.
2728 view[fnoffset] = '\xf9';
2729 this->set_view_to_nop(view, view_size, fnoffset + 1, 6);
2730 }
2731 // lea NN(%esp),%ecx
1782c879
ILT
2732 // lea NN(%esp),%edx
2733 else if ((this->match_view(view, view_size, fnoffset, "\x8d\x8c\x24", 3)
2734 || this->match_view(view, view_size, fnoffset, "\x8d\x94\x24", 3))
364c7fa5
ILT
2735 && fnsize > 7)
2736 {
2737 // This is loading an offset from the stack pointer for a
2738 // comparison. The offset is negative, so we decrease the
2739 // offset by the amount of space we need for the stack. This
2740 // means we will avoid calling __morestack if there happens to
2741 // be plenty of space on the stack already.
2742 unsigned char* pval = view + fnoffset + 3;
2743 uint32_t val = elfcpp::Swap_unaligned<32, false>::readval(pval);
2744 val -= parameters->options().split_stack_adjust_size();
2745 elfcpp::Swap_unaligned<32, false>::writeval(pval, val);
2746 }
2747 else
2748 {
2749 if (!object->has_no_split_stack())
2750 object->error(_("failed to match split-stack sequence at "
2751 "section %u offset %0zx"),
ac33a407 2752 shndx, static_cast<size_t>(fnoffset));
364c7fa5
ILT
2753 return;
2754 }
2755
2756 // We have to change the function so that it calls
2757 // __morestack_non_split instead of __morestack. The former will
2758 // allocate additional stack space.
2759 *from = "__morestack";
2760 *to = "__morestack_non_split";
2761}
2762
14bfc3f5
ILT
2763// The selector for i386 object files.
2764
36959681 2765class Target_selector_i386 : public Target_selector_freebsd
14bfc3f5
ILT
2766{
2767public:
2768 Target_selector_i386()
36959681
ILT
2769 : Target_selector_freebsd(elfcpp::EM_386, 32, false,
2770 "elf32-i386", "elf32-i386-freebsd")
14bfc3f5
ILT
2771 { }
2772
2773 Target*
e96caa79
ILT
2774 do_instantiate_target()
2775 { return new Target_i386(); }
14bfc3f5
ILT
2776};
2777
14bfc3f5
ILT
2778Target_selector_i386 target_selector_i386;
2779
2780} // End anonymous namespace.
This page took 0.305496 seconds and 4 git commands to generate.