Skip to content

Commit a45466f

Browse files
committed
Remove notion of image name from objective-c processor
This made the macho objective-c processor aware of images and was passed around everywhere, instead we have an overridable section getter that gives the control over the section retrieval to the derived processor. In the case of shared cache this means we can store the image to be processed and then use the header to locate the relevant sections. Fixes: #6594
1 parent a6c5c22 commit a45466f

File tree

6 files changed

+54
-40
lines changed

6 files changed

+54
-40
lines changed

objectivec/objc.cpp

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,28 +1119,21 @@ void ObjCProcessor::ApplyMethodTypes(Class& cls)
11191119
}
11201120
}
11211121

1122-
Ref<Section> ObjCProcessor::GetSectionForImage(std::optional<std::string> imageName, const char* sectionName)
1122+
Ref<Section> ObjCProcessor::GetSectionWithName(const char* sectionName)
11231123
{
1124-
if (imageName)
1125-
{
1126-
return m_data->GetSectionByName(*imageName + "::" + sectionName);
1127-
}
1128-
else
1129-
{
1130-
return m_data->GetSectionByName(sectionName);
1131-
}
1124+
return m_data->GetSectionByName(sectionName);
11321125
}
11331126

1134-
void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader, std::optional<std::string> imageName)
1127+
void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader)
11351128
{
11361129
auto ptrSize = m_data->GetAddressSize();
1137-
if (auto imageInfo = GetSectionForImage(imageName, "__objc_imageinfo"))
1130+
if (auto imageInfo = GetSectionWithName("__objc_imageinfo"))
11381131
{
11391132
auto start = imageInfo->GetStart();
11401133
auto type = Type::NamedType(m_data, m_typeNames.imageInfo);
11411134
m_data->DefineDataVariable(start, type);
11421135
}
1143-
if (auto selrefs = GetSectionForImage(imageName, "__objc_selrefs"))
1136+
if (auto selrefs = GetSectionWithName("__objc_selrefs"))
11441137
{
11451138
auto start = selrefs->GetStart();
11461139
auto end = selrefs->GetEnd();
@@ -1163,7 +1156,7 @@ void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader, std::optional<st
11631156
DefineObjCSymbol(DataSymbol, type, "selRef_" + sel, i, true);
11641157
}
11651158
}
1166-
if (auto superRefs = GetSectionForImage(imageName, "__objc_classrefs"))
1159+
if (auto superRefs = GetSectionWithName("__objc_classrefs"))
11671160
{
11681161
auto start = superRefs->GetStart();
11691162
auto end = superRefs->GetEnd();
@@ -1181,7 +1174,7 @@ void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader, std::optional<st
11811174
}
11821175
}
11831176
}
1184-
if (auto superRefs = GetSectionForImage(imageName, "__objc_superrefs"))
1177+
if (auto superRefs = GetSectionWithName("__objc_superrefs"))
11851178
{
11861179
auto start = superRefs->GetStart();
11871180
auto end = superRefs->GetEnd();
@@ -1199,7 +1192,7 @@ void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader, std::optional<st
11991192
}
12001193
}
12011194
}
1202-
if (auto protoRefs = GetSectionForImage(imageName, "__objc_protorefs"))
1195+
if (auto protoRefs = GetSectionWithName("__objc_protorefs"))
12031196
{
12041197
auto start = protoRefs->GetStart();
12051198
auto end = protoRefs->GetEnd();
@@ -1217,7 +1210,7 @@ void ObjCProcessor::PostProcessObjCSections(ObjCReader* reader, std::optional<st
12171210
}
12181211
}
12191212
}
1220-
if (auto ivars = GetSectionForImage(imageName, "__objc_ivar"))
1213+
if (auto ivars = GetSectionWithName("__objc_ivar"))
12211214
{
12221215
auto start = ivars->GetStart();
12231216
auto end = ivars->GetEnd();
@@ -1258,7 +1251,7 @@ Ref<Symbol> ObjCProcessor::GetSymbol(uint64_t address)
12581251
return m_data->GetSymbolByAddress(address);
12591252
}
12601253

1261-
void ObjCProcessor::ProcessObjCData(std::optional<std::string> imageName)
1254+
void ObjCProcessor::ProcessObjCData()
12621255
{
12631256
m_symbolQueue = new SymbolQueue();
12641257
auto addrSize = m_data->GetAddressSize();
@@ -1426,26 +1419,26 @@ void ObjCProcessor::ProcessObjCData(std::optional<std::string> imageName)
14261419
m_typeNames.protocol = finalizeStructureBuilder(m_data, protocolBuilder, "objc_protocol_t").first;
14271420

14281421
m_data->BeginBulkModifySymbols();
1429-
if (auto classList = GetSectionForImage(imageName, "__objc_classlist"))
1422+
if (auto classList = GetSectionWithName("__objc_classlist"))
14301423
LoadClasses(reader.get(), classList);
1431-
if (auto nonLazyClassList = GetSectionForImage(imageName, "__objc_nlclslist"))
1424+
if (auto nonLazyClassList = GetSectionWithName("__objc_nlclslist"))
14321425
LoadClasses(reader.get(), nonLazyClassList); // See: https://stackoverflow.com/a/15318325
14331426

14341427
GenerateClassTypes();
14351428
for (auto& [_, cls] : m_classes)
14361429
ApplyMethodTypes(cls);
14371430

1438-
if (auto catList = GetSectionForImage(imageName, "__objc_catlist")) // Do this after loading class type data.
1431+
if (auto catList = GetSectionWithName("__objc_catlist")) // Do this after loading class type data.
14391432
LoadCategories(reader.get(), catList);
1440-
if (auto nonLazyCatList = GetSectionForImage(imageName, "__objc_nlcatlist")) // Do this after loading class type data.
1433+
if (auto nonLazyCatList = GetSectionWithName("__objc_nlcatlist")) // Do this after loading class type data.
14411434
LoadCategories(reader.get(), nonLazyCatList);
14421435
for (auto& [_, cat] : m_categories)
14431436
ApplyMethodTypes(cat);
14441437

1445-
if (auto protoList = GetSectionForImage(imageName, "__objc_protolist"))
1438+
if (auto protoList = GetSectionWithName("__objc_protolist"))
14461439
LoadProtocols(reader.get(), protoList);
14471440

1448-
PostProcessObjCSections(reader.get(), imageName);
1441+
PostProcessObjCSections(reader.get());
14491442

14501443
auto id = m_data->BeginUndoActions();
14511444
m_symbolQueue->Process();
@@ -1460,7 +1453,7 @@ void ObjCProcessor::ProcessObjCData(std::optional<std::string> imageName)
14601453
}
14611454

14621455

1463-
void ObjCProcessor::ProcessCFStrings(std::optional<std::string> imageName)
1456+
void ObjCProcessor::ProcessCFStrings()
14641457
{
14651458
m_symbolQueue = new SymbolQueue();
14661459
uint64_t ptrSize = m_data->GetAddressSize();
@@ -1495,7 +1488,7 @@ void ObjCProcessor::ProcessCFStrings(std::optional<std::string> imageName)
14951488
m_typeNames.cfStringUTF16 = type.first;
14961489

14971490
auto reader = GetReader();
1498-
if (auto cfstrings = GetSectionForImage(imageName, "__cfstring"))
1491+
if (auto cfstrings = GetSectionWithName("__cfstring"))
14991492
{
15001493
auto start = cfstrings->GetStart();
15011494
auto end = cfstrings->GetEnd();

objectivec/objc.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,7 @@ namespace BinaryNinja {
314314
bool ApplyMethodType(Class& cls, Method& method, bool isInstanceMethod);
315315
void ApplyMethodTypes(Class& cls);
316316

317-
Ref<Section> GetSectionForImage(std::optional<std::string> imageName, const char* sectionName);
318-
void PostProcessObjCSections(ObjCReader* reader, std::optional<std::string> imageName);
317+
void PostProcessObjCSections(ObjCReader* reader);
319318

320319
protected:
321320
Ref<BinaryView> m_data;
@@ -327,13 +326,15 @@ namespace BinaryNinja {
327326
// Because an objective-c processor might have access to other non-view symbols that we want to retrieve.
328327
// By default, this will just get symbol at the address in the view.
329328
virtual Ref<Symbol> GetSymbol(uint64_t address);
329+
virtual Ref<Section> GetSectionWithName(const char* sectionName);
330330

331331
public:
332332
virtual ~ObjCProcessor() = default;
333333

334334
ObjCProcessor(BinaryView* data, const char* loggerName, bool isBackedByDatabase, bool skipClassBaseProtocols = false);
335-
void ProcessObjCData(std::optional<std::string> imageName);
336-
void ProcessCFStrings(std::optional<std::string> imageName);
335+
// TODO: Instead of passing in image name the processor must be given section refs in a structure that outlines all objc sections.
336+
void ProcessObjCData();
337+
void ProcessCFStrings();
337338
void AddRelocatedPointer(uint64_t location, uint64_t rewrite);
338339
};
339340
}

view/macho/machoview.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2336,7 +2336,7 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
23362336
if (parseCFStrings)
23372337
{
23382338
try {
2339-
m_objcProcessor->ProcessCFStrings(std::nullopt);
2339+
m_objcProcessor->ProcessCFStrings();
23402340
}
23412341
catch (std::exception& ex)
23422342
{
@@ -2348,7 +2348,7 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
23482348
if (parseObjCStructs)
23492349
{
23502350
try {
2351-
m_objcProcessor->ProcessObjCData(std::nullopt);
2351+
m_objcProcessor->ProcessObjCData();
23522352
}
23532353
catch (std::exception& ex)
23542354
{

view/sharedcache/core/ObjC.cpp

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ Ref<Symbol> SharedCacheObjCProcessor::GetSymbol(uint64_t address)
170170
return symbol;
171171
}
172172

173-
SharedCacheObjCProcessor::SharedCacheObjCProcessor(BinaryView* data, bool isBackedByDatabase) :
174-
ObjCProcessor(data, "SharedCache.ObjC", isBackedByDatabase, true)
175-
{}
173+
Ref<Section> SharedCacheObjCProcessor::GetSectionWithName(const char *sectionName)
174+
{
175+
const auto controller = DSC::SharedCacheController::FromView(*m_data);
176+
if (!controller)
177+
return nullptr;
178+
179+
const auto image = controller->GetCache().GetImageAt(m_imageAddress);
180+
if (!image)
181+
return nullptr;
182+
183+
for (const auto& section : image->header->sectionNames)
184+
if (section.find(sectionName) != std::string::npos)
185+
return m_data->GetSectionByName(section);
186+
187+
return nullptr;
188+
}
189+
190+
SharedCacheObjCProcessor::SharedCacheObjCProcessor(BinaryView *data, bool isBackedByDatabase, uint64_t imageAddress)
191+
: ObjCProcessor(data, "SharedCache.ObjC", isBackedByDatabase, true)
192+
{
193+
m_imageAddress = imageAddress;
194+
}

view/sharedcache/core/ObjC.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,18 @@ namespace DSCObjC {
5858
class SharedCacheObjCProcessor : public BinaryNinja::ObjCProcessor
5959
{
6060
std::optional<uint64_t> m_customRelativeMethodSelectorBase = std::nullopt;
61+
uint64_t m_imageAddress;
6162

6263
std::shared_ptr<BinaryNinja::ObjCReader> GetReader() override;
6364

6465
void GetRelativeMethod(BinaryNinja::ObjCReader* reader, BinaryNinja::method_t& meth) override;
6566

6667
BinaryNinja::Ref<BinaryNinja::Symbol> GetSymbol(uint64_t address) override;
6768

69+
BinaryNinja::Ref<BinaryNinja::Section> GetSectionWithName(const char *sectionName) override;
70+
6871
public:
69-
SharedCacheObjCProcessor(BinaryNinja::BinaryView* data, bool isBackedByDatabase);
72+
SharedCacheObjCProcessor(BinaryNinja::BinaryView* data, bool isBackedByDatabase, uint64_t imageAddress);
7073

7174
uint64_t GetObjCRelativeMethodBaseAddress(BinaryNinja::ObjCReader* reader) override;
7275
};

view/sharedcache/core/SharedCacheController.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,14 @@ bool SharedCacheController::ApplyImage(BinaryView& view, const CacheImage& image
215215
machoProcessor.ApplyHeader(*image.header);
216216
view.SetFunctionAnalysisUpdateDisabled(prevDisabledState);
217217

218-
// TODO: Passing in an image name here is weird considering this is shared with the MACHO view.
219-
// TODO: We should abstract out the "image" into an objc image type that represents what is required, which ig is the name?
220218
// Load objective-c information.
221-
auto objcProcessor = DSCObjC::SharedCacheObjCProcessor(&view, false);
219+
auto objcProcessor = DSCObjC::SharedCacheObjCProcessor(&view, false, image.headerAddress);
222220
try
223221
{
224222
if (m_processObjC)
225-
objcProcessor.ProcessObjCData(image.GetName());
223+
objcProcessor.ProcessObjCData();
226224
if (m_processCFStrings)
227-
objcProcessor.ProcessCFStrings(image.GetName());
225+
objcProcessor.ProcessCFStrings();
228226
}
229227
catch (std::exception& e)
230228
{

0 commit comments

Comments
 (0)