1#pragma once
2
3#include <elf.h>
4
5#include <fstream>
6#include <iomanip>
7#include <iostream>
8#include <sstream>
9#include <vector>
10
11class nullstream : public std::ostream {
12public:
13 nullstream() : std::ostream(nullptr) {}
14};
15inline nullstream null;
16
17#define CHECK(x) \
18 if (!(x)) { \
19 std::cerr << "CHECK failed: " << #x << std::endl; \
20 exit(1); \
21 }
22#define CHECK_GT(x, y) CHECK((x) > (y))
23#define CHECK_LT(x, y) CHECK((x) < (y))
24#define CHECK_EQ(x, y) CHECK((x) == (y))
25#define CHECK_NE(x, y) CHECK((x) != (y))
26#define CHECK_GE(x, y) CHECK((x) >= (y))
27#define CHECK_LE(x, y) CHECK((x) <= (y))
28/* Replace null with std::cout to enable logging */
29#define LOG(loglevel) \
30 null << #loglevel << " " << __FILE__ << ":" << __LINE__ << " " \
31 << " "
32
33#define LOG_KEY_VALUE(key, value) " " << key << "=" << value
34#define LOG_KEY(key) LOG_KEY_VALUE(#key, key)
35#define LOG_64BITS(key) LOG_KEY_VALUE(#key, HexString(key, 16))
36#define LOG_32BITS(key) LOG_KEY_VALUE(#key, HexString(key, 8))
37#define LOG_16BITS(key) LOG_KEY_VALUE(#key, HexString(key, 4))
38#define LOG_8BITS(key) LOG_KEY_VALUE(#key, HexString(key, 2))
39#define LOG_BITS(key) LOG_KEY_VALUE(#key, HexString(key))
40#define LOG_DWEHPE(type) LOG_KEY_VALUE(#type, ShowDW_EH_PE(type))
41
42template <class T>
43std::string HexString(T* num, int length = -1) {
44 uint64_t n = reinterpret_cast<uint64_t>(num);
45 if (length == -1) {
46 length = 16;
47 }
48 std::stringstream ss;
49 ss << "0x" << std::uppercase << std::setfill('0') << std::setw(length) << std::hex << n;
50 return ss.str();
51}
52
53template <class T>
54std::string HexString(const T* num, int length = -1) {
55 return HexString(const_cast<T*>(num), length);
56}
57
58template <class T>
59std::string HexString(T num, int length = -1) {
60 if (length == -1) {
61 length = sizeof(T) / 2;
62 }
63 std::stringstream ss;
64 ss << "0x" << std::uppercase << std::setfill('0') << std::setw(length) << std::hex << +num;
65 return ss.str();
66}
67
68std::vector<std::string> SplitWith(std::string str, const std::string& delimiter);
69
70std::string ShowRela(const Elf64_Rela& r);
71
72std::string ShowSym(const Elf64_Sym& s, const char* strtab);
73
74std::string ShowRelocationType(int type);
75