blob: f705306bd6ca7b32137e8665123f78ed12d36301 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
#include <cstdint>
#include <ore/BinaryFile.h>
#include <ore/BitUtils.h>
namespace ore {
bool BinaryFileHeader::IsValid(s64 magic_, int ver_major_, int ver_minor_, int ver_patch_,
int ver_sub_) const {
bool valid = true;
valid &= int(ver_major) == ver_major_ && int(ver_minor) == ver_minor_ &&
magic == magic_ & int(ver_patch) <= ver_patch_;
valid &= IsEndianReverse() || IsEndianValid();
valid &= IsAlignmentValid();
return valid;
}
bool BinaryFileHeader::IsSignatureValid(s64 magic_) const {
return magic == magic_;
}
bool BinaryFileHeader::IsVersionValid(int major, int minor, int patch, int sub) const {
if (int(ver_major) != major)
return false;
if (int(ver_minor) != minor)
return false;
if (int(ver_patch) > patch)
return false;
return true;
}
bool BinaryFileHeader::IsEndianReverse() const {
return bom == s16(0xFFFE);
}
bool BinaryFileHeader::IsEndianValid() const {
return bom == s16(0xFEFF);
}
bool BinaryFileHeader::IsAlignmentValid() const {
return (std::uintptr_t(this) & (GetAlignment() - 1)) == 0;
}
int BinaryFileHeader::GetAlignment() const {
return 1 << alignment;
}
static constexpr u32 FlagRelocated = 1 << 0;
bool BinaryFileHeader::IsRelocated() const {
return relocation_flags & FlagRelocated;
}
void BinaryFileHeader::SetRelocated(bool relocated) {
if (relocated)
relocation_flags |= FlagRelocated;
else
relocation_flags &= ~FlagRelocated;
}
void BinaryFileHeader::SetByteOrderMark() {
bom = s16(0xFEFF);
}
int BinaryFileHeader::GetFileSize() const {
return file_size;
}
void BinaryFileHeader::SetFileSize(int size) {
file_size = size;
}
void BinaryFileHeader::SetAlignment(int alignment_) {
alignment = CountTrailingZeros(u32(alignment_));
}
StringView BinaryFileHeader::GetFileName() const {
StringView name;
if (file_name_offset != 0)
name = reinterpret_cast<const char*>(this) + file_name_offset;
return name;
}
void BinaryFileHeader::SetFileName(const StringView& name) {
if (name.empty()) {
file_name_offset = 0;
} else {
file_name_offset = int(intptr_t(name.data()) - intptr_t(this));
#ifdef MATCHING_HACK_NX_CLANG
asm("");
#endif
}
}
RelocationTable* BinaryFileHeader::GetRelocationTable() {
if (relocation_table_offset == 0)
return nullptr;
return reinterpret_cast<RelocationTable*>(reinterpret_cast<char*>(this) +
relocation_table_offset);
}
void BinaryFileHeader::SetRelocationTable(RelocationTable* table) {
if (table == nullptr) {
relocation_table_offset = 0;
} else {
relocation_table_offset = int(intptr_t(table) - intptr_t(this));
#ifdef MATCHING_HACK_NX_CLANG
asm("");
#endif
}
}
BinaryBlockHeader* BinaryFileHeader::GetFirstBlock() {
if (first_block_offset == 0)
return nullptr;
return reinterpret_cast<BinaryBlockHeader*>(reinterpret_cast<char*>(this) + first_block_offset);
}
const BinaryBlockHeader* BinaryFileHeader::GetFirstBlock() const {
if (first_block_offset == 0)
return nullptr;
return reinterpret_cast<const BinaryBlockHeader*>(reinterpret_cast<const char*>(this) +
first_block_offset);
}
BinaryBlockHeader* BinaryFileHeader::FindFirstBlock(int type) {
auto* block = GetFirstBlock();
if (!block || block->magic == type)
return block;
return block->FindNextBlock(type);
}
const BinaryBlockHeader* BinaryFileHeader::FindFirstBlock(int type) const {
auto* block = GetFirstBlock();
if (!block || block->magic == type)
return block;
return block->FindNextBlock(type);
}
void BinaryFileHeader::SetFirstBlock(BinaryBlockHeader* block) {
if (block == nullptr)
first_block_offset = 0;
else
first_block_offset = int(intptr_t(block) - intptr_t(this));
}
BinaryBlockHeader* BinaryBlockHeader::FindNextBlock(int type) {
auto* block = this;
do
block = block->GetNextBlock();
while (block && block->magic != type);
return block;
}
const BinaryBlockHeader* BinaryBlockHeader::FindNextBlock(int type) const {
auto* block = this;
do
block = block->GetNextBlock();
while (block && block->magic != type);
return block;
}
BinaryBlockHeader* BinaryBlockHeader::GetNextBlock() {
if (next_block_offset == 0)
return nullptr;
return reinterpret_cast<BinaryBlockHeader*>(reinterpret_cast<char*>(this) + next_block_offset);
}
const BinaryBlockHeader* BinaryBlockHeader::GetNextBlock() const {
if (next_block_offset == 0)
return nullptr;
return reinterpret_cast<const BinaryBlockHeader*>(reinterpret_cast<const char*>(this) +
next_block_offset);
}
void BinaryBlockHeader::SetNextBlock(BinaryBlockHeader* block) {
if (block == nullptr)
next_block_offset = 0;
else
next_block_offset = int(intptr_t(block) - intptr_t(this));
}
} // namespace ore
|