TapiFile.cpp 3.1 KB

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