This is an archive of the discontinued LLVM Phabricator instance.

[COFF] Make the DOS stub a real DOS program
ClosedPublic

Authored by hans on Mar 2 2018, 8:22 AM.

Diff Detail

Repository
rLLD LLVM Linker

Event Timeline

hans created this revision.Mar 2 2018, 8:22 AM
hans added inline comments.
COFF/Writer.cpp
45

Grateful for ideas on how to format/do this better.

700

Is there a guarantee that the buffer is zero-initialized? Otherwise maybe we should memset DOS here, because it's important that the un-set fields are really zero if this is going to be loaded.

ruiu added inline comments.Mar 2 2018, 11:04 AM
COFF/Writer.cpp
64

Maybe it's easier to make it always 64-byte long and use sizeof(DOSProgram) instead of alignTo()

static unsigned char DOSProgram[64] = { ... };
692–696

It's perhaps better to expand this comment a bit so that the thing we are doing here is not important to understand the linker itself -- it's just a small DOS program at beginning of every PE/COFF executable to help DOS 2.0 (!) users that the program needs Windows.

700

The buffer is a new mmap'ed file, so it is guaranteed to be zero-initialized.

(The situation is different for ELF executable sections because we write INT3 or equivalent before writing anything to that regions, but that's not relevant here because this is COFF header.)

hans added inline comments.Mar 5 2018, 3:00 AM
COFF/Writer.cpp
64

That seems wasteful since the program doesn't need 64 bytes (and it seems fragile in case it would ever change and require more than 64 bytes).

A better idea I think would be to put an

align   8, db 0

directive in the asm above to pad the program itself to a multiple of 8. That way we're not wasting bytes, it won't break if someone changes the program, and it simplifies the lld code a little. What do you think?

ruiu accepted this revision.Mar 7 2018, 8:33 AM

LGTM

This revision is now accepted and ready to land.Mar 7 2018, 8:33 AM
hans added a comment.Mar 8 2018, 6:29 AM

LGTM

Thanks! I'll make the change and commit.

COFF/Writer.cpp
692–696

I've expanded the comment to explain.

This revision was automatically updated to reflect the committed changes.