From d6bd1371fc4b540098285e5e2bf673879cf709f5 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Wed, 3 Aug 2016 04:39:42 +0000 Subject: [PATCH] Include filenames and section names to error messages. llvm-svn: 277566 --- lld/ELF/InputSection.cpp | 37 ++++++++++++++++++++------------ lld/ELF/InputSection.h | 3 +++ lld/test/ELF/merge-string-error.s | 2 +- lld/test/ELF/relocation-past-merge-end.s | 2 +- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lld/ELF/InputSection.cpp b/lld/ELF/InputSection.cpp index 83d2ad1..a61cf56 100644 --- a/lld/ELF/InputSection.cpp +++ b/lld/ELF/InputSection.cpp @@ -62,6 +62,11 @@ ArrayRef InputSectionBase::getSectionData() const { return check(this->File->getObj().getSectionContents(this->Header)); } +// Returns a string for an error message. +template static std::string getName(SectionT *Sec) { + return (Sec->getFile()->getName() + "(" + Sec->getSectionName() + ")").str(); +} + template typename ELFT::uint InputSectionBase::getOffset(uintX_t Offset) const { switch (SectionKind) { @@ -81,8 +86,8 @@ typename ELFT::uint InputSectionBase::getOffset(uintX_t Offset) const { // input files contain section symbol points to the corresponding input // section. Redirect it to the produced output section. if (Offset != 0) - fatal("Unsupported reference to the middle of '" + getSectionName() + - "' section"); + fatal(getName(this) + ": unsupported reference to the middle of '" + + getSectionName() + "' section"); return this->OutSec->getVA(); } llvm_unreachable("invalid section kind"); @@ -90,7 +95,8 @@ typename ELFT::uint InputSectionBase::getOffset(uintX_t Offset) const { template void InputSectionBase::uncompress() { if (!zlib::isAvailable()) - fatal("build lld with zlib to enable compressed sections support"); + fatal(getName(this) + + ": build lld with zlib to enable compressed sections support"); // A compressed section consists of a header of Elf_Chdr type // followed by compressed data. @@ -103,11 +109,11 @@ template void InputSectionBase::uncompress() { Data = Data.slice(sizeof(Elf_Chdr)); if (Hdr->ch_type != ELFCOMPRESS_ZLIB) - fatal("unsupported compression type"); + fatal(getName(this) + ": unsupported compression type"); StringRef Buf((const char *)Data.data(), Data.size()); if (zlib::uncompress(Buf, Uncompressed, Hdr->ch_size) != zlib::StatusOK) - fatal("error uncompressing section"); + fatal(getName(this) + ": error uncompressing section"); } template @@ -314,7 +320,7 @@ void InputSection::relocateNonAlloc(uint8_t *Buf, ArrayRef Rels) { SymbolBody &Sym = this->File->getRelocTargetSym(Rel); if (Target->getRelExpr(Type, Sym) != R_ABS) { - error(this->getSectionName() + " has non-ABS reloc"); + error(getName(this) + " has non-ABS reloc"); return; } @@ -515,14 +521,15 @@ static size_t findNull(ArrayRef A, size_t EntSize) { // Split SHF_STRINGS section. Such section is a sequence of // null-terminated strings. -static std::vector splitStrings(ArrayRef Data, - size_t EntSize) { +template +std::vector +MergeInputSection::splitStrings(ArrayRef Data, size_t EntSize) { std::vector V; size_t Off = 0; while (!Data.empty()) { size_t End = findNull(Data, EntSize); if (End == StringRef::npos) - fatal("string is not null terminated"); + fatal(getName(this) + ": string is not null terminated"); size_t Size = End + EntSize; V.emplace_back(Off, Data.slice(0, Size)); Data = Data.slice(Size); @@ -533,8 +540,10 @@ static std::vector splitStrings(ArrayRef Data, // Split non-SHF_STRINGS section. Such section is a sequence of // fixed size records. -static std::vector splitNonStrings(ArrayRef Data, - size_t EntSize) { +template +std::vector +MergeInputSection::splitNonStrings(ArrayRef Data, + size_t EntSize) { std::vector V; size_t Size = Data.size(); assert((Size % EntSize) == 0); @@ -580,7 +589,7 @@ MergeInputSection::getSectionPiece(uintX_t Offset) const { StringRef Data((const char *)D.data(), D.size()); uintX_t Size = Data.size(); if (Offset >= Size) - fatal("entry is past the end of the section"); + fatal(getName(this) + ": entry is past the end of the section"); // Find the element this offset points to. auto I = std::upper_bound( @@ -632,7 +641,7 @@ MipsReginfoInputSection::MipsReginfoInputSection(elf::ObjectFile *F, // Initialize this->Reginfo. ArrayRef D = this->getSectionData(); if (D.size() != sizeof(Elf_Mips_RegInfo)) { - error("invalid size of .reginfo section"); + error(getName(this) + ": invalid size of .reginfo section"); return; } Reginfo = reinterpret_cast *>(D.data()); @@ -651,7 +660,7 @@ MipsOptionsInputSection::MipsOptionsInputSection(elf::ObjectFile *F, ArrayRef D = this->getSectionData(); while (!D.empty()) { if (D.size() < sizeof(Elf_Mips_Options)) { - error("invalid size of .MIPS.options section"); + error(getName(this) + ": invalid size of .MIPS.options section"); break; } auto *O = reinterpret_cast *>(D.data()); diff --git a/lld/ELF/InputSection.h b/lld/ELF/InputSection.h index ab1bb53..ddb9ce0 100644 --- a/lld/ELF/InputSection.h +++ b/lld/ELF/InputSection.h @@ -146,6 +146,9 @@ public: const SectionPiece *getSectionPiece(uintX_t Offset) const; private: + std::vector splitStrings(ArrayRef A, size_t Size); + std::vector splitNonStrings(ArrayRef A, size_t Size); + llvm::DenseMap OffsetMap; llvm::DenseSet LiveOffsets; }; diff --git a/lld/test/ELF/merge-string-error.s b/lld/test/ELF/merge-string-error.s index c5088ac..3a98523 100644 --- a/lld/test/ELF/merge-string-error.s +++ b/lld/test/ELF/merge-string-error.s @@ -8,4 +8,4 @@ .data .long .rodata.str1.1 + 4 -// CHECK: entry is past the end of the section +// CHECK: merge-string-error.s.tmp.o(.rodata.str1.1): entry is past the end of the section diff --git a/lld/test/ELF/relocation-past-merge-end.s b/lld/test/ELF/relocation-past-merge-end.s index 993b071..de67a45 100644 --- a/lld/test/ELF/relocation-past-merge-end.s +++ b/lld/test/ELF/relocation-past-merge-end.s @@ -1,7 +1,7 @@ // REQUIRES: x86 // RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux // RUN: not ld.lld %t.o -o %t.so -shared 2>&1 | FileCheck %s -// CHECK: entry is past the end of the section +// CHECK: relocation-past-merge-end.s.tmp.o(.foo): entry is past the end of the section .data .long .foo + 1 -- 2.7.4