OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [gold/] [i386.cc] - Blame information for rev 104

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.