[lld] r364660 - [COFF] Fix .rsrc sections with differing permissions

Martin Storsjo via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 28 10:13:53 PDT 2019


Author: mstorsjo
Date: Fri Jun 28 10:13:52 2019
New Revision: 364660

URL: http://llvm.org/viewvc/llvm-project?rev=364660&view=rev
Log:
[COFF] Fix .rsrc sections with differing permissions

GNU windres, and MS cvtres (unless the /readonly option is passed)
produce read-write .rsrc sections, when creating resource object files.
This caused the sections to not be added to the precreated RsrcSec,
and therefore not be added to the data directory.

Differential Revision: https://reviews.llvm.org/D63837

Added:
    lld/trunk/test/COFF/Inputs/id.res.o
    lld/trunk/test/COFF/resource-objs.test
Modified:
    lld/trunk/COFF/Writer.cpp

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=364660&r1=364659&r2=364660&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Fri Jun 28 10:13:52 2019
@@ -219,6 +219,7 @@ private:
   void sortExceptionTable();
   void sortCRTSectionChunks(std::vector<Chunk *> &Chunks);
   void addSyntheticIdata();
+  void fixPartialSectionChars(StringRef Name, uint32_t Chars);
   bool fixGnuImportChunks();
   PartialSection *createPartialSection(StringRef Name, uint32_t OutChars);
   PartialSection *findPartialSection(StringRef Name, uint32_t OutChars);
@@ -652,6 +653,24 @@ static void sortBySectionOrder(std::vect
   });
 }
 
+// Change the characteristics of existing PartialSections that belong to the
+// section Name to Chars.
+void Writer::fixPartialSectionChars(StringRef Name, uint32_t Chars) {
+  for (auto It : PartialSections) {
+    PartialSection *PSec = It.second;
+    StringRef CurName = PSec->Name;
+    if (!CurName.consume_front(Name) ||
+        (!CurName.empty() && !CurName.startswith("$")))
+      continue;
+    if (PSec->Characteristics == Chars)
+      continue;
+    PartialSection *DestSec = createPartialSection(PSec->Name, Chars);
+    DestSec->Chunks.insert(DestSec->Chunks.end(), PSec->Chunks.begin(),
+                           PSec->Chunks.end());
+    PSec->Chunks.clear();
+  }
+}
+
 // Sort concrete section chunks from GNU import libraries.
 //
 // GNU binutils doesn't use short import files, but instead produces import
@@ -667,17 +686,7 @@ bool Writer::fixGnuImportChunks() {
 
   // Make sure all .idata$* section chunks are mapped as RDATA in order to
   // be sorted into the same sections as our own synthesized .idata chunks.
-  for (auto It : PartialSections) {
-    PartialSection *PSec = It.second;
-    if (!PSec->Name.startswith(".idata"))
-      continue;
-    if (PSec->Characteristics == RDATA)
-      continue;
-    PartialSection *RDataSec = createPartialSection(PSec->Name, RDATA);
-    RDataSec->Chunks.insert(RDataSec->Chunks.end(), PSec->Chunks.begin(),
-                            PSec->Chunks.end());
-    PSec->Chunks.clear();
-  }
+  fixPartialSectionChars(".idata", RDATA);
 
   bool HasIdata = false;
   // Sort all .idata$* chunks, grouping chunks from the same library,
@@ -808,6 +817,7 @@ void Writer::createSections() {
     PSec->Chunks.push_back(C);
   }
 
+  fixPartialSectionChars(".rsrc", DATA | R);
   // Even in non MinGW cases, we might need to link against GNU import
   // libraries.
   bool HasIdata = fixGnuImportChunks();

Added: lld/trunk/test/COFF/Inputs/id.res.o
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/Inputs/id.res.o?rev=364660&view=auto
==============================================================================
Binary files lld/trunk/test/COFF/Inputs/id.res.o (added) and lld/trunk/test/COFF/Inputs/id.res.o Fri Jun 28 10:13:52 2019 differ

Added: lld/trunk/test/COFF/resource-objs.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/resource-objs.test?rev=364660&view=auto
==============================================================================
--- lld/trunk/test/COFF/resource-objs.test (added)
+++ lld/trunk/test/COFF/resource-objs.test Fri Jun 28 10:13:52 2019
@@ -0,0 +1,5 @@
+# RUN: lld-link /out:%t.exe /dll /noentry %S/Inputs/id.res.o
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s
+
+CHECK: ResourceTableRVA: 0x1000
+CHECK: ResourceTableSize: 0x80




More information about the llvm-commits mailing list