aboutsummaryrefslogtreecommitdiff
path: root/llvm/include/llvm/Object/SFrameParser.h
blob: 23298357191b3fa0885b93e558d13b123de2d4f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//===- 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/ADT/fallible_iterator.h"
#include "llvm/BinaryFormat/SFrame.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Error.h"
#include <cstdint>

namespace llvm {
namespace object {

template <endianness E> class SFrameParser {
  class FallibleFREIterator;

public:
  static Expected<SFrameParser> create(ArrayRef<uint8_t> Contents,
                                       uint64_t SectionAddress);

  const sframe::Preamble<E> &getPreamble() const { return Header.Preamble; }
  const sframe::Header<E> &getHeader() const { return Header; }

  Expected<ArrayRef<uint8_t>> getAuxHeader() const;

  bool usesFixedRAOffset() const {
    return getHeader().ABIArch == sframe::ABI::AMD64EndianLittle;
  }
  bool usesFixedFPOffset() const {
    return false; // Not used in any currently defined ABI.
  }

  using FDERange = ArrayRef<sframe::FuncDescEntry<E>>;
  Expected<FDERange> fdes() const;

  // Decodes the start address of the given FDE, which must be one of the
  // objects returned by the `fdes()` function.
  uint64_t getAbsoluteStartAddress(typename FDERange::iterator FDE) const;

  // Returns the offset (in the SFrame section) of the given FDE, which must be
  // one of the objects returned by the `fdes()` function.
  uint64_t offsetOf(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;
  const sframe::Header<E> &Header;

  SFrameParser(ArrayRef<uint8_t> Data, uint64_t SectionAddress,
               const sframe::Header<E> &Header)
      : Data(Data), SectionAddress(SectionAddress), Header(Header) {}

  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) {}

  LLVM_ABI 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>;
extern template class LLVM_TEMPLATE_ABI SFrameParser<endianness::little>;

} // end namespace object
} // end namespace llvm

#endif // LLVM_OBJECT_SFRAME_H