From 8950183b3834dc2179dc4965138b1091e291ae9f Mon Sep 17 00:00:00 2001 From: Jacek Blaszczynski Date: Sun, 17 Dec 2017 01:41:21 +0100 Subject: Add Arm64 support to COFF symbol export feature --- Source/bindexplib.cxx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Source/bindexplib.cxx b/Source/bindexplib.cxx index 2eb47f3..698ab78 100644 --- a/Source/bindexplib.cxx +++ b/Source/bindexplib.cxx @@ -328,8 +328,9 @@ bool DumpFile(const char* filename, std::set& symbols, /* Does it look like a COFF OBJ file??? */ else if (((dosHeader->e_magic == IMAGE_FILE_MACHINE_I386) || (dosHeader->e_magic == IMAGE_FILE_MACHINE_AMD64) || - (dosHeader->e_magic == IMAGE_FILE_MACHINE_ARMNT)) && - (dosHeader->e_sp == 0)) { + (dosHeader->e_magic == IMAGE_FILE_MACHINE_ARMNT)) || + (dosHeader->e_magic == IMAGE_FILE_MACHINE_ARM64) && + (dosHeader->e_sp == 0)) { /* * The two tests above aren't what they look like. They're * really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C) -- cgit v0.12 From 14ebad533dd278137e5a4768768217ca95c4ca24 Mon Sep 17 00:00:00 2001 From: Jacek Blaszczynski Date: Mon, 18 Dec 2017 16:15:58 +0100 Subject: Use IMAGE_FILE_HEADER and add missing Arm 32bit images support --- Source/bindexplib.cxx | 38 +++++++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/Source/bindexplib.cxx b/Source/bindexplib.cxx index 698ab78..21f27d6 100644 --- a/Source/bindexplib.cxx +++ b/Source/bindexplib.cxx @@ -31,7 +31,7 @@ * Extension (Axel 2006-03-15) * As soon as an object file contains an /EXPORT directive (which * is generated by the compiler when a symbol is declared as - * declspec(dllexport)) no to-be-exported symbols are printed, + * __declspec(dllexport) no to-be-exported symbols are printed, * as the linker will see these directives, and if those directives * are present we only export selectively (i.e. we trust the * programmer). @@ -50,12 +50,12 @@ * * It created a wrong EXPORTS for the global pointers and constants. * The Section Header has been involved to discover the missing information -* Now the pointers are correctly supplied supplied with "DATA" descriptor +* Now the pointers are correctly supplied with "DATA" descriptor * the constants with no extra descriptor. * * Corrections (Valery Fine 16/09/96): * -* It didn't work for C++ code with global variables and class definitons +* It didn't work for C++ code with global variables and class definitions * The DumpExternalObject function has been introduced to generate .DEF file * * Author: Valery Fine 16/09/96 (E-mail: fine@vxcern.cern.ch) @@ -68,8 +68,20 @@ #include #include +#ifndef IMAGE_FILE_MACHINE_ARM +#define IMAGE_FILE_MACHINE_ARM 0x01c0 // ARM Little-Endian +#endif + +#ifndef IMAGE_FILE_MACHINE_THUMB +#define IMAGE_FILE_MACHINE_THUMB 0x01c2 // ARM Thumb/Thumb-2 Little-Endian +#endif + #ifndef IMAGE_FILE_MACHINE_ARMNT -#define IMAGE_FILE_MACHINE_ARMNT 0x01c4 +#define IMAGE_FILE_MACHINE_ARMNT 0x01c4 // ARM Thumb-2 Little-Endian +#endif + +#ifndef IMAGE_FILE_MACHINE_ARM64 +#define IMAGE_FILE_MACHINE_ARM64 0xaa64 // ARM64 Little-Endian #endif typedef struct cmANON_OBJECT_HEADER_BIGOBJ @@ -294,7 +306,6 @@ bool DumpFile(const char* filename, std::set& symbols, HANDLE hFile; HANDLE hFileMapping; LPVOID lpFileBase; - PIMAGE_DOS_HEADER dosHeader; hFile = CreateFileW(cmsys::Encoding::ToWide(filename).c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, @@ -320,17 +331,18 @@ bool DumpFile(const char* filename, std::set& symbols, return false; } - dosHeader = (PIMAGE_DOS_HEADER)lpFileBase; - if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { + const PIMAGE_FILE_HEADER imageHeader = (PIMAGE_FILE_HEADER)lpFileBase; + if (imageHeader->Machine == IMAGE_DOS_SIGNATURE) { fprintf(stderr, "File is an executable. I don't dump those.\n"); return false; } /* Does it look like a COFF OBJ file??? */ - else if (((dosHeader->e_magic == IMAGE_FILE_MACHINE_I386) || - (dosHeader->e_magic == IMAGE_FILE_MACHINE_AMD64) || - (dosHeader->e_magic == IMAGE_FILE_MACHINE_ARMNT)) || - (dosHeader->e_magic == IMAGE_FILE_MACHINE_ARM64) && - (dosHeader->e_sp == 0)) { + else if (((imageHeader->Machine == IMAGE_FILE_MACHINE_I386) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_AMD64) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARMNT) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM64)) && + (imageHeader->Characteristics == 0)) { /* * The two tests above aren't what they look like. They're * really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C) @@ -338,7 +350,7 @@ bool DumpFile(const char* filename, std::set& symbols, */ DumpSymbols symbolDumper( (PIMAGE_FILE_HEADER)lpFileBase, symbols, dataSymbols, - (dosHeader->e_magic == IMAGE_FILE_MACHINE_I386)); + (imageHeader->Machine == IMAGE_FILE_MACHINE_I386)); symbolDumper.DumpObjFile(); } else { // check for /bigobj format -- cgit v0.12 From 1f3933d3825e6e99cc60065a8666c5e22bb1e7c2 Mon Sep 17 00:00:00 2001 From: Jacek Blaszczynski Date: Mon, 18 Dec 2017 18:22:50 +0100 Subject: Address code review feedback --- Source/bindexplib.cxx | 58 +++++++++++++++++++++++++++------------------------ 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/Source/bindexplib.cxx b/Source/bindexplib.cxx index 21f27d6..9ec9624 100644 --- a/Source/bindexplib.cxx +++ b/Source/bindexplib.cxx @@ -331,38 +331,42 @@ bool DumpFile(const char* filename, std::set& symbols, return false; } - const PIMAGE_FILE_HEADER imageHeader = (PIMAGE_FILE_HEADER)lpFileBase; - if (imageHeader->Machine == IMAGE_DOS_SIGNATURE) { + const PIMAGE_DOS_HEADER dosHeader = (PIMAGE_DOS_HEADER)lpFileBase; + if (dosHeader->e_magic == IMAGE_DOS_SIGNATURE) { fprintf(stderr, "File is an executable. I don't dump those.\n"); return false; - } - /* Does it look like a COFF OBJ file??? */ - else if (((imageHeader->Machine == IMAGE_FILE_MACHINE_I386) || - (imageHeader->Machine == IMAGE_FILE_MACHINE_AMD64) || - (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM) || - (imageHeader->Machine == IMAGE_FILE_MACHINE_ARMNT) || - (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM64)) && - (imageHeader->Characteristics == 0)) { - /* - * The two tests above aren't what they look like. They're - * really checking for IMAGE_FILE_HEADER.Machine == i386 (0x14C) - * and IMAGE_FILE_HEADER.SizeOfOptionalHeader == 0; - */ - DumpSymbols symbolDumper( - (PIMAGE_FILE_HEADER)lpFileBase, symbols, dataSymbols, - (imageHeader->Machine == IMAGE_FILE_MACHINE_I386)); - symbolDumper.DumpObjFile(); } else { - // check for /bigobj format - cmANON_OBJECT_HEADER_BIGOBJ* h = (cmANON_OBJECT_HEADER_BIGOBJ*)lpFileBase; - if (h->Sig1 == 0x0 && h->Sig2 == 0xffff) { - DumpSymbols symbolDumper( - (cmANON_OBJECT_HEADER_BIGOBJ*)lpFileBase, symbols, dataSymbols, - (h->Machine == IMAGE_FILE_MACHINE_I386)); + const PIMAGE_FILE_HEADER imageHeader = (PIMAGE_FILE_HEADER)lpFileBase; + /* Does it look like a COFF OBJ file??? */ + if (((imageHeader->Machine == IMAGE_FILE_MACHINE_I386) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_AMD64) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARMNT) || + (imageHeader->Machine == IMAGE_FILE_MACHINE_ARM64)) && + (imageHeader->Characteristics == 0)) { + /* + * The tests above are checking for IMAGE_FILE_HEADER.Machine + * if it contains supported machine formats (currently ARM and x86) + * and IMAGE_FILE_HEADER.Characteristics == 0 indicating that + * this is not linked COFF OBJ file; + */ + DumpSymbols symbolDumper( + (PIMAGE_FILE_HEADER)lpFileBase, symbols, dataSymbols, + (imageHeader->Machine == IMAGE_FILE_MACHINE_I386)); symbolDumper.DumpObjFile(); } else { - printf("unrecognized file format in '%s'\n", filename); - return false; + // check for /bigobj format + cmANON_OBJECT_HEADER_BIGOBJ* h = + (cmANON_OBJECT_HEADER_BIGOBJ*)lpFileBase; + if (h->Sig1 == 0x0 && h->Sig2 == 0xffff) { + DumpSymbols + symbolDumper((cmANON_OBJECT_HEADER_BIGOBJ*)lpFileBase, symbols, + dataSymbols, (h->Machine == IMAGE_FILE_MACHINE_I386)); + symbolDumper.DumpObjFile(); + } else { + printf("unrecognized file format in '%s'\n", filename); + return false; + } } } UnmapViewOfFile(lpFileBase); -- cgit v0.12