Skip to content

Commit c336d8b

Browse files
committed
[TextAPI] Add DylibReader
Add support for reading binary Mach-o dynamic libraries. It uses libObject APIs for extracting information relavant to TAPI and tbd files. This includes but is not limited to load commands encode data like install names, current/compat versions and symbols.
1 parent 8b11811 commit c336d8b

File tree

6 files changed

+470
-1
lines changed

6 files changed

+470
-1
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//===- TextAPI/DylibReader.h - TAPI MachO Dylib Reader ----------*- C++ -*-===//
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+
/// Defines the MachO Dynamic Library Reader.
10+
///
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_TEXTAPI_DYLIBREADER_H
14+
#define LLVM_TEXTAPI_DYLIBREADER_H
15+
16+
#include "llvm/BinaryFormat/Magic.h"
17+
#include "llvm/Support/Error.h"
18+
#include "llvm/Support/MemoryBuffer.h"
19+
#include "llvm/TextAPI/ArchitectureSet.h"
20+
#include "llvm/TextAPI/RecordsSlice.h"
21+
22+
namespace llvm {
23+
namespace MachO {
24+
25+
namespace DylibReader {
26+
27+
struct ParseOption {
28+
/// Determines arch slice to parse.
29+
ArchitectureSet Archs = ArchitectureSet::All();
30+
/// Capture Mach-O header from binary, primarily load commands.
31+
bool MachOHeader = true;
32+
/// Capture defined symbols out of export trie and n-list.
33+
bool SymbolTable = true;
34+
/// Capture undefined symbols too.
35+
bool Undefineds = true;
36+
};
37+
38+
/// Parse Mach-O dynamic libraries to extract TAPI attributes.
39+
///
40+
/// \param Buffer Data that points to dylib.
41+
/// \param Options Determines which attributes to extract.
42+
/// \return List of record slices.
43+
Expected<Records> readFile(MemoryBufferRef Buffer, const ParseOption &Opt);
44+
45+
} // namespace DylibReader
46+
47+
} // end namespace MachO.
48+
} // end namespace llvm.
49+
50+
#endif // LLVM_TEXTAPI_DYLIBREADER_H

llvm/include/llvm/TextAPI/Record.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,10 @@ class GlobalRecord : public Record {
103103

104104
bool isFunction() const { return GV == Kind::Function; }
105105
bool isVariable() const { return GV == Kind::Variable; }
106+
void setKind(const Kind V) {
107+
if (GV == Kind::Unknown)
108+
GV = V;
109+
}
106110

107111
private:
108112
Kind GV;

llvm/include/llvm/TextAPI/RecordsSlice.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ class RecordsSlice {
181181
std::unique_ptr<BinaryAttrs> BA{nullptr};
182182
};
183183

184+
using Records = llvm::SmallVector<std::shared_ptr<RecordsSlice>, 4>;
185+
184186
} // namespace MachO
185187
} // namespace llvm
186188
#endif // LLVM_TEXTAPI_RECORDSLICE_H

llvm/include/llvm/TextAPI/TextAPIError.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ enum class TextAPIErrorCode {
2121
NoSuchArchitecture,
2222
EmptyResults,
2323
GenericFrontendError,
24-
InvalidInputFormat
24+
InvalidInputFormat,
25+
UnsupportedTarget
2526
};
2627

2728
class TextAPIError : public llvm::ErrorInfo<TextAPIError> {

llvm/lib/TextAPI/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
add_llvm_component_library(LLVMTextAPI
22
Architecture.cpp
33
ArchitectureSet.cpp
4+
DylibReader.cpp
45
InterfaceFile.cpp
56
TextStubV5.cpp
67
PackedVersion.cpp

0 commit comments

Comments
 (0)