summaryrefslogtreecommitdiff
path: root/gold/aarch64-reloc-property.cc
blob: 0bda832bf886e31ac225cc12da079a18eef39f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// aarch64-reloc-property.cc -- AArch64 relocation properties   -*- C++ -*-

// Copyright (C) 2014-2018 Free Software Foundation, Inc.
// Written by Han Shen <shenhan@google.com> and Jing Yu <jingyu@google.com>.

// This file is part of gold.

// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
// MA 02110-1301, USA.

#include "gold.h"

#include "aarch64-reloc-property.h"
#include "aarch64.h"

#include "symtab.h"

#include<stdio.h>

namespace gold
{

template<int L, int U>
bool
rvalue_checkup(int64_t x)
{
  // We save the extra_alignment_requirement bits on [31:16] of U.
  // "extra_alignment_requirement" could be 0, 1, 3, 7 and 15.
  unsigned short extra_alignment_requirement = (U & 0xFFFF0000) >> 16;
  // [15:0] of U indicates the upper bound check.
  int64_t u = U & 0x0000FFFF;
  if (u == 0)
    {
      // No requirement to check overflow.
      gold_assert(L == 0);
      return (x & extra_alignment_requirement) == 0;
    }

  // Check both overflow and alignment if needed.
  int64_t low_bound = -(L == 0 ? 0 : ((int64_t)1 << L));
  int64_t up_bound = ((int64_t)1 << u);
  return ((low_bound <= x && x < up_bound)
	  && ((x & extra_alignment_requirement) == 0));
}

template<>
bool
rvalue_checkup<0, 0>(int64_t) { return true; }

namespace
{

template<int L, int U>
class Rvalue_bit_select_impl
{
public:
  static uint64_t
  calc(uint64_t x)
  {
    return (x & ((1ULL << (U+1)) - 1)) >> L;
  }
};

template<int L>
class Rvalue_bit_select_impl<L, 63>
{
public:
  static uint64_t
  calc(uint64_t x)
  {
    return x >> L;
  }
};

// By our convention, L=U=0 means that the whole value should be retrieved.
template<>
class Rvalue_bit_select_impl<0, 0>
{
public:
  static uint64_t
  calc(uint64_t x)
  {
    return x;
  }
};

} // End anonymous namespace.

template<int L, int U>
uint64_t
rvalue_bit_select(uint64_t x)
{
  return Rvalue_bit_select_impl<L, U>::calc(x);
}

AArch64_reloc_property::AArch64_reloc_property(
    unsigned int code,
    const char* name,
    Reloc_type rtype,
    Reloc_class rclass,
    bool is_implemented,
    int group_index,
    int reference_flags,
    Reloc_inst reloc_inst,
    rvalue_checkup_func_p rvalue_checkup_func,
    rvalue_bit_select_func rvalue_bit_select)
  : code_(code), name_(name), reloc_type_(rtype), reloc_class_(rclass),
    group_index_(group_index),
    is_implemented_(is_implemented),
    reference_flags_(reference_flags),
    reloc_inst_(reloc_inst),
    rvalue_checkup_func_(rvalue_checkup_func),
    rvalue_bit_select_func_(rvalue_bit_select)
{}

AArch64_reloc_property_table::AArch64_reloc_property_table()
{
  const bool Y(true), N(false);
  for (unsigned int i = 0; i < Property_table_size; ++i)
    table_[i] = NULL;

#define RL_CHECK_ALIGN2   (1  << 16)
#define RL_CHECK_ALIGN4   (3  << 16)
#define RL_CHECK_ALIGN8   (7  << 16)
#define RL_CHECK_ALIGN16  (15 << 16)

#undef ARD
#define ARD(rname, type, class, is_implemented, group_index, LB, UB, BSL, BSH, RFLAGS, inst) \
    do \
      { \
	int tidx = code_to_array_index(elfcpp::R_AARCH64_##rname); \
	AArch64_reloc_property * p = new AArch64_reloc_property( \
	  elfcpp::R_AARCH64_##rname, "R_AARCH64_" #rname, \
	  AArch64_reloc_property::RT_##type, \
	  AArch64_reloc_property::RC_##class, \
	  is_implemented, \
	  group_index, \
	  (RFLAGS), \
	  AArch64_reloc_property::INST_##inst,	\
	  rvalue_checkup<LB,UB>,    \
	  rvalue_bit_select<BSL,BSH>);		\
	table_[tidx] = p; \
      } \
    while (0);
#include"aarch64-reloc.def"
#undef ARD
}

// Return a string describing a relocation code that fails to get a
// relocation property in get_implemented_static_reloc_property().

std::string
AArch64_reloc_property_table::reloc_name_in_error_message(unsigned int code)
{
  int tidx = code_to_array_index(code);
  const AArch64_reloc_property* arp = this->table_[tidx];

  if (arp == NULL)
    {
      char buffer[100];
      sprintf(buffer, _("invalid reloc %u"), code);
      return std::string(buffer);
    }

  // gold only implements static relocation codes.
  AArch64_reloc_property::Reloc_type reloc_type = arp->reloc_type();
  gold_assert(reloc_type == AArch64_reloc_property::RT_STATIC
	      || !arp->is_implemented());

  const char* prefix = NULL;
  switch (reloc_type)
    {
    case AArch64_reloc_property::RT_STATIC:
      prefix = arp->is_implemented() ? _("reloc ") : _("unimplemented reloc ");
      break;
    case AArch64_reloc_property::RT_DYNAMIC:
      prefix = _("dynamic reloc ");
      break;
    default:
      gold_unreachable();
    }
  return std::string(prefix) + arp->name();
}

}