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