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

Fixed RETDEC-74 and RETDEC-61 #1003

Merged
merged 8 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fixed MPRESS decompiler bug related do imports by ordinal
  • Loading branch information
Ladislav Zezula committed Aug 19, 2021
commit 40b436b044f21f610c243d74222568ecdcf3fd1c
14 changes: 7 additions & 7 deletions include/retdec/fileformat/file_format/pe/pe_format_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -336,21 +336,21 @@ class PeFormatParser
std::uint32_t ordinalNumber = 0;
std::uint32_t patchRva = 0;
std::uint16_t importHint = 0;
bool isImportByOrdinal = false;

if(peImports.getImportedFunction(fileIndex, importIndex, importName, importHint, ordinalNumber, patchRva, false))
if(peImports.getImportedFunction(fileIndex, importIndex, importName, importHint, ordinalNumber, patchRva, isImportByOrdinal, false))
{
auto import = std::make_unique<PeImport>(PeImportFlag::None);

if(importName.length())
{
import->invalidateOrdinalNumber();
import->setName(importName);
}
else
if(isImportByOrdinal)
{
import->setOrdinalNumber(ordinalNumber);
}

// Note: Even when the function is imported by ordinal, there can be name
// Example: WS2_32.dll!@115 -> WSAStartup
import->setName(importName);

import->setLibraryIndex(fileIndex);
import->setAddress(imageBase + patchRva);
return import;
Expand Down
30 changes: 6 additions & 24 deletions include/retdec/pelib/ImportDirectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ namespace PeLib
/// Get the number of functions which are imported by a specific file.
std::uint32_t getNumberOfFunctions(std::size_t dwFilenr, bool newDir) const; // EXPORT
/// Get information about n-th imported function
bool getImportedFunction(std::size_t dwFilenr, std::size_t dwFuncnr, std::string & importName, std::uint16_t & importHint, std::uint32_t & importOrdinal, std::uint32_t & patchRva, bool newDir) const;
bool getImportedFunction(std::size_t dwFilenr, std::size_t dwFuncnr, std::string & importName, std::uint16_t & importHint, std::uint32_t & importOrdinal, std::uint32_t & patchRva, bool & isImportByOrdinal, bool newDir) const;

/// Get the hint of an imported function.
std::uint16_t getFunctionHint(std::uint32_t dwFilenr, std::uint32_t dwFuncnr, bool newDir) const; // EXPORT
Expand Down Expand Up @@ -243,7 +243,7 @@ namespace PeLib
PELIB_IMAGE_IMPORT_DIRECTORY iid;
PELIB_THUNK_DATA td;
td.hint = wHint;
td.itd.Ordinal = wHint /* | PELIB_IMAGE_ORDINAL_FLAGS::PELIB_IMAGE_ORDINAL_FLAG */;
td.itd.Ordinal = wHint | m_ordinalMask;
iid.name = strFilename;
if (FileIter == m_vNewiid.end())
{
Expand Down Expand Up @@ -448,6 +448,7 @@ namespace PeLib
* @param importName If this is import by name, this string is filled by the import name
* @param importHint If this is import by name, this 16-bit integer will be filled by the import hint
* @param importOrdinal If this is import by orginal, this 32-bit integer will be filled by the ordinal of the function
* @param isImportByOrdinal Set to true if this is import by ordinal
* @return true = the indexes are in range, so an import was returned
**/
inline
Expand All @@ -458,6 +459,7 @@ namespace PeLib
std::uint16_t& importHint,
std::uint32_t& importOrdinal,
std::uint32_t& patchRva,
bool& isImportByOrdinal,
bool newDir) const
{
auto& il = getImportList(newDir);
Expand All @@ -471,11 +473,13 @@ namespace PeLib
if(il[dwFilenr].thunk_data[dwFuncnr].itd.Ordinal & m_ordinalMask)
{
importOrdinal = il[dwFilenr].thunk_data[dwFuncnr].itd.Ordinal & ~m_ordinalMask;
isImportByOrdinal = true;
importHint = 0;
}
else
{
importHint = il[dwFilenr].thunk_data[dwFuncnr].hint;
isImportByOrdinal = false;
importOrdinal = 0;
}

Expand Down Expand Up @@ -522,28 +526,6 @@ namespace PeLib
il[dwFilenr].thunk_data[dwFuncnr].hint = value;
}
}
/*
inline bool isInvalidOrdinal(std::uint64_t ordinal, std::uint64_t ordinalMask, std::uint64_t sizeOfImage)
{
// Check for invalid name
if((ordinal & ordinalMask) == 0)
{
// Any name RVA that goes out of image is considered invalid
if(ordinal >= sizeOfImage)
{
return true;
}
}
else
{
// Mask out the ordinal bit. Then, any ordinal must not be larger than 0xFFFF
ordinal = ordinal & ~ordinalMask;
return (ordinal >> 0x10) != 0;
}

return false;
}
*/

/**
* Updates pointer size for import directory
Expand Down