[lld] r245258 - COFF: Allow forward reference for weak externals

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 17 16:35:44 PDT 2015


Author: ruiu
Date: Mon Aug 17 18:35:43 2015
New Revision: 245258

URL: http://llvm.org/viewvc/llvm-project?rev=245258&view=rev
Log:
COFF: Allow forward reference for weak externals

Previously, weak external symbols could reference only symbols that
appeared before them. Although that covers almost all use cases
of weak externals, there are object files out there which contains
weak externals that have forward references.

This patch supports such weak externals.

Modified:
    lld/trunk/COFF/InputFiles.cpp
    lld/trunk/COFF/Symbols.cpp
    lld/trunk/test/COFF/weak-external.test

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=245258&r1=245257&r2=245258&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Mon Aug 17 18:35:43 2015
@@ -154,6 +154,7 @@ void ObjectFile::initializeSymbols() {
   uint32_t NumSymbols = COFFObj->getNumberOfSymbols();
   SymbolBodies.reserve(NumSymbols);
   SparseSymbolBodies.resize(NumSymbols);
+  llvm::SmallVector<Undefined *, 8> WeakAliases;
   int32_t LastSectionNumber = 0;
   for (uint32_t I = 0; I < NumSymbols; ++I) {
     // Get a COFFSymbolRef object.
@@ -172,6 +173,7 @@ void ObjectFile::initializeSymbols() {
       Body = createUndefined(Sym);
     } else if (Sym.isWeakExternal()) {
       Body = createWeakExternal(Sym, AuxP);
+      WeakAliases.push_back((Undefined *)Body);
     } else {
       Body = createDefined(Sym, AuxP, IsFirst);
     }
@@ -182,6 +184,8 @@ void ObjectFile::initializeSymbols() {
     I += Sym.getNumberOfAuxSymbols();
     LastSectionNumber = Sym.getSectionNumber();
   }
+  for (Undefined *U : WeakAliases)
+    U->WeakAlias = SparseSymbolBodies[(uintptr_t)U->WeakAlias];
 }
 
 Undefined *ObjectFile::createUndefined(COFFSymbolRef Sym) {
@@ -195,7 +199,7 @@ Undefined *ObjectFile::createWeakExterna
   COFFObj->getSymbolName(Sym, Name);
   auto *U = new (Alloc) Undefined(Name);
   auto *Aux = (const coff_aux_weak_external *)AuxP;
-  U->WeakAlias = SparseSymbolBodies[Aux->TagIndex];
+  U->WeakAlias = (Undefined *)(uintptr_t)Aux->TagIndex;
   return U;
 }
 

Modified: lld/trunk/COFF/Symbols.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Symbols.cpp?rev=245258&r1=245257&r2=245258&view=diff
==============================================================================
--- lld/trunk/COFF/Symbols.cpp (original)
+++ lld/trunk/COFF/Symbols.cpp Mon Aug 17 18:35:43 2015
@@ -132,7 +132,7 @@ int SymbolBody::compare(SymbolBody *Othe
     auto *RHS = cast<Undefined>(Other);
     // Tie if both undefined symbols have different weak aliases.
     if (LHS->WeakAlias && RHS->WeakAlias) {
-      if (LHS->WeakAlias->repl() != RHS->WeakAlias->repl())
+      if (LHS->WeakAlias->getName() != RHS->WeakAlias->getName())
         return 0;
       return uintptr_t(LHS) < uintptr_t(RHS) ? 1 : -1;
     }

Modified: lld/trunk/test/COFF/weak-external.test
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/COFF/weak-external.test?rev=245258&r1=245257&r2=245258&view=diff
==============================================================================
--- lld/trunk/test/COFF/weak-external.test (original)
+++ lld/trunk/test/COFF/weak-external.test Mon Aug 17 18:35:43 2015
@@ -18,12 +18,6 @@ sections:
     Alignment:       16
     SectionData:     00
 symbols:
-  - Name:            'f'
-    Value:           0
-    SectionNumber:   1
-    SimpleType:      IMAGE_SYM_TYPE_NULL
-    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
-    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
   - Name:            'g'
     Value:           0
     SectionNumber:   0
@@ -31,6 +25,12 @@ symbols:
     ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
     StorageClass:    IMAGE_SYM_CLASS_WEAK_EXTERNAL
     WeakExternal:
-      TagIndex:        0
+      TagIndex:        2
       Characteristics: IMAGE_WEAK_EXTERN_SEARCH_LIBRARY
+  - Name:            'f'
+    Value:           0
+    SectionNumber:   1
+    SimpleType:      IMAGE_SYM_TYPE_NULL
+    ComplexType:     IMAGE_SYM_DTYPE_FUNCTION
+    StorageClass:    IMAGE_SYM_CLASS_EXTERNAL
 ...




More information about the llvm-commits mailing list