[lld] a54919e - [LLD] [COFF] Error out if creating a DLL with too many exported symbols

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 11:32:53 PDT 2020


Author: Martin Storsjö
Date: 2020-08-31T21:15:13+03:00
New Revision: a54919e0c11542f6716043003e403f1910f32528

URL: https://github.com/llvm/llvm-project/commit/a54919e0c11542f6716043003e403f1910f32528
DIFF: https://github.com/llvm/llvm-project/commit/a54919e0c11542f6716043003e403f1910f32528.diff

LOG: [LLD] [COFF] Error out if creating a DLL with too many exported symbols

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

Added: 
    lld/test/COFF/Inputs/def-many.py
    lld/test/COFF/export-limit.s

Modified: 
    lld/COFF/DriverUtils.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/DriverUtils.cpp b/lld/COFF/DriverUtils.cpp
index 6cb761abea4e..de78359bb446 100644
--- 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 <limits>
 #include <memory>
 
 using namespace llvm::COFF;
@@ -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

diff  --git a/lld/test/COFF/Inputs/def-many.py b/lld/test/COFF/Inputs/def-many.py
new file mode 100644
index 000000000000..ec2b811fce75
--- /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
index 000000000000..e65a84cb718f
--- /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


        


More information about the llvm-commits mailing list