bool autoImport = false;
bool pseudoRelocs = false;
bool stdcallFixup = false;
+ bool writeCheckSum = false;
};
extern std::unique_ptr<Configuration> config;
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;
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);
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">;
void setSectionPermissions();
void writeSections();
void writeBuildId();
+ void writePEChecksum();
void sortSections();
void sortExceptionTable();
void sortCRTSectionChunks(std::vector<Chunk *> &chunks);
}
}
+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);
writeLLDMapFile(ctx);
writeMapFile(ctx);
+ writePEChecksum();
+
if (errorCount())
return;
--- /dev/null
+# 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