Skip to content

[Object] Parsing and dumping of SFrame Frame Row Entries #151301

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 3 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
1 change: 1 addition & 0 deletions llvm/include/llvm/BinaryFormat/SFrame.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ LLVM_ABI ArrayRef<EnumEntry<FREType>> getFRETypes();
LLVM_ABI ArrayRef<EnumEntry<FDEType>> getFDETypes();
LLVM_ABI ArrayRef<EnumEntry<AArch64PAuthKey>> getAArch64PAuthKeys();
LLVM_ABI ArrayRef<EnumEntry<FREOffset>> getFREOffsets();
LLVM_ABI ArrayRef<EnumEntry<BaseReg>> getBaseRegisters();

} // namespace sframe
} // namespace llvm
Expand Down
51 changes: 51 additions & 0 deletions llvm/include/llvm/Object/SFrameParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define LLVM_OBJECT_SFRAME_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/fallible_iterator.h"
#include "llvm/BinaryFormat/SFrame.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
Expand All @@ -19,6 +20,8 @@ namespace llvm {
namespace object {

template <endianness E> class SFrameParser {
class FallibleFREIterator;

public:
static Expected<SFrameParser> create(ArrayRef<uint8_t> Contents,
uint64_t SectionAddress);
Expand All @@ -42,6 +45,21 @@ template <endianness E> class SFrameParser {
// objects returned by the `fdes()` function.
uint64_t getAbsoluteStartAddress(typename FDERange::iterator FDE) const;

struct FrameRowEntry {
uint32_t StartAddress;
sframe::FREInfo<endianness::native> Info;
SmallVector<int32_t, 3> Offsets;
};

using fre_iterator = fallible_iterator<FallibleFREIterator>;
iterator_range<fre_iterator> fres(const sframe::FuncDescEntry<E> &FDE,
Error &Err) const;

std::optional<int32_t> getCFAOffset(const FrameRowEntry &FRE) const;
std::optional<int32_t> getRAOffset(const FrameRowEntry &FRE) const;
std::optional<int32_t> getFPOffset(const FrameRowEntry &FRE) const;
ArrayRef<int32_t> getExtraOffsets(const FrameRowEntry &FRE) const;

private:
ArrayRef<uint8_t> Data;
uint64_t SectionAddress;
Expand All @@ -54,6 +72,39 @@ template <endianness E> class SFrameParser {
uint64_t getFDEBase() const {
return sizeof(Header) + Header.AuxHdrLen + Header.FDEOff;
}

uint64_t getFREBase() const {
return getFDEBase() + Header.NumFDEs * sizeof(sframe::FuncDescEntry<E>);
}
};

template <endianness E> class SFrameParser<E>::FallibleFREIterator {
public:
// NB: This iterator starts out in the before_begin() state. It must be
// ++'ed to reach the first element.
FallibleFREIterator(ArrayRef<uint8_t> Data, sframe::FREType FREType,
uint32_t Idx, uint32_t Size, uint64_t Offset)
: Data(Data), FREType(FREType), Idx(Idx), Size(Size), Offset(Offset) {}

Error inc();
const FrameRowEntry &operator*() const { return FRE; }

friend bool operator==(const FallibleFREIterator &LHS,
const FallibleFREIterator &RHS) {
assert(LHS.Data.data() == RHS.Data.data());
assert(LHS.Data.size() == RHS.Data.size());
assert(LHS.FREType == RHS.FREType);
assert(LHS.Size == RHS.Size);
return LHS.Idx == RHS.Idx;
}

private:
ArrayRef<uint8_t> Data;
sframe::FREType FREType;
uint32_t Idx;
uint32_t Size;
uint64_t Offset;
FrameRowEntry FRE;
};

extern template class LLVM_TEMPLATE_ABI SFrameParser<endianness::big>;
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/BinaryFormat/SFrame.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ ArrayRef<EnumEntry<sframe::FREOffset>> sframe::getFREOffsets() {
};
return ArrayRef(FREOffsets);
}

ArrayRef<EnumEntry<sframe::BaseReg>> sframe::getBaseRegisters() {
static constexpr EnumEntry<sframe::BaseReg> BaseRegs[] = {
{"FP", sframe::BaseReg::FP},
{"SP", sframe::BaseReg::SP},
};
return ArrayRef(BaseRegs);
}
Comment on lines +72 to +78
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 probably missing something, but why not just have the static local exposed as a constant without the function wrapper?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

If you mean something like:

// SFrame.h
extern ArrayRef<EnumEntry<sframe::BaseReg>> BaseRegisters;
// SFrame.cpp
static constexpr EnumEntry<sframe::BaseReg> BaseRegs[] = ...
ArrayRef<EnumEntry<sframe::BaseReg>> BaseRegisters = BaseRegs;

then I believe that should work (without creating new global constructors or anything). I did it this way because that's how this is implemented elsewhere (BinaryFormat/DXContainer.h, DebugInfo/CodeView/EnumTables.h)

132 changes: 128 additions & 4 deletions llvm/lib/Object/SFrameParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,25 @@ getDataSlice(ArrayRef<uint8_t> Data, uint64_t Offset, uint64_t Size) {
}

template <typename T>
static Expected<const T &> getDataSliceAs(ArrayRef<uint8_t> Data,
uint64_t Offset) {
static Expected<ArrayRef<T>>
getDataSliceAsArrayOf(ArrayRef<uint8_t> Data, uint64_t Offset, uint64_t Count) {
static_assert(std::is_trivial_v<T>);
Expected<ArrayRef<uint8_t>> Slice = getDataSlice(Data, Offset, sizeof(T));
Expected<ArrayRef<uint8_t>> Slice =
getDataSlice(Data, Offset, sizeof(T) * Count);
if (!Slice)
return Slice.takeError();

return *reinterpret_cast<const T *>(Slice->data());
return ArrayRef(reinterpret_cast<const T *>(Slice->data()), Count);
}

template <typename T>
static Expected<const T &> getDataSliceAs(ArrayRef<uint8_t> Data,
uint64_t Offset) {
Expected<ArrayRef<T>> Array = getDataSliceAsArrayOf<T>(Data, Offset, 1);
if (!Array)
return Array.takeError();

return Array->front();
}

template <endianness E>
Expand Down Expand Up @@ -100,6 +111,119 @@ uint64_t SFrameParser<E>::getAbsoluteStartAddress(
return Result;
}

template <typename EndianT>
static Error readArray(ArrayRef<uint8_t> Data, uint64_t Count, uint64_t &Offset,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why is this returning an Error rather than an Expected?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Mainly because it simplifies the caller, which would otherwise need to unpack the Expected and copy/move the value into the larger object. I wouldn't do that for a public interface, but these functions are essentially an implementation detail of FallibleFREIterator::inc(), so I thought that would be okay.

I can that if you feel strongly about it, but I think version looks better.

SmallVectorImpl<int32_t> &Vec) {
Expected<ArrayRef<EndianT>> RawArray =
getDataSliceAsArrayOf<EndianT>(Data, Offset, Count);
if (!RawArray)
return RawArray.takeError();
Offset += Count * sizeof(EndianT);
Vec.resize(Count);
llvm::copy(*RawArray, Vec.begin());
return Error::success();
}

template <typename T, endianness E>
static Error readFRE(ArrayRef<uint8_t> Data, uint64_t &Offset,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question.

typename SFrameParser<E>::FrameRowEntry &FRE) {
Expected<sframe::FrameRowEntry<T, E>> RawFRE =
getDataSliceAs<sframe::FrameRowEntry<T, E>>(Data, Offset);
if (!RawFRE)
return RawFRE.takeError();

Offset += sizeof(*RawFRE);
FRE.StartAddress = RawFRE->StartAddress;
FRE.Info.Info = RawFRE->Info.Info;

switch (FRE.Info.getOffsetSize()) {
case sframe::FREOffset::B1:
return readArray<sframe::detail::packed<int8_t, E>>(
Data, FRE.Info.getOffsetCount(), Offset, FRE.Offsets);
case sframe::FREOffset::B2:
return readArray<sframe::detail::packed<int16_t, E>>(
Data, FRE.Info.getOffsetCount(), Offset, FRE.Offsets);
case sframe::FREOffset::B4:
return readArray<sframe::detail::packed<int32_t, E>>(
Data, FRE.Info.getOffsetCount(), Offset, FRE.Offsets);
default:
return createError("unsupported/unknown offset size");
}
}

template <endianness E> Error SFrameParser<E>::FallibleFREIterator::inc() {
if (++Idx == Size)
return Error::success();

switch (FREType) {
case sframe::FREType::Addr1:
return readFRE<uint8_t, E>(Data, Offset, FRE);
case sframe::FREType::Addr2:
return readFRE<uint16_t, E>(Data, Offset, FRE);
case sframe::FREType::Addr4:
return readFRE<uint32_t, E>(Data, Offset, FRE);
default:
return createError("invalid/unsupported FRE type");
}
}

template <endianness E>
iterator_range<typename SFrameParser<E>::fre_iterator>
SFrameParser<E>::fres(const sframe::FuncDescEntry<E> &FDE, Error &Err) const {
uint64_t Offset = getFREBase() + FDE.StartFREOff;
fre_iterator BeforeBegin = make_fallible_itr(
FallibleFREIterator(Data, FDE.getFREType(), -1, FDE.NumFREs, Offset),
Err);
fre_iterator End = make_fallible_end(
FallibleFREIterator(Data, FDE.getFREType(), FDE.NumFREs, FDE.NumFREs,
/*Offset=*/0));
return {++BeforeBegin, End};
}

static std::optional<int32_t> getOffset(ArrayRef<int32_t> Offsets, size_t Idx) {
if (Offsets.size() > Idx)
return Offsets[Idx];
return std::nullopt;
}

// The interpretation of offsets is ABI-specific. The implementation of this and
// the following functions may need to be adjusted when adding support for a new
// ABI.
template <endianness E>
std::optional<int32_t>
SFrameParser<E>::getCFAOffset(const FrameRowEntry &FRE) const {
return getOffset(FRE.Offsets, 0);
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 a little concerned about this and the similar code in the following blocks, mostly because future ABIs/architectures won't necessarily have the same meanings for the different offsets. It's probably sufficient for now to add a comment or two highlighting that all supported ABIs use the specified offset.

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, I expect that the implementation of these functions will need to change when adding a new ABI. In fact, it looks like the recently updated specification adds support for the s390x architecture/ABI. The offset numbers don't really change, but their interpretation is a lot more complicated (mainly to account for the fact that FP/RA registers can be saved in another register.

That will most likely mean changing the interface (return value) of these functions as well.

I added a comment as requested. I will also look into adding s390x support to make the parser complete.

}

template <endianness E>
std::optional<int32_t>
SFrameParser<E>::getRAOffset(const FrameRowEntry &FRE) const {
if (usesFixedRAOffset())
return Header.CFAFixedRAOffset;
return getOffset(FRE.Offsets, 1);
}

template <endianness E>
std::optional<int32_t>
SFrameParser<E>::getFPOffset(const FrameRowEntry &FRE) const {
if (usesFixedFPOffset())
return Header.CFAFixedFPOffset;
return getOffset(FRE.Offsets, usesFixedRAOffset() ? 1 : 2);
}

template <endianness E>
ArrayRef<int32_t>
SFrameParser<E>::getExtraOffsets(const FrameRowEntry &FRE) const {
size_t UsedOffsets = 1; // CFA
if (!usesFixedRAOffset())
++UsedOffsets;
if (!usesFixedFPOffset())
++UsedOffsets;
if (FRE.Offsets.size() > UsedOffsets)
return ArrayRef(FRE.Offsets).drop_front(UsedOffsets);
return {};
}

template class LLVM_EXPORT_TEMPLATE llvm::object::SFrameParser<endianness::big>;
template class LLVM_EXPORT_TEMPLATE
llvm::object::SFrameParser<endianness::little>;
10 changes: 8 additions & 2 deletions llvm/test/tools/llvm-readobj/ELF/sframe-fde.test
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ Sections:
# CASE1-NEXT: }
# CASE1-NEXT: Repetitive block size: 0xDE
# CASE1-NEXT: Padding2: 0xAD
# CASE1-NEXT: FREs [
# CASE1-NEXT: ]
# CASE1-NEXT: }
# CASE1-NEXT: ]
# CASE1-NEXT:}
Expand Down Expand Up @@ -169,6 +171,8 @@ Sections:
# CASE1-NEXT: }
# CASE1-NEXT: Repetitive block size (unused): 0xDE
# CASE1-NEXT: Padding2: 0xAD
# CASE1-NEXT: FREs [
# CASE1-NEXT: ]
# CASE1-NEXT: }
# CASE1-NEXT: ]
# CASE1-NEXT:}
Expand Down Expand Up @@ -196,7 +200,7 @@ Sections:
0x00, 0xde, 0xad, 0x00, # Start Address
0x00, 0x00, 0x01, 0xbe, # Size
0x00, 0x00, 0x00, 0x10, # Start FRE Offset
0x00, 0x00, 0x00, 0x10, # Number of FREs
0x00, 0x00, 0x00, 0x00, # Number of FREs
0x02, 0xde, 0xad, 0x00, # Info, RepSize, Padding2
]
# CASE2-LABEL:SFrame section '.sframe' {
Expand All @@ -223,7 +227,7 @@ Sections:
# CASE2-NEXT: PC: 0xDEAD1C
# CASE2-NEXT: Size: 0x1BE
# CASE2-NEXT: Start FRE Offset: 0x10
# CASE2-NEXT: Num FREs: 16
# CASE2-NEXT: Num FREs: 0
# CASE2-NEXT: Info {
# CASE2-NEXT: FRE Type: Addr4 (0x2)
# CASE2-NEXT: FDE Type: PCInc (0x0)
Expand All @@ -232,6 +236,8 @@ Sections:
# CASE2-NEXT: }
# CASE2-NEXT: Repetitive block size (unused): 0xDE
# CASE2-NEXT: Padding2: 0xAD00
# CASE2-NEXT: FREs [
# CASE2-NEXT: ]
# CASE2-NEXT: }
# CASE2-NEXT: ]
# CASE2-NEXT:}
Loading