MCSymbolELF.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. //===- lib/MC/MCSymbolELF.cpp ---------------------------------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/MC/MCSymbolELF.h"
  9. #include "llvm/BinaryFormat/ELF.h"
  10. namespace llvm {
  11. namespace {
  12. enum {
  13. // Shift value for STT_* flags. 7 possible values. 3 bits.
  14. ELF_STT_Shift = 0,
  15. // Shift value for STB_* flags. 4 possible values, 2 bits.
  16. ELF_STB_Shift = 3,
  17. // Shift value for STV_* flags. 4 possible values, 2 bits.
  18. ELF_STV_Shift = 5,
  19. // Shift value for STO_* flags. 3 bits. All the values are between 0x20 and
  20. // 0xe0, so we shift right by 5 before storing.
  21. ELF_STO_Shift = 7,
  22. // One bit.
  23. ELF_IsSignature_Shift = 10,
  24. // One bit.
  25. ELF_WeakrefUsedInReloc_Shift = 11,
  26. // One bit.
  27. ELF_BindingSet_Shift = 12,
  28. // One bit.
  29. ELF_IsMemoryTagged_Shift = 13,
  30. };
  31. }
  32. void MCSymbolELF::setBinding(unsigned Binding) const {
  33. setIsBindingSet();
  34. unsigned Val;
  35. switch (Binding) {
  36. default:
  37. llvm_unreachable("Unsupported Binding");
  38. case ELF::STB_LOCAL:
  39. Val = 0;
  40. break;
  41. case ELF::STB_GLOBAL:
  42. Val = 1;
  43. break;
  44. case ELF::STB_WEAK:
  45. Val = 2;
  46. break;
  47. case ELF::STB_GNU_UNIQUE:
  48. Val = 3;
  49. break;
  50. }
  51. uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STB_Shift);
  52. setFlags(OtherFlags | (Val << ELF_STB_Shift));
  53. }
  54. unsigned MCSymbolELF::getBinding() const {
  55. if (isBindingSet()) {
  56. uint32_t Val = (Flags >> ELF_STB_Shift) & 3;
  57. switch (Val) {
  58. default:
  59. llvm_unreachable("Invalid value");
  60. case 0:
  61. return ELF::STB_LOCAL;
  62. case 1:
  63. return ELF::STB_GLOBAL;
  64. case 2:
  65. return ELF::STB_WEAK;
  66. case 3:
  67. return ELF::STB_GNU_UNIQUE;
  68. }
  69. }
  70. if (isDefined())
  71. return ELF::STB_LOCAL;
  72. if (isUsedInReloc())
  73. return ELF::STB_GLOBAL;
  74. if (isWeakrefUsedInReloc())
  75. return ELF::STB_WEAK;
  76. if (isSignature())
  77. return ELF::STB_LOCAL;
  78. return ELF::STB_GLOBAL;
  79. }
  80. void MCSymbolELF::setType(unsigned Type) const {
  81. unsigned Val;
  82. switch (Type) {
  83. default:
  84. llvm_unreachable("Unsupported Binding");
  85. case ELF::STT_NOTYPE:
  86. Val = 0;
  87. break;
  88. case ELF::STT_OBJECT:
  89. Val = 1;
  90. break;
  91. case ELF::STT_FUNC:
  92. Val = 2;
  93. break;
  94. case ELF::STT_SECTION:
  95. Val = 3;
  96. break;
  97. case ELF::STT_COMMON:
  98. Val = 4;
  99. break;
  100. case ELF::STT_TLS:
  101. Val = 5;
  102. break;
  103. case ELF::STT_GNU_IFUNC:
  104. Val = 6;
  105. break;
  106. }
  107. uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STT_Shift);
  108. setFlags(OtherFlags | (Val << ELF_STT_Shift));
  109. }
  110. unsigned MCSymbolELF::getType() const {
  111. uint32_t Val = (Flags >> ELF_STT_Shift) & 7;
  112. switch (Val) {
  113. default:
  114. llvm_unreachable("Invalid value");
  115. case 0:
  116. return ELF::STT_NOTYPE;
  117. case 1:
  118. return ELF::STT_OBJECT;
  119. case 2:
  120. return ELF::STT_FUNC;
  121. case 3:
  122. return ELF::STT_SECTION;
  123. case 4:
  124. return ELF::STT_COMMON;
  125. case 5:
  126. return ELF::STT_TLS;
  127. case 6:
  128. return ELF::STT_GNU_IFUNC;
  129. }
  130. }
  131. void MCSymbolELF::setVisibility(unsigned Visibility) {
  132. assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
  133. Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
  134. uint32_t OtherFlags = getFlags() & ~(0x3 << ELF_STV_Shift);
  135. setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
  136. }
  137. unsigned MCSymbolELF::getVisibility() const {
  138. unsigned Visibility = (Flags >> ELF_STV_Shift) & 3;
  139. return Visibility;
  140. }
  141. void MCSymbolELF::setOther(unsigned Other) {
  142. assert((Other & 0x1f) == 0);
  143. Other >>= 5;
  144. assert(Other <= 0x7);
  145. uint32_t OtherFlags = getFlags() & ~(0x7 << ELF_STO_Shift);
  146. setFlags(OtherFlags | (Other << ELF_STO_Shift));
  147. }
  148. unsigned MCSymbolELF::getOther() const {
  149. unsigned Other = (Flags >> ELF_STO_Shift) & 7;
  150. return Other << 5;
  151. }
  152. void MCSymbolELF::setIsWeakrefUsedInReloc() const {
  153. uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_WeakrefUsedInReloc_Shift);
  154. setFlags(OtherFlags | (1 << ELF_WeakrefUsedInReloc_Shift));
  155. }
  156. bool MCSymbolELF::isWeakrefUsedInReloc() const {
  157. return getFlags() & (0x1 << ELF_WeakrefUsedInReloc_Shift);
  158. }
  159. void MCSymbolELF::setIsSignature() const {
  160. uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_IsSignature_Shift);
  161. setFlags(OtherFlags | (1 << ELF_IsSignature_Shift));
  162. }
  163. bool MCSymbolELF::isSignature() const {
  164. return getFlags() & (0x1 << ELF_IsSignature_Shift);
  165. }
  166. void MCSymbolELF::setIsBindingSet() const {
  167. uint32_t OtherFlags = getFlags() & ~(0x1 << ELF_BindingSet_Shift);
  168. setFlags(OtherFlags | (1 << ELF_BindingSet_Shift));
  169. }
  170. bool MCSymbolELF::isBindingSet() const {
  171. return getFlags() & (0x1 << ELF_BindingSet_Shift);
  172. }
  173. bool MCSymbolELF::isMemtag() const {
  174. return getFlags() & (0x1 << ELF_IsMemoryTagged_Shift);
  175. }
  176. void MCSymbolELF::setMemtag(bool Tagged) {
  177. uint32_t OtherFlags = getFlags() & ~(1 << ELF_IsMemoryTagged_Shift);
  178. if (Tagged)
  179. setFlags(OtherFlags | (1 << ELF_IsMemoryTagged_Shift));
  180. else
  181. setFlags(OtherFlags);
  182. }
  183. }