MCSymbolELF.cpp 4.9 KB

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