LLVM  14.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"
15 #include "llvm/Object/Error.h"
17 #include "llvm/TextAPI/Symbol.h"
18 
19 using namespace llvm;
20 using namespace MachO;
21 using namespace object;
22 
23 static uint32_t getFlags(const Symbol *Sym) {
24  uint32_t Flags = BasicSymbolRef::SF_Global;
25  if (Sym->isUndefined())
26  Flags |= BasicSymbolRef::SF_Undefined;
27  else
28  Flags |= BasicSymbolRef::SF_Exported;
29 
30  if (Sym->isWeakDefined() || Sym->isWeakReferenced())
31  Flags |= BasicSymbolRef::SF_Weak;
32 
33  return Flags;
34 }
35 
36 TapiFile::TapiFile(MemoryBufferRef Source, const InterfaceFile &interface,
37  Architecture Arch)
38  : SymbolicFile(ID_TapiFile, Source), Arch(Arch) {
39  for (const auto *Symbol : interface.symbols()) {
40  if (!Symbol->getArchitectures().has(Arch))
41  continue;
42 
43  switch (Symbol->getKind()) {
44  case SymbolKind::GlobalSymbol:
45  Symbols.emplace_back(StringRef(), Symbol->getName(), getFlags(Symbol));
46  break;
47  case SymbolKind::ObjectiveCClass:
48  if (interface.getPlatforms().count(PlatformKind::macOS) &&
49  Arch == AK_i386) {
50  Symbols.emplace_back(ObjC1ClassNamePrefix, Symbol->getName(),
51  getFlags(Symbol));
52  } else {
53  Symbols.emplace_back(ObjC2ClassNamePrefix, Symbol->getName(),
54  getFlags(Symbol));
55  Symbols.emplace_back(ObjC2MetaClassNamePrefix, Symbol->getName(),
56  getFlags(Symbol));
57  }
58  break;
59  case SymbolKind::ObjectiveCClassEHType:
60  Symbols.emplace_back(ObjC2EHTypePrefix, Symbol->getName(),
61  getFlags(Symbol));
62  break;
63  case SymbolKind::ObjectiveCInstanceVariable:
64  Symbols.emplace_back(ObjC2IVarPrefix, Symbol->getName(),
65  getFlags(Symbol));
66  break;
67  }
68  }
69 }
70 
71 TapiFile::~TapiFile() = default;
72 
73 void TapiFile::moveSymbolNext(DataRefImpl &DRI) const { DRI.d.a++; }
74 
76  assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
77  const Symbol &Sym = Symbols[DRI.d.a];
78  OS << Sym.Prefix << Sym.Name;
79  return Error::success();
80 }
81 
83  assert(DRI.d.a < Symbols.size() && "Attempt to access symbol out of bounds");
84  return Symbols[DRI.d.a].Flags;
85 }
86 
88  DataRefImpl DRI;
89  DRI.d.a = 0;
90  return BasicSymbolRef{DRI, this};
91 }
92 
94  DataRefImpl DRI;
95  DRI.d.a = Symbols.size();
96  return BasicSymbolRef{DRI, this};
97 }
llvm::MachO::ObjC2EHTypePrefix
constexpr StringLiteral ObjC2EHTypePrefix
Definition: Symbol.h:59
MemoryBuffer.h
llvm
This file implements support for optimizing divisions by a constant.
Definition: AllocatorList.h:23
llvm::object::TapiFile::moveSymbolNext
void moveSymbolNext(DataRefImpl &DRI) const override
Definition: TapiFile.cpp:73
llvm::object::DataRefImpl::a
uint32_t a
Definition: SymbolicFile.h:37
StringRef.h
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
llvm::MachO::Symbol::isWeakDefined
bool isWeakDefined() const
Definition: Symbol.h:76
llvm::object::TapiFile::getSymbolFlags
Expected< uint32_t > getSymbolFlags(DataRefImpl DRI) const override
Definition: TapiFile.cpp:82
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
llvm::MemoryBufferRef
Definition: MemoryBufferRef.h:22
TapiFile.h
Error.h
llvm::MachO::ObjC2ClassNamePrefix
constexpr StringLiteral ObjC2ClassNamePrefix
Definition: Symbol.h:57
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:101
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:75
llvm::object::DataRefImpl::d
struct llvm::object::DataRefImpl::@321 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:164
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:80
llvm::MachO::ObjC2MetaClassNamePrefix
constexpr StringLiteral ObjC2MetaClassNamePrefix
Definition: Symbol.h:58
llvm::MachO::InterfaceFile
Defines the interface file.
Definition: InterfaceFile.h:156
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::object::content_iterator
Definition: SymbolicFile.h:67
llvm::MachO::InterfaceFile::symbols
const_symbol_range symbols() const
Definition: InterfaceFile.h:380
llvm::MachO::ObjC1ClassNamePrefix
constexpr StringLiteral ObjC1ClassNamePrefix
Definition: Symbol.h:56
Symbol.h
llvm::object::TapiFile::symbol_begin
basic_symbol_iterator symbol_begin() const override
Definition: TapiFile.cpp:87
llvm::Sched::Source
@ Source
Definition: TargetLowering.h:100
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::MachO::Symbol
Definition: Symbol.h:63
llvm::object::DataRefImpl
Definition: SymbolicFile.h:33
getFlags
static uint32_t getFlags(const Symbol *Sym)
Definition: TapiFile.cpp:23
llvm::MachO::ObjC2IVarPrefix
constexpr StringLiteral ObjC2IVarPrefix
Definition: Symbol.h:60
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
llvm::ARMBuildAttrs::Symbol
@ Symbol
Definition: ARMBuildAttributes.h:79
llvm::object::TapiFile::symbol_end
basic_symbol_iterator symbol_end() const override
Definition: TapiFile.cpp:93
llvm::object::SymbolicFile
Definition: SymbolicFile.h:143
llvm::MachO::Symbol::isUndefined
bool isUndefined() const
Definition: Symbol.h:89
llvm::MachO::InterfaceFile::getPlatforms
PlatformSet getPlatforms() const
Get the platforms.
Definition: InterfaceFile.h:191