13 changes: 13 additions & 0 deletions llvm/lib/ExecutionEngine/JITLink/COFFLinkGraphBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ExecutionEngine/JITLink/JITLink.h"
#include "llvm/Object/COFF.h"

#include "COFFDirectiveParser.h"
#include "EHFrameSupportImpl.h"
#include "JITLinkGeneric.h"

Expand Down Expand Up @@ -133,6 +134,11 @@ class COFFLinkGraphBuilder {

Section &getCommonSection();

Symbol *createExternalSymbol(COFFSymbolIndex SymIndex, StringRef SymbolName,
object::COFFSymbolRef Symbol,
const object::coff_section *Section);
Expected<Symbol *> createAliasSymbol(StringRef SymbolName, Linkage L, Scope S,
Symbol &Target);
Expected<Symbol *> createDefinedSymbol(COFFSymbolIndex SymIndex,
StringRef SymbolName,
object::COFFSymbolRef Symbol,
Expand All @@ -143,7 +149,10 @@ class COFFLinkGraphBuilder {
Expected<Symbol *> exportCOMDATSymbol(COFFSymbolIndex SymIndex,
StringRef SymbolName,
object::COFFSymbolRef Symbol);

Error handleDirectiveSection(StringRef Str);
Error flushWeakAliasRequests();
Error handleAlternateNames();
Error calculateImplicitSizeOfSymbols();

static uint64_t getSectionAddress(const object::COFFObjectFile &Obj,
Expand All @@ -154,18 +163,22 @@ class COFFLinkGraphBuilder {
static unsigned getPointerSize(const object::COFFObjectFile &Obj);
static support::endianness getEndianness(const object::COFFObjectFile &Obj);
static StringRef getDLLImportStubPrefix() { return "__imp_"; }
static StringRef getDirectiveSectionName() { return ".drectve"; }
StringRef getCOFFSectionName(COFFSectionIndex SectionIndex,
const object::coff_section *Sec,
object::COFFSymbolRef Sym);

const object::COFFObjectFile &Obj;
std::unique_ptr<LinkGraph> G;
COFFDirectiveParser DirectiveParser;

Section *CommonSection = nullptr;
std::vector<Block *> GraphBlocks;
std::vector<Symbol *> GraphSymbols;

DenseMap<StringRef, StringRef> AlternateNames;
DenseMap<StringRef, Symbol *> ExternalSymbols;
DenseMap<StringRef, Symbol *> DefinedSymbols;
};

template <typename RelocHandlerFunction>
Expand Down
21 changes: 21 additions & 0 deletions llvm/lib/ExecutionEngine/JITLink/COFFOptions.td
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
include "llvm/Option/OptParser.td"

// link.exe accepts options starting with either a dash or a slash.

// Flag that takes no arguments.
class F<string name> : Flag<["/", "-", "/?", "-?"], name>;

// Flag that takes one argument after ":".
class P<string name> :
Joined<["/", "-", "/?", "-?"], name#":">;

// Boolean flag which can be suffixed by ":no". Using it unsuffixed turns the
// flag on and using it suffixed by ":no" turns it off.
multiclass B_priv<string name> {
def "" : F<name>;
def _no : F<name#":no">;
}

def export : P<"export">;
def alternatename : P<"alternatename">;
def incl : Joined<["/", "-", "/?", "-?"], "include:">;
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc %s -o %t
# RUN: llvm-jitlink -noexec %t
#
# Check object with alternatename directive does not fail, because
# foo external symbol was resolved to foo_def.
#

.text

.def foo_def;
.scl 2;
.type 32;
.endef
.globl foo_def
.p2align 4, 0x90
foo_def:
retq

.def main;
.scl 2;
.type 32;
.endef
.globl main
.p2align 4, 0x90
main:
callq foo
retq

.section .drectve,"yn"
.ascii "/alternatename:foo=foo_def"
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc %s -o %t
# RUN: not llvm-jitlink -noexec %t 2>&1 | FileCheck %s
#
# Check object without alternatename directive fails because of
# external symbol not found error.
#
# CHECK: error: Symbols not found: [ foo ]
.text

.def foo_def;
.scl 2;
.type 32;
.endef
.globl foo_def
.p2align 4, 0x90
foo_def:
retq

.def main;
.scl 2;
.type 32;
.endef
.globl main
.p2align 4, 0x90
main:
callq foo
retq
21 changes: 21 additions & 0 deletions llvm/test/ExecutionEngine/JITLink/X86/COFF_directive_include.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# RUN: llvm-mc -filetype=obj -triple=x86_64-windows-msvc %s -o %t
# RUN: not llvm-jitlink -noexec %t 2>&1 | FileCheck %s
#
# Check an external symbol "foo" is generated and not dead-stripped
# because of include directive which turned into symbol not found error.
#
# CHECK: error: Symbols not found: [ foo ]

.text

.def main;
.scl 2;
.type 32;
.endef
.globl main
.p2align 4, 0x90
main:
retq

.section .drectve,"yn"
.ascii "/include:foo"