Skip to content

Commit 44daaa8

Browse files
committed
fix macho support.
1 parent 1f5b132 commit 44daaa8

File tree

5 files changed

+36
-29
lines changed

5 files changed

+36
-29
lines changed

src/abi/itanium/ItaniumVTable.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ JSON VTable::toJson() const {
1717
});
1818
}
1919
return JSON{
20-
{"sub_tables", subTables },
21-
{"type_name", mTypeName.has_value() ? *mTypeName : nullptr}
20+
{"sub_tables", subTables },
21+
{"type_name", mTypeName.has_value() ? JSON(*mTypeName) : JSON{}}
2222
};
2323
}
2424

src/abi/itanium/ItaniumVTableReader.cpp

+27-25
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,27 @@ ItaniumVTableReader::ItaniumVTableReader(const std::shared_ptr<Executable>& imag
2020

2121
void ItaniumVTableReader::_initFormatConstants() {
2222
if (dynamic_cast<format::ELF*>(mImage.get())) {
23-
_constant.SEGMENT_DATA = ".data.rel.ro";
24-
_constant.SEGMENT_TEXT = ".text";
25-
_constant.PREFIX_VTABLE = "_ZTV";
26-
_constant.PREFIX_TYPEINFO = "_ZTI";
27-
_constant.SYM_CLASS_INFO = "_ZTVN10__cxxabiv117__class_type_infoE";
28-
_constant.SYM_SI_CLASS_INFO = "_ZTVN10__cxxabiv120__si_class_type_infoE";
29-
_constant.SYM_VMI_CLASS_INFO = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
30-
_constant.SYM_PURE_VFN = "__cxa_pure_virtual";
23+
_constant.SEGMENT_DATA = ".data.rel.ro";
24+
_constant.SEGMENT_READONLY_DATA = ".rodata";
25+
_constant.SEGMENT_TEXT = ".text";
26+
_constant.PREFIX_VTABLE = "_ZTV";
27+
_constant.PREFIX_TYPEINFO = "_ZTI";
28+
_constant.SYM_CLASS_INFO = "_ZTVN10__cxxabiv117__class_type_infoE";
29+
_constant.SYM_SI_CLASS_INFO = "_ZTVN10__cxxabiv120__si_class_type_infoE";
30+
_constant.SYM_VMI_CLASS_INFO = "_ZTVN10__cxxabiv121__vmi_class_type_infoE";
31+
_constant.SYM_PURE_VFN = "__cxa_pure_virtual";
3132
return;
3233
}
3334
if (dynamic_cast<format::MachO*>(mImage.get())) {
34-
_constant.SEGMENT_DATA = "__const";
35-
_constant.SEGMENT_TEXT = "__text";
36-
_constant.PREFIX_VTABLE = "_ZTV"; // internal
37-
_constant.PREFIX_TYPEINFO = "_ZTI";
38-
_constant.SYM_CLASS_INFO = "__ZTVN10__cxxabiv117__class_type_infoE";
39-
_constant.SYM_SI_CLASS_INFO = "__ZTVN10__cxxabiv120__si_class_type_infoE";
40-
_constant.SYM_VMI_CLASS_INFO = "__ZTVN10__cxxabiv121__vmi_class_type_infoE";
41-
_constant.SYM_PURE_VFN = "___cxa_pure_virtual";
35+
_constant.SEGMENT_DATA = "__const";
36+
_constant.SEGMENT_READONLY_DATA = "__const";
37+
_constant.SEGMENT_TEXT = "__text";
38+
_constant.PREFIX_VTABLE = "_ZTV"; // internal
39+
_constant.PREFIX_TYPEINFO = "_ZTI";
40+
_constant.SYM_CLASS_INFO = "__ZTVN10__cxxabiv117__class_type_infoE";
41+
_constant.SYM_SI_CLASS_INFO = "__ZTVN10__cxxabiv120__si_class_type_infoE";
42+
_constant.SYM_VMI_CLASS_INFO = "__ZTVN10__cxxabiv121__vmi_class_type_infoE";
43+
_constant.SYM_PURE_VFN = "___cxa_pure_virtual";
4244
return;
4345
}
4446
}
@@ -93,7 +95,7 @@ DumpVFTableResult ItaniumVTableReader::dumpVFTable() {
9395
std::string ItaniumVTableReader::_readZTS() {
9496
auto value = mImage->read<intptr_t>();
9597
// spdlog::debug("\tReading ZTS at {:#x}", value);
96-
if (!mImage->isInSection(value, _constant.SEGMENT_DATA)) return {};
98+
if (!mImage->isInSection(value, _constant.SEGMENT_READONLY_DATA)) return {};
9799
auto str = mImage->readCString(value, 2048);
98100
return str.empty() ? str : _constant.PREFIX_TYPEINFO + str;
99101
}
@@ -147,15 +149,15 @@ std::optional<VTable> ItaniumVTableReader::readVTable() {
147149
}
148150
// read: TypeInfo
149151
type = _readZTI();
150-
if (!type.starts_with(_constant.PREFIX_TYPEINFO)) {
151-
spdlog::warn(
152-
"Failed to reading vtable at {:#x} in {}. [INVALID_TYPEINFO]",
153-
mImage->last(),
154-
symbol.has_value() ? *symbol : "<unknown>"
155-
);
156-
return std::nullopt;
157-
}
158152
if (!type.empty()) {
153+
if (!type.starts_with(_constant.PREFIX_TYPEINFO)) {
154+
spdlog::warn(
155+
"Failed to reading vtable at {:#x} in {}. [INVALID_TYPEINFO]",
156+
mImage->last(),
157+
symbol.has_value() ? *symbol : "<unknown>"
158+
);
159+
return std::nullopt;
160+
}
159161
if (!symbol) symbol = _constant.PREFIX_VTABLE + StringRemovePrefix(type, _constant.PREFIX_TYPEINFO);
160162
result.mTypeName = type;
161163
}

src/abi/itanium/ItaniumVTableReader.h

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class ItaniumVTableReader {
5151
struct FormatConstants {
5252
std::string SEGMENT_TEXT;
5353
std::string SEGMENT_DATA;
54+
std::string SEGMENT_READONLY_DATA;
5455
std::string PREFIX_VTABLE;
5556
std::string PREFIX_TYPEINFO;
5657
std::string SYM_CLASS_INFO;

src/format/ELF.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ void ELF::_relocateReadonlyData() {
7777

7878
for (auto& relocation : mImage->dynamic_relocations()) {
7979
auto address = relocation.address();
80-
if (isInSection(address, ".data.rel.ro")) continue;
80+
if (!isInSection(address, ".data.rel.ro")) continue;
8181
auto offset = address - getGapInFront(address);
8282
auto type = relocation.type();
8383
using RELOC = LIEF::ELF::Relocation::TYPE;
@@ -116,7 +116,7 @@ void ELF::_relocateReadonlyData() {
116116
break;
117117
}
118118
default:
119-
spdlog::warn("Unhandled relocation type: {}.", (uint32_t)type);
119+
spdlog::warn("Unhandled relocation type: {:#x}.", (uint32_t)type);
120120
break;
121121
}
122122
}

src/format/MachO.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,10 @@ LIEF::MachO::Symbol* MachO::lookupSymbol(const std::string& pName) {
114114
void MachO::_buildSymbolCache() {
115115
if (!mIsValid) return;
116116

117+
if (!mImage->has_section("__symtab")) {
118+
spdlog::warn("__symtab not found in this image!");
119+
}
120+
117121
for (auto& symbol : mImage->symbols()) {
118122
mSymbolCache.mFromName.try_emplace(symbol.name(), &symbol);
119123
mSymbolCache.mFromValue.try_emplace(symbol.value(), &symbol);

0 commit comments

Comments
 (0)