Skip to content

[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
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
28 changes: 18 additions & 10 deletions llvm/include/llvm/BinaryFormat/SFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,36 @@
#ifndef LLVM_BINARYFORMAT_SFRAME_H
#define LLVM_BINARYFORMAT_SFRAME_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/BitmaskEnum.h"
#include "llvm/Support/DataTypes.h"
#include "llvm/Support/Endian.h"

namespace llvm::sframe {
namespace llvm {

template <typename T> struct EnumEntry;

namespace sframe {

LLVM_ENABLE_BITMASK_ENUMS_IN_NAMESPACE();

constexpr uint16_t Magic = 0xdee2;

enum class Version : uint8_t {
V1 = 1,
V2 = 2,
#define HANDLE_SFRAME_VERSION(CODE, NAME) NAME = CODE,
Copy link
Collaborator

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!

Copy link
Collaborator Author

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.

Copy link
Collaborator

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).

Copy link
Collaborator Author

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.

Copy link
Collaborator

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.

#include "llvm/BinaryFormat/SFrameConstants.def"
};

enum class Flags : uint8_t {
FDESorted = 0x01,
FramePointer = 0x02,
FDEFuncStartPCRel = 0x04,
#define HANDLE_SFRAME_FLAG(CODE, NAME) NAME = CODE,
#include "llvm/BinaryFormat/SFrameConstants.def"
V2AllFlags = FDESorted | FramePointer | FDEFuncStartPCRel,
LLVM_MARK_AS_BITMASK_ENUM(/*LargestValue=*/0xff),
};

enum class ABI : uint8_t {
AArch64EndianBig = 1,
AArch64EndianLittle = 2,
AMD64EndianLittle = 3,
#define HANDLE_SFRAME_ABI(CODE, NAME) NAME = CODE,
#include "llvm/BinaryFormat/SFrameConstants.def"
};

/// SFrame FRE Types. Bits 0-3 of FuncDescEntry.Info.
Expand Down Expand Up @@ -160,6 +163,11 @@ template <endianness E> using FrameRowEntryAddr1 = FrameRowEntry<uint8_t, E>;
template <endianness E> using FrameRowEntryAddr2 = FrameRowEntry<uint16_t, E>;
template <endianness E> using FrameRowEntryAddr4 = FrameRowEntry<uint32_t, E>;

} // namespace llvm::sframe
ArrayRef<EnumEntry<Version>> getVersions();
ArrayRef<EnumEntry<Flags>> getFlags();
ArrayRef<EnumEntry<ABI>> getABIs();

} // namespace sframe
} // namespace llvm

#endif // LLVM_BINARYFORMAT_SFRAME_H
39 changes: 39 additions & 0 deletions llvm/include/llvm/BinaryFormat/SFrameConstants.def
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
48 changes: 48 additions & 0 deletions llvm/include/llvm/Object/SFrameParser.h
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
1 change: 1 addition & 0 deletions llvm/lib/BinaryFormat/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ add_llvm_component_library(LLVMBinaryFormat
MsgPackDocumentYAML.cpp
MsgPackReader.cpp
MsgPackWriter.cpp
SFrame.cpp
Wasm.cpp
XCOFF.cpp

Expand Down
37 changes: 37 additions & 0 deletions llvm/lib/BinaryFormat/SFrame.cpp
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);
}
1 change: 1 addition & 0 deletions llvm/lib/Object/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ add_llvm_component_library(LLVMObject
OffloadBundle.cpp
RecordStreamer.cpp
RelocationResolver.cpp
SFrameParser.cpp
SymbolicFile.cpp
SymbolSize.cpp
TapiFile.cpp
Expand Down
55 changes: 55 additions & 0 deletions llvm/lib/Object/SFrameParser.cpp
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>;
Loading