Home | History | Annotate | Download | only in Target
      1 //===- GNULDBackend.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/ELF.h>
     10 #include <mcld/ADT/SizeTraits.h>
     11 #include <mcld/Target/GNULDBackend.h>
     12 #include <mcld/MC/MCLDInfo.h>
     13 #include <mcld/MC/MCLDOutput.h>
     14 #include <mcld/MC/MCLDInputTree.h>
     15 #include <mcld/MC/SymbolCategory.h>
     16 #include <mcld/LD/LDSymbol.h>
     17 #include <mcld/LD/Layout.h>
     18 #include <mcld/Support/MemoryArea.h>
     19 #include <mcld/Support/MemoryRegion.h>
     20 #include <string>
     21 #include <cstring>
     22 #include <cassert>
     23 
     24 using namespace mcld;
     25 
     26 //===----------------------------------------------------------------------===//
     27 // GNULDBackend
     28 GNULDBackend::GNULDBackend()
     29   : m_pArchiveReader(0),
     30     m_pObjectReader(0),
     31     m_pDynObjReader(0),
     32     m_pObjectWriter(0),
     33     m_pDynObjWriter(0),
     34     m_pDynObjFileFormat(0),
     35     m_pExecFileFormat(0),
     36     m_ELFSegmentTable(9)// magic number
     37 {
     38   m_pSymIndexMap = new HashTableType(1024);
     39 }
     40 
     41 GNULDBackend::~GNULDBackend()
     42 {
     43   if (m_pArchiveReader)
     44     delete m_pArchiveReader;
     45   if (m_pObjectReader)
     46     delete m_pObjectReader;
     47   if (m_pDynObjReader)
     48     delete m_pDynObjReader;
     49   if (m_pObjectWriter)
     50     delete m_pObjectWriter;
     51   if (m_pDynObjWriter)
     52     delete m_pDynObjWriter;
     53   if (m_pDynObjFileFormat)
     54     delete m_pDynObjFileFormat;
     55   if (m_pExecFileFormat)
     56     delete m_pExecFileFormat;
     57   if(m_pSymIndexMap)
     58     delete m_pSymIndexMap;
     59 }
     60 
     61 size_t GNULDBackend::sectionStartOffset() const
     62 {
     63   // FIXME: use fixed offset, we need 10 segments by default
     64   return sizeof(llvm::ELF::Elf64_Ehdr)+10*sizeof(llvm::ELF::Elf64_Phdr);
     65 }
     66 
     67 bool GNULDBackend::initArchiveReader(MCLinker&, MCLDInfo &pInfo)
     68 {
     69   if (0 == m_pArchiveReader)
     70   {
     71     LDReader::Endian isLittleEndian = LDReader::LittleEndian;
     72     m_pArchiveReader = new GNUArchiveReader(pInfo, isLittleEndian);
     73   }
     74   return true;
     75 }
     76 
     77 bool GNULDBackend::initObjectReader(MCLinker& pLinker)
     78 {
     79   if (0 == m_pObjectReader)
     80     m_pObjectReader = new ELFObjectReader(*this, pLinker);
     81   return true;
     82 }
     83 
     84 bool GNULDBackend::initDynObjReader(MCLinker& pLinker)
     85 {
     86   if (0 == m_pDynObjReader)
     87     m_pDynObjReader = new ELFDynObjReader(*this, pLinker);
     88   return true;
     89 }
     90 
     91 bool GNULDBackend::initObjectWriter(MCLinker&)
     92 {
     93   // TODO
     94   return true;
     95 }
     96 
     97 bool GNULDBackend::initDynObjWriter(MCLinker& pLinker)
     98 {
     99   if (0 == m_pDynObjWriter)
    100     m_pDynObjWriter = new ELFDynObjWriter(*this, pLinker);
    101   return true;
    102 }
    103 
    104 bool GNULDBackend::initExecSections(MCLinker& pMCLinker)
    105 {
    106   if (0 == m_pExecFileFormat)
    107     m_pExecFileFormat = new ELFExecFileFormat(*this);
    108 
    109   // initialize standard sections
    110   m_pExecFileFormat->initStdSections(pMCLinker);
    111   return true;
    112 }
    113 
    114 bool GNULDBackend::initDynObjSections(MCLinker& pMCLinker)
    115 {
    116   if (0 == m_pDynObjFileFormat)
    117     m_pDynObjFileFormat = new ELFDynObjFileFormat(*this);
    118 
    119   // initialize standard sections
    120   m_pDynObjFileFormat->initStdSections(pMCLinker);
    121   return true;
    122 }
    123 
    124 bool GNULDBackend::initStandardSymbols(MCLinker& pLinker)
    125 {
    126   return true;
    127 }
    128 
    129 GNUArchiveReader *GNULDBackend::getArchiveReader()
    130 {
    131   assert(0 != m_pArchiveReader);
    132   return m_pArchiveReader;
    133 }
    134 
    135 GNUArchiveReader *GNULDBackend::getArchiveReader() const
    136 {
    137   assert(0 != m_pArchiveReader);
    138   return m_pArchiveReader;
    139 }
    140 
    141 ELFObjectReader *GNULDBackend::getObjectReader()
    142 {
    143   assert(0 != m_pObjectReader);
    144   return m_pObjectReader;
    145 }
    146 
    147 ELFObjectReader *GNULDBackend::getObjectReader() const
    148 {
    149   assert(0 != m_pObjectReader);
    150   return m_pObjectReader;
    151 }
    152 
    153 ELFDynObjReader *GNULDBackend::getDynObjReader()
    154 {
    155   assert(0 != m_pDynObjReader);
    156   return m_pDynObjReader;
    157 }
    158 
    159 ELFDynObjReader *GNULDBackend::getDynObjReader() const
    160 {
    161   assert(0 != m_pDynObjReader);
    162   return m_pDynObjReader;
    163 }
    164 
    165 ELFObjectWriter *GNULDBackend::getObjectWriter()
    166 {
    167   // TODO
    168   return NULL;
    169 }
    170 
    171 ELFObjectWriter *GNULDBackend::getObjectWriter() const
    172 {
    173   // TODO
    174   return NULL;
    175 }
    176 
    177 ELFDynObjWriter *GNULDBackend::getDynObjWriter()
    178 {
    179   assert(0 != m_pDynObjWriter);
    180   return m_pDynObjWriter;
    181 }
    182 
    183 ELFDynObjWriter *GNULDBackend::getDynObjWriter() const
    184 {
    185   assert(0 != m_pDynObjWriter);
    186   return m_pDynObjWriter;
    187 }
    188 
    189 ELFDynObjFileFormat* GNULDBackend::getDynObjFileFormat()
    190 {
    191   assert(0 != m_pDynObjFileFormat);
    192   return m_pDynObjFileFormat;
    193 }
    194 
    195 ELFDynObjFileFormat* GNULDBackend::getDynObjFileFormat() const
    196 {
    197   assert(0 != m_pDynObjFileFormat);
    198   return m_pDynObjFileFormat;
    199 }
    200 
    201 ELFExecFileFormat* GNULDBackend::getExecFileFormat()
    202 {
    203   assert(0 != m_pExecFileFormat);
    204   return m_pExecFileFormat;
    205 }
    206 
    207 ELFExecFileFormat* GNULDBackend::getExecFileFormat() const
    208 {
    209   assert(0 != m_pExecFileFormat);
    210   return m_pExecFileFormat;
    211 }
    212 
    213 /// sizeNamePools - compute the size of regular name pools
    214 /// In ELF executable files, regular name pools are .symtab, .strtab,
    215 /// .dynsym, .dynstr, and .hash
    216 void
    217 GNULDBackend::sizeNamePools(const Output& pOutput,
    218                             const SymbolCategory& pSymbols,
    219                             const MCLDInfo& pLDInfo)
    220 {
    221   // size of string tables starts from 1 to hold the null character in their
    222   // first byte
    223   size_t symtab = 1;
    224   size_t dynsym = 1;
    225   // number of entries in symbol tables starts from 1 to hold the special entry
    226   // at index 0 (STN_UNDEF). See ELF Spec Book I, p1-21.
    227   size_t strtab = 1;
    228   size_t dynstr = 1;
    229   size_t hash   = 0;
    230 
    231   // compute size of .symtab, .dynsym and .strtab
    232   SymbolCategory::const_iterator symbol;
    233   SymbolCategory::const_iterator symEnd = pSymbols.end();
    234   for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) {
    235     size_t str_size = (*symbol)->nameSize() + 1;
    236     if (isDynamicSymbol(**symbol, pOutput)) {
    237       ++dynsym;
    238       dynstr += str_size;
    239     }
    240     ++symtab;
    241     strtab += str_size;
    242   }
    243 
    244   ELFFileFormat* file_format = NULL;
    245   switch(pOutput.type()) {
    246     // compute size of .dynstr and .hash
    247     case Output::DynObj:
    248       file_format = getDynObjFileFormat();
    249       break;
    250     case Output::Exec:
    251       file_format = getExecFileFormat();
    252       break;
    253     case Output::Object:
    254     default:
    255       // TODO: not support yet
    256       return;
    257   }
    258 
    259   switch(pOutput.type()) {
    260     // compute size of .dynstr and .hash
    261     case Output::DynObj:
    262     case Output::Exec: {
    263       // add DT_NEED strings into .dynstr and .dynamic
    264       // Rules:
    265       //   1. ignore --no-add-needed
    266       //   2. force count in --no-as-needed
    267       //   3. judge --as-needed
    268       InputTree::const_bfs_iterator input, inputEnd = pLDInfo.inputs().bfs_end();
    269       for (input = pLDInfo.inputs().bfs_begin(); input != inputEnd; ++input) {
    270         if (Input::DynObj == (*input)->type()) {
    271           // --add-needed
    272           if ((*input)->attribute()->isAddNeeded()) {
    273             // --no-as-needed
    274             if (!(*input)->attribute()->isAsNeeded()) {
    275               dynstr += (*input)->name().size() + 1;
    276               dynamic().reserveNeedEntry();
    277             }
    278             // --as-needed
    279             else if ((*input)->isNeeded()) {
    280               dynstr += (*input)->name().size() + 1;
    281               dynamic().reserveNeedEntry();
    282             }
    283           }
    284         }
    285       } // for
    286 
    287       // compute .hash
    288       // Both Elf32_Word and Elf64_Word are 4 bytes
    289       hash = (2 + getHashBucketCount(dynsym, false) + dynsym) *
    290              sizeof(llvm::ELF::Elf32_Word);
    291 
    292       // set size
    293       dynstr += pOutput.name().size() + 1;
    294       if (32 == bitclass())
    295         file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf32_Sym));
    296       else
    297         file_format->getDynSymTab().setSize(dynsym*sizeof(llvm::ELF::Elf64_Sym));
    298       file_format->getDynStrTab().setSize(dynstr);
    299       file_format->getHashTab().setSize(hash);
    300 
    301     }
    302     /* fall through */
    303     case Output::Object: {
    304       if (32 == bitclass())
    305         file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf32_Sym));
    306       else
    307         file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf64_Sym));
    308       file_format->getStrTab().setSize(strtab);
    309       break;
    310     }
    311   } // end of switch
    312 
    313   // reserve fixed entries in the .dynamic section.
    314   if (Output::DynObj == pOutput.type() || Output::Exec == pOutput.type()) {
    315     // Because some entries in .dynamic section need information of .dynsym,
    316     // .dynstr, .symtab, .strtab and .hash, we can not reserve non-DT_NEEDED
    317     // entries until we get the size of the sections mentioned above
    318     dynamic().reserveEntries(pLDInfo, *file_format);
    319     file_format->getDynamic().setSize(dynamic().numOfBytes());
    320   }
    321 }
    322 
    323 /// emitRegNamePools - emit regular name pools - .symtab, .strtab
    324 ///
    325 /// the size of these tables should be computed before layout
    326 /// layout should computes the start offset of these tables
    327 void GNULDBackend::emitRegNamePools(Output& pOutput,
    328                                     SymbolCategory& pSymbols,
    329                                     const Layout& pLayout,
    330                                     const MCLDInfo& pLDInfo)
    331 {
    332 
    333   assert(pOutput.hasMemArea());
    334 
    335   bool sym_exist = false;
    336   HashTableType::entry_type* entry = 0;
    337 
    338   ELFFileFormat* file_format = NULL;
    339   switch(pOutput.type()) {
    340     // compute size of .dynstr and .hash
    341     case Output::DynObj:
    342       file_format = getDynObjFileFormat();
    343       break;
    344     case Output::Exec:
    345       file_format = getExecFileFormat();
    346       break;
    347     case Output::Object:
    348     default:
    349       // add first symbol into m_pSymIndexMap
    350       entry = m_pSymIndexMap->insert(NULL, sym_exist);
    351       entry->setValue(0);
    352 
    353       // TODO: not support yet
    354       return;
    355   }
    356 
    357   LDSection& symtab_sect = file_format->getSymTab();
    358   LDSection& strtab_sect = file_format->getStrTab();
    359 
    360   MemoryRegion* symtab_region = pOutput.memArea()->request(symtab_sect.offset(),
    361                                                            symtab_sect.size());
    362   MemoryRegion* strtab_region = pOutput.memArea()->request(strtab_sect.offset(),
    363                                                            strtab_sect.size());
    364 
    365   // set up symtab_region
    366   llvm::ELF::Elf32_Sym* symtab32 = NULL;
    367   llvm::ELF::Elf64_Sym* symtab64 = NULL;
    368   if (32 == bitclass())
    369     symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
    370   else if (64 == bitclass())
    371     symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
    372   else
    373     llvm::report_fatal_error(llvm::Twine("unsupported bitclass ") +
    374                              llvm::Twine(bitclass()) +
    375                              llvm::Twine(".\n"));
    376   // set up strtab_region
    377   char* strtab = (char*)strtab_region->start();
    378   strtab[0] = '\0';
    379 
    380   // initialize the first ELF symbol
    381   if (32 == bitclass()) {
    382     symtab32[0].st_name  = 0;
    383     symtab32[0].st_value = 0;
    384     symtab32[0].st_size  = 0;
    385     symtab32[0].st_info  = 0;
    386     symtab32[0].st_other = 0;
    387     symtab32[0].st_shndx = 0;
    388   }
    389   else { // must 64
    390     symtab64[0].st_name  = 0;
    391     symtab64[0].st_value = 0;
    392     symtab64[0].st_size  = 0;
    393     symtab64[0].st_info  = 0;
    394     symtab64[0].st_other = 0;
    395     symtab64[0].st_shndx = 0;
    396   }
    397 
    398   size_t symtabIdx = 1;
    399   size_t strtabsize = 1;
    400   // compute size of .symtab, .dynsym and .strtab
    401   SymbolCategory::iterator symbol;
    402   SymbolCategory::iterator symEnd = pSymbols.end();
    403   for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) {
    404 
    405      // maintain output's symbol and index map if building .o file
    406     if (Output::Object == pOutput.type()) {
    407       entry = m_pSymIndexMap->insert(NULL, sym_exist);
    408       entry->setValue(symtabIdx);
    409     }
    410 
    411     // FIXME: check the endian between host and target
    412     // write out symbol
    413     if (32 == bitclass()) {
    414       symtab32[symtabIdx].st_name  = strtabsize;
    415       symtab32[symtabIdx].st_value = getSymbolValue(**symbol);
    416       symtab32[symtabIdx].st_size  = getSymbolSize(**symbol);
    417       symtab32[symtabIdx].st_info  = getSymbolInfo(**symbol);
    418       symtab32[symtabIdx].st_other = (*symbol)->visibility();
    419       symtab32[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout);
    420     }
    421     else { // must 64
    422       symtab64[symtabIdx].st_name  = strtabsize;
    423       symtab64[symtabIdx].st_value = getSymbolValue(**symbol);
    424       symtab64[symtabIdx].st_size  = getSymbolSize(**symbol);
    425       symtab64[symtabIdx].st_info  = getSymbolInfo(**symbol);
    426       symtab64[symtabIdx].st_other = (*symbol)->visibility();
    427       symtab64[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout);
    428     }
    429     // write out string
    430     strcpy((strtab + strtabsize), (*symbol)->name());
    431 
    432     // write out
    433     // sum up counters
    434     ++symtabIdx;
    435     strtabsize += (*symbol)->nameSize() + 1;
    436   }
    437 }
    438 
    439 /// emitNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash
    440 ///
    441 /// the size of these tables should be computed before layout
    442 /// layout should computes the start offset of these tables
    443 void GNULDBackend::emitDynNamePools(Output& pOutput,
    444                                     SymbolCategory& pSymbols,
    445                                     const Layout& pLayout,
    446                                     const MCLDInfo& pLDInfo)
    447 {
    448   assert(pOutput.hasMemArea());
    449   ELFFileFormat* file_format = NULL;
    450 
    451   bool sym_exist = false;
    452   HashTableType::entry_type* entry = 0;
    453 
    454   switch(pOutput.type()) {
    455     // compute size of .dynstr and .hash
    456     case Output::DynObj:
    457       file_format = getDynObjFileFormat();
    458       break;
    459     case Output::Exec:
    460       file_format = getExecFileFormat();
    461       break;
    462     case Output::Object:
    463     default:
    464       // TODO: not support yet
    465       return;
    466   }
    467 
    468   LDSection& symtab_sect = file_format->getDynSymTab();
    469   LDSection& strtab_sect = file_format->getDynStrTab();
    470   LDSection& hash_sect   = file_format->getHashTab();
    471   LDSection& dyn_sect    = file_format->getDynamic();
    472 
    473   MemoryRegion* symtab_region = pOutput.memArea()->request(symtab_sect.offset(),
    474                                                            symtab_sect.size());
    475   MemoryRegion* strtab_region = pOutput.memArea()->request(strtab_sect.offset(),
    476                                                            strtab_sect.size());
    477   MemoryRegion* hash_region = pOutput.memArea()->request(hash_sect.offset(),
    478                                                          hash_sect.size());
    479   MemoryRegion* dyn_region = pOutput.memArea()->request(dyn_sect.offset(),
    480                                                         dyn_sect.size());
    481   // set up symtab_region
    482   llvm::ELF::Elf32_Sym* symtab32 = NULL;
    483   llvm::ELF::Elf64_Sym* symtab64 = NULL;
    484   if (32 == bitclass())
    485     symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
    486   else if (64 == bitclass())
    487     symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
    488   else
    489     llvm::report_fatal_error(llvm::Twine("unsupported bitclass ") +
    490                              llvm::Twine(bitclass()) +
    491                              llvm::Twine(".\n"));
    492 
    493   // initialize the first ELF symbol
    494   if (32 == bitclass()) {
    495     symtab32[0].st_name  = 0;
    496     symtab32[0].st_value = 0;
    497     symtab32[0].st_size  = 0;
    498     symtab32[0].st_info  = 0;
    499     symtab32[0].st_other = 0;
    500     symtab32[0].st_shndx = 0;
    501   }
    502   else { // must 64
    503     symtab64[0].st_name  = 0;
    504     symtab64[0].st_value = 0;
    505     symtab64[0].st_size  = 0;
    506     symtab64[0].st_info  = 0;
    507     symtab64[0].st_other = 0;
    508     symtab64[0].st_shndx = 0;
    509   }
    510   // set up strtab_region
    511   char* strtab = (char*)strtab_region->start();
    512   strtab[0] = '\0';
    513 
    514   // add the first symbol into m_pSymIndexMap
    515   entry = m_pSymIndexMap->insert(NULL, sym_exist);
    516   entry->setValue(0);
    517 
    518   size_t symtabIdx = 1;
    519   size_t strtabsize = 1;
    520 
    521   // emit of .dynsym, and .dynstr
    522   SymbolCategory::iterator symbol;
    523   SymbolCategory::iterator symEnd = pSymbols.end();
    524   for (symbol = pSymbols.begin(); symbol != symEnd; ++symbol) {
    525     if (!isDynamicSymbol(**symbol, pOutput))
    526       continue;
    527 
    528     // maintain output's symbol and index map
    529     entry = m_pSymIndexMap->insert(*symbol, sym_exist);
    530     entry->setValue(symtabIdx);
    531 
    532     // FIXME: check the endian between host and target
    533     // write out symbol
    534     if (32 == bitclass()) {
    535       symtab32[symtabIdx].st_name  = strtabsize;
    536       symtab32[symtabIdx].st_value = (*symbol)->value();
    537       symtab32[symtabIdx].st_size  = getSymbolSize(**symbol);
    538       symtab32[symtabIdx].st_info  = getSymbolInfo(**symbol);
    539       symtab32[symtabIdx].st_other = (*symbol)->visibility();
    540       symtab32[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout);
    541     }
    542     else { // must 64
    543       symtab64[symtabIdx].st_name  = strtabsize;
    544       symtab64[symtabIdx].st_value = (*symbol)->value();
    545       symtab64[symtabIdx].st_size  = getSymbolSize(**symbol);
    546       symtab64[symtabIdx].st_info  = getSymbolInfo(**symbol);
    547       symtab64[symtabIdx].st_other = (*symbol)->visibility();
    548       symtab64[symtabIdx].st_shndx = getSymbolShndx(**symbol, pLayout);
    549     }
    550     // write out string
    551     strcpy((strtab + strtabsize), (*symbol)->name());
    552 
    553     // sum up counters
    554     ++symtabIdx;
    555     strtabsize += (*symbol)->nameSize() + 1;
    556   }
    557 
    558   // emit DT_NEED
    559   // add DT_NEED strings into .dynstr
    560   // Rules:
    561   //   1. ignore --no-add-needed
    562   //   2. force count in --no-as-needed
    563   //   3. judge --as-needed
    564   ELFDynamic::iterator dt_need = dynamic().needBegin();
    565   InputTree::const_bfs_iterator input, inputEnd = pLDInfo.inputs().bfs_end();
    566   for (input = pLDInfo.inputs().bfs_begin(); input != inputEnd; ++input) {
    567     if (Input::DynObj == (*input)->type()) {
    568       // --add-needed
    569       if ((*input)->attribute()->isAddNeeded()) {
    570         // --no-as-needed
    571         if (!(*input)->attribute()->isAsNeeded()) {
    572           strcpy((strtab + strtabsize), (*input)->name().c_str());
    573           (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize);
    574           strtabsize += (*input)->name().size() + 1;
    575           ++dt_need;
    576         }
    577         // --as-needed
    578         else if ((*input)->isNeeded()) {
    579           strcpy((strtab + strtabsize), (*input)->name().c_str());
    580           (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize);
    581           strtabsize += (*input)->name().size() + 1;
    582           ++dt_need;
    583         }
    584       }
    585     }
    586   } // for
    587 
    588   // emit soname
    589   // initialize value of ELF .dynamic section
    590   dynamic().applySoname(strtabsize);
    591   dynamic().applyEntries(pLDInfo, *file_format);
    592   dynamic().emit(dyn_sect, *dyn_region);
    593 
    594   strcpy((strtab + strtabsize), pOutput.name().c_str());
    595   strtabsize += pOutput.name().size() + 1;
    596 
    597   // emit hash table
    598   // FIXME: this verion only emit SVR4 hash section.
    599   //        Please add GNU new hash section
    600 
    601   // both 32 and 64 bits hash table use 32-bit entry
    602   // set up hash_region
    603   uint32_t* word_array = (uint32_t*)hash_region->start();
    604   uint32_t& nbucket = word_array[0];
    605   uint32_t& nchain  = word_array[1];
    606 
    607   nbucket = getHashBucketCount(symtabIdx, false);
    608   nchain  = symtabIdx;
    609 
    610   uint32_t* bucket = (word_array + 2);
    611   uint32_t* chain  = (bucket + nbucket);
    612 
    613   // initialize bucket
    614   bzero((void*)bucket, nbucket);
    615 
    616   StringHash<ELF> hash_func;
    617 
    618   if (32 == bitclass()) {
    619     for (size_t sym_idx=0; sym_idx < symtabIdx; ++sym_idx) {
    620       llvm::StringRef name(strtab + symtab32[sym_idx].st_name);
    621       size_t bucket_pos = hash_func(name) % nbucket;
    622       chain[sym_idx] = bucket[bucket_pos];
    623       bucket[bucket_pos] = sym_idx;
    624     }
    625   }
    626   else if (64 == bitclass()) {
    627     for (size_t sym_idx=0; sym_idx < symtabIdx; ++sym_idx) {
    628       llvm::StringRef name(strtab + symtab64[sym_idx].st_name);
    629       size_t bucket_pos = hash_func(name) % nbucket;
    630       chain[sym_idx] = bucket[bucket_pos];
    631       bucket[bucket_pos] = sym_idx;
    632     }
    633   }
    634 }
    635 
    636 /// getSectionOrder
    637 unsigned int GNULDBackend::getSectionOrder(const Output& pOutput,
    638                                            const LDSection& pSectHdr) const
    639 {
    640   // NULL section should be the "1st" section
    641   if (LDFileFormat::Null == pSectHdr.kind())
    642     return 0;
    643 
    644   // if the section is not ALLOC, lay it out until the last possible moment
    645   if (0 == (pSectHdr.flag() & llvm::ELF::SHF_ALLOC))
    646     return SHO_UNDEFINED;
    647 
    648   bool is_write = (pSectHdr.flag() & llvm::ELF::SHF_WRITE) != 0;
    649   bool is_exec = (pSectHdr.flag() & llvm::ELF::SHF_EXECINSTR) != 0;
    650   ELFFileFormat* file_format = NULL;
    651   switch (pOutput.type()) {
    652     case Output::DynObj:
    653       file_format = getDynObjFileFormat();
    654       break;
    655     case Output::Exec:
    656       file_format = getExecFileFormat();
    657       break;
    658     case Output::Object:
    659     default:
    660       assert(0 && "Not support yet.\n");
    661       break;
    662   }
    663 
    664   // TODO: need to take care other possible output sections
    665   switch (pSectHdr.kind()) {
    666     case LDFileFormat::Regular:
    667       if (is_exec) {
    668         if (&pSectHdr == &file_format->getInit())
    669           return SHO_INIT;
    670         if (&pSectHdr == &file_format->getFini())
    671           return SHO_FINI;
    672         return SHO_TEXT;
    673       } else if (!is_write) {
    674         return SHO_RO;
    675       } else {
    676         if (pSectHdr.type() == llvm::ELF::SHT_PREINIT_ARRAY ||
    677             pSectHdr.type() == llvm::ELF::SHT_INIT_ARRAY ||
    678             pSectHdr.type() == llvm::ELF::SHT_FINI_ARRAY ||
    679             &pSectHdr == &file_format->getCtors() ||
    680             &pSectHdr == &file_format->getDtors())
    681           return SHO_RELRO;
    682 
    683         return SHO_DATA;
    684       }
    685 
    686     case LDFileFormat::BSS:
    687       return SHO_BSS;
    688 
    689     case LDFileFormat::NamePool:
    690       if (&pSectHdr == &file_format->getDynamic())
    691         return SHO_RELRO;
    692       return SHO_NAMEPOOL;
    693 
    694     case LDFileFormat::Relocation:
    695       if (&pSectHdr == &file_format->getRelPlt() ||
    696           &pSectHdr == &file_format->getRelaPlt())
    697         return SHO_REL_PLT;
    698       return SHO_RELOCATION;
    699 
    700     // get the order from target for target specific sections
    701     case LDFileFormat::Target:
    702       return getTargetSectionOrder(pOutput, pSectHdr);
    703 
    704     // handle .interp
    705     case LDFileFormat::Note:
    706       return SHO_INTERP;
    707 
    708     case LDFileFormat::Exception:
    709       return SHO_EHFRAME;
    710 
    711     case LDFileFormat::MetaData:
    712     case LDFileFormat::Debug:
    713     default:
    714       return SHO_UNDEFINED;
    715   }
    716 }
    717 
    718 /// getSymbolSize
    719 uint64_t GNULDBackend::getSymbolSize(const LDSymbol& pSymbol) const
    720 {
    721   // @ref Google gold linker: symtab.cc: 2780
    722   // undefined and dynamic symbols should have zero size.
    723   if (pSymbol.isDyn() || pSymbol.desc() == ResolveInfo::Undefined)
    724     return 0x0;
    725   return pSymbol.resolveInfo()->size();
    726 }
    727 
    728 /// getSymbolInfo
    729 uint64_t GNULDBackend::getSymbolInfo(const LDSymbol& pSymbol) const
    730 {
    731   // set binding
    732   uint8_t bind = 0x0;
    733   if (pSymbol.resolveInfo()->isLocal())
    734     bind = llvm::ELF::STB_LOCAL;
    735   else if (pSymbol.resolveInfo()->isGlobal())
    736     bind = llvm::ELF::STB_GLOBAL;
    737   else if (pSymbol.resolveInfo()->isWeak())
    738     bind = llvm::ELF::STB_WEAK;
    739   else if (pSymbol.resolveInfo()->isAbsolute()) {
    740     // (Luba) Is a absolute but not global (weak or local) symbol meaningful?
    741     bind = llvm::ELF::STB_GLOBAL;
    742   }
    743 
    744   if (pSymbol.visibility() == llvm::ELF::STV_INTERNAL ||
    745       pSymbol.visibility() == llvm::ELF::STV_HIDDEN)
    746     bind = llvm::ELF::STB_LOCAL;
    747 
    748   return (pSymbol.resolveInfo()->type() | (bind << 4));
    749 }
    750 
    751 /// getSymbolValue - this function is called after layout()
    752 uint64_t GNULDBackend::getSymbolValue(const LDSymbol& pSymbol) const
    753 {
    754   if (pSymbol.isDyn())
    755     return 0x0;
    756 
    757   return pSymbol.value();
    758 }
    759 
    760 /// getSymbolShndx - this function is called after layout()
    761 uint64_t
    762 GNULDBackend::getSymbolShndx(const LDSymbol& pSymbol, const Layout& pLayout) const
    763 {
    764   if (pSymbol.resolveInfo()->isAbsolute())
    765     return llvm::ELF::SHN_ABS;
    766   if (pSymbol.resolveInfo()->isCommon())
    767     return llvm::ELF::SHN_COMMON;
    768   if (pSymbol.resolveInfo()->isUndef() || pSymbol.isDyn())
    769     return llvm::ELF::SHN_UNDEF;
    770 
    771   if (pSymbol.resolveInfo()->isLocal()) {
    772     switch (pSymbol.type()) {
    773       case ResolveInfo::NoType:
    774       case ResolveInfo::File:
    775         return llvm::ELF::SHN_ABS;
    776     }
    777   }
    778 
    779   assert(pSymbol.hasFragRef());
    780   return pLayout.getOutputLDSection(*pSymbol.fragRef()->frag())->index();
    781 }
    782 
    783 /// getSymbolIdx - called by emitRelocation to get the ouput symbol table index
    784 size_t GNULDBackend::getSymbolIdx(LDSymbol* pSymbol) const
    785 {
    786    HashTableType::iterator entry = m_pSymIndexMap->find(pSymbol);
    787    return entry.getEntry()->value();
    788 }
    789 
    790 /// emitProgramHdrs - emit ELF program headers
    791 void GNULDBackend::emitProgramHdrs(Output& pOutput)
    792 {
    793   assert(NULL != pOutput.context());
    794   createProgramHdrs(*pOutput.context());
    795 
    796   if (32 == bitclass())
    797     writeELF32ProgramHdrs(pOutput);
    798   else
    799     writeELF64ProgramHdrs(pOutput);
    800 }
    801 
    802 /// createProgramHdrs - base on output sections to create the program headers
    803 void GNULDBackend::createProgramHdrs(LDContext& pContext)
    804 {
    805   // make PT_PHDR
    806   m_ELFSegmentTable.produce(llvm::ELF::PT_PHDR);
    807 
    808   // make PT_INTERP
    809   LDSection* interp = pContext.getSection(".interp");
    810   if (NULL != interp) {
    811     ELFSegment* interp_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_INTERP);
    812     interp_seg->addSection(interp);
    813     interp_seg->setAlign(bitclass() / 8);
    814   }
    815 
    816   uint32_t cur_seg_flag, prev_seg_flag = getSegmentFlag(0);
    817   uint64_t padding = 0;
    818   ELFSegment* load_seg = NULL;
    819   // make possible PT_LOAD segments
    820   LDContext::sect_iterator sect, sect_end = pContext.sectEnd();
    821   for (sect = pContext.sectBegin(); sect != sect_end; ++sect) {
    822     if (0 == ((*sect)->flag() & llvm::ELF::SHF_ALLOC) &&
    823         LDFileFormat::Null != (*sect)->kind())
    824       continue;
    825 
    826     // FIXME: Now only separate writable and non-writable PT_LOAD
    827     cur_seg_flag = getSegmentFlag((*sect)->flag());
    828     if ((prev_seg_flag & llvm::ELF::PF_W) ^ (cur_seg_flag & llvm::ELF::PF_W) ||
    829          LDFileFormat::Null == (*sect)->kind()) {
    830       // create new PT_LOAD segment
    831       load_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_LOAD);
    832       load_seg->setAlign(pagesize());
    833 
    834       // check if this segment needs padding
    835       padding = 0;
    836       if (((*sect)->offset() & (load_seg->align() - 1)) != 0)
    837         padding = load_seg->align();
    838     }
    839 
    840     assert(NULL != load_seg);
    841     load_seg->addSection(*sect);
    842     load_seg->updateFlag(cur_seg_flag);
    843 
    844     // FIXME: set section's vma
    845     // need to handle start vma for user-defined one or for executable.
    846     (*sect)->setAddr((*sect)->offset() + padding);
    847 
    848     prev_seg_flag = cur_seg_flag;
    849   }
    850 
    851   // make PT_DYNAMIC
    852   LDSection* dynamic = pContext.getSection(".dynamic");
    853   if (NULL != dynamic) {
    854     ELFSegment* dyn_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_DYNAMIC);
    855     dyn_seg->setFlag(llvm::ELF::PF_R | llvm::ELF::PF_W);
    856     dyn_seg->addSection(dynamic);
    857     dyn_seg->setAlign(bitclass() / 8);
    858   }
    859 
    860   // update segment info
    861   uint64_t file_size = 0;
    862   ELFSegmentFactory::iterator seg, seg_end = m_ELFSegmentTable.end();
    863   for (seg = m_ELFSegmentTable.begin(); seg != seg_end; ++seg) {
    864     ELFSegment& segment = *seg;
    865 
    866     // update PT_PHDR
    867     if (llvm::ELF::PT_PHDR == segment.type()) {
    868       uint64_t offset, phdr_size;
    869       if (32 == bitclass()) {
    870         offset = sizeof(llvm::ELF::Elf32_Ehdr);
    871         phdr_size = sizeof(llvm::ELF::Elf32_Phdr);
    872       }
    873       else {
    874         offset = sizeof(llvm::ELF::Elf64_Ehdr);
    875         phdr_size = sizeof(llvm::ELF::Elf64_Phdr);
    876       }
    877       segment.setOffset(offset);
    878       segment.setVaddr(offset);
    879       segment.setPaddr(segment.vaddr());
    880       segment.setFilesz(numOfSegments() * phdr_size);
    881       segment.setMemsz(numOfSegments() * phdr_size);
    882       segment.setAlign(bitclass() / 8);
    883       continue;
    884     }
    885 
    886     assert(NULL != segment.getFirstSection());
    887     segment.setOffset(segment.getFirstSection()->offset());
    888     segment.setVaddr(segment.getFirstSection()->addr());
    889     segment.setPaddr(segment.vaddr());
    890 
    891     const LDSection* last_sect = segment.getLastSection();
    892     assert(NULL != last_sect);
    893     file_size = last_sect->offset() - segment.offset();
    894     if (LDFileFormat::BSS != last_sect->kind())
    895       file_size += last_sect->size();
    896     segment.setFilesz(file_size);
    897 
    898     segment.setMemsz(last_sect->addr() - segment.vaddr() + last_sect->size());
    899   }
    900 }
    901 
    902 /// writeELF32ProgramHdrs - write out the ELF32 program headers
    903 void GNULDBackend::writeELF32ProgramHdrs(Output& pOutput)
    904 {
    905   assert(pOutput.hasMemArea());
    906 
    907   uint64_t start_offset, phdr_size;
    908 
    909   start_offset = sizeof(llvm::ELF::Elf32_Ehdr);
    910   phdr_size = sizeof(llvm::ELF::Elf32_Phdr);
    911   // Program header must start directly after ELF header
    912   MemoryRegion *region = pOutput.memArea()->request(start_offset,
    913                                                     numOfSegments()*phdr_size);
    914 
    915   llvm::ELF::Elf32_Phdr* phdr = (llvm::ELF::Elf32_Phdr*)region->start();
    916 
    917   size_t index = 0;
    918   ELFSegmentFactory::iterator seg, segEnd = m_ELFSegmentTable.end();
    919   for (seg = m_ELFSegmentTable.begin(); seg != segEnd; ++seg, ++index) {
    920     phdr[index].p_type   = (*seg).type();
    921     phdr[index].p_flags  = (*seg).flag();
    922     phdr[index].p_offset = (*seg).offset();
    923     phdr[index].p_vaddr  = (*seg).vaddr();
    924     phdr[index].p_paddr  = (*seg).paddr();
    925     phdr[index].p_filesz = (*seg).filesz();
    926     phdr[index].p_memsz  = (*seg).memsz();
    927     phdr[index].p_align  = (*seg).align();
    928   }
    929 }
    930 
    931 /// writeELF64ProgramHdrs - write out the ELF64 program headers
    932 void GNULDBackend::writeELF64ProgramHdrs(Output& pOutput)
    933 {
    934   assert(pOutput.hasMemArea());
    935 
    936   uint64_t start_offset, phdr_size;
    937 
    938   start_offset = sizeof(llvm::ELF::Elf64_Ehdr);
    939   phdr_size = sizeof(llvm::ELF::Elf64_Phdr);
    940   // Program header must start directly after ELF header
    941   MemoryRegion *region = pOutput.memArea()->request(start_offset,
    942                                                     numOfSegments() *phdr_size);
    943   llvm::ELF::Elf64_Phdr* phdr = (llvm::ELF::Elf64_Phdr*)region->start();
    944 
    945   size_t index = 0;
    946   ELFSegmentFactory::iterator seg, segEnd = m_ELFSegmentTable.end();
    947   for (seg = m_ELFSegmentTable.begin(); seg != segEnd; ++seg, ++index) {
    948     phdr[index].p_type   = (*seg).type();
    949     phdr[index].p_flags  = (*seg).flag();
    950     phdr[index].p_offset = (*seg).offset();
    951     phdr[index].p_vaddr  = (*seg).vaddr();
    952     phdr[index].p_paddr  = (*seg).paddr();
    953     phdr[index].p_filesz = (*seg).filesz();
    954     phdr[index].p_memsz  = (*seg).memsz();
    955     phdr[index].p_align  = (*seg).align();
    956   }
    957 }
    958 
    959 /// preLayout - Backend can do any needed modification before layout
    960 void GNULDBackend::preLayout(const Output& pOutput,
    961                              const MCLDInfo& pLDInfo,
    962                              MCLinker& pLinker)
    963 {
    964   // prelayout target first
    965   doPreLayout(pOutput, pLDInfo, pLinker);
    966 }
    967 
    968 /// postLayout -Backend can do any needed modification after layout
    969 void GNULDBackend::postLayout(const Output& pOutput,
    970                               const MCLDInfo& pInfo,
    971                               MCLinker& pLinker)
    972 {
    973   // post layout target first
    974   doPostLayout(pOutput, pInfo, pLinker);
    975 }
    976 
    977 /// getHashBucketCount - calculate hash bucket count.
    978 /// @ref Google gold linker, dynobj.cc:791
    979 unsigned GNULDBackend::getHashBucketCount(unsigned pNumOfSymbols,
    980                                           bool pIsGNUStyle)
    981 {
    982   // @ref Google gold, dynobj.cc:loc 791
    983   static const unsigned int buckets[] =
    984   {
    985     1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
    986     16411, 32771, 65537, 131101, 262147
    987   };
    988   const unsigned buckets_count = sizeof buckets / sizeof buckets[0];
    989 
    990   unsigned int result = 1;
    991   for (unsigned i = 0; i < buckets_count; ++i) {
    992     if (pNumOfSymbols < buckets[i])
    993       break;
    994     result = buckets[i];
    995   }
    996 
    997   if (pIsGNUStyle && result < 2)
    998     result = 2;
    999 
   1000   return result;
   1001 }
   1002 
   1003 /// isDynamicSymbol
   1004 /// @ref Google gold linker: symtab.cc:311
   1005 bool GNULDBackend::isDynamicSymbol(const LDSymbol& pSymbol,
   1006                                    const Output& pOutput)
   1007 {
   1008   // If a local symbol is in the LDContext's symbol table, it's a real local
   1009   // symbol. We should not add it
   1010   if (pSymbol.binding() == ResolveInfo::Local)
   1011     return false;
   1012 
   1013   // If we are building shared object, and the visibility is external, we
   1014   // need to add it.
   1015   if (Output::DynObj == pOutput.type())
   1016     if (pSymbol.resolveInfo()->visibility() == ResolveInfo::Default ||
   1017         pSymbol.resolveInfo()->visibility() == ResolveInfo::Protected)
   1018       return true;
   1019 
   1020   return false;
   1021 }
   1022