[lld][COFF] Fix bug causing assertion in Chunk::setAlignment
authorAndrew Ng <andrew.ng@sony.com>
Tue, 10 Jan 2023 14:03:48 +0000 (14:03 +0000)
committerAndrew Ng <andrew.ng@sony.com>
Tue, 10 Jan 2023 14:19:53 +0000 (14:19 +0000)
Reinstate use of FakeSection class to avoid constructing SectionChunk
from unintialised coff_section in FakeSectionChunk constructor.

Issue was caused by commit 5a58b19f9c93f3ac51bcde318508131ae78aa10c,
"[LLD] Remove global state in lld/COFF".

lld/COFF/COFFLinkerContext.cpp
lld/COFF/COFFLinkerContext.h
lld/COFF/Chunks.h

index 43902fd..2aba87d 100644 (file)
 namespace lld::coff {
 COFFLinkerContext::COFFLinkerContext()
     : driver(*this), symtab(*this),
-      ltoTextSectionChunk(llvm::COFF::IMAGE_SCN_MEM_EXECUTE),
-      ltoDataSectionChunk(llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA),
+      ltoTextSection(llvm::COFF::IMAGE_SCN_MEM_EXECUTE),
+      ltoDataSection(llvm::COFF::IMAGE_SCN_CNT_INITIALIZED_DATA),
+      ltoTextSectionChunk(&ltoTextSection.section),
+      ltoDataSectionChunk(&ltoDataSection.section),
       rootTimer("Total Linking Time"),
       inputFileTimer("Input File Reading", rootTimer),
       ltoTimer("LTO", rootTimer), gcTimer("GC", rootTimer),
index 311cfb5..059d4ae 100644 (file)
@@ -56,6 +56,8 @@ public:
   }
 
   // Fake sections for parsing bitcode files.
+  FakeSection ltoTextSection;
+  FakeSection ltoDataSection;
   FakeSectionChunk ltoTextSectionChunk;
   FakeSectionChunk ltoDataSectionChunk;
 
index f5335fd..9d9e738 100644 (file)
@@ -715,18 +715,24 @@ void applyArm64Addr(uint8_t *off, uint64_t s, uint64_t p, int shift);
 void applyArm64Imm(uint8_t *off, uint64_t imm, uint32_t rangeLimit);
 void applyArm64Branch26(uint8_t *off, int64_t v);
 
+// Convenience class for initializing a coff_section with specific flags.
+class FakeSection {
+public:
+  FakeSection(int c) { section.Characteristics = c; }
+
+  coff_section section;
+};
+
 // Convenience class for initializing a SectionChunk with specific flags.
 class FakeSectionChunk {
 public:
-  FakeSectionChunk(int c) : chunk(nullptr, &section) {
-    section.Characteristics = c;
+  FakeSectionChunk(const coff_section *section) : chunk(nullptr, section) {
     // Comdats from LTO files can't be fully treated as regular comdats
     // at this point; we don't know what size or contents they are going to
     // have, so we can't do proper checking of such aspects of them.
     chunk.selection = llvm::COFF::IMAGE_COMDAT_SELECT_ANY;
   }
 
-  coff_section section;
   SectionChunk chunk;
 };