Index: lld/COFF/Writer.cpp =================================================================== --- lld/COFF/Writer.cpp +++ lld/COFF/Writer.cpp @@ -655,12 +655,25 @@ SizeOfHeaders = alignTo(SizeOfHeaders, SectorSize); uint64_t RVA = PageSize; // The first page is kept unmapped. FileSize = SizeOfHeaders; - // Move DISCARDABLE (or non-memory-mapped) sections to the end of file because - // the loader cannot handle holes. - std::stable_partition( - OutputSections.begin(), OutputSections.end(), [](OutputSection *S) { - return (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) == 0; - }); + + // There are two constraints on section ordering: + // 1) DISCARDABLE (or non-memory-mapped) sections need to be at the end of + // the file because the loader cannot handle holes. + // 2) The .rsrc section needs to appear at the end of the list of + // non-discardable sections because the UpdateResource function may change + // its size. + auto sectionOrder = [](OutputSection *S) { + if (S->getPermissions() & IMAGE_SCN_MEM_DISCARDABLE) + return 2; + if (S->Name == ".rsrc") + return 1; + return 0; + }; + std::stable_sort(OutputSections.begin(), OutputSections.end(), + [&](OutputSection *S1, OutputSection *S2) { + return sectionOrder(S1) < sectionOrder(S2); + }); + for (OutputSection *Sec : OutputSections) { if (Sec->Name == ".reloc") addBaserels(Sec); Index: lld/test/COFF/combined-resources.test =================================================================== --- lld/test/COFF/combined-resources.test +++ lld/test/COFF/combined-resources.test @@ -11,7 +11,7 @@ # RUN: llvm-readobj -coff-resources -file-headers -section-data %t.exe | \ # RUN: FileCheck %s -CHECK: ResourceTableRVA: 0x1000 +CHECK: ResourceTableRVA: 0x2000 CHECK-NEXT: ResourceTableSize: 0xC1C CHECK-DAG: Resources [ CHECK-NEXT: Total Number of Resources: 13 @@ -49,19 +49,19 @@ CHECK-NEXT: 01E0: 09040000 A0020000 00000000 00000000 |................| CHECK-NEXT: 01F0: 00000000 00000300 09040000 B0020000 |................| CHECK-NEXT: 0200: 04080000 C0020000 07100000 D0020000 |................| -CHECK-NEXT: 0210: FC1A0000 39000000 00000000 00000000 |....9...........| -CHECK-NEXT: 0220: C4130000 28030000 00000000 00000000 |....(...........| -CHECK-NEXT: 0230: EC160000 28030000 00000000 00000000 |....(...........| -CHECK-NEXT: 0240: CC1A0000 30000000 00000000 00000000 |....0...........| -CHECK-NEXT: 0250: 141A0000 2E000000 00000000 00000000 |................| -CHECK-NEXT: 0260: 441A0000 6C000000 00000000 00000000 |D...l...........| -CHECK-NEXT: 0270: 7C130000 2A000000 00000000 00000000 ||...*...........| -CHECK-NEXT: 0280: AC130000 18000000 00000000 00000000 |................| -CHECK-NEXT: 0290: 041C0000 18000000 00000000 00000000 |................| -CHECK-NEXT: 02A0: B41A0000 18000000 00000000 00000000 |................| -CHECK-NEXT: 02B0: 3C1B0000 36000000 00000000 00000000 |<...6...........| -CHECK-NEXT: 02C0: 741B0000 43000000 00000000 00000000 |t...C...........| -CHECK-NEXT: 02D0: BC1B0000 42000000 00000000 00000000 |....B...........| +CHECK-NEXT: 0210: FC2A0000 39000000 00000000 00000000 |.*..9...........| +CHECK-NEXT: 0220: C4230000 28030000 00000000 00000000 |.#..(...........| +CHECK-NEXT: 0230: EC260000 28030000 00000000 00000000 |.&..(...........| +CHECK-NEXT: 0240: CC2A0000 30000000 00000000 00000000 |.*..0...........| +CHECK-NEXT: 0250: 142A0000 2E000000 00000000 00000000 |.*..............| +CHECK-NEXT: 0260: 442A0000 6C000000 00000000 00000000 |D*..l...........| +CHECK-NEXT: 0270: 7C230000 2A000000 00000000 00000000 ||#..*...........| +CHECK-NEXT: 0280: AC230000 18000000 00000000 00000000 |.#..............| +CHECK-NEXT: 0290: 042C0000 18000000 00000000 00000000 |.,..............| +CHECK-NEXT: 02A0: B42A0000 18000000 00000000 00000000 |.*..............| +CHECK-NEXT: 02B0: 3C2B0000 36000000 00000000 00000000 |<+..6...........| +CHECK-NEXT: 02C0: 742B0000 43000000 00000000 00000000 |t+..C...........| +CHECK-NEXT: 02D0: BC2B0000 42000000 00000000 00000000 |.+..B...........| CHECK-NEXT: 02E0: 0E004D00 59004100 43004300 45004C00 |..M.Y.A.C.C.E.L.| CHECK-NEXT: 02F0: 45005200 41005400 4F005200 53000600 |E.R.A.T.O.R.S...| CHECK-NEXT: 0300: 43005500 52005300 4F005200 04004F00 |C.U.R.S.O.R...O.| Index: lld/test/COFF/manifestinput.test =================================================================== --- lld/test/COFF/manifestinput.test +++ lld/test/COFF/manifestinput.test @@ -8,7 +8,7 @@ # RUN: llvm-readobj -coff-resources -file-headers %t.exe | FileCheck %s \ # RUN: -check-prefix TEST_EMBED -TEST_EMBED: ResourceTableRVA: 0x1000 +TEST_EMBED: ResourceTableRVA: 0x2000 TEST_EMBED-NEXT: ResourceTableSize: 0x298 TEST_EMBED-DAG: Resources [ TEST_EMBED-NEXT: Total Number of Resources: 1 Index: lld/test/COFF/resource.test =================================================================== --- lld/test/COFF/resource.test +++ lld/test/COFF/resource.test @@ -10,11 +10,11 @@ # RUN: llvm-readobj -file-headers -coff-resources -section-data %t.exe | \ # RUN: FileCheck --check-prefix=RESOURCE_INFO %s -RESOURCE_INFO: ResourceTableRVA: 0x1000 +RESOURCE_INFO: ResourceTableRVA: 0x2000 RESOURCE_INFO-NEXT: ResourceTableSize: 0x88 RESOURCE_INFO-DAG: Resources [ RESOURCE_INFO-NEXT: Total Number of Resources: 1 -RESOURCE_INFO-NEXT: Base Table Address: 0x400 +RESOURCE_INFO-NEXT: Base Table Address: 0x600 RESOURCE_INFO-DAG: Number of String Entries: 0 RESOURCE_INFO-NEXT: Number of ID Entries: 1 RESOURCE_INFO-NEXT: Type: kRT_STRING (ID 6) [ @@ -36,7 +36,7 @@ RESOURCE_INFO-NEXT: 0010: 06000000 18000080 00000000 00000000 |................| RESOURCE_INFO-NEXT: 0020: 00000000 00000100 01000000 30000080 |............0...| RESOURCE_INFO-NEXT: 0030: 00000000 00000000 00000000 00000100 |................| -RESOURCE_INFO-NEXT: 0040: 09040000 48000000 58100000 2A000000 |....H...X...*...| +RESOURCE_INFO-NEXT: 0040: 09040000 48000000 58200000 2A000000 |....H...X ..*...| RESOURCE_INFO-NEXT: 0050: 00000000 00000000 00000500 48006500 |............H.e.| RESOURCE_INFO-NEXT: 0060: 6C006C00 6F000000 00000000 00000000 |l.l.o...........| RESOURCE_INFO-NEXT: 0070: 00000000 00000000 00000000 00000000 |................|