From 90b4388f5612f3088b15a351a0ad421f312fbabb Mon Sep 17 00:00:00 2001 From: Martin Storsjo Date: Thu, 1 Aug 2019 18:47:27 +0000 Subject: [PATCH] [COFF] Fix wholearchive with thin archives The Archive object created when loading an archive specified with wholearchive got cleaned up immediately, when the owning std::unique_ptr went out of scope, even if persisted StringRefs pointed to memory that belonged to the archive, which no longer was mapped in memory. This hasn't been an issue with regular (as opposed to thin) archives, as references to the member objects has kept the mapping for the whole archive file alive - but with thin archives, all such references point to other files. Add the std::unique_ptr to the arena allocator, to retain it as long as necessary. This fixes (the last issue raised in) PR42388. Differential Revision: https://reviews.llvm.org/D65565 llvm-svn: 367599 --- lld/COFF/Driver.cpp | 4 +++- lld/test/COFF/thin-archive.s | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index 3d2660b..9fb2c51 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -184,8 +184,10 @@ void LinkerDriver::addBuffer(std::unique_ptr mb, if (wholeArchive) { std::unique_ptr file = CHECK(Archive::create(mbref), filename + ": failed to parse archive"); + Archive *archive = file.get(); + make>(std::move(file)); // take ownership - for (MemoryBufferRef m : getArchiveMembers(file.get())) + for (MemoryBufferRef m : getArchiveMembers(archive)) addArchiveBuffer(m, "", filename, 0); return; } diff --git a/lld/test/COFF/thin-archive.s b/lld/test/COFF/thin-archive.s index 6eb896c..a1491cb 100644 --- a/lld/test/COFF/thin-archive.s +++ b/lld/test/COFF/thin-archive.s @@ -11,6 +11,8 @@ # RUN: FileCheck --allow-empty %s # RUN: lld-link /entry:main %t.main.obj %t_thin.lib /out:%t.exe 2>&1 | \ # RUN: FileCheck --allow-empty %s +# RUN: lld-link /entry:main %t.main.obj /wholearchive:%t_thin.lib /out:%t.exe 2>&1 | \ +# RUN: FileCheck --allow-empty %s # RUN: rm %t.lib.obj # RUN: lld-link /entry:main %t.main.obj %t.lib /out:%t.exe 2>&1 | \ -- 2.7.4