[PATCH] D63837: [LLD] [COFF] Fix .rsrc sections with differing permissions

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 26 12:35:03 PDT 2019


mstorsjo created this revision.
mstorsjo added reviewers: ruiu, rnk, thakis.
Herald added a project: LLVM.

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


Repository:
  rLLD LLVM Linker

https://reviews.llvm.org/D63837

Files:
  COFF/Writer.cpp
  test/COFF/Inputs/id.res.obj
  test/COFF/resource-objs.test


Index: test/COFF/resource-objs.test
===================================================================
--- /dev/null
+++ test/COFF/resource-objs.test
@@ -0,0 +1,5 @@
+# RUN: lld-link /out:%t.exe /dll /noentry %S/Inputs/id.res.obj
+# RUN: llvm-readobj -file-headers %t.exe | FileCheck %s
+
+CHECK: ResourceTableRVA: 0x1000
+CHECK: ResourceTableSize: 0x80
Index: COFF/Writer.cpp
===================================================================
--- COFF/Writer.cpp
+++ COFF/Writer.cpp
@@ -219,6 +219,7 @@
   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,22 @@
   });
 }
 
+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 +684,7 @@
 
   // 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 +815,7 @@
     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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63837.206725.patch
Type: text/x-patch
Size: 2609 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190626/4575798f/attachment.bin>


More information about the llvm-commits mailing list