summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXusheng <xusheng@vector35.com>2024-11-08 13:29:00 +0800
committerXusheng <xusheng@vector35.com>2024-11-13 18:45:59 +0800
commit8a00fb5b8c1b9c82db5528b288859227a7e35868 (patch)
tree8d240dcd729437064c0c9d254cde37dc2229dd07
parent30a0fbb5839b08dc58efe602a5cd19c90fbd4c50 (diff)
Bulk add segments in the binary view to improve performance
-rw-r--r--view/elf/elfview.cpp43
-rw-r--r--view/macho/machoview.cpp3
-rw-r--r--view/pe/coffview.cpp6
-rw-r--r--view/pe/peview.cpp4
-rw-r--r--view/pe/teview.cpp2
5 files changed, 30 insertions, 28 deletions
diff --git a/view/elf/elfview.cpp b/view/elf/elfview.cpp
index 8be478f1..fb6942e3 100644
--- a/view/elf/elfview.cpp
+++ b/view/elf/elfview.cpp
@@ -496,38 +496,23 @@ bool ElfView::Init()
m_entryPoint = m_entryPoint + imageBaseAdjustment;
- // Add segments in bulk first
- {
- vector<BNSegmentInfo> segmentsToAdd;
- for (auto& i : m_programHeaders)
- {
- uint64_t adjustedVirtualAddr = i.virtualAddress + imageBaseAdjustment;
- if (i.type == ELF_PT_LOAD) // || i.type == ELF_PT_GNU_RELRO)
- {
- uint32_t flags = 0;
- if (i.flags & 1)
- flags |= SegmentExecutable;
- if (i.flags & 2)
- flags |= SegmentWritable;
- if (i.flags & 4)
- flags |= SegmentReadable;
-
- BNSegmentInfo segmentInfo;
- segmentInfo.start = adjustedVirtualAddr;
- segmentInfo.length = i.memorySize;
- segmentInfo.dataOffset = i.offset;
- segmentInfo.dataLength = i.fileSize;
- segmentInfo.flags = flags;
- segmentsToAdd.push_back(segmentInfo);
- }
- }
- AddAutoSegments(segmentsToAdd);
- }
-
+ BeginBulkAddSegments();
for (auto& i : m_programHeaders)
{
uint64_t adjustedVirtualAddr = i.virtualAddress + imageBaseAdjustment;
+ if (i.type == ELF_PT_LOAD) // || i.type == ELF_PT_GNU_RELRO)
+ {
+ uint32_t flags = 0;
+ if (i.flags & 1)
+ flags |= SegmentExecutable;
+ if (i.flags & 2)
+ flags |= SegmentWritable;
+ if (i.flags & 4)
+ flags |= SegmentReadable;
+ AddAutoSegment(adjustedVirtualAddr, i.memorySize, i.offset, i.fileSize, flags);
+ }
+
// Create sections for the program headers with the standard section names. This will ensure that
// the standard name for sections such as .dynamic will always refer to what the loader actually uses.
if (i.type == ELF_PT_DYNAMIC)
@@ -583,6 +568,8 @@ bool ElfView::Init()
}
}
+ EndBulkAddSegments();
+
// Gather names for the sections
vector<string> sectionNames {""};
for (size_t i = 1; i < m_elfSections.size(); i++)
diff --git a/view/macho/machoview.cpp b/view/macho/machoview.cpp
index 5a118256..d26769e1 100644
--- a/view/macho/machoview.cpp
+++ b/view/macho/machoview.cpp
@@ -1593,6 +1593,7 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
if (!(m_header.ident.filetype == MH_FILESET && isMainHeader)) \
{
+ BeginBulkAddSegments();
for (auto &segment: header.segments) {
if ((segment.initprot == MACHO_VM_PROT_NONE) || (!segment.vmsize))
continue;
@@ -1619,6 +1620,8 @@ bool MachoView::InitializeHeader(MachOHeader& header, bool isMainHeader, uint64_
AddAutoSegment(segment.vmaddr, segment.vmsize, segment.fileoff, segment.filesize, flags);
}
+ EndBulkAddSegments();
+
for (auto& section : header.sections)
{
char sectionName[17];
diff --git a/view/pe/coffview.cpp b/view/pe/coffview.cpp
index 28103a4a..814c8cf2 100644
--- a/view/pe/coffview.cpp
+++ b/view/pe/coffview.cpp
@@ -246,6 +246,7 @@ bool COFFView::Init()
// Read sections
reader.Seek(sectionHeadersOffset);
BinaryReader sectionNameReader(GetParentView(), LittleEndian);
+ BeginBulkAddSegments();
for (uint32_t i = 0; i < sectionCount; i++)
{
@@ -432,6 +433,8 @@ bool COFFView::Init()
}
}
+ EndBulkAddSegments();
+
// Apply architecture and platform
if (!m_arch)
{
@@ -1214,6 +1217,7 @@ bool COFFView::Init()
QualifiedName coffRelocTypeName = DefineType(coffRelocTypeId, coffRelocName, coffRelocStructType);
auto relocHandler = m_arch->GetRelocationHandler("COFF");
+ BeginBulkAddSegments();
for (uint32_t i = 0; i < sectionCount; i++)
{
@@ -1351,6 +1355,8 @@ bool COFFView::Init()
}
}
}
+
+ EndBulkAddSegments();
}
}
catch (std::exception& e)
diff --git a/view/pe/peview.cpp b/view/pe/peview.cpp
index ecf4a42b..f0c43056 100644
--- a/view/pe/peview.cpp
+++ b/view/pe/peview.cpp
@@ -742,6 +742,8 @@ bool PEView::Init()
reader.Seek(optionalHeaderOffset + header.optionalHeaderSize);
// Read sections
BinaryReader sectionNameReader(GetParentView(), LittleEndian);
+ BeginBulkAddSegments();
+
for (uint16_t i = 0; i < header.sectionCount; i++)
{
PESection section;
@@ -900,6 +902,8 @@ bool PEView::Init()
}
}
+ EndBulkAddSegments();
+
// Finished for parse only mode
if (m_parseOnly)
return true;
diff --git a/view/pe/teview.cpp b/view/pe/teview.cpp
index 5e0a2a0e..f8666717 100644
--- a/view/pe/teview.cpp
+++ b/view/pe/teview.cpp
@@ -109,6 +109,7 @@ void TEView::ReadTEImageSectionHeaders(BinaryReader& reader, uint32_t numSection
void TEView::CreateSections()
{
+ BeginBulkAddSegments();
for (size_t i = 0; i < m_sections.size(); i++)
{
auto section = m_sections[i];
@@ -138,6 +139,7 @@ void TEView::CreateSections()
semantics = ReadWriteDataSectionSemantics;
AddAutoSection(section.name, section.virtualAddress + m_imageBase, section.virtualSize, semantics);
}
+ EndBulkAddSegments();
}
void TEView::AssignHeaderTypes()