LLVM 23.0.0git
GsymCreatorV2.cpp
Go to the documentation of this file.
1//===- GsymCreatorV2.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
13
14#include <cassert>
15
16using namespace llvm;
17using namespace gsym;
18
19uint64_t GsymCreatorV2::calculateHeaderAndTableSize() const {
20 const uint64_t HeaderSize = HeaderV2::getEncodedSize();
21 const size_t NumFuncs = Funcs.size();
22 const uint32_t NumEntries = 5 + (UUID.empty() ? 0 : 1) + 1;
23 uint64_t Size = HeaderSize + NumEntries * 20;
25 Size += NumFuncs * getAddressOffsetSize();
30 Size += StrTab.getSize();
31 Size += UUID.size();
32 return Size;
33}
34
35/// For V2 file layout, see HeaderV2.h
37 std::lock_guard<std::mutex> Guard(Mutex);
38 std::optional<uint64_t> BaseAddr;
39 if (auto Err = validateForEncoding(BaseAddr))
40 return Err;
41
42 const uint8_t AddrOffSize = getAddressOffsetSize();
43
44 // Pre-encode all FunctionInfo objects into a temporary buffer so we know the
45 // total FunctionInfo section size and each function's offset within it.
47 raw_svector_ostream FIOS(FIBuf);
48 FileWriter FIFW(FIOS, O.getByteOrder());
50 std::vector<uint64_t> FIRelativeOffsets;
51 for (const auto &FI : Funcs) {
52 if (auto OffOrErr = FI.encode(FIFW))
53 FIRelativeOffsets.push_back(*OffOrErr);
54 else
55 return OffOrErr.takeError();
56 }
57 const uint64_t FISectionSize = FIBuf.size();
58 const uint64_t StringTableSize = StrTab.getSize();
59
60 const uint8_t StrpSize = 8;
61
62 const bool HasUUID = !UUID.empty();
63 const uint32_t NumGlobalDataEntries = 5 + (HasUUID ? 1 : 0) + 1;
64 const uint64_t GlobalDataArraySize =
65 static_cast<uint64_t>(NumGlobalDataEntries) * 20;
66
67 const uint64_t HeaderSize = HeaderV2::getEncodedSize();
68 uint64_t CurOffset = HeaderSize + GlobalDataArraySize;
69
70 // UUID section (first, no alignment requirement).
71 const uint64_t UUIDOffset = CurOffset;
72 const uint64_t UUIDSectionSize = UUID.size();
73 if (HasUUID)
74 CurOffset += UUIDSectionSize;
75
76 // AddrOffsets section.
77 CurOffset = llvm::alignTo(CurOffset, AddrOffSize);
78 const uint64_t AddrOffsetsOffset = CurOffset;
79 const uint64_t AddrOffsetsSize = Funcs.size() * AddrOffSize;
80 CurOffset += AddrOffsetsSize;
81
82 // AddrInfoOffsets section.
83 const uint8_t AddrInfoOffSize = 8;
84 CurOffset = llvm::alignTo(CurOffset, AddrInfoOffSize);
85 const uint64_t AddrInfoOffsetsOffset = CurOffset;
86 const uint64_t AddrInfoOffsetsSize = Funcs.size() * AddrInfoOffSize;
87 CurOffset += AddrInfoOffsetsSize;
88
89 // FileTable section.
90 CurOffset = llvm::alignTo(CurOffset, 4);
91 const uint64_t FileTableOffset = CurOffset;
92 const uint64_t FileTableSize =
93 4 + Files.size() * FileEntry::getEncodedSize(StrpSize);
94 CurOffset += FileTableSize;
95
96 // StringTable section.
97 const uint64_t StringTableOffset = CurOffset;
98 CurOffset += StringTableSize;
99
100 // FunctionInfo section.
101 CurOffset = llvm::alignTo(CurOffset, 4);
102 const uint64_t FISectionOffset = CurOffset;
103 CurOffset += FISectionSize;
104
105 // Build and write the header.
106 HeaderV2 Hdr;
107 Hdr.Magic = GSYM_MAGIC;
109 Hdr.BaseAddress = *BaseAddr;
110 Hdr.NumAddresses = static_cast<uint32_t>(Funcs.size());
111 Hdr.AddrOffSize = AddrOffSize;
113 if (auto Err = Hdr.encode(O))
114 return Err;
115
116 // Write GlobalData entries.
117 if (HasUUID)
118 GlobalData{GlobalInfoType::UUID, UUIDOffset, UUIDSectionSize}.encode(O);
119 GlobalData{GlobalInfoType::AddrOffsets, AddrOffsetsOffset, AddrOffsetsSize}
120 .encode(O);
121 GlobalData{GlobalInfoType::AddrInfoOffsets, AddrInfoOffsetsOffset,
122 AddrInfoOffsetsSize}
123 .encode(O);
124 GlobalData{GlobalInfoType::FileTable, FileTableOffset, FileTableSize}.encode(
125 O);
126 GlobalData{GlobalInfoType::StringTable, StringTableOffset, StringTableSize}
127 .encode(O);
128 GlobalData{GlobalInfoType::FunctionInfo, FISectionOffset, FISectionSize}
129 .encode(O);
131
132 // Write UUID section.
133 if (HasUUID) {
134 assert(O.tell() == UUIDOffset);
135 O.writeData(ArrayRef<uint8_t>(UUID.data(), UUID.size()));
136 }
137
138 // Write AddrOffsets section.
139 O.alignTo(AddrOffSize);
140 assert(O.tell() == AddrOffsetsOffset);
141 encodeAddrOffsets(O, AddrOffSize, *BaseAddr);
142
143 // Write AddrInfoOffsets section. Values are relative to FunctionInfo section.
144 O.alignTo(AddrInfoOffSize);
145 assert(O.tell() == AddrInfoOffsetsOffset);
146 for (uint64_t RelOff : FIRelativeOffsets)
147 O.writeU64(RelOff);
148
149 // Write FileTable section.
150 O.alignTo(4);
151 assert(O.tell() == FileTableOffset);
152 if (auto Err = encodeFileTable(O))
153 return Err;
154
155 // Write StringTable section.
156 assert(O.tell() == StringTableOffset);
157 StrTab.write(O.get_stream());
158
159 // Write FunctionInfo section.
160 O.alignTo(4);
161 assert(O.tell() == FISectionOffset);
162 O.writeData(ArrayRef<uint8_t>(reinterpret_cast<const uint8_t *>(FIBuf.data()),
163 FIBuf.size()));
164
165 return Error::success();
166}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
static ErrorSuccess success()
Create a success value.
Definition Error.h:336
pointer data()
Return a pointer to the vector's buffer, even if empty().
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition FileWriter.h:30
void setStringOffsetSize(uint8_t Size)
Set the string offset size for this writer.
Definition FileWriter.h:139
LLVM_ABI llvm::Error encode(FileWriter &O) const override
For V2 file layout, see HeaderV2.h.
uint8_t getStringOffsetSize() const override
Get the size in bytes needed for encoding string offsets.
std::vector< llvm::gsym::FileEntry > Files
llvm::Error validateForEncoding(std::optional< uint64_t > &BaseAddr) const
Validate that the creator is ready for encoding.
llvm::Error encodeFileTable(FileWriter &O) const
Write the file table to the output stream.
StringTableBuilder StrTab
std::vector< uint8_t > UUID
std::vector< FunctionInfo > Funcs
void encodeAddrOffsets(FileWriter &O, uint8_t AddrOffSize, uint64_t BaseAddr) const
Write the address offsets table to the output stream.
uint8_t getAddressOffsetSize() const
Get the size of an address offset in the address offset table.
A raw_ostream that writes to an SmallVector or SmallString.
@ HeaderSize
Definition BTF.h:61
constexpr uint32_t GSYM_MAGIC
Definition Header.h:25
@ Default
A list of NULL-terminated strings (same as V1).
Definition HeaderV2.h:30
This is an optimization pass for GlobalISel generic memory operations.
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
Definition Alignment.h:144
static constexpr uint64_t getEncodedSize(uint8_t StringOffsetSize)
Returns the on-disk encoded size of a FileEntry for the given string offset size.
Definition FileEntry.h:38
GlobalData describes a section of data in a GSYM file by its type, file offset, and size.
Definition GlobalData.h:61
LLVM_ABI void encode(FileWriter &O) const
Encode this GlobalData entry into a FileWriter stream.
The GSYM V2 header.
Definition HeaderV2.h:60
static constexpr uint32_t getVersion()
Return the version of this header.
Definition HeaderV2.h:83
static constexpr uint64_t getEncodedSize()
Return the on-disk encoded size of the header in bytes.
Definition HeaderV2.h:87
uint64_t BaseAddress
The 64 bit base address that all address offsets in the address offsets table are relative to.
Definition HeaderV2.h:77
uint32_t Magic
The magic bytes should be set to GSYM_MAGIC.
Definition HeaderV2.h:64
static constexpr uint8_t getAddressInfoOffsetSize()
Return the size in bytes of address info offsets.
Definition HeaderV2.h:90
uint32_t NumAddresses
The number of addresses stored in the address offsets table and the address info offsets table.
Definition HeaderV2.h:80
uint16_t Version
The version number determines how the header is decoded.
Definition HeaderV2.h:69
StringTableEncoding StrTableEncoding
String table encoding. Allows for future encoding for string table.
Definition HeaderV2.h:73
uint8_t AddrOffSize
The size in bytes of each address offset in the address offsets table.
Definition HeaderV2.h:71
LLVM_ABI llvm::Error encode(FileWriter &O) const
Encode this object into FileWriter stream.
Definition HeaderV2.cpp:77