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