Skip to content

Commit

Permalink
[ELF] Fix use after free in case of using --whole-archive.
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D34554

llvm-svn: 325313
  • Loading branch information
igorkudrin committed Feb 16, 2018
1 parent f287518 commit 943f62d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lld/ELF/Driver.cpp
Expand Up @@ -984,7 +984,7 @@ static Optional<StringRef> getArchiveName(InputFile *File) {
if (isa<ArchiveFile>(File))
return File->getName();
if (!File->ArchiveName.empty())
return File->ArchiveName;
return StringRef(File->ArchiveName);
return None;
}

Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/InputFiles.h
Expand Up @@ -98,7 +98,7 @@ class InputFile {
// Filename of .a which contained this file. If this file was
// not in an archive file, it is the empty string. We use this
// string for creating error messages.
StringRef ArchiveName;
std::string ArchiveName;

// If this is an architecture-specific file, the following members
// have ELF type (i.e. ELF{32,64}{LE,BE}) and target machine type.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/InputSection.cpp
Expand Up @@ -277,7 +277,7 @@ std::string InputSectionBase::getObjMsg(uint64_t Off) {

std::string Archive;
if (!File->ArchiveName.empty())
Archive = (" in archive " + File->ArchiveName).str();
Archive = " in archive " + File->ArchiveName;

// Find a symbol that encloses a given location.
for (Symbol *B : File->getSymbols())
Expand Down
15 changes: 15 additions & 0 deletions lld/test/ELF/whole-archive-name.s
@@ -0,0 +1,15 @@
// REQUIRES: x86
// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
// RUN: mkdir -p %t.dir
// RUN: rm -f %t.dir/liba.a
// RUN: llvm-ar rcs %t.dir/liba.a %t.o
// RUN: ld.lld -L%t.dir --whole-archive -la -o %t -Map=- | FileCheck %s

.globl _start
_start:
nop

// There was a use after free of an archive name.
// Valgrind/asan would detect it.
// CHECK: liba.a(whole-archive-name.s.tmp.o):(.text)
// CHECK-NEXT: _start

0 comments on commit 943f62d

Please sign in to comment.