LLVM  10.0.0svn
FunctionInfo.cpp
Go to the documentation of this file.
1 //===- FunctionInfo.cpp ---------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
14 
15 using namespace llvm;
16 using namespace gsym;
17 
18 /// FunctionInfo information type that is used to encode the optional data
19 /// that is associated with a FunctionInfo object.
21  EndOfList = 0u,
24 };
25 
27  OS << '[' << HEX64(FI.Range.Start) << '-' << HEX64(FI.Range.End) << "): "
28  << "Name=" << HEX32(FI.Name) << '\n' << FI.OptLineTable << FI.Inline;
29  return OS;
30 }
31 
33  uint64_t BaseAddr) {
34  FunctionInfo FI;
35  FI.Range.Start = BaseAddr;
36  uint64_t Offset = 0;
37  if (!Data.isValidOffsetForDataOfSize(Offset, 4))
38  return createStringError(std::errc::io_error,
39  "0x%8.8" PRIx64 ": missing FunctionInfo Size", Offset);
40  FI.Range.End = FI.Range.Start + Data.getU32(&Offset);
41  if (!Data.isValidOffsetForDataOfSize(Offset, 4))
42  return createStringError(std::errc::io_error,
43  "0x%8.8" PRIx64 ": missing FunctionInfo Name", Offset);
44  FI.Name = Data.getU32(&Offset);
45  if (FI.Name == 0)
46  return createStringError(std::errc::io_error,
47  "0x%8.8" PRIx64 ": invalid FunctionInfo Name value 0x%8.8x",
48  Offset - 4, FI.Name);
49  bool Done = false;
50  while (!Done) {
51  if (!Data.isValidOffsetForDataOfSize(Offset, 4))
52  return createStringError(std::errc::io_error,
53  "0x%8.8" PRIx64 ": missing FunctionInfo InfoType value", Offset);
54  const uint32_t IT = Data.getU32(&Offset);
55  if (!Data.isValidOffsetForDataOfSize(Offset, 4))
56  return createStringError(std::errc::io_error,
57  "0x%8.8" PRIx64 ": missing FunctionInfo InfoType length", Offset);
58  const uint32_t InfoLength = Data.getU32(&Offset);
59  if (!Data.isValidOffsetForDataOfSize(Offset, InfoLength))
60  return createStringError(std::errc::io_error,
61  "0x%8.8" PRIx64 ": missing FunctionInfo data for InfoType %u",
62  Offset, IT);
63  DataExtractor InfoData(Data.getData().substr(Offset, InfoLength),
64  Data.isLittleEndian(),
65  Data.getAddressSize());
66  switch (IT) {
68  Done = true;
69  break;
70 
72  if (Expected<LineTable> LT = LineTable::decode(InfoData, BaseAddr))
73  FI.OptLineTable = std::move(LT.get());
74  else
75  return LT.takeError();
76  break;
77 
79  if (Expected<InlineInfo> II = InlineInfo::decode(InfoData, BaseAddr))
80  FI.Inline = std::move(II.get());
81  else
82  return II.takeError();
83  break;
84 
85  default:
86  return createStringError(std::errc::io_error,
87  "0x%8.8" PRIx64 ": unsupported InfoType %u",
88  Offset-8, IT);
89  }
90  Offset += InfoLength;
91  }
92  return std::move(FI);
93 }
94 
96  if (!isValid())
97  return createStringError(std::errc::invalid_argument,
98  "attempted to encode invalid FunctionInfo object");
99  // Align FunctionInfo data to a 4 byte alignment.
100  O.alignTo(4);
101  const uint64_t FuncInfoOffset = O.tell();
102  // Write the size in bytes of this function as a uint32_t. This can be zero
103  // if we just have a symbol from a symbol table and that symbol has no size.
104  O.writeU32(size());
105  // Write the name of this function as a uint32_t string table offset.
106  O.writeU32(Name);
107 
108  if (OptLineTable.hasValue()) {
110  // Write a uint32_t length as zero for now, we will fix this up after
111  // writing the LineTable out with the number of bytes that were written.
112  O.writeU32(0);
113  const auto StartOffset = O.tell();
114  llvm::Error err = OptLineTable->encode(O, Range.Start);
115  if (err)
116  return std::move(err);
117  const off_t Length = O.tell() - StartOffset;
118  if (Length > UINT32_MAX)
119  return createStringError(std::errc::invalid_argument,
120  "LineTable length is greater than UINT32_MAX");
121  // Fixup the size of the LineTable data with the correct size.
122  O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
123  }
124 
125  // Write out the inline function info if we have any and if it is valid.
126  if (Inline.hasValue()) {
128  // Write a uint32_t length as zero for now, we will fix this up after
129  // writing the LineTable out with the number of bytes that were written.
130  O.writeU32(0);
131  const auto StartOffset = O.tell();
132  llvm::Error err = Inline->encode(O, Range.Start);
133  if (err)
134  return std::move(err);
135  const off_t Length = O.tell() - StartOffset;
136  if (Length > UINT32_MAX)
137  return createStringError(std::errc::invalid_argument,
138  "InlineInfo length is greater than UINT32_MAX");
139  // Fixup the size of the InlineInfo data with the correct size.
140  O.fixup32(static_cast<uint32_t>(Length), StartOffset - 4);
141  }
142 
143  // Terminate the data chunks with and end of list with zero size
145  O.writeU32(0);
146  return FuncInfoOffset;
147 }
llvm::Optional< LineTable > OptLineTable
Definition: FunctionInfo.h:89
#define HEX64(v)
Definition: Range.h:20
InfoType
FunctionInfo information type that is used to encode the optional data that is associated with a Func...
This class represents lattice values for constants.
Definition: AllocatorList.h:23
Inline information stores the name of the inline function along with an array of address ranges...
Definition: InlineInfo.h:58
uint32_t getU32(uint64_t *offset_ptr, Error *Err=nullptr) const
Extract a uint32_t value from *offset_ptr.
StringRef getData() const
Get the data pointed to by this extractor.
Definition: DataExtractor.h:92
void alignTo(size_t Align)
Pad with zeroes at the current file position until the current file position matches the specified al...
Definition: FileWriter.cpp:71
llvm::Optional< InlineInfo > Inline
Definition: FunctionInfo.h:90
Function information in GSYM files encodes information for one contiguous address range...
Definition: FunctionInfo.h:86
void fixup32(uint32_t Value, uint64_t Offset)
Fixup a uint32_t value at the specified offset in the stream.
Definition: FileWriter.cpp:53
uint64_t size() const
Definition: FunctionInfo.h:145
void writeU32(uint32_t Value)
Write a single uint32_t value into the stream at the current file position.
Definition: FileWriter.cpp:43
Tagged union holding either a T or a Error.
Definition: yaml2obj.h:21
LLVM_NODISCARD StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:592
bool isLittleEndian() const
Get the endianness for this extractor.
Definition: DataExtractor.h:94
bool isValid() const
Query if a FunctionInfo object is valid.
Definition: FunctionInfo.h:115
uint8_t getAddressSize() const
Get the address size for this extractor.
Definition: DataExtractor.h:96
A simplified binary data writer class that doesn&#39;t require targets, target definitions, architectures, or require any other optional compile time libraries to be enabled via the build process.
Definition: FileWriter.h:29
static llvm::Expected< InlineInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an InlineInfo object from a binary data stream.
Definition: InlineInfo.cpp:116
static llvm::Expected< LineTable > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an LineTable object from a binary data stream.
Definition: LineTable.cpp:251
bool isValidOffsetForDataOfSize(uint64_t offset, uint64_t length) const
Test the availability of length bytes of data from offset.
static cl::opt< ITMode > IT(cl::desc("IT block support"), cl::Hidden, cl::init(DefaultIT), cl::ZeroOrMore, cl::values(clEnumValN(DefaultIT, "arm-default-it", "Generate IT block based on arch"), clEnumValN(RestrictedIT, "arm-restrict-it", "Disallow deprecated IT based on ARMv8"), clEnumValN(NoRestrictedIT, "arm-no-restrict-it", "Allow IT blocks based on ARMv7")))
static llvm::Expected< FunctionInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an object from a binary data stream.
uint32_t Name
String table offset in the string table.
Definition: FunctionInfo.h:88
llvm::Expected< uint64_t > encode(FileWriter &O) const
Encode this object into FileWriter stream.
uint64_t tell()
Return the current offset within the file.
Definition: FileWriter.cpp:67
#define HEX32(v)
Definition: Range.h:19
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:45
raw_ostream & operator<<(raw_ostream &OS, const FunctionInfo &R)
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1197