Skip to content

Commit d098651

Browse files
authored
[LLD] [COFF] Preserve directives and export names from LTO objects (#78802)
The export names are saved as StringRefs pointing into the COFF directives. In the case of LTO objects, this can be memory allocated that is owned by the LTO InputFile, which gets destructed when doing the compilation. In the case of LTO objects from an older version of LLVM, which require being upgraded when loaded, the directives string gets destructed, while when using LTO objects of a matching version (the common case), the directives string points into memory that doesn't get destructed on LTO compilation. Test this by linking a bundled binary LTO object file, from an older version of LLVM. This fixes issue #78591, and downstream issue mstorsjo/llvm-mingw#392.
1 parent fcb6737 commit d098651

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

lld/COFF/InputFiles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ void BitcodeFile::parse() {
10811081
if (objSym.isUsed())
10821082
ctx.config.gcroot.push_back(sym);
10831083
}
1084-
directives = obj->getCOFFLinkerOpts();
1084+
directives = saver.save(obj->getCOFFLinkerOpts());
10851085
}
10861086

10871087
void BitcodeFile::parseLazy() {
2.26 KB
Binary file not shown.

lld/test/COFF/lto-directives.test

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; REQUIRES: x86
2+
3+
;; Test linking an LTO object file that contains directives. The
4+
;; LTO object file is built with an older toolchain, to force it
5+
;; to be upgraded when loaded.
6+
7+
;; The input file is compiled from source that looks like this:
8+
;; void __declspec(dllexport) entry(void) { }
9+
;; with this command:
10+
;; clang -target x86_64-windows-msvc -c main.c -flto
11+
12+
; RUN: lld-link /entry:entry /subsystem:console %p/Inputs/lto-directives.obj /dll /out:%t.dll
13+
; RUN: llvm-readobj --coff-exports %t.dll | FileCheck %s
14+
15+
; CHECK: Name: entry

0 commit comments

Comments
 (0)