aarch64-reloc-property.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // aarch64-reloc-property.cc -- AArch64 relocation properties -*- C++ -*-
  2. // Copyright (C) 2014-2015 Free Software Foundation, Inc.
  3. // Written by Han Shen <shenhan@google.com> and Jing Yu <jingyu@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 "aarch64-reloc-property.h"
  19. #include "aarch64.h"
  20. #include "symtab.h"
  21. #include<stdio.h>
  22. namespace gold
  23. {
  24. template<int L, int U>
  25. bool
  26. rvalue_checkup(int64_t x)
  27. {
  28. // We save the extra_alignment_requirement bits on [31:16] of U.
  29. // "extra_alignment_requirement" could be 0, 1, 3, 7 and 15.
  30. unsigned short extra_alignment_requirement = (U & 0xFFFF0000) >> 16;
  31. // [15:0] of U indicates the upper bound check.
  32. int64_t u = U & 0x0000FFFF;
  33. if (u == 0)
  34. {
  35. // No requirement to check overflow.
  36. gold_assert(L == 0);
  37. return (x & extra_alignment_requirement) == 0;
  38. }
  39. // Check both overflow and alignment if needed.
  40. int64_t low_bound = -(L == 0 ? 0 : ((int64_t)1 << L));
  41. int64_t up_bound = ((int64_t)1 << u);
  42. return ((low_bound <= x && x < up_bound)
  43. && ((x & extra_alignment_requirement) == 0));
  44. }
  45. template<>
  46. bool
  47. rvalue_checkup<0, 0>(int64_t) { return true; }
  48. template<int L, int U>
  49. uint64_t
  50. rvalue_bit_select(uint64_t x)
  51. {
  52. if (U == 63) return x >> L;
  53. return (x & (((uint64_t)1 << (U+1)) - 1)) >> L;
  54. }
  55. template<>
  56. uint64_t
  57. rvalue_bit_select<0, 0>(uint64_t x) { return x; }
  58. AArch64_reloc_property::AArch64_reloc_property(
  59. unsigned int code,
  60. const char* name,
  61. Reloc_type rtype,
  62. Reloc_class rclass,
  63. bool is_implemented,
  64. int group_index,
  65. int reference_flags,
  66. Reloc_inst reloc_inst,
  67. rvalue_checkup_func_p rvalue_checkup_func,
  68. rvalue_bit_select_func rvalue_bit_select)
  69. : code_(code), name_(name), reloc_type_(rtype), reloc_class_(rclass),
  70. group_index_(group_index),
  71. is_implemented_(is_implemented),
  72. reference_flags_(reference_flags),
  73. reloc_inst_(reloc_inst),
  74. rvalue_checkup_func_(rvalue_checkup_func),
  75. rvalue_bit_select_func_(rvalue_bit_select)
  76. {}
  77. AArch64_reloc_property_table::AArch64_reloc_property_table()
  78. {
  79. const bool Y(true), N(false);
  80. for (unsigned int i = 0; i < Property_table_size; ++i)
  81. table_[i] = NULL;
  82. #define RL_CHECK_ALIGN2 (1 << 16)
  83. #define RL_CHECK_ALIGN4 (3 << 16)
  84. #define RL_CHECK_ALIGN8 (7 << 16)
  85. #define RL_CHECK_ALIGN16 (15 << 16)
  86. #undef ARD
  87. #define ARD(rname, type, class, is_implemented, group_index, LB, UB, BSL, BSH, RFLAGS, inst) \
  88. do \
  89. { \
  90. int tidx = code_to_array_index(elfcpp::R_AARCH64_##rname); \
  91. AArch64_reloc_property * p = new AArch64_reloc_property( \
  92. elfcpp::R_AARCH64_##rname, "R_AARCH64_" #rname, \
  93. AArch64_reloc_property::RT_##type, \
  94. AArch64_reloc_property::RC_##class, \
  95. is_implemented, \
  96. group_index, \
  97. (RFLAGS), \
  98. AArch64_reloc_property::INST_##inst, \
  99. rvalue_checkup<LB,UB>, \
  100. rvalue_bit_select<BSL,BSH>); \
  101. table_[tidx] = p; \
  102. } \
  103. while (0);
  104. #include"aarch64-reloc.def"
  105. #undef ARD
  106. }
  107. // Return a string describing a relocation code that fails to get a
  108. // relocation property in get_implemented_static_reloc_property().
  109. std::string
  110. AArch64_reloc_property_table::reloc_name_in_error_message(unsigned int code)
  111. {
  112. int tidx = code_to_array_index(code);
  113. const AArch64_reloc_property* arp = this->table_[tidx];
  114. if (arp == NULL)
  115. {
  116. char buffer[100];
  117. sprintf(buffer, _("invalid reloc %u"), code);
  118. return std::string(buffer);
  119. }
  120. // gold only implements static relocation codes.
  121. AArch64_reloc_property::Reloc_type reloc_type = arp->reloc_type();
  122. gold_assert(reloc_type == AArch64_reloc_property::RT_STATIC
  123. || !arp->is_implemented());
  124. const char* prefix = NULL;
  125. switch (reloc_type)
  126. {
  127. case AArch64_reloc_property::RT_STATIC:
  128. prefix = arp->is_implemented() ? _("reloc ") : _("unimplemented reloc ");
  129. break;
  130. case AArch64_reloc_property::RT_DYNAMIC:
  131. prefix = _("dynamic reloc ");
  132. break;
  133. default:
  134. gold_unreachable();
  135. }
  136. return std::string(prefix) + arp->name();
  137. }
  138. }