[lld] r223200 - [PECOFF] Fix a bug in /export option handler.

Rui Ueyama ruiu at google.com
Tue Dec 2 20:34:20 PST 2014


Author: ruiu
Date: Tue Dec  2 22:34:20 2014
New Revision: 223200

URL: http://llvm.org/viewvc/llvm-project?rev=223200&view=rev
Log:
[PECOFF] Fix a bug in /export option handler.

/export option can be given multiple times to specify multiple
symbols to be exported. /export accepts both decorated and
undecorated name.

If you give both undecorated and decorated name of the same symbol
to /export, they are resolved to the same symbol. In this case,
we need to de-duplicate the exported names, so that we don't have
duplicated items in the export symbol table in a DLL.

We remove duplicate items from a vector. The bug was there.
Because we had pointers pointing to elements of the vector,
after an item is removed, they would point wrong elements.

This patch is to remove these pointers. Added a test for that case.

Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
    lld/trunk/test/pecoff/export.test

Modified: lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h?rev=223200&r1=223199&r2=223200&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/LinkerGeneratedSymbolFile.h Tue Dec  2 22:34:20 2014
@@ -250,18 +250,16 @@ public:
       : VirtualArchiveLibraryFile("<export>"), _syms(syms),
         _ctx(const_cast<PECOFFLinkingContext *>(&ctx)) {
     for (PECOFFLinkingContext::ExportDesc &desc : _ctx->getDllExports())
-      _exportedSyms[desc.name] = &desc;
+      _exportedSyms.insert(desc.name);
   }
 
   const File *find(StringRef sym, bool dataSymbolOnly) const override {
     typedef PECOFFLinkingContext::ExportDesc ExportDesc;
-    auto it = _exportedSyms.find(sym);
-    if (it == _exportedSyms.end())
+    if (_exportedSyms.count(sym) == 0)
       return nullptr;
     std::string replace;
     if (!findDecoratedSymbol(_ctx, _syms.get(), sym.str(), replace))
       return nullptr;
-    ExportDesc *desc = it->second;
 
     // We found a decorated symbol. There may be another symbol that
     // has the same decorated name. If that's the case, we remove the
@@ -273,10 +271,15 @@ public:
     if (isFound != exp.end()) {
       exp.erase(
           std::remove_if(exp.begin(), exp.end(),
-                         [&](ExportDesc &e) { return &e == desc; }),
+                         [&](ExportDesc &e) { return e.name == sym; }),
           exp.end());
     } else {
-      it->second->name = replace;
+      for (ExportDesc &e : exp) {
+        if (e.name == sym) {
+          e.name = replace;
+          break;
+        }
+      }
       if (_ctx->deadStrip())
         _ctx->addDeadStripRoot(_ctx->allocate(replace));
     }
@@ -285,7 +288,7 @@ public:
   }
 
 private:
-  std::map<std::string, PECOFFLinkingContext::ExportDesc *> _exportedSyms;
+  std::set<std::string> _exportedSyms;
   std::shared_ptr<ResolvableSymbols> _syms;
   mutable llvm::BumpPtrAllocator _alloc;
   mutable PECOFFLinkingContext *_ctx;

Modified: lld/trunk/test/pecoff/export.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/export.test?rev=223200&r1=223199&r2=223200&view=diff
==============================================================================
--- lld/trunk/test/pecoff/export.test (original)
+++ lld/trunk/test/pecoff/export.test Tue Dec  2 22:34:20 2014
@@ -62,6 +62,7 @@ CHECK6:       Ordinal      RVA  Name
 CHECK6-NEXT:        1   0x2010  ?exportfn8@@YAXXZ
 
 # RUN: lld -flavor link /out:%t6.dll /dll /entry:init \
+# RUN:   /export:exportfn7 /export:exportfn7 at 8 \
 # RUN:   /export:exportfn8 /export:exportfn8 /export:exportfn3 -- %t.obj
 # RUN: llvm-objdump -p %t6.dll | FileCheck -check-prefix=DUP %s
 
@@ -70,6 +71,7 @@ DUP:      DLL name: export.test.tmp6.dll
 DUP:       Ordinal      RVA  Name
 DUP-NEXT:        1   0x2010  ?exportfn8@@YAXXZ
 DUP-NEXT:        2   0x2010  exportfn3 at 256
+DUP-NEXT:        3   0x2010  exportfn7 at 8
 DUP-NOT:  ?exportfn8@@YAXXZ
 DUP-NOT:  exportfn3 at 256
 





More information about the llvm-commits mailing list