summaryrefslogtreecommitdiffstats
path: root/binutils-2.25/gold/arm-reloc-property.cc
blob: dfc2fe11198bebe2b955695c2c36aa9f550f8d56 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// arm-reloc-property.cc -- ARM relocation property.

// Copyright (C) 2010-2014 Free Software Foundation, Inc.
// Written by Doug Kwan <dougkwan@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 <cstdio>
#include <cstring>
#include <stack>
#include <string>
#include <vector>

#include "elfcpp.h"
#include "arm.h"
#include "arm-reloc-property.h"

namespace gold
{

// Arm_reloc_property::Tree_node methods.

// Parse an S-expression S and build a tree and return the root node.
// Caller is responsible for releasing tree after use.

Arm_reloc_property::Tree_node*
Arm_reloc_property::Tree_node::make_tree(const std::string& s)
{
  std::stack<size_t> size_stack;
  Tree_node_vector node_stack;

  // strtok needs a non-const string pointer.
  char* buffer = new char[s.size() + 1];
  memcpy(buffer, s.data(), s.size());
  buffer[s.size()] = '\0';
  char* token = strtok(buffer, " ");

  while (token != NULL)
    {
      if (strcmp(token, "(") == 0)
	// Remember the node stack position for start of a new internal node.
	size_stack.push(node_stack.size());
      else if (strcmp(token, ")") == 0)
	{
	  // Pop all tree nodes after the previous '(' and use them as
	  // children to build a new internal node.  Push internal node back.
	  size_t current_size = node_stack.size();
	  size_t prev_size = size_stack.top();
	  size_stack.pop();
	  Tree_node* node =
	    new Tree_node(node_stack.begin() + prev_size,
			  node_stack.begin() + current_size); 
	  node_stack.resize(prev_size);
	  node_stack.push_back(node);
	}
      else
	// Just push a leaf node to node_stack.
	node_stack.push_back(new Tree_node(token));

      token = strtok(NULL, " ");
    }

  delete[] buffer;

  // At this point, size_stack should be empty and node_stack should only
  // contain the root node.
  gold_assert(size_stack.empty() && node_stack.size() == 1);
  return node_stack[0];
}

// Arm_reloc_property methods.

// Constructor.

Arm_reloc_property::Arm_reloc_property(
    unsigned int code,
    const char* name,
    Reloc_type rtype,
    bool is_deprecated,
    Reloc_class rclass,
    const std::string& operation,
    bool is_implemented,
    int group_index,
    bool checks_overflow)
  : code_(code), name_(name), reloc_type_(rtype), reloc_class_(rclass),
    group_index_(group_index), size_(0), align_(1),
    relative_address_base_(RAB_NONE), is_deprecated_(is_deprecated),
    is_implemented_(is_implemented), checks_overflow_(checks_overflow),
    uses_got_entry_(false), uses_got_origin_(false), uses_plt_entry_(false),
    uses_thumb_bit_(false), uses_symbol_base_(false), uses_addend_(false),
    uses_symbol_(false)
{
  // Set size and alignment of static and dynamic relocations.
  if (rtype == RT_STATIC)
    {
      switch (rclass)
	{
	case RC_DATA:
	  // Except for R_ARM_ABS16 and R_ARM_ABS8, all static data relocations
	  // have size 4.  All static data relocations have alignment of 1.
	  if (code == elfcpp::R_ARM_ABS8)
	    this->size_ = 1;
	  else if (code == elfcpp::R_ARM_ABS16)
	    this->size_ = 2;
	  else
	    this->size_ = 4;
	  this->align_ = 1;
	  break;
	case RC_MISC:
	  // R_ARM_V4BX should be treated as an ARM relocation.  For all
	  // others, just use defaults.
	  if (code != elfcpp::R_ARM_V4BX)
	    break;
	  // Fall through.
	case RC_ARM:
	  this->size_ = 4;
	  this->align_ = 4;
	  break;
	case RC_THM16:
	  this->size_ = 2;
	  this->align_ = 2;
	  break;
	case RC_THM32:
	  this->size_ = 4;
	  this->align_ = 2;
	  break;
	default:
	  gold_unreachable();
	}
    }
  else if (rtype == RT_DYNAMIC)
    {
      // With the exception of R_ARM_COPY, all dynamic relocations requires
      // that the place being relocated is a word-aligned 32-bit object.
      if (code != elfcpp::R_ARM_COPY)
	{
	  this->size_ = 4;
	  this->align_ = 4;
	}
    }

  // If no relocation operation is specified, we are done.
  if (operation == "NONE")
    return;

  // Extract information from relocation operation.
  Tree_node* root_node = Tree_node::make_tree(operation);
  Tree_node* node = root_node;

  // Check for an expression of the form XXX - YYY.
  if (!node->is_leaf()
      && node->child(0)->is_leaf()
      && node->child(0)->name() == "-")
    {
      struct RAB_table_entry
      {
	Relative_address_base rab;
	const char* name;
      };

      static const RAB_table_entry rab_table[] =
      {
	{ RAB_B_S, "( B S )" },
	{ RAB_DELTA_B_S, "( DELTA_B ( S ) )" },
      	{ RAB_GOT_ORG, "GOT_ORG" },
      	{ RAB_P, "P" },
      	{ RAB_Pa, "Pa" },
      	{ RAB_TLS, "TLS" },
      	{ RAB_tp, "tp" }
      };

      static size_t rab_table_size = sizeof(rab_table) / sizeof(rab_table[0]);
      const std::string rhs(node->child(2)->s_expression());
      for (size_t i = 0; i < rab_table_size; ++i)
	if (rhs == rab_table[i].name)
	  {
	    this->relative_address_base_ = rab_table[i].rab;
	    break;
	  }

      gold_assert(this->relative_address_base_ != RAB_NONE);
      if (this->relative_address_base_ == RAB_B_S)
	this->uses_symbol_base_ = true;
      node = node->child(1);
    }

  // Check for an expression of the form XXX | T.
  if (!node->is_leaf()
      && node->child(0)->is_leaf()
      && node->child(0)->name() == "|")
    {
      gold_assert(node->number_of_children() == 3
		  && node->child(2)->is_leaf()
		  && node->child(2)->name() == "T");
      this->uses_thumb_bit_ = true;
      node = node->child(1);
    }

  // Check for an expression of the form XXX + A.
  if (!node->is_leaf()
      && node->child(0)->is_leaf()
      && node->child(0)->name() == "+")
    {
      gold_assert(node->number_of_children() == 3
		  && node->child(2)->is_leaf()
		  && node->child(2)->name() == "A");
      this->uses_addend_ = true;
      node = node->child(1);
    }

  // Check for an expression of the form XXX(S).
  if (!node->is_leaf() && node->child(0)->is_leaf())
    {
      gold_assert(node->number_of_children() == 2
		  && node->child(1)->is_leaf()
		  && node->child(1)->name() == "S");
      const std::string func(node->child(0)->name());
      if (func == "B")
	this->uses_symbol_base_ = true;
      else if (func == "GOT")
	this->uses_got_entry_ = true;
      else if (func == "PLT")
	this->uses_plt_entry_ = true;
      else if (func == "Module" || func == "DELTA_B")
	// These are used in dynamic relocations.
	;
      else
	gold_unreachable();
      node = node->child(1);
    }

  gold_assert(node->is_leaf() && node->name() == "S");
  this->uses_symbol_ = true;

  delete root_node;
}

// Arm_reloc_property_table methods.

// Constructor.  This processing informations in arm-reloc.def to
// initialize the table.

Arm_reloc_property_table::Arm_reloc_property_table()
{
  // These appear in arm-reloc.def.  Do not rename them.
  Parse_expression A("A"), GOT_ORG("GOT_ORG"), NONE("NONE"), P("P"),
		   Pa("Pa"), S("S"), T("T"), TLS("TLS"), tp("tp");
  const bool Y(true), N(false);

  for (unsigned int i = 0; i < Property_table_size; ++i)
    this->table_[i] = NULL;

#undef RD
#define RD(name, type, deprecated, class, operation, is_implemented, \
	   group_index, checks_oveflow) \
  do \
    { \
      unsigned int code = elfcpp::R_ARM_##name; \
      gold_assert(code < Property_table_size); \
      this->table_[code] = \
	new Arm_reloc_property(elfcpp::R_ARM_##name, "R_ARM_" #name, \
			       Arm_reloc_property::RT_##type, deprecated, \
			       Arm_reloc_property::RC_##class, \
			       (operation).s_expression(), is_implemented, \
			       group_index, checks_oveflow); \
    } \
  while(0);

#include "arm-reloc.def"
#undef RD
}

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

std::string
Arm_reloc_property_table::reloc_name_in_error_message(unsigned int code)
{
  gold_assert(code < Property_table_size);

  const Arm_reloc_property* arp = this->table_[code];

  if (arp == NULL)
    {
      char buffer[100];
      sprintf(buffer, _("invalid reloc %u"), code);
      return std::string(buffer);
    }
  
  // gold only implements static relocation codes.
  Arm_reloc_property::Reloc_type reloc_type = arp->reloc_type();
  gold_assert(reloc_type == Arm_reloc_property::RT_STATIC
	      || !arp->is_implemented());

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

} // End namespace gold.