Skip to content

Commit

Permalink
[LLD] [COFF] Error out if creating a DLL with too many exported symbols
Browse files Browse the repository at this point in the history
The PE/DLL format has a limit on 64k exported symbols per DLL; make
sure to check this.

Differential Revision: https://reviews.llvm.org/D86701
  • Loading branch information
mstorsjo committed Aug 31, 2020
1 parent 44133d9 commit a54919e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
8 changes: 6 additions & 2 deletions lld/COFF/DriverUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "llvm/Support/Program.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/WindowsManifest/WindowsManifestMerger.h"
#include <limits>
#include <memory>

using namespace llvm::COFF;
Expand Down Expand Up @@ -673,12 +674,15 @@ void fixupExports() {

void assignExportOrdinals() {
// Assign unique ordinals if default (= 0).
uint16_t max = 0;
uint32_t max = 0;
for (Export &e : config->exports)
max = std::max(max, e.ordinal);
max = std::max(max, (uint32_t)e.ordinal);
for (Export &e : config->exports)
if (e.ordinal == 0)
e.ordinal = ++max;
if (max > std::numeric_limits<uint16_t>::max())
fatal("too many exported symbols (max " +
Twine(std::numeric_limits<uint16_t>::max()) + ")");
}

// Parses a string in the form of "key=value" and check
Expand Down
5 changes: 5 additions & 0 deletions lld/test/COFF/Inputs/def-many.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import sys

print("EXPORTS")
for i in range(0, int(sys.argv[1])):
print("f%d=f" % (i))
13 changes: 13 additions & 0 deletions lld/test/COFF/export-limit.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# REQUIRES: x86
# RUN: %python %p/Inputs/def-many.py 65535 > %t-65535.def
# RUN: %python %p/Inputs/def-many.py 65536 > %t-65536.def
# RUN: llvm-mc -triple x86_64-win32 %s -filetype=obj -o %t.obj
# RUN: lld-link -dll -noentry %t.obj -out:%t.dll -def:%t-65535.def
# RUN: not lld-link -dll -noentry %t.obj -out:%t.dll -def:%t-65536.def 2>&1 | FileCheck %s

# CHECK: error: too many exported symbols

.text
.globl f
f:
ret

0 comments on commit a54919e

Please sign in to comment.