-
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
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 |
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); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//===- 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("invalid magic number"); | ||
if (Preamble->Version != sframe::Version::V2) | ||
return createError("invalid/unsupported version number"); | ||
|
||
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>; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
## Check parsing and dumping of the SFrame header. | ||
# RUN: yaml2obj --docnum=1 %s -o %t.1 | ||
jh7370 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# RUN: llvm-readobj --sframe=.sframe_1b --sframe=.sframe_bad_magic \ | ||
# RUN: --sframe=.sframe_bad_version --sframe=.sframe_6b \ | ||
# RUN: --sframe=.sframe_header %t.1 2>&1 \ | ||
# RUN: | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=CASE1 | ||
|
||
## Check big-endian support and the handling of --sframe argument default. | ||
# RUN: yaml2obj --docnum=2 %s -o %t.2 | ||
# RUN: llvm-readobj --sframe %t.2 2>&1 \ | ||
# RUN: | FileCheck %s --strict-whitespace --match-full-lines --check-prefix=CASE2 | ||
|
||
--- !ELF | ||
FileHeader: | ||
Class: ELFCLASS64 | ||
Data: ELFDATA2LSB | ||
Type: ET_EXEC | ||
Machine: EM_X86_64 | ||
Sections: | ||
- Name: .sframe_1b | ||
Type: SHT_PROGBITS | ||
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. A new section type for SFrame SHT_GNU_SFRAME with value 0x6ffffff4 has been added (https://sourceware.org/git/?p=gnu-gabi.git;a=commit;h=b85391cf2ad97ec209b7cfe84b24c65ad5748576). GNU Binutils 2.45 and later will generate SFrame sections with that section type. Please have SHT_GNU_SFRAME as the section type on the LLVM side too. 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. Thanks for letting me know. I've created #148803 for that. For the moment, I'm keeping the PROGBITS here. I'll change that once the other PR lands (the occurrence in this test is not particularly important, it's the generator that matters). |
||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ 0x00 ] | ||
# CASE1-LABEL:SFrame section '.sframe_1b' { | ||
# CASE1:{{.*}}: warning: {{.*}}: unexpected end of data at offset 0x1 while reading [0x0, 0x4) | ||
|
||
- Name: .sframe_bad_magic | ||
Type: SHT_PROGBITS | ||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ 0xde, 0xad, 0xbe, 0xef] | ||
# CASE1-LABEL:SFrame section '.sframe_bad_magic' { | ||
# CASE1:{{.*}}: warning: {{.*}}: invalid magic number | ||
|
||
- Name: .sframe_bad_version | ||
Type: SHT_PROGBITS | ||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ | ||
0xe2, 0xde, 0x01, 0x00 # Preamble (magic, version, flags) | ||
] | ||
# CASE1-LABEL:SFrame section '.sframe_bad_version' { | ||
# CASE1:{{.*}}: warning: {{.*}}: invalid/unsupported version number | ||
|
||
- Name: .sframe_6b | ||
Type: SHT_PROGBITS | ||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ | ||
0xe2, 0xde, 0x02, 0x00, # Preamble (magic, version, flags) | ||
0x01, 0x02 | ||
] | ||
|
||
# CASE1-LABEL:SFrame section '.sframe_6b' { | ||
# CASE1:{{.*}}: warning: {{.*}}: unexpected end of data at offset 0x6 while reading [0x0, 0x1c) | ||
|
||
- Name: .sframe_header | ||
Type: SHT_PROGBITS | ||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ | ||
0xe2, 0xde, 0x02, 0x06, # Preamble (magic, version, flags) | ||
# Header: | ||
0x03, 0x42, 0x47, 0x00, # ABI, Fixed FP offset, Fixed RA Offset, AUX header length | ||
0x01, 0x00, 0x00, 0x00, # Number of FDEs | ||
0x10, 0x00, 0x00, 0x00, # Number of FREs | ||
0x00, 0x10, 0x00, 0x00, # FRE length | ||
0x04, 0x00, 0x00, 0x00, # FDE offset | ||
0x00, 0x01, 0x00, 0x00, # FRE offset | ||
] | ||
# CASE1-LABEL:SFrame section '.sframe_header' { | ||
# CASE1: Header { | ||
# CASE1-NEXT: Magic: 0xDEE2 | ||
# CASE1-NEXT: Version: V2 (0x2) | ||
# CASE1-NEXT: Flags [ (0x6) | ||
# CASE1-NEXT: FDEFuncStartPCRel (0x4){{ *}} | ||
# CASE1-NEXT: FramePointer (0x2){{ *}} | ||
# CASE1-NEXT: ] | ||
# CASE1-NEXT: ABI: AMD64EndianLittle (0x3) | ||
# CASE1-NEXT: CFA fixed FP offset (unused): 66 | ||
# CASE1-NEXT: CFA fixed RA offset: 71 | ||
# CASE1-NEXT: Auxiliary header length: 0 | ||
# CASE1-NEXT: Num FDEs: 1 | ||
# CASE1-NEXT: Num FREs: 16 | ||
# CASE1-NEXT: FRE subsection length: 4096 | ||
# CASE1-NEXT: FDE subsection offset: 4 | ||
# CASE1-NEXT: FRE subsection offset: 256 | ||
# CASE1-NEXT: } | ||
# CASE1-NEXT:} | ||
|
||
--- !ELF | ||
FileHeader: | ||
Class: ELFCLASS64 | ||
Data: ELFDATA2MSB | ||
Type: ET_EXEC | ||
Machine: EM_AARCH64 | ||
Sections: | ||
- Name: .sframe | ||
Type: SHT_PROGBITS | ||
Flags: [ SHF_ALLOC ] | ||
ContentArray: [ | ||
0xde, 0xe2, 0x02, 0x01, # Preamble (magic, version, flags) | ||
# Header: | ||
0x01, 0x42, 0x47, 0x00, # ABI, Fixed FP offset, Fixed RA Offset, AUX header length | ||
0x00, 0x00, 0x00, 0x01, # Number of FDEs | ||
0x00, 0x00, 0x00, 0x10, # Number of FREs | ||
0x00, 0x00, 0x10, 0x00, # FRE length | ||
0x00, 0x00, 0x00, 0x04, # FDE offset | ||
0x00, 0x00, 0x01, 0x00, # FRE offset | ||
] | ||
# CASE2-LABEL:SFrame section '.sframe' { | ||
# CASE2: Header { | ||
# CASE2-NEXT: Magic: 0xDEE2 | ||
# CASE2-NEXT: Version: V2 (0x2) | ||
# CASE2-NEXT: Flags [ (0x1) | ||
# CASE2-NEXT: FDESorted (0x1){{ *}} | ||
# CASE2-NEXT: ] | ||
# CASE2-NEXT: ABI: AArch64EndianBig (0x1) | ||
# CASE2-NEXT: CFA fixed FP offset (unused): 66 | ||
# CASE2-NEXT: CFA fixed RA offset (unused): 71 | ||
# CASE2-NEXT: Auxiliary header length: 0 | ||
# CASE2-NEXT: Num FDEs: 1 | ||
# CASE2-NEXT: Num FREs: 16 | ||
# CASE2-NEXT: FRE subsection length: 4096 | ||
# CASE2-NEXT: FDE subsection offset: 4 | ||
# CASE2-NEXT: FRE subsection offset: 256 | ||
# CASE2-NEXT: } | ||
# CASE2-NEXT:} |
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.