Skip to content

Commit 5d513ef

Browse files
committed
[LLD] [COFF] Add support for a new, mingw specific embedded directive -exclude-symbols:
This is an entirely new embedded directive - extending the GNU ld command line option --exclude-symbols to be usable in embedded directives too. (GNU ld.bfd also got support for the same new directive, currently in the latest git version, after the 2.39 branch.) This works as an inverse to the regular embedded dllexport directives, for cases when autoexport of all eligible symbols is performed. Differential Revision: https://reviews.llvm.org/D130120
1 parent d1da646 commit 5d513ef

File tree

7 files changed

+49
-4
lines changed

7 files changed

+49
-4
lines changed

lld/COFF/Driver.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,14 @@ void LinkerDriver::parseDirectives(InputFile *file) {
375375
for (StringRef inc : directives.includes)
376376
addUndefined(inc);
377377

378+
// Handle /exclude-symbols: in bulk.
379+
for (StringRef e : directives.excludes) {
380+
SmallVector<StringRef, 2> vec;
381+
e.split(vec, ',');
382+
for (StringRef sym : vec)
383+
excludedSymbols.insert(mangle(sym));
384+
}
385+
378386
// https://docs.microsoft.com/en-us/cpp/preprocessor/comment-c-cpp?view=msvc-160
379387
for (auto *arg : directives.args) {
380388
switch (arg->getOption().getID()) {
@@ -1309,7 +1317,7 @@ void LinkerDriver::maybeExportMinGWSymbols(const opt::InputArgList &args) {
13091317
return;
13101318
}
13111319

1312-
AutoExporter exporter;
1320+
AutoExporter exporter(excludedSymbols);
13131321

13141322
for (auto *arg : args.filtered(OPT_wholearchive_file))
13151323
if (Optional<StringRef> path = doFindFile(arg->getValue()))

lld/COFF/Driver.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ extern COFFOptTable optTable;
5353
struct ParsedDirectives {
5454
std::vector<StringRef> exports;
5555
std::vector<StringRef> includes;
56+
std::vector<StringRef> excludes;
5657
llvm::opt::InputArgList args;
5758
};
5859

@@ -159,6 +160,7 @@ class LinkerDriver {
159160
std::vector<MemoryBufferRef> resources;
160161

161162
llvm::DenseSet<StringRef> directivesExports;
163+
llvm::DenseSet<StringRef> excludedSymbols;
162164

163165
COFFLinkerContext &ctx;
164166

lld/COFF/DriverUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -899,6 +899,9 @@ ParsedDirectives ArgParser::parseDirectives(StringRef s) {
899899
else if (tok.startswith_insensitive("/include:") ||
900900
tok.startswith_insensitive("-include:"))
901901
result.includes.push_back(tok.substr(strlen("/include:")));
902+
else if (tok.startswith_insensitive("/exclude-symbols:") ||
903+
tok.startswith_insensitive("-exclude-symbols:"))
904+
result.excludes.push_back(tok.substr(strlen("/exclude-symbols:")));
902905
else {
903906
// Copy substrings that are not valid C strings. The tokenizer may have
904907
// already copied quoted arguments for us, so those do not need to be

lld/COFF/MinGW.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ using namespace llvm::COFF;
2323
using namespace lld;
2424
using namespace lld::coff;
2525

26-
AutoExporter::AutoExporter() {
26+
AutoExporter::AutoExporter(
27+
const llvm::DenseSet<StringRef> &manualExcludeSymbols)
28+
: manualExcludeSymbols(manualExcludeSymbols) {
2729
excludeLibs = {
2830
"libgcc",
2931
"libgcc_s",
@@ -135,7 +137,7 @@ bool AutoExporter::shouldExport(const COFFLinkerContext &ctx,
135137
// disallow import symbols.
136138
if (!isa<DefinedRegular>(sym) && !isa<DefinedCommon>(sym))
137139
return false;
138-
if (excludeSymbols.count(sym->getName()))
140+
if (excludeSymbols.count(sym->getName()) || manualExcludeSymbols.count(sym->getName()))
139141
return false;
140142

141143
for (StringRef prefix : excludeSymbolPrefixes.keys())

lld/COFF/MinGW.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "Symbols.h"
1414
#include "lld/Common/LLVM.h"
1515
#include "llvm/ADT/ArrayRef.h"
16+
#include "llvm/ADT/DenseSet.h"
1617
#include "llvm/ADT/StringSet.h"
1718
#include "llvm/Option/ArgList.h"
1819
#include <vector>
@@ -24,7 +25,7 @@ class COFFLinkerContext;
2425
// symbols for MinGW.
2526
class AutoExporter {
2627
public:
27-
AutoExporter();
28+
AutoExporter(const llvm::DenseSet<StringRef> &manualExcludeSymbols);
2829

2930
void addWholeArchive(StringRef path);
3031
void addExcludedSymbol(StringRef symbol);
@@ -35,6 +36,8 @@ class AutoExporter {
3536
llvm::StringSet<> excludeLibs;
3637
llvm::StringSet<> excludeObjects;
3738

39+
const llvm::DenseSet<StringRef> &manualExcludeSymbols;
40+
3841
bool shouldExport(const COFFLinkerContext &ctx, Defined *sym) const;
3942
};
4043

lld/docs/ReleaseNotes.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ MinGW Improvements
4242
* The ``--exclude-symbols`` option is now supported.
4343
(`D130118 <https://reviews.llvm.org/D130118>`_)
4444

45+
* Support for an entirely new object file directive, ``-exclude-symbols:``,
46+
has been implemented. (`D130120 <https://reviews.llvm.org/D130120>`_)
47+
4548
MachO Improvements
4649
------------------
4750

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// REQUIRES: x86
2+
// RUN: llvm-mc -filetype=obj -triple=i686-win32-gnu %s -o %t.o
3+
4+
// RUN: lld-link -lldmingw -dll -out:%t.dll %t.o -noentry
5+
// RUN: llvm-readobj --coff-exports %t.dll | FileCheck --implicit-check-not=Name: %s
6+
7+
// CHECK: Name:
8+
// CHECK: Name: sym1
9+
10+
.global _sym1
11+
_sym1:
12+
ret
13+
14+
.global _sym2
15+
_sym2:
16+
ret
17+
18+
.global _sym3
19+
_sym3:
20+
ret
21+
22+
.section .drectve,"yn"
23+
.ascii " -exclude-symbols:sym2,unknownsym"
24+
.ascii " -exclude-symbols:unkonwnsym,sym3"

0 commit comments

Comments
 (0)