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