LLVM  14.0.0git
Header.cpp
Go to the documentation of this file.
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 
12 #include "llvm/Support/Format.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 
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(H.UUID[I], 2);
36  OS << '\n';
37  return OS;
38 }
39 
40 /// Check the header and detect any errors.
42  if (Magic != GSYM_MAGIC)
43  return createStringError(std::errc::invalid_argument,
44  "invalid GSYM magic 0x%8.8x", Magic);
45  if (Version != GSYM_VERSION)
46  return createStringError(std::errc::invalid_argument,
47  "unsupported GSYM version %u", 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(std::errc::invalid_argument,
55  "invalid address offset size %u",
56  AddrOffSize);
57  }
59  return createStringError(std::errc::invalid_argument,
60  "invalid UUID size %u", UUIDSize);
61  return Error::success();
62 }
63 
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, sizeof(Header)))
68  return createStringError(std::errc::invalid_argument,
69  "not enough data for a gsym::Header");
70  Header H;
71  H.Magic = Data.getU32(&Offset);
72  H.Version = Data.getU16(&Offset);
73  H.AddrOffSize = Data.getU8(&Offset);
74  H.UUIDSize = Data.getU8(&Offset);
75  H.BaseAddress = Data.getU64(&Offset);
76  H.NumAddresses = Data.getU32(&Offset);
77  H.StrtabOffset = Data.getU32(&Offset);
78  H.StrtabSize = Data.getU32(&Offset);
79  Data.getU8(&Offset, H.UUID, GSYM_MAX_UUID_SIZE);
80  if (llvm::Error Err = H.checkForError())
81  return std::move(Err);
82  return H;
83 }
84 
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(Magic);
90  O.writeU16(Version);
91  O.writeU8(AddrOffSize);
92  O.writeU8(UUIDSize);
93  O.writeU64(BaseAddress);
94  O.writeU32(NumAddresses);
95  O.writeU32(StrtabOffset);
96  O.writeU32(StrtabSize);
97  O.writeData(llvm::ArrayRef<uint8_t>(UUID));
98  return Error::success();
99 }
100 
101 bool llvm::gsym::operator==(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(LHS.UUID, RHS.UUID, LHS.UUIDSize) == 0;
109 }
HEX8
#define HEX8(v)
Definition: Header.cpp:15
llvm::gsym::Header::UUIDSize
uint8_t UUIDSize
The size in bytes of the UUID encoded in the "UUID" member.
Definition: Header.h:58
llvm
---------------------— PointerInfo ------------------------------------—
Definition: AllocatorList.h:23
llvm::gsym::Header::BaseAddress
uint64_t BaseAddress
The 64 bit base address that all address offsets in the address offsets table are relative to.
Definition: Header.h:62
llvm::gsym::Header
The GSYM header.
Definition: Header.h:45
llvm::Error::success
static ErrorSuccess success()
Create a success value.
Definition: Error.h:331
memcmp
Merge contiguous icmps into a memcmp
Definition: MergeICmps.cpp:912
llvm::gsym::GSYM_MAX_UUID_SIZE
constexpr size_t GSYM_MAX_UUID_SIZE
Definition: Header.h:27
Offset
uint64_t Offset
Definition: ELFObjHandler.cpp:81
llvm::Expected
Tagged union holding either a T or a Error.
Definition: APFloat.h:42
Format.h
llvm::format_hex_no_prefix
FormattedNumber format_hex_no_prefix(uint64_t N, unsigned Width, bool Upper=false)
format_hex_no_prefix - Output N as a fixed width hexadecimal.
Definition: Format.h:199
llvm::Data
@ Data
Definition: SIMachineScheduler.h:56
HEX16
#define HEX16(v)
Definition: Header.cpp:16
llvm::gsym::operator==
bool operator==(const FunctionInfo &LHS, const FunctionInfo &RHS)
Definition: FunctionInfo.h:188
llvm::gsym::GSYM_MAGIC
constexpr uint32_t GSYM_MAGIC
Definition: Header.h:24
llvm::gsym::operator<<
raw_ostream & operator<<(raw_ostream &OS, const FunctionInfo &R)
Definition: FunctionInfo.cpp:27
llvm::gsym::Header::encode
llvm::Error encode(FileWriter &O) const
Encode this object into FileWriter stream.
Definition: Header.cpp:85
llvm::gsym::GSYM_VERSION
constexpr uint32_t GSYM_VERSION
Definition: Header.h:26
llvm::gsym::Header::Magic
uint32_t Magic
The magic bytes should be set to GSYM_MAGIC.
Definition: Header.h:49
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::gsym::Header::StrtabSize
uint32_t StrtabSize
The size in bytes of the string table.
Definition: Header.h:80
llvm::RISCVFenceField::O
@ O
Definition: RISCVBaseInfo.h:179
HEX32
#define HEX32(v)
Definition: Header.cpp:17
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::gsym::Header::UUID
uint8_t UUID[GSYM_MAX_UUID_SIZE]
The UUID of the original executable file.
Definition: Header.h:86
FileWriter.h
llvm::gsym::FileWriter
A simplified binary data writer class that doesn't require targets, target definitions,...
Definition: FileWriter.h:29
UUID
std::pair< llvm::MachO::Target, std::string > UUID
Definition: TextStubCommon.h:23
llvm::ArrayRef< uint8_t >
llvm::gsym::Header::NumAddresses
uint32_t NumAddresses
The number of addresses stored in the address offsets table.
Definition: Header.h:64
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1231
H
#define H(x, y, z)
Definition: MD5.cpp:58
llvm::gsym::Header::decode
static llvm::Expected< Header > decode(DataExtractor &Data)
Decode an object from a binary data stream.
Definition: Header.cpp:64
llvm::Error
Lightweight error class with error context and mandatory checking.
Definition: Error.h:157
DataExtractor.h
llvm::gsym::Header::StrtabOffset
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
llvm::DataExtractor
Definition: DataExtractor.h:41
llvm::gsym::Header::Version
uint16_t Version
The version can number determines how the header is decoded and how each InfoType in FunctionInfo is ...
Definition: Header.h:54
HEX64
#define HEX64(v)
Definition: Header.cpp:18
Header.h
raw_ostream.h
llvm::gsym::Header::checkForError
llvm::Error checkForError() const
Check if a header is valid and return an error if anything is wrong.
Definition: Header.cpp:41
llvm::gsym::Header::AddrOffSize
uint8_t AddrOffSize
The size in bytes of each address offset in the address offsets table.
Definition: Header.h:56