diff --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp --- a/lld/COFF/DriverUtils.cpp +++ b/lld/COFF/DriverUtils.cpp @@ -32,6 +32,7 @@ #include "llvm/Support/Program.h" #include "llvm/Support/raw_ostream.h" #include "llvm/WindowsManifest/WindowsManifestMerger.h" +#include #include using namespace llvm::COFF; @@ -673,12 +674,15 @@ 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::max()) + fatal("too many exported symbols (max " + + Twine(std::numeric_limits::max()) + ")"); } // Parses a string in the form of "key=value" and check diff --git a/lld/test/COFF/Inputs/def-many.py b/lld/test/COFF/Inputs/def-many.py new file mode 100644 --- /dev/null +++ b/lld/test/COFF/Inputs/def-many.py @@ -0,0 +1,5 @@ +import sys + +print("EXPORTS") +for i in range(0, int(sys.argv[1])): + print("f%d=f" % (i)) diff --git a/lld/test/COFF/export-limit.s b/lld/test/COFF/export-limit.s new file mode 100644 --- /dev/null +++ b/lld/test/COFF/export-limit.s @@ -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