Skip to content

Commit e3627e2

Browse files
authored
Reland '[TextAPI] Add DylibReader' (llvm#75862)
> Add support for reading binary Mach-o dynamic libraries. It uses libObject APIs for extracting information relevant to TAPI and tbd files. This includes but is not limited to load commands encode data like install names, current/compat versions, and symbols. This originally broke because DylibReader uses Object and Object depends on TextAPI. Breaking this up in a nested library prevents this cycle.
1 parent 06b2da4 commit e3627e2

File tree

8 files changed

+473
-1
lines changed

8 files changed

+473
-1
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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/Support/Error.h"
17+
#include "llvm/Support/MemoryBuffer.h"
18+
#include "llvm/TextAPI/ArchitectureSet.h"
19+
#include "llvm/TextAPI/RecordsSlice.h"
20+
21+
namespace llvm::MachO::DylibReader {
22+
23+
struct ParseOption {
24+
/// Determines arch slice to parse.
25+
ArchitectureSet Archs = ArchitectureSet::All();
26+
/// Capture Mach-O header from binary, primarily load commands.
27+
bool MachOHeader = true;
28+
/// Capture defined symbols out of export trie and n-list.
29+
bool SymbolTable = true;
30+
/// Capture undefined symbols too.
31+
bool Undefineds = true;
32+
};
33+
34+
/// Parse Mach-O dynamic libraries to extract TAPI attributes.
35+
///
36+
/// \param Buffer Data that points to dylib.
37+
/// \param Options Determines which attributes to extract.
38+
/// \return List of record slices.
39+
Expected<Records> readFile(MemoryBufferRef Buffer, const ParseOption &Opt);
40+
41+
} // namespace llvm::MachO::DylibReader
42+
43+
#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> {
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
add_llvm_component_library(LLVMTextAPIBinaryReader
2+
DylibReader.cpp
3+
4+
LINK_COMPONENTS
5+
Support
6+
Object
7+
TextAPI
8+
TargetParser
9+
)

0 commit comments

Comments
 (0)