TapiFile.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/BinaryFormat/MachO.h"
  15. #include "llvm/Object/Error.h"
  16. #include "llvm/Support/MemoryBufferRef.h"
  17. #include "llvm/TextAPI/ArchitectureSet.h"
  18. #include "llvm/TextAPI/InterfaceFile.h"
  19. #include "llvm/TextAPI/Platform.h"
  20. #include "llvm/TextAPI/Symbol.h"
  21. using namespace llvm;
  22. using namespace MachO;
  23. using namespace object;
  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(PLATFORM_MACOS) && Arch == AK_i386) {
  46. Symbols.emplace_back(ObjC1ClassNamePrefix, Symbol->getName(),
  47. getFlags(Symbol));
  48. } else {
  49. Symbols.emplace_back(ObjC2ClassNamePrefix, Symbol->getName(),
  50. getFlags(Symbol));
  51. Symbols.emplace_back(ObjC2MetaClassNamePrefix, Symbol->getName(),
  52. getFlags(Symbol));
  53. }
  54. break;
  55. case SymbolKind::ObjectiveCClassEHType:
  56. Symbols.emplace_back(ObjC2EHTypePrefix, Symbol->getName(),
  57. getFlags(Symbol));
  58. break;
  59. case SymbolKind::ObjectiveCInstanceVariable:
  60. Symbols.emplace_back(ObjC2IVarPrefix, Symbol->getName(),
  61. getFlags(Symbol));
  62. break;
  63. }
  64. }
  65. }
  66. TapiFile::~TapiFile() = default;
  67. void TapiFile::moveSymbolNext(DataRefImpl &DRI) const { DRI.d.a++; }
  68. Error TapiFile::printSymbolName(raw_ostream &OS, DataRefImpl DRI) const {
  69. assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
  70. const Symbol &Sym = Symbols[DRI.d.a];
  71. OS << Sym.Prefix << Sym.Name;
  72. return Error::success();
  73. }
  74. Expected<uint32_t> TapiFile::getSymbolFlags(DataRefImpl DRI) const {
  75. assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
  76. return Symbols[DRI.d.a].Flags;
  77. }
  78. basic_symbol_iterator TapiFile::symbol_begin() const {
  79. DataRefImpl DRI;
  80. DRI.d.a = 0;
  81. return BasicSymbolRef{DRI, this};
  82. }
  83. basic_symbol_iterator TapiFile::symbol_end() const {
  84. DataRefImpl DRI;
  85. DRI.d.a = Symbols.size();
  86. return BasicSymbolRef{DRI, this};
  87. }