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

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [binutils-2.20.1/] [gold/] [i386.cc] - Blame information for rev 294

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

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

powered by: WebSVN 2.1.0

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