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