1 //===- FunctionInfo.cpp ---------------------------------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/GSYM/FunctionInfo.h"
10 #include "llvm/DebugInfo/GSYM/FileWriter.h"
11 #include "llvm/DebugInfo/GSYM/GsymReader.h"
12 #include "llvm/DebugInfo/GSYM/LineTable.h"
13 #include "llvm/DebugInfo/GSYM/InlineInfo.h"
14 #include "llvm/Support/DataExtractor.h"
19 /// FunctionInfo information type that is used to encode the optional data
20 /// that is associated with a FunctionInfo object.
21 enum InfoType
: uint32_t {
27 raw_ostream
&llvm::gsym::operator<<(raw_ostream
&OS
, const FunctionInfo
&FI
) {
28 OS
<< FI
.Range
<< ": " << "Name=" << HEX32(FI
.Name
) << '\n';
30 OS
<< FI
.OptLineTable
<< '\n';
32 OS
<< FI
.Inline
<< '\n';
36 llvm::Expected
<FunctionInfo
> FunctionInfo::decode(DataExtractor
&Data
,
39 FI
.Range
.Start
= BaseAddr
;
41 if (!Data
.isValidOffsetForDataOfSize(Offset
, 4))
42 return createStringError(std::errc::io_error
,
43 "0x%8.8" PRIx64
": missing FunctionInfo Size", Offset
);
44 FI
.Range
.End
= FI
.Range
.Start
+ Data
.getU32(&Offset
);
45 if (!Data
.isValidOffsetForDataOfSize(Offset
, 4))
46 return createStringError(std::errc::io_error
,
47 "0x%8.8" PRIx64
": missing FunctionInfo Name", Offset
);
48 FI
.Name
= Data
.getU32(&Offset
);
50 return createStringError(std::errc::io_error
,
51 "0x%8.8" PRIx64
": invalid FunctionInfo Name value 0x%8.8x",
55 if (!Data
.isValidOffsetForDataOfSize(Offset
, 4))
56 return createStringError(std::errc::io_error
,
57 "0x%8.8" PRIx64
": missing FunctionInfo InfoType value", Offset
);
58 const uint32_t IT
= Data
.getU32(&Offset
);
59 if (!Data
.isValidOffsetForDataOfSize(Offset
, 4))
60 return createStringError(std::errc::io_error
,
61 "0x%8.8" PRIx64
": missing FunctionInfo InfoType length", Offset
);
62 const uint32_t InfoLength
= Data
.getU32(&Offset
);
63 if (!Data
.isValidOffsetForDataOfSize(Offset
, InfoLength
))
64 return createStringError(std::errc::io_error
,
65 "0x%8.8" PRIx64
": missing FunctionInfo data for InfoType %u",
67 DataExtractor
InfoData(Data
.getData().substr(Offset
, InfoLength
),
68 Data
.isLittleEndian(),
69 Data
.getAddressSize());
71 case InfoType::EndOfList
:
75 case InfoType::LineTableInfo
:
76 if (Expected
<LineTable
> LT
= LineTable::decode(InfoData
, BaseAddr
))
77 FI
.OptLineTable
= std::move(LT
.get());
79 return LT
.takeError();
82 case InfoType::InlineInfo
:
83 if (Expected
<InlineInfo
> II
= InlineInfo::decode(InfoData
, BaseAddr
))
84 FI
.Inline
= std::move(II
.get());
86 return II
.takeError();
90 return createStringError(std::errc::io_error
,
91 "0x%8.8" PRIx64
": unsupported InfoType %u",
99 llvm::Expected
<uint64_t> FunctionInfo::encode(FileWriter
&O
) const {
101 return createStringError(std::errc::invalid_argument
,
102 "attempted to encode invalid FunctionInfo object");
103 // Align FunctionInfo data to a 4 byte alignment.
105 const uint64_t FuncInfoOffset
= O
.tell();
106 // Write the size in bytes of this function as a uint32_t. This can be zero
107 // if we just have a symbol from a symbol table and that symbol has no size.
109 // Write the name of this function as a uint32_t string table offset.
112 if (OptLineTable
.hasValue()) {
113 O
.writeU32(InfoType::LineTableInfo
);
114 // Write a uint32_t length as zero for now, we will fix this up after
115 // writing the LineTable out with the number of bytes that were written.
117 const auto StartOffset
= O
.tell();
118 llvm::Error err
= OptLineTable
->encode(O
, Range
.Start
);
120 return std::move(err
);
121 const auto Length
= O
.tell() - StartOffset
;
122 if (Length
> UINT32_MAX
)
123 return createStringError(std::errc::invalid_argument
,
124 "LineTable length is greater than UINT32_MAX");
125 // Fixup the size of the LineTable data with the correct size.
126 O
.fixup32(static_cast<uint32_t>(Length
), StartOffset
- 4);
129 // Write out the inline function info if we have any and if it is valid.
130 if (Inline
.hasValue()) {
131 O
.writeU32(InfoType::InlineInfo
);
132 // Write a uint32_t length as zero for now, we will fix this up after
133 // writing the LineTable out with the number of bytes that were written.
135 const auto StartOffset
= O
.tell();
136 llvm::Error err
= Inline
->encode(O
, Range
.Start
);
138 return std::move(err
);
139 const auto Length
= O
.tell() - StartOffset
;
140 if (Length
> UINT32_MAX
)
141 return createStringError(std::errc::invalid_argument
,
142 "InlineInfo length is greater than UINT32_MAX");
143 // Fixup the size of the InlineInfo data with the correct size.
144 O
.fixup32(static_cast<uint32_t>(Length
), StartOffset
- 4);
147 // Terminate the data chunks with and end of list with zero size
148 O
.writeU32(InfoType::EndOfList
);
150 return FuncInfoOffset
;
154 llvm::Expected
<LookupResult
> FunctionInfo::lookup(DataExtractor
&Data
,
155 const GsymReader
&GR
,
159 LR
.LookupAddr
= Addr
;
160 LR
.FuncRange
.Start
= FuncAddr
;
162 LR
.FuncRange
.End
= FuncAddr
+ Data
.getU32(&Offset
);
163 uint32_t NameOffset
= Data
.getU32(&Offset
);
164 // The "lookup" functions doesn't report errors as accurately as the "decode"
165 // function as it is meant to be fast. For more accurage errors we could call
167 if (!Data
.isValidOffset(Offset
))
168 return createStringError(std::errc::io_error
,
169 "FunctionInfo data is truncated");
170 // This function will be called with the result of a binary search of the
171 // address table, we must still make sure the address does not fall into a
172 // gap between functions are after the last function.
173 if (LR
.FuncRange
.size() > 0 && !LR
.FuncRange
.contains(Addr
))
174 return createStringError(std::errc::io_error
,
175 "address 0x%" PRIx64
" is not in GSYM", Addr
);
178 return createStringError(std::errc::io_error
,
179 "0x%8.8" PRIx64
": invalid FunctionInfo Name value 0x00000000",
181 LR
.FuncName
= GR
.getString(NameOffset
);
183 Optional
<LineEntry
> LineEntry
;
184 Optional
<DataExtractor
> InlineInfoData
;
186 if (!Data
.isValidOffsetForDataOfSize(Offset
, 8))
187 return createStringError(std::errc::io_error
,
188 "FunctionInfo data is truncated");
189 const uint32_t IT
= Data
.getU32(&Offset
);
190 const uint32_t InfoLength
= Data
.getU32(&Offset
);
191 const StringRef InfoBytes
= Data
.getData().substr(Offset
, InfoLength
);
192 if (InfoLength
!= InfoBytes
.size())
193 return createStringError(std::errc::io_error
,
194 "FunctionInfo data is truncated");
195 DataExtractor
InfoData(InfoBytes
, Data
.isLittleEndian(),
196 Data
.getAddressSize());
198 case InfoType::EndOfList
:
202 case InfoType::LineTableInfo
:
203 if (auto ExpectedLE
= LineTable::lookup(InfoData
, FuncAddr
, Addr
))
204 LineEntry
= ExpectedLE
.get();
206 return ExpectedLE
.takeError();
209 case InfoType::InlineInfo
:
210 // We will parse the inline info after our line table, but only if
211 // we have a line entry.
212 InlineInfoData
= InfoData
;
218 Offset
+= InfoLength
;
222 // We don't have a valid line entry for our address, fill in our source
223 // location as best we can and return.
224 SourceLocation SrcLoc
;
225 SrcLoc
.Name
= LR
.FuncName
;
226 SrcLoc
.Offset
= Addr
- FuncAddr
;
227 LR
.Locations
.push_back(SrcLoc
);
231 Optional
<FileEntry
> LineEntryFile
= GR
.getFile(LineEntry
->File
);
233 return createStringError(std::errc::invalid_argument
,
234 "failed to extract file[%" PRIu32
"]",
237 SourceLocation SrcLoc
;
238 SrcLoc
.Name
= LR
.FuncName
;
239 SrcLoc
.Offset
= Addr
- FuncAddr
;
240 SrcLoc
.Dir
= GR
.getString(LineEntryFile
->Dir
);
241 SrcLoc
.Base
= GR
.getString(LineEntryFile
->Base
);
242 SrcLoc
.Line
= LineEntry
->Line
;
243 LR
.Locations
.push_back(SrcLoc
);
244 // If we don't have inline information, we are done.
247 // We have inline information. Try to augment the lookup result with this
249 llvm::Error Err
= InlineInfo::lookup(GR
, *InlineInfoData
, FuncAddr
, Addr
,
252 return std::move(Err
);