LLVM  10.0.0svn
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 
18 using namespace llvm;
19 using namespace MachO;
20 using namespace object;
21 
22 static constexpr StringLiteral ObjC1ClassNamePrefix = ".objc_class_name_";
23 static constexpr StringLiteral ObjC2ClassNamePrefix = "_OBJC_CLASS_$_";
24 static constexpr StringLiteral ObjC2MetaClassNamePrefix = "_OBJC_METACLASS_$_";
25 static constexpr StringLiteral ObjC2EHTypePrefix = "_OBJC_EHTYPE_$_";
26 static constexpr StringLiteral ObjC2IVarPrefix = "_OBJC_IVAR_$_";
27 
28 static uint32_t getFlags(const Symbol *Sym) {
29  uint32_t Flags = BasicSymbolRef::SF_Global;
30  if (Sym->isUndefined())
31  Flags |= BasicSymbolRef::SF_Undefined;
32  else
33  Flags |= BasicSymbolRef::SF_Exported;
34 
35  if (Sym->isWeakDefined() || Sym->isWeakReferenced())
36  Flags |= BasicSymbolRef::SF_Weak;
37 
38  return Flags;
39 }
40 
41 TapiFile::TapiFile(MemoryBufferRef Source, const InterfaceFile &interface,
42  Architecture Arch)
43  : SymbolicFile(ID_TapiFile, Source) {
44  for (const auto *Symbol : interface.symbols()) {
45  if (!Symbol->getArchitectures().has(Arch))
46  continue;
47 
48  switch (Symbol->getKind()) {
49  case SymbolKind::GlobalSymbol:
50  Symbols.emplace_back(StringRef(), Symbol->getName(), getFlags(Symbol));
51  break;
52  case SymbolKind::ObjectiveCClass:
53  if (interface.getPlatforms().count(PlatformKind::macOS) &&
54  Arch == AK_i386) {
55  Symbols.emplace_back(ObjC1ClassNamePrefix, Symbol->getName(),
56  getFlags(Symbol));
57  } else {
58  Symbols.emplace_back(ObjC2ClassNamePrefix, Symbol->getName(),
59  getFlags(Symbol));
60  Symbols.emplace_back(ObjC2MetaClassNamePrefix, Symbol->getName(),
61  getFlags(Symbol));
62  }
63  break;
64  case SymbolKind::ObjectiveCClassEHType:
65  Symbols.emplace_back(ObjC2EHTypePrefix, Symbol->getName(),
66  getFlags(Symbol));
67  break;
68  case SymbolKind::ObjectiveCInstanceVariable:
69  Symbols.emplace_back(ObjC2IVarPrefix, Symbol->getName(),
70  getFlags(Symbol));
71  break;
72  }
73  }
74 }
75 
76 TapiFile::~TapiFile() = default;
77 
79  const auto *Sym = reinterpret_cast<const Symbol *>(DRI.p);
80  DRI.p = reinterpret_cast<uintptr_t>(++Sym);
81 }
82 
84  const auto *Sym = reinterpret_cast<const Symbol *>(DRI.p);
85  OS << Sym->Prefix << Sym->Name;
86  return Error::success();
87 }
88 
90  const auto *Sym = reinterpret_cast<const Symbol *>(DRI.p);
91  return Sym->Flags;
92 }
93 
95  DataRefImpl DRI;
96  DRI.p = reinterpret_cast<uintptr_t>(&*Symbols.begin());
97  return BasicSymbolRef{DRI, this};
98 }
99 
101  DataRefImpl DRI;
102  DRI.p = reinterpret_cast<uintptr_t>(&*Symbols.end());
103  return BasicSymbolRef{DRI, this};
104 }
Defines the interface file.
This class represents lattice values for constants.
Definition: AllocatorList.h:23
static constexpr StringLiteral ObjC2ClassNamePrefix
Definition: TapiFile.cpp:23
basic_symbol_iterator symbol_begin() const override
Definition: TapiFile.cpp:94
void moveSymbolNext(DataRefImpl &DRI) const override
Definition: TapiFile.cpp:78
bool isWeakReferenced() const
Definition: Symbol.h:74
static constexpr StringLiteral ObjC2MetaClassNamePrefix
Definition: TapiFile.cpp:24
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:852
static constexpr StringLiteral ObjC2EHTypePrefix
Definition: TapiFile.cpp:25
basic_symbol_iterator symbol_end() const override
Definition: TapiFile.cpp:100
static constexpr StringLiteral ObjC1ClassNamePrefix
Definition: TapiFile.cpp:22
static constexpr StringLiteral ObjC2IVarPrefix
Definition: TapiFile.cpp:26
Error printSymbolName(raw_ostream &OS, DataRefImpl DRI) const override
Definition: TapiFile.cpp:83
static ErrorSuccess success()
Create a success value.
Definition: Error.h:326
Architecture
Defines the architecture slices that are supported by Text-based Stub files.
Definition: Architecture.h:24
bool isUndefined() const
Definition: Symbol.h:83
static uint32_t getFlags(const Symbol *Sym)
Definition: TapiFile.cpp:28
uint32_t getSymbolFlags(DataRefImpl DRI) const override
Definition: TapiFile.cpp:89
This is a value type class that represents a single symbol in the list of symbols in the object file...
Definition: SymbolicFile.h:98
PlatformSet getPlatforms() const
Get the platforms.
bool isWeakDefined() const
Definition: Symbol.h:70
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
const_symbol_range symbols() const
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:48
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
Definition: SmallSet.h:164