From 7e95d9e362b37fe10e23acd240aa8e3640bd3006 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 24 Jul 2018 22:52:11 +0000 Subject: [PATCH] Fix error messages for bad symbols. Previously, the error messages didn't contain symbol name because we didn't read a symbol name for these error messages. Differential Revision: https://reviews.llvm.org/D49762 llvm-svn: 337863 --- lld/COFF/InputFiles.cpp | 32 +++++++++++++++++------------ lld/test/COFF/invalid-section-number.test | 34 +++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 13 deletions(-) create mode 100644 lld/test/COFF/invalid-section-number.test diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index 7fa3b0e..2788f3b 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -300,17 +300,22 @@ Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) { Optional ObjFile::createDefined( COFFSymbolRef Sym, std::vector &ComdatDefs) { - StringRef Name; + auto GetName = [&]() { + StringRef S; + COFFObj->getSymbolName(Sym, S); + return S; + }; + if (Sym.isCommon()) { auto *C = make(Sym); Chunks.push_back(C); - COFFObj->getSymbolName(Sym, Name); - Symbol *S = - Symtab->addCommon(this, Name, Sym.getValue(), Sym.getGeneric(), C); - return S; + return Symtab->addCommon(this, GetName(), Sym.getValue(), Sym.getGeneric(), + C); } + if (Sym.isAbsolute()) { - COFFObj->getSymbolName(Sym, Name); + StringRef Name = GetName(); + // Skip special symbols. if (Name == "@comp.id") return nullptr; @@ -318,21 +323,22 @@ Optional ObjFile::createDefined( Feat00Flags = Sym.getValue(); return nullptr; } + if (Sym.isExternal()) return Symtab->addAbsolute(Name, Sym); - else - return make(Name, Sym); + return make(Name, Sym); } + int32_t SectionNumber = Sym.getSectionNumber(); if (SectionNumber == llvm::COFF::IMAGE_SYM_DEBUG) return nullptr; if (llvm::COFF::isReservedSectionNumber(SectionNumber)) - fatal(toString(this) + ": " + Name + + fatal(toString(this) + ": " + GetName() + " should not refer to special section " + Twine(SectionNumber)); if ((uint32_t)SectionNumber >= SparseChunks.size()) - fatal(toString(this) + ": " + Name + + fatal(toString(this) + ": " + GetName() + " should not refer to non-existent section " + Twine(SectionNumber)); // Handle comdat leader symbols. @@ -341,16 +347,16 @@ Optional ObjFile::createDefined( Symbol *Leader; bool Prevailing; if (Sym.isExternal()) { - COFFObj->getSymbolName(Sym, Name); std::tie(Leader, Prevailing) = - Symtab->addComdat(this, Name, Sym.getGeneric()); + Symtab->addComdat(this, GetName(), Sym.getGeneric()); } else { Leader = make(this, /*Name*/ "", false, /*IsExternal*/ false, Sym.getGeneric()); Prevailing = true; } + if (Prevailing) { - SectionChunk *C = readSection(SectionNumber, Def, Name); + SectionChunk *C = readSection(SectionNumber, Def, GetName()); SparseChunks[SectionNumber] = C; C->Sym = cast(Leader); cast(Leader)->Data = &C->Repl; diff --git a/lld/test/COFF/invalid-section-number.test b/lld/test/COFF/invalid-section-number.test new file mode 100644 index 0000000..bada902 --- /dev/null +++ b/lld/test/COFF/invalid-section-number.test @@ -0,0 +1,34 @@ +# RUN: yaml2obj %s > %t.obj +# RUN: not lld-link %t.obj 2>&1 | FileCheck %s + +# CHECK: foo should not refer to special section -10 + +--- !COFF +header: + Machine: IMAGE_FILE_MACHINE_I386 + Characteristics: [] +sections: + - Name: .text + Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ] + Alignment: 4 + SectionData: B82A000000C3 +symbols: + - Name: .text + Value: 0 + SectionNumber: 1 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_STATIC + SectionDefinition: + Length: 6 + NumberOfRelocations: 0 + NumberOfLinenumbers: 0 + CheckSum: 0 + Number: 0 + - Name: foo + Value: 0 + SectionNumber: -10 + SimpleType: IMAGE_SYM_TYPE_NULL + ComplexType: IMAGE_SYM_DTYPE_NULL + StorageClass: IMAGE_SYM_CLASS_EXTERNAL +... -- 2.7.4