Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# is provided to nmake
# test -

!IFNDEF "$(PREFIX)"
!IFNDEF PREFIX
PREFIX="$(MAKEDIR)\install\"
!ENDIF

Expand Down Expand Up @@ -64,12 +64,12 @@ cl.exe : $(SRCS)
link $(LFLAGS) $** $(API_LIBS) /out:cl.exe

install : cl.exe
mkdir $(PREFIX)
move cl.exe $(PREFIX)
mklink $(PREFIX)\link.exe $(PREFIX)\cl.exe
mklink $(PREFIX)\ifx.exe $(PREFIX)\cl.exe
mklink $(PREFIX)\ifort.exe $(PREFIX)\ifort.exe
mklink $(PREFIX)\relocate.exe $(PREFIX)\cl.exe
@if not exist "$(PREFIX)" mkdir "$(PREFIX)""
@if not exist "$(PREFIX)\cl.exe" move cl.exe "$(PREFIX)"
@if not exist "$(PREFIX)\link.exe" mklink "$(PREFIX)\link.exe" "$(PREFIX)\cl.exe"
@if not exist "$(PREFIX)\ifx.exe" mklink "$(PREFIX)\ifx.exe" "$(PREFIX)\cl.exe"
@if not exist "$(PREFIX)\ifort.exe" mklink "$(PREFIX)\ifort.exe" "$(PREFIX)\cl.exe"
@if not exist "$(PREFIX)\relocate.exe" mklink "$(PREFIX)\relocate.exe" "$(PREFIX)\cl.exe"

setup_test: cl.exe
@echo \n
Expand Down
3 changes: 2 additions & 1 deletion src/ld.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,8 @@ std::unique_ptr<RCFileManager> LdInvocation::createRC(LinkerInvocation& link_run
const std::string rc_file_name = join({rc_tmp_dir, base_rc_file_name}, "\\");

ExecuteCommand rc_executor("rc",
{"/fo" + res_file_name + " " + rc_file_name});
{"/fo\"" + res_file_name + "\"",
"\"" + rc_file_name + "\""});
std::ofstream rc_out(rc_file_name);
if (!rc_out) {
std::cerr << "Error: could not open rc file for creation: "
Expand Down
2 changes: 1 addition & 1 deletion src/toolchain.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void ToolChainInvocation::ParseCommandArgs(char const* const* cli) {
}

std::string ToolChainInvocation::ComposeIncludeArg(std::string& include) {
return "/external:I " + include;
return "/external:I\"" + include + "\"";
}

std::string ToolChainInvocation::ComposeLibPathArg(std::string& libPath) {
Expand Down
2 changes: 1 addition & 1 deletion src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ class LibraryFinder {

class PathRelocator {
private:
bool bc_;
bool bc_ = true; // default: build-cache prefix→prefix mode
std::string new_prefix_;
std::map<std::string, std::string> old_new_map;
std::string relocateBC(std::string const& pe);
Expand Down
67 changes: 47 additions & 20 deletions src/winrpath.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,42 @@ bool LibRename::FindDllAndRename(HANDLE& pe_in) {
}
// Establish base PE headers
auto* dos_header = static_cast<PIMAGE_DOS_HEADER>(basepointer);
auto* nt_header = reinterpret_cast<PIMAGE_NT_HEADERS>(
if (dos_header->e_magic != IMAGE_DOS_SIGNATURE) {
std::cerr << "Error: Invalid DOS signature (Not an Executable/DLL file).\n";
return false;
}
auto* pe_signature = reinterpret_cast<DWORD*>(
static_cast<char*>(basepointer) + dos_header->e_lfanew);

if (*pe_signature != IMAGE_NT_SIGNATURE) {
std::cerr << "Error: Invalid PE signature (Not a Windows PE binary).\n";
return false;
}

auto* coff_header = reinterpret_cast<PIMAGE_FILE_HEADER>(
static_cast<char*>(basepointer) + dos_header->e_lfanew +
sizeof(nt_header->Signature));
static_cast<char*>(basepointer) + dos_header->e_lfanew + sizeof(DWORD));

auto* raw_optional_ptr = static_cast<char*>(basepointer) + dos_header->e_lfanew +
sizeof(DWORD) + sizeof(IMAGE_FILE_HEADER);

WORD const magic = *reinterpret_cast<WORD*>(raw_optional_ptr);

auto* optional_header = reinterpret_cast<PIMAGE_OPTIONAL_HEADER>(
static_cast<char*>(basepointer) + dos_header->e_lfanew +
sizeof(nt_header->Signature) + sizeof(nt_header->FileHeader));
DWORD number_of_rva_and_sections = 0;
DWORD rva_import_directory = 0;

if (magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC) {
auto* opt64 = reinterpret_cast<PIMAGE_OPTIONAL_HEADER64>(raw_optional_ptr);
number_of_rva_and_sections = opt64->NumberOfRvaAndSizes;
rva_import_directory = opt64->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
} else {
auto* opt32 = reinterpret_cast<PIMAGE_OPTIONAL_HEADER32>(raw_optional_ptr);
number_of_rva_and_sections = opt32->NumberOfRvaAndSizes;
rva_import_directory = opt32->DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress;
}

auto* section_header = reinterpret_cast<PIMAGE_SECTION_HEADER>(
static_cast<char*>(basepointer) + dos_header->e_lfanew +
sizeof(nt_header->Signature) + sizeof(nt_header->FileHeader) +
sizeof(nt_header->OptionalHeader));
raw_optional_ptr + coff_header->SizeOfOptionalHeader);

DWORD const number_of_rva_and_sections =
optional_header->NumberOfRvaAndSizes;
if (number_of_rva_and_sections == 0) {
std::cerr << "PE file does not import symbols" << "\n";
return false;
Expand All @@ -150,13 +168,22 @@ bool LibRename::FindDllAndRename(HANDLE& pe_in) {
return false;
}

if (rva_import_directory == 0) {
// No import table is valid (e.g. python3.dll which only forwards exports).
FlushViewOfFile((LPCVOID)basepointer, 0);
UnmapViewOfFile((LPCVOID)basepointer);
return SafeHandleCleanup(h_map_object) != 0;
}

DWORD const number_of_sections = coff_header->NumberOfSections;
// Data directory #2 points to the RVA of the import section
DWORD const rva_import_directory =
nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT]
.VirtualAddress;
DWORD const import_section_file_offset = RvaToFileOffset(
section_header, number_of_sections, rva_import_directory);
if (import_section_file_offset == 0) {
std::cerr << "Import table RVA 0x" << std::hex << rva_import_directory
<< " could not be resolved to a file offset.\n";
UnmapViewOfFile((LPCVOID)basepointer);
return false;
}
char* import_table_offset =
static_cast<char*>(basepointer) + import_section_file_offset;
auto* import_image_descriptor =
Expand Down Expand Up @@ -225,7 +252,7 @@ LibRename::LibRename(std::string p_exe, std::string coff, bool full,
* Produces something like `/EXPORTS <name of coff file>`
*/
std::string LibRename::ComputeDefLine() {
return "/NOLOGO /EXPORTS " + this->coff;
return "/NOLOGO /EXPORTS \"" + this->coff + "\"";
}

/**
Expand Down Expand Up @@ -440,10 +467,10 @@ bool LibRename::ExecutePERename() {
*
*/
std::string LibRename::ComputeRenameLink() {
std::string line("-def:");
line += this->def_file + " ";
line += "-name:";
line += mangle_name(this->pe) + " ";
std::string line("-def:\"");
line += this->def_file + "\" ";
line += "-name:\"";
line += mangle_name(this->pe) + "\" ";
std::string const name(stem(this->coff));
if (!this->replace) {
this->new_lib = name + ".abs-name.lib";
Expand Down
Loading