From d649faff9c0ca87fd5a00a7f1056071fd46432ea Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sun, 7 Nov 2021 16:46:29 +0100 Subject: [PATCH] [LLD][COFF] Support GNU style == aliases D46245 added support for this in llvm-libtool, but while lld-link can also create .lib files from .def files it didn't support aliases. I compared the Inputs/library.def test against the output from llvm-libtool and it matches, except for the fact that lld-link reorders functions for some reason. I have also verified that this fixes a bug I was running into while trying to compile .def files to .lib files in MinGW-w64 (using lld-link instead of llvm-libtool). Differential Revision: https://reviews.llvm.org/D113365 --- lld/COFF/Config.h | 2 ++ lld/COFF/Driver.cpp | 2 ++ lld/test/COFF/Inputs/library.def | 1 + lld/test/COFF/lib.test | 10 ++++++++++ 4 files changed, 15 insertions(+) diff --git a/lld/COFF/Config.h b/lld/COFF/Config.h index 5597389b4226..4bb42c88aa93 100644 --- a/lld/COFF/Config.h +++ b/lld/COFF/Config.h @@ -43,6 +43,7 @@ static const auto I386 = llvm::COFF::IMAGE_FILE_MACHINE_I386; struct Export { StringRef name; // N in /export:N or /export:E=N StringRef extName; // E in /export:E=N + StringRef aliasTarget; // GNU specific: N in "alias == N" Symbol *sym = nullptr; uint16_t ordinal = 0; bool noname = false; @@ -63,6 +64,7 @@ struct Export { bool operator==(const Export &e) { return (name == e.name && extName == e.extName && + aliasTarget == e.aliasTarget && ordinal == e.ordinal && noname == e.noname && data == e.data && isPrivate == e.isPrivate); } diff --git a/lld/COFF/Driver.cpp b/lld/COFF/Driver.cpp index f8da63c5edbf..3076871fd98b 100644 --- a/lld/COFF/Driver.cpp +++ b/lld/COFF/Driver.cpp @@ -810,6 +810,7 @@ static void createImportLibrary(bool asLib) { e2.Name = std::string(e1.name); e2.SymbolName = std::string(e1.symbolName); e2.ExtName = std::string(e1.extName); + e2.AliasTarget = std::string(e1.aliasTarget); e2.Ordinal = e1.ordinal; e2.Noname = e1.noname; e2.Data = e1.data; @@ -908,6 +909,7 @@ static void parseModuleDefs(StringRef path) { } e2.name = saver.save(e1.Name); e2.extName = saver.save(e1.ExtName); + e2.aliasTarget = saver.save(e1.AliasTarget); e2.ordinal = e1.Ordinal; e2.noname = e1.Noname; e2.data = e1.Data; diff --git a/lld/test/COFF/Inputs/library.def b/lld/test/COFF/Inputs/library.def index 6c6880ffa18e..5c81351596ee 100644 --- a/lld/test/COFF/Inputs/library.def +++ b/lld/test/COFF/Inputs/library.def @@ -1,5 +1,6 @@ LIBRARY library EXPORTS function + alias == function data DATA constant CONSTANT diff --git a/lld/test/COFF/lib.test b/lld/test/COFF/lib.test index 2d63ed6c2324..7525ef4226cd 100644 --- a/lld/test/COFF/lib.test +++ b/lld/test/COFF/lib.test @@ -1,6 +1,16 @@ # RUN: lld-link /machine:x64 /def:%S/Inputs/library.def /out:%t.lib # RUN: llvm-nm %t.lib | FileCheck %s +CHECK: 00000000 a @comp.id +CHECK: 00000000 a @feat.00 +CHECK: 00000000 W alias +CHECK: U function + +CHECK: 00000000 a @comp.id +CHECK: 00000000 a @feat.00 +CHECK: 00000000 W __imp_alias +CHECK: U __imp_function + CHECK: 00000000 R __imp_constant CHECK: 00000000 R constant