Skip to content

[TextAPI] Add DylibReader #75006

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions llvm/include/llvm/TextAPI/DylibReader.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//===- TextAPI/DylibReader.h - TAPI MachO Dylib Reader ----------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
///
/// Defines the MachO Dynamic Library Reader.
///
//===----------------------------------------------------------------------===//

#ifndef LLVM_TEXTAPI_DYLIBREADER_H
#define LLVM_TEXTAPI_DYLIBREADER_H

#include "llvm/Support/Error.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/TextAPI/ArchitectureSet.h"
#include "llvm/TextAPI/RecordsSlice.h"

namespace llvm::MachO::DylibReader {

struct ParseOption {
/// Determines arch slice to parse.
ArchitectureSet Archs = ArchitectureSet::All();
/// Capture Mach-O header from binary, primarily load commands.
bool MachOHeader = true;
/// Capture defined symbols out of export trie and n-list.
bool SymbolTable = true;
/// Capture undefined symbols too.
bool Undefineds = true;
};

/// Parse Mach-O dynamic libraries to extract TAPI attributes.
///
/// \param Buffer Data that points to dylib.
/// \param Options Determines which attributes to extract.
/// \return List of record slices.
Expected<Records> readFile(MemoryBufferRef Buffer, const ParseOption &Opt);

} // namespace llvm::MachO::DylibReader

#endif // LLVM_TEXTAPI_DYLIBREADER_H
4 changes: 4 additions & 0 deletions llvm/include/llvm/TextAPI/Record.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,10 @@ class GlobalRecord : public Record {

bool isFunction() const { return GV == Kind::Function; }
bool isVariable() const { return GV == Kind::Variable; }
void setKind(const Kind &V) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case I would prefer to pass the argument by value. It is only an uint8_t enum. There is no need to pass it by reference.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree, I also wanted to keep the const mostly to ensure that the passed value isn't modified, just assigned. But that puts us back to #75006 (comment)

if (GV == Kind::Unknown)
GV = V;
}

private:
Kind GV;
Expand Down
2 changes: 2 additions & 0 deletions llvm/include/llvm/TextAPI/RecordsSlice.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class RecordsSlice {
std::unique_ptr<BinaryAttrs> BA{nullptr};
};

using Records = llvm::SmallVector<std::shared_ptr<RecordsSlice>, 4>;

} // namespace MachO
} // namespace llvm
#endif // LLVM_TEXTAPI_RECORDSLICE_H
3 changes: 2 additions & 1 deletion llvm/include/llvm/TextAPI/TextAPIError.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ enum class TextAPIErrorCode {
NoSuchArchitecture,
EmptyResults,
GenericFrontendError,
InvalidInputFormat
InvalidInputFormat,
UnsupportedTarget
};

class TextAPIError : public llvm::ErrorInfo<TextAPIError> {
Expand Down
1 change: 1 addition & 0 deletions llvm/lib/TextAPI/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
add_llvm_component_library(LLVMTextAPI
Architecture.cpp
ArchitectureSet.cpp
DylibReader.cpp
InterfaceFile.cpp
TextStubV5.cpp
PackedVersion.cpp
Expand Down
Loading