Skip to content

Commit 9916d8d

Browse files
committed
Merging r355141:
------------------------------------------------------------------------ r355141 | rnk | 2019-02-28 13:05:41 -0800 (Thu, 28 Feb 2019) | 11 lines [COFF] Add address-taken import thunks to the fid table Summary: Fixes PR39799 Reviewers: dmajor, hans Subscribers: jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58739 ------------------------------------------------------------------------ llvm-svn: 360803
1 parent 35349ba commit 9916d8d

File tree

2 files changed

+82
-11
lines changed

2 files changed

+82
-11
lines changed

lld/COFF/Writer.cpp

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,19 +1351,47 @@ static void addSymbolToRVASet(SymbolRVASet &RVASet, Defined *S) {
13511351
// symbol in an executable section.
13521352
static void maybeAddAddressTakenFunction(SymbolRVASet &AddressTakenSyms,
13531353
Symbol *S) {
1354-
auto *D = dyn_cast_or_null<DefinedCOFF>(S);
1355-
1356-
// Ignore undefined symbols and references to non-functions (e.g. globals and
1357-
// labels).
1358-
if (!D ||
1359-
D->getCOFFSymbol().getComplexType() != COFF::IMAGE_SYM_DTYPE_FUNCTION)
1354+
if (!S)
13601355
return;
13611356

1362-
// Mark the symbol as address taken if it's in an executable section.
1363-
Chunk *RefChunk = D->getChunk();
1364-
OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
1365-
if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
1366-
addSymbolToRVASet(AddressTakenSyms, D);
1357+
switch (S->kind()) {
1358+
case Symbol::DefinedLocalImportKind:
1359+
case Symbol::DefinedImportDataKind:
1360+
// Defines an __imp_ pointer, so it is data, so it is ignored.
1361+
break;
1362+
case Symbol::DefinedCommonKind:
1363+
// Common is always data, so it is ignored.
1364+
break;
1365+
case Symbol::DefinedAbsoluteKind:
1366+
case Symbol::DefinedSyntheticKind:
1367+
// Absolute is never code, synthetic generally isn't and usually isn't
1368+
// determinable.
1369+
break;
1370+
case Symbol::LazyKind:
1371+
case Symbol::UndefinedKind:
1372+
// Undefined symbols resolve to zero, so they don't have an RVA. Lazy
1373+
// symbols shouldn't have relocations.
1374+
break;
1375+
1376+
case Symbol::DefinedImportThunkKind:
1377+
// Thunks are always code, include them.
1378+
addSymbolToRVASet(AddressTakenSyms, cast<Defined>(S));
1379+
break;
1380+
1381+
case Symbol::DefinedRegularKind: {
1382+
// This is a regular, defined, symbol from a COFF file. Mark the symbol as
1383+
// address taken if the symbol type is function and it's in an executable
1384+
// section.
1385+
auto *D = cast<DefinedRegular>(S);
1386+
if (D->getCOFFSymbol().getComplexType() == COFF::IMAGE_SYM_DTYPE_FUNCTION) {
1387+
Chunk *RefChunk = D->getChunk();
1388+
OutputSection *OS = RefChunk ? RefChunk->getOutputSection() : nullptr;
1389+
if (OS && OS->Header.Characteristics & IMAGE_SCN_MEM_EXECUTE)
1390+
addSymbolToRVASet(AddressTakenSyms, D);
1391+
}
1392+
break;
1393+
}
1394+
}
13671395
}
13681396

13691397
// Visit all relocations from all section contributions of this object file and

lld/test/COFF/guardcf-thunk.s

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# REQUIRES: x86
2+
3+
# Make a DLL that exports exportfn1.
4+
# RUN: yaml2obj < %p/Inputs/export.yaml > %t.obj
5+
# RUN: lld-link /out:%t.dll /dll %t.obj /export:exportfn1 /implib:%t.lib
6+
7+
# Make an obj that takes the address of that exported function.
8+
# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc %s -o %t2.obj
9+
# RUN: lld-link -entry:main -guard:cf %t2.obj %t.lib -nodefaultlib -out:%t.exe
10+
# RUN: llvm-readobj -coff-load-config %t.exe | FileCheck %s
11+
12+
# Check that the gfids table contains *exactly* two entries, one for exportfn1
13+
# and one for main.
14+
# CHECK: GuardFidTable [
15+
# CHECK-NEXT: 0x{{[0-9A-Fa-f]+0$}}
16+
# CHECK-NEXT: 0x{{[0-9A-Fa-f]+0$}}
17+
# CHECK-NEXT: ]
18+
19+
20+
.def @feat.00;
21+
.scl 3;
22+
.type 0;
23+
.endef
24+
.globl @feat.00
25+
@feat.00 = 0x001
26+
27+
.section .text,"rx"
28+
.def main; .scl 2; .type 32; .endef
29+
.global main
30+
main:
31+
leaq exportfn1(%rip), %rax
32+
retq
33+
34+
.section .rdata,"dr"
35+
.globl _load_config_used
36+
_load_config_used:
37+
.long 256
38+
.fill 124, 1, 0
39+
.quad __guard_fids_table
40+
.quad __guard_fids_count
41+
.long __guard_flags
42+
.fill 128, 1, 0
43+

0 commit comments

Comments
 (0)