-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[llvm-objdump] Add preliminary support for decoding binary files #115667
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
Open
michaeljclark
wants to merge
1
commit into
llvm:main
Choose a base branch
from
michaeljclark:objdump-binary
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| //===- BinaryObjectFile.h - Binary object file implementation ---*- 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 | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file declares the BinaryObjectFile class. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| #ifndef LLVM_OBJECT_RAWOBJECTFILE_H | ||
| #define LLVM_OBJECT_RAWOBJECTFILE_H | ||
|
|
||
| #include "llvm/ADT/ArrayRef.h" | ||
| #include "llvm/ADT/STLExtras.h" | ||
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/ADT/iterator_range.h" | ||
| #include "llvm/Object/Binary.h" | ||
| #include "llvm/Object/Error.h" | ||
| #include "llvm/Object/ObjectFile.h" | ||
| #include "llvm/Object/SymbolicFile.h" | ||
| #include "llvm/Support/Casting.h" | ||
| #include "llvm/Support/Error.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
| #include "llvm/Support/MemoryBufferRef.h" | ||
| #include "llvm/Support/ScopedPrinter.h" | ||
| #include "llvm/TargetParser/SubtargetFeature.h" | ||
| #include "llvm/TargetParser/Triple.h" | ||
| #include <cassert> | ||
| #include <cstdint> | ||
|
|
||
| namespace llvm { | ||
|
|
||
| template <typename T> class SmallVectorImpl; | ||
|
|
||
| namespace object { | ||
|
|
||
| struct BinarySymbol { | ||
| uint32_t Flags = 0; | ||
| uint64_t Value = 0; | ||
| StringRef Name; | ||
| }; | ||
|
|
||
| struct BinaryRelocation { | ||
| uint64_t Offset = 0; | ||
| uint64_t Symbol = 0; | ||
| uint64_t Type = 0; | ||
| }; | ||
|
|
||
| struct BinarySection { | ||
| uint64_t Offset = 0; | ||
| uint64_t Index = 0; | ||
| uint64_t Address = 0; | ||
| uint64_t Size = 0; | ||
| StringRef Name; | ||
| std::vector<BinaryRelocation> Relocations; | ||
| }; | ||
|
|
||
| class BinaryObjectFile : public ObjectFile { | ||
| private: | ||
| std::vector<BinarySymbol> Symbols; | ||
| std::vector<BinarySection> Sections; | ||
|
|
||
| public: | ||
| BinaryObjectFile(MemoryBufferRef Source); | ||
|
|
||
| bool is64Bit() const override; | ||
|
|
||
| basic_symbol_iterator symbol_begin() const override; | ||
| basic_symbol_iterator symbol_end() const override; | ||
| section_iterator section_begin() const override; | ||
| section_iterator section_end() const override; | ||
|
|
||
| const BinarySymbol &getBinarySymbol(const DataRefImpl &Symb) const; | ||
| const BinarySymbol &getBinarySymbol(const SymbolRef &Symb) const; | ||
|
|
||
| void moveSymbolNext(DataRefImpl &Symb) const override; | ||
| Expected<StringRef> getSymbolName(DataRefImpl Symb) const override; | ||
| Expected<uint32_t> getSymbolFlags(DataRefImpl Symb) const override; | ||
| Expected<uint64_t> getSymbolAddress(DataRefImpl Symb) const override; | ||
| uint64_t getSymbolValueImpl(DataRefImpl Symb) const override; | ||
| uint64_t getCommonSymbolSizeImpl(DataRefImpl Symb) const override; | ||
| Expected<SymbolRef::Type> getSymbolType(DataRefImpl Symb) const override; | ||
| Expected<section_iterator> getSymbolSection(DataRefImpl Symb) const override; | ||
|
|
||
| const BinarySection &getBinarySection(const DataRefImpl Ref) const; | ||
| const BinarySection &getBinarySection(const SectionRef &Section) const; | ||
|
|
||
| void moveSectionNext(DataRefImpl &Sec) const override; | ||
| Expected<StringRef> getSectionName(DataRefImpl Sec) const override; | ||
| uint64_t getSectionAddress(DataRefImpl Sec) const override; | ||
| uint64_t getSectionIndex(DataRefImpl Sec) const override; | ||
| uint64_t getSectionSize(DataRefImpl Sec) const override; | ||
| Expected<ArrayRef<uint8_t>> | ||
| getSectionContents(DataRefImpl Sec) const override; | ||
| uint64_t getSectionAlignment(DataRefImpl Sec) const override; | ||
| bool isSectionCompressed(DataRefImpl Sec) const override; | ||
| bool isSectionText(DataRefImpl Sec) const override; | ||
| bool isSectionData(DataRefImpl Sec) const override; | ||
| bool isSectionBSS(DataRefImpl Sec) const override; | ||
| bool isSectionVirtual(DataRefImpl Sec) const override; | ||
|
|
||
| relocation_iterator section_rel_begin(DataRefImpl Sec) const override; | ||
| relocation_iterator section_rel_end(DataRefImpl Sec) const override; | ||
|
|
||
| const BinaryRelocation &getBinaryRelocation(const RelocationRef &Ref) const; | ||
| const BinaryRelocation &getBinaryRelocation(DataRefImpl Ref) const; | ||
|
|
||
| // Overrides from RelocationRef. | ||
| void moveRelocationNext(DataRefImpl &Rel) const override; | ||
| uint64_t getRelocationOffset(DataRefImpl Rel) const override; | ||
| symbol_iterator getRelocationSymbol(DataRefImpl Rel) const override; | ||
| uint64_t getRelocationType(DataRefImpl Rel) const override; | ||
| void getRelocationTypeName(DataRefImpl Rel, | ||
| SmallVectorImpl<char> &Result) const override; | ||
|
|
||
| uint8_t getBytesInAddress() const override; | ||
| StringRef getFileFormatName() const override; | ||
| Triple::ArchType getArch() const override; | ||
| Expected<SubtargetFeatures> getFeatures() const override; | ||
| std::optional<StringRef> tryGetCPUName() const override; | ||
| bool isRelocatableObject() const override; | ||
| }; | ||
|
|
||
| } // end namespace object | ||
| } // end namespace llvm | ||
|
|
||
| #endif // LLVM_OBJECT_RAWOBJECTFILE_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -14,6 +14,7 @@ | |
| #include "llvm/ADT/StringRef.h" | ||
| #include "llvm/BinaryFormat/Magic.h" | ||
| #include "llvm/Object/Archive.h" | ||
| #include "llvm/Object/BinaryObjectFile.h" | ||
| #include "llvm/Object/Error.h" | ||
| #include "llvm/Object/MachOUniversal.h" | ||
| #include "llvm/Object/Minidump.h" | ||
|
|
@@ -44,9 +45,14 @@ MemoryBufferRef Binary::getMemoryBufferRef() const { return Data; } | |
|
|
||
| Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, | ||
| LLVMContext *Context, | ||
| bool InitContent) { | ||
| bool InitContent, | ||
| bool RawBinary) { | ||
| file_magic Type = identify_magic(Buffer.getBuffer()); | ||
|
|
||
| if (RawBinary) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: no need for braces for single line if. |
||
| return ObjectFile::createBinaryObjectFile(Buffer); | ||
| } | ||
|
|
||
| switch (Type) { | ||
| case file_magic::archive: | ||
| return Archive::create(Buffer); | ||
|
|
@@ -103,8 +109,10 @@ Expected<std::unique_ptr<Binary>> object::createBinary(MemoryBufferRef Buffer, | |
| llvm_unreachable("Unexpected Binary File Type"); | ||
| } | ||
|
|
||
| Expected<OwningBinary<Binary>> | ||
| object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) { | ||
| Expected<OwningBinary<Binary>> object::createBinary(StringRef Path, | ||
| LLVMContext *Context, | ||
| bool InitContent, | ||
| bool RawBinary) { | ||
| ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = | ||
| MemoryBuffer::getFileOrSTDIN(Path, /*IsText=*/false, | ||
| /*RequiresNullTerminator=*/false); | ||
|
|
@@ -113,7 +121,7 @@ object::createBinary(StringRef Path, LLVMContext *Context, bool InitContent) { | |
| std::unique_ptr<MemoryBuffer> &Buffer = FileOrErr.get(); | ||
|
|
||
| Expected<std::unique_ptr<Binary>> BinOrErr = | ||
| createBinary(Buffer->getMemBufferRef(), Context, InitContent); | ||
| createBinary(Buffer->getMemBufferRef(), Context, InitContent, RawBinary); | ||
| if (!BinOrErr) | ||
| return BinOrErr.takeError(); | ||
| std::unique_ptr<Binary> &Bin = BinOrErr.get(); | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be really helpful to have a summary of what a
BinaryObjectFileactually represents/how it represents it.