MCSymbolMachO.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- MCSymbolMachO.h - ---------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_MC_MCSYMBOLMACHO_H
  14. #define LLVM_MC_MCSYMBOLMACHO_H
  15. #include "llvm/ADT/Twine.h"
  16. #include "llvm/MC/MCSymbol.h"
  17. namespace llvm {
  18. class MCSymbolMachO : public MCSymbol {
  19. /// We store the value for the 'desc' symbol field in the
  20. /// lowest 16 bits of the implementation defined flags.
  21. enum MachOSymbolFlags : uint16_t { // See <mach-o/nlist.h>.
  22. SF_DescFlagsMask = 0xFFFF,
  23. // Reference type flags.
  24. SF_ReferenceTypeMask = 0x0007,
  25. SF_ReferenceTypeUndefinedNonLazy = 0x0000,
  26. SF_ReferenceTypeUndefinedLazy = 0x0001,
  27. SF_ReferenceTypeDefined = 0x0002,
  28. SF_ReferenceTypePrivateDefined = 0x0003,
  29. SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
  30. SF_ReferenceTypePrivateUndefinedLazy = 0x0005,
  31. // Other 'desc' flags.
  32. SF_ThumbFunc = 0x0008,
  33. SF_NoDeadStrip = 0x0020,
  34. SF_WeakReference = 0x0040,
  35. SF_WeakDefinition = 0x0080,
  36. SF_SymbolResolver = 0x0100,
  37. SF_AltEntry = 0x0200,
  38. SF_Cold = 0x0400,
  39. // Common alignment
  40. SF_CommonAlignmentMask = 0xF0FF,
  41. SF_CommonAlignmentShift = 8
  42. };
  43. public:
  44. MCSymbolMachO(const StringMapEntry<bool> *Name, bool isTemporary)
  45. : MCSymbol(SymbolKindMachO, Name, isTemporary) {}
  46. // Reference type methods.
  47. void clearReferenceType() const {
  48. modifyFlags(0, SF_ReferenceTypeMask);
  49. }
  50. void setReferenceTypeUndefinedLazy(bool Value) const {
  51. modifyFlags(Value ? SF_ReferenceTypeUndefinedLazy : 0,
  52. SF_ReferenceTypeUndefinedLazy);
  53. }
  54. // Other 'desc' methods.
  55. void setThumbFunc() const {
  56. modifyFlags(SF_ThumbFunc, SF_ThumbFunc);
  57. }
  58. bool isNoDeadStrip() const {
  59. return getFlags() & SF_NoDeadStrip;
  60. }
  61. void setNoDeadStrip() const {
  62. modifyFlags(SF_NoDeadStrip, SF_NoDeadStrip);
  63. }
  64. bool isWeakReference() const {
  65. return getFlags() & SF_WeakReference;
  66. }
  67. void setWeakReference() const {
  68. modifyFlags(SF_WeakReference, SF_WeakReference);
  69. }
  70. bool isWeakDefinition() const {
  71. return getFlags() & SF_WeakDefinition;
  72. }
  73. void setWeakDefinition() const {
  74. modifyFlags(SF_WeakDefinition, SF_WeakDefinition);
  75. }
  76. bool isSymbolResolver() const {
  77. return getFlags() & SF_SymbolResolver;
  78. }
  79. void setSymbolResolver() const {
  80. modifyFlags(SF_SymbolResolver, SF_SymbolResolver);
  81. }
  82. void setAltEntry() const {
  83. modifyFlags(SF_AltEntry, SF_AltEntry);
  84. }
  85. bool isAltEntry() const {
  86. return getFlags() & SF_AltEntry;
  87. }
  88. void setCold() const { modifyFlags(SF_Cold, SF_Cold); }
  89. bool isCold() const { return getFlags() & SF_Cold; }
  90. void setDesc(unsigned Value) const {
  91. assert(Value == (Value & SF_DescFlagsMask) &&
  92. "Invalid .desc value!");
  93. setFlags(Value & SF_DescFlagsMask);
  94. }
  95. /// Get the encoded value of the flags as they will be emitted in to
  96. /// the MachO binary
  97. uint16_t getEncodedFlags(bool EncodeAsAltEntry) const {
  98. uint16_t Flags = getFlags();
  99. // Common alignment is packed into the 'desc' bits.
  100. if (isCommon()) {
  101. if (MaybeAlign MaybeAlignment = getCommonAlignment()) {
  102. Align Alignment = *MaybeAlignment;
  103. unsigned Log2Size = Log2(Alignment);
  104. if (Log2Size > 15)
  105. report_fatal_error("invalid 'common' alignment '" +
  106. Twine(Alignment.value()) + "' for '" +
  107. getName() + "'",
  108. false);
  109. Flags = (Flags & SF_CommonAlignmentMask) |
  110. (Log2Size << SF_CommonAlignmentShift);
  111. }
  112. }
  113. if (EncodeAsAltEntry)
  114. Flags |= SF_AltEntry;
  115. return Flags;
  116. }
  117. static bool classof(const MCSymbol *S) { return S->isMachO(); }
  118. };
  119. }
  120. #endif
  121. #ifdef __GNUC__
  122. #pragma GCC diagnostic pop
  123. #endif