[lld] [LLD][COFF] Add support for custom DOS stub (PR #122561)
Hans Wennborg via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 17 04:25:24 PST 2025
================
@@ -1668,21 +1680,38 @@ template <typename PEHeaderTy> void Writer::writeHeader() {
// When run under Windows, the loader looks at AddressOfNewExeHeader and uses
// the PE header instead.
Configuration *config = &ctx.config;
+
uint8_t *buf = buffer->getBufferStart();
auto *dos = reinterpret_cast<dos_header *>(buf);
- buf += sizeof(dos_header);
- dos->Magic[0] = 'M';
- dos->Magic[1] = 'Z';
- dos->UsedBytesInTheLastPage = dosStubSize % 512;
- dos->FileSizeInPages = divideCeil(dosStubSize, 512);
- dos->HeaderSizeInParagraphs = sizeof(dos_header) / 16;
-
- dos->AddressOfRelocationTable = sizeof(dos_header);
- dos->AddressOfNewExeHeader = dosStubSize;
// Write DOS program.
- memcpy(buf, dosProgram, sizeof(dosProgram));
- buf += sizeof(dosProgram);
+ if (config->dosStub) {
+ memcpy(buf, config->dosStub->getBufferStart(),
+ config->dosStub->getBufferSize());
+ // MS link.exe accepts an invalid `e_lfanew` (AddressOfNewExeHeader) and
+ // updates it automatically. Replicate the same behaviour.
+ dos->AddressOfNewExeHeader = config->dosStub->getBufferSize();
+ buf += config->dosStub->getBufferSize();
+ // Unlike MS link.exe, LLD accepts non-8-byte-aligned stubs.
+ // In that case, we add zero paddings ourselves.
+ buf += (8 - (config->dosStub->getBufferSize() % 8)) % 8;
----------------
zmodem wrote:
Sounds good, thanks for confirming.
https://github.com/llvm/llvm-project/pull/122561
More information about the llvm-commits
mailing list