Skip to content

[LLD][COFF] Add support for custom DOS stub #122561

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 20, 2025
Merged

[LLD][COFF] Add support for custom DOS stub #122561

merged 18 commits into from
Jan 20, 2025

Conversation

kkent030315
Copy link
Contributor

This change implements support for the /stub flag to align with MS link.exe. This option is useful when a program needs to optimize the DOS program that executes when the PE runs on DOS, avoiding the traditional hardcoded DOS program in LLD.

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jan 11, 2025

@llvm/pr-subscribers-lld-coff
@llvm/pr-subscribers-platform-windows

@llvm/pr-subscribers-lld

Author: None (kkent030315)

Changes

This change implements support for the /stub flag to align with MS link.exe. This option is useful when a program needs to optimize the DOS program that executes when the PE runs on DOS, avoiding the traditional hardcoded DOS program in LLD.


Full diff: https://github.com/llvm/llvm-project/pull/122561.diff

10 Files Affected:

  • (modified) lld/COFF/Config.h (+1)
  • (modified) lld/COFF/Driver.cpp (+4)
  • (modified) lld/COFF/Driver.h (+3)
  • (modified) lld/COFF/DriverUtils.cpp (+15)
  • (modified) lld/COFF/Writer.cpp (+29-16)
  • (added) lld/test/COFF/Inputs/stub511mz ()
  • (added) lld/test/COFF/Inputs/stub512mz ()
  • (added) lld/test/COFF/Inputs/stub512zz ()
  • (added) lld/test/COFF/Inputs/stub516mz ()
  • (added) lld/test/COFF/stub.test (+31)
diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h
index 9e6b17e87c9e70..294df1912ff5ae 100644
--- a/lld/COFF/Config.h
+++ b/lld/COFF/Config.h
@@ -115,6 +115,7 @@ struct Configuration {
   enum ManifestKind { Default, SideBySide, Embed, No };
   bool is64() const { return llvm::COFF::is64Bit(machine); }
 
+  llvm::SmallVector<uint8_t> stub;
   llvm::COFF::MachineTypes machine = IMAGE_FILE_MACHINE_UNKNOWN;
   bool machineInferred = false;
   size_t wordsize;
diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp
index 791382fd9bdd4a..9ae25f80d03f01 100644
--- a/lld/COFF/Driver.cpp
+++ b/lld/COFF/Driver.cpp
@@ -2418,6 +2418,10 @@ void LinkerDriver::linkerMain(ArrayRef<const char *> argsArr) {
     config->noSEH = args.hasArg(OPT_noseh);
   }
 
+  // Handle /stub
+  if (auto *arg = args.getLastArg(OPT_stub))
+    parseStub(arg);
+
   // Handle /functionpadmin
   for (auto *arg : args.filtered(OPT_functionpadmin, OPT_functionpadmin_opt))
     parseFunctionPadMin(arg);
diff --git a/lld/COFF/Driver.h b/lld/COFF/Driver.h
index 51325689042981..057f579a8848b9 100644
--- a/lld/COFF/Driver.h
+++ b/lld/COFF/Driver.h
@@ -236,6 +236,9 @@ class LinkerDriver {
   void parseSection(StringRef);
   void parseAligncomm(StringRef);
 
+  // Parses a MS-DOS stub file
+  void parseStub(llvm::opt::Arg *a);
+
   // Parses a string in the form of "[:<integer>]"
   void parseFunctionPadMin(llvm::opt::Arg *a);
 
diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index 1148be09fb10cc..d11187c3447878 100644
--- a/lld/COFF/DriverUtils.cpp
+++ b/lld/COFF/DriverUtils.cpp
@@ -246,6 +246,21 @@ void LinkerDriver::parseAligncomm(StringRef s) {
       std::max(ctx.config.alignComm[std::string(name)], 1 << v);
 }
 
+void LinkerDriver::parseStub(llvm::opt::Arg *a) {
+  StringRef arg = a->getValue();
+  std::unique_ptr<MemoryBuffer> stub = check(MemoryBuffer::getFile(arg));
+  size_t bufferSize = stub->getBufferSize();
+  const char *bufferStart = stub->getBufferStart();
+  // MS link.exe compatibility:
+  // 1. stub must be greater or equal than 64 bytes
+  // 2. stub must be 8-byte aligned
+  // 3. stub must be start with a valid dos signature 'MZ'
+  if (bufferSize < 0x40 || bufferSize % 8 != 0 ||
+      (bufferStart[0] != 'M' || bufferStart[1] != 'Z'))
+    Err(ctx) << "/stub: invalid format for MS-DOS stub file: " << arg;
+  ctx.config.stub.append(bufferStart, bufferStart + bufferSize);
+}
+
 // Parses /functionpadmin option argument.
 void LinkerDriver::parseFunctionPadMin(llvm::opt::Arg *a) {
   StringRef arg = a->getNumValues() ? a->getValue() : "";
diff --git a/lld/COFF/Writer.cpp b/lld/COFF/Writer.cpp
index eb82a9cc015933..73d20360512e60 100644
--- a/lld/COFF/Writer.cpp
+++ b/lld/COFF/Writer.cpp
@@ -79,11 +79,6 @@ static_assert(sizeof(dosProgram) % 8 == 0,
 
 static const int dosStubSize = sizeof(dos_header) + sizeof(dosProgram);
 static_assert(dosStubSize % 8 == 0, "DOSStub size must be multiple of 8");
-static const uint32_t coffHeaderOffset = dosStubSize + sizeof(PEMagic);
-static const uint32_t peHeaderOffset =
-    coffHeaderOffset + sizeof(coff_file_header);
-static const uint32_t dataDirOffset64 =
-    peHeaderOffset + sizeof(pe32plus_header);
 
 static const int numberOfDataDirectory = 16;
 
@@ -315,6 +310,10 @@ class Writer {
   uint64_t sizeOfImage;
   uint64_t sizeOfHeaders;
 
+  uint32_t coffHeaderOffset;
+  uint32_t peHeaderOffset;
+  uint32_t dataDirOffset64;
+
   OutputSection *textSec;
   OutputSection *hexpthkSec;
   OutputSection *rdataSec;
@@ -1668,21 +1667,35 @@ 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.
+    // Replicate the same behaviour.
+    dos->AddressOfNewExeHeader = config->stub.size();
+    buf += config->stub.size();
+  } else {
+    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;
+
+    memcpy(buf, dosProgram, sizeof(dosProgram));
+    buf += sizeof(dosProgram);
+  }
+
+  coffHeaderOffset = buf - buffer->getBufferStart() + sizeof(PEMagic);
+  peHeaderOffset = coffHeaderOffset + sizeof(coff_file_header);
+  dataDirOffset64 = peHeaderOffset + sizeof(pe32plus_header);
 
   // Write PE magic
   memcpy(buf, PEMagic, sizeof(PEMagic));
diff --git a/lld/test/COFF/Inputs/stub511mz b/lld/test/COFF/Inputs/stub511mz
new file mode 100644
index 00000000000000..2a8954d2d6917f
Binary files /dev/null and b/lld/test/COFF/Inputs/stub511mz differ
diff --git a/lld/test/COFF/Inputs/stub512mz b/lld/test/COFF/Inputs/stub512mz
new file mode 100644
index 00000000000000..aaeb005adb54cb
Binary files /dev/null and b/lld/test/COFF/Inputs/stub512mz differ
diff --git a/lld/test/COFF/Inputs/stub512zz b/lld/test/COFF/Inputs/stub512zz
new file mode 100644
index 00000000000000..fa58df18aabe77
Binary files /dev/null and b/lld/test/COFF/Inputs/stub512zz differ
diff --git a/lld/test/COFF/Inputs/stub516mz b/lld/test/COFF/Inputs/stub516mz
new file mode 100644
index 00000000000000..a4ee6b2291e672
Binary files /dev/null and b/lld/test/COFF/Inputs/stub516mz differ
diff --git a/lld/test/COFF/stub.test b/lld/test/COFF/stub.test
new file mode 100644
index 00000000000000..2d5ad351d407d2
--- /dev/null
+++ b/lld/test/COFF/stub.test
@@ -0,0 +1,31 @@
+# RUN: yaml2obj %p/Inputs/ret42.yaml -o %t.obj
+
+# RUN: lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub512mz %t.obj
+# RUN: llvm-readobj --file-headers %t.exe | FileCheck -check-prefix=CHECK1 %s
+
+CHECK1: Magic: MZ
+CHECK1: UsedBytesInTheLastPage: 144
+CHECK1: FileSizeInPages: 3
+CHECK1: NumberOfRelocationItems: 0
+CHECK1: HeaderSizeInParagraphs: 4
+CHECK1: MinimumExtraParagraphs: 0
+CHECK1: MaximumExtraParagraphs: 65535
+CHECK1: InitialRelativeSS: 0
+CHECK1: InitialSP: 184
+CHECK1: Checksum: 0
+CHECK1: InitialIP: 0
+CHECK1: InitialRelativeCS: 0
+CHECK1: AddressOfRelocationTable: 64
+CHECK1: OverlayNumber: 0
+CHECK1: OEMid: 0
+CHECK1: OEMinfo: 0
+CHECK1: AddressOfNewExeHeader: 64
+
+# RUN: not lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub512zz %t.obj 2>&1 | FileCheck -check-prefix=CHECK2 %s
+# CHECK2: lld-link: error: /stub: invalid format for MS-DOS stub file: {{.*}}
+
+# RUN: not lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub516mz %t.obj 2>&1 | FileCheck -check-prefix=CHECK3 %s
+# CHECK3: lld-link: error: /stub: invalid format for MS-DOS stub file: {{.*}}
+
+# RUN: not lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub511mz %t.obj 2>&1 | FileCheck -check-prefix=CHECK4 %s
+# CHECK4: lld-link: error: /stub: invalid format for MS-DOS stub file: {{.*}}

This change implements support for the /stub flag to align with MS link.exe. This option is useful when a program needs to optimize the DOS program that executes when the PE runs on DOS, avoiding the traditional hardcoded DOS program in LLD.
Copy link
Member

@mstorsjo mstorsjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks mostly quite reasonable.

I wonder if there's any way that we could generate the input data without needing to commit binaries for it, but perhaps this is one of the cases where binaries are warranted.

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I presume e_lfanew is a different name for AddressOfNewExeHeader? This is a bit hard to follow if one doesn't have the struct definitions handy while reading the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, dos_header::AddressOfNewExeHeader is IMAGE_DOS_HEADER::e_lfanew defined in COFF.h. I'll mention that in the comment.

# CHECK2: lld-link: error: /stub: invalid format for MS-DOS stub file: {{.*}}

# RUN: not lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub516mz %t.obj 2>&1 | FileCheck -check-prefix=CHECK3 %s
# CHECK3: lld-link: error: /stub: invalid format for MS-DOS stub file: {{.*}}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As all these three checks for the negative tests are the same, you could just as well use one single CHECK-INVALID: for all of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CHECK{2,3,4} are technically different negative tests.

  • CHECK1: Invalid DOS signature (must be MZ)
  • CHECK2: Unaligned stub (must be aligned to 8)
  • CHECK3: Too-small stub (must be at least 64 bytes long)

I'll add comments for this one as well :).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm combining to a single CHECK-INVALID, done.

@@ -115,6 +115,7 @@ struct Configuration {
enum ManifestKind { Default, SideBySide, Embed, No };
bool is64() const { return llvm::COFF::is64Bit(machine); }

llvm::SmallVector<uint8_t> stub;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could use std::unique_ptr<MemoryBuffer> here to avoid copying the blob in parseStub?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes :).

size_t bufferSize = stub->getBufferSize();
const char *bufferStart = stub->getBufferStart();
// MS link.exe compatibility:
// 1. stub must be greater or equal than 64 bytes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small nitpick, but, I presume you mean "greater than or equal (to)"?

## Unaligned stub (must be aligned to 8)
# RUN: not lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub516mz %t.obj 2>&1 | FileCheck -check-prefix=CHECK-INVALID %s

## Too-small stub (must be at least 64 bytes long)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this case also fail due to not being aligned to 8 bytes? So without seeing the implementation (or looking at an error message telling the exact reason), we don't strictly know if we're hitting the intended error, or erroring out due to some other reason.

Also, the numbers in the file names, 511/512/516 are quite puzzling how they relate to the actual file sizes (63/64/68 bytes). Can you enlighten me, or rename them to include the actual file size in the name instead :-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and nice catch, I'll fix the file names. They used to be 511/512/516 sizes and I compressed before pushing out and forgot to rename them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this case also fail due to not being aligned to 8 bytes? So without seeing the implementation (or looking at an error message telling the exact reason), we don't strictly know if we're hitting the intended error, or erroring out due to some other reason.

Too-small stub (63 bytes) is not aligned to 8 bytes, yes.

For the latter, I was thinking the similar. Maybe it's better to put an exact cause for each malformed patterns. For now I followed the way how LINK.exe puts an error (they only put "invalid format" in any of the case). Should I do that?

Copy link
Contributor Author

@kkent030315 kkent030315 Jan 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be something like:

  if (bufferSize < 0x40)
    Err(ctx) << "/stub: stub must be greater than or equal to 64 bytes: " << path;
  if (bufferSize % 8 != 0)
    Err(ctx) << "/stub: stub must be aligned to 8 bytes: " << path;
  if (bufferStart[0] != 'M' || bufferStart[1] != 'Z')
    Err(ctx) << "/stub: invalid DOS signature: " << path;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could probably be valuable to add more value in the error messages, even if link.exe doesn't; we don't need to match link.exe bit-by-bit exact in such details anyway. (For some larger cases, we print matching LNK123 error numbers, but most of other warnings/errors are different anyway.)

If we can grep for the right error message, this is probably fine, but otherwise it'd be good to make the file e.g. 56 bytes, so that it does pass the alignment test, so we trigger specifically the error we want to test, regardless of in which order the checks are made.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That sounds reasonable to me, thanks!

@@ -115,6 +115,7 @@ struct Configuration {
enum ManifestKind { Default, SideBySide, Embed, No };
bool is64() const { return llvm::COFF::is64Bit(machine); }

std::unique_ptr<MemoryBuffer> stub;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe dosStub would be clearer?

@@ -236,6 +236,9 @@ class LinkerDriver {
void parseSection(StringRef);
void parseAligncomm(StringRef);

// Parses a MS-DOS stub file
void parseStub(StringRef path);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if you renamed this to parseDosStub there would be no need to have a comment for it.

const char *bufferStart = stub->getBufferStart();
// MS link.exe compatibility:
// 1. stub must be greater or equal than 64 bytes
// 2. stub must be 8-byte aligned
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the size must be a multiple of 8? Does link.exe really require that? I suppose the PE header needs to be 8-byte aligned, but the linker could easily add padding after the stub.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the size must be a multiple of 8? Does link.exe really require that?

Yes, I already tested that and confirmed that LINK.exe does not allow unaligned stub input. :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't feel too strongly about it, but unless there's a technical reason to reject such stubs, I think we should accept also non-8-byte-aligned programs and add any padding ourselves.

https://learn.microsoft.com/en-us/cpp/build/reference/stub-ms-dos-stub-file-name?view=msvc-170 says "any valid MS-DOS application [.exe file] can be a stub program", for example.

Copy link
Contributor Author

@kkent030315 kkent030315 Jan 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I am pretty sure we should keep MZ dos signature check. On the other hand, there's nothing prevents us to accept unaligned stubs, if we don't have to replicate MS linker bug-by-bug. It's implemented on 94ac28f183d994e0ba5f61a9fcc788369be95970 and I'd love to hear feedbacks from you :) .

// 3. stub must be start with a valid dos signature 'MZ'
if (bufferSize < 0x40 || bufferSize % 8 != 0 ||
(bufferStart[0] != 'M' || bufferStart[1] != 'Z'))
Err(ctx) << "/stub: invalid format for MS-DOS stub file: " << path;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be helpful if the error message could mention which requirement (size, signature, ..) failed.

(Useless trivia: 'zm' is also a valid dos .exe signature, but the windows loader doesn't like it: https://www.hanshq.net/making-executables.html#zm)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure I'll improve the error messages. And also I tested that ZM with LINK.exe and confirmed that it does not allowed so I think we should keep the DOS magic thing there.

const char *bufferStart = stub->getBufferStart();
// MS link.exe compatibility:
// 1. stub must be greater than or equal to 64 bytes
// 2. stub must be start with a valid dos signature 'MZ'
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: drop the "be"

// MS link.exe compatibility:
// 1. stub must be greater than or equal to 64 bytes
// 2. stub must be start with a valid dos signature 'MZ'
if (bufferSize < 0x40)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: maybe just use 64 instead of 0x40 since we say "64 bytes" in the comments and error message etc.

else
dosStubSize = sizeof(dos_header) + sizeof(dosProgram);

coffHeaderOffset = dosStubSize + sizeof(PEMagic);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the PEMagic needs to be 8-byte aligned, I think we need to round up dosStubSize here: alignTo(dosStubSize, 8).

Also, we need to audit all the uses of dosStubSize. For example, Writer::writePEChecksum() uses dosStubSize to locate the coffHeader. It should use coffHeaderOffset instead.

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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need rounding here.

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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better to use alignTo().

And how is buf's size computed? Can we be sure there's room for this potential extra padding?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the final file size (fileSize) is computed in Writer::assignAddresses() based on sizeofHeaders and the caller Writer::run() checks buf size: if (fileSize > UINT32_MAX) before committing to a final file. So technically there must be an enough space for the paddings (at most it'd be 7 bytes long), except the case where the size of given stub input exceeds gigabytes (wow). Needless to say, such considered an expected behaviour imo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, thanks for confirming.

CHECK2: lld-link: error: /stub: invalid DOS signature: {{.*}}

## Unlike MS linker, we accept non-8byte-aligned stubs and we add paddings ourselves
# RUN: lld-link /out:%t.exe /entry:main /stub:%p/Inputs/stub64mz %t.obj
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stub64mz sounds like it is aligned though. Did you mean stub68mz?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it was accidentally swapped to stub64mz instead of stub68mz. thanks for pointing out!

@zmodem
Copy link
Collaborator

zmodem commented Jan 17, 2025

Besides one tiny nit, this lgtm.

size_t bufferSize = stub->getBufferSize();
const char *bufferStart = stub->getBufferStart();
// MS link.exe compatibility:
// 1. stub must greater than or equal to 64 bytes
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't mean for you to remove "be" on this line though :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's fixed, thank you for pointing out!

Copy link
Collaborator

@zmodem zmodem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mstorsjo mstorsjo merged commit fb974e8 into llvm:main Jan 20, 2025
8 checks passed
Copy link

@kkent030315 Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@kkent030315 kkent030315 deleted the stub branch January 21, 2025 17:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants