26 GsymReader::GsymReader(std::unique_ptr<MemoryBuffer> Buffer) :
27 MemBuffer(
std::move(Buffer)),
30 GsymReader::GsymReader(
GsymReader &&RHS) =
default;
41 return create(BuffOrErr.
get());
46 return create(MemBuffer);
53 "invalid memory buffer");
57 return std::move(Err);
68 if (FileData.readObject(Hdr))
70 "not enough data for a GSYM header");
75 Endian = HostByteOrder;
80 Swap.reset(
new SwappedData);
92 Swap->Hdr = ExpectedHdr.get();
94 return ExpectedHdr.takeError();
101 if (
Error Err = Hdr->checkForError())
110 if (FileData.padToAlignment(Hdr->AddrOffSize) ||
111 FileData.readArray(AddrOffsets,
112 Hdr->NumAddresses * Hdr->AddrOffSize))
114 "failed to read address table");
117 if (FileData.padToAlignment(4) ||
118 FileData.readArray(AddrInfoOffsets, Hdr->NumAddresses))
120 "failed to read address info offsets table");
124 if (FileData.readInteger(NumFiles) || FileData.readArray(Files, NumFiles))
126 "failed to read file table");
129 FileData.setOffset(Hdr->StrtabOffset);
130 if (FileData.readFixedString(StrTab.Data, Hdr->StrtabSize))
132 "failed to read string table");
142 Swap->AddrOffsets.resize(Hdr->NumAddresses * Hdr->AddrOffSize);
143 switch (Hdr->AddrOffSize) {
145 if (!
Data.getU8(&Offset, Swap->AddrOffsets.data(), Hdr->NumAddresses))
147 "failed to read address table");
150 if (!
Data.getU16(&Offset,
151 reinterpret_cast<uint16_t *>(Swap->AddrOffsets.data()),
154 "failed to read address table");
157 if (!
Data.getU32(&Offset,
158 reinterpret_cast<uint32_t *>(Swap->AddrOffsets.data()),
161 "failed to read address table");
164 if (!
Data.getU64(&Offset,
165 reinterpret_cast<uint64_t *>(Swap->AddrOffsets.data()),
168 "failed to read address table");
174 Swap->AddrInfoOffsets.resize(Hdr->NumAddresses);
175 if (
Data.getU32(&Offset, Swap->AddrInfoOffsets.
data(), Hdr->NumAddresses))
179 "failed to read address table");
183 Swap->Files.resize(NumFiles);
184 if (
Data.getU32(&Offset, &Swap->Files[0].Dir, NumFiles*2))
188 "failed to read file table");
191 StrTab.Data = MemBuffer->getBuffer().substr(Hdr->StrtabOffset,
193 if (StrTab.Data.empty())
195 "failed to read string table");
210 switch (Hdr->AddrOffSize) {
211 case 1:
return addressForIndex<uint8_t>(
Index);
212 case 2:
return addressForIndex<uint16_t>(
Index);
213 case 4:
return addressForIndex<uint32_t>(
Index);
214 case 8:
return addressForIndex<uint64_t>(
Index);
220 const auto NumAddrInfoOffsets = AddrInfoOffsets.size();
221 if (Index < NumAddrInfoOffsets)
222 return AddrInfoOffsets[
Index];
228 if (Addr < Hdr->BaseAddress)
230 "address 0x%" PRIx64
" not in GSYM", Addr);
231 const uint64_t AddrOffset = Addr - Hdr->BaseAddress;
232 switch (Hdr->AddrOffSize) {
233 case 1:
return getAddressOffsetIndex<uint8_t>(AddrOffset);
234 case 2:
return getAddressOffsetIndex<uint16_t>(AddrOffset);
235 case 4:
return getAddressOffsetIndex<uint32_t>(AddrOffset);
236 case 8:
return getAddressOffsetIndex<uint64_t>(AddrOffset);
240 "unsupported address offset size %u",
249 assert(*AddressIndex < AddrInfoOffsets.size());
250 auto AddrInfoOffset = AddrInfoOffsets[*AddressIndex];
255 if (ExpectedFI->Range.contains(Addr) || ExpectedFI->Range.size() == 0)
258 "address 0x%" PRIx64
" not in GSYM", Addr);
262 "failed to extract address[%" PRIu64
"]",
271 assert(*AddressIndex < AddrInfoOffsets.size());
272 auto AddrInfoOffset = AddrInfoOffsets[*AddressIndex];
277 "failed to extract address[%" PRIu64
"]",
GsymReader is used to read GSYM data from a file or buffer.
static llvm::Expected< GsymReader > copyBuffer(StringRef Bytes)
Construct a GsymReader from a buffer.
Represents either an error or a value T.
const Header & getHeader() const
Access the GSYM header.
This class represents lattice values for constants.
Error takeError()
Take ownership of the stored error.
constexpr bool IsBigEndianHost
static llvm::Expected< LookupResult > lookup(DataExtractor &Data, const GsymReader &GR, uint64_t FuncAddr, uint64_t Addr)
Lookup an address within a FunctionInfo object's data stream.
constexpr uint32_t GSYM_CIGAM
Tagged union holding either a T or a Error.
llvm::Expected< FunctionInfo > getFunctionInfo(uint64_t Addr) const
Get the full function info for an address.
Expected< uint64_t > getAddressIndex(const uint64_t Addr) const
Given an address, find the address index.
Optional< uint64_t > getAddress(size_t Index) const
Gets an address from the address table.
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
std::error_code getError() const
static llvm::Expected< GsymReader > openFile(StringRef Path)
Construct a GsymReader from a file on disk.
static ErrorSuccess success()
Create a success value.
llvm::Expected< LookupResult > lookup(uint64_t Addr) const
Lookup an address in the a GSYM.
static llvm::Expected< llvm::gsym::GsymReader > create(std::unique_ptr< MemoryBuffer > &MemBuffer)
Create a GSYM from a memory buffer.
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
constexpr endianness system_endianness()
uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
static llvm::Expected< FunctionInfo > decode(DataExtractor &Data, uint64_t BaseAddr)
Decode an object from a binary data stream.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
constexpr uint32_t GSYM_MAGIC
Provides read only access to a subclass of BinaryStream.
StringRef - Represent a constant reference to a string, i.e.
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Optional< uint64_t > getAddressInfoOffset(size_t Index) const
Given an address index, get the offset for the FunctionInfo.