Skip to content

Commit c9dbb48

Browse files
committed
[DSC] Correctly mark symbols as having local, global, or weak binding
1 parent e2feac8 commit c9dbb48

12 files changed

Lines changed: 70 additions & 20 deletions

view/sharedcache/api/python/generator.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,23 @@ int main(int argc, char* argv[])
331331
continue;
332332
}
333333

334-
fprintf(out, "%sEnum = ctypes.c_int\n", name.c_str());
334+
const char* ctypesType = nullptr;
335+
switch (i.second->GetWidth())
336+
{
337+
case 1:
338+
ctypesType = i.second->IsSigned() ? "ctypes.c_int8" : "ctypes.c_uint8";
339+
break;
340+
case 2:
341+
ctypesType = i.second->IsSigned() ? "ctypes.c_int16" : "ctypes.c_uint16";
342+
break;
343+
case 4:
344+
ctypesType = i.second->IsSigned() ? "ctypes.c_int32" : "ctypes.c_uint32";
345+
break;
346+
default:
347+
ctypesType = i.second->IsSigned() ? "ctypes.c_int64" : "ctypes.c_uint64";
348+
break;
349+
}
350+
fprintf(out, "%sEnum = %s\n", name.c_str(), ctypesType);
335351

336352
fprintf(enums, "\n\nclass %s(enum.IntEnum):\n", name.c_str());
337353
for (auto& j : i.second->GetEnumeration()->GetMembers())

view/sharedcache/api/python/sharedcache.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ def __repr__(self):
4040
@dataclasses.dataclass
4141
class CacheSymbol:
4242
symbol_type: sccore.SymbolTypeEnum
43+
symbol_binding: sccore.SymbolBindingEnum
4344
address: int
4445
name: str
4546

@@ -94,13 +95,15 @@ def image_to_api(image: CacheImage) -> sccore.BNSharedCacheImage:
9495
def symbol_from_api(symbol: sccore.BNSharedCacheSymbol) -> CacheSymbol:
9596
return CacheSymbol(
9697
symbol_type=symbol.symbolType,
98+
symbol_binding=symbol.symbolBinding,
9799
address=symbol.address,
98100
name=symbol.name
99101
)
100102

101103
def symbol_to_api(symbol: CacheSymbol) -> sccore.BNSharedCacheSymbol:
102104
return sccore.BNSharedCacheSymbol(
103105
symbolType=symbol.symbol_type,
106+
symbolBinding=symbol.symbol_binding,
104107
address=symbol.address,
105108
_name=BNAllocString(symbol.name)
106109
)

view/sharedcache/api/python/sharedcache_enums.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class SharedCacheRegionType(enum.IntEnum):
2626
SharedCacheRegionTypeNonImage = 3
2727

2828

29+
class SymbolBinding(enum.IntEnum):
30+
NoBinding = 0
31+
LocalBinding = 1
32+
GlobalBinding = 2
33+
WeakBinding = 3
34+
35+
2936
class SymbolType(enum.IntEnum):
3037
FunctionSymbol = 0
3138
ImportAddressSymbol = 1

view/sharedcache/api/sharedcache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ CacheSymbol SymbolFromApi(BNSharedCacheSymbol apiSymbol)
107107
symbol.name = apiSymbol.name;
108108
symbol.address = apiSymbol.address;
109109
symbol.type = apiSymbol.symbolType;
110+
symbol.binding = apiSymbol.symbolBinding;
110111
return symbol;
111112
}
112113

@@ -140,7 +141,7 @@ std::pair<std::string, Ref<Type>> CacheSymbol::DemangledName(BinaryView &view) c
140141
Ref<Symbol> CacheSymbol::GetBNSymbol(BinaryView &view) const
141142
{
142143
auto [shortName, _] = DemangledName(view);
143-
return new Symbol(type, shortName, shortName, name, address, nullptr);
144+
return new Symbol(type, shortName, shortName, name, address, binding);
144145
}
145146

146147
std::string SharedCacheAPI::GetSymbolTypeAsString(const BNSymbolType &type)

view/sharedcache/api/sharedcacheapi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,7 @@ namespace SharedCacheAPI {
286286
struct CacheSymbol
287287
{
288288
BNSymbolType type;
289+
BNSymbolBinding binding = NoBinding;
289290
uint64_t address;
290291
std::string name;
291292

view/sharedcache/api/sharedcachecore.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ extern "C"
2727
{
2828
#endif
2929

30-
// binaryninjacore.h is not included so we must duplicate enum types here.
30+
// binaryninjacore.h is not included so we must duplicate enum types here.
31+
// TODO: Why isn't it?!
3132
#ifdef BN_TYPE_PARSER
32-
typedef enum BNSegmentFlag
33+
enum BNSegmentFlag : uint8_t
3334
{
3435
SegmentExecutable = 1,
3536
SegmentWritable = 2,
@@ -38,9 +39,9 @@ extern "C"
3839
SegmentContainsCode = 0x10,
3940
SegmentDenyWrite = 0x20,
4041
SegmentDenyExecute = 0x40
41-
} BNSegmentFlag;
42+
};
4243

43-
typedef enum BNSymbolType
44+
enum BNSymbolType : uint8_t
4445
{
4546
FunctionSymbol = 0,
4647
ImportAddressSymbol = 1,
@@ -51,7 +52,15 @@ extern "C"
5152
LibraryFunctionSymbol = 6,
5253
SymbolicFunctionSymbol = 7,
5354
LocalLabelSymbol = 8,
54-
} BNSymbolType;
55+
};
56+
57+
enum BNSymbolBinding : uint8_t
58+
{
59+
NoBinding = 0,
60+
LocalBinding = 1,
61+
GlobalBinding = 2,
62+
WeakBinding = 3,
63+
};
5564
#endif
5665

5766
typedef struct BNBinaryView BNBinaryView;
@@ -105,6 +114,7 @@ extern "C"
105114

106115
typedef struct BNSharedCacheSymbol {
107116
BNSymbolType symbolType;
117+
BNSymbolBinding symbolBinding;
108118
uint64_t address;
109119
char* name;
110120
} BNSharedCacheSymbol;

view/sharedcache/core/MachO.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,8 @@ std::optional<SharedCacheMachOHeader> SharedCacheMachOHeader::ParseHeaderForAddr
457457
return header;
458458
}
459459

460-
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(VirtualMemory& vm, const TableInfo &symbolInfo, const TableInfo &stringInfo) const
460+
std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(VirtualMemory& vm, const TableInfo &symbolInfo, const TableInfo &stringInfo,
461+
BNSymbolBinding bindingOverride) const
461462
{
462463
std::vector<CacheSymbol> symbolList;
463464
// TODO: This assumes that 95% (or more) are going to be added.
@@ -544,11 +545,15 @@ std::vector<CacheSymbol> SharedCacheMachOHeader::ReadSymbolTable(VirtualMemory&
544545
if ((nlist.n_desc & N_ARM_THUMB_DEF) == N_ARM_THUMB_DEF)
545546
symbolAddress++;
546547

547-
CacheSymbol symbol;
548-
symbol.address = symbolAddress;
549-
symbol.name = std::move(symbolName);
550-
symbol.type = symbolType.value();
551-
symbolList.emplace_back(symbol);
548+
BNSymbolBinding symbolBinding = GlobalBinding;
549+
if (bindingOverride != NoBinding)
550+
symbolBinding = bindingOverride;
551+
else if (dysymPresent && dysymtab.nlocalsym && entryIndex >= dysymtab.ilocalsym && entryIndex < dysymtab.ilocalsym + dysymtab.nlocalsym)
552+
symbolBinding = LocalBinding;
553+
else if (nlist.n_desc & N_WEAK_DEF)
554+
symbolBinding = WeakBinding;
555+
556+
symbolList.emplace_back(symbolType.value(), symbolBinding, symbolAddress, std::move(symbolName));
552557
}
553558

554559
return symbolList;
@@ -566,6 +571,9 @@ bool SharedCacheMachOHeader::AddExportTerminalSymbol(
566571
if (symbolName.empty() || symbolAddress == 0)
567572
return false;
568573

574+
// Export trie entries are exported by definition.
575+
BNSymbolBinding symbolBinding = (symbolFlags & EXPORT_SYMBOL_FLAGS_WEAK_DEFINITION) ? WeakBinding : GlobalBinding;
576+
569577
// Tries to get the symbol type based off the section containing it.
570578
auto sectionSymbolType = [&]() -> BNSymbolType {
571579
uint32_t sectionFlags = 0;
@@ -593,10 +601,10 @@ bool SharedCacheMachOHeader::AddExportTerminalSymbol(
593601
{
594602
case EXPORT_SYMBOL_FLAGS_KIND_REGULAR:
595603
case EXPORT_SYMBOL_FLAGS_KIND_THREAD_LOCAL:
596-
symbols.emplace_back(sectionSymbolType(), symbolAddress, symbolName);
604+
symbols.emplace_back(sectionSymbolType(), symbolBinding, symbolAddress, symbolName);
597605
break;
598606
case EXPORT_SYMBOL_FLAGS_KIND_ABSOLUTE:
599-
symbols.emplace_back(DataSymbol, symbolAddress, symbolName);
607+
symbols.emplace_back(DataSymbol, symbolBinding, symbolAddress, symbolName);
600608
break;
601609
default:
602610
LogWarnF("Unhandled export symbol kind: {:#x}", symbolFlags & EXPORT_SYMBOL_FLAGS_KIND_MASK);

view/sharedcache/core/MachO.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,8 @@ struct SharedCacheMachOHeader
7070
static std::optional<SharedCacheMachOHeader> ParseHeaderForAddress(
7171
std::shared_ptr<VirtualMemory> vm, uint64_t address, const std::string& imagePath);
7272

73-
std::vector<CacheSymbol> ReadSymbolTable(VirtualMemory& vm, const TableInfo &symbolInfo, const TableInfo &stringInfo) const;
73+
std::vector<CacheSymbol> ReadSymbolTable(VirtualMemory& vm, const TableInfo &symbolInfo, const TableInfo &stringInfo,
74+
BNSymbolBinding bindingOverride = NoBinding) const;
7475

7576
bool AddExportTerminalSymbol(
7677
std::vector<CacheSymbol>& symbols, const std::string& symbolName, const uint8_t* current,

view/sharedcache/core/MachOProcessor.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ void SharedCacheMachOProcessor::ApplyUnmappedLocalSymbols(const SharedCache& cac
130130
TableInfo symbolInfo = {symbolTableStart, localSymbolsEntry.nlistCount};
131131
TableInfo stringInfo = {localStringsAddr, localSymbolsInfo.stringsSize};
132132
BulkSymbolModification bulkSymbolModification(m_view);
133-
const auto symbols = header.ReadSymbolTable(*localSymbolsVM, symbolInfo, stringInfo);
133+
const auto symbols = header.ReadSymbolTable(*localSymbolsVM, symbolInfo, stringInfo, LocalBinding);
134134
for (const auto &sym: symbols)
135135
{
136136
auto [symbol, symbolType] = sym.GetBNSymbolAndType(*m_view);

view/sharedcache/core/SharedCache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ std::pair<std::string, Ref<Type>> CacheSymbol::DemangledName(BinaryView &view) c
2121
std::pair<Ref<Symbol>, Ref<Type>> CacheSymbol::GetBNSymbolAndType(BinaryView& view) const
2222
{
2323
auto [shortName, demangledType] = DemangledName(view);
24-
auto symbol = new Symbol(type, shortName, shortName, name, address, nullptr);
24+
auto symbol = new Symbol(type, shortName, shortName, name, address, binding);
2525
return {symbol, demangledType};
2626
}
2727

0 commit comments

Comments
 (0)