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