[lld] r196754 - [PECOFF] Implement /alternatename weak symbols.

Rui Ueyama ruiu at google.com
Sun Dec 8 21:02:57 PST 2013


Author: ruiu
Date: Sun Dec  8 23:02:57 2013
New Revision: 196754

URL: http://llvm.org/viewvc/llvm-project?rev=196754&view=rev
Log:
[PECOFF] Implement /alternatename weak symbols.

Added:
    lld/trunk/test/pecoff/Inputs/alternatename1.obj.yaml
    lld/trunk/test/pecoff/Inputs/alternatename2.obj.yaml
    lld/trunk/test/pecoff/alternatename.test
Modified:
    lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp

Modified: lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp?rev=196754&r1=196753&r2=196754&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp (original)
+++ lld/trunk/lib/ReaderWriter/PECOFF/ReaderCOFF.cpp Sun Dec  8 23:02:57 2013
@@ -62,7 +62,7 @@ private:
   SectionToAtomsT;
 
 public:
-  FileCOFF(const LinkingContext &context, std::unique_ptr<MemoryBuffer> mb,
+  FileCOFF(const PECOFFLinkingContext &context, std::unique_ptr<MemoryBuffer> mb,
            error_code &ec);
 
   virtual const atom_collection<DefinedAtom> &defined() const {
@@ -156,7 +156,7 @@ private:
   _definedAtomLocations;
 
   mutable llvm::BumpPtrAllocator _alloc;
-  const LinkingContext &_context;
+  const PECOFFLinkingContext &_context;
   uint64_t _ordinal;
 };
 
@@ -278,7 +278,7 @@ DefinedAtom::Merge getMerge(const coff_a
   }
 }
 
-FileCOFF::FileCOFF(const LinkingContext &context,
+FileCOFF::FileCOFF(const PECOFFLinkingContext &context,
                    std::unique_ptr<MemoryBuffer> mb, error_code &ec)
     : File(mb->getBufferIdentifier(), kindObject), _context(context),
       _ordinal(0) {
@@ -626,6 +626,18 @@ FileCOFF::AtomizeDefinedSymbolsInSection
     // if this is the last symbol, take up the remaining data.
     const uint8_t *end = (si + 1 == se) ? secData.data() + secData.size()
                                         : secData.data() + (*(si + 1))->Value;
+    StringRef symbolName = _symbolName[*si];
+    StringRef alias = _context.getAlternateName(symbolName);
+
+    if (!alias.empty()) {
+      auto *atom = new (_alloc) COFFDefinedAtom(
+        *this, alias, sectionName, getScope(*si), type, isComdat,
+        perms, DefinedAtom::mergeAsWeak, ArrayRef<uint8_t>(), _ordinal++);
+      atoms.push_back(atom);
+      _symbolAtom[*si] = atom;
+      _definedAtomLocations[section][(*si)->Value].push_back(atom);
+    }
+
     ArrayRef<uint8_t> data(start, end);
     auto *atom = new (_alloc) COFFDefinedAtom(
         *this, _symbolName[*si], sectionName, getScope(*si), type, isComdat,
@@ -953,7 +965,7 @@ ReaderCOFF::parseCOFFFile(std::unique_pt
                           std::vector<std::unique_ptr<File> > &result) const {
   // Parse the memory buffer as PECOFF file.
   error_code ec;
-  std::unique_ptr<FileCOFF> file(new FileCOFF(_context, std::move(mb), ec));
+  std::unique_ptr<FileCOFF> file(new FileCOFF(_PECOFFLinkingContext, std::move(mb), ec));
   if (ec)
     return ec;
 

Added: lld/trunk/test/pecoff/Inputs/alternatename1.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/alternatename1.obj.yaml?rev=196754&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/alternatename1.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/alternatename1.obj.yaml Sun Dec  8 23:02:57 2013
@@ -0,0 +1,23 @@
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       1
+    SectionData:     90909090
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            _foo
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

Added: lld/trunk/test/pecoff/Inputs/alternatename2.obj.yaml
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/Inputs/alternatename2.obj.yaml?rev=196754&view=auto
==============================================================================
--- lld/trunk/test/pecoff/Inputs/alternatename2.obj.yaml (added)
+++ lld/trunk/test/pecoff/Inputs/alternatename2.obj.yaml Sun Dec  8 23:02:57 2013
@@ -0,0 +1,23 @@
+---
+header:
+  Machine:         IMAGE_FILE_MACHINE_I386
+  Characteristics: [  ]
+sections:
+  - Name:            .text
+    Characteristics: [ IMAGE_SCN_CNT_CODE, IMAGE_SCN_MEM_EXECUTE, IMAGE_SCN_MEM_READ ]
+    Alignment:       4
+    SectionData:     CCCCCCCC
+symbols:
+  - Name:            .text
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_STATIC
+  - Name:            _main
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_NULL
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
+...

Added: lld/trunk/test/pecoff/alternatename.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/pecoff/alternatename.test?rev=196754&view=auto
==============================================================================
--- lld/trunk/test/pecoff/alternatename.test (added)
+++ lld/trunk/test/pecoff/alternatename.test Sun Dec  8 23:02:57 2013
@@ -0,0 +1,18 @@
+# RUN: yaml2obj %p/Inputs/alternatename1.obj.yaml > %t1.obj
+# RUN: yaml2obj %p/Inputs/alternatename2.obj.yaml > %t2.obj
+#
+# RUN: lld -flavor link /out:%t1.exe /alternatename:_main=_foo -- %t1.obj
+# RUN: llvm-objdump -d %t2.exe | FileCheck -check-prefix=CHECK1 %s
+#
+# RUN: lld -flavor link /out:%t2.exe /alternatename:_main=_foo  -- %t1.obj %t2.obj
+# RUN: llvm-objdump -d %t2.exe | FileCheck -check-prefix=CHECK2 %s
+
+CHECK1:      nop
+CHECK1-NEXT: nop
+CHECK1-NEXT: nop
+CHECK1-NEXT: nop
+
+CHECK2:      int3
+CHECK2-NEXT: int3
+CHECK2-NEXT: int3
+CHECK2-NEXT: int3





More information about the llvm-commits mailing list