Index: ELF/Driver.cpp =================================================================== --- ELF/Driver.cpp +++ ELF/Driver.cpp @@ -984,7 +984,7 @@ if (isa(File)) return File->getName(); if (!File->ArchiveName.empty()) - return File->ArchiveName; + return StringRef(File->ArchiveName); return None; } Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -98,7 +98,7 @@ // 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. Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -277,7 +277,7 @@ 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()) Index: test/ELF/whole-archive-name.s =================================================================== --- test/ELF/whole-archive-name.s +++ 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