arm-reloc-property.cc 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. // arm-reloc-property.cc -- ARM relocation property.
  2. // Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. // Written by Doug Kwan <dougkwan@google.com>.
  4. // This file is part of gold.
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 3 of the License, or
  8. // (at your option) any later version.
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License
  14. // along with this program; if not, write to the Free Software
  15. // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. // MA 02110-1301, USA.
  17. #include "gold.h"
  18. #include <cstdio>
  19. #include <cstring>
  20. #include <stack>
  21. #include <string>
  22. #include <vector>
  23. #include "elfcpp.h"
  24. #include "arm.h"
  25. #include "arm-reloc-property.h"
  26. namespace gold
  27. {
  28. // Arm_reloc_property::Tree_node methods.
  29. // Parse an S-expression S and build a tree and return the root node.
  30. // Caller is responsible for releasing tree after use.
  31. Arm_reloc_property::Tree_node*
  32. Arm_reloc_property::Tree_node::make_tree(const std::string& s)
  33. {
  34. std::stack<size_t> size_stack;
  35. Tree_node_vector node_stack;
  36. // strtok needs a non-const string pointer.
  37. char* buffer = new char[s.size() + 1];
  38. memcpy(buffer, s.data(), s.size());
  39. buffer[s.size()] = '\0';
  40. char* token = strtok(buffer, " ");
  41. while (token != NULL)
  42. {
  43. if (strcmp(token, "(") == 0)
  44. // Remember the node stack position for start of a new internal node.
  45. size_stack.push(node_stack.size());
  46. else if (strcmp(token, ")") == 0)
  47. {
  48. // Pop all tree nodes after the previous '(' and use them as
  49. // children to build a new internal node. Push internal node back.
  50. size_t current_size = node_stack.size();
  51. size_t prev_size = size_stack.top();
  52. size_stack.pop();
  53. Tree_node* node =
  54. new Tree_node(node_stack.begin() + prev_size,
  55. node_stack.begin() + current_size);
  56. node_stack.resize(prev_size);
  57. node_stack.push_back(node);
  58. }
  59. else
  60. // Just push a leaf node to node_stack.
  61. node_stack.push_back(new Tree_node(token));
  62. token = strtok(NULL, " ");
  63. }
  64. delete[] buffer;
  65. // At this point, size_stack should be empty and node_stack should only
  66. // contain the root node.
  67. gold_assert(size_stack.empty() && node_stack.size() == 1);
  68. return node_stack[0];
  69. }
  70. // Arm_reloc_property methods.
  71. // Constructor.
  72. Arm_reloc_property::Arm_reloc_property(
  73. unsigned int code,
  74. const char* name,
  75. Reloc_type rtype,
  76. bool is_deprecated,
  77. Reloc_class rclass,
  78. const std::string& operation,
  79. bool is_implemented,
  80. int group_index,
  81. bool checks_overflow)
  82. : code_(code), name_(name), reloc_type_(rtype), reloc_class_(rclass),
  83. group_index_(group_index), size_(0), align_(1),
  84. relative_address_base_(RAB_NONE), is_deprecated_(is_deprecated),
  85. is_implemented_(is_implemented), checks_overflow_(checks_overflow),
  86. uses_got_entry_(false), uses_got_origin_(false), uses_plt_entry_(false),
  87. uses_thumb_bit_(false), uses_symbol_base_(false), uses_addend_(false),
  88. uses_symbol_(false)
  89. {
  90. // Set size and alignment of static and dynamic relocations.
  91. if (rtype == RT_STATIC)
  92. {
  93. switch (rclass)
  94. {
  95. case RC_DATA:
  96. // Except for R_ARM_ABS16 and R_ARM_ABS8, all static data relocations
  97. // have size 4. All static data relocations have alignment of 1.
  98. if (code == elfcpp::R_ARM_ABS8)
  99. this->size_ = 1;
  100. else if (code == elfcpp::R_ARM_ABS16)
  101. this->size_ = 2;
  102. else
  103. this->size_ = 4;
  104. this->align_ = 1;
  105. break;
  106. case RC_MISC:
  107. // R_ARM_V4BX should be treated as an ARM relocation. For all
  108. // others, just use defaults.
  109. if (code != elfcpp::R_ARM_V4BX)
  110. break;
  111. // Fall through.
  112. case RC_ARM:
  113. this->size_ = 4;
  114. this->align_ = 4;
  115. break;
  116. case RC_THM16:
  117. this->size_ = 2;
  118. this->align_ = 2;
  119. break;
  120. case RC_THM32:
  121. this->size_ = 4;
  122. this->align_ = 2;
  123. break;
  124. default:
  125. gold_unreachable();
  126. }
  127. }
  128. else if (rtype == RT_DYNAMIC)
  129. {
  130. // With the exception of R_ARM_COPY, all dynamic relocations requires
  131. // that the place being relocated is a word-aligned 32-bit object.
  132. if (code != elfcpp::R_ARM_COPY)
  133. {
  134. this->size_ = 4;
  135. this->align_ = 4;
  136. }
  137. }
  138. // If no relocation operation is specified, we are done.
  139. if (operation == "NONE")
  140. return;
  141. // Extract information from relocation operation.
  142. Tree_node* root_node = Tree_node::make_tree(operation);
  143. Tree_node* node = root_node;
  144. // Check for an expression of the form XXX - YYY.
  145. if (!node->is_leaf()
  146. && node->child(0)->is_leaf()
  147. && node->child(0)->name() == "-")
  148. {
  149. struct RAB_table_entry
  150. {
  151. Relative_address_base rab;
  152. const char* name;
  153. };
  154. static const RAB_table_entry rab_table[] =
  155. {
  156. { RAB_B_S, "( B S )" },
  157. { RAB_DELTA_B_S, "( DELTA_B ( S ) )" },
  158. { RAB_GOT_ORG, "GOT_ORG" },
  159. { RAB_P, "P" },
  160. { RAB_Pa, "Pa" },
  161. { RAB_TLS, "TLS" },
  162. { RAB_tp, "tp" }
  163. };
  164. static size_t rab_table_size = sizeof(rab_table) / sizeof(rab_table[0]);
  165. const std::string rhs(node->child(2)->s_expression());
  166. for (size_t i = 0; i < rab_table_size; ++i)
  167. if (rhs == rab_table[i].name)
  168. {
  169. this->relative_address_base_ = rab_table[i].rab;
  170. break;
  171. }
  172. gold_assert(this->relative_address_base_ != RAB_NONE);
  173. if (this->relative_address_base_ == RAB_B_S)
  174. this->uses_symbol_base_ = true;
  175. node = node->child(1);
  176. }
  177. // Check for an expression of the form XXX | T.
  178. if (!node->is_leaf()
  179. && node->child(0)->is_leaf()
  180. && node->child(0)->name() == "|")
  181. {
  182. gold_assert(node->number_of_children() == 3
  183. && node->child(2)->is_leaf()
  184. && node->child(2)->name() == "T");
  185. this->uses_thumb_bit_ = true;
  186. node = node->child(1);
  187. }
  188. // Check for an expression of the form XXX + A.
  189. if (!node->is_leaf()
  190. && node->child(0)->is_leaf()
  191. && node->child(0)->name() == "+")
  192. {
  193. gold_assert(node->number_of_children() == 3
  194. && node->child(2)->is_leaf()
  195. && node->child(2)->name() == "A");
  196. this->uses_addend_ = true;
  197. node = node->child(1);
  198. }
  199. // Check for an expression of the form XXX(S).
  200. if (!node->is_leaf() && node->child(0)->is_leaf())
  201. {
  202. gold_assert(node->number_of_children() == 2
  203. && node->child(1)->is_leaf()
  204. && node->child(1)->name() == "S");
  205. const std::string func(node->child(0)->name());
  206. if (func == "B")
  207. this->uses_symbol_base_ = true;
  208. else if (func == "GOT")
  209. this->uses_got_entry_ = true;
  210. else if (func == "PLT")
  211. this->uses_plt_entry_ = true;
  212. else if (func == "Module" || func == "DELTA_B")
  213. // These are used in dynamic relocations.
  214. ;
  215. else
  216. gold_unreachable();
  217. node = node->child(1);
  218. }
  219. gold_assert(node->is_leaf() && node->name() == "S");
  220. this->uses_symbol_ = true;
  221. delete root_node;
  222. }
  223. // Arm_reloc_property_table methods.
  224. // Constructor. This processing informations in arm-reloc.def to
  225. // initialize the table.
  226. Arm_reloc_property_table::Arm_reloc_property_table()
  227. {
  228. // These appear in arm-reloc.def. Do not rename them.
  229. Parse_expression A("A"), GOT_ORG("GOT_ORG"), NONE("NONE"), P("P"),
  230. Pa("Pa"), S("S"), T("T"), TLS("TLS"), tp("tp");
  231. const bool Y(true), N(false);
  232. for (unsigned int i = 0; i < Property_table_size; ++i)
  233. this->table_[i] = NULL;
  234. #undef RD
  235. #define RD(name, type, deprecated, class, operation, is_implemented, \
  236. group_index, checks_oveflow) \
  237. do \
  238. { \
  239. unsigned int code = elfcpp::R_ARM_##name; \
  240. gold_assert(code < Property_table_size); \
  241. this->table_[code] = \
  242. new Arm_reloc_property(elfcpp::R_ARM_##name, "R_ARM_" #name, \
  243. Arm_reloc_property::RT_##type, deprecated, \
  244. Arm_reloc_property::RC_##class, \
  245. (operation).s_expression(), is_implemented, \
  246. group_index, checks_oveflow); \
  247. } \
  248. while(0);
  249. #include "arm-reloc.def"
  250. #undef RD
  251. }
  252. // Return a string describing a relocation code that fails to get a
  253. // relocation property in get_implemented_static_reloc_property().
  254. std::string
  255. Arm_reloc_property_table::reloc_name_in_error_message(unsigned int code)
  256. {
  257. gold_assert(code < Property_table_size);
  258. const Arm_reloc_property* arp = this->table_[code];
  259. if (arp == NULL)
  260. {
  261. char buffer[100];
  262. sprintf(buffer, _("invalid reloc %u"), code);
  263. return std::string(buffer);
  264. }
  265. // gold only implements static relocation codes.
  266. Arm_reloc_property::Reloc_type reloc_type = arp->reloc_type();
  267. gold_assert(reloc_type == Arm_reloc_property::RT_STATIC
  268. || !arp->is_implemented());
  269. const char* prefix = NULL;
  270. switch (reloc_type)
  271. {
  272. case Arm_reloc_property::RT_STATIC:
  273. prefix = arp->is_implemented() ? _("reloc ") : _("unimplemented reloc ");
  274. break;
  275. case Arm_reloc_property::RT_DYNAMIC:
  276. prefix = _("dynamic reloc ");
  277. break;
  278. case Arm_reloc_property::RT_PRIVATE:
  279. prefix = _("private reloc ");
  280. break;
  281. case Arm_reloc_property::RT_OBSOLETE:
  282. prefix = _("obsolete reloc ");
  283. break;
  284. default:
  285. gold_unreachable();
  286. }
  287. return std::string(prefix) + arp->name();
  288. }
  289. } // End namespace gold.