LLVM  16.0.0git
TapiFile.cpp
Go to the documentation of this file.
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 
13 #include "llvm/Object/TapiFile.h"
14 #include "llvm/ADT/StringRef.h"
16 #include "llvm/Object/Error.h"
20 #include "llvm/TextAPI/Platform.h"
21 #include "llvm/TextAPI/Symbol.h"
22 
23 using namespace llvm;
24 using namespace MachO;
25 using namespace object;
26 
27 static uint32_t getFlags(const Symbol *Sym) {
28  uint32_t Flags = BasicSymbolRef::SF_Global;
29  if (Sym->isUndefined())
30  Flags |= BasicSymbolRef::SF_Undefined;
31  else
32  Flags |= BasicSymbolRef::SF_Exported;
33 
34  if (Sym->isWeakDefined() || Sym->isWeakReferenced())
35  Flags |= BasicSymbolRef::SF_Weak;
36 
37  return Flags;
38 }
39 
40 TapiFile::TapiFile(MemoryBufferRef Source, const InterfaceFile &interface,
41  Architecture Arch)
42  : SymbolicFile(ID_TapiFile, Source), Arch(Arch) {
43  for (const auto *Symbol : interface.symbols()) {
44  if (!Symbol->getArchitectures().has(Arch))
45  continue;
46 
47  switch (Symbol->getKind()) {
48  case SymbolKind::GlobalSymbol:
49  Symbols.emplace_back(StringRef(), Symbol->getName(), getFlags(Symbol));
50  break;
51  case SymbolKind::ObjectiveCClass:
52  if (interface.getPlatforms().count(PLATFORM_MACOS) && Arch == AK_i386) {
53  Symbols.emplace_back(ObjC1ClassNamePrefix, Symbol->getName(),
54  getFlags(Symbol));
55  } else {
56  Symbols.emplace_back(ObjC2ClassNamePrefix, Symbol->getName(),
57  getFlags(Symbol));
58  Symbols.emplace_back(ObjC2MetaClassNamePrefix, Symbol->getName(),
59  getFlags(Symbol));
60  }
61  break;
62  case SymbolKind::ObjectiveCClassEHType:
63  Symbols.emplace_back(ObjC2EHTypePrefix, Symbol->getName(),
64  getFlags(Symbol));
65  break;
66  case SymbolKind::ObjectiveCInstanceVariable:
67  Symbols.emplace_back(ObjC2IVarPrefix, Symbol->getName(),
68  getFlags(Symbol));
69  break;
70  }
71  }
72 }
73 
74 TapiFile::~TapiFile() = default;
75 
76 void TapiFile::moveSymbolNext(DataRefImpl &DRI) const { DRI.d.a++; }
77 
79  assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
80  const Symbol &Sym = Symbols[DRI.d.a];
81  OS << Sym.Prefix << Sym.Name;
82  return Error::success();
83 }
84 
86  assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
87  return Symbols[DRI.d.a].Flags;
88 }
89 
91  DataRefImpl DRI;
92  DRI.d.a = 0;
93  return BasicSymbolRef{DRI, this};
94 }
95 
97  DataRefImpl DRI;
98  DRI.d.a = Symbols.size();
99  return BasicSymbolRef{DRI, this};
100 }
llvm::MachO::ObjC2EHTypePrefix
constexpr StringLiteral ObjC2EHTypePrefix
Definition: Symbol.h:58
llvm::MachO::PLATFORM_MACOS
@ PLATFORM_MACOS
Definition: MachO.h:499
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
llvm::object::TapiFile::moveSymbolNext
void moveSymbolNext(DataRefImpl &DRI) const override
Definition: TapiFile.cpp:76
llvm::object::DataRefImpl::a
uint32_t a
Definition: SymbolicFile.h:39
StringRef.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:329
llvm::MachO::Symbol::isWeakDefined
bool isWeakDefined() const
Definition: Symbol.h:75
MemoryBufferRef.h
llvm::object::TapiFile::getSymbolFlags
Expected< uint32_t > getSymbolFlags(DataRefImpl DRI) const override
Definition: TapiFile.cpp:85
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:41
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
TapiFile.h
Error.h
llvm::MachO::ObjC2ClassNamePrefix
constexpr StringLiteral ObjC2ClassNamePrefix
Definition: Symbol.h:56
Platform.h
llvm::object::TapiFile::~TapiFile
~TapiFile() override
llvm::object::BasicSymbolRef
This is a value type class that represents a single symbol in the list of symbols in the object file.
Definition: SymbolicFile.h:103
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::object::TapiFile::printSymbolName
Error printSymbolName(raw_ostream &OS, DataRefImpl DRI) const override
Definition: TapiFile.cpp:78
llvm::object::DataRefImpl::d
struct llvm::object::DataRefImpl::@340 d
object
bar al al movzbl eax ret Missed when stored in a memory object
Definition: README.txt:1411
llvm::SmallSet::count
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:165
llvm::MachO::Architecture
Architecture
Defines the architecture slices that are supported by Text-based Stub files.
Definition: Architecture.h:27
llvm::MachO::Symbol::isWeakReferenced
bool isWeakReferenced() const
Definition: Symbol.h:79
llvm::MachO::ObjC2MetaClassNamePrefix
constexpr StringLiteral ObjC2MetaClassNamePrefix
Definition: Symbol.h:57
llvm::MachO::InterfaceFile
Defines the interface file.
Definition: InterfaceFile.h:152
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::content_iterator
Definition: SymbolicFile.h:69
llvm::MachO::InterfaceFile::symbols
const_symbol_range symbols() const
Definition: InterfaceFile.h:376
llvm::MachO::ObjC1ClassNamePrefix
constexpr StringLiteral ObjC1ClassNamePrefix
Definition: Symbol.h:55
Symbol.h
llvm::object::TapiFile::symbol_begin
basic_symbol_iterator symbol_begin() const override
Definition: TapiFile.cpp:90
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:99
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
uint32_t
llvm::MachO::Symbol
Definition: Symbol.h:62
llvm::object::DataRefImpl
Definition: SymbolicFile.h:35
getFlags
static uint32_t getFlags(const Symbol *Sym)
Definition: TapiFile.cpp:27
llvm::MachO::ObjC2IVarPrefix
constexpr StringLiteral ObjC2IVarPrefix
Definition: Symbol.h:59
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:155
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:83
ArchitectureSet.h
llvm::object::TapiFile::symbol_end
basic_symbol_iterator symbol_end() const override
Definition: TapiFile.cpp:96
MachO.h
llvm::object::SymbolicFile
Definition: SymbolicFile.h:145
llvm::MachO::Symbol::isUndefined
bool isUndefined() const
Definition: Symbol.h:88
InterfaceFile.h
llvm::MachO::InterfaceFile::getPlatforms
PlatformSet getPlatforms() const
Get the platforms.
Definition: InterfaceFile.h:187