aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2019-01-23 02:07:10 +0000
committerNico Weber <nicolasweber@gmx.de>2019-01-23 02:07:10 +0000
commit637390b315e1dc41396d1f757c17943fecdef816 (patch)
treee66f9d222d7f6963cf5532755c4621fce4694be3
parentf6abb4b500f4f30f8ced624c7ff53e192f4c38e4 (diff)
lld-link: Use just one code path to process associative comdats, reject some invalid associated comdats
Currently, if an associative comdat appears after the comdat it's associated with it's processed immediately, else it's deferred until the end of the object file. I found this confusing to think about while working on PR40094, so this makes it so that associated comdats are always processed at the end of the object file. This seems to be perf-neutral and simpler. Now there's a natural place to reject the associated comdats referring to later associated comdats (associated comdats referring to associated comdats is invalid per COFF spec) that, so reject those. (A later patch will reject associated comdats referring to earlier comdats.) Differential Revision: https://reviews.llvm.org/D56929 git-svn-id: https://llvm.org/svn/llvm-project/lld/trunk@351917 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--COFF/InputFiles.cpp38
-rw-r--r--test/COFF/associative-comdat-empty.s56
-rw-r--r--test/COFF/associative-comdat-order.s85
3 files changed, 161 insertions, 18 deletions
diff --git a/COFF/InputFiles.cpp b/COFF/InputFiles.cpp
index 4753de6a6..f291a9dbf 100644
--- a/COFF/InputFiles.cpp
+++ b/COFF/InputFiles.cpp
@@ -223,14 +223,23 @@ void ObjFile::readAssociativeDefinition(
void ObjFile::readAssociativeDefinition(COFFSymbolRef Sym,
const coff_aux_section_definition *Def,
- uint32_t ParentSection) {
- SectionChunk *Parent = SparseChunks[ParentSection];
+ uint32_t ParentIndex) {
+ SectionChunk *Parent = SparseChunks[ParentIndex];
+
+ if (Parent == PendingComdat) {
+ // This can happen if an associative comdat refers to another associative
+ // comdat that appears after it (invalid per COFF spec) or to a section
+ // without any symbols.
+ StringRef Name, ParentName;
+ COFFObj->getSymbolName(Sym, Name);
- // If the parent is pending, it probably means that its section definition
- // appears after us in the symbol table. Leave the associated section as
- // pending; we will handle it during the second pass in initializeSymbols().
- if (Parent == PendingComdat)
+ const coff_section *ParentSec;
+ COFFObj->getSection(ParentIndex, ParentSec);
+ COFFObj->getSectionName(ParentSec, ParentName);
+ error(toString(this) + ": associative comdat " + Name +
+ " has invalid reference to section " + ParentName);
return;
+ }
// Check whether the parent is prevailing. If it is, so are we, and we read
// the section; otherwise mark it as discarded.
@@ -326,8 +335,7 @@ void ObjFile::initializeSymbols() {
// was pending at the point when the symbol was read. This can happen in
// two cases:
// 1) section definition symbol for a comdat leader;
- // 2) symbol belongs to a comdat section associated with a section whose
- // section definition symbol appears later in the symbol table.
+ // 2) symbol belongs to a comdat section associated with another section.
// In both of these cases, we can expect the section to be resolved by
// the time we finish visiting the remaining symbols in the symbol
// table. So we postpone the handling of this symbol until that time.
@@ -437,21 +445,15 @@ Optional<Symbol *> ObjFile::createDefined(
return Leader;
}
- // Read associative section definitions and prepare to handle the comdat
- // leader symbol by setting the section's ComdatDefs pointer if we encounter a
- // non-associative comdat.
+ // Prepare to handle the comdat leader symbol by setting the section's
+ // ComdatDefs pointer if we encounter a non-associative comdat.
if (SparseChunks[SectionNumber] == PendingComdat) {
if (const coff_aux_section_definition *Def = Sym.getSectionDefinition()) {
- if (Def->Selection == IMAGE_COMDAT_SELECT_ASSOCIATIVE)
- readAssociativeDefinition(Sym, Def);
- else
+ if (Def->Selection != IMAGE_COMDAT_SELECT_ASSOCIATIVE)
ComdatDefs[SectionNumber] = Def;
}
- }
-
- // readAssociativeDefinition() writes to SparseChunks, so need to check again.
- if (SparseChunks[SectionNumber] == PendingComdat)
return None;
+ }
return createRegular(Sym);
}
diff --git a/test/COFF/associative-comdat-empty.s b/test/COFF/associative-comdat-empty.s
new file mode 100644
index 000000000..1499d9074
--- /dev/null
+++ b/test/COFF/associative-comdat-empty.s
@@ -0,0 +1,56 @@
+# RUN: yaml2obj < %s > %t.obj
+# RUN: not lld-link /include:symbol /dll /noentry /nodefaultlib %t.obj /out:%t.exe 2>&1 | FileCheck %s
+
+# Tests an associative comdat being associated with an empty section errors.
+# CHECK: lld-link: error: {{.*}}: associative comdat .text$ac1 has invalid reference to section .text$nm
+# CHECK-NOT: lld-link: error:
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: '.text$ac1'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 1
+ SectionData: '01000000'
+ - Name: '.text$nm'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 1
+ SectionData: '01000000'
+symbols:
+ - Name: '.text$ac1'
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 4
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 3099354981
+ Number: 2
+ Selection: IMAGE_COMDAT_SELECT_ASSOCIATIVE
+ - Name: '.text$nm'
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 0
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 3099354981
+ Number: 4
+ Selection: IMAGE_COMDAT_SELECT_ANY
+ - Name: assocsym
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+...
+
+
diff --git a/test/COFF/associative-comdat-order.s b/test/COFF/associative-comdat-order.s
new file mode 100644
index 000000000..31105d512
--- /dev/null
+++ b/test/COFF/associative-comdat-order.s
@@ -0,0 +1,85 @@
+# RUN: yaml2obj < %s > %t.obj
+# RUN: not lld-link /include:symbol /dll /noentry /nodefaultlib %t.obj /out:%t.exe 2>&1 | FileCheck %s
+
+# Tests that an associative comdat being associated with another
+# associated comdat later in the file produces an error.
+# CHECK: lld-link: error: {{.*}}: associative comdat .text$ac1 has invalid reference to section .text$ac2
+# CHECK-NOT: lld-link: error:
+
+--- !COFF
+header:
+ Machine: IMAGE_FILE_MACHINE_AMD64
+ Characteristics: [ ]
+sections:
+ - Name: '.text$ac1'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 1
+ SectionData: '01000000'
+ - Name: '.text$ac2'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 1
+ SectionData: '01000000'
+ - Name: '.text$nm'
+ Characteristics: [ IMAGE_SCN_CNT_INITIALIZED_DATA, IMAGE_SCN_LNK_COMDAT, IMAGE_SCN_MEM_READ, IMAGE_SCN_MEM_WRITE ]
+ Alignment: 1
+ SectionData: '01000000'
+symbols:
+ - Name: '.text$ac1'
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 4
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 3099354981
+ Number: 2
+ Selection: IMAGE_COMDAT_SELECT_ASSOCIATIVE
+ - Name: '.text$ac2'
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 4
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 3099354981
+ Number: 3
+ Selection: IMAGE_COMDAT_SELECT_ASSOCIATIVE
+ - Name: '.text$nm'
+ Value: 0
+ SectionNumber: 3
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ SectionDefinition:
+ Length: 4
+ NumberOfRelocations: 0
+ NumberOfLinenumbers: 0
+ CheckSum: 3099354981
+ Number: 4
+ Selection: IMAGE_COMDAT_SELECT_ANY
+ - Name: symbol
+ Value: 0
+ SectionNumber: 3
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_EXTERNAL
+ - Name: assocsym2
+ Value: 0
+ SectionNumber: 1
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+ - Name: assocsym
+ Value: 0
+ SectionNumber: 2
+ SimpleType: IMAGE_SYM_TYPE_NULL
+ ComplexType: IMAGE_SYM_DTYPE_NULL
+ StorageClass: IMAGE_SYM_CLASS_STATIC
+...
+