TapiFile.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. //===- TapiFile.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. //
  9. // This file defines the Text-based Dynamcic Library Stub format.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Object/TapiFile.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Object/Error.h"
  15. #include "llvm/Support/MemoryBuffer.h"
  16. using namespace llvm;
  17. using namespace MachO;
  18. using namespace object;
  19. static constexpr StringLiteral ObjC1ClassNamePrefix = ".objc_class_name_";
  20. static constexpr StringLiteral ObjC2ClassNamePrefix = "_OBJC_CLASS_$_";
  21. static constexpr StringLiteral ObjC2MetaClassNamePrefix = "_OBJC_METACLASS_$_";
  22. static constexpr StringLiteral ObjC2EHTypePrefix = "_OBJC_EHTYPE_$_";
  23. static constexpr StringLiteral ObjC2IVarPrefix = "_OBJC_IVAR_$_";
  24. static uint32_t getFlags(const Symbol *Sym) {
  25. uint32_t Flags = BasicSymbolRef::SF_Global;
  26. if (Sym->isUndefined())
  27. Flags |= BasicSymbolRef::SF_Undefined;
  28. else
  29. Flags |= BasicSymbolRef::SF_Exported;
  30. if (Sym->isWeakDefined() || Sym->isWeakReferenced())
  31. Flags |= BasicSymbolRef::SF_Weak;
  32. return Flags;
  33. }
  34. TapiFile::TapiFile(MemoryBufferRef Source, const InterfaceFile &interface,
  35. Architecture Arch)
  36. : SymbolicFile(ID_TapiFile, Source), Arch(Arch) {
  37. for (const auto *Symbol : interface.symbols()) {
  38. if (!Symbol->getArchitectures().has(Arch))
  39. continue;
  40. switch (Symbol->getKind()) {
  41. case SymbolKind::GlobalSymbol:
  42. Symbols.emplace_back(StringRef(), Symbol->getName(), getFlags(Symbol));
  43. break;
  44. case SymbolKind::ObjectiveCClass:
  45. if (interface.getPlatforms().count(PlatformKind::macOS) &&
  46. Arch == AK_i386) {
  47. Symbols.emplace_back(ObjC1ClassNamePrefix, Symbol->getName(),
  48. getFlags(Symbol));
  49. } else {
  50. Symbols.emplace_back(ObjC2ClassNamePrefix, Symbol->getName(),
  51. getFlags(Symbol));
  52. Symbols.emplace_back(ObjC2MetaClassNamePrefix, Symbol->getName(),
  53. getFlags(Symbol));
  54. }
  55. break;
  56. case SymbolKind::ObjectiveCClassEHType:
  57. Symbols.emplace_back(ObjC2EHTypePrefix, Symbol->getName(),
  58. getFlags(Symbol));
  59. break;
  60. case SymbolKind::ObjectiveCInstanceVariable:
  61. Symbols.emplace_back(ObjC2IVarPrefix, Symbol->getName(),
  62. getFlags(Symbol));
  63. break;
  64. }
  65. }
  66. }
  67. TapiFile::~TapiFile() = default;
  68. void TapiFile::moveSymbolNext(DataRefImpl &DRI) const { DRI.d.a++; }
  69. Error TapiFile::printSymbolName(raw_ostream &OS, DataRefImpl DRI) const {
  70. assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
  71. const Symbol &Sym = Symbols[DRI.d.a];
  72. OS << Sym.Prefix << Sym.Name;
  73. return Error::success();
  74. }
  75. Expected<uint32_t> TapiFile::getSymbolFlags(DataRefImpl DRI) const {
  76. assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
  77. return Symbols[DRI.d.a].Flags;
  78. }
  79. basic_symbol_iterator TapiFile::symbol_begin() const {
  80. DataRefImpl DRI;
  81. DRI.d.a = 0;
  82. return BasicSymbolRef{DRI, this};
  83. }
  84. basic_symbol_iterator TapiFile::symbol_end() const {
  85. DataRefImpl DRI;
  86. DRI.d.a = Symbols.size();
  87. return BasicSymbolRef{DRI, this};
  88. }