Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not return entry point offset if it's not backed up by disk data #975

Merged
merged 12 commits into from
Aug 30, 2021
Merged
12 changes: 6 additions & 6 deletions include/retdec/cpdetect/cptypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,17 @@ struct ToolInformation
/// @c false if file has no has no or invalid EP offset
bool entryPointOffset = false;
/// entry point offset
long long unsigned int epOffset =
std::numeric_limits<long long unsigned int>::max();
std::uint64_t epOffset =
std::numeric_limits<std::uint64_t>::max();

/// @c false if file has no has no or invalid EP address
bool entryPointAddress = false;
/// entry point address
long long unsigned int epAddress =
std::numeric_limits<long long unsigned int>::max();
std::uint64_t epAddress =
std::numeric_limits<std::uint64_t>::max();
/// image base address
long long unsigned int imageBase =
std::numeric_limits<long long unsigned int>::max();
std::uint64_t imageBase =
std::numeric_limits<std::uint64_t>::max();

/// offset of the file overlay. 0 if no overlay
uint64_t overlayOffset = 0;
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/coff/coff_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ class CoffFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &epOffset) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &epOffset) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/elf/elf_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ class ElfFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &epOffset) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &epOffset) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/file_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -307,11 +307,11 @@ class FileFormat : public retdec::utils::ByteValueStorage, private retdec::utils
virtual bool isObjectFile() const = 0;
virtual bool isDll() const = 0;
virtual bool isExecutable() const = 0;
virtual bool getMachineCode(unsigned long long &result) const = 0;
virtual bool getAbiVersion(unsigned long long &result) const = 0;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const = 0;
virtual bool getEpAddress(unsigned long long &result) const = 0;
virtual bool getEpOffset(unsigned long long &epOffset) const = 0;
virtual bool getMachineCode(std::uint64_t &result) const = 0;
virtual bool getAbiVersion(std::uint64_t &result) const = 0;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const = 0;
virtual bool getEpAddress(std::uint64_t &result) const = 0;
virtual bool getEpOffset(std::uint64_t &epOffset) const = 0;
virtual Architecture getTargetArchitecture() const = 0;
virtual std::size_t getDeclaredNumberOfSections() const = 0;
virtual std::size_t getDeclaredNumberOfSegments() const = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ class IntelHexFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &epOffset) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &epOffset) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/macho/macho_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ class MachOFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &epOffset) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &epOffset) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/pe/pe_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,11 +132,11 @@ class PeFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &epOffset) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &epOffset) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ class PeFormatParser
{
std::uint32_t entryPoint = peFile->imageLoader().getOptionalHeader().AddressOfEntryPoint;

epOffset = peFile->imageLoader().getFileOffsetFromRva(entryPoint);
epOffset = peFile->imageLoader().getValidOffsetFromRva(entryPoint);
return (entryPoint != 0 || isDll() == false) && (epOffset != UINT32_MAX);
}

Expand Down
10 changes: 5 additions & 5 deletions include/retdec/fileformat/file_format/raw_data/raw_data_format.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ class RawDataFormat : public FileFormat
virtual bool isObjectFile() const override;
virtual bool isDll() const override;
virtual bool isExecutable() const override;
virtual bool getMachineCode(unsigned long long &result) const override;
virtual bool getAbiVersion(unsigned long long &result) const override;
virtual bool getImageBaseAddress(unsigned long long &imageBase) const override;
virtual bool getEpAddress(unsigned long long &result) const override;
virtual bool getEpOffset(unsigned long long &result) const override;
virtual bool getMachineCode(std::uint64_t &result) const override;
virtual bool getAbiVersion(std::uint64_t &result) const override;
virtual bool getImageBaseAddress(std::uint64_t &imageBase) const override;
virtual bool getEpAddress(std::uint64_t &result) const override;
virtual bool getEpOffset(std::uint64_t &result) const override;
virtual Architecture getTargetArchitecture() const override;
virtual std::size_t getDeclaredNumberOfSections() const override;
virtual std::size_t getDeclaredNumberOfSegments() const override;
Expand Down
1 change: 1 addition & 0 deletions include/retdec/pelib/ImageLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ class ImageLoader

std::uint32_t vaToRva(std::uint64_t VirtualAddress) const;
std::uint32_t getFileOffsetFromRva(std::uint32_t rva) const;
std::uint32_t getValidOffsetFromRva(std::uint32_t rva) const;
std::uint32_t getRealPointerToRawData(std::size_t sectionIndex) const;
std::uint32_t getImageProtection(std::uint32_t characteristics) const;
std::size_t getSectionIndexByRva(std::uint32_t Rva) const;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ bool ProviderInitialization::runOnModule(Module& m)
if (f->getFileFormat()->isObjectFile()) ft.setIsObject();
if (f->getFileFormat()->isDll()) ft.setIsShared();
}
unsigned long long ep = 0;
std::uint64_t ep = 0;
if (f->getFileFormat()->getEpAddress(ep))
{
c->getConfig().parameters.setEntryPoint(ep);
Expand Down
2 changes: 1 addition & 1 deletion src/bin2llvmir/providers/names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ void NameContainer::initFromImage()

if (_image->getFileFormat())
{
unsigned long long ep = 0;
std::uint64_t ep = 0;
if (_image->getFileFormat()->getEpAddress(ep))
{
if (_config->getConfig().architecture.isArm32OrThumb() && ep % 2)
Expand Down
16 changes: 14 additions & 2 deletions src/cpdetect/cpdetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ ReturnCode CompilerDetector::getAllInformation()
}

fileParser.getImageBaseAddress(toolInfo.imageBase);

toolInfo.entryPointAddress = fileParser.getEpAddress(toolInfo.epAddress);
toolInfo.entryPointOffset = fileParser.getEpOffset(toolInfo.epOffset);

Expand All @@ -774,8 +775,19 @@ ReturnCode CompilerDetector::getAllInformation()
toolInfo.overlayOffset = fileParser.getDeclaredFileLength();
}

const bool invalidEntryPoint = !toolInfo.entryPointAddress
|| !toolInfo.entryPointOffset;
bool invalidEntryPoint = false;
Format format = fileParser.getFileFormat();
if (format == Format::PE)
{
// False EP offset (offset outside of file) doesn't have
// to mean invalid EP as it can be memory only
invalidEntryPoint = !toolInfo.entryPointAddress;
}
else
{
invalidEntryPoint = !toolInfo.entryPointAddress || !toolInfo.entryPointOffset;
}

if (!fileParser.getHexEpBytes(toolInfo.epBytes, cpParams.epBytesCount)
&& !invalidEntryPoint
&& !fileParser.isInValidState())
Expand Down
2 changes: 1 addition & 1 deletion src/cpdetect/heuristics/pe_heuristics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ void PeHeuristics::getBorlandDelphiHeuristics()
auto source = DetectionMethod::COMBINED;
auto strength = DetectionStrength::MEDIUM;

unsigned long long imageBaseAddr;
std::uint64_t imageBaseAddr;
if (!fileParser.getImageBaseAddress(imageBaseAddr)
|| !toolInfo.entryPointSection
|| toolInfo.epSection.getIndex()
Expand Down
2 changes: 1 addition & 1 deletion src/debugformat/debugformat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ DebugFormat::DebugFormat(
{
LOG << "\n*** DebugFormat::DebugFormat(): PDB" << std::endl;

unsigned long long imageBase = 0;
std::uint64_t imageBase = 0;
if (auto* pe = dynamic_cast<const fileformat::PeFormat*>(
inFile->getFileFormat()))
{
Expand Down
10 changes: 5 additions & 5 deletions src/fileformat/file_format/coff/coff_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -609,34 +609,34 @@ bool CoffFormat::isExecutable() const
return !isDll() && !isObjectFile();
}

bool CoffFormat::getMachineCode(unsigned long long &result) const
bool CoffFormat::getMachineCode(std::uint64_t &result) const
{
result = file->getMachine();
return true;
}

bool CoffFormat::getAbiVersion(unsigned long long &result) const
bool CoffFormat::getAbiVersion(std::uint64_t &result) const
{
// not in COFF files
static_cast<void>(result);
return false;
}

bool CoffFormat::getImageBaseAddress(unsigned long long &imageBase) const
bool CoffFormat::getImageBaseAddress(std::uint64_t &imageBase) const
{
// not in COFF files
static_cast<void>(imageBase);
return false;
}

bool CoffFormat::getEpAddress(unsigned long long &result) const
bool CoffFormat::getEpAddress(std::uint64_t &result) const
{
// not in COFF files
static_cast<void>(result);
return false;
}

bool CoffFormat::getEpOffset(unsigned long long &epOffset) const
bool CoffFormat::getEpOffset(std::uint64_t &epOffset) const
{
// not in COFF files
static_cast<void>(epOffset);
Expand Down
14 changes: 7 additions & 7 deletions src/fileformat/file_format/elf/elf_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2699,7 +2699,7 @@ std::size_t ElfFormat::getBytesPerWord() const

bool ElfFormat::hasMixedEndianForDouble() const
{
unsigned long long abiVersion = 0;
std::uint64_t abiVersion = 0;
bool hasAbi = getAbiVersion(abiVersion);
return isArm() && (!hasAbi || abiVersion < 5);
}
Expand Down Expand Up @@ -2734,13 +2734,13 @@ bool ElfFormat::isExecutable() const
return reader.get_type() == ET_EXEC;
}

bool ElfFormat::getMachineCode(unsigned long long &result) const
bool ElfFormat::getMachineCode(std::uint64_t &result) const
{
result = reader.get_machine();
return true;
}

bool ElfFormat::getAbiVersion(unsigned long long &result) const
bool ElfFormat::getAbiVersion(std::uint64_t &result) const
{
// this works only for 32-bit ARM
if(!isArm() || getWordLength() != 32)
Expand All @@ -2757,14 +2757,14 @@ bool ElfFormat::getAbiVersion(unsigned long long &result) const
return abi;
}

bool ElfFormat::getImageBaseAddress(unsigned long long &imageBase) const
bool ElfFormat::getImageBaseAddress(std::uint64_t &imageBase) const
{
// not in ELF files
static_cast<void>(imageBase);
return false;
}

bool ElfFormat::getEpAddress(unsigned long long &result) const
bool ElfFormat::getEpAddress(std::uint64_t &result) const
{
const unsigned long long epAddress = reader.get_entry();
if(epAddress)
Expand Down Expand Up @@ -2795,9 +2795,9 @@ bool ElfFormat::getEpAddress(unsigned long long &result) const
return false;
}

bool ElfFormat::getEpOffset(unsigned long long &epOffset) const
bool ElfFormat::getEpOffset(std::uint64_t &epOffset) const
{
unsigned long long epRva;
std::uint64_t epRva;
if(!getEpAddress(epRva))
{
return false;
Expand Down
14 changes: 8 additions & 6 deletions src/fileformat/file_format/file_format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <algorithm>
#include <cassert>
#include <climits>
#include <cstdint>
#include <cstring>
#include <functional>
#include <sstream>
Expand Down Expand Up @@ -1211,7 +1212,7 @@ bool FileFormat::getBytes(std::vector<std::uint8_t> &result, unsigned long long
*/
bool FileFormat::getEpBytes(std::vector<std::uint8_t> &result, unsigned long long numberOfBytes) const
{
unsigned long long epOffset;
std::uint64_t epOffset;
if(stateIsValid && getEpOffset(epOffset))
{
return getBytes(result, epOffset, numberOfBytes);
Expand Down Expand Up @@ -1247,7 +1248,7 @@ bool FileFormat::getHexBytes(std::string &result, unsigned long long offset, uns
*/
bool FileFormat::getHexEpBytes(std::string &result, unsigned long long numberOfBytes) const
{
unsigned long long epOffset;
std::uint64_t epOffset;
if(stateIsValid && getEpOffset(epOffset))
{
return getHexBytes(result, epOffset, numberOfBytes);
Expand Down Expand Up @@ -1329,9 +1330,10 @@ bool FileFormat::isObjectStretchedOverSections(std::size_t addr, std::size_t siz
* Get information about section containing entry point
* @return Pointer to EP section if file has entry point and EP section was detected, @c nullptr otherwise
*/
// useless?
const Section* FileFormat::getEpSection()
{
unsigned long long ep;
std::uint64_t ep;
if(!getEpOffset(ep))
{
return nullptr;
Expand Down Expand Up @@ -1415,7 +1417,7 @@ const Section* FileFormat::getLastButOneSection() const
*/
const Segment* FileFormat::getEpSegment()
{
unsigned long long epAddress;
std::uint64_t epAddress;
if(!getEpAddress(epAddress))
{
return nullptr;
Expand Down Expand Up @@ -2434,13 +2436,13 @@ void FileFormat::dump(std::string &dumpFile)
ret << "; Endianness: " << sEndian << "\n";
ret << "; Type: " << sType << "\n";

unsigned long long addr;
std::uint64_t addr;
if(getEpAddress(addr))
{
ret << "; Entry point address: " << std::hex << addr << "\n";
}

unsigned long long offset;
std::uint64_t offset;
if(getEpOffset(offset))
{
ret << "; Entry point offset: " << offset << "\n";
Expand Down
Loading