1//===- HeaderV2.cpp ---------------------------------------------*- C++ -*-===//
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
9#include "llvm/DebugInfo/GSYM/HeaderV2.h"
10#include "llvm/DebugInfo/GSYM/FileWriter.h"
11#include "llvm/DebugInfo/GSYM/GsymDataExtractor.h"
12#include "llvm/Support/Format.h"
13#include "llvm/Support/raw_ostream.h"
14
15#define HEX8(v) llvm::format_hex(v, 4)
16#define HEX16(v) llvm::format_hex(v, 6)
17#define HEX32(v) llvm::format_hex(v, 10)
18#define HEX64(v) llvm::format_hex(v, 18)
19
20using namespace llvm;
21using namespace gsym;
22
23raw_ostream &llvm::gsym::operator<<(raw_ostream &OS, const HeaderV2 &H) {
24 OS << "Header:\n";
25 OS << " Magic = " << HEX32(H.Magic) << "\n";
26 OS << " Version = " << HEX16(H.Version) << '\n';
27 OS << " AddrOffSize = " << HEX8(H.AddrOffSize) << '\n';
28 OS << " StrTableEnc = " << HEX8(static_cast<uint8_t>(H.StrTableEncoding))
29 << '\n';
30 OS << " BaseAddress = " << HEX64(H.BaseAddress) << '\n';
31 OS << " NumAddresses = " << HEX32(H.NumAddresses) << '\n';
32 return OS;
33}
34
35llvm::Error HeaderV2::checkForError() const {
36 if (Magic != GSYM_MAGIC)
37 return createStringError(EC: std::errc::invalid_argument,
38 Fmt: "invalid GSYM magic 0x%8.8x", Vals: Magic);
39 if (Version != HeaderV2::getVersion())
40 return createStringError(EC: std::errc::invalid_argument,
41 Fmt: "unsupported GSYM version %u", Vals: Version);
42 if (AddrOffSize < 1 || AddrOffSize > 8)
43 return createStringError(EC: std::errc::invalid_argument,
44 Fmt: "invalid address offset size %u", Vals: AddrOffSize);
45 uint8_t Encoding = static_cast<uint8_t>(StrTableEncoding);
46 switch (Encoding) {
47 case static_cast<uint8_t>(StringTableEncoding::Default):
48 break;
49 default:
50 return createStringError(EC: std::errc::invalid_argument,
51 Fmt: "unsupported string table encoding %u", Vals: Encoding);
52 }
53 return Error::success();
54}
55
56llvm::Expected<HeaderV2> HeaderV2::decode(GsymDataExtractor &Data) {
57 uint64_t Offset = 0;
58 // The fixed portion of the HeaderV2 is 20 bytes:
59 // Magic(4) + Version(2) + AddrOffSize(1) + StrTableEncoding(1) +
60 // BaseAddress(8) + NumAddresses(4)
61 const uint64_t FixedHeaderSize = HeaderV2::getEncodedSize();
62 if (!Data.isValidOffsetForDataOfSize(offset: Offset, length: FixedHeaderSize))
63 return createStringError(EC: std::errc::invalid_argument,
64 Fmt: "not enough data for a gsym::HeaderV2");
65 HeaderV2 H;
66 H.Magic = Data.getU32(offset_ptr: &Offset);
67 H.Version = Data.getU16(offset_ptr: &Offset);
68 H.AddrOffSize = Data.getU8(offset_ptr: &Offset);
69 H.StrTableEncoding = static_cast<StringTableEncoding>(Data.getU8(offset_ptr: &Offset));
70 H.BaseAddress = Data.getU64(offset_ptr: &Offset);
71 H.NumAddresses = Data.getU32(offset_ptr: &Offset);
72 if (llvm::Error Err = H.checkForError())
73 return std::move(Err);
74 return H;
75}
76
77llvm::Error HeaderV2::encode(FileWriter &O) const {
78 if (llvm::Error Err = checkForError())
79 return Err;
80 O.writeU32(Value: Magic);
81 O.writeU16(Value: Version);
82 O.writeU8(Value: AddrOffSize);
83 O.writeU8(Value: static_cast<uint8_t>(StrTableEncoding));
84 O.writeU64(Value: BaseAddress);
85 O.writeU32(Value: NumAddresses);
86 return Error::success();
87}
88
89bool llvm::gsym::operator==(const HeaderV2 &LHS, const HeaderV2 &RHS) {
90 return LHS.Magic == RHS.Magic && LHS.Version == RHS.Version &&
91 LHS.AddrOffSize == RHS.AddrOffSize &&
92 LHS.StrTableEncoding == RHS.StrTableEncoding &&
93 LHS.BaseAddress == RHS.BaseAddress &&
94 LHS.NumAddresses == RHS.NumAddresses;
95}
96