|
| 1 | +#include <Windows.h> |
| 2 | +#include <algorithm> |
| 3 | +#include <cstdint> |
| 4 | +#include <fstream> |
| 5 | +#include <iostream> |
| 6 | +#include <memory> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +// This executable takes a Windows DLL and uses it to generate |
| 10 | +// a module-definition file [1] which forwards all the exported |
| 11 | +// symbols from the DLL and redirects them back to the DLL. |
| 12 | +// This allows node.exe to export the same symbols as libnode.dll |
| 13 | +// when building Node.js as a shared library. This is conceptually |
| 14 | +// similary to the create_expfile.sh script used on AIX. |
| 15 | +// |
| 16 | +// Generating this .def file requires parsing data out of the |
| 17 | +// PE32/PE32+ file format. Helper structs are defined in <Windows.h> |
| 18 | +// hence why this is an executable and not a script. See [2] for |
| 19 | +// details on the PE format. |
| 20 | +// |
| 21 | +// [1]: https://docs.microsoft.com/en-us/cpp/build/reference/module-definition-dot-def-files |
| 22 | +// [2]: https://docs.microsoft.com/en-us/windows/win32/debug/pe-format |
| 23 | + |
| 24 | +// The PE32 format encodes pointers as Relative Virtual Addresses |
| 25 | +// which are 32 bit offsets from the start of the image. This helper |
| 26 | +// class hides the mess of the pointer arithmetic |
| 27 | +struct RelativeAddress { |
| 28 | + uintptr_t root; |
| 29 | + uintptr_t offset = 0; |
| 30 | + |
| 31 | + RelativeAddress(HMODULE handle) noexcept |
| 32 | + : root(reinterpret_cast<uintptr_t>(handle)) {} |
| 33 | + |
| 34 | + RelativeAddress(HMODULE handle, uintptr_t offset) noexcept |
| 35 | + : root(reinterpret_cast<uintptr_t>(handle)), offset(offset) {} |
| 36 | + |
| 37 | + RelativeAddress(uintptr_t root, uintptr_t offset) noexcept |
| 38 | + : root(root), offset(offset) {} |
| 39 | + |
| 40 | + template <typename T> |
| 41 | + const T* AsPtrTo() const noexcept { |
| 42 | + return reinterpret_cast<const T*>(root + offset); |
| 43 | + } |
| 44 | + |
| 45 | + template <typename T> |
| 46 | + T Read() const noexcept { |
| 47 | + return *AsPtrTo<T>(); |
| 48 | + } |
| 49 | + |
| 50 | + RelativeAddress AtOffset(uintptr_t amount) const noexcept { |
| 51 | + return {root, offset + amount}; |
| 52 | + } |
| 53 | + |
| 54 | + RelativeAddress operator+(uintptr_t amount) const noexcept { |
| 55 | + return {root, offset + amount}; |
| 56 | + } |
| 57 | + |
| 58 | + RelativeAddress ReadRelativeAddress() const noexcept { |
| 59 | + return {root, Read<uint32_t>()}; |
| 60 | + } |
| 61 | +}; |
| 62 | + |
| 63 | +// A wrapper around a dynamically loaded Windows DLL. This steps through the |
| 64 | +// PE file structure to find the export directory and pulls out a list of |
| 65 | +// all the exported symbol names. |
| 66 | +struct Library { |
| 67 | + HMODULE library; |
| 68 | + std::string libraryName; |
| 69 | + std::vector<std::string> exportedSymbols; |
| 70 | + |
| 71 | + Library(HMODULE library) : library(library) { |
| 72 | + auto libnode = RelativeAddress(library); |
| 73 | + |
| 74 | + // At relative offset 0x3C is a 32 bit offset to the COFF signature, 4 bytes |
| 75 | + // after that is the start of the COFF header. |
| 76 | + auto coffHeaderPtr = |
| 77 | + libnode.AtOffset(0x3C).ReadRelativeAddress().AtOffset(4); |
| 78 | + auto coffHeader = coffHeaderPtr.AsPtrTo<IMAGE_FILE_HEADER>(); |
| 79 | + |
| 80 | + // After the coff header is the Optional Header (which is not optional). We |
| 81 | + // don't know what type of optional header we have without examining the |
| 82 | + // magic number |
| 83 | + auto optionalHeaderPtr = coffHeaderPtr.AtOffset(sizeof(IMAGE_FILE_HEADER)); |
| 84 | + auto optionalHeader = optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER>(); |
| 85 | + |
| 86 | + auto exportDirectory = |
| 87 | + (optionalHeader->Magic == 0x20b) ? optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER64>() |
| 88 | + ->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT] |
| 89 | + : optionalHeaderPtr.AsPtrTo<IMAGE_OPTIONAL_HEADER32>() |
| 90 | + ->DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT]; |
| 91 | + |
| 92 | + auto exportTable = libnode.AtOffset(exportDirectory.VirtualAddress) |
| 93 | + .AsPtrTo<IMAGE_EXPORT_DIRECTORY>(); |
| 94 | + |
| 95 | + // This is the name of the library without the suffix, this is more robust |
| 96 | + // than parsing the filename as this is what the linker uses. |
| 97 | + libraryName = libnode.AtOffset(exportTable->Name).AsPtrTo<char>(); |
| 98 | + libraryName = libraryName.substr(0, libraryName.size() - 4); |
| 99 | + |
| 100 | + const uint32_t* functionNameTable = |
| 101 | + libnode.AtOffset(exportTable->AddressOfNames).AsPtrTo<uint32_t>(); |
| 102 | + |
| 103 | + // Given an RVA, parse it as a std::string. The resulting string is empty |
| 104 | + // if the symbol does not have a name (i.e. it is ordinal only). |
| 105 | + auto nameRvaToName = [&](uint32_t rva) -> std::string { |
| 106 | + auto namePtr = libnode.AtOffset(rva).AsPtrTo<char>(); |
| 107 | + if (namePtr == nullptr) return {}; |
| 108 | + return {namePtr}; |
| 109 | + }; |
| 110 | + std::transform(functionNameTable, |
| 111 | + functionNameTable + exportTable->NumberOfNames, |
| 112 | + std::back_inserter(exportedSymbols), |
| 113 | + nameRvaToName); |
| 114 | + } |
| 115 | + |
| 116 | + ~Library() { FreeLibrary(library); } |
| 117 | +}; |
| 118 | + |
| 119 | +bool IsPageExecutable(void* address) { |
| 120 | + MEMORY_BASIC_INFORMATION memoryInformation; |
| 121 | + size_t rc = VirtualQuery( |
| 122 | + address, &memoryInformation, sizeof(MEMORY_BASIC_INFORMATION)); |
| 123 | + |
| 124 | + if (rc != 0 && memoryInformation.Protect != 0) { |
| 125 | + return memoryInformation.Protect == PAGE_EXECUTE || |
| 126 | + memoryInformation.Protect == PAGE_EXECUTE_READ || |
| 127 | + memoryInformation.Protect == PAGE_EXECUTE_READWRITE || |
| 128 | + memoryInformation.Protect == PAGE_EXECUTE_WRITECOPY; |
| 129 | + } |
| 130 | + return false; |
| 131 | +} |
| 132 | + |
| 133 | +Library LoadLibraryOrExit(const char* dllPath) { |
| 134 | + auto library = LoadLibrary(dllPath); |
| 135 | + if (library != nullptr) return library; |
| 136 | + |
| 137 | + auto error = GetLastError(); |
| 138 | + std::cerr << "ERROR: Failed to load " << dllPath << std::endl; |
| 139 | + LPCSTR buffer = nullptr; |
| 140 | + auto rc = FormatMessageA( |
| 141 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, |
| 142 | + nullptr, |
| 143 | + error, |
| 144 | + LANG_USER_DEFAULT, |
| 145 | + (LPSTR)&buffer, |
| 146 | + 0, |
| 147 | + nullptr); |
| 148 | + if (rc != 0) { |
| 149 | + std::cerr << buffer << std::endl; |
| 150 | + LocalFree((HLOCAL)buffer); |
| 151 | + } |
| 152 | + exit(1); |
| 153 | +} |
| 154 | + |
| 155 | +int main(int argc, char** argv) { |
| 156 | + if (argc != 3) { |
| 157 | + std::cerr << "Usage: " << argv[0] |
| 158 | + << " path\\to\\libnode.dll path\\to\\node.def" << std::endl; |
| 159 | + return 1; |
| 160 | + } |
| 161 | + |
| 162 | + auto libnode = LoadLibraryOrExit(argv[1]); |
| 163 | + auto defFile = std::ofstream(argv[2]); |
| 164 | + defFile << "EXPORTS" << std::endl; |
| 165 | + |
| 166 | + for (const std::string& functionName : libnode.exportedSymbols) { |
| 167 | + // If a symbol doesn't have a name then it has been exported as an |
| 168 | + // ordinal only. We assume that only named symbols are exported. |
| 169 | + if (functionName.empty()) continue; |
| 170 | + |
| 171 | + // Every name in the exported symbols table should be resolvable |
| 172 | + // to an address because we have actually loaded the library into |
| 173 | + // our address space. |
| 174 | + auto address = GetProcAddress(libnode.library, functionName.c_str()); |
| 175 | + if (address == nullptr) { |
| 176 | + std::cerr << "WARNING: " << functionName |
| 177 | + << " appears in export table but is not a valid symbol" |
| 178 | + << std::endl; |
| 179 | + continue; |
| 180 | + } |
| 181 | + |
| 182 | + defFile << " " << functionName << " = " << libnode.libraryName << "." |
| 183 | + << functionName; |
| 184 | + |
| 185 | + // Nothing distinguishes exported global data from exported functions |
| 186 | + // with C linkage. If we do not specify the DATA keyword for such symbols |
| 187 | + // then consumers of the .def file will get a linker error. This manifests |
| 188 | + // as nodedbg_ symbols not being found. We assert that if the symbol is in |
| 189 | + // an executable page in this process then it is a function, not data. |
| 190 | + if (!IsPageExecutable(address)) { |
| 191 | + defFile << " DATA"; |
| 192 | + } |
| 193 | + defFile << std::endl; |
| 194 | + } |
| 195 | + |
| 196 | + return 0; |
| 197 | +} |
0 commit comments