[prev in list] [next in list] [prev in thread] [next in thread]
List: llvm-commits
Subject: [PATCH] D139184: [LLD][Windows]Feature "checksum" for Windows PE
From: Qfrost via Phabricator via llvm-commits <llvm-commits () lists ! llvm ! org>
Date: 2022-12-31 16:46:14
Message-ID: 1SjPuqzuTIOlucJUPnBq9g () geopod-ismtpd-6-1
[Download RAW message or body]
Qfrost911 updated this revision to Diff 485779.
Qfrost911 added a comment.
add commit
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D139184/new/
https://reviews.llvm.org/D139184
Files:
lld/COFF/Config.h
lld/COFF/Driver.cpp
lld/COFF/Options.td
lld/COFF/Writer.cpp
lld/test/COFF/checksum.test
["D139184.485779.patch" (D139184.485779.patch)]
Index: lld/test/COFF/checksum.test
===================================================================
--- /dev/null
+++ lld/test/COFF/checksum.test
@@ -0,0 +1,11 @@
+# RUN: yaml2obj %p/Inputs/hello32.yaml -o %t.obj
+# RUN: lld-link /out:%t.exe /entry:main /timestamp:0 %p/Inputs/std32.lib %t.obj
+# RUN: llvm-readobj --file-headers %t.exe | FileCheck -check-prefix=CHECKSUM1 %s
+
+CHECKSUM1: CheckSum: 0x0
+
+# RUN: yaml2obj %p/Inputs/hello32.yaml -o %t.obj
+# RUN: lld-link /out:%t.exe /entry:main /timestamp:0 /RELEASE %p/Inputs/std32.lib %t.obj
+# RUN: llvm-readobj --file-headers %t.exe | FileCheck -check-prefix=CHECKSUM2 %s
+
+CHECKSUM2: CheckSum: 0x9196
Index: lld/COFF/Writer.cpp
===================================================================
--- lld/COFF/Writer.cpp
+++ lld/COFF/Writer.cpp
@@ -229,6 +229,7 @@
void setSectionPermissions();
void writeSections();
void writeBuildId();
+ void writePEChecksum();
void sortSections();
void sortExceptionTable();
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
@@ -598,6 +599,43 @@
}
}
+void Writer::writePEChecksum() {
+ if (!config->writeCheckSum) {
+ return;
+ }
+
+ // https://docs.microsoft.com/en-us/windows/win32/debug/pe-format#checksum
+ uint32_t *buf = (uint32_t *)buffer->getBufferStart();
+ uint32_t size = (uint32_t)(buffer->getBufferSize());
+
+ coff_file_header *coffHeader =
+ (coff_file_header *)((uint8_t *)buf + dosStubSize + sizeof(PEMagic));
+ pe32_header *peHeader =
+ (pe32_header *)((uint8_t *)coffHeader + sizeof(coff_file_header));
+
+ uint64_t sum = 0;
+ uint32_t count = size;
+ ulittle16_t *addr = (ulittle16_t *)buf;
+
+ // The PE checksum algorithm, implemented as suggested in RFC1071
+ while (count > 1) {
+ sum += *addr++;
+ count -= 2;
+ }
+
+ // Add left-over byte, if any
+ if (count > 0)
+ sum += *(unsigned char *)addr;
+
+ // Fold 32-bit sum to 16 bits
+ while (sum >> 16) {
+ sum = (sum & 0xffff) + (sum >> 16);
+ }
+
+ sum += size;
+ peHeader->CheckSum = sum;
+}
+
// The main function of the writer.
void Writer::run() {
ScopedTimer t1(ctx.codeLayoutTimer);
@@ -646,6 +684,8 @@
writeLLDMapFile(ctx);
writeMapFile(ctx);
+ writePEChecksum();
+
if (errorCount())
return;
Index: lld/COFF/Options.td
===================================================================
--- lld/COFF/Options.td
+++ lld/COFF/Options.td
@@ -161,6 +161,8 @@
def verbose : F<"verbose">;
def wholearchive_flag : F<"wholearchive">,
HelpText<"Include all object files from all libraries">;
+def release : F<"release">,
+ HelpText<"Set the Checksum in the header of an PE file">;
def force : F<"force">,
HelpText<"Allow undefined and multiply defined symbols">;
Index: lld/COFF/Driver.cpp
===================================================================
--- lld/COFF/Driver.cpp
+++ lld/COFF/Driver.cpp
@@ -414,6 +414,9 @@
case OPT_nodefaultlib:
config->noDefaultLibs.insert(doFindLib(arg->getValue()).lower());
break;
+ case OPT_release:
+ config->writeCheckSum = true;
+ break;
case OPT_section:
parseSection(arg->getValue());
break;
@@ -2020,6 +2023,10 @@
if (errorCount())
return;
+ // Handle /RELEASE
+ if (args.hasArg(OPT_release))
+ config->writeCheckSum = true;
+
// Handle /safeseh, x86 only, on by default, except for mingw.
if (config->machine == I386) {
config->safeSEH = args.hasFlag(OPT_safeseh, OPT_safeseh_no, !config->mingw);
Index: lld/COFF/Config.h
===================================================================
--- lld/COFF/Config.h
+++ lld/COFF/Config.h
@@ -289,6 +289,7 @@
bool autoImport = false;
bool pseudoRelocs = false;
bool stdcallFixup = false;
+ bool writeCheckSum = false;
};
extern std::unique_ptr<Configuration> config;
[Attachment #4 (text/plain)]
_______________________________________________
llvm-commits mailing list
llvm-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic