Skip to content

Commit 5a5ac65

Browse files
committed
[MC] Always emit relocations for same-section function references
Summary: We already emit relocations in this case when the "incremental linker compatible" flag is set, but it turns out these relocations are also required for /guard:cf. Now that we have two use cases for this behavior, let's make it unconditional to try to keep things simple. We never hit this problem in Clang because it always sets the "incremental linker compatible" flag when targeting MSVC. However, LLD LTO doesn't set this flag, so we'd get CFG failures at runtime when using ThinLTO and /guard:cf. We probably don't want LLD LTO to set the "incremental linker compatible" assembler flag, since this has nothing to do with incremental linking, and we don't need to timestamp LTO temporary objects. Fixes PR36624. Reviewers: inglorion, espindola, majnemer Subscribers: mehdi_amini, llvm-commits, hiraditya Differential Revision: https://reviews.llvm.org/D44485 llvm-svn: 327557
1 parent 59be4b4 commit 5a5ac65

File tree

2 files changed

+24
-13
lines changed

2 files changed

+24
-13
lines changed

llvm/lib/MC/WinCOFFObjectWriter.cpp

+7-5
Original file line numberDiff line numberDiff line change
@@ -697,12 +697,14 @@ void WinCOFFObjectWriter::executePostLayoutBinding(MCAssembler &Asm,
697697
bool WinCOFFObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(
698698
const MCAssembler &Asm, const MCSymbol &SymA, const MCFragment &FB,
699699
bool InSet, bool IsPCRel) const {
700-
// MS LINK expects to be able to replace all references to a function with a
701-
// thunk to implement their /INCREMENTAL feature. Make sure we don't optimize
702-
// away any relocations to functions.
700+
// Don't drop relocations between functions, even if they are in the same text
701+
// section. Multiple Visual C++ linker features depend on having the
702+
// relocations present. The /INCREMENTAL flag will cause these relocations to
703+
// point to thunks, and the /GUARD:CF flag assumes that it can use relocations
704+
// to approximate the set of all address taken functions. LLD's implementation
705+
// of /GUARD:CF also relies on the existance of these relocations.
703706
uint16_t Type = cast<MCSymbolCOFF>(SymA).getType();
704-
if (Asm.isIncrementalLinkerCompatible() &&
705-
(Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
707+
if ((Type >> COFF::SCT_COMPLEX_TYPE_SHIFT) == COFF::IMAGE_SYM_DTYPE_FUNCTION)
706708
return false;
707709
return MCObjectWriter::isSymbolRefDifferenceFullyResolvedImpl(Asm, SymA, FB,
708710
InSet, IsPCRel);

llvm/test/MC/COFF/diff.s

+17-8
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,14 @@
11
// RUN: llvm-mc -filetype=obj -triple i686-pc-mingw32 %s | llvm-readobj -s -sr -sd | FileCheck %s
22

3+
// COFF resolves differences between labels in the same section, unless that
4+
// label is declared with function type.
5+
36
.section baz, "xr"
4-
.def X
5-
.scl 2;
6-
.type 32;
7-
.endef
87
.globl X
98
X:
109
mov Y-X+42, %eax
1110
retl
1211

13-
.def Y
14-
.scl 2;
15-
.type 32;
16-
.endef
1712
.globl Y
1813
Y:
1914
retl
@@ -30,6 +25,11 @@ _foobar: # @foobar
3025
# %bb.0:
3126
ret
3227

28+
.globl _baz
29+
_baz:
30+
calll _foobar
31+
retl
32+
3333
.data
3434
.globl _rust_crate # @rust_crate
3535
.align 4
@@ -39,6 +39,15 @@ _rust_crate:
3939
.long _foobar-_rust_crate
4040
.long _foobar-_rust_crate
4141

42+
// Even though _baz and _foobar are in the same .text section, we keep the
43+
// relocation for compatibility with the VC linker's /guard:cf and /incremental
44+
// flags, even on mingw.
45+
46+
// CHECK: Name: .text
47+
// CHECK: Relocations [
48+
// CHECK-NEXT: 0x12 IMAGE_REL_I386_REL32 _foobar
49+
// CHECK-NEXT: ]
50+
4251
// CHECK: Name: .data
4352
// CHECK: Relocations [
4453
// CHECK-NEXT: 0x4 IMAGE_REL_I386_DIR32 _foobar

0 commit comments

Comments
 (0)