Home | History | Annotate | Download | only in LD
      1 //===- NamePool.cpp -------------------------------------------------------===//
      2 //
      3 //                     The MCLinker Project
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 #include <llvm/Support/raw_ostream.h>
     10 #include <mcld/LD/NamePool.h>
     11 #include <mcld/LD/StaticResolver.h>
     12 
     13 using namespace mcld;
     14 
     15 //===----------------------------------------------------------------------===//
     16 // NamePool
     17 //===----------------------------------------------------------------------===//
     18 NamePool::NamePool(NamePool::size_type pSize)
     19   : m_pResolver(new StaticResolver()), m_Table(pSize) {
     20 }
     21 
     22 NamePool::~NamePool()
     23 {
     24   delete m_pResolver;
     25 
     26   FreeInfoSet::iterator info, iEnd = m_FreeInfoSet.end();
     27   for (info = m_FreeInfoSet.begin(); info != iEnd; ++info) {
     28     ResolveInfo::Destroy(*info);
     29   }
     30 }
     31 
     32 /// createSymbol - create a symbol
     33 ResolveInfo* NamePool::createSymbol(const llvm::StringRef& pName,
     34                                     bool pIsDyn,
     35                                     ResolveInfo::Type pType,
     36                                     ResolveInfo::Desc pDesc,
     37                                     ResolveInfo::Binding pBinding,
     38                                     ResolveInfo::SizeType pSize,
     39                                     ResolveInfo::Visibility pVisibility)
     40 {
     41   ResolveInfo** result = m_FreeInfoSet.allocate();
     42   (*result) = ResolveInfo::Create(pName);
     43   (*result)->setIsSymbol(true);
     44   (*result)->setSource(pIsDyn);
     45   (*result)->setType(pType);
     46   (*result)->setDesc(pDesc);
     47   (*result)->setBinding(pBinding);
     48   (*result)->setVisibility(pVisibility);
     49   (*result)->setSize(pSize);
     50   return *result;
     51 }
     52 
     53 /// insertSymbol - insert a symbol and resolve it immediately
     54 /// @return the pointer of resolved ResolveInfo
     55 /// @return is the symbol existent?
     56 void NamePool::insertSymbol(const llvm::StringRef& pName,
     57                               bool pIsDyn,
     58                               ResolveInfo::Type pType,
     59                               ResolveInfo::Desc pDesc,
     60                               ResolveInfo::Binding pBinding,
     61                               ResolveInfo::SizeType pSize,
     62                               LDSymbol::ValueType pValue,
     63                               ResolveInfo::Visibility pVisibility,
     64                               ResolveInfo* pOldInfo,
     65                               Resolver::Result& pResult)
     66 {
     67   // We should check if there is any symbol with the same name existed.
     68   // If it already exists, we should use resolver to decide which symbol
     69   // should be reserved. Otherwise, we insert the symbol and set up its
     70   // attributes.
     71   bool exist = false;
     72   ResolveInfo* old_symbol = m_Table.insert(pName, exist);
     73   ResolveInfo* new_symbol = NULL;
     74   if (exist && old_symbol->isSymbol()) {
     75     new_symbol = m_Table.getEntryFactory().produce(pName);
     76   }
     77   else {
     78     exist = false;
     79     new_symbol = old_symbol;
     80   }
     81 
     82   new_symbol->setIsSymbol(true);
     83   new_symbol->setSource(pIsDyn);
     84   new_symbol->setType(pType);
     85   new_symbol->setDesc(pDesc);
     86   new_symbol->setBinding(pBinding);
     87   new_symbol->setVisibility(pVisibility);
     88   new_symbol->setSize(pSize);
     89 
     90   if (!exist) {
     91     // old_symbol is neither existed nor a symbol.
     92     pResult.info      = new_symbol;
     93     pResult.existent  = false;
     94     pResult.overriden = true;
     95     return;
     96   }
     97   else if (NULL != pOldInfo) {
     98     // existent, remember its attribute
     99     pOldInfo->override(*old_symbol);
    100   }
    101 
    102   // exist and is a symbol
    103   // symbol resolution
    104   bool override = false;
    105   unsigned int action = Resolver::LastAction;
    106   if (m_pResolver->resolve(*old_symbol, *new_symbol, override, pValue)) {
    107     pResult.info      = old_symbol;
    108     pResult.existent  = true;
    109     pResult.overriden = override;
    110   }
    111   else {
    112       m_pResolver->resolveAgain(*this, action, *old_symbol, *new_symbol, pResult);
    113   }
    114 
    115   m_Table.getEntryFactory().destroy(new_symbol);
    116   return;
    117 }
    118 
    119 llvm::StringRef NamePool::insertString(const llvm::StringRef& pString)
    120 {
    121   bool exist = false;
    122   ResolveInfo* resolve_info = m_Table.insert(pString, exist);
    123   return llvm::StringRef(resolve_info->name(), resolve_info->nameSize());
    124 }
    125 
    126 void NamePool::reserve(NamePool::size_type pSize)
    127 {
    128   m_Table.rehash(pSize);
    129 }
    130 
    131 NamePool::size_type NamePool::capacity() const
    132 {
    133   return (m_Table.numOfBuckets() - m_Table.numOfEntries());
    134 }
    135 
    136 /// findInfo - find the resolved ResolveInfo
    137 ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName)
    138 {
    139   Table::iterator iter = m_Table.find(pName);
    140   return iter.getEntry();
    141 }
    142 
    143 /// findInfo - find the resolved ResolveInfo
    144 const ResolveInfo* NamePool::findInfo(const llvm::StringRef& pName) const
    145 {
    146   Table::const_iterator iter = m_Table.find(pName);
    147   return iter.getEntry();
    148 }
    149 
    150 /// findSymbol - find the resolved output LDSymbol
    151 LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName)
    152 {
    153   ResolveInfo* info = findInfo(pName);
    154   if (NULL == info)
    155     return NULL;
    156   return info->outSymbol();
    157 }
    158 
    159 /// findSymbol - find the resolved output LDSymbol
    160 const LDSymbol* NamePool::findSymbol(const llvm::StringRef& pName) const
    161 {
    162   const ResolveInfo* info = findInfo(pName);
    163   if (NULL == info)
    164     return NULL;
    165   return info->outSymbol();
    166 }
    167 
    168