Skip to content

Commit 033c4d2

Browse files
committedMar 12, 2019
Include an archive file name in an error message for a corrupted file.
Differential Revision: https://reviews.llvm.org/D59212 llvm-svn: 355886
1 parent 17ee3b4 commit 033c4d2

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed
 

‎lld/ELF/InputFiles.cpp

+15-7
Original file line numberDiff line numberDiff line change
@@ -1188,20 +1188,28 @@ void BitcodeFile::parse(DenseSet<CachedHashStringRef> &ComdatGroups) {
11881188
Symbols.push_back(createBitcodeSymbol<ELFT>(KeptComdats, ObjSym, *this));
11891189
}
11901190

1191-
static ELFKind getELFKind(MemoryBufferRef MB) {
1191+
static ELFKind getELFKind(MemoryBufferRef MB, StringRef ArchiveName) {
11921192
unsigned char Size;
11931193
unsigned char Endian;
11941194
std::tie(Size, Endian) = getElfArchType(MB.getBuffer());
11951195

1196+
auto Fatal = [&](StringRef Msg) {
1197+
StringRef Filename = MB.getBufferIdentifier();
1198+
if (ArchiveName.empty())
1199+
fatal(Filename + ": corrupted ELF file: " + Msg);
1200+
else
1201+
fatal(ArchiveName + "(" + Filename + "): corrupted ELF file: " + Msg);
1202+
};
1203+
11961204
if (Endian != ELFDATA2LSB && Endian != ELFDATA2MSB)
1197-
fatal(MB.getBufferIdentifier() + ": invalid data encoding");
1205+
Fatal("invalid data encoding");
11981206
if (Size != ELFCLASS32 && Size != ELFCLASS64)
1199-
fatal(MB.getBufferIdentifier() + ": invalid file class");
1207+
Fatal("invalid file class");
12001208

12011209
size_t BufSize = MB.getBuffer().size();
12021210
if ((Size == ELFCLASS32 && BufSize < sizeof(Elf32_Ehdr)) ||
12031211
(Size == ELFCLASS64 && BufSize < sizeof(Elf64_Ehdr)))
1204-
fatal(MB.getBufferIdentifier() + ": file is too short");
1212+
Fatal("file is too short");
12051213

12061214
if (Size == ELFCLASS32)
12071215
return (Endian == ELFDATA2LSB) ? ELF32LEKind : ELF32BEKind;
@@ -1236,7 +1244,7 @@ InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
12361244
if (isBitcode(MB))
12371245
return make<BitcodeFile>(MB, ArchiveName, OffsetInArchive);
12381246

1239-
switch (getELFKind(MB)) {
1247+
switch (getELFKind(MB, ArchiveName)) {
12401248
case ELF32LEKind:
12411249
return make<ObjFile<ELF32LE>>(MB, ArchiveName);
12421250
case ELF32BEKind:
@@ -1251,7 +1259,7 @@ InputFile *elf::createObjectFile(MemoryBufferRef MB, StringRef ArchiveName,
12511259
}
12521260

12531261
InputFile *elf::createSharedFile(MemoryBufferRef MB, StringRef DefaultSoName) {
1254-
switch (getELFKind(MB)) {
1262+
switch (getELFKind(MB, "")) {
12551263
case ELF32LEKind:
12561264
return make<SharedFile<ELF32LE>>(MB, DefaultSoName);
12571265
case ELF32BEKind:
@@ -1293,7 +1301,7 @@ template <class ELFT> void LazyObjFile::parse() {
12931301
return;
12941302
}
12951303

1296-
if (getELFKind(this->MB) != Config->EKind) {
1304+
if (getELFKind(this->MB, ArchiveName) != Config->EKind) {
12971305
error("incompatible file: " + this->MB.getBufferIdentifier());
12981306
return;
12991307
}

‎lld/test/ELF/invalid/invalid-elf.test

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33

44
# RUN: not ld.lld %t %p/Inputs/data-encoding.a -o %t2 2>&1 | \
55
# RUN: FileCheck --check-prefix=INVALID-DATA-ENC %s
6-
# INVALID-DATA-ENC: test.o: invalid data encoding
6+
# INVALID-DATA-ENC: data-encoding.a(test.o): corrupted ELF file: invalid data encoding
77

88
# RUN: not ld.lld %t %p/Inputs/file-class.a -o %t2 2>&1 | \
99
# RUN: FileCheck --check-prefix=INVALID-FILE-CLASS %s
10-
# INVALID-FILE-CLASS: test.o: invalid file class
10+
# INVALID-FILE-CLASS: file-class.a(test.o): corrupted ELF file: invalid file class
1111

1212
# RUN: not ld.lld %p/Inputs/binding.elf -o %t2 2>&1 | \
1313
# RUN: FileCheck --check-prefix=INVALID-BINDING %s

0 commit comments

Comments
 (0)
Please sign in to comment.