diff options
Diffstat (limited to 'binaryview.cpp')
| -rw-r--r-- | binaryview.cpp | 414 |
1 files changed, 336 insertions, 78 deletions
diff --git a/binaryview.cpp b/binaryview.cpp index bebfb801..c6970ff5 100644 --- a/binaryview.cpp +++ b/binaryview.cpp @@ -267,6 +267,196 @@ void AnalysisCompletionEvent::Cancel() } +Segment::Segment(BNSegment* seg) +{ + m_object = seg; +} + + +vector<pair<uint64_t, uint64_t>> Segment::GetRelocationRanges() const +{ + size_t count = 0; + BNRange* ranges = BNSegmentGetRelocationRanges(m_object, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +vector<pair<uint64_t, uint64_t>> Segment::GetRelocationRangesAtAddress(uint64_t addr) const +{ + size_t count = 0; + BNRange* ranges = BNSegmentGetRelocationRangesAtAddress(m_object, addr, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +uint64_t Segment::GetRelocationsCount() const +{ + return BNSegmentGetRelocationsCount(m_object); +} + + +uint64_t Segment::GetStart() const +{ + return BNSegmentGetStart(m_object); +} + + +uint64_t Segment::GetLength() const +{ + return BNSegmentGetLength(m_object); +} + + +uint64_t Segment::GetEnd() const +{ + return BNSegmentGetEnd(m_object); +} + + +uint64_t Segment::GetDataEnd() const +{ + return BNSegmentGetDataEnd(m_object); +} + + +uint64_t Segment::GetDataOffset() const +{ + return BNSegmentGetDataOffset(m_object); +} + + +uint64_t Segment::GetDataLength() const +{ + return BNSegmentGetDataLength(m_object); +} + + +uint32_t Segment::GetFlags() const +{ + return BNSegmentGetFlags(m_object); +} + + +bool Segment::IsAutoDefined() const +{ + return BNSegmentIsAutoDefined(m_object); +} + + +void Segment::SetLength(uint64_t length) +{ + BNSegmentSetLength(m_object, length); +} + + +void Segment::SetDataOffset(uint64_t dataOffset) +{ + BNSegmentSetDataOffset(m_object, dataOffset); +} + + +void Segment::SetDataLength(uint64_t dataLength) +{ + BNSegmentSetDataLength(m_object, dataLength); +} + + +void Segment::SetFlags(uint64_t flags) +{ + BNSegmentSetFlags(m_object, flags); +} + + +size_t Segment::Read(BinaryView* view, uint8_t* dest, uint64_t offset, size_t len) +{ + return BNSegmentRead(m_object, view->GetObject(), dest, offset, len); +} + + +Section::Section(BNSection* sec) +{ + m_object = sec; +} + + +std::string Section::GetName() const +{ + return BNSectionGetName(m_object); +} + + +std::string Section::GetType() const +{ + return BNSectionGetType(m_object); +} + + +uint64_t Section::GetStart() const +{ + return BNSectionGetStart(m_object); +} + + +uint64_t Section::GetLength() const +{ + return BNSectionGetLength(m_object); +} + + +uint64_t Section::GetInfoData() const +{ + return BNSectionGetInfoData(m_object); +} + + +uint64_t Section::GetAlignment() const +{ + return BNSectionGetAlign(m_object); +} + + +uint64_t Section::GetEntrySize() const +{ + return BNSectionGetEntrySize(m_object); +} + + +std::string Section::GetLinkedSection() const +{ + return BNSectionGetLinkedSection(m_object); +} + + +std::string Section::GetInfoSection() const +{ + return BNSectionGetInfoSection(m_object); +} + + +BNSectionSemantics Section::GetSemantics() const +{ + return BNSectionGetSemantics(m_object); +} + + +bool Section::AutoDefined() const +{ + return BNSectionIsAutoDefined(m_object); +} + + BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryView* parentView) { BNCustomBinaryView view; @@ -292,7 +482,8 @@ BinaryView::BinaryView(const std::string& typeName, FileMetadata* file, BinaryVi view.isRelocatable = IsRelocatableCallback; view.getAddressSize = GetAddressSizeCallback; view.save = SaveCallback; - + view.defineRelocation = DefineRelocationCallback; + view.defineSymbolRelocation = DefineSymbolRelocationCallback; m_file = file; AddRefForRegistration(); m_object = BNCreateCustomBinaryView(typeName.c_str(), m_file->GetObject(), @@ -455,6 +646,27 @@ bool BinaryView::SaveCallback(void* ctxt, BNFileAccessor* file) } +void BinaryView::DefineRelocationCallback(void* ctxt, BNArchitecture* arch, BNRelocationInfo* info, uint64_t target, + uint64_t reloc) +{ + BinaryView* view = (BinaryView*)ctxt; + BNRelocationInfo curInfo = *info; + Architecture* curArch = new CoreArchitecture(arch); + return view->PerformDefineRelocation(curArch, curInfo, target, reloc); +} + + +void BinaryView::DefineSymbolRelocationCallback(void* ctxt, BNArchitecture* arch, BNRelocationInfo* info, BNSymbol* sym, + uint64_t reloc) +{ + BinaryView* view = (BinaryView*)ctxt; + BNRelocationInfo curInfo = *info; + Architecture* curArch = new CoreArchitecture(arch); + Ref<Symbol> curSymbol = new Symbol(sym); + return view->PerformDefineRelocation(curArch, curInfo, curSymbol, reloc); +} + + bool BinaryView::PerformIsValidOffset(uint64_t offset) { uint8_t val; @@ -528,6 +740,18 @@ bool BinaryView::PerformSave(FileAccessor* file) } +void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc) +{ + DefineRelocation(arch, info, target, reloc); +} + + +void BinaryView::PerformDefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> target, uint64_t reloc) +{ + DefineRelocation(arch, info, target, reloc); +} + + void BinaryView::NotifyDataWritten(uint64_t offset, size_t len) { BNNotifyDataWritten(m_object, offset, len); @@ -707,6 +931,46 @@ bool BinaryView::Save(const string& path) } +void BinaryView::DefineRelocation(Architecture* arch, BNRelocationInfo& info, uint64_t target, uint64_t reloc) +{ + BNDefineRelocation(m_object, arch->GetObject(), &info, target, reloc); +} + + +void BinaryView::DefineRelocation(Architecture* arch, BNRelocationInfo& info, Ref<Symbol> target, uint64_t reloc) +{ + BNDefineSymbolRelocation(m_object, arch->GetObject(), &info, target->GetObject(), reloc); +} + + +vector<pair<uint64_t, uint64_t>> BinaryView::GetRelocationRanges() const +{ + size_t count = 0; + BNRange* ranges = BNGetRelocationRanges(m_object, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + +vector<pair<uint64_t, uint64_t>> BinaryView::GetRelocationRangesAtAddress(uint64_t addr) const +{ + size_t count = 0; + BNRange* ranges = BNGetRelocationRangesAtAddress(m_object, addr, &count); + vector<pair<uint64_t, uint64_t>> result(count); + for (size_t i = 0; i < count; i++) + { + result.push_back({ranges[i].start, ranges[i].end}); + } + BNFreeRelocationRanges(ranges); + return result; +} + + void BinaryView::RegisterNotification(BinaryDataNotification* notify) { BNRegisterDataNotification(m_object, notify->GetCallbacks()); @@ -785,6 +1049,12 @@ bool BinaryView::IsOffsetCodeSemantics(uint64_t offset) const } +bool BinaryView::IsOffsetExternSemantics(uint64_t offset) const +{ + return BNIsOffsetExternSemantics(m_object, offset); +} + + bool BinaryView::IsOffsetWritableSemantics(uint64_t offset) const { return BNIsOffsetWritableSemantics(m_object, offset); @@ -1757,42 +2027,28 @@ void BinaryView::RemoveUserSegment(uint64_t start, uint64_t length) } -vector<Segment> BinaryView::GetSegments() +vector<Ref<Segment>> BinaryView::GetSegments() { size_t count; - BNSegment* segments = BNGetSegments(m_object, &count); + BNSegment** segments = BNGetSegments(m_object, &count); - vector<Segment> result; + vector<Ref<Segment>> result; result.reserve(count); for (size_t i = 0; i < count; i++) - { - Segment segment; - segment.start = segments[i].start; - segment.length = segments[i].length; - segment.dataOffset = segments[i].dataOffset; - segment.dataLength = segments[i].dataLength; - segment.flags = segments[i].flags; - segment.autoDefined = segments[i].autoDefined; - result.push_back(segment); - } + result.push_back(new Segment(BNNewSegmentReference(segments[i]))); - BNFreeSegmentList(segments); + BNFreeSegmentList(segments, count); return result; } -bool BinaryView::GetSegmentAt(uint64_t addr, Segment& result) + +Ref<Segment> BinaryView::GetSegmentAt(uint64_t addr) { - BNSegment segment; - if (!BNGetSegmentAt(m_object, addr, &segment)) - return false; + BNSegment* segment = BNGetSegmentAt(m_object, addr); + if (!segment) + return nullptr; - result.start = segment.start; - result.length = segment.length; - result.dataOffset = segment.dataOffset; - result.dataLength = segment.dataLength; - result.flags = segment.flags; - result.autoDefined = segment.autoDefined; - return true; + return new Segment(BNNewSegmentReference(segment)); } @@ -1832,55 +2088,31 @@ void BinaryView::RemoveUserSection(const string& name) } -vector<Section> BinaryView::GetSections() +vector<Ref<Section>> BinaryView::GetSections() { size_t count; - BNSection* sections = BNGetSections(m_object, &count); + BNSection** sections = BNGetSections(m_object, &count); - vector<Section> result; - result.reserve(count); + vector<Ref<Section>> result; + result.reserve(count); for (size_t i = 0; i < count; i++) - { - Section section; - section.name = sections[i].name; - section.type = sections[i].type; - section.start = sections[i].start; - section.length = sections[i].length; - section.linkedSection = sections[i].linkedSection; - section.infoSection = sections[i].infoSection; - section.infoData = sections[i].infoData; - section.align = sections[i].align; - section.entrySize = sections[i].entrySize; - section.semantics = sections[i].semantics; - result.push_back(section); - } + result.push_back(new Section(BNNewSectionReference(sections[i]))); BNFreeSectionList(sections, count); return result; } -vector<Section> BinaryView::GetSectionsAt(uint64_t addr) +vector<Ref<Section>> BinaryView::GetSectionsAt(uint64_t addr) { size_t count; - BNSection* sections = BNGetSectionsAt(m_object, addr, &count); + BNSection** sections = BNGetSectionsAt(m_object, addr, &count); - vector<Section> result; - result.reserve(count); + vector<Ref<Section>> result; + result.reserve(count); for (size_t i = 0; i < count; i++) { - Section section; - section.name = sections[i].name; - section.type = sections[i].type; - section.start = sections[i].start; - section.length = sections[i].length; - section.linkedSection = sections[i].linkedSection; - section.infoSection = sections[i].infoSection; - section.infoData = sections[i].infoData; - section.align = sections[i].align; - section.entrySize = sections[i].entrySize; - section.semantics = sections[i].semantics; - result.push_back(section); + result.push_back(new Section(BNNewSectionReference(sections[i]))); } BNFreeSectionList(sections, count); @@ -1888,25 +2120,12 @@ vector<Section> BinaryView::GetSectionsAt(uint64_t addr) } -bool BinaryView::GetSectionByName(const string& name, Section& result) +Ref<Section> BinaryView::GetSectionByName(const string& name) { - BNSection section; - if (!BNGetSectionByName(m_object, name.c_str(), §ion)) - return false; - - result.name = section.name; - result.type = section.type; - result.start = section.start; - result.length = section.length; - result.linkedSection = section.linkedSection; - result.infoSection = section.infoSection; - result.infoData = section.infoData; - result.align = section.align; - result.entrySize = section.entrySize; - result.semantics = section.semantics; - - BNFreeSection(§ion); - return true; + BNSection* section = BNGetSectionByName(m_object, name.c_str()); + if (section) + return new Section(section); + return nullptr; } @@ -2008,6 +2227,45 @@ void BinaryView::SetMaxFunctionSizeForAnalysis(uint64_t size) } +Relocation::Relocation(BNRelocation* reloc) +{ + m_object = reloc; +} + + +BNRelocationInfo Relocation::GetInfo() const +{ + return BNRelocationGetInfo(m_object); +} + + +Architecture* Relocation::GetArchitecture() const +{ + return new CoreArchitecture(BNRelocationGetArchitecture(m_object)); +} + + +uint64_t Relocation::GetTarget() const +{ + return BNRelocationGetTarget(m_object); +} + + +uint64_t Relocation::GetReloc() const +{ + return BNRelocationGetReloc(m_object); +} + + +Ref<Symbol> Relocation::GetSymbol() const +{ + BNSymbol* sym = BNRelocationGetSymbol(m_object); + if (!sym) + return nullptr; + return new Symbol(sym); +} + + BinaryData::BinaryData(FileMetadata* file): BinaryView(BNCreateBinaryDataView(file->GetObject())) { } |
