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