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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [binutils-2.20.1/] [gold/] [arm.cc] - Blame information for rev 853

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

Line No. Rev Author Line
1 205 julius
// arm.cc -- arm target support for gold.
2
 
3
// Copyright 2009 Free Software Foundation, Inc.
4
// Written by Doug Kwan <dougkwan@google.com> based on the i386 code
5
// by Ian Lance Taylor <iant@google.com>.
6
 
7
// This file is part of gold.
8
 
9
// This program is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU General Public License as published by
11
// the Free Software Foundation; either version 3 of the License, or
12
// (at your option) any later version.
13
 
14
// This program is distributed in the hope that it will be useful,
15
// but WITHOUT ANY WARRANTY; without even the implied warranty of
16
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
// GNU General Public License for more details.
18
 
19
// You should have received a copy of the GNU General Public License
20
// along with this program; if not, write to the Free Software
21
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22
// MA 02110-1301, USA.
23
 
24
#include "gold.h"
25
 
26
#include <cstring>
27
#include <limits>
28
#include <cstdio>
29
#include <string>
30
 
31
#include "elfcpp.h"
32
#include "parameters.h"
33
#include "reloc.h"
34
#include "arm.h"
35
#include "object.h"
36
#include "symtab.h"
37
#include "layout.h"
38
#include "output.h"
39
#include "copy-relocs.h"
40
#include "target.h"
41
#include "target-reloc.h"
42
#include "target-select.h"
43
#include "tls.h"
44
#include "defstd.h"
45
#include "gc.h"
46
 
47
namespace
48
{
49
 
50
using namespace gold;
51
 
52
template<bool big_endian>
53
class Output_data_plt_arm;
54
 
55
// The arm target class.
56
//
57
// This is a very simple port of gold for ARM-EABI.  It is intended for
58
// supporting Android only for the time being.  Only these relocation types
59
// are supported.
60
//
61
// R_ARM_NONE
62
// R_ARM_ABS32
63
// R_ARM_ABS32_NOI
64
// R_ARM_ABS16
65
// R_ARM_ABS12
66
// R_ARM_ABS8
67
// R_ARM_THM_ABS5
68
// R_ARM_BASE_ABS
69
// R_ARM_REL32
70
// R_ARM_THM_CALL
71
// R_ARM_COPY
72
// R_ARM_GLOB_DAT
73
// R_ARM_BASE_PREL
74
// R_ARM_JUMP_SLOT
75
// R_ARM_RELATIVE
76
// R_ARM_GOTOFF32
77
// R_ARM_GOT_BREL
78
// R_ARM_GOT_PREL
79
// R_ARM_PLT32
80
// R_ARM_CALL
81
// R_ARM_JUMP24
82
// R_ARM_TARGET1
83
// R_ARM_PREL31
84
// R_ARM_ABS8
85
// R_ARM_MOVW_ABS_NC
86
// R_ARM_MOVT_ABS
87
// R_ARM_THM_MOVW_ABS_NC
88
// R_ARM_THM_MOVT_ABS
89
// R_ARM_MOVW_PREL_NC
90
// R_ARM_MOVT_PREL
91
// R_ARM_THM_MOVW_PREL_NC
92
// R_ARM_THM_MOVT_PREL
93
// 
94
// TODOs:
95
// - Generate various branch stubs.
96
// - Support interworking.
97
// - Define section symbols __exidx_start and __exidx_stop.
98
// - Support more relocation types as needed. 
99
// - Make PLTs more flexible for different architecture features like
100
//   Thumb-2 and BE8.
101
// There are probably a lot more.
102
 
103
// Utilities for manipulating integers of up to 32-bits
104
 
105
namespace utils
106
{
107
  // Sign extend an n-bit unsigned integer stored in an uint32_t into
108
  // an int32_t.  NO_BITS must be between 1 to 32.
109
  template<int no_bits>
110
  static inline int32_t
111
  sign_extend(uint32_t bits)
112
  {
113
    gold_assert(no_bits >= 0 && no_bits <= 32);
114
    if (no_bits == 32)
115
      return static_cast<int32_t>(bits);
116
    uint32_t mask = (~((uint32_t) 0)) >> (32 - no_bits);
117
    bits &= mask;
118
    uint32_t top_bit = 1U << (no_bits - 1);
119
    int32_t as_signed = static_cast<int32_t>(bits);
120
    return (bits & top_bit) ? as_signed + (-top_bit * 2) : as_signed;
121
  }
122
 
123
  // Detects overflow of an NO_BITS integer stored in a uint32_t.
124
  template<int no_bits>
125
  static inline bool
126
  has_overflow(uint32_t bits)
127
  {
128
    gold_assert(no_bits >= 0 && no_bits <= 32);
129
    if (no_bits == 32)
130
      return false;
131
    int32_t max = (1 << (no_bits - 1)) - 1;
132
    int32_t min = -(1 << (no_bits - 1));
133
    int32_t as_signed = static_cast<int32_t>(bits);
134
    return as_signed > max || as_signed < min;
135
  }
136
 
137
  // Detects overflow of an NO_BITS integer stored in a uint32_t when it
138
  // fits in the given number of bits as either a signed or unsigned value.
139
  // For example, has_signed_unsigned_overflow<8> would check
140
  // -128 <= bits <= 255
141
  template<int no_bits>
142
  static inline bool
143
  has_signed_unsigned_overflow(uint32_t bits)
144
  {
145
    gold_assert(no_bits >= 2 && no_bits <= 32);
146
    if (no_bits == 32)
147
      return false;
148
    int32_t max = static_cast<int32_t>((1U << no_bits) - 1);
149
    int32_t min = -(1 << (no_bits - 1));
150
    int32_t as_signed = static_cast<int32_t>(bits);
151
    return as_signed > max || as_signed < min;
152
  }
153
 
154
  // Select bits from A and B using bits in MASK.  For each n in [0..31],
155
  // the n-th bit in the result is chosen from the n-th bits of A and B.
156
  // A zero selects A and a one selects B.
157
  static inline uint32_t
158
  bit_select(uint32_t a, uint32_t b, uint32_t mask)
159
  { return (a & ~mask) | (b & mask); }
160
};
161
 
162
template<bool big_endian>
163
class Target_arm : public Sized_target<32, big_endian>
164
{
165
 public:
166
  typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
167
    Reloc_section;
168
 
169
  Target_arm()
170
    : Sized_target<32, big_endian>(&arm_info),
171
      got_(NULL), plt_(NULL), got_plt_(NULL), rel_dyn_(NULL),
172
      copy_relocs_(elfcpp::R_ARM_COPY), dynbss_(NULL)
173
  { }
174
 
175
  // Process the relocations to determine unreferenced sections for 
176
  // garbage collection.
177
  void
178
  gc_process_relocs(const General_options& options,
179
                    Symbol_table* symtab,
180
                    Layout* layout,
181
                    Sized_relobj<32, big_endian>* object,
182
                    unsigned int data_shndx,
183
                    unsigned int sh_type,
184
                    const unsigned char* prelocs,
185
                    size_t reloc_count,
186
                    Output_section* output_section,
187
                    bool needs_special_offset_handling,
188
                    size_t local_symbol_count,
189
                    const unsigned char* plocal_symbols);
190
 
191
  // Scan the relocations to look for symbol adjustments.
192
  void
193
  scan_relocs(const General_options& options,
194
              Symbol_table* symtab,
195
              Layout* layout,
196
              Sized_relobj<32, big_endian>* object,
197
              unsigned int data_shndx,
198
              unsigned int sh_type,
199
              const unsigned char* prelocs,
200
              size_t reloc_count,
201
              Output_section* output_section,
202
              bool needs_special_offset_handling,
203
              size_t local_symbol_count,
204
              const unsigned char* plocal_symbols);
205
 
206
  // Finalize the sections.
207
  void
208
  do_finalize_sections(Layout*);
209
 
210
  // Return the value to use for a dynamic symbol which requires special
211
  // treatment.
212
  uint64_t
213
  do_dynsym_value(const Symbol*) const;
214
 
215
  // Relocate a section.
216
  void
217
  relocate_section(const Relocate_info<32, big_endian>*,
218
                   unsigned int sh_type,
219
                   const unsigned char* prelocs,
220
                   size_t reloc_count,
221
                   Output_section* output_section,
222
                   bool needs_special_offset_handling,
223
                   unsigned char* view,
224
                   elfcpp::Elf_types<32>::Elf_Addr view_address,
225
                   section_size_type view_size,
226
                   const Reloc_symbol_changes*);
227
 
228
  // Scan the relocs during a relocatable link.
229
  void
230
  scan_relocatable_relocs(const General_options& options,
231
                          Symbol_table* symtab,
232
                          Layout* layout,
233
                          Sized_relobj<32, big_endian>* object,
234
                          unsigned int data_shndx,
235
                          unsigned int sh_type,
236
                          const unsigned char* prelocs,
237
                          size_t reloc_count,
238
                          Output_section* output_section,
239
                          bool needs_special_offset_handling,
240
                          size_t local_symbol_count,
241
                          const unsigned char* plocal_symbols,
242
                          Relocatable_relocs*);
243
 
244
  // Relocate a section during a relocatable link.
245
  void
246
  relocate_for_relocatable(const Relocate_info<32, big_endian>*,
247
                           unsigned int sh_type,
248
                           const unsigned char* prelocs,
249
                           size_t reloc_count,
250
                           Output_section* output_section,
251
                           off_t offset_in_output_section,
252
                           const Relocatable_relocs*,
253
                           unsigned char* view,
254
                           elfcpp::Elf_types<32>::Elf_Addr view_address,
255
                           section_size_type view_size,
256
                           unsigned char* reloc_view,
257
                           section_size_type reloc_view_size);
258
 
259
  // Return whether SYM is defined by the ABI.
260
  bool
261
  do_is_defined_by_abi(Symbol* sym) const
262
  { return strcmp(sym->name(), "__tls_get_addr") == 0; }
263
 
264
  // Return the size of the GOT section.
265
  section_size_type
266
  got_size()
267
  {
268
    gold_assert(this->got_ != NULL);
269
    return this->got_->data_size();
270
  }
271
 
272
  // Map platform-specific reloc types
273
  static unsigned int
274
  get_real_reloc_type (unsigned int r_type);
275
 
276
 private:
277
  // The class which scans relocations.
278
  class Scan
279
  {
280
   public:
281
    Scan()
282
      : issued_non_pic_error_(false)
283
    { }
284
 
285
    inline void
286
    local(const General_options& options, Symbol_table* symtab,
287
          Layout* layout, Target_arm* target,
288
          Sized_relobj<32, big_endian>* object,
289
          unsigned int data_shndx,
290
          Output_section* output_section,
291
          const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
292
          const elfcpp::Sym<32, big_endian>& lsym);
293
 
294
    inline void
295
    global(const General_options& options, Symbol_table* symtab,
296
           Layout* layout, Target_arm* target,
297
           Sized_relobj<32, big_endian>* object,
298
           unsigned int data_shndx,
299
           Output_section* output_section,
300
           const elfcpp::Rel<32, big_endian>& reloc, unsigned int r_type,
301
           Symbol* gsym);
302
 
303
   private:
304
    static void
305
    unsupported_reloc_local(Sized_relobj<32, big_endian>*,
306
                            unsigned int r_type);
307
 
308
    static void
309
    unsupported_reloc_global(Sized_relobj<32, big_endian>*,
310
                             unsigned int r_type, Symbol*);
311
 
312
    void
313
    check_non_pic(Relobj*, unsigned int r_type);
314
 
315
    // Almost identical to Symbol::needs_plt_entry except that it also
316
    // handles STT_ARM_TFUNC.
317
    static bool
318
    symbol_needs_plt_entry(const Symbol* sym)
319
    {
320
      // An undefined symbol from an executable does not need a PLT entry.
321
      if (sym->is_undefined() && !parameters->options().shared())
322
        return false;
323
 
324
      return (!parameters->doing_static_link()
325
              && (sym->type() == elfcpp::STT_FUNC
326
                  || sym->type() == elfcpp::STT_ARM_TFUNC)
327
              && (sym->is_from_dynobj()
328
                  || sym->is_undefined()
329
                  || sym->is_preemptible()));
330
    }
331
 
332
    // Whether we have issued an error about a non-PIC compilation.
333
    bool issued_non_pic_error_;
334
  };
335
 
336
  // The class which implements relocation.
337
  class Relocate
338
  {
339
   public:
340
    Relocate()
341
    { }
342
 
343
    ~Relocate()
344
    { }
345
 
346
    // Return whether the static relocation needs to be applied.
347
    inline bool
348
    should_apply_static_reloc(const Sized_symbol<32>* gsym,
349
                              int ref_flags,
350
                              bool is_32bit,
351
                              Output_section* output_section);
352
 
353
    // Do a relocation.  Return false if the caller should not issue
354
    // any warnings about this relocation.
355
    inline bool
356
    relocate(const Relocate_info<32, big_endian>*, Target_arm*,
357
             Output_section*,  size_t relnum,
358
             const elfcpp::Rel<32, big_endian>&,
359
             unsigned int r_type, const Sized_symbol<32>*,
360
             const Symbol_value<32>*,
361
             unsigned char*, elfcpp::Elf_types<32>::Elf_Addr,
362
             section_size_type);
363
 
364
    // Return whether we want to pass flag NON_PIC_REF for this
365
    // reloc.
366
    static inline bool
367
    reloc_is_non_pic (unsigned int r_type)
368
    {
369
      switch (r_type)
370
        {
371
        case elfcpp::R_ARM_REL32:
372
        case elfcpp::R_ARM_THM_CALL:
373
        case elfcpp::R_ARM_CALL:
374
        case elfcpp::R_ARM_JUMP24:
375
        case elfcpp::R_ARM_PREL31:
376
        case elfcpp::R_ARM_THM_ABS5:
377
        case elfcpp::R_ARM_ABS8:
378
        case elfcpp::R_ARM_ABS12:
379
        case elfcpp::R_ARM_ABS16:
380
        case elfcpp::R_ARM_BASE_ABS:
381
          return true;
382
        default:
383
          return false;
384
        }
385
    }
386
  };
387
 
388
  // A class which returns the size required for a relocation type,
389
  // used while scanning relocs during a relocatable link.
390
  class Relocatable_size_for_reloc
391
  {
392
   public:
393
    unsigned int
394
    get_size_for_reloc(unsigned int, Relobj*);
395
  };
396
 
397
  // Get the GOT section, creating it if necessary.
398
  Output_data_got<32, big_endian>*
399
  got_section(Symbol_table*, Layout*);
400
 
401
  // Get the GOT PLT section.
402
  Output_data_space*
403
  got_plt_section() const
404
  {
405
    gold_assert(this->got_plt_ != NULL);
406
    return this->got_plt_;
407
  }
408
 
409
  // Create a PLT entry for a global symbol.
410
  void
411
  make_plt_entry(Symbol_table*, Layout*, Symbol*);
412
 
413
  // Get the PLT section.
414
  const Output_data_plt_arm<big_endian>*
415
  plt_section() const
416
  {
417
    gold_assert(this->plt_ != NULL);
418
    return this->plt_;
419
  }
420
 
421
  // Get the dynamic reloc section, creating it if necessary.
422
  Reloc_section*
423
  rel_dyn_section(Layout*);
424
 
425
  // Return true if the symbol may need a COPY relocation.
426
  // References from an executable object to non-function symbols
427
  // defined in a dynamic object may need a COPY relocation.
428
  bool
429
  may_need_copy_reloc(Symbol* gsym)
430
  {
431
    return (gsym->type() != elfcpp::STT_ARM_TFUNC
432
            && gsym->may_need_copy_reloc());
433
  }
434
 
435
  // Add a potential copy relocation.
436
  void
437
  copy_reloc(Symbol_table* symtab, Layout* layout,
438
             Sized_relobj<32, big_endian>* object,
439
             unsigned int shndx, Output_section* output_section,
440
             Symbol* sym, const elfcpp::Rel<32, big_endian>& reloc)
441
  {
442
    this->copy_relocs_.copy_reloc(symtab, layout,
443
                                  symtab->get_sized_symbol<32>(sym),
444
                                  object, shndx, output_section, reloc,
445
                                  this->rel_dyn_section(layout));
446
  }
447
 
448
  // Information about this specific target which we pass to the
449
  // general Target structure.
450
  static const Target::Target_info arm_info;
451
 
452
  // The types of GOT entries needed for this platform.
453
  enum Got_type
454
  {
455
    GOT_TYPE_STANDARD = 0        // GOT entry for a regular symbol
456
  };
457
 
458
  // The GOT section.
459
  Output_data_got<32, big_endian>* got_;
460
  // The PLT section.
461
  Output_data_plt_arm<big_endian>* plt_;
462
  // The GOT PLT section.
463
  Output_data_space* got_plt_;
464
  // The dynamic reloc section.
465
  Reloc_section* rel_dyn_;
466
  // Relocs saved to avoid a COPY reloc.
467
  Copy_relocs<elfcpp::SHT_REL, 32, big_endian> copy_relocs_;
468
  // Space for variables copied with a COPY reloc.
469
  Output_data_space* dynbss_;
470
};
471
 
472
template<bool big_endian>
473
const Target::Target_info Target_arm<big_endian>::arm_info =
474
{
475
  32,                   // size
476
  big_endian,           // is_big_endian
477
  elfcpp::EM_ARM,       // machine_code
478
  false,                // has_make_symbol
479
  false,                // has_resolve
480
  false,                // has_code_fill
481
  true,                 // is_default_stack_executable
482
  '\0',                 // wrap_char
483
  "/usr/lib/libc.so.1", // dynamic_linker
484
  0x8000,               // default_text_segment_address
485
  0x1000,               // abi_pagesize (overridable by -z max-page-size)
486
  0x1000,               // common_pagesize (overridable by -z common-page-size)
487
  elfcpp::SHN_UNDEF,    // small_common_shndx
488
  elfcpp::SHN_UNDEF,    // large_common_shndx
489
  0,                     // small_common_section_flags
490
 
491
};
492
 
493
// Arm relocate functions class
494
//
495
 
496
template<bool big_endian>
497
class Arm_relocate_functions : public Relocate_functions<32, big_endian>
498
{
499
 public:
500
  typedef enum
501
  {
502
    STATUS_OKAY,        // No error during relocation.
503
    STATUS_OVERFLOW,    // Relocation oveflow.
504
    STATUS_BAD_RELOC    // Relocation cannot be applied.
505
  } Status;
506
 
507
 private:
508
  typedef Relocate_functions<32, big_endian> Base;
509
  typedef Arm_relocate_functions<big_endian> This;
510
 
511
  // Get an symbol value of *PSYMVAL with an ADDEND.  This is a wrapper
512
  // to Symbol_value::value().  If HAS_THUMB_BIT is true, that LSB is used
513
  // to distinguish ARM and THUMB functions and it is treated specially.
514
  static inline Symbol_value<32>::Value
515
  arm_symbol_value (const Sized_relobj<32, big_endian> *object,
516
                    const Symbol_value<32>* psymval,
517
                    Symbol_value<32>::Value addend,
518
                    bool has_thumb_bit)
519
  {
520
    typedef Symbol_value<32>::Value Valtype;
521
 
522
    if (has_thumb_bit)
523
      {
524
        Valtype raw = psymval->value(object, 0);
525
        Valtype thumb_bit = raw & 1;
526
        return ((raw & ~((Valtype) 1)) + addend) | thumb_bit;
527
      }
528
    else
529
      return psymval->value(object, addend);
530
  }
531
 
532
  // Encoding of imm16 argument for movt and movw ARM instructions
533
  // from ARM ARM:
534
  //     
535
  //     imm16 := imm4 | imm12
536
  //
537
  //  f e d c b a 9 8 7 6 5 4 3 2 1 0 f e d c b a 9 8 7 6 5 4 3 2 1 0 
538
  // +-------+---------------+-------+-------+-----------------------+
539
  // |       |               |imm4   |       |imm12                  |
540
  // +-------+---------------+-------+-------+-----------------------+
541
 
542
  // Extract the relocation addend from VAL based on the ARM
543
  // instruction encoding described above.
544
  static inline typename elfcpp::Swap<32, big_endian>::Valtype
545
  extract_arm_movw_movt_addend(
546
      typename elfcpp::Swap<32, big_endian>::Valtype val)
547
  {
548
    // According to the Elf ABI for ARM Architecture the immediate
549
    // field is sign-extended to form the addend.
550
    return utils::sign_extend<16>(((val >> 4) & 0xf000) | (val & 0xfff));
551
  }
552
 
553
  // Insert X into VAL based on the ARM instruction encoding described
554
  // above.
555
  static inline typename elfcpp::Swap<32, big_endian>::Valtype
556
  insert_val_arm_movw_movt(
557
      typename elfcpp::Swap<32, big_endian>::Valtype val,
558
      typename elfcpp::Swap<32, big_endian>::Valtype x)
559
  {
560
    val &= 0xfff0f000;
561
    val |= x & 0x0fff;
562
    val |= (x & 0xf000) << 4;
563
    return val;
564
  }
565
 
566
  // Encoding of imm16 argument for movt and movw Thumb2 instructions
567
  // from ARM ARM:
568
  //     
569
  //     imm16 := imm4 | i | imm3 | imm8
570
  //
571
  //  f e d c b a 9 8 7 6 5 4 3 2 1 0  f e d c b a 9 8 7 6 5 4 3 2 1 0 
572
  // +---------+-+-----------+-------++-+-----+-------+---------------+
573
  // |         |i|           |imm4   || |imm3 |       |imm8           |
574
  // +---------+-+-----------+-------++-+-----+-------+---------------+
575
 
576
  // Extract the relocation addend from VAL based on the Thumb2
577
  // instruction encoding described above.
578
  static inline typename elfcpp::Swap<32, big_endian>::Valtype
579
  extract_thumb_movw_movt_addend(
580
      typename elfcpp::Swap<32, big_endian>::Valtype val)
581
  {
582
    // According to the Elf ABI for ARM Architecture the immediate
583
    // field is sign-extended to form the addend.
584
    return utils::sign_extend<16>(((val >> 4) & 0xf000)
585
                                  | ((val >> 15) & 0x0800)
586
                                  | ((val >> 4) & 0x0700)
587
                                  | (val & 0x00ff));
588
  }
589
 
590
  // Insert X into VAL based on the Thumb2 instruction encoding
591
  // described above.
592
  static inline typename elfcpp::Swap<32, big_endian>::Valtype
593
  insert_val_thumb_movw_movt(
594
      typename elfcpp::Swap<32, big_endian>::Valtype val,
595
      typename elfcpp::Swap<32, big_endian>::Valtype x)
596
  {
597
    val &= 0xfbf08f00;
598
    val |= (x & 0xf000) << 4;
599
    val |= (x & 0x0800) << 15;
600
    val |= (x & 0x0700) << 4;
601
    val |= (x & 0x00ff);
602
    return val;
603
  }
604
 
605
  // FIXME: This probably only works for Android on ARM v5te. We should
606
  // following GNU ld for the general case.
607
  template<unsigned r_type>
608
  static inline typename This::Status
609
  arm_branch_common(unsigned char *view,
610
                    const Sized_relobj<32, big_endian>* object,
611
                    const Symbol_value<32>* psymval,
612
                    elfcpp::Elf_types<32>::Elf_Addr address,
613
                    bool has_thumb_bit)
614
  {
615
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
616
    Valtype* wv = reinterpret_cast<Valtype*>(view);
617
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
618
 
619
    bool insn_is_b = (((val >> 28) & 0xf) <= 0xe)
620
                      && ((val & 0x0f000000UL) == 0x0a000000UL);
621
    bool insn_is_uncond_bl = (val & 0xff000000UL) == 0xeb000000UL;
622
    bool insn_is_cond_bl = (((val >> 28) & 0xf) < 0xe)
623
                            && ((val & 0x0f000000UL) == 0x0b000000UL);
624
    bool insn_is_blx = (val & 0xfe000000UL) == 0xfa000000UL;
625
    bool insn_is_any_branch = (val & 0x0e000000UL) == 0x0a000000UL;
626
 
627
    if (r_type == elfcpp::R_ARM_CALL)
628
      {
629
        if (!insn_is_uncond_bl && !insn_is_blx)
630
          return This::STATUS_BAD_RELOC;
631
      }
632
    else if (r_type == elfcpp::R_ARM_JUMP24)
633
      {
634
        if (!insn_is_b && !insn_is_cond_bl)
635
          return This::STATUS_BAD_RELOC;
636
      }
637
    else if (r_type == elfcpp::R_ARM_PLT32)
638
      {
639
        if (!insn_is_any_branch)
640
          return This::STATUS_BAD_RELOC;
641
      }
642
    else
643
      gold_unreachable();
644
 
645
    Valtype addend = utils::sign_extend<26>(val << 2);
646
    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
647
                 - address);
648
 
649
    // If target has thumb bit set, we need to either turn the BL
650
    // into a BLX (for ARMv5 or above) or generate a stub.
651
    if (x & 1)
652
      {
653
        // Turn BL to BLX.
654
        if (insn_is_uncond_bl)
655
          val = (val & 0xffffff) | 0xfa000000 | ((x & 2) << 23);
656
        else
657
          return This::STATUS_BAD_RELOC;
658
      }
659
    else
660
      gold_assert(!insn_is_blx);
661
 
662
    val = utils::bit_select(val, (x >> 2), 0xffffffUL);
663
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
664
    return (utils::has_overflow<26>(x)
665
            ? This::STATUS_OVERFLOW : This::STATUS_OKAY);
666
  }
667
 
668
 public:
669
 
670
  // R_ARM_ABS8: S + A
671
  static inline typename This::Status
672
  abs8(unsigned char *view,
673
       const Sized_relobj<32, big_endian>* object,
674
       const Symbol_value<32>* psymval)
675
  {
676
    typedef typename elfcpp::Swap<8, big_endian>::Valtype Valtype;
677
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
678
    Valtype* wv = reinterpret_cast<Valtype*>(view);
679
    Valtype val = elfcpp::Swap<8, big_endian>::readval(wv);
680
    Reltype addend = utils::sign_extend<8>(val);
681
    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
682
    val = utils::bit_select(val, x, 0xffU);
683
    elfcpp::Swap<8, big_endian>::writeval(wv, val);
684
    return (utils::has_signed_unsigned_overflow<8>(x)
685
            ? This::STATUS_OVERFLOW
686
            : This::STATUS_OKAY);
687
  }
688
 
689
  // R_ARM_THM_ABS5: S + A
690
  static inline typename This::Status
691
  thm_abs5(unsigned char *view,
692
       const Sized_relobj<32, big_endian>* object,
693
       const Symbol_value<32>* psymval)
694
  {
695
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
696
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
697
    Valtype* wv = reinterpret_cast<Valtype*>(view);
698
    Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
699
    Reltype addend = (val & 0x7e0U) >> 6;
700
    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
701
    val = utils::bit_select(val, x << 6, 0x7e0U);
702
    elfcpp::Swap<16, big_endian>::writeval(wv, val);
703
    return (utils::has_overflow<5>(x)
704
            ? This::STATUS_OVERFLOW
705
            : This::STATUS_OKAY);
706
  }
707
 
708
  // R_ARM_ABS12: S + A
709
  static inline typename This::Status
710
  abs12(unsigned char *view,
711
       const Sized_relobj<32, big_endian>* object,
712
       const Symbol_value<32>* psymval)
713
  {
714
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
715
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
716
    Valtype* wv = reinterpret_cast<Valtype*>(view);
717
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
718
    Reltype addend = val & 0x0fffU;
719
    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
720
    val = utils::bit_select(val, x, 0x0fffU);
721
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
722
    return (utils::has_overflow<12>(x)
723
            ? This::STATUS_OVERFLOW
724
            : This::STATUS_OKAY);
725
  }
726
 
727
  // R_ARM_ABS16: S + A
728
  static inline typename This::Status
729
  abs16(unsigned char *view,
730
       const Sized_relobj<32, big_endian>* object,
731
       const Symbol_value<32>* psymval)
732
  {
733
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
734
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
735
    Valtype* wv = reinterpret_cast<Valtype*>(view);
736
    Valtype val = elfcpp::Swap<16, big_endian>::readval(wv);
737
    Reltype addend = utils::sign_extend<16>(val);
738
    Reltype x = This::arm_symbol_value(object, psymval, addend, false);
739
    val = utils::bit_select(val, x, 0xffffU);
740
    elfcpp::Swap<16, big_endian>::writeval(wv, val);
741
    return (utils::has_signed_unsigned_overflow<16>(x)
742
            ? This::STATUS_OVERFLOW
743
            : This::STATUS_OKAY);
744
  }
745
 
746
  // R_ARM_ABS32: (S + A) | T
747
  static inline typename This::Status
748
  abs32(unsigned char *view,
749
        const Sized_relobj<32, big_endian>* object,
750
        const Symbol_value<32>* psymval,
751
        bool has_thumb_bit)
752
  {
753
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
754
    Valtype* wv = reinterpret_cast<Valtype*>(view);
755
    Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
756
    Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
757
    elfcpp::Swap<32, big_endian>::writeval(wv, x);
758
    return This::STATUS_OKAY;
759
  }
760
 
761
  // R_ARM_REL32: (S + A) | T - P
762
  static inline typename This::Status
763
  rel32(unsigned char *view,
764
        const Sized_relobj<32, big_endian>* object,
765
        const Symbol_value<32>* psymval,
766
        elfcpp::Elf_types<32>::Elf_Addr address,
767
        bool has_thumb_bit)
768
  {
769
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
770
    Valtype* wv = reinterpret_cast<Valtype*>(view);
771
    Valtype addend = elfcpp::Swap<32, big_endian>::readval(wv);
772
    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
773
                 - address);
774
    elfcpp::Swap<32, big_endian>::writeval(wv, x);
775
    return This::STATUS_OKAY;
776
  }
777
 
778
  // R_ARM_THM_CALL: (S + A) | T - P
779
  static inline typename This::Status
780
  thm_call(unsigned char *view,
781
           const Sized_relobj<32, big_endian>* object,
782
           const Symbol_value<32>* psymval,
783
           elfcpp::Elf_types<32>::Elf_Addr address,
784
           bool has_thumb_bit)
785
  {
786
    // A thumb call consists of two instructions.
787
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
788
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
789
    Valtype* wv = reinterpret_cast<Valtype*>(view);
790
    Valtype hi = elfcpp::Swap<16, big_endian>::readval(wv);
791
    Valtype lo = elfcpp::Swap<16, big_endian>::readval(wv + 1);
792
    // Must be a BL instruction. lo == 11111xxxxxxxxxxx.
793
    gold_assert((lo & 0xf800) == 0xf800);
794
    Reltype addend = utils::sign_extend<23>(((hi & 0x7ff) << 12)
795
                                           | ((lo & 0x7ff) << 1));
796
    Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
797
                 - address);
798
 
799
    // If target has no thumb bit set, we need to either turn the BL
800
    // into a BLX (for ARMv5 or above) or generate a stub.
801
    if ((x & 1) == 0)
802
      {
803
        // This only works for ARMv5 and above with interworking enabled.
804
        lo &= 0xefff;
805
      }
806
    hi = utils::bit_select(hi, (x >> 12), 0x7ffU);
807
    lo = utils::bit_select(lo, (x >> 1), 0x7ffU);
808
    elfcpp::Swap<16, big_endian>::writeval(wv, hi);
809
    elfcpp::Swap<16, big_endian>::writeval(wv + 1, lo);
810
    return (utils::has_overflow<23>(x)
811
            ? This::STATUS_OVERFLOW
812
            : This::STATUS_OKAY);
813
  }
814
 
815
  // R_ARM_BASE_PREL: B(S) + A - P
816
  static inline typename This::Status
817
  base_prel(unsigned char* view,
818
            elfcpp::Elf_types<32>::Elf_Addr origin,
819
            elfcpp::Elf_types<32>::Elf_Addr address)
820
  {
821
    Base::rel32(view, origin - address);
822
    return STATUS_OKAY;
823
  }
824
 
825
  // R_ARM_BASE_ABS: B(S) + A
826
  static inline typename This::Status
827
  base_abs(unsigned char* view,
828
            elfcpp::Elf_types<32>::Elf_Addr origin)
829
  {
830
    Base::rel32(view, origin);
831
    return STATUS_OKAY;
832
  }
833
 
834
  // R_ARM_GOT_BREL: GOT(S) + A - GOT_ORG
835
  static inline typename This::Status
836
  got_brel(unsigned char* view,
837
           typename elfcpp::Swap<32, big_endian>::Valtype got_offset)
838
  {
839
    Base::rel32(view, got_offset);
840
    return This::STATUS_OKAY;
841
  }
842
 
843
  // R_ARM_GOT_PREL: GOT(S) + A – P
844
  static inline typename This::Status
845
  got_prel(unsigned char* view,
846
           typename elfcpp::Swap<32, big_endian>::Valtype got_offset,
847
           elfcpp::Elf_types<32>::Elf_Addr address)
848
  {
849
    Base::rel32(view, got_offset - address);
850
    return This::STATUS_OKAY;
851
  }
852
 
853
  // R_ARM_PLT32: (S + A) | T - P
854
  static inline typename This::Status
855
  plt32(unsigned char *view,
856
        const Sized_relobj<32, big_endian>* object,
857
        const Symbol_value<32>* psymval,
858
        elfcpp::Elf_types<32>::Elf_Addr address,
859
        bool has_thumb_bit)
860
  {
861
    return arm_branch_common<elfcpp::R_ARM_PLT32>(view, object, psymval,
862
                                                  address, has_thumb_bit);
863
  }
864
 
865
  // R_ARM_CALL: (S + A) | T - P
866
  static inline typename This::Status
867
  call(unsigned char *view,
868
       const Sized_relobj<32, big_endian>* object,
869
       const Symbol_value<32>* psymval,
870
       elfcpp::Elf_types<32>::Elf_Addr address,
871
       bool has_thumb_bit)
872
  {
873
    return arm_branch_common<elfcpp::R_ARM_CALL>(view, object, psymval,
874
                                                 address, has_thumb_bit);
875
  }
876
 
877
  // R_ARM_JUMP24: (S + A) | T - P
878
  static inline typename This::Status
879
  jump24(unsigned char *view,
880
         const Sized_relobj<32, big_endian>* object,
881
         const Symbol_value<32>* psymval,
882
         elfcpp::Elf_types<32>::Elf_Addr address,
883
         bool has_thumb_bit)
884
  {
885
    return arm_branch_common<elfcpp::R_ARM_JUMP24>(view, object, psymval,
886
                                                   address, has_thumb_bit);
887
  }
888
 
889
  // R_ARM_PREL: (S + A) | T - P
890
  static inline typename This::Status
891
  prel31(unsigned char *view,
892
         const Sized_relobj<32, big_endian>* object,
893
         const Symbol_value<32>* psymval,
894
         elfcpp::Elf_types<32>::Elf_Addr address,
895
         bool has_thumb_bit)
896
  {
897
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
898
    Valtype* wv = reinterpret_cast<Valtype*>(view);
899
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
900
    Valtype addend = utils::sign_extend<31>(val);
901
    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
902
                 - address);
903
    val = utils::bit_select(val, x, 0x7fffffffU);
904
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
905
    return (utils::has_overflow<31>(x) ?
906
            This::STATUS_OVERFLOW : This::STATUS_OKAY);
907
  }
908
 
909
  // R_ARM_MOVW_ABS_NC: (S + A) | T
910
  static inline typename This::Status
911
  movw_abs_nc(unsigned char *view,
912
              const Sized_relobj<32, big_endian>* object,
913
              const Symbol_value<32>* psymval,
914
              bool has_thumb_bit)
915
  {
916
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
917
    Valtype* wv = reinterpret_cast<Valtype*>(view);
918
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
919
    Valtype addend =  This::extract_arm_movw_movt_addend(val);
920
    Valtype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
921
    val = This::insert_val_arm_movw_movt(val, x);
922
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
923
    return This::STATUS_OKAY;
924
  }
925
 
926
  // R_ARM_MOVT_ABS: S + A
927
  static inline typename This::Status
928
  movt_abs(unsigned char *view,
929
           const Sized_relobj<32, big_endian>* object,
930
           const Symbol_value<32>* psymval)
931
  {
932
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
933
    Valtype* wv = reinterpret_cast<Valtype*>(view);
934
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
935
    Valtype addend = This::extract_arm_movw_movt_addend(val);
936
    Valtype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
937
    val = This::insert_val_arm_movw_movt(val, x);
938
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
939
    return This::STATUS_OKAY;
940
  }
941
 
942
  //  R_ARM_THM_MOVW_ABS_NC: S + A | T
943
  static inline typename This::Status
944
  thm_movw_abs_nc(unsigned char *view,
945
                  const Sized_relobj<32, big_endian>* object,
946
                  const Symbol_value<32>* psymval,
947
                  bool has_thumb_bit)
948
  {
949
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
950
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
951
    Valtype* wv = reinterpret_cast<Valtype*>(view);
952
    Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
953
                   | elfcpp::Swap<16, big_endian>::readval(wv + 1));
954
    Reltype addend = extract_thumb_movw_movt_addend(val);
955
    Reltype x = This::arm_symbol_value(object, psymval, addend, has_thumb_bit);
956
    val = This::insert_val_thumb_movw_movt(val, x);
957
    elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
958
    elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
959
    return This::STATUS_OKAY;
960
  }
961
 
962
  //  R_ARM_THM_MOVT_ABS: S + A
963
  static inline typename This::Status
964
  thm_movt_abs(unsigned char *view,
965
               const Sized_relobj<32, big_endian>* object,
966
               const Symbol_value<32>* psymval)
967
  {
968
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
969
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
970
    Valtype* wv = reinterpret_cast<Valtype*>(view);
971
    Reltype val = ((elfcpp::Swap<16, big_endian>::readval(wv) << 16)
972
                   | elfcpp::Swap<16, big_endian>::readval(wv + 1));
973
    Reltype addend = This::extract_thumb_movw_movt_addend(val);
974
    Reltype x = This::arm_symbol_value(object, psymval, addend, 0) >> 16;
975
    val = This::insert_val_thumb_movw_movt(val, x);
976
    elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
977
    elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
978
    return This::STATUS_OKAY;
979
  }
980
 
981
  // R_ARM_MOVW_PREL_NC: (S + A) | T - P
982
  static inline typename This::Status
983
  movw_prel_nc(unsigned char *view,
984
               const Sized_relobj<32, big_endian>* object,
985
               const Symbol_value<32>* psymval,
986
               elfcpp::Elf_types<32>::Elf_Addr address,
987
               bool has_thumb_bit)
988
  {
989
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
990
    Valtype* wv = reinterpret_cast<Valtype*>(view);
991
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
992
    Valtype addend = This::extract_arm_movw_movt_addend(val);
993
    Valtype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
994
                 - address);
995
    val = This::insert_val_arm_movw_movt(val, x);
996
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
997
    return This::STATUS_OKAY;
998
  }
999
 
1000
  // R_ARM_MOVT_PREL: S + A - P
1001
  static inline typename This::Status
1002
  movt_prel(unsigned char *view,
1003
            const Sized_relobj<32, big_endian>* object,
1004
            const Symbol_value<32>* psymval,
1005
            elfcpp::Elf_types<32>::Elf_Addr address)
1006
  {
1007
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Valtype;
1008
    Valtype* wv = reinterpret_cast<Valtype*>(view);
1009
    Valtype val = elfcpp::Swap<32, big_endian>::readval(wv);
1010
    Valtype addend = This::extract_arm_movw_movt_addend(val);
1011
    Valtype x = (This::arm_symbol_value(object, psymval, addend, 0)
1012
                 - address) >> 16;
1013
    val = This::insert_val_arm_movw_movt(val, x);
1014
    elfcpp::Swap<32, big_endian>::writeval(wv, val);
1015
    return This::STATUS_OKAY;
1016
  }
1017
 
1018
  // R_ARM_THM_MOVW_PREL_NC: (S + A) | T - P
1019
  static inline typename This::Status
1020
  thm_movw_prel_nc(unsigned char *view,
1021
                   const Sized_relobj<32, big_endian>* object,
1022
                   const Symbol_value<32>* psymval,
1023
                   elfcpp::Elf_types<32>::Elf_Addr address,
1024
                   bool has_thumb_bit)
1025
  {
1026
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1027
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1028
    Valtype* wv = reinterpret_cast<Valtype*>(view);
1029
    Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1030
                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1031
    Reltype addend = This::extract_thumb_movw_movt_addend(val);
1032
    Reltype x = (This::arm_symbol_value(object, psymval, addend, has_thumb_bit)
1033
                 - address);
1034
    val = This::insert_val_thumb_movw_movt(val, x);
1035
    elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1036
    elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1037
    return This::STATUS_OKAY;
1038
  }
1039
 
1040
  // R_ARM_THM_MOVT_PREL: S + A - P
1041
  static inline typename This::Status
1042
  thm_movt_prel(unsigned char *view,
1043
                const Sized_relobj<32, big_endian>* object,
1044
                const Symbol_value<32>* psymval,
1045
                elfcpp::Elf_types<32>::Elf_Addr address)
1046
  {
1047
    typedef typename elfcpp::Swap<16, big_endian>::Valtype Valtype;
1048
    typedef typename elfcpp::Swap<32, big_endian>::Valtype Reltype;
1049
    Valtype* wv = reinterpret_cast<Valtype*>(view);
1050
    Reltype val = (elfcpp::Swap<16, big_endian>::readval(wv) << 16)
1051
                  | elfcpp::Swap<16, big_endian>::readval(wv + 1);
1052
    Reltype addend = This::extract_thumb_movw_movt_addend(val);
1053
    Reltype x = (This::arm_symbol_value(object, psymval, addend, 0)
1054
                 - address) >> 16;
1055
    val = This::insert_val_thumb_movw_movt(val, x);
1056
    elfcpp::Swap<16, big_endian>::writeval(wv, val >> 16);
1057
    elfcpp::Swap<16, big_endian>::writeval(wv + 1, val & 0xffff);
1058
    return This::STATUS_OKAY;
1059
  }
1060
};
1061
 
1062
// Get the GOT section, creating it if necessary.
1063
 
1064
template<bool big_endian>
1065
Output_data_got<32, big_endian>*
1066
Target_arm<big_endian>::got_section(Symbol_table* symtab, Layout* layout)
1067
{
1068
  if (this->got_ == NULL)
1069
    {
1070
      gold_assert(symtab != NULL && layout != NULL);
1071
 
1072
      this->got_ = new Output_data_got<32, big_endian>();
1073
 
1074
      Output_section* os;
1075
      os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1076
                                           (elfcpp::SHF_ALLOC
1077
                                            | elfcpp::SHF_WRITE),
1078
                                           this->got_, false);
1079
      os->set_is_relro();
1080
 
1081
      // The old GNU linker creates a .got.plt section.  We just
1082
      // create another set of data in the .got section.  Note that we
1083
      // always create a PLT if we create a GOT, although the PLT
1084
      // might be empty.
1085
      this->got_plt_ = new Output_data_space(4, "** GOT PLT");
1086
      os = layout->add_output_section_data(".got", elfcpp::SHT_PROGBITS,
1087
                                           (elfcpp::SHF_ALLOC
1088
                                            | elfcpp::SHF_WRITE),
1089
                                           this->got_plt_, false);
1090
      os->set_is_relro();
1091
 
1092
      // The first three entries are reserved.
1093
      this->got_plt_->set_current_data_size(3 * 4);
1094
 
1095
      // Define _GLOBAL_OFFSET_TABLE_ at the start of the PLT.
1096
      symtab->define_in_output_data("_GLOBAL_OFFSET_TABLE_", NULL,
1097
                                    this->got_plt_,
1098
                                    0, 0, elfcpp::STT_OBJECT,
1099
                                    elfcpp::STB_LOCAL,
1100
                                    elfcpp::STV_HIDDEN, 0,
1101
                                    false, false);
1102
    }
1103
  return this->got_;
1104
}
1105
 
1106
// Get the dynamic reloc section, creating it if necessary.
1107
 
1108
template<bool big_endian>
1109
typename Target_arm<big_endian>::Reloc_section*
1110
Target_arm<big_endian>::rel_dyn_section(Layout* layout)
1111
{
1112
  if (this->rel_dyn_ == NULL)
1113
    {
1114
      gold_assert(layout != NULL);
1115
      this->rel_dyn_ = new Reloc_section(parameters->options().combreloc());
1116
      layout->add_output_section_data(".rel.dyn", elfcpp::SHT_REL,
1117
                                      elfcpp::SHF_ALLOC, this->rel_dyn_, true);
1118
    }
1119
  return this->rel_dyn_;
1120
}
1121
 
1122
// A class to handle the PLT data.
1123
 
1124
template<bool big_endian>
1125
class Output_data_plt_arm : public Output_section_data
1126
{
1127
 public:
1128
  typedef Output_data_reloc<elfcpp::SHT_REL, true, 32, big_endian>
1129
    Reloc_section;
1130
 
1131
  Output_data_plt_arm(Layout*, Output_data_space*);
1132
 
1133
  // Add an entry to the PLT.
1134
  void
1135
  add_entry(Symbol* gsym);
1136
 
1137
  // Return the .rel.plt section data.
1138
  const Reloc_section*
1139
  rel_plt() const
1140
  { return this->rel_; }
1141
 
1142
 protected:
1143
  void
1144
  do_adjust_output_section(Output_section* os);
1145
 
1146
  // Write to a map file.
1147
  void
1148
  do_print_to_mapfile(Mapfile* mapfile) const
1149
  { mapfile->print_output_data(this, _("** PLT")); }
1150
 
1151
 private:
1152
  // Template for the first PLT entry.
1153
  static const uint32_t first_plt_entry[5];
1154
 
1155
  // Template for subsequent PLT entries. 
1156
  static const uint32_t plt_entry[3];
1157
 
1158
  // Set the final size.
1159
  void
1160
  set_final_data_size()
1161
  {
1162
    this->set_data_size(sizeof(first_plt_entry)
1163
                        + this->count_ * sizeof(plt_entry));
1164
  }
1165
 
1166
  // Write out the PLT data.
1167
  void
1168
  do_write(Output_file*);
1169
 
1170
  // The reloc section.
1171
  Reloc_section* rel_;
1172
  // The .got.plt section.
1173
  Output_data_space* got_plt_;
1174
  // The number of PLT entries.
1175
  unsigned int count_;
1176
};
1177
 
1178
// Create the PLT section.  The ordinary .got section is an argument,
1179
// since we need to refer to the start.  We also create our own .got
1180
// section just for PLT entries.
1181
 
1182
template<bool big_endian>
1183
Output_data_plt_arm<big_endian>::Output_data_plt_arm(Layout* layout,
1184
                                                     Output_data_space* got_plt)
1185
  : Output_section_data(4), got_plt_(got_plt), count_(0)
1186
{
1187
  this->rel_ = new Reloc_section(false);
1188
  layout->add_output_section_data(".rel.plt", elfcpp::SHT_REL,
1189
                                  elfcpp::SHF_ALLOC, this->rel_, true);
1190
}
1191
 
1192
template<bool big_endian>
1193
void
1194
Output_data_plt_arm<big_endian>::do_adjust_output_section(Output_section* os)
1195
{
1196
  os->set_entsize(0);
1197
}
1198
 
1199
// Add an entry to the PLT.
1200
 
1201
template<bool big_endian>
1202
void
1203
Output_data_plt_arm<big_endian>::add_entry(Symbol* gsym)
1204
{
1205
  gold_assert(!gsym->has_plt_offset());
1206
 
1207
  // Note that when setting the PLT offset we skip the initial
1208
  // reserved PLT entry.
1209
  gsym->set_plt_offset((this->count_) * sizeof(plt_entry)
1210
                       + sizeof(first_plt_entry));
1211
 
1212
  ++this->count_;
1213
 
1214
  section_offset_type got_offset = this->got_plt_->current_data_size();
1215
 
1216
  // Every PLT entry needs a GOT entry which points back to the PLT
1217
  // entry (this will be changed by the dynamic linker, normally
1218
  // lazily when the function is called).
1219
  this->got_plt_->set_current_data_size(got_offset + 4);
1220
 
1221
  // Every PLT entry needs a reloc.
1222
  gsym->set_needs_dynsym_entry();
1223
  this->rel_->add_global(gsym, elfcpp::R_ARM_JUMP_SLOT, this->got_plt_,
1224
                         got_offset);
1225
 
1226
  // Note that we don't need to save the symbol.  The contents of the
1227
  // PLT are independent of which symbols are used.  The symbols only
1228
  // appear in the relocations.
1229
}
1230
 
1231
// ARM PLTs.
1232
// FIXME:  This is not very flexible.  Right now this has only been tested
1233
// on armv5te.  If we are to support additional architecture features like
1234
// Thumb-2 or BE8, we need to make this more flexible like GNU ld.
1235
 
1236
// The first entry in the PLT.
1237
template<bool big_endian>
1238
const uint32_t Output_data_plt_arm<big_endian>::first_plt_entry[5] =
1239
{
1240
  0xe52de004,   // str   lr, [sp, #-4]!
1241
  0xe59fe004,   // ldr   lr, [pc, #4]
1242
  0xe08fe00e,   // add   lr, pc, lr 
1243
  0xe5bef008,   // ldr   pc, [lr, #8]!
1244
  0x00000000,   // &GOT[0] - .
1245
};
1246
 
1247
// Subsequent entries in the PLT.
1248
 
1249
template<bool big_endian>
1250
const uint32_t Output_data_plt_arm<big_endian>::plt_entry[3] =
1251
{
1252
  0xe28fc600,   // add   ip, pc, #0xNN00000
1253
  0xe28cca00,   // add   ip, ip, #0xNN000
1254
  0xe5bcf000,   // ldr   pc, [ip, #0xNNN]!
1255
};
1256
 
1257
// Write out the PLT.  This uses the hand-coded instructions above,
1258
// and adjusts them as needed.  This is all specified by the arm ELF
1259
// Processor Supplement.
1260
 
1261
template<bool big_endian>
1262
void
1263
Output_data_plt_arm<big_endian>::do_write(Output_file* of)
1264
{
1265
  const off_t offset = this->offset();
1266
  const section_size_type oview_size =
1267
    convert_to_section_size_type(this->data_size());
1268
  unsigned char* const oview = of->get_output_view(offset, oview_size);
1269
 
1270
  const off_t got_file_offset = this->got_plt_->offset();
1271
  const section_size_type got_size =
1272
    convert_to_section_size_type(this->got_plt_->data_size());
1273
  unsigned char* const got_view = of->get_output_view(got_file_offset,
1274
                                                      got_size);
1275
  unsigned char* pov = oview;
1276
 
1277
  elfcpp::Elf_types<32>::Elf_Addr plt_address = this->address();
1278
  elfcpp::Elf_types<32>::Elf_Addr got_address = this->got_plt_->address();
1279
 
1280
  // Write first PLT entry.  All but the last word are constants.
1281
  const size_t num_first_plt_words = (sizeof(first_plt_entry)
1282
                                      / sizeof(plt_entry[0]));
1283
  for (size_t i = 0; i < num_first_plt_words - 1; i++)
1284
    elfcpp::Swap<32, big_endian>::writeval(pov + i * 4, first_plt_entry[i]);
1285
  // Last word in first PLT entry is &GOT[0] - .
1286
  elfcpp::Swap<32, big_endian>::writeval(pov + 16,
1287
                                         got_address - (plt_address + 16));
1288
  pov += sizeof(first_plt_entry);
1289
 
1290
  unsigned char* got_pov = got_view;
1291
 
1292
  memset(got_pov, 0, 12);
1293
  got_pov += 12;
1294
 
1295
  const int rel_size = elfcpp::Elf_sizes<32>::rel_size;
1296
  unsigned int plt_offset = sizeof(first_plt_entry);
1297
  unsigned int plt_rel_offset = 0;
1298
  unsigned int got_offset = 12;
1299
  const unsigned int count = this->count_;
1300
  for (unsigned int i = 0;
1301
       i < count;
1302
       ++i,
1303
         pov += sizeof(plt_entry),
1304
         got_pov += 4,
1305
         plt_offset += sizeof(plt_entry),
1306
         plt_rel_offset += rel_size,
1307
         got_offset += 4)
1308
    {
1309
      // Set and adjust the PLT entry itself.
1310
      int32_t offset = ((got_address + got_offset)
1311
                         - (plt_address + plt_offset + 8));
1312
 
1313
      gold_assert(offset >= 0 && offset < 0x0fffffff);
1314
      uint32_t plt_insn0 = plt_entry[0] | ((offset >> 20) & 0xff);
1315
      elfcpp::Swap<32, big_endian>::writeval(pov, plt_insn0);
1316
      uint32_t plt_insn1 = plt_entry[1] | ((offset >> 12) & 0xff);
1317
      elfcpp::Swap<32, big_endian>::writeval(pov + 4, plt_insn1);
1318
      uint32_t plt_insn2 = plt_entry[2] | (offset & 0xfff);
1319
      elfcpp::Swap<32, big_endian>::writeval(pov + 8, plt_insn2);
1320
 
1321
      // Set the entry in the GOT.
1322
      elfcpp::Swap<32, big_endian>::writeval(got_pov, plt_address);
1323
    }
1324
 
1325
  gold_assert(static_cast<section_size_type>(pov - oview) == oview_size);
1326
  gold_assert(static_cast<section_size_type>(got_pov - got_view) == got_size);
1327
 
1328
  of->write_output_view(offset, oview_size, oview);
1329
  of->write_output_view(got_file_offset, got_size, got_view);
1330
}
1331
 
1332
// Create a PLT entry for a global symbol.
1333
 
1334
template<bool big_endian>
1335
void
1336
Target_arm<big_endian>::make_plt_entry(Symbol_table* symtab, Layout* layout,
1337
                                       Symbol* gsym)
1338
{
1339
  if (gsym->has_plt_offset())
1340
    return;
1341
 
1342
  if (this->plt_ == NULL)
1343
    {
1344
      // Create the GOT sections first.
1345
      this->got_section(symtab, layout);
1346
 
1347
      this->plt_ = new Output_data_plt_arm<big_endian>(layout, this->got_plt_);
1348
      layout->add_output_section_data(".plt", elfcpp::SHT_PROGBITS,
1349
                                      (elfcpp::SHF_ALLOC
1350
                                       | elfcpp::SHF_EXECINSTR),
1351
                                      this->plt_, false);
1352
    }
1353
  this->plt_->add_entry(gsym);
1354
}
1355
 
1356
// Report an unsupported relocation against a local symbol.
1357
 
1358
template<bool big_endian>
1359
void
1360
Target_arm<big_endian>::Scan::unsupported_reloc_local(
1361
    Sized_relobj<32, big_endian>* object,
1362
    unsigned int r_type)
1363
{
1364
  gold_error(_("%s: unsupported reloc %u against local symbol"),
1365
             object->name().c_str(), r_type);
1366
}
1367
 
1368
// We are about to emit a dynamic relocation of type R_TYPE.  If the
1369
// dynamic linker does not support it, issue an error.  The GNU linker
1370
// only issues a non-PIC error for an allocated read-only section.
1371
// Here we know the section is allocated, but we don't know that it is
1372
// read-only.  But we check for all the relocation types which the
1373
// glibc dynamic linker supports, so it seems appropriate to issue an
1374
// error even if the section is not read-only.
1375
 
1376
template<bool big_endian>
1377
void
1378
Target_arm<big_endian>::Scan::check_non_pic(Relobj* object,
1379
                                            unsigned int r_type)
1380
{
1381
  switch (r_type)
1382
    {
1383
    // These are the relocation types supported by glibc for ARM.
1384
    case elfcpp::R_ARM_RELATIVE:
1385
    case elfcpp::R_ARM_COPY:
1386
    case elfcpp::R_ARM_GLOB_DAT:
1387
    case elfcpp::R_ARM_JUMP_SLOT:
1388
    case elfcpp::R_ARM_ABS32:
1389
    case elfcpp::R_ARM_ABS32_NOI:
1390
    case elfcpp::R_ARM_PC24:
1391
    // FIXME: The following 3 types are not supported by Android's dynamic
1392
    // linker.
1393
    case elfcpp::R_ARM_TLS_DTPMOD32:
1394
    case elfcpp::R_ARM_TLS_DTPOFF32:
1395
    case elfcpp::R_ARM_TLS_TPOFF32:
1396
      return;
1397
 
1398
    default:
1399
      // This prevents us from issuing more than one error per reloc
1400
      // section.  But we can still wind up issuing more than one
1401
      // error per object file.
1402
      if (this->issued_non_pic_error_)
1403
        return;
1404
      object->error(_("requires unsupported dynamic reloc; "
1405
                      "recompile with -fPIC"));
1406
      this->issued_non_pic_error_ = true;
1407
      return;
1408
 
1409
    case elfcpp::R_ARM_NONE:
1410
      gold_unreachable();
1411
    }
1412
}
1413
 
1414
// Scan a relocation for a local symbol.
1415
// FIXME: This only handles a subset of relocation types used by Android
1416
// on ARM v5te devices.
1417
 
1418
template<bool big_endian>
1419
inline void
1420
Target_arm<big_endian>::Scan::local(const General_options&,
1421
                                    Symbol_table* symtab,
1422
                                    Layout* layout,
1423
                                    Target_arm* target,
1424
                                    Sized_relobj<32, big_endian>* object,
1425
                                    unsigned int data_shndx,
1426
                                    Output_section* output_section,
1427
                                    const elfcpp::Rel<32, big_endian>& reloc,
1428
                                    unsigned int r_type,
1429
                                    const elfcpp::Sym<32, big_endian>&)
1430
{
1431
  r_type = get_real_reloc_type(r_type);
1432
  switch (r_type)
1433
    {
1434
    case elfcpp::R_ARM_NONE:
1435
      break;
1436
 
1437
    case elfcpp::R_ARM_ABS32:
1438
    case elfcpp::R_ARM_ABS32_NOI:
1439
      // If building a shared library (or a position-independent
1440
      // executable), we need to create a dynamic relocation for
1441
      // this location. The relocation applied at link time will
1442
      // apply the link-time value, so we flag the location with
1443
      // an R_ARM_RELATIVE relocation so the dynamic loader can
1444
      // relocate it easily.
1445
      if (parameters->options().output_is_position_independent())
1446
        {
1447
          Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1448
          unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1449
          // If we are to add more other reloc types than R_ARM_ABS32,
1450
          // we need to add check_non_pic(object, r_type) here.
1451
          rel_dyn->add_local_relative(object, r_sym, elfcpp::R_ARM_RELATIVE,
1452
                                      output_section, data_shndx,
1453
                                      reloc.get_r_offset());
1454
        }
1455
      break;
1456
 
1457
    case elfcpp::R_ARM_REL32:
1458
    case elfcpp::R_ARM_THM_CALL:
1459
    case elfcpp::R_ARM_CALL:
1460
    case elfcpp::R_ARM_PREL31:
1461
    case elfcpp::R_ARM_JUMP24:
1462
    case elfcpp::R_ARM_PLT32:
1463
    case elfcpp::R_ARM_THM_ABS5:
1464
    case elfcpp::R_ARM_ABS8:
1465
    case elfcpp::R_ARM_ABS12:
1466
    case elfcpp::R_ARM_ABS16:
1467
    case elfcpp::R_ARM_BASE_ABS:
1468
    case elfcpp::R_ARM_MOVW_ABS_NC:
1469
    case elfcpp::R_ARM_MOVT_ABS:
1470
    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1471
    case elfcpp::R_ARM_THM_MOVT_ABS:
1472
    case elfcpp::R_ARM_MOVW_PREL_NC:
1473
    case elfcpp::R_ARM_MOVT_PREL:
1474
    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1475
    case elfcpp::R_ARM_THM_MOVT_PREL:
1476
      break;
1477
 
1478
    case elfcpp::R_ARM_GOTOFF32:
1479
      // We need a GOT section:
1480
      target->got_section(symtab, layout);
1481
      break;
1482
 
1483
    case elfcpp::R_ARM_BASE_PREL:
1484
      // FIXME: What about this?
1485
      break;
1486
 
1487
    case elfcpp::R_ARM_GOT_BREL:
1488
    case elfcpp::R_ARM_GOT_PREL:
1489
      {
1490
        // The symbol requires a GOT entry.
1491
        Output_data_got<32, big_endian>* got =
1492
          target->got_section(symtab, layout);
1493
        unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1494
        if (got->add_local(object, r_sym, GOT_TYPE_STANDARD))
1495
          {
1496
            // If we are generating a shared object, we need to add a
1497
            // dynamic RELATIVE relocation for this symbol's GOT entry.
1498
            if (parameters->options().output_is_position_independent())
1499
              {
1500
                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1501
                unsigned int r_sym = elfcpp::elf_r_sym<32>(reloc.get_r_info());
1502
                rel_dyn->add_local_relative(
1503
                    object, r_sym, elfcpp::R_ARM_RELATIVE, got,
1504
                    object->local_got_offset(r_sym, GOT_TYPE_STANDARD));
1505
              }
1506
          }
1507
      }
1508
      break;
1509
 
1510
    case elfcpp::R_ARM_TARGET1:
1511
      // This should have been mapped to another type already.
1512
      // Fall through.
1513
    case elfcpp::R_ARM_COPY:
1514
    case elfcpp::R_ARM_GLOB_DAT:
1515
    case elfcpp::R_ARM_JUMP_SLOT:
1516
    case elfcpp::R_ARM_RELATIVE:
1517
      // These are relocations which should only be seen by the
1518
      // dynamic linker, and should never be seen here.
1519
      gold_error(_("%s: unexpected reloc %u in object file"),
1520
                 object->name().c_str(), r_type);
1521
      break;
1522
 
1523
    default:
1524
      unsupported_reloc_local(object, r_type);
1525
      break;
1526
    }
1527
}
1528
 
1529
// Report an unsupported relocation against a global symbol.
1530
 
1531
template<bool big_endian>
1532
void
1533
Target_arm<big_endian>::Scan::unsupported_reloc_global(
1534
    Sized_relobj<32, big_endian>* object,
1535
    unsigned int r_type,
1536
    Symbol* gsym)
1537
{
1538
  gold_error(_("%s: unsupported reloc %u against global symbol %s"),
1539
             object->name().c_str(), r_type, gsym->demangled_name().c_str());
1540
}
1541
 
1542
// Scan a relocation for a global symbol.
1543
// FIXME: This only handles a subset of relocation types used by Android
1544
// on ARM v5te devices.
1545
 
1546
template<bool big_endian>
1547
inline void
1548
Target_arm<big_endian>::Scan::global(const General_options&,
1549
                                     Symbol_table* symtab,
1550
                                     Layout* layout,
1551
                                     Target_arm* target,
1552
                                     Sized_relobj<32, big_endian>* object,
1553
                                     unsigned int data_shndx,
1554
                                     Output_section* output_section,
1555
                                     const elfcpp::Rel<32, big_endian>& reloc,
1556
                                     unsigned int r_type,
1557
                                     Symbol* gsym)
1558
{
1559
  r_type = get_real_reloc_type(r_type);
1560
  switch (r_type)
1561
    {
1562
    case elfcpp::R_ARM_NONE:
1563
      break;
1564
 
1565
    case elfcpp::R_ARM_ABS32:
1566
    case elfcpp::R_ARM_ABS32_NOI:
1567
      {
1568
        // Make a dynamic relocation if necessary.
1569
        if (gsym->needs_dynamic_reloc(Symbol::ABSOLUTE_REF))
1570
          {
1571
            if (target->may_need_copy_reloc(gsym))
1572
              {
1573
                target->copy_reloc(symtab, layout, object,
1574
                                   data_shndx, output_section, gsym, reloc);
1575
              }
1576
            else if (gsym->can_use_relative_reloc(false))
1577
              {
1578
                // If we are to add more other reloc types than R_ARM_ABS32,
1579
                // we need to add check_non_pic(object, r_type) here.
1580
                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1581
                rel_dyn->add_global_relative(gsym, elfcpp::R_ARM_RELATIVE,
1582
                                             output_section, object,
1583
                                             data_shndx, reloc.get_r_offset());
1584
              }
1585
            else
1586
              {
1587
                // If we are to add more other reloc types than R_ARM_ABS32,
1588
                // we need to add check_non_pic(object, r_type) here.
1589
                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1590
                rel_dyn->add_global(gsym, r_type, output_section, object,
1591
                                    data_shndx, reloc.get_r_offset());
1592
              }
1593
          }
1594
      }
1595
      break;
1596
 
1597
    case elfcpp::R_ARM_MOVW_ABS_NC:
1598
    case elfcpp::R_ARM_MOVT_ABS:
1599
    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
1600
    case elfcpp::R_ARM_THM_MOVT_ABS:
1601
    case elfcpp::R_ARM_MOVW_PREL_NC:
1602
    case elfcpp::R_ARM_MOVT_PREL:
1603
    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
1604
    case elfcpp::R_ARM_THM_MOVT_PREL:
1605
      break;
1606
 
1607
    case elfcpp::R_ARM_THM_ABS5:
1608
    case elfcpp::R_ARM_ABS8:
1609
    case elfcpp::R_ARM_ABS12:
1610
    case elfcpp::R_ARM_ABS16:
1611
    case elfcpp::R_ARM_BASE_ABS:
1612
      {
1613
        // No dynamic relocs of this kinds.
1614
        // Report the error in case of PIC.
1615
        int flags = Symbol::NON_PIC_REF;
1616
        if (gsym->type() == elfcpp::STT_FUNC
1617
            || gsym->type() == elfcpp::STT_ARM_TFUNC)
1618
          flags |= Symbol::FUNCTION_CALL;
1619
        if (gsym->needs_dynamic_reloc(flags))
1620
          check_non_pic(object, r_type);
1621
      }
1622
      break;
1623
 
1624
    case elfcpp::R_ARM_REL32:
1625
    case elfcpp::R_ARM_PREL31:
1626
      {
1627
        // Make a dynamic relocation if necessary.
1628
        int flags = Symbol::NON_PIC_REF;
1629
        if (gsym->needs_dynamic_reloc(flags))
1630
          {
1631
            if (target->may_need_copy_reloc(gsym))
1632
              {
1633
                target->copy_reloc(symtab, layout, object,
1634
                                   data_shndx, output_section, gsym, reloc);
1635
              }
1636
            else
1637
              {
1638
                check_non_pic(object, r_type);
1639
                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1640
                rel_dyn->add_global(gsym, r_type, output_section, object,
1641
                                    data_shndx, reloc.get_r_offset());
1642
              }
1643
          }
1644
      }
1645
      break;
1646
 
1647
    case elfcpp::R_ARM_JUMP24:
1648
    case elfcpp::R_ARM_THM_CALL:
1649
    case elfcpp::R_ARM_CALL:
1650
      {
1651
        if (Target_arm<big_endian>::Scan::symbol_needs_plt_entry(gsym))
1652
          target->make_plt_entry(symtab, layout, gsym);
1653
        // Make a dynamic relocation if necessary.
1654
        int flags = Symbol::NON_PIC_REF;
1655
        if (gsym->type() == elfcpp::STT_FUNC
1656
            || gsym->type() == elfcpp::STT_ARM_TFUNC)
1657
          flags |= Symbol::FUNCTION_CALL;
1658
        if (gsym->needs_dynamic_reloc(flags))
1659
          {
1660
            if (target->may_need_copy_reloc(gsym))
1661
              {
1662
                target->copy_reloc(symtab, layout, object,
1663
                                   data_shndx, output_section, gsym,
1664
                                   reloc);
1665
              }
1666
            else
1667
              {
1668
                check_non_pic(object, r_type);
1669
                Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1670
                rel_dyn->add_global(gsym, r_type, output_section, object,
1671
                                    data_shndx, reloc.get_r_offset());
1672
              }
1673
          }
1674
      }
1675
      break;
1676
 
1677
    case elfcpp::R_ARM_PLT32:
1678
      // If the symbol is fully resolved, this is just a relative
1679
      // local reloc.  Otherwise we need a PLT entry.
1680
      if (gsym->final_value_is_known())
1681
        break;
1682
      // If building a shared library, we can also skip the PLT entry
1683
      // if the symbol is defined in the output file and is protected
1684
      // or hidden.
1685
      if (gsym->is_defined()
1686
          && !gsym->is_from_dynobj()
1687
          && !gsym->is_preemptible())
1688
        break;
1689
      target->make_plt_entry(symtab, layout, gsym);
1690
      break;
1691
 
1692
    case elfcpp::R_ARM_GOTOFF32:
1693
      // We need a GOT section.
1694
      target->got_section(symtab, layout);
1695
      break;
1696
 
1697
    case elfcpp::R_ARM_BASE_PREL:
1698
      // FIXME: What about this?
1699
      break;
1700
 
1701
    case elfcpp::R_ARM_GOT_BREL:
1702
    case elfcpp::R_ARM_GOT_PREL:
1703
      {
1704
        // The symbol requires a GOT entry.
1705
        Output_data_got<32, big_endian>* got =
1706
          target->got_section(symtab, layout);
1707
        if (gsym->final_value_is_known())
1708
          got->add_global(gsym, GOT_TYPE_STANDARD);
1709
        else
1710
          {
1711
            // If this symbol is not fully resolved, we need to add a
1712
            // GOT entry with a dynamic relocation.
1713
            Reloc_section* rel_dyn = target->rel_dyn_section(layout);
1714
            if (gsym->is_from_dynobj()
1715
                || gsym->is_undefined()
1716
                || gsym->is_preemptible())
1717
              got->add_global_with_rel(gsym, GOT_TYPE_STANDARD,
1718
                                       rel_dyn, elfcpp::R_ARM_GLOB_DAT);
1719
            else
1720
              {
1721
                if (got->add_global(gsym, GOT_TYPE_STANDARD))
1722
                  rel_dyn->add_global_relative(
1723
                      gsym, elfcpp::R_ARM_RELATIVE, got,
1724
                      gsym->got_offset(GOT_TYPE_STANDARD));
1725
              }
1726
          }
1727
      }
1728
      break;
1729
 
1730
    case elfcpp::R_ARM_TARGET1:
1731
      // This should have been mapped to another type already.
1732
      // Fall through.
1733
    case elfcpp::R_ARM_COPY:
1734
    case elfcpp::R_ARM_GLOB_DAT:
1735
    case elfcpp::R_ARM_JUMP_SLOT:
1736
    case elfcpp::R_ARM_RELATIVE:
1737
      // These are relocations which should only be seen by the
1738
      // dynamic linker, and should never be seen here.
1739
      gold_error(_("%s: unexpected reloc %u in object file"),
1740
                 object->name().c_str(), r_type);
1741
      break;
1742
 
1743
    default:
1744
      unsupported_reloc_global(object, r_type, gsym);
1745
      break;
1746
    }
1747
}
1748
 
1749
// Process relocations for gc.
1750
 
1751
template<bool big_endian>
1752
void
1753
Target_arm<big_endian>::gc_process_relocs(const General_options& options,
1754
                                          Symbol_table* symtab,
1755
                                          Layout* layout,
1756
                                          Sized_relobj<32, big_endian>* object,
1757
                                          unsigned int data_shndx,
1758
                                          unsigned int,
1759
                                          const unsigned char* prelocs,
1760
                                          size_t reloc_count,
1761
                                          Output_section* output_section,
1762
                                          bool needs_special_offset_handling,
1763
                                          size_t local_symbol_count,
1764
                                          const unsigned char* plocal_symbols)
1765
{
1766
  typedef Target_arm<big_endian> Arm;
1767
  typedef typename Target_arm<big_endian>::Scan Scan;
1768
 
1769
  gold::gc_process_relocs<32, big_endian, Arm, elfcpp::SHT_REL, Scan>(
1770
    options,
1771
    symtab,
1772
    layout,
1773
    this,
1774
    object,
1775
    data_shndx,
1776
    prelocs,
1777
    reloc_count,
1778
    output_section,
1779
    needs_special_offset_handling,
1780
    local_symbol_count,
1781
    plocal_symbols);
1782
}
1783
 
1784
// Scan relocations for a section.
1785
 
1786
template<bool big_endian>
1787
void
1788
Target_arm<big_endian>::scan_relocs(const General_options& options,
1789
                                    Symbol_table* symtab,
1790
                                    Layout* layout,
1791
                                    Sized_relobj<32, big_endian>* object,
1792
                                    unsigned int data_shndx,
1793
                                    unsigned int sh_type,
1794
                                    const unsigned char* prelocs,
1795
                                    size_t reloc_count,
1796
                                    Output_section* output_section,
1797
                                    bool needs_special_offset_handling,
1798
                                    size_t local_symbol_count,
1799
                                    const unsigned char* plocal_symbols)
1800
{
1801
  typedef typename Target_arm<big_endian>::Scan Scan;
1802
  if (sh_type == elfcpp::SHT_RELA)
1803
    {
1804
      gold_error(_("%s: unsupported RELA reloc section"),
1805
                 object->name().c_str());
1806
      return;
1807
    }
1808
 
1809
  gold::scan_relocs<32, big_endian, Target_arm, elfcpp::SHT_REL, Scan>(
1810
    options,
1811
    symtab,
1812
    layout,
1813
    this,
1814
    object,
1815
    data_shndx,
1816
    prelocs,
1817
    reloc_count,
1818
    output_section,
1819
    needs_special_offset_handling,
1820
    local_symbol_count,
1821
    plocal_symbols);
1822
}
1823
 
1824
// Finalize the sections.
1825
 
1826
template<bool big_endian>
1827
void
1828
Target_arm<big_endian>::do_finalize_sections(Layout* layout)
1829
{
1830
  // Fill in some more dynamic tags.
1831
  Output_data_dynamic* const odyn = layout->dynamic_data();
1832
  if (odyn != NULL)
1833
    {
1834
      if (this->got_plt_ != NULL
1835
          && this->got_plt_->output_section() != NULL)
1836
        odyn->add_section_address(elfcpp::DT_PLTGOT, this->got_plt_);
1837
 
1838
      if (this->plt_ != NULL
1839
          && this->plt_->output_section() != NULL)
1840
        {
1841
          const Output_data* od = this->plt_->rel_plt();
1842
          odyn->add_section_size(elfcpp::DT_PLTRELSZ, od);
1843
          odyn->add_section_address(elfcpp::DT_JMPREL, od);
1844
          odyn->add_constant(elfcpp::DT_PLTREL, elfcpp::DT_REL);
1845
        }
1846
 
1847
      if (this->rel_dyn_ != NULL
1848
          && this->rel_dyn_->output_section() != NULL)
1849
        {
1850
          const Output_data* od = this->rel_dyn_;
1851
          odyn->add_section_address(elfcpp::DT_REL, od);
1852
          odyn->add_section_size(elfcpp::DT_RELSZ, od);
1853
          odyn->add_constant(elfcpp::DT_RELENT,
1854
                             elfcpp::Elf_sizes<32>::rel_size);
1855
        }
1856
 
1857
      if (!parameters->options().shared())
1858
        {
1859
          // The value of the DT_DEBUG tag is filled in by the dynamic
1860
          // linker at run time, and used by the debugger.
1861
          odyn->add_constant(elfcpp::DT_DEBUG, 0);
1862
        }
1863
    }
1864
 
1865
  // Emit any relocs we saved in an attempt to avoid generating COPY
1866
  // relocs.
1867
  if (this->copy_relocs_.any_saved_relocs())
1868
    this->copy_relocs_.emit(this->rel_dyn_section(layout));
1869
 
1870
  // For the ARM target, we need to add a PT_ARM_EXIDX segment for
1871
  // the .ARM.exidx section.
1872
  if (!layout->script_options()->saw_phdrs_clause()
1873
      && !parameters->options().relocatable())
1874
    {
1875
      Output_section* exidx_section =
1876
        layout->find_output_section(".ARM.exidx");
1877
 
1878
      if (exidx_section != NULL
1879
          && exidx_section->type() == elfcpp::SHT_ARM_EXIDX)
1880
        {
1881
          gold_assert(layout->find_output_segment(elfcpp::PT_ARM_EXIDX, 0, 0)
1882
                      == NULL);
1883
          Output_segment*  exidx_segment =
1884
            layout->make_output_segment(elfcpp::PT_ARM_EXIDX, elfcpp::PF_R);
1885
          exidx_segment->add_output_section(exidx_section, elfcpp::PF_R,
1886
                                            false);
1887
        }
1888
    }
1889
}
1890
 
1891
// Return whether a direct absolute static relocation needs to be applied.
1892
// In cases where Scan::local() or Scan::global() has created
1893
// a dynamic relocation other than R_ARM_RELATIVE, the addend
1894
// of the relocation is carried in the data, and we must not
1895
// apply the static relocation.
1896
 
1897
template<bool big_endian>
1898
inline bool
1899
Target_arm<big_endian>::Relocate::should_apply_static_reloc(
1900
    const Sized_symbol<32>* gsym,
1901
    int ref_flags,
1902
    bool is_32bit,
1903
    Output_section* output_section)
1904
{
1905
  // If the output section is not allocated, then we didn't call
1906
  // scan_relocs, we didn't create a dynamic reloc, and we must apply
1907
  // the reloc here.
1908
  if ((output_section->flags() & elfcpp::SHF_ALLOC) == 0)
1909
      return true;
1910
 
1911
  // For local symbols, we will have created a non-RELATIVE dynamic
1912
  // relocation only if (a) the output is position independent,
1913
  // (b) the relocation is absolute (not pc- or segment-relative), and
1914
  // (c) the relocation is not 32 bits wide.
1915
  if (gsym == NULL)
1916
    return !(parameters->options().output_is_position_independent()
1917
             && (ref_flags & Symbol::ABSOLUTE_REF)
1918
             && !is_32bit);
1919
 
1920
  // For global symbols, we use the same helper routines used in the
1921
  // scan pass.  If we did not create a dynamic relocation, or if we
1922
  // created a RELATIVE dynamic relocation, we should apply the static
1923
  // relocation.
1924
  bool has_dyn = gsym->needs_dynamic_reloc(ref_flags);
1925
  bool is_rel = (ref_flags & Symbol::ABSOLUTE_REF)
1926
                 && gsym->can_use_relative_reloc(ref_flags
1927
                                                 & Symbol::FUNCTION_CALL);
1928
  return !has_dyn || is_rel;
1929
}
1930
 
1931
// Perform a relocation.
1932
 
1933
template<bool big_endian>
1934
inline bool
1935
Target_arm<big_endian>::Relocate::relocate(
1936
    const Relocate_info<32, big_endian>* relinfo,
1937
    Target_arm* target,
1938
    Output_section *output_section,
1939
    size_t relnum,
1940
    const elfcpp::Rel<32, big_endian>& rel,
1941
    unsigned int r_type,
1942
    const Sized_symbol<32>* gsym,
1943
    const Symbol_value<32>* psymval,
1944
    unsigned char* view,
1945
    elfcpp::Elf_types<32>::Elf_Addr address,
1946
    section_size_type /* view_size */ )
1947
{
1948
  typedef Arm_relocate_functions<big_endian> Arm_relocate_functions;
1949
 
1950
  r_type = get_real_reloc_type(r_type);
1951
 
1952
  // If this the symbol may be a Thumb function, set thumb bit to 1.
1953
  bool has_thumb_bit = ((gsym != NULL)
1954
                        && (gsym->type() == elfcpp::STT_FUNC
1955
                            || gsym->type() == elfcpp::STT_ARM_TFUNC));
1956
 
1957
  // Pick the value to use for symbols defined in shared objects.
1958
  Symbol_value<32> symval;
1959
  if (gsym != NULL
1960
      && gsym->use_plt_offset(reloc_is_non_pic(r_type)))
1961
    {
1962
      symval.set_output_value(target->plt_section()->address()
1963
                              + gsym->plt_offset());
1964
      psymval = &symval;
1965
      has_thumb_bit = 0;
1966
    }
1967
 
1968
  const Sized_relobj<32, big_endian>* object = relinfo->object;
1969
 
1970
  // Get the GOT offset if needed.
1971
  // The GOT pointer points to the end of the GOT section.
1972
  // We need to subtract the size of the GOT section to get
1973
  // the actual offset to use in the relocation.
1974
  bool have_got_offset = false;
1975
  unsigned int got_offset = 0;
1976
  switch (r_type)
1977
    {
1978
    case elfcpp::R_ARM_GOT_BREL:
1979
    case elfcpp::R_ARM_GOT_PREL:
1980
      if (gsym != NULL)
1981
        {
1982
          gold_assert(gsym->has_got_offset(GOT_TYPE_STANDARD));
1983
          got_offset = (gsym->got_offset(GOT_TYPE_STANDARD)
1984
                        - target->got_size());
1985
        }
1986
      else
1987
        {
1988
          unsigned int r_sym = elfcpp::elf_r_sym<32>(rel.get_r_info());
1989
          gold_assert(object->local_has_got_offset(r_sym, GOT_TYPE_STANDARD));
1990
          got_offset = (object->local_got_offset(r_sym, GOT_TYPE_STANDARD)
1991
                        - target->got_size());
1992
        }
1993
      have_got_offset = true;
1994
      break;
1995
 
1996
    default:
1997
      break;
1998
    }
1999
 
2000
  typename Arm_relocate_functions::Status reloc_status =
2001
        Arm_relocate_functions::STATUS_OKAY;
2002
  switch (r_type)
2003
    {
2004
    case elfcpp::R_ARM_NONE:
2005
      break;
2006
 
2007
    case elfcpp::R_ARM_ABS8:
2008
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2009
                                    output_section))
2010
        reloc_status = Arm_relocate_functions::abs8(view, object, psymval);
2011
      break;
2012
 
2013
    case elfcpp::R_ARM_ABS12:
2014
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2015
                                    output_section))
2016
        reloc_status = Arm_relocate_functions::abs12(view, object, psymval);
2017
      break;
2018
 
2019
    case elfcpp::R_ARM_ABS16:
2020
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2021
                                    output_section))
2022
        reloc_status = Arm_relocate_functions::abs16(view, object, psymval);
2023
      break;
2024
 
2025
    case elfcpp::R_ARM_ABS32:
2026
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2027
                                    output_section))
2028
        reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2029
                                                     has_thumb_bit);
2030
      break;
2031
 
2032
    case elfcpp::R_ARM_ABS32_NOI:
2033
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2034
                                    output_section))
2035
        // No thumb bit for this relocation: (S + A)
2036
        reloc_status = Arm_relocate_functions::abs32(view, object, psymval,
2037
                                                     false);
2038
      break;
2039
 
2040
    case elfcpp::R_ARM_MOVW_ABS_NC:
2041
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2042
                                    output_section))
2043
        reloc_status = Arm_relocate_functions::movw_abs_nc(view, object,
2044
                                                           psymval,
2045
                                                           has_thumb_bit);
2046
      else
2047
        gold_error(_("relocation R_ARM_MOVW_ABS_NC cannot be used when making"
2048
                     "a shared object; recompile with -fPIC"));
2049
      break;
2050
 
2051
    case elfcpp::R_ARM_MOVT_ABS:
2052
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2053
                                    output_section))
2054
        reloc_status = Arm_relocate_functions::movt_abs(view, object, psymval);
2055
      else
2056
        gold_error(_("relocation R_ARM_MOVT_ABS cannot be used when making"
2057
                     "a shared object; recompile with -fPIC"));
2058
      break;
2059
 
2060
    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2061
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2062
                                    output_section))
2063
        reloc_status = Arm_relocate_functions::thm_movw_abs_nc(view, object,
2064
                                                               psymval,
2065
                                                               has_thumb_bit);
2066
      else
2067
        gold_error(_("relocation R_ARM_THM_MOVW_ABS_NC cannot be used when"
2068
                     "making a shared object; recompile with -fPIC"));
2069
      break;
2070
 
2071
    case elfcpp::R_ARM_THM_MOVT_ABS:
2072
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2073
                                    output_section))
2074
        reloc_status = Arm_relocate_functions::thm_movt_abs(view, object,
2075
                                                            psymval);
2076
      else
2077
        gold_error(_("relocation R_ARM_THM_MOVT_ABS cannot be used when"
2078
                     "making a shared object; recompile with -fPIC"));
2079
      break;
2080
 
2081
    case elfcpp::R_ARM_MOVW_PREL_NC:
2082
      reloc_status = Arm_relocate_functions::movw_prel_nc(view, object,
2083
                                                          psymval, address,
2084
                                                          has_thumb_bit);
2085
      break;
2086
 
2087
    case elfcpp::R_ARM_MOVT_PREL:
2088
      reloc_status = Arm_relocate_functions::movt_prel(view, object,
2089
                                                       psymval, address);
2090
      break;
2091
 
2092
    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2093
      reloc_status = Arm_relocate_functions::thm_movw_prel_nc(view, object,
2094
                                                              psymval, address,
2095
                                                              has_thumb_bit);
2096
      break;
2097
 
2098
    case elfcpp::R_ARM_THM_MOVT_PREL:
2099
      reloc_status = Arm_relocate_functions::thm_movt_prel(view, object,
2100
                                                           psymval, address);
2101
      break;
2102
 
2103
    case elfcpp::R_ARM_REL32:
2104
      reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2105
                                                   address, has_thumb_bit);
2106
      break;
2107
 
2108
    case elfcpp::R_ARM_THM_ABS5:
2109
      if (should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, false,
2110
                                    output_section))
2111
        reloc_status = Arm_relocate_functions::thm_abs5(view, object, psymval);
2112
      break;
2113
 
2114
    case elfcpp::R_ARM_THM_CALL:
2115
      reloc_status = Arm_relocate_functions::thm_call(view, object, psymval,
2116
                                                      address, has_thumb_bit);
2117
      break;
2118
 
2119
    case elfcpp::R_ARM_GOTOFF32:
2120
      {
2121
        elfcpp::Elf_types<32>::Elf_Addr got_origin;
2122
        got_origin = target->got_plt_section()->address();
2123
        reloc_status = Arm_relocate_functions::rel32(view, object, psymval,
2124
                                                     got_origin, has_thumb_bit);
2125
      }
2126
      break;
2127
 
2128
    case elfcpp::R_ARM_BASE_PREL:
2129
      {
2130
        uint32_t origin;
2131
        // Get the addressing origin of the output segment defining the 
2132
        // symbol gsym (AAELF 4.6.1.2 Relocation types)
2133
        gold_assert(gsym != NULL);
2134
        if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2135
          origin = gsym->output_segment()->vaddr();
2136
        else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2137
          origin = gsym->output_data()->address();
2138
        else
2139
          {
2140
            gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2141
                                   _("cannot find origin of R_ARM_BASE_PREL"));
2142
            return true;
2143
          }
2144
        reloc_status = Arm_relocate_functions::base_prel(view, origin, address);
2145
      }
2146
      break;
2147
 
2148
    case elfcpp::R_ARM_BASE_ABS:
2149
      {
2150
        if (!should_apply_static_reloc(gsym, Symbol::ABSOLUTE_REF, true,
2151
                                      output_section))
2152
          break;
2153
 
2154
        uint32_t origin;
2155
        // Get the addressing origin of the output segment defining
2156
        // the symbol gsym (AAELF 4.6.1.2 Relocation types).
2157
        if (gsym == NULL)
2158
          // R_ARM_BASE_ABS with the NULL symbol will give the
2159
          // absolute address of the GOT origin (GOT_ORG) (see ARM IHI
2160
          // 0044C (AAELF): 4.6.1.8 Proxy generating relocations).
2161
          origin = target->got_plt_section()->address();
2162
        else if (gsym->source() == Symbol::IN_OUTPUT_SEGMENT)
2163
          origin = gsym->output_segment()->vaddr();
2164
        else if (gsym->source () == Symbol::IN_OUTPUT_DATA)
2165
          origin = gsym->output_data()->address();
2166
        else
2167
          {
2168
            gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2169
                                   _("cannot find origin of R_ARM_BASE_ABS"));
2170
            return true;
2171
          }
2172
 
2173
        reloc_status = Arm_relocate_functions::base_abs(view, origin);
2174
      }
2175
      break;
2176
 
2177
    case elfcpp::R_ARM_GOT_BREL:
2178
      gold_assert(have_got_offset);
2179
      reloc_status = Arm_relocate_functions::got_brel(view, got_offset);
2180
      break;
2181
 
2182
    case elfcpp::R_ARM_GOT_PREL:
2183
      gold_assert(have_got_offset);
2184
      // Get the address origin for GOT PLT, which is allocated right
2185
      // after the GOT section, to calculate an absolute address of
2186
      // the symbol GOT entry (got_origin + got_offset).
2187
      elfcpp::Elf_types<32>::Elf_Addr got_origin;
2188
      got_origin = target->got_plt_section()->address();
2189
      reloc_status = Arm_relocate_functions::got_prel(view,
2190
                                                      got_origin + got_offset,
2191
                                                      address);
2192
      break;
2193
 
2194
    case elfcpp::R_ARM_PLT32:
2195
      gold_assert(gsym == NULL
2196
                  || gsym->has_plt_offset()
2197
                  || gsym->final_value_is_known()
2198
                  || (gsym->is_defined()
2199
                      && !gsym->is_from_dynobj()
2200
                      && !gsym->is_preemptible()));
2201
      reloc_status = Arm_relocate_functions::plt32(view, object, psymval,
2202
                                                   address, has_thumb_bit);
2203
      break;
2204
 
2205
    case elfcpp::R_ARM_CALL:
2206
      reloc_status = Arm_relocate_functions::call(view, object, psymval,
2207
                                                  address, has_thumb_bit);
2208
      break;
2209
 
2210
    case elfcpp::R_ARM_JUMP24:
2211
      reloc_status = Arm_relocate_functions::jump24(view, object, psymval,
2212
                                                    address, has_thumb_bit);
2213
      break;
2214
 
2215
    case elfcpp::R_ARM_PREL31:
2216
      reloc_status = Arm_relocate_functions::prel31(view, object, psymval,
2217
                                                    address, has_thumb_bit);
2218
      break;
2219
 
2220
    case elfcpp::R_ARM_TARGET1:
2221
      // This should have been mapped to another type already.
2222
      // Fall through.
2223
    case elfcpp::R_ARM_COPY:
2224
    case elfcpp::R_ARM_GLOB_DAT:
2225
    case elfcpp::R_ARM_JUMP_SLOT:
2226
    case elfcpp::R_ARM_RELATIVE:
2227
      // These are relocations which should only be seen by the
2228
      // dynamic linker, and should never be seen here.
2229
      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2230
                             _("unexpected reloc %u in object file"),
2231
                             r_type);
2232
      break;
2233
 
2234
    default:
2235
      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2236
                             _("unsupported reloc %u"),
2237
                             r_type);
2238
      break;
2239
    }
2240
 
2241
  // Report any errors.
2242
  switch (reloc_status)
2243
    {
2244
    case Arm_relocate_functions::STATUS_OKAY:
2245
      break;
2246
    case Arm_relocate_functions::STATUS_OVERFLOW:
2247
      gold_error_at_location(relinfo, relnum, rel.get_r_offset(),
2248
                             _("relocation overflow in relocation %u"),
2249
                             r_type);
2250
      break;
2251
    case Arm_relocate_functions::STATUS_BAD_RELOC:
2252
      gold_error_at_location(
2253
        relinfo,
2254
        relnum,
2255
        rel.get_r_offset(),
2256
        _("unexpected opcode while processing relocation %u"),
2257
        r_type);
2258
      break;
2259
    default:
2260
      gold_unreachable();
2261
    }
2262
 
2263
  return true;
2264
}
2265
 
2266
// Relocate section data.
2267
 
2268
template<bool big_endian>
2269
void
2270
Target_arm<big_endian>::relocate_section(
2271
    const Relocate_info<32, big_endian>* relinfo,
2272
    unsigned int sh_type,
2273
    const unsigned char* prelocs,
2274
    size_t reloc_count,
2275
    Output_section* output_section,
2276
    bool needs_special_offset_handling,
2277
    unsigned char* view,
2278
    elfcpp::Elf_types<32>::Elf_Addr address,
2279
    section_size_type view_size,
2280
    const Reloc_symbol_changes* reloc_symbol_changes)
2281
{
2282
  typedef typename Target_arm<big_endian>::Relocate Arm_relocate;
2283
  gold_assert(sh_type == elfcpp::SHT_REL);
2284
 
2285
  gold::relocate_section<32, big_endian, Target_arm, elfcpp::SHT_REL,
2286
                         Arm_relocate>(
2287
    relinfo,
2288
    this,
2289
    prelocs,
2290
    reloc_count,
2291
    output_section,
2292
    needs_special_offset_handling,
2293
    view,
2294
    address,
2295
    view_size,
2296
    reloc_symbol_changes);
2297
}
2298
 
2299
// Return the size of a relocation while scanning during a relocatable
2300
// link.
2301
 
2302
template<bool big_endian>
2303
unsigned int
2304
Target_arm<big_endian>::Relocatable_size_for_reloc::get_size_for_reloc(
2305
    unsigned int r_type,
2306
    Relobj* object)
2307
{
2308
  r_type = get_real_reloc_type(r_type);
2309
  switch (r_type)
2310
    {
2311
    case elfcpp::R_ARM_NONE:
2312
      return 0;
2313
 
2314
    case elfcpp::R_ARM_ABS8:
2315
      return 1;
2316
 
2317
    case elfcpp::R_ARM_ABS16:
2318
    case elfcpp::R_ARM_THM_ABS5:
2319
      return 2;
2320
 
2321
    case elfcpp::R_ARM_ABS32:
2322
    case elfcpp::R_ARM_ABS32_NOI:
2323
    case elfcpp::R_ARM_ABS12:
2324
    case elfcpp::R_ARM_BASE_ABS:
2325
    case elfcpp::R_ARM_REL32:
2326
    case elfcpp::R_ARM_THM_CALL:
2327
    case elfcpp::R_ARM_GOTOFF32:
2328
    case elfcpp::R_ARM_BASE_PREL:
2329
    case elfcpp::R_ARM_GOT_BREL:
2330
    case elfcpp::R_ARM_GOT_PREL:
2331
    case elfcpp::R_ARM_PLT32:
2332
    case elfcpp::R_ARM_CALL:
2333
    case elfcpp::R_ARM_JUMP24:
2334
    case elfcpp::R_ARM_PREL31:
2335
    case elfcpp::R_ARM_MOVW_ABS_NC:
2336
    case elfcpp::R_ARM_MOVT_ABS:
2337
    case elfcpp::R_ARM_THM_MOVW_ABS_NC:
2338
    case elfcpp::R_ARM_THM_MOVT_ABS:
2339
    case elfcpp::R_ARM_MOVW_PREL_NC:
2340
    case elfcpp::R_ARM_MOVT_PREL:
2341
    case elfcpp::R_ARM_THM_MOVW_PREL_NC:
2342
    case elfcpp::R_ARM_THM_MOVT_PREL:
2343
      return 4;
2344
 
2345
    case elfcpp::R_ARM_TARGET1:
2346
      // This should have been mapped to another type already.
2347
      // Fall through.
2348
    case elfcpp::R_ARM_COPY:
2349
    case elfcpp::R_ARM_GLOB_DAT:
2350
    case elfcpp::R_ARM_JUMP_SLOT:
2351
    case elfcpp::R_ARM_RELATIVE:
2352
      // These are relocations which should only be seen by the
2353
      // dynamic linker, and should never be seen here.
2354
      gold_error(_("%s: unexpected reloc %u in object file"),
2355
                 object->name().c_str(), r_type);
2356
      return 0;
2357
 
2358
    default:
2359
      object->error(_("unsupported reloc %u in object file"), r_type);
2360
      return 0;
2361
    }
2362
}
2363
 
2364
// Scan the relocs during a relocatable link.
2365
 
2366
template<bool big_endian>
2367
void
2368
Target_arm<big_endian>::scan_relocatable_relocs(
2369
    const General_options& options,
2370
    Symbol_table* symtab,
2371
    Layout* layout,
2372
    Sized_relobj<32, big_endian>* object,
2373
    unsigned int data_shndx,
2374
    unsigned int sh_type,
2375
    const unsigned char* prelocs,
2376
    size_t reloc_count,
2377
    Output_section* output_section,
2378
    bool needs_special_offset_handling,
2379
    size_t local_symbol_count,
2380
    const unsigned char* plocal_symbols,
2381
    Relocatable_relocs* rr)
2382
{
2383
  gold_assert(sh_type == elfcpp::SHT_REL);
2384
 
2385
  typedef gold::Default_scan_relocatable_relocs<elfcpp::SHT_REL,
2386
    Relocatable_size_for_reloc> Scan_relocatable_relocs;
2387
 
2388
  gold::scan_relocatable_relocs<32, big_endian, elfcpp::SHT_REL,
2389
      Scan_relocatable_relocs>(
2390
    options,
2391
    symtab,
2392
    layout,
2393
    object,
2394
    data_shndx,
2395
    prelocs,
2396
    reloc_count,
2397
    output_section,
2398
    needs_special_offset_handling,
2399
    local_symbol_count,
2400
    plocal_symbols,
2401
    rr);
2402
}
2403
 
2404
// Relocate a section during a relocatable link.
2405
 
2406
template<bool big_endian>
2407
void
2408
Target_arm<big_endian>::relocate_for_relocatable(
2409
    const Relocate_info<32, big_endian>* relinfo,
2410
    unsigned int sh_type,
2411
    const unsigned char* prelocs,
2412
    size_t reloc_count,
2413
    Output_section* output_section,
2414
    off_t offset_in_output_section,
2415
    const Relocatable_relocs* rr,
2416
    unsigned char* view,
2417
    elfcpp::Elf_types<32>::Elf_Addr view_address,
2418
    section_size_type view_size,
2419
    unsigned char* reloc_view,
2420
    section_size_type reloc_view_size)
2421
{
2422
  gold_assert(sh_type == elfcpp::SHT_REL);
2423
 
2424
  gold::relocate_for_relocatable<32, big_endian, elfcpp::SHT_REL>(
2425
    relinfo,
2426
    prelocs,
2427
    reloc_count,
2428
    output_section,
2429
    offset_in_output_section,
2430
    rr,
2431
    view,
2432
    view_address,
2433
    view_size,
2434
    reloc_view,
2435
    reloc_view_size);
2436
}
2437
 
2438
// Return the value to use for a dynamic symbol which requires special
2439
// treatment.  This is how we support equality comparisons of function
2440
// pointers across shared library boundaries, as described in the
2441
// processor specific ABI supplement.
2442
 
2443
template<bool big_endian>
2444
uint64_t
2445
Target_arm<big_endian>::do_dynsym_value(const Symbol* gsym) const
2446
{
2447
  gold_assert(gsym->is_from_dynobj() && gsym->has_plt_offset());
2448
  return this->plt_section()->address() + gsym->plt_offset();
2449
}
2450
 
2451
// Map platform-specific relocs to real relocs
2452
//
2453
template<bool big_endian>
2454
unsigned int
2455
Target_arm<big_endian>::get_real_reloc_type (unsigned int r_type)
2456
{
2457
  switch (r_type)
2458
    {
2459
    case elfcpp::R_ARM_TARGET1:
2460
      // This is either R_ARM_ABS32 or R_ARM_REL32;
2461
      return elfcpp::R_ARM_ABS32;
2462
 
2463
    case elfcpp::R_ARM_TARGET2:
2464
      // This can be any reloc type but ususally is R_ARM_GOT_PREL
2465
      return elfcpp::R_ARM_GOT_PREL;
2466
 
2467
    default:
2468
      return r_type;
2469
    }
2470
}
2471
 
2472
// The selector for arm object files.
2473
 
2474
template<bool big_endian>
2475
class Target_selector_arm : public Target_selector
2476
{
2477
 public:
2478
  Target_selector_arm()
2479
    : Target_selector(elfcpp::EM_ARM, 32, big_endian,
2480
                      (big_endian ? "elf32-bigarm" : "elf32-littlearm"))
2481
  { }
2482
 
2483
  Target*
2484
  do_instantiate_target()
2485
  { return new Target_arm<big_endian>(); }
2486
};
2487
 
2488
Target_selector_arm<false> target_selector_arm;
2489
Target_selector_arm<true> target_selector_armbe;
2490
 
2491
} // End anonymous namespace.

powered by: WebSVN 2.1.0

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