1 | //===- Header.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/Header.h" |
10 | #include "llvm/DebugInfo/GSYM/FileWriter.h" |
11 | #include "llvm/Support/DataExtractor.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 | |
20 | using namespace llvm; |
21 | using namespace gsym; |
22 | |
23 | raw_ostream &llvm::gsym::(raw_ostream &OS, const Header &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 << " UUIDSize = " << HEX8(H.UUIDSize) << '\n'; |
29 | OS << " BaseAddress = " << HEX64(H.BaseAddress) << '\n'; |
30 | OS << " NumAddresses = " << HEX32(H.NumAddresses) << '\n'; |
31 | OS << " StrtabOffset = " << HEX32(H.StrtabOffset) << '\n'; |
32 | OS << " StrtabSize = " << HEX32(H.StrtabSize) << '\n'; |
33 | OS << " UUID = " ; |
34 | for (uint8_t I = 0; I < H.UUIDSize; ++I) |
35 | OS << format_hex_no_prefix(N: H.UUID[I], Width: 2); |
36 | OS << '\n'; |
37 | return OS; |
38 | } |
39 | |
40 | /// Check the header and detect any errors. |
41 | llvm::Error Header::() const { |
42 | if (Magic != GSYM_MAGIC) |
43 | return createStringError(EC: std::errc::invalid_argument, |
44 | Fmt: "invalid GSYM magic 0x%8.8x" , Vals: Magic); |
45 | if (Version != GSYM_VERSION) |
46 | return createStringError(EC: std::errc::invalid_argument, |
47 | Fmt: "unsupported GSYM version %u" , Vals: Version); |
48 | switch (AddrOffSize) { |
49 | case 1: break; |
50 | case 2: break; |
51 | case 4: break; |
52 | case 8: break; |
53 | default: |
54 | return createStringError(EC: std::errc::invalid_argument, |
55 | Fmt: "invalid address offset size %u" , |
56 | Vals: AddrOffSize); |
57 | } |
58 | if (UUIDSize > GSYM_MAX_UUID_SIZE) |
59 | return createStringError(EC: std::errc::invalid_argument, |
60 | Fmt: "invalid UUID size %u" , Vals: UUIDSize); |
61 | return Error::success(); |
62 | } |
63 | |
64 | llvm::Expected<Header> Header::(DataExtractor &Data) { |
65 | uint64_t Offset = 0; |
66 | // The header is stored as a single blob of data that has a fixed byte size. |
67 | if (!Data.isValidOffsetForDataOfSize(offset: Offset, length: sizeof(Header))) |
68 | return createStringError(EC: std::errc::invalid_argument, |
69 | Fmt: "not enough data for a gsym::Header" ); |
70 | Header H; |
71 | H.Magic = Data.getU32(offset_ptr: &Offset); |
72 | H.Version = Data.getU16(offset_ptr: &Offset); |
73 | H.AddrOffSize = Data.getU8(offset_ptr: &Offset); |
74 | H.UUIDSize = Data.getU8(offset_ptr: &Offset); |
75 | H.BaseAddress = Data.getU64(offset_ptr: &Offset); |
76 | H.NumAddresses = Data.getU32(offset_ptr: &Offset); |
77 | H.StrtabOffset = Data.getU32(offset_ptr: &Offset); |
78 | H.StrtabSize = Data.getU32(offset_ptr: &Offset); |
79 | Data.getU8(offset_ptr: &Offset, dst: H.UUID, count: GSYM_MAX_UUID_SIZE); |
80 | if (llvm::Error Err = H.checkForError()) |
81 | return std::move(Err); |
82 | return H; |
83 | } |
84 | |
85 | llvm::Error Header::(FileWriter &O) const { |
86 | // Users must verify the Header is valid prior to calling this funtion. |
87 | if (llvm::Error Err = checkForError()) |
88 | return Err; |
89 | O.writeU32(Value: Magic); |
90 | O.writeU16(Value: Version); |
91 | O.writeU8(Value: AddrOffSize); |
92 | O.writeU8(Value: UUIDSize); |
93 | O.writeU64(Value: BaseAddress); |
94 | O.writeU32(Value: NumAddresses); |
95 | O.writeU32(Value: StrtabOffset); |
96 | O.writeU32(Value: StrtabSize); |
97 | O.writeData(Data: llvm::ArrayRef<uint8_t>(UUID)); |
98 | return Error::success(); |
99 | } |
100 | |
101 | bool llvm::gsym::(const Header &LHS, const Header &RHS) { |
102 | return LHS.Magic == RHS.Magic && LHS.Version == RHS.Version && |
103 | LHS.AddrOffSize == RHS.AddrOffSize && LHS.UUIDSize == RHS.UUIDSize && |
104 | LHS.BaseAddress == RHS.BaseAddress && |
105 | LHS.NumAddresses == RHS.NumAddresses && |
106 | LHS.StrtabOffset == RHS.StrtabOffset && |
107 | LHS.StrtabSize == RHS.StrtabSize && |
108 | memcmp(s1: LHS.UUID, s2: RHS.UUID, n: LHS.UUIDSize) == 0; |
109 | } |
110 | |