Skip to content

Commit 9fa3dd7

Browse files
WeiN76LQhemesare
authored andcommitted
[SharedCache] Define BackingCacheType
Split out from https://github.com/WeiN76LQh/binaryninja-api/tree/process-local-symbols
1 parent a30831a commit 9fa3dd7

File tree

10 files changed

+164
-35
lines changed

10 files changed

+164
-35
lines changed

view/macho/machoview.cpp

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,6 @@
1414
#include "lowlevelilinstruction.h"
1515
#include "rapidjsonwrapper.h"
1616

17-
enum {
18-
N_STAB = 0xe0,
19-
N_PEXT = 0x10,
20-
N_TYPE = 0x0e,
21-
N_EXT = 0x01
22-
};
23-
2417
using namespace BinaryNinja;
2518
using namespace std;
2619

view/macho/machoview.h

Lines changed: 123 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -268,14 +268,129 @@ typedef int vm_prot_t;
268268
#define SEG_UNIXSTACK "__UNIXSTACK"
269269
#define SEG_IMPORT "__IMPORT"
270270

271-
//Symbol Types (N_TYPE)
272-
#define N_UNDF 0x0
273-
#define N_ABS 0x2
274-
#define N_SECT 0xe
275-
#define N_PBUD 0xc
276-
#define N_INDR 0xa
277-
278-
#define N_ARM_THUMB_DEF 0x0008
271+
/*
272+
* Symbols with a index into the string table of zero (n_un.n_strx == 0) are
273+
* defined to have a null, "", name. Therefore all string indexes to non null
274+
* names must not have a zero string index. This is bit historical information
275+
* that has never been well documented.
276+
*/
277+
278+
/*
279+
* The n_type field really contains four fields:
280+
* unsigned char N_STAB:3,
281+
* N_PEXT:1,
282+
* N_TYPE:3,
283+
* N_EXT:1;
284+
* which are used via the following masks.
285+
*/
286+
#define N_STAB 0xe0 /* if any of these bits set, a symbolic debugging entry */
287+
#define N_PEXT 0x10 /* private external symbol bit */
288+
#define N_TYPE 0x0e /* mask for the type bits */
289+
#define N_EXT 0x01 /* external symbol bit, set for external symbols */
290+
291+
/*
292+
* Only symbolic debugging entries have some of the N_STAB bits set and if any
293+
* of these bits are set then it is a symbolic debugging entry (a stab). In
294+
* which case then the values of the n_type field (the entire field) are given
295+
* in <mach-o/stab.h>
296+
*/
297+
298+
/*
299+
* Values for N_TYPE bits of the n_type field.
300+
*/
301+
#define N_UNDF 0x0 /* undefined, n_sect == NO_SECT */
302+
#define N_ABS 0x2 /* absolute, n_sect == NO_SECT */
303+
#define N_SECT 0xe /* defined in section number n_sect */
304+
#define N_PBUD 0xc /* prebound undefined (defined in a dylib) */
305+
#define N_INDR 0xa /* indirect */
306+
307+
/*
308+
* If the type is N_INDR then the symbol is defined to be the same as another
309+
* symbol. In this case the n_value field is an index into the string table
310+
* of the other symbol's name. When the other symbol is defined then they both
311+
* take on the defined type and value.
312+
*/
313+
314+
/*
315+
* If the type is N_SECT then the n_sect field contains an ordinal of the
316+
* section the symbol is defined in. The sections are numbered from 1 and
317+
* refer to sections in order they appear in the load commands for the file
318+
* they are in. This means the same ordinal may very well refer to different
319+
* sections in different files.
320+
*
321+
* The n_value field for all symbol table entries (including N_STAB's) gets
322+
* updated by the link editor based on the value of it's n_sect field and where
323+
* the section n_sect references gets relocated. If the value of the n_sect
324+
* field is NO_SECT then it's n_value field is not changed by the link editor.
325+
*/
326+
#define NO_SECT 0 /* symbol is not in any section */
327+
#define MAX_SECT 255 /* 1 thru 255 inclusive */
328+
329+
/*
330+
* The bit 0x0020 of the n_desc field is used for two non-overlapping purposes
331+
* and has two different symbolic names, N_NO_DEAD_STRIP and N_DESC_DISCARDED.
332+
*/
333+
334+
/*
335+
* The N_NO_DEAD_STRIP bit of the n_desc field only ever appears in a
336+
* relocatable .o file (MH_OBJECT filetype). And is used to indicate to the
337+
* static link editor it is never to dead strip the symbol.
338+
*/
339+
#define N_NO_DEAD_STRIP 0x0020 /* symbol is not to be dead stripped */
340+
341+
/*
342+
* The N_DESC_DISCARDED bit of the n_desc field never appears in linked image.
343+
* But is used in very rare cases by the dynamic link editor to mark an in
344+
* memory symbol as discared and longer used for linking.
345+
*/
346+
#define N_DESC_DISCARDED 0x0020 /* symbol is discarded */
347+
348+
/*
349+
* The N_WEAK_REF bit of the n_desc field indicates to the dynamic linker that
350+
* the undefined symbol is allowed to be missing and is to have the address of
351+
* zero when missing.
352+
*/
353+
#define N_WEAK_REF 0x0040 /* symbol is weak referenced */
354+
355+
/*
356+
* The N_WEAK_DEF bit of the n_desc field indicates to the static and dynamic
357+
* linkers that the symbol definition is weak, allowing a non-weak symbol to
358+
* also be used which causes the weak definition to be discared. Currently this
359+
* is only supported for symbols in coalesed sections.
360+
*/
361+
#define N_WEAK_DEF 0x0080 /* coalesed symbol is a weak definition */
362+
363+
/*
364+
* The N_REF_TO_WEAK bit of the n_desc field indicates to the dynamic linker
365+
* that the undefined symbol should be resolved using flat namespace searching.
366+
*/
367+
#define N_REF_TO_WEAK 0x0080 /* reference to a weak symbol */
368+
369+
/*
370+
* The N_ARM_THUMB_DEF bit of the n_desc field indicates that the symbol is
371+
* a defintion of a Thumb function.
372+
*/
373+
#define N_ARM_THUMB_DEF 0x0008 /* symbol is a Thumb function (ARM) */
374+
375+
/*
376+
* The N_SYMBOL_RESOLVER bit of the n_desc field indicates that the
377+
* that the function is actually a resolver function and should
378+
* be called to get the address of the real function to use.
379+
* This bit is only available in .o files (MH_OBJECT filetype)
380+
*/
381+
#define N_SYMBOL_RESOLVER 0x0100
382+
383+
/*
384+
* The N_ALT_ENTRY bit of the n_desc field indicates that the
385+
* symbol is pinned to the previous content.
386+
*/
387+
#define N_ALT_ENTRY 0x0200
388+
389+
/*
390+
* The N_COLD_FUNC bit of the n_desc field indicates that the symbol is used
391+
* infrequently and the linker should order it towards the end of the section.
392+
*/
393+
#define N_COLD_FUNC 0x0400
279394

280395
/*
281396
* An indirect symbol table entry is simply a 32bit index into the symbol table

view/sharedcache/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endif()
3333
set(HARD_FAIL_MODE OFF CACHE BOOL "Enable hard fail mode")
3434
set(SLIDEINFO_DEBUG_TAGS OFF CACHE BOOL "Enable debug tags in slideinfo")
3535
set(VIEW_NAME "DSCView" CACHE STRING "Name of the view")
36-
set(METADATA_VERSION 4 CACHE STRING "Version of the metadata")
36+
set(METADATA_VERSION 5 CACHE STRING "Version of the metadata")
3737

3838
add_subdirectory(core)
3939
add_subdirectory(api)

view/sharedcache/api/python/sharedcache.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,21 @@ def __repr__(self):
5252
@dataclasses.dataclass
5353
class DSCBackingCache:
5454
path: str
55-
isPrimary: bool
55+
cacheType: BackingCacheType
5656
mappings: list[DSCBackingCacheMapping]
5757

5858
def __str__(self):
5959
return repr(self)
6060

6161
def __repr__(self):
62-
return f"<DSCBackingCache {self.path} {'Primary' if self.isPrimary else 'Secondary'} | {len(self.mappings)} mappings>"
62+
match self.cacheType:
63+
case BackingCacheType.BackingCacheTypePrimary:
64+
cacheTypeStr = 'Primary'
65+
case BackingCacheType.BackingCacheTypeSecondary:
66+
cacheTypeStr = 'Secondary'
67+
case BackingCacheType.BackingCacheTypeSymbols:
68+
cacheTypeStr = 'Symbols'
69+
return f"<DSCBackingCache {self.path} {cacheTypeStr} | {len(self.mappings)} mappings>"
6370

6471

6572
@dataclasses.dataclass
@@ -142,7 +149,7 @@ def caches(self):
142149
mappings.append(mapping)
143150
result.append(DSCBackingCache(
144151
value[i].path,
145-
value[i].isPrimary,
152+
value[i].cacheType,
146153
mappings
147154
))
148155

view/sharedcache/api/python/sharedcache_enums.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import enum
22

33

4+
class BackingCacheType(enum.IntEnum):
5+
BackingCacheTypePrimary = 0
6+
BackingCacheTypeSecondary = 1
7+
BackingCacheTypeSymbols = 2
8+
9+
410
class DSCViewLoadProgress(enum.IntEnum):
511
LoadProgressNotStarted = 0
612
LoadProgressLoadingCaches = 1

view/sharedcache/api/sharedcache.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ namespace SharedCacheAPI {
102102
{
103103
BackingCache cache;
104104
cache.path = value[i].path;
105-
cache.isPrimary = value[i].isPrimary;
105+
cache.cacheType = value[i].cacheType;
106106
for (size_t j = 0; j < value[i].mappingCount; j++)
107107
{
108108
BackingCacheMapping mapping;

view/sharedcache/api/sharedcacheapi.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace SharedCacheAPI {
105105

106106
struct BackingCache {
107107
std::string path;
108-
bool isPrimary;
108+
BNBackingCacheType cacheType;
109109
std::vector<BackingCacheMapping> mappings;
110110
};
111111

view/sharedcache/api/sharedcachecore.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,12 @@ extern "C"
6464
LoadProgressFinished,
6565
} BNDSCViewLoadProgress;
6666

67+
typedef enum BNBackingCacheType {
68+
BackingCacheTypePrimary,
69+
BackingCacheTypeSecondary,
70+
BackingCacheTypeSymbols,
71+
} BNBackingCacheType;
72+
6773
typedef struct BNBinaryView BNBinaryView;
6874
typedef struct BNSharedCache BNSharedCache;
6975
typedef struct BNStringRef BNStringRef;
@@ -98,7 +104,7 @@ extern "C"
98104

99105
typedef struct BNDSCBackingCache {
100106
char* path;
101-
bool isPrimary;
107+
BNBackingCacheType cacheType;
102108
BNDSCBackingCacheMapping* mappings;
103109
size_t mappingCount;
104110
} BNDSCBackingCache;

view/sharedcache/core/SharedCache.cpp

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
517517
{
518518
dyld_cache_mapping_info mapping {};
519519
BackingCache cache;
520-
cache.isPrimary = true;
520+
cache.cacheType = BackingCacheTypePrimary;
521521
cache.path = path;
522522

523523
for (size_t i = 0; i < primaryCacheHeader.mappingCount; i++)
@@ -583,7 +583,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
583583
// briefly.
584584

585585
BackingCache cache;
586-
cache.isPrimary = true;
586+
cache.cacheType = BackingCacheTypePrimary;
587587
cache.path = path;
588588

589589
for (size_t i = 0; i < primaryCacheHeader.mappingCount; i++)
@@ -654,7 +654,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
654654

655655
dyld_cache_mapping_info subCacheMapping {};
656656
BackingCache subCache;
657-
subCache.isPrimary = false;
657+
subCache.cacheType = BackingCacheTypeSecondary;
658658
subCache.path = subCachePath;
659659

660660
for (size_t j = 0; j < subCacheHeader.mappingCount; j++)
@@ -688,7 +688,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
688688
dyld_cache_mapping_info mapping {}; // We're going to reuse this for all of the mappings. We only need it
689689
// briefly.
690690
BackingCache cache;
691-
cache.isPrimary = true;
691+
cache.cacheType = BackingCacheTypePrimary;
692692
cache.path = path;
693693

694694
for (size_t i = 0; i < primaryCacheHeader.mappingCount; i++)
@@ -740,7 +740,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
740740
subCacheFile->Read(&subCacheHeader, 0, headerSize);
741741

742742
BackingCache subCache;
743-
subCache.isPrimary = false;
743+
subCache.cacheType = BackingCacheTypeSecondary;
744744
subCache.path = subCachePath;
745745

746746
dyld_cache_mapping_info subCacheMapping {};
@@ -808,7 +808,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
808808
dyld_cache_mapping_info mapping {};
809809

810810
BackingCache cache;
811-
cache.isPrimary = true;
811+
cache.cacheType = BackingCacheTypePrimary;
812812
cache.path = path;
813813

814814
for (size_t i = 0; i < primaryCacheHeader.mappingCount; i++)
@@ -885,7 +885,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
885885
dyld_cache_mapping_info subCacheMapping {};
886886

887887
BackingCache subCache;
888-
subCache.isPrimary = false;
888+
subCache.cacheType = BackingCacheTypeSecondary;
889889
subCache.path = subCachePath;
890890

891891
for (size_t j = 0; j < subCacheHeader.mappingCount; j++)
@@ -942,7 +942,7 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
942942
subCacheFile->Read(&subCacheHeader, 0, headerSize);
943943

944944
BackingCache subCache;
945-
subCache.isPrimary = false;
945+
subCache.cacheType = BackingCacheTypeSymbols;
946946
subCache.path = subCachePath;
947947

948948
dyld_cache_mapping_info subCacheMapping {};
@@ -957,7 +957,9 @@ void SharedCache::PerformInitialLoad(std::lock_guard<std::mutex>& lock)
957957
initialState.backingCaches.push_back(std::move(subCache));
958958
}
959959
catch (...)
960-
{}
960+
{
961+
m_logger->LogWarn("Failed to load the symbols cache");
962+
}
961963
break;
962964
}
963965
}
@@ -3370,7 +3372,7 @@ extern "C"
33703372
for (size_t i = 0; i < viewCaches.size(); i++)
33713373
{
33723374
caches[i].path = BNAllocString(viewCaches[i].path.c_str());
3373-
caches[i].isPrimary = viewCaches[i].isPrimary;
3375+
caches[i].cacheType = viewCaches[i].cacheType;
33743376

33753377
BNDSCBackingCacheMapping* mappings;
33763378
mappings = new BNDSCBackingCacheMapping[viewCaches[i].mappings.size()];
@@ -3750,15 +3752,15 @@ void SharedCache::ModifiedState::Merge(SharedCache::ModifiedState&& newer)
37503752
void BackingCache::Store(SerializationContext& context) const
37513753
{
37523754
MSS(path);
3753-
MSS(isPrimary);
3755+
MSS_CAST(cacheType, uint32_t);
37543756
MSS(mappings);
37553757
}
37563758

37573759
BackingCache BackingCache::Load(DeserializationContext& context)
37583760
{
37593761
BackingCache cache;
37603762
cache.MSL(path);
3761-
cache.MSL(isPrimary);
3763+
cache.MSL_CAST(cacheType, uint32_t, BNBackingCacheType);
37623764
cache.MSL(mappings);
37633765
return cache;
37643766
}

view/sharedcache/core/SharedCache.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ namespace SharedCacheCore {
104104

105105
struct BackingCache : public MetadataSerializable<BackingCache> {
106106
std::string path;
107-
bool isPrimary = false;
107+
BNBackingCacheType cacheType = BackingCacheTypeSecondary;
108108
std::vector<dyld_cache_mapping_info> mappings;
109109

110110
void Store(SerializationContext& context) const;

0 commit comments

Comments
 (0)