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
This commit is contained in:
Rui Ueyama 2018-07-24 22:52:11 +00:00
parent f8c101ff19
commit 7e95d9e362
2 changed files with 53 additions and 13 deletions

View File

@ -300,17 +300,22 @@ Symbol *ObjFile::createUndefined(COFFSymbolRef Sym) {
Optional<Symbol *> ObjFile::createDefined(
COFFSymbolRef Sym,
std::vector<const coff_aux_section_definition *> &ComdatDefs) {
StringRef Name;
auto GetName = [&]() {
StringRef S;
COFFObj->getSymbolName(Sym, S);
return S;
};
if (Sym.isCommon()) {
auto *C = make<CommonChunk>(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<Symbol *> ObjFile::createDefined(
Feat00Flags = Sym.getValue();
return nullptr;
}
if (Sym.isExternal())
return Symtab->addAbsolute(Name, Sym);
else
return make<DefinedAbsolute>(Name, Sym);
return make<DefinedAbsolute>(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<Symbol *> 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<DefinedRegular>(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<DefinedRegular>(Leader);
cast<DefinedRegular>(Leader)->Data = &C->Repl;

View File

@ -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
...