[lld] [LLD][COFF] Add support for custom DOS stub (PR #122561)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 13 15:59:24 PST 2025
================
@@ -1668,21 +1680,31 @@ 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->stub.size()) {
+ memcpy(buf, config->stub.data(), config->stub.size());
+ // MS link.exe accepts an invalid `e_lfanew` and updates it automatically.
----------------
kkent030315 wrote:
Yes, `dos_header::AddressOfNewExeHeader` is `IMAGE_DOS_HEADER::e_lfanew` defined in [COFF.h](https://github.com/llvm/llvm-project/blob/051612c0180e4e5a9ba750a994a91d2c1b05b00c/llvm/include/llvm/Object/COFF.h#L57). I'll mention that in the comment.
https://github.com/llvm/llvm-project/pull/122561
More information about the llvm-commits
mailing list