Skip to content

coff: initial rewrite of the COFF/PE linker - #12677

Merged
kubkon merged 21 commits into
masterfrom
coff-linker
Aug 30, 2022
Merged

kubkon merged 21 commits into
masterfrom
coff-linker

Conversation

@kubkon

@kubkon kubkon commented Aug 29, 2022 •

Copy link
Copy Markdown
Member

tl;dr we can correctly link and execute a very crude and hacky program of the form:

pub export fn _start() noreturn {
    exit();
}

fn exit() noreturn {
    asm volatile ("syscall"
        :
        : [arg1] "{r10}" (0),
          [arg2] "{rdx}" (0xc0000005),
          [number] "{eax}" (0x2c),
    );
    unreachable;
}

This is clearly not a program you'd expect to every see for Windows as we call into a syscall directly, but nonetheless, it was minimal enough just to test some basic assumptions about the PE/COFF structure. Also, for interested, 0x2c corresponds to NtTerminateProcess in latest Win10 (a word of warning, Microsoft changes those very regularly).

In order to simplify working on the incremental COFF linker, I have move all code that deals with LLD into Coff/lld.zig - this way, Coff.zig has manageable LOCs.

Finally, you have hopefully noticed that in the example above I have made _start the default entry point for COFF - this is incorrect, and wWinMainCRTStartup or wMainCRTStartup (or any of the million alternatives) are the right way to go, however, for simplicity and for the time being, I will leave it at _start. Once I figure out how to link with ntdll in the linker, I will bring it back.

kubkon added 20 commits August 30, 2022 10:42
Maximum number is always 16, and this also unbreaks `dumpbin.exe`
run on a simple section-less PE image created with our linker.
I believe this is going to be vital for section headers having names
that require the use of a string table.
This is not technically correct, but given that we are not yet able
to link against the CRT, it's a good default until then.

Add basic logging of generated symbol table in the linker.
Given that COFF will want to support PIC from ground-up, there is no
point in leaving outdated code for COFF in other backends such as
arm or aarch64. Instead, when we are ready to look into those, we
can start figuring out what to add and where.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant