-
Notifications
You must be signed in to change notification settings - Fork 14.5k
[Object] Beginnings of SFrame parser and dumper #147294
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
labath
wants to merge
7
commits into
llvm:main
Choose a base branch
from
labath:sframe2
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.
+420
−13
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0d71f2a
[Object] Beginnings of SFrame parser and dumper
labath 8fe4918
format
labath 254b5c9
Merge remote-tracking branch 'origin/main' into sframe2
labath 3fde4de
Merge remote-tracking branch 'origin/main' into sframe2
labath 54a956b
- add tests for corrupted elf headers
labath bd1d045
improve warnings
labath ed64615
more fixes
labath 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,39 @@ | ||
//===- SFrameConstants.def --------------------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if !(defined(HANDLE_SFRAME_VERSION) || defined(HANDLE_SFRAME_FLAG) || \ | ||
defined(HANDLE_SFRAME_ABI)) | ||
#error "Missing HANDLE_SFRAME definition" | ||
#endif | ||
|
||
#ifndef HANDLE_SFRAME_VERSION | ||
#define HANDLE_SFRAME_VERSION(CODE, NAME) | ||
#endif | ||
|
||
#ifndef HANDLE_SFRAME_FLAG | ||
#define HANDLE_SFRAME_FLAG(CODE, NAME) | ||
#endif | ||
|
||
#ifndef HANDLE_SFRAME_ABI | ||
#define HANDLE_SFRAME_ABI(CODE, NAME) | ||
#endif | ||
|
||
HANDLE_SFRAME_VERSION(0x01, V1) | ||
HANDLE_SFRAME_VERSION(0x02, V2) | ||
|
||
HANDLE_SFRAME_FLAG(0x01, FDESorted) | ||
HANDLE_SFRAME_FLAG(0x02, FramePointer) | ||
HANDLE_SFRAME_FLAG(0x04, FDEFuncStartPCRel) | ||
|
||
HANDLE_SFRAME_ABI(0x01, AArch64EndianBig) | ||
HANDLE_SFRAME_ABI(0x02, AArch64EndianLittle) | ||
HANDLE_SFRAME_ABI(0x03, AMD64EndianLittle) | ||
|
||
#undef HANDLE_SFRAME_VERSION | ||
#undef HANDLE_SFRAME_FLAG | ||
#undef HANDLE_SFRAME_ABI |
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,48 @@ | ||
//===- SFrameParser.h -------------------------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_OBJECT_SFRAME_H | ||
#define LLVM_OBJECT_SFRAME_H | ||
|
||
#include "llvm/ADT/ArrayRef.h" | ||
#include "llvm/BinaryFormat/SFrame.h" | ||
#include "llvm/Support/Error.h" | ||
#include <cstdint> | ||
|
||
namespace llvm { | ||
namespace object { | ||
|
||
template <endianness E> class SFrameParser { | ||
public: | ||
static Expected<SFrameParser> create(ArrayRef<uint8_t> Contents); | ||
|
||
const sframe::Preamble<E> &getPreamble() const { return Header.Preamble; } | ||
const sframe::Header<E> &getHeader() const { return Header; } | ||
|
||
bool usesFixedRAOffset() const { | ||
return getHeader().ABIArch == sframe::ABI::AMD64EndianLittle; | ||
} | ||
bool usesFixedFPOffset() const { | ||
return false; // Not used in any currently defined ABI. | ||
} | ||
|
||
private: | ||
ArrayRef<uint8_t> Data; | ||
const sframe::Header<E> &Header; | ||
|
||
SFrameParser(ArrayRef<uint8_t> Data, const sframe::Header<E> &Header) | ||
: Data(Data), Header(Header) {} | ||
}; | ||
|
||
extern template class SFrameParser<endianness::big>; | ||
extern template class SFrameParser<endianness::little>; | ||
|
||
} // end namespace object | ||
} // end namespace llvm | ||
|
||
#endif // LLVM_OBJECT_SFRAME_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 |
---|---|---|
@@ -0,0 +1,37 @@ | ||
//===-- SFrame.cpp -----------------------------------------------*- 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/BinaryFormat/SFrame.h" | ||
#include "llvm/Support/ScopedPrinter.h" | ||
|
||
using namespace llvm; | ||
|
||
ArrayRef<EnumEntry<sframe::Version>> sframe::getVersions() { | ||
static constexpr EnumEntry<Version> Versions[] = { | ||
#define HANDLE_SFRAME_VERSION(CODE, NAME) {#NAME, sframe::Version::NAME}, | ||
#include "llvm/BinaryFormat/SFrameConstants.def" | ||
}; | ||
|
||
return ArrayRef(Versions); | ||
} | ||
|
||
ArrayRef<EnumEntry<sframe::Flags>> sframe::getFlags() { | ||
static constexpr EnumEntry<sframe::Flags> Flags[] = { | ||
#define HANDLE_SFRAME_FLAG(CODE, NAME) {#NAME, sframe::Flags::NAME}, | ||
#include "llvm/BinaryFormat/SFrameConstants.def" | ||
}; | ||
return ArrayRef(Flags); | ||
} | ||
|
||
ArrayRef<EnumEntry<sframe::ABI>> sframe::getABIs() { | ||
static constexpr EnumEntry<sframe::ABI> ABIs[] = { | ||
#define HANDLE_SFRAME_ABI(CODE, NAME) {#NAME, sframe::ABI::NAME}, | ||
#include "llvm/BinaryFormat/SFrameConstants.def" | ||
}; | ||
return ArrayRef(ABIs); | ||
} |
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,55 @@ | ||
//===- SFrameParser.cpp ---------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/Object/SFrameParser.h" | ||
#include "llvm/BinaryFormat/SFrame.h" | ||
#include "llvm/Object/Error.h" | ||
#include "llvm/Support/FormatVariadic.h" | ||
|
||
using namespace llvm; | ||
using namespace llvm::object; | ||
|
||
template <typename T> | ||
static Expected<const T &> getDataSliceAs(ArrayRef<uint8_t> Data, | ||
uint64_t Offset) { | ||
static_assert(std::is_trivial_v<T>); | ||
if (Data.size() < Offset + sizeof(T)) { | ||
return createStringError( | ||
formatv("unexpected end of data at offset {0:x} while reading [{1:x}, " | ||
"{2:x})", | ||
Data.size(), Offset, Offset + sizeof(T)) | ||
.str(), | ||
object_error::unexpected_eof); | ||
} | ||
return *reinterpret_cast<const T *>(Data.data() + Offset); | ||
} | ||
|
||
template <endianness E> | ||
Expected<SFrameParser<E>> SFrameParser<E>::create(ArrayRef<uint8_t> Contents) { | ||
Expected<const sframe::Preamble<E> &> Preamble = | ||
getDataSliceAs<sframe::Preamble<E>>(Contents, 0); | ||
if (!Preamble) | ||
return Preamble.takeError(); | ||
|
||
if (Preamble->Magic != sframe::Magic) | ||
return createError( | ||
formatv("invalid magic number ({0:x+4})", Preamble->Magic.value())); | ||
if (Preamble->Version != sframe::Version::V2) | ||
return createError( | ||
formatv("invalid/unsupported version number ({0})", | ||
static_cast<unsigned>(Preamble->Version.value()))); | ||
|
||
Expected<const sframe::Header<E> &> Header = | ||
getDataSliceAs<sframe::Header<E>>(Contents, 0); | ||
if (!Header) | ||
return Header.takeError(); | ||
return SFrameParser(Contents, *Header); | ||
} | ||
|
||
template class llvm::object::SFrameParser<endianness::big>; | ||
template class llvm::object::SFrameParser<endianness::little>; |
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.
Aside: I'm looking forward to C++26 when we can have reflection do this. Only need to wait until about 2036 for it to be available in LLVM!
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.
Yeah, definitely. I'm actually contemplating removing this and hardcoding everything in c++. All of the enums here have at most three cases, which means that the .def file adds more boilerplate (the preprocessor goo) than it removes.
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'm happy to go with whichever approach you prefer, as long as it's easily maintainable (i.e. isn't likely to fall over/get forgotten if we e.g. introduce a new enum value).
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.
The last part is the tricky one. I don't see a way to ensure a new enumerator isn't added without the corresponding stringifier without introducing more boilerplate. I guess I'm going to stick with the .def file.
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.
Something involving an "end" entry in the enum combined with a static assert verifying the value hasn't increased might do the trick, but it feels a bit suboptimal.