LLVM 23.0.0git
GsymCreatorV1.cpp
Go to the documentation of this file.
1//===- GsymCreatorV1.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
11
12#include <cassert>
13
14using namespace llvm;
15using namespace gsym;
16
17uint64_t GsymCreatorV1::calculateHeaderAndTableSize() const {
18 uint64_t Size = sizeof(Header);
19 const size_t NumFuncs = Funcs.size();
20 Size += NumFuncs * getAddressOffsetSize();
21 Size += NumFuncs * sizeof(uint32_t);
23 Size += StrTab.getSize();
24 return Size;
25}
26
28 std::lock_guard<std::mutex> Guard(Mutex);
29 std::optional<uint64_t> BaseAddress;
30 if (auto Err = validateForEncoding(BaseAddress))
31 return Err;
32 Header Hdr;
33 Hdr.Magic = GSYM_MAGIC;
36 Hdr.UUIDSize = static_cast<uint8_t>(UUID.size());
38 Hdr.NumAddresses = static_cast<uint32_t>(Funcs.size());
39 Hdr.StrtabOffset = 0;
40 Hdr.StrtabSize = 0;
41 memset(Hdr.UUID, 0, sizeof(Hdr.UUID));
42 if (UUID.size() > sizeof(Hdr.UUID))
43 return createStringError(std::errc::invalid_argument,
44 "invalid UUID size %u", (uint32_t)UUID.size());
45 if (UUID.size() > 0)
46 memcpy(Hdr.UUID, UUID.data(), UUID.size());
47 llvm::Error Err = Hdr.encode(O);
48 if (Err)
49 return Err;
50
51 O.setStringOffsetSize(getStringOffsetSize());
53
54 O.alignTo(4);
55 const uint64_t AddrInfoOffsetsOffset = O.tell();
56 for (size_t i = 0, n = Funcs.size(); i < n; ++i)
57 O.writeU32(0);
58
59 O.alignTo(4);
60 if (auto Err = encodeFileTable(O))
61 return Err;
62
63 const uint64_t StrtabOffset = O.tell();
64 StrTab.write(O.get_stream());
65 const uint64_t StrtabSize = O.tell() - StrtabOffset;
66 std::vector<uint32_t> AddrInfoOffsets;
67
68 if (StrtabSize > UINT32_MAX) {
69 return createStringError(std::errc::invalid_argument,
70 "string table size exceeded 32-bit max");
71 }
72
73 for (const auto &FuncInfo : Funcs) {
74 if (Expected<uint64_t> OffsetOrErr = FuncInfo.encode(O)) {
75 uint64_t Offset = OffsetOrErr.get();
76 if (Offset > UINT32_MAX) {
77 return createStringError(std::errc::invalid_argument,
78 "address info offset exceeded 32-bit max");
79 }
80 AddrInfoOffsets.push_back(Offset);
81 } else
82 return OffsetOrErr.takeError();
83 }
84 O.fixup32((uint32_t)StrtabOffset, offsetof(Header, StrtabOffset));
85 O.fixup32((uint32_t)StrtabSize, offsetof(Header, StrtabSize));
86
87 uint64_t Offset = 0;
88 for (auto AddrInfoOffset : AddrInfoOffsets) {
89 O.fixup32(AddrInfoOffset, AddrInfoOffsetsOffset + Offset);
90 Offset += 4;
91 }
92 return ErrorSuccess();
93}
#define offsetof(TYPE, MEMBER)
Subclass of Error for the sole purpose of identifying the success path in the type system.
Definition Error.h:334
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition FileWriter.h:30
LLVM_ABI llvm::Error encode(FileWriter &O) const override
Encode a GSYM into the file writer stream at the current position.
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.
std::optional< uint64_t > BaseAddress
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.
constexpr uint32_t GSYM_MAGIC
Definition Header.h:25
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition Error.h:1321
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
The GSYM header.
Definition Header.h:45
uint16_t Version
The version can number determines how the header is decoded and how each InfoType in FunctionInfo is ...
Definition Header.h:54
static constexpr uint32_t getVersion()
Return the version of this header.
Definition Header.h:89
uint8_t AddrOffSize
The size in bytes of each address offset in the address offsets table.
Definition Header.h:56
uint32_t Magic
The magic bytes should be set to GSYM_MAGIC.
Definition Header.h:49
LLVM_ABI llvm::Error encode(FileWriter &O) const
Encode this object into FileWriter stream.
Definition Header.cpp:85
uint32_t StrtabOffset
The file relative offset of the start of the string table for strings contained in the GSYM file.
Definition Header.h:72
uint8_t UUID[GSYM_MAX_UUID_SIZE]
The UUID of the original executable file.
Definition Header.h:86
uint32_t StrtabSize
The size in bytes of the string table.
Definition Header.h:80
uint8_t UUIDSize
The size in bytes of the UUID encoded in the "UUID" member.
Definition Header.h:58
uint32_t NumAddresses
The number of addresses stored in the address offsets table.
Definition Header.h:64
uint64_t BaseAddress
The 64 bit base address that all address offsets in the address offsets table are relative to.
Definition Header.h:62