@@ -63,6 +63,13 @@ void VMShutdown() {}
6363std::string ResolveFilePath (BinaryNinja::Ref<BinaryNinja::BinaryView> dscView, const std::string& path)
6464{
6565 auto dscProjectFile = dscView->GetFile ()->GetProjectFile ();
66+ BinaryNinja::LogDebugF (
67+ " ResolveFilePath:\n of: {}\n path: {}\n pfn: {}\n pfp: {}" ,
68+ dscView->GetFile ()->GetOriginalFilename (),
69+ path,
70+ dscProjectFile ? dscProjectFile->GetName () : " " ,
71+ dscProjectFile ? dscProjectFile->GetPathOnDisk () : " "
72+ );
6673
6774 // If we're not in a project, just return the path we were given
6875 if (!dscProjectFile)
@@ -73,8 +80,22 @@ std::string ResolveFilePath(BinaryNinja::Ref<BinaryNinja::BinaryView> dscView, c
7380 // TODO: do we need to support looking in subfolders?
7481 // Replace project file path on disk with project file name for resolution
7582 std::string projectFilePathOnDisk = dscProjectFile->GetPathOnDisk ();
83+ std::string originalFilePathOnDisk = dscView->GetFile ()->GetOriginalFilename ();
7684 std::string cleanPath = path;
77- cleanPath.replace (cleanPath.find (projectFilePathOnDisk), projectFilePathOnDisk.size (), dscProjectFile->GetName ());
85+ if (auto pindex = cleanPath.find (projectFilePathOnDisk); pindex != std::string::npos)
86+ {
87+ cleanPath.replace (pindex, projectFilePathOnDisk.size (), dscProjectFile->GetName ());
88+ }
89+ else if (auto oindex = cleanPath.find (originalFilePathOnDisk); oindex != std::string::npos)
90+ {
91+ BinaryNinja::Ref<BinaryNinja::ProjectFile> originalProjectFile = dscProjectFile->GetProject ()->GetFileByPathOnDisk (originalFilePathOnDisk);
92+ if (!originalProjectFile)
93+ {
94+ BinaryNinja::LogErrorF (" Failed to resolve file path for {}: original file {} not found" , path, originalFilePathOnDisk);
95+ return path;
96+ }
97+ cleanPath.replace (oindex, originalFilePathOnDisk.size (), originalProjectFile->GetName ());
98+ }
7899
79100 size_t lastSlashPos = cleanPath.find_last_of (" /\\ " );
80101 std::string fileName;
@@ -108,10 +129,7 @@ std::string ResolveFilePath(BinaryNinja::Ref<BinaryNinja::BinaryView> dscView, c
108129 }
109130 }
110131
111- if (dscView->GetFile ()->GetProjectFile ())
112- {
113- BinaryNinja::LogError (" Failed to resolve file path for %s" , path.c_str ());
114- }
132+ BinaryNinja::LogErrorF (" Failed to resolve file path for {}" , path);
115133
116134 // If we couldn't find a sibling filename, just return the path we were given
117135 return path;
@@ -235,7 +253,7 @@ void FileAccessorCache::EvictFromCacheIfNeeded()
235253 }
236254}
237255
238- std::shared_ptr<LazyMappedFileAccessor> FileAccessorCache::OpenLazily (BinaryNinja::Ref<BinaryNinja::BinaryView> dscView,
256+ std::shared_ptr<LazyMappedFileAccessor> FileAccessorCache::OpenLazily (
239257 const uint64_t sessionID, const std::string& path,
240258 std::function<void (std::shared_ptr<MMappedFileAccessor>)> postAllocationRoutine)
241259{
@@ -247,9 +265,9 @@ std::shared_ptr<LazyMappedFileAccessor> FileAccessorCache::OpenLazily(BinaryNinj
247265 }
248266
249267 auto accessor = std::make_shared<LazyMappedFileAccessor>(path,
250- [=, dscView = std::move (dscView), postAllocationRoutine = std::move (postAllocationRoutine)](
268+ [=, postAllocationRoutine = std::move (postAllocationRoutine)](
251269 const std::string& path) {
252- auto accessor = Open (dscView, sessionID, path);
270+ auto accessor = Open (sessionID, path);
253271 if (postAllocationRoutine)
254272 {
255273 postAllocationRoutine (accessor);
@@ -262,12 +280,12 @@ std::shared_ptr<LazyMappedFileAccessor> FileAccessorCache::OpenLazily(BinaryNinj
262280}
263281
264282std::shared_ptr<MMappedFileAccessor> FileAccessorCache::Open (
265- BinaryNinja::Ref<BinaryNinja::BinaryView> dscView, const uint64_t sessionID, const std::string& path)
283+ const uint64_t sessionID, const std::string& path)
266284{
267285 EvictFromCacheIfNeeded ();
268286
269287 mmapCount++;
270- auto accessor = std::shared_ptr<MMappedFileAccessor>(new MMappedFileAccessor (ResolveFilePath (dscView, path) ),
288+ auto accessor = std::shared_ptr<MMappedFileAccessor>(new MMappedFileAccessor (path),
271289 [this ](MMappedFileAccessor* accessor) { Close (accessor); });
272290
273291 std::lock_guard lock (m_mutex);
@@ -291,11 +309,11 @@ void FileAccessorCache::Close(MMappedFileAccessor* accessor)
291309 delete accessor;
292310}
293311
294- std::shared_ptr<LazyMappedFileAccessor> MMappedFileAccessor::Open (BinaryNinja::Ref<BinaryNinja::BinaryView> dscView,
312+ std::shared_ptr<LazyMappedFileAccessor> MMappedFileAccessor::Open (
295313 const uint64_t sessionID, const std::string& path,
296314 std::function<void (std::shared_ptr<MMappedFileAccessor>)> postAllocationRoutine)
297315{
298- return FileAccessorCache::Shared ().OpenLazily (dscView, sessionID, path, std::move (postAllocationRoutine));
316+ return FileAccessorCache::Shared ().OpenLazily (sessionID, path, std::move (postAllocationRoutine));
299317}
300318
301319void MMappedFileAccessor::InitialVMSetup ()
@@ -537,7 +555,7 @@ void VM::MapPages(BinaryNinja::Ref<BinaryNinja::BinaryView> dscView, uint64_t se
537555 }
538556
539557 auto accessor =
540- MMappedFileAccessor::Open (std::move (dscView), sessionID, filePath, std::move (postAllocationRoutine));
558+ MMappedFileAccessor::Open (sessionID, ResolveFilePath (dscView, filePath) , std::move (postAllocationRoutine));
541559 auto [it, inserted] = m_map.insert_or_assign ({vm_address, vm_address + size}, PageMapping (std::move (accessor), fileoff));
542560 if (m_safe && !inserted)
543561 {
0 commit comments