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 <mcld/Target/GNULDBackend.h>
     10 
     11 #include <string>
     12 #include <cstring>
     13 #include <cassert>
     14 #include <vector>
     15 #include <algorithm>
     16 #include <map>
     17 
     18 #include <mcld/Module.h>
     19 #include <mcld/LinkerConfig.h>
     20 #include <mcld/IRBuilder.h>
     21 #include <mcld/InputTree.h>
     22 #include <mcld/Config/Config.h>
     23 #include <mcld/ADT/SizeTraits.h>
     24 #include <mcld/LD/LDSymbol.h>
     25 #include <mcld/LD/LDContext.h>
     26 #include <mcld/Fragment/FillFragment.h>
     27 #include <mcld/LD/EhFrame.h>
     28 #include <mcld/LD/EhFrameHdr.h>
     29 #include <mcld/LD/RelocData.h>
     30 #include <mcld/LD/RelocationFactory.h>
     31 #include <mcld/MC/Attribute.h>
     32 #include <mcld/Support/MemoryArea.h>
     33 #include <mcld/Support/MemoryRegion.h>
     34 #include <mcld/Support/MsgHandling.h>
     35 #include <mcld/Support/MemoryAreaFactory.h>
     36 #include <mcld/LD/BranchIslandFactory.h>
     37 #include <mcld/LD/StubFactory.h>
     38 #include <mcld/Object/ObjectBuilder.h>
     39 
     40 namespace {
     41 
     42 //===--------------------------------------------------------------------===//
     43 // non-member functions
     44 //===----------------------------------------------------------------------===//
     45 static const std::string simple_c_identifier_allowed_chars =
     46   "0123456789"
     47   "ABCDEFGHIJKLMNOPWRSTUVWXYZ"
     48   "abcdefghijklmnopqrstuvwxyz"
     49   "_";
     50 
     51 /// isCIdentifier - return if the pName is a valid C identifier
     52 static bool isCIdentifier(const std::string& pName)
     53 {
     54   return (pName.find_first_not_of(simple_c_identifier_allowed_chars)
     55               == std::string::npos);
     56 }
     57 
     58 } // anonymous namespace
     59 
     60 using namespace mcld;
     61 
     62 //===----------------------------------------------------------------------===//
     63 // GNULDBackend
     64 //===----------------------------------------------------------------------===//
     65 GNULDBackend::GNULDBackend(const LinkerConfig& pConfig, GNUInfo* pInfo)
     66   : TargetLDBackend(pConfig),
     67     m_pObjectReader(NULL),
     68     m_pDynObjFileFormat(NULL),
     69     m_pExecFileFormat(NULL),
     70     m_pObjectFileFormat(NULL),
     71     m_pInfo(pInfo),
     72     m_ELFSegmentTable(9), // magic number
     73     m_pBRIslandFactory(NULL),
     74     m_pStubFactory(NULL),
     75     m_pEhFrameHdr(NULL),
     76     m_bHasTextRel(false),
     77     m_bHasStaticTLS(false),
     78     f_pPreInitArrayStart(NULL),
     79     f_pPreInitArrayEnd(NULL),
     80     f_pInitArrayStart(NULL),
     81     f_pInitArrayEnd(NULL),
     82     f_pFiniArrayStart(NULL),
     83     f_pFiniArrayEnd(NULL),
     84     f_pStack(NULL),
     85     f_pDynamic(NULL),
     86     f_pTDATA(NULL),
     87     f_pTBSS(NULL),
     88     f_pExecutableStart(NULL),
     89     f_pEText(NULL),
     90     f_p_EText(NULL),
     91     f_p__EText(NULL),
     92     f_pEData(NULL),
     93     f_p_EData(NULL),
     94     f_pBSSStart(NULL),
     95     f_pEnd(NULL),
     96     f_p_End(NULL) {
     97   m_pSymIndexMap = new HashTableType(1024);
     98 }
     99 
    100 GNULDBackend::~GNULDBackend()
    101 {
    102   delete m_pInfo;
    103   delete m_pDynObjFileFormat;
    104   delete m_pExecFileFormat;
    105   delete m_pObjectFileFormat;
    106   delete m_pSymIndexMap;
    107   delete m_pEhFrameHdr;
    108   delete m_pBRIslandFactory;
    109   delete m_pStubFactory;
    110 }
    111 
    112 size_t GNULDBackend::sectionStartOffset() const
    113 {
    114   if (LinkerConfig::Binary == config().codeGenType())
    115     return 0x0;
    116 
    117   switch (config().targets().bitclass()) {
    118     case 32u:
    119       return sizeof(llvm::ELF::Elf32_Ehdr) +
    120              numOfSegments() * sizeof(llvm::ELF::Elf32_Phdr);
    121     case 64u:
    122       return sizeof(llvm::ELF::Elf64_Ehdr) +
    123              numOfSegments() * sizeof(llvm::ELF::Elf64_Phdr);
    124     default:
    125       fatal(diag::unsupported_bitclass) << config().targets().triple().str()
    126                                         << config().targets().bitclass();
    127       return 0;
    128   }
    129 }
    130 
    131 uint64_t GNULDBackend::getSegmentStartAddr(const LinkerScript& pScript) const
    132 {
    133   LinkerScript::AddressMap::const_iterator mapping =
    134     pScript.addressMap().find(".text");
    135   if (pScript.addressMap().end() != mapping)
    136     return mapping.getEntry()->value();
    137   else if (config().isCodeIndep())
    138     return 0x0;
    139   else
    140     return m_pInfo->defaultTextSegmentAddr();
    141 }
    142 
    143 GNUArchiveReader*
    144 GNULDBackend::createArchiveReader(Module& pModule)
    145 {
    146   assert(NULL != m_pObjectReader);
    147   return new GNUArchiveReader(pModule, *m_pObjectReader);
    148 }
    149 
    150 ELFObjectReader* GNULDBackend::createObjectReader(IRBuilder& pBuilder)
    151 {
    152   m_pObjectReader = new ELFObjectReader(*this, pBuilder, config());
    153   return m_pObjectReader;
    154 }
    155 
    156 ELFDynObjReader* GNULDBackend::createDynObjReader(IRBuilder& pBuilder)
    157 {
    158   return new ELFDynObjReader(*this, pBuilder, config());
    159 }
    160 
    161 ELFBinaryReader* GNULDBackend::createBinaryReader(IRBuilder& pBuilder)
    162 {
    163   return new ELFBinaryReader(*this, pBuilder, config());
    164 }
    165 
    166 ELFObjectWriter* GNULDBackend::createWriter()
    167 {
    168   return new ELFObjectWriter(*this, config());
    169 }
    170 
    171 bool GNULDBackend::initStdSections(ObjectBuilder& pBuilder)
    172 {
    173   switch (config().codeGenType()) {
    174     case LinkerConfig::DynObj: {
    175       if (NULL == m_pDynObjFileFormat)
    176         m_pDynObjFileFormat = new ELFDynObjFileFormat();
    177       m_pDynObjFileFormat->initStdSections(pBuilder,
    178                                            config().targets().bitclass());
    179       return true;
    180     }
    181     case LinkerConfig::Exec:
    182     case LinkerConfig::Binary: {
    183       if (NULL == m_pExecFileFormat)
    184         m_pExecFileFormat = new ELFExecFileFormat();
    185       m_pExecFileFormat->initStdSections(pBuilder,
    186                                          config().targets().bitclass());
    187       return true;
    188     }
    189     case LinkerConfig::Object: {
    190       if (NULL == m_pObjectFileFormat)
    191         m_pObjectFileFormat = new ELFObjectFileFormat();
    192       m_pObjectFileFormat->initStdSections(pBuilder,
    193                                            config().targets().bitclass());
    194       return true;
    195     }
    196     default:
    197       fatal(diag::unrecognized_output_file) << config().codeGenType();
    198       return false;
    199   }
    200 }
    201 
    202 /// initStandardSymbols - define and initialize standard symbols.
    203 /// This function is called after section merging but before read relocations.
    204 bool GNULDBackend::initStandardSymbols(IRBuilder& pBuilder,
    205                                        Module& pModule)
    206 {
    207   if (LinkerConfig::Object == config().codeGenType())
    208     return true;
    209 
    210   // GNU extension: define __start and __stop symbols for the sections whose
    211   // name can be presented as C symbol
    212   // ref: GNU gold, Layout::define_section_symbols
    213   Module::iterator iter, iterEnd = pModule.end();
    214   for (iter = pModule.begin(); iter != iterEnd; ++iter) {
    215     LDSection* section = *iter;
    216 
    217     switch (section->kind()) {
    218       case LDFileFormat::Relocation:
    219         continue;
    220       case LDFileFormat::EhFrame:
    221         if (!section->hasEhFrame())
    222           continue;
    223         break;
    224       default:
    225         if (!section->hasSectionData())
    226           continue;
    227         break;
    228     } // end of switch
    229 
    230     if (isCIdentifier(section->name())) {
    231       std::string start_name = "__start_" + section->name();
    232       FragmentRef* start_fragref = FragmentRef::Create(
    233                                        section->getSectionData()->front(), 0x0);
    234       pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    235                                                     start_name,
    236                                                     ResolveInfo::NoType,
    237                                                     ResolveInfo::Define,
    238                                                     ResolveInfo::Global,
    239                                                     0x0, // size
    240                                                     0x0, // value
    241                                                     start_fragref, // FragRef
    242                                                     ResolveInfo::Default);
    243 
    244       std::string stop_name = "__stop_" + section->name();
    245       FragmentRef* stop_fragref = FragmentRef::Create(
    246                            section->getSectionData()->front(), section->size());
    247       pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    248                                                     stop_name,
    249                                                     ResolveInfo::NoType,
    250                                                     ResolveInfo::Define,
    251                                                     ResolveInfo::Global,
    252                                                     0x0, // size
    253                                                     0x0, // value
    254                                                     stop_fragref, // FragRef
    255                                                     ResolveInfo::Default);
    256     }
    257   }
    258 
    259   ELFFileFormat* file_format = getOutputFormat();
    260 
    261   // -----  section symbols  ----- //
    262   // .preinit_array
    263   FragmentRef* preinit_array = NULL;
    264   if (file_format->hasPreInitArray()) {
    265     preinit_array = FragmentRef::Create(
    266                    file_format->getPreInitArray().getSectionData()->front(),
    267                    0x0);
    268   }
    269   else {
    270     preinit_array = FragmentRef::Null();
    271   }
    272   f_pPreInitArrayStart =
    273      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    274                                              "__preinit_array_start",
    275                                              ResolveInfo::NoType,
    276                                              ResolveInfo::Define,
    277                                              ResolveInfo::Global,
    278                                              0x0, // size
    279                                              0x0, // value
    280                                              preinit_array, // FragRef
    281                                              ResolveInfo::Hidden);
    282   f_pPreInitArrayEnd =
    283      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    284                                              "__preinit_array_end",
    285                                              ResolveInfo::NoType,
    286                                              ResolveInfo::Define,
    287                                              ResolveInfo::Global,
    288                                              0x0, // size
    289                                              0x0, // value
    290                                              FragmentRef::Null(), // FragRef
    291                                              ResolveInfo::Hidden);
    292 
    293   // .init_array
    294   FragmentRef* init_array = NULL;
    295   if (file_format->hasInitArray()) {
    296     init_array = FragmentRef::Create(
    297                       file_format->getInitArray().getSectionData()->front(),
    298                       0x0);
    299   }
    300   else {
    301     init_array = FragmentRef::Null();
    302   }
    303 
    304   f_pInitArrayStart =
    305      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    306                                              "__init_array_start",
    307                                              ResolveInfo::NoType,
    308                                              ResolveInfo::Define,
    309                                              ResolveInfo::Global,
    310                                              0x0, // size
    311                                              0x0, // value
    312                                              init_array, // FragRef
    313                                              ResolveInfo::Hidden);
    314   f_pInitArrayEnd =
    315      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    316                                              "__init_array_end",
    317                                              ResolveInfo::NoType,
    318                                              ResolveInfo::Define,
    319                                              ResolveInfo::Global,
    320                                              0x0, // size
    321                                              0x0, // value
    322                                              init_array, // FragRef
    323                                              ResolveInfo::Hidden);
    324 
    325   // .fini_array
    326   FragmentRef* fini_array = NULL;
    327   if (file_format->hasFiniArray()) {
    328     fini_array = FragmentRef::Create(
    329                      file_format->getFiniArray().getSectionData()->front(),
    330                      0x0);
    331   }
    332   else {
    333     fini_array = FragmentRef::Null();
    334   }
    335 
    336   f_pFiniArrayStart =
    337      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    338                                              "__fini_array_start",
    339                                              ResolveInfo::NoType,
    340                                              ResolveInfo::Define,
    341                                              ResolveInfo::Global,
    342                                              0x0, // size
    343                                              0x0, // value
    344                                              fini_array, // FragRef
    345                                              ResolveInfo::Hidden);
    346   f_pFiniArrayEnd =
    347      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    348                                              "__fini_array_end",
    349                                              ResolveInfo::NoType,
    350                                              ResolveInfo::Define,
    351                                              ResolveInfo::Global,
    352                                              0x0, // size
    353                                              0x0, // value
    354                                              fini_array, // FragRef
    355                                              ResolveInfo::Hidden);
    356 
    357   // .stack
    358   FragmentRef* stack = NULL;
    359   if (file_format->hasStack()) {
    360     stack = FragmentRef::Create(
    361                           file_format->getStack().getSectionData()->front(),
    362                           0x0);
    363   }
    364   else {
    365     stack = FragmentRef::Null();
    366   }
    367 
    368   f_pStack =
    369      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    370                                              "__stack",
    371                                              ResolveInfo::NoType,
    372                                              ResolveInfo::Define,
    373                                              ResolveInfo::Global,
    374                                              0x0, // size
    375                                              0x0, // value
    376                                              stack, // FragRef
    377                                              ResolveInfo::Hidden);
    378 
    379   // _DYNAMIC
    380   // TODO: add SectionData for .dynamic section, and then we can get the correct
    381   // symbol section index for _DYNAMIC. Now it will be ABS.
    382   f_pDynamic =
    383      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    384                                                    "_DYNAMIC",
    385                                                    ResolveInfo::Object,
    386                                                    ResolveInfo::Define,
    387                                                    ResolveInfo::Local,
    388                                                    0x0, // size
    389                                                    0x0, // value
    390                                                    FragmentRef::Null(), // FragRef
    391                                                    ResolveInfo::Hidden);
    392 
    393   // -----  segment symbols  ----- //
    394   f_pExecutableStart =
    395      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    396                                              "__executable_start",
    397                                              ResolveInfo::NoType,
    398                                              ResolveInfo::Define,
    399                                              ResolveInfo::Absolute,
    400                                              0x0, // size
    401                                              0x0, // value
    402                                              FragmentRef::Null(), // FragRef
    403                                              ResolveInfo::Default);
    404   f_pEText =
    405      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    406                                              "etext",
    407                                              ResolveInfo::NoType,
    408                                              ResolveInfo::Define,
    409                                              ResolveInfo::Absolute,
    410                                              0x0, // size
    411                                              0x0, // value
    412                                              FragmentRef::Null(), // FragRef
    413                                              ResolveInfo::Default);
    414   f_p_EText =
    415      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    416                                              "_etext",
    417                                              ResolveInfo::NoType,
    418                                              ResolveInfo::Define,
    419                                              ResolveInfo::Absolute,
    420                                              0x0, // size
    421                                              0x0, // value
    422                                              FragmentRef::Null(), // FragRef
    423                                              ResolveInfo::Default);
    424   f_p__EText =
    425      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    426                                              "__etext",
    427                                              ResolveInfo::NoType,
    428                                              ResolveInfo::Define,
    429                                              ResolveInfo::Absolute,
    430                                              0x0, // size
    431                                              0x0, // value
    432                                              FragmentRef::Null(), // FragRef
    433                                              ResolveInfo::Default);
    434   f_pEData =
    435      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    436                                              "edata",
    437                                              ResolveInfo::NoType,
    438                                              ResolveInfo::Define,
    439                                              ResolveInfo::Absolute,
    440                                              0x0, // size
    441                                              0x0, // value
    442                                              FragmentRef::Null(), // FragRef
    443                                              ResolveInfo::Default);
    444 
    445   f_pEnd =
    446      pBuilder.AddSymbol<IRBuilder::AsReferred, IRBuilder::Resolve>(
    447                                              "end",
    448                                              ResolveInfo::NoType,
    449                                              ResolveInfo::Define,
    450                                              ResolveInfo::Absolute,
    451                                              0x0, // size
    452                                              0x0, // value
    453                                              FragmentRef::Null(), // FragRef
    454                                              ResolveInfo::Default);
    455 
    456   // _edata is defined forcefully.
    457   // @ref Google gold linker: defstd.cc: 186
    458   f_p_EData =
    459      pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
    460                                              "_edata",
    461                                              ResolveInfo::NoType,
    462                                              ResolveInfo::Define,
    463                                              ResolveInfo::Absolute,
    464                                              0x0, // size
    465                                              0x0, // value
    466                                              FragmentRef::Null(), // FragRef
    467                                              ResolveInfo::Default);
    468 
    469   // __bss_start is defined forcefully.
    470   // @ref Google gold linker: defstd.cc: 214
    471   f_pBSSStart =
    472      pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
    473                                              "__bss_start",
    474                                              ResolveInfo::NoType,
    475                                              ResolveInfo::Define,
    476                                              ResolveInfo::Absolute,
    477                                              0x0, // size
    478                                              0x0, // value
    479                                              FragmentRef::Null(), // FragRef
    480                                              ResolveInfo::Default);
    481 
    482   // _end is defined forcefully.
    483   // @ref Google gold linker: defstd.cc: 228
    484   f_p_End =
    485      pBuilder.AddSymbol<IRBuilder::Force, IRBuilder::Resolve>(
    486                                              "_end",
    487                                              ResolveInfo::NoType,
    488                                              ResolveInfo::Define,
    489                                              ResolveInfo::Absolute,
    490                                              0x0, // size
    491                                              0x0, // value
    492                                              FragmentRef::Null(), // FragRef
    493                                              ResolveInfo::Default);
    494 
    495   return true;
    496 }
    497 
    498 bool GNULDBackend::finalizeStandardSymbols()
    499 {
    500   if (LinkerConfig::Object == config().codeGenType())
    501     return true;
    502 
    503   ELFFileFormat* file_format = getOutputFormat();
    504 
    505   // -----  section symbols  ----- //
    506   if (NULL != f_pPreInitArrayStart) {
    507     if (!f_pPreInitArrayStart->hasFragRef()) {
    508       f_pPreInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
    509       f_pPreInitArrayStart->setValue(0x0);
    510     }
    511   }
    512 
    513   if (NULL != f_pPreInitArrayEnd) {
    514     if (f_pPreInitArrayEnd->hasFragRef()) {
    515       f_pPreInitArrayEnd->setValue(f_pPreInitArrayEnd->value() +
    516                                    file_format->getPreInitArray().size());
    517     }
    518     else {
    519       f_pPreInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
    520       f_pPreInitArrayEnd->setValue(0x0);
    521     }
    522   }
    523 
    524   if (NULL != f_pInitArrayStart) {
    525     if (!f_pInitArrayStart->hasFragRef()) {
    526       f_pInitArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
    527       f_pInitArrayStart->setValue(0x0);
    528     }
    529   }
    530 
    531   if (NULL != f_pInitArrayEnd) {
    532     if (f_pInitArrayEnd->hasFragRef()) {
    533       f_pInitArrayEnd->setValue(f_pInitArrayEnd->value() +
    534                                 file_format->getInitArray().size());
    535     }
    536     else {
    537       f_pInitArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
    538       f_pInitArrayEnd->setValue(0x0);
    539     }
    540   }
    541 
    542   if (NULL != f_pFiniArrayStart) {
    543     if (!f_pFiniArrayStart->hasFragRef()) {
    544       f_pFiniArrayStart->resolveInfo()->setBinding(ResolveInfo::Absolute);
    545       f_pFiniArrayStart->setValue(0x0);
    546     }
    547   }
    548 
    549   if (NULL != f_pFiniArrayEnd) {
    550     if (f_pFiniArrayEnd->hasFragRef()) {
    551       f_pFiniArrayEnd->setValue(f_pFiniArrayEnd->value() +
    552                                 file_format->getFiniArray().size());
    553     }
    554     else {
    555       f_pFiniArrayEnd->resolveInfo()->setBinding(ResolveInfo::Absolute);
    556       f_pFiniArrayEnd->setValue(0x0);
    557     }
    558   }
    559 
    560   if (NULL != f_pStack) {
    561     if (!f_pStack->hasFragRef()) {
    562       f_pStack->resolveInfo()->setBinding(ResolveInfo::Absolute);
    563       f_pStack->setValue(0x0);
    564     }
    565   }
    566 
    567   if (NULL != f_pDynamic) {
    568     f_pDynamic->resolveInfo()->setBinding(ResolveInfo::Local);
    569     f_pDynamic->setValue(file_format->getDynamic().addr());
    570     f_pDynamic->setSize(file_format->getDynamic().size());
    571   }
    572 
    573   // -----  segment symbols  ----- //
    574   if (NULL != f_pExecutableStart) {
    575     ELFSegment* exec_start = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD, 0x0, 0x0);
    576     if (NULL != exec_start) {
    577       if (ResolveInfo::ThreadLocal != f_pExecutableStart->type()) {
    578         f_pExecutableStart->setValue(f_pExecutableStart->value() +
    579                                      exec_start->vaddr());
    580       }
    581     }
    582     else
    583       f_pExecutableStart->setValue(0x0);
    584   }
    585 
    586   if (NULL != f_pEText || NULL != f_p_EText || NULL !=f_p__EText) {
    587     ELFSegment* etext = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD,
    588                                                llvm::ELF::PF_X,
    589                                                llvm::ELF::PF_W);
    590     if (NULL != etext) {
    591       if (NULL != f_pEText && ResolveInfo::ThreadLocal != f_pEText->type()) {
    592         f_pEText->setValue(f_pEText->value() +
    593                            etext->vaddr() +
    594                            etext->memsz());
    595       }
    596       if (NULL != f_p_EText && ResolveInfo::ThreadLocal != f_p_EText->type()) {
    597         f_p_EText->setValue(f_p_EText->value() +
    598                             etext->vaddr() +
    599                             etext->memsz());
    600       }
    601       if (NULL != f_p__EText && ResolveInfo::ThreadLocal != f_p__EText->type()) {
    602         f_p__EText->setValue(f_p__EText->value() +
    603                             etext->vaddr() +
    604                             etext->memsz());
    605       }
    606     }
    607     else {
    608       if (NULL != f_pEText)
    609         f_pEText->setValue(0x0);
    610       if (NULL != f_p_EText)
    611         f_p_EText->setValue(0x0);
    612       if (NULL != f_p__EText)
    613         f_p__EText->setValue(0x0);
    614     }
    615   }
    616 
    617   if (NULL != f_pEData || NULL != f_p_EData || NULL != f_pBSSStart ||
    618       NULL != f_pEnd || NULL != f_p_End) {
    619     ELFSegment* edata = m_ELFSegmentTable.find(llvm::ELF::PT_LOAD,
    620                                                llvm::ELF::PF_W,
    621                                                0x0);
    622     if (NULL != edata) {
    623       if (NULL != f_pEData && ResolveInfo::ThreadLocal != f_pEData->type()) {
    624         f_pEData->setValue(f_pEData->value() +
    625                             edata->vaddr() +
    626                             edata->filesz());
    627       }
    628       if (NULL != f_p_EData && ResolveInfo::ThreadLocal != f_p_EData->type()) {
    629         f_p_EData->setValue(f_p_EData->value() +
    630                             edata->vaddr() +
    631                             edata->filesz());
    632       }
    633       if (NULL != f_pBSSStart && ResolveInfo::ThreadLocal != f_pBSSStart->type()) {
    634         f_pBSSStart->setValue(f_pBSSStart->value() +
    635                               edata->vaddr() +
    636                               edata->filesz());
    637       }
    638 
    639       if (NULL != f_pEnd && ResolveInfo::ThreadLocal != f_pEnd->type()) {
    640         f_pEnd->setValue(f_pEnd->value() +
    641                          edata->vaddr() +
    642                          edata->memsz());
    643       }
    644       if (NULL != f_p_End && ResolveInfo::ThreadLocal != f_p_End->type()) {
    645         f_p_End->setValue(f_p_End->value() +
    646                           edata->vaddr() +
    647                           edata->memsz());
    648       }
    649     }
    650     else {
    651       if (NULL != f_pEData)
    652         f_pEData->setValue(0x0);
    653       if (NULL != f_p_EData)
    654         f_p_EData->setValue(0x0);
    655       if (NULL != f_pBSSStart)
    656         f_pBSSStart->setValue(0x0);
    657 
    658       if (NULL != f_pEnd)
    659         f_pEnd->setValue(0x0);
    660       if (NULL != f_p_End)
    661         f_p_End->setValue(0x0);
    662     }
    663   }
    664 
    665   return true;
    666 }
    667 
    668 bool GNULDBackend::finalizeTLSSymbol(LDSymbol& pSymbol)
    669 {
    670   // ignore if symbol has no fragRef
    671   if (!pSymbol.hasFragRef())
    672     return true;
    673 
    674   // the value of a TLS symbol is the offset to the TLS segment
    675   ELFSegment* tls_seg = m_ELFSegmentTable.find(llvm::ELF::PT_TLS,
    676                                                llvm::ELF::PF_R, 0x0);
    677   uint64_t value = pSymbol.fragRef()->getOutputOffset();
    678   uint64_t addr  = pSymbol.fragRef()->frag()->getParent()->getSection().addr();
    679   pSymbol.setValue(value + addr - tls_seg->vaddr());
    680   return true;
    681 }
    682 
    683 ELFFileFormat* GNULDBackend::getOutputFormat()
    684 {
    685   switch (config().codeGenType()) {
    686     case LinkerConfig::DynObj:
    687       assert(NULL != m_pDynObjFileFormat);
    688       return m_pDynObjFileFormat;
    689     case LinkerConfig::Exec:
    690     case LinkerConfig::Binary:
    691       assert(NULL != m_pExecFileFormat);
    692       return m_pExecFileFormat;
    693     case LinkerConfig::Object:
    694       assert(NULL != m_pObjectFileFormat);
    695       return m_pObjectFileFormat;
    696     default:
    697       fatal(diag::unrecognized_output_file) << config().codeGenType();
    698       return NULL;
    699   }
    700 }
    701 
    702 const ELFFileFormat* GNULDBackend::getOutputFormat() const
    703 {
    704   switch (config().codeGenType()) {
    705     case LinkerConfig::DynObj:
    706       assert(NULL != m_pDynObjFileFormat);
    707       return m_pDynObjFileFormat;
    708     case LinkerConfig::Exec:
    709     case LinkerConfig::Binary:
    710       assert(NULL != m_pExecFileFormat);
    711       return m_pExecFileFormat;
    712     case LinkerConfig::Object:
    713       assert(NULL != m_pObjectFileFormat);
    714       return m_pObjectFileFormat;
    715     default:
    716       fatal(diag::unrecognized_output_file) << config().codeGenType();
    717       return NULL;
    718   }
    719 }
    720 
    721 /// sizeNamePools - compute the size of regular name pools
    722 /// In ELF executable files, regular name pools are .symtab, .strtab,
    723 /// .dynsym, .dynstr, .hash and .shstrtab.
    724 void GNULDBackend::sizeNamePools(Module& pModule)
    725 {
    726   assert(LinkerConfig::Unset != config().codePosition());
    727 
    728   // number of entries in symbol tables starts from 1 to hold the special entry
    729   // at index 0 (STN_UNDEF). See ELF Spec Book I, p1-21.
    730   size_t symtab = 1;
    731   size_t dynsym = config().isCodeStatic()? 0 : 1;
    732 
    733   // size of string tables starts from 1 to hold the null character in their
    734   // first byte
    735   size_t strtab   = 1;
    736   size_t dynstr   = config().isCodeStatic()? 0 : 1;
    737   size_t shstrtab = 1;
    738   size_t hash     = 0;
    739   size_t gnuhash  = 0;
    740 
    741   // number of local symbol in the .symtab and .dynsym
    742   size_t symtab_local_cnt = 0;
    743   size_t dynsym_local_cnt = 0;
    744 
    745   Module::SymbolTable& symbols = pModule.getSymbolTable();
    746   Module::const_sym_iterator symbol, symEnd;
    747   /// Compute the size of .symtab, .strtab, and symtab_local_cnt
    748   /// @{
    749   symEnd = symbols.end();
    750   for (symbol = symbols.begin(); symbol != symEnd; ++symbol) {
    751     ++symtab;
    752     if (hasEntryInStrTab(**symbol))
    753       strtab += (*symbol)->nameSize() + 1;
    754   }
    755   symtab_local_cnt = 1 + symbols.numOfFiles() + symbols.numOfLocals() +
    756                      symbols.numOfLocalDyns();
    757 
    758   ELFFileFormat* file_format = getOutputFormat();
    759 
    760   switch(config().codeGenType()) {
    761     case LinkerConfig::DynObj: {
    762       // soname
    763       dynstr += pModule.name().size() + 1;
    764     }
    765     /** fall through **/
    766     case LinkerConfig::Exec:
    767     case LinkerConfig::Binary: {
    768       if (!config().isCodeStatic()) {
    769         /// Compute the size of .dynsym, .dynstr, and dynsym_local_cnt
    770         symEnd = symbols.dynamicEnd();
    771         for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) {
    772           ++dynsym;
    773           if (hasEntryInStrTab(**symbol))
    774             dynstr += (*symbol)->nameSize() + 1;
    775         }
    776         dynsym_local_cnt = 1 + symbols.numOfLocalDyns();
    777 
    778         // compute .gnu.hash
    779         if (GeneralOptions::GNU  == config().options().getHashStyle() ||
    780             GeneralOptions::Both == config().options().getHashStyle()) {
    781           // count the number of dynsym to hash
    782           size_t hashed_sym_cnt = 0;
    783           symEnd = symbols.dynamicEnd();
    784           for (symbol = symbols.dynamicBegin(); symbol != symEnd; ++symbol) {
    785             if (DynsymCompare().needGNUHash(**symbol))
    786               ++hashed_sym_cnt;
    787           }
    788           // Special case for empty .dynsym
    789           if (hashed_sym_cnt == 0)
    790             gnuhash = 5 * 4 + config().targets().bitclass() / 8;
    791           else {
    792             size_t nbucket = getHashBucketCount(hashed_sym_cnt, true);
    793             gnuhash = (4 + nbucket + hashed_sym_cnt) * 4;
    794             gnuhash += (1U << getGNUHashMaskbitslog2(hashed_sym_cnt)) / 8;
    795           }
    796         }
    797 
    798         // compute .hash
    799         if (GeneralOptions::SystemV == config().options().getHashStyle() ||
    800             GeneralOptions::Both == config().options().getHashStyle()) {
    801           // Both Elf32_Word and Elf64_Word are 4 bytes
    802           hash = (2 + getHashBucketCount(dynsym, false) + dynsym) *
    803                  sizeof(llvm::ELF::Elf32_Word);
    804         }
    805 
    806         // add DT_NEEDED
    807         Module::const_lib_iterator lib, libEnd = pModule.lib_end();
    808         for (lib = pModule.lib_begin(); lib != libEnd; ++lib) {
    809           if (!(*lib)->attribute()->isAsNeeded() || (*lib)->isNeeded()) {
    810             dynstr += (*lib)->name().size() + 1;
    811             dynamic().reserveNeedEntry();
    812           }
    813         }
    814 
    815         // add DT_RPATH
    816         if (!config().options().getRpathList().empty()) {
    817           dynamic().reserveNeedEntry();
    818           GeneralOptions::const_rpath_iterator rpath,
    819             rpathEnd = config().options().rpath_end();
    820           for (rpath = config().options().rpath_begin();
    821                rpath != rpathEnd; ++rpath)
    822             dynstr += (*rpath).size() + 1;
    823         }
    824 
    825         // set size
    826         if (config().targets().is32Bits()) {
    827           file_format->getDynSymTab().setSize(dynsym *
    828                                               sizeof(llvm::ELF::Elf32_Sym));
    829         } else {
    830           file_format->getDynSymTab().setSize(dynsym *
    831                                               sizeof(llvm::ELF::Elf64_Sym));
    832         }
    833         file_format->getDynStrTab().setSize(dynstr);
    834         file_format->getHashTab().setSize(hash);
    835         file_format->getGNUHashTab().setSize(gnuhash);
    836 
    837         // set .dynsym sh_info to one greater than the symbol table
    838         // index of the last local symbol
    839         file_format->getDynSymTab().setInfo(dynsym_local_cnt);
    840 
    841         // Because some entries in .dynamic section need information of .dynsym,
    842         // .dynstr, .symtab, .strtab and .hash, we can not reserve non-DT_NEEDED
    843         // entries until we get the size of the sections mentioned above
    844         dynamic().reserveEntries(*file_format);
    845         file_format->getDynamic().setSize(dynamic().numOfBytes());
    846       }
    847     }
    848     /* fall through */
    849     case LinkerConfig::Object: {
    850       if (config().targets().is32Bits())
    851         file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf32_Sym));
    852       else
    853         file_format->getSymTab().setSize(symtab*sizeof(llvm::ELF::Elf64_Sym));
    854       file_format->getStrTab().setSize(strtab);
    855 
    856       // set .symtab sh_info to one greater than the symbol table
    857       // index of the last local symbol
    858       file_format->getSymTab().setInfo(symtab_local_cnt);
    859 
    860       // compute the size of .shstrtab section.
    861       Module::const_iterator sect, sectEnd = pModule.end();
    862       for (sect = pModule.begin(); sect != sectEnd; ++sect) {
    863         switch ((*sect)->kind()) {
    864         case LDFileFormat::Null:
    865           break;
    866         // take StackNote directly
    867         case LDFileFormat::StackNote:
    868           shstrtab += ((*sect)->name().size() + 1);
    869           break;
    870         case LDFileFormat::EhFrame:
    871           if (((*sect)->size() != 0) ||
    872               ((*sect)->hasEhFrame() &&
    873                config().codeGenType() == LinkerConfig::Object))
    874             shstrtab += ((*sect)->name().size() + 1);
    875           break;
    876         case LDFileFormat::Relocation:
    877           if (((*sect)->size() != 0) ||
    878               ((*sect)->hasRelocData() &&
    879                config().codeGenType() == LinkerConfig::Object))
    880             shstrtab += ((*sect)->name().size() + 1);
    881           break;
    882         default:
    883           if (((*sect)->size() != 0) ||
    884               ((*sect)->hasSectionData() &&
    885                config().codeGenType() == LinkerConfig::Object))
    886             shstrtab += ((*sect)->name().size() + 1);
    887           break;
    888         } // end of switch
    889       } // end of for
    890       shstrtab += (strlen(".shstrtab") + 1);
    891       file_format->getShStrTab().setSize(shstrtab);
    892       break;
    893     }
    894     default:
    895       fatal(diag::fatal_illegal_codegen_type) << pModule.name();
    896       break;
    897   } // end of switch
    898 }
    899 
    900 /// emitSymbol32 - emit an ELF32 symbol
    901 void GNULDBackend::emitSymbol32(llvm::ELF::Elf32_Sym& pSym,
    902                                 LDSymbol& pSymbol,
    903                                 char* pStrtab,
    904                                 size_t pStrtabsize,
    905                                 size_t pSymtabIdx)
    906 {
    907    // FIXME: check the endian between host and target
    908    // write out symbol
    909    if (hasEntryInStrTab(pSymbol)) {
    910      pSym.st_name  = pStrtabsize;
    911      strcpy((pStrtab + pStrtabsize), pSymbol.name());
    912    }
    913    else {
    914      pSym.st_name  = 0;
    915    }
    916    pSym.st_value = pSymbol.value();
    917    pSym.st_size  = getSymbolSize(pSymbol);
    918    pSym.st_info  = getSymbolInfo(pSymbol);
    919    pSym.st_other = pSymbol.visibility();
    920    pSym.st_shndx = getSymbolShndx(pSymbol);
    921 }
    922 
    923 /// emitSymbol64 - emit an ELF64 symbol
    924 void GNULDBackend::emitSymbol64(llvm::ELF::Elf64_Sym& pSym,
    925                                 LDSymbol& pSymbol,
    926                                 char* pStrtab,
    927                                 size_t pStrtabsize,
    928                                 size_t pSymtabIdx)
    929 {
    930    // FIXME: check the endian between host and target
    931    // write out symbol
    932    if (hasEntryInStrTab(pSymbol)) {
    933      pSym.st_name  = pStrtabsize;
    934      strcpy((pStrtab + pStrtabsize), pSymbol.name());
    935    }
    936    else {
    937      pSym.st_name  = 0;
    938    }
    939    pSym.st_value = pSymbol.value();
    940    pSym.st_size  = getSymbolSize(pSymbol);
    941    pSym.st_info  = getSymbolInfo(pSymbol);
    942    pSym.st_other = pSymbol.visibility();
    943    pSym.st_shndx = getSymbolShndx(pSymbol);
    944 }
    945 
    946 /// emitRegNamePools - emit regular name pools - .symtab, .strtab
    947 ///
    948 /// the size of these tables should be computed before layout
    949 /// layout should computes the start offset of these tables
    950 void GNULDBackend::emitRegNamePools(const Module& pModule,
    951                                     MemoryArea& pOutput)
    952 {
    953   ELFFileFormat* file_format = getOutputFormat();
    954 
    955   LDSection& symtab_sect = file_format->getSymTab();
    956   LDSection& strtab_sect = file_format->getStrTab();
    957 
    958   MemoryRegion* symtab_region = pOutput.request(symtab_sect.offset(),
    959                                                 symtab_sect.size());
    960   MemoryRegion* strtab_region = pOutput.request(strtab_sect.offset(),
    961                                                 strtab_sect.size());
    962 
    963   // set up symtab_region
    964   llvm::ELF::Elf32_Sym* symtab32 = NULL;
    965   llvm::ELF::Elf64_Sym* symtab64 = NULL;
    966   if (config().targets().is32Bits())
    967     symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
    968   else if (config().targets().is64Bits())
    969     symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
    970   else {
    971     fatal(diag::unsupported_bitclass) << config().targets().triple().str()
    972                                       << config().targets().bitclass();
    973   }
    974 
    975   // set up strtab_region
    976   char* strtab = (char*)strtab_region->start();
    977 
    978   // emit the first ELF symbol
    979   if (config().targets().is32Bits())
    980     emitSymbol32(symtab32[0], *LDSymbol::Null(), strtab, 0, 0);
    981   else
    982     emitSymbol64(symtab64[0], *LDSymbol::Null(), strtab, 0, 0);
    983 
    984   bool sym_exist = false;
    985   HashTableType::entry_type* entry = NULL;
    986   if (LinkerConfig::Object == config().codeGenType()) {
    987     entry = m_pSymIndexMap->insert(LDSymbol::Null(), sym_exist);
    988     entry->setValue(0);
    989   }
    990 
    991   size_t symIdx = 1;
    992   size_t strtabsize = 1;
    993 
    994   const Module::SymbolTable& symbols = pModule.getSymbolTable();
    995   Module::const_sym_iterator symbol, symEnd;
    996 
    997   symEnd = symbols.end();
    998   for (symbol = symbols.begin(); symbol != symEnd; ++symbol) {
    999     if (LinkerConfig::Object == config().codeGenType()) {
   1000       entry = m_pSymIndexMap->insert(*symbol, sym_exist);
   1001       entry->setValue(symIdx);
   1002     }
   1003     if (config().targets().is32Bits())
   1004       emitSymbol32(symtab32[symIdx], **symbol, strtab, strtabsize, symIdx);
   1005     else
   1006       emitSymbol64(symtab64[symIdx], **symbol, strtab, strtabsize, symIdx);
   1007     ++symIdx;
   1008     if (hasEntryInStrTab(**symbol))
   1009       strtabsize += (*symbol)->nameSize() + 1;
   1010   }
   1011 }
   1012 
   1013 /// emitDynNamePools - emit dynamic name pools - .dyntab, .dynstr, .hash
   1014 ///
   1015 /// the size of these tables should be computed before layout
   1016 /// layout should computes the start offset of these tables
   1017 void GNULDBackend::emitDynNamePools(Module& pModule, MemoryArea& pOutput)
   1018 {
   1019   ELFFileFormat* file_format = getOutputFormat();
   1020   if (!file_format->hasDynSymTab() ||
   1021       !file_format->hasDynStrTab() ||
   1022       !file_format->hasDynamic())
   1023     return;
   1024 
   1025   bool sym_exist = false;
   1026   HashTableType::entry_type* entry = 0;
   1027 
   1028   LDSection& symtab_sect = file_format->getDynSymTab();
   1029   LDSection& strtab_sect = file_format->getDynStrTab();
   1030   LDSection& dyn_sect    = file_format->getDynamic();
   1031 
   1032   MemoryRegion* symtab_region = pOutput.request(symtab_sect.offset(),
   1033                                                 symtab_sect.size());
   1034   MemoryRegion* strtab_region = pOutput.request(strtab_sect.offset(),
   1035                                                 strtab_sect.size());
   1036   MemoryRegion* dyn_region    = pOutput.request(dyn_sect.offset(),
   1037                                                 dyn_sect.size());
   1038   // set up symtab_region
   1039   llvm::ELF::Elf32_Sym* symtab32 = NULL;
   1040   llvm::ELF::Elf64_Sym* symtab64 = NULL;
   1041   if (config().targets().is32Bits())
   1042     symtab32 = (llvm::ELF::Elf32_Sym*)symtab_region->start();
   1043   else if (config().targets().is64Bits())
   1044     symtab64 = (llvm::ELF::Elf64_Sym*)symtab_region->start();
   1045   else {
   1046     fatal(diag::unsupported_bitclass) << config().targets().triple().str()
   1047                                       << config().targets().bitclass();
   1048   }
   1049 
   1050   // set up strtab_region
   1051   char* strtab = (char*)strtab_region->start();
   1052 
   1053   // emit the first ELF symbol
   1054   if (config().targets().is32Bits())
   1055     emitSymbol32(symtab32[0], *LDSymbol::Null(), strtab, 0, 0);
   1056   else
   1057     emitSymbol64(symtab64[0], *LDSymbol::Null(), strtab, 0, 0);
   1058 
   1059   size_t symIdx = 1;
   1060   size_t strtabsize = 1;
   1061 
   1062   Module::SymbolTable& symbols = pModule.getSymbolTable();
   1063   // emit .gnu.hash
   1064   if (GeneralOptions::GNU  == config().options().getHashStyle() ||
   1065       GeneralOptions::Both == config().options().getHashStyle())
   1066     emitGNUHashTab(symbols, pOutput);
   1067 
   1068   // emit .hash
   1069   if (GeneralOptions::SystemV == config().options().getHashStyle() ||
   1070       GeneralOptions::Both == config().options().getHashStyle())
   1071     emitELFHashTab(symbols, pOutput);
   1072 
   1073   // emit .dynsym, and .dynstr (emit LocalDyn and Dynamic category)
   1074   Module::const_sym_iterator symbol, symEnd = symbols.dynamicEnd();
   1075   for (symbol = symbols.localDynBegin(); symbol != symEnd; ++symbol) {
   1076     if (config().targets().is32Bits())
   1077       emitSymbol32(symtab32[symIdx], **symbol, strtab, strtabsize, symIdx);
   1078     else
   1079       emitSymbol64(symtab64[symIdx], **symbol, strtab, strtabsize, symIdx);
   1080     // maintain output's symbol and index map
   1081     entry = m_pSymIndexMap->insert(*symbol, sym_exist);
   1082     entry->setValue(symIdx);
   1083     // sum up counters
   1084     ++symIdx;
   1085     if (hasEntryInStrTab(**symbol))
   1086       strtabsize += (*symbol)->nameSize() + 1;
   1087   }
   1088 
   1089   // emit DT_NEED
   1090   // add DT_NEED strings into .dynstr
   1091   ELFDynamic::iterator dt_need = dynamic().needBegin();
   1092   Module::const_lib_iterator lib, libEnd = pModule.lib_end();
   1093   for (lib = pModule.lib_begin(); lib != libEnd; ++lib) {
   1094     if (!(*lib)->attribute()->isAsNeeded() || (*lib)->isNeeded()) {
   1095       strcpy((strtab + strtabsize), (*lib)->name().c_str());
   1096       (*dt_need)->setValue(llvm::ELF::DT_NEEDED, strtabsize);
   1097       strtabsize += (*lib)->name().size() + 1;
   1098       ++dt_need;
   1099     }
   1100   }
   1101 
   1102   if (!config().options().getRpathList().empty()) {
   1103     if (!config().options().hasNewDTags())
   1104       (*dt_need)->setValue(llvm::ELF::DT_RPATH, strtabsize);
   1105     else
   1106       (*dt_need)->setValue(llvm::ELF::DT_RUNPATH, strtabsize);
   1107     ++dt_need;
   1108 
   1109     GeneralOptions::const_rpath_iterator rpath,
   1110       rpathEnd = config().options().rpath_end();
   1111     for (rpath = config().options().rpath_begin(); rpath != rpathEnd; ++rpath) {
   1112       memcpy((strtab + strtabsize), (*rpath).data(), (*rpath).size());
   1113       strtabsize += (*rpath).size();
   1114       strtab[strtabsize++] = (rpath + 1 == rpathEnd ? '\0' : ':');
   1115     }
   1116   }
   1117 
   1118   // initialize value of ELF .dynamic section
   1119   if (LinkerConfig::DynObj == config().codeGenType()) {
   1120     // set pointer to SONAME entry in dynamic string table.
   1121     dynamic().applySoname(strtabsize);
   1122   }
   1123   dynamic().applyEntries(*file_format);
   1124   dynamic().emit(dyn_sect, *dyn_region);
   1125 
   1126   // emit soname
   1127   if (LinkerConfig::DynObj == config().codeGenType()) {
   1128     strcpy((strtab + strtabsize), pModule.name().c_str());
   1129     strtabsize += pModule.name().size() + 1;
   1130   }
   1131 }
   1132 
   1133 /// emitELFHashTab - emit .hash
   1134 void GNULDBackend::emitELFHashTab(const Module::SymbolTable& pSymtab,
   1135                                   MemoryArea& pOutput)
   1136 {
   1137   ELFFileFormat* file_format = getOutputFormat();
   1138   if (!file_format->hasHashTab())
   1139     return;
   1140   LDSection& hash_sect = file_format->getHashTab();
   1141   MemoryRegion* hash_region = pOutput.request(hash_sect.offset(),
   1142                                               hash_sect.size());
   1143   // both 32 and 64 bits hash table use 32-bit entry
   1144   // set up hash_region
   1145   uint32_t* word_array = (uint32_t*)hash_region->start();
   1146   uint32_t& nbucket = word_array[0];
   1147   uint32_t& nchain  = word_array[1];
   1148 
   1149   size_t dynsymSize = 1 + pSymtab.numOfLocalDyns() + pSymtab.numOfDynamics();
   1150   nbucket = getHashBucketCount(dynsymSize, false);
   1151   nchain  = dynsymSize;
   1152 
   1153   uint32_t* bucket = (word_array + 2);
   1154   uint32_t* chain  = (bucket + nbucket);
   1155 
   1156   // initialize bucket
   1157   memset((void*)bucket, 0, nbucket);
   1158 
   1159   hash::StringHash<hash::ELF> hash_func;
   1160 
   1161   size_t idx = 1;
   1162   Module::const_sym_iterator symbol, symEnd = pSymtab.dynamicEnd();
   1163   for (symbol = pSymtab.localDynBegin(); symbol != symEnd; ++symbol) {
   1164     llvm::StringRef name((*symbol)->name());
   1165     size_t bucket_pos = hash_func(name) % nbucket;
   1166     chain[idx] = bucket[bucket_pos];
   1167     bucket[bucket_pos] = idx;
   1168     ++idx;
   1169   }
   1170 }
   1171 
   1172 /// emitGNUHashTab - emit .gnu.hash
   1173 void GNULDBackend::emitGNUHashTab(Module::SymbolTable& pSymtab,
   1174                                   MemoryArea& pOutput)
   1175 {
   1176   ELFFileFormat* file_format = getOutputFormat();
   1177   if (!file_format->hasGNUHashTab())
   1178     return;
   1179 
   1180   MemoryRegion* gnuhash_region =
   1181     pOutput.request(file_format->getGNUHashTab().offset(),
   1182                     file_format->getGNUHashTab().size());
   1183 
   1184   uint32_t* word_array = (uint32_t*)gnuhash_region->start();
   1185   // fixed-length fields
   1186   uint32_t& nbucket   = word_array[0];
   1187   uint32_t& symidx    = word_array[1];
   1188   uint32_t& maskwords = word_array[2];
   1189   uint32_t& shift2    = word_array[3];
   1190   // variable-length fields
   1191   uint8_t*  bitmask = (uint8_t*)(word_array + 4);
   1192   uint32_t* bucket  = NULL;
   1193   uint32_t* chain   = NULL;
   1194 
   1195   // count the number of dynsym to hash
   1196   size_t unhashed_sym_cnt = pSymtab.numOfLocalDyns();
   1197   size_t hashed_sym_cnt   = pSymtab.numOfDynamics();
   1198   Module::const_sym_iterator symbol, symEnd = pSymtab.dynamicEnd();
   1199   for (symbol = pSymtab.dynamicBegin(); symbol != symEnd; ++symbol) {
   1200     if (DynsymCompare().needGNUHash(**symbol))
   1201       break;
   1202       ++unhashed_sym_cnt;
   1203       --hashed_sym_cnt;
   1204   }
   1205 
   1206   // special case for the empty hash table
   1207   if (hashed_sym_cnt == 0) {
   1208     nbucket   = 1; // one empty bucket
   1209     symidx    = 1 + unhashed_sym_cnt; // symidx above unhashed symbols
   1210     maskwords = 1; // bitmask length
   1211     shift2    = 0; // bloom filter
   1212 
   1213     if (config().targets().is32Bits()) {
   1214       uint32_t* maskval = (uint32_t*)bitmask;
   1215       *maskval = 0; // no valid hashes
   1216     } else {
   1217       // must be 64
   1218       uint64_t* maskval = (uint64_t*)bitmask;
   1219       *maskval = 0; // no valid hashes
   1220     }
   1221     bucket  = (uint32_t*)(bitmask + config().targets().bitclass() / 8);
   1222     *bucket = 0; // no hash in the only bucket
   1223     return;
   1224   }
   1225 
   1226   uint32_t maskbitslog2 = getGNUHashMaskbitslog2(hashed_sym_cnt);
   1227   uint32_t maskbits = 1u << maskbitslog2;
   1228   uint32_t shift1 = config().targets().is32Bits() ? 5 : 6;
   1229   uint32_t mask = (1u << shift1) - 1;
   1230 
   1231   nbucket   = getHashBucketCount(hashed_sym_cnt, true);
   1232   symidx    = 1 + unhashed_sym_cnt;
   1233   maskwords = 1 << (maskbitslog2 - shift1);
   1234   shift2    = maskbitslog2;
   1235 
   1236   // setup bucket and chain
   1237   bucket = (uint32_t*)(bitmask + maskbits / 8);
   1238   chain  = (bucket + nbucket);
   1239 
   1240   // build the gnu style hash table
   1241   typedef std::multimap<uint32_t,
   1242                         std::pair<LDSymbol*, uint32_t> > SymMapType;
   1243   SymMapType symmap;
   1244   symEnd = pSymtab.dynamicEnd();
   1245   for (symbol = pSymtab.localDynBegin() + symidx - 1; symbol != symEnd;
   1246     ++symbol) {
   1247     hash::StringHash<hash::DJB> hasher;
   1248     uint32_t djbhash = hasher((*symbol)->name());
   1249     uint32_t hash = djbhash % nbucket;
   1250     symmap.insert(std::make_pair(hash, std::make_pair(*symbol, djbhash)));
   1251   }
   1252 
   1253   // compute bucket, chain, and bitmask
   1254   std::vector<uint64_t> bitmasks(maskwords);
   1255   size_t hashedidx = symidx;
   1256   for (size_t idx = 0; idx < nbucket; ++idx) {
   1257     size_t count = 0;
   1258     std::pair<SymMapType::iterator, SymMapType::iterator> ret;
   1259     ret = symmap.equal_range(idx);
   1260     for (SymMapType::iterator it = ret.first; it != ret.second; ) {
   1261       // rearrange the hashed symbol ordering
   1262       *(pSymtab.localDynBegin() + hashedidx - 1) = it->second.first;
   1263       uint32_t djbhash = it->second.second;
   1264       uint32_t val = ((djbhash >> shift1) & ((maskbits >> shift1) - 1));
   1265       bitmasks[val] |= 1u << (djbhash & mask);
   1266       bitmasks[val] |= 1u << ((djbhash >> shift2) & mask);
   1267       val = djbhash & ~1u;
   1268       // advance the iterator and check if we're dealing w/ the last elment
   1269       if (++it == ret.second) {
   1270         // last element terminates the chain
   1271         val |= 1;
   1272       }
   1273       chain[hashedidx - symidx] = val;
   1274 
   1275       ++hashedidx;
   1276       ++count;
   1277     }
   1278 
   1279     if (count == 0)
   1280       bucket[idx] = 0;
   1281     else
   1282       bucket[idx] = hashedidx - count;
   1283   }
   1284 
   1285   // write the bitmasks
   1286   if (config().targets().is32Bits()) {
   1287     uint32_t* maskval = (uint32_t*)bitmask;
   1288     for (size_t i = 0; i < maskwords; ++i)
   1289       std::memcpy(maskval + i, &bitmasks[i], 4);
   1290   } else {
   1291     // must be 64
   1292     uint64_t* maskval = (uint64_t*)bitmask;
   1293     for (size_t i = 0; i < maskwords; ++i)
   1294       std::memcpy(maskval + i, &bitmasks[i], 8);
   1295   }
   1296 }
   1297 
   1298 /// sizeInterp - compute the size of the .interp section
   1299 void GNULDBackend::sizeInterp()
   1300 {
   1301   const char* dyld_name;
   1302   if (config().options().hasDyld())
   1303     dyld_name = config().options().dyld().c_str();
   1304   else
   1305     dyld_name = m_pInfo->dyld();
   1306 
   1307   LDSection& interp = getOutputFormat()->getInterp();
   1308   interp.setSize(std::strlen(dyld_name) + 1);
   1309 }
   1310 
   1311 /// emitInterp - emit the .interp
   1312 void GNULDBackend::emitInterp(MemoryArea& pOutput)
   1313 {
   1314   if (getOutputFormat()->hasInterp()) {
   1315     const LDSection& interp = getOutputFormat()->getInterp();
   1316     MemoryRegion *region = pOutput.request(interp.offset(), interp.size());
   1317     const char* dyld_name;
   1318     if (config().options().hasDyld())
   1319       dyld_name = config().options().dyld().c_str();
   1320     else
   1321       dyld_name = m_pInfo->dyld();
   1322 
   1323     std::memcpy(region->start(), dyld_name, interp.size());
   1324   }
   1325 }
   1326 
   1327 bool GNULDBackend::hasEntryInStrTab(const LDSymbol& pSym) const
   1328 {
   1329   return ResolveInfo::Section != pSym.type();
   1330 }
   1331 
   1332 void GNULDBackend::orderSymbolTable(Module& pModule)
   1333 {
   1334   Module::SymbolTable& symbols = pModule.getSymbolTable();
   1335 
   1336   if (GeneralOptions::GNU  == config().options().getHashStyle() ||
   1337       GeneralOptions::Both == config().options().getHashStyle())
   1338     // Currently we may add output symbols after sizeNamePools(), and a
   1339     // non-stable sort is used in SymbolCategory::arrange(), so we just
   1340     // sort .dynsym right before emitting .gnu.hash
   1341     std::stable_sort(symbols.dynamicBegin(), symbols.dynamicEnd(),
   1342                      DynsymCompare());
   1343 }
   1344 
   1345 /// getSectionOrder
   1346 unsigned int GNULDBackend::getSectionOrder(const LDSection& pSectHdr) const
   1347 {
   1348   const ELFFileFormat* file_format = getOutputFormat();
   1349 
   1350   // NULL section should be the "1st" section
   1351   if (LDFileFormat::Null == pSectHdr.kind())
   1352     return 0;
   1353 
   1354   if (&pSectHdr == &file_format->getStrTab())
   1355     return SHO_STRTAB;
   1356 
   1357   // if the section is not ALLOC, lay it out until the last possible moment
   1358   if (0 == (pSectHdr.flag() & llvm::ELF::SHF_ALLOC))
   1359     return SHO_UNDEFINED;
   1360 
   1361   bool is_write = (pSectHdr.flag() & llvm::ELF::SHF_WRITE) != 0;
   1362   bool is_exec = (pSectHdr.flag() & llvm::ELF::SHF_EXECINSTR) != 0;
   1363   // TODO: need to take care other possible output sections
   1364   switch (pSectHdr.kind()) {
   1365     case LDFileFormat::Regular:
   1366       if (is_exec) {
   1367         if (&pSectHdr == &file_format->getInit())
   1368           return SHO_INIT;
   1369         if (&pSectHdr == &file_format->getFini())
   1370           return SHO_FINI;
   1371         return SHO_TEXT;
   1372       } else if (!is_write) {
   1373         return SHO_RO;
   1374       } else {
   1375         if (config().options().hasRelro()) {
   1376           if (&pSectHdr == &file_format->getPreInitArray() ||
   1377               &pSectHdr == &file_format->getInitArray() ||
   1378               &pSectHdr == &file_format->getFiniArray() ||
   1379               &pSectHdr == &file_format->getCtors() ||
   1380               &pSectHdr == &file_format->getDtors() ||
   1381               &pSectHdr == &file_format->getJCR() ||
   1382               &pSectHdr == &file_format->getDataRelRo())
   1383             return SHO_RELRO;
   1384           if (&pSectHdr == &file_format->getDataRelRoLocal())
   1385             return SHO_RELRO_LOCAL;
   1386         }
   1387         if ((pSectHdr.flag() & llvm::ELF::SHF_TLS) != 0x0) {
   1388           return SHO_TLS_DATA;
   1389         }
   1390         return SHO_DATA;
   1391       }
   1392 
   1393     case LDFileFormat::BSS:
   1394       if ((pSectHdr.flag() & llvm::ELF::SHF_TLS) != 0x0)
   1395         return SHO_TLS_BSS;
   1396       return SHO_BSS;
   1397 
   1398     case LDFileFormat::NamePool: {
   1399       if (&pSectHdr == &file_format->getDynamic())
   1400         return SHO_RELRO;
   1401       return SHO_NAMEPOOL;
   1402     }
   1403     case LDFileFormat::Relocation:
   1404       if (&pSectHdr == &file_format->getRelPlt() ||
   1405           &pSectHdr == &file_format->getRelaPlt())
   1406         return SHO_REL_PLT;
   1407       return SHO_RELOCATION;
   1408 
   1409     // get the order from target for target specific sections
   1410     case LDFileFormat::Target:
   1411       return getTargetSectionOrder(pSectHdr);
   1412 
   1413     // handle .interp and .note.* sections
   1414     case LDFileFormat::Note:
   1415       if (file_format->hasInterp() && (&pSectHdr == &file_format->getInterp()))
   1416         return SHO_INTERP;
   1417       else if (is_write)
   1418         return SHO_RW_NOTE;
   1419       else
   1420         return SHO_RO_NOTE;
   1421 
   1422     case LDFileFormat::EhFrame:
   1423       // set writable .eh_frame as relro
   1424       if (is_write)
   1425         return SHO_RELRO;
   1426     case LDFileFormat::EhFrameHdr:
   1427     case LDFileFormat::GCCExceptTable:
   1428       return SHO_EXCEPTION;
   1429 
   1430     case LDFileFormat::MetaData:
   1431     case LDFileFormat::Debug:
   1432     default:
   1433       return SHO_UNDEFINED;
   1434   }
   1435 }
   1436 
   1437 /// getSymbolSize
   1438 uint64_t GNULDBackend::getSymbolSize(const LDSymbol& pSymbol) const
   1439 {
   1440   // @ref Google gold linker: symtab.cc: 2780
   1441   // undefined and dynamic symbols should have zero size.
   1442   if (pSymbol.isDyn() || pSymbol.desc() == ResolveInfo::Undefined)
   1443     return 0x0;
   1444   return pSymbol.resolveInfo()->size();
   1445 }
   1446 
   1447 /// getSymbolInfo
   1448 uint64_t GNULDBackend::getSymbolInfo(const LDSymbol& pSymbol) const
   1449 {
   1450   // set binding
   1451   uint8_t bind = 0x0;
   1452   if (pSymbol.resolveInfo()->isLocal())
   1453     bind = llvm::ELF::STB_LOCAL;
   1454   else if (pSymbol.resolveInfo()->isGlobal())
   1455     bind = llvm::ELF::STB_GLOBAL;
   1456   else if (pSymbol.resolveInfo()->isWeak())
   1457     bind = llvm::ELF::STB_WEAK;
   1458   else if (pSymbol.resolveInfo()->isAbsolute()) {
   1459     // (Luba) Is a absolute but not global (weak or local) symbol meaningful?
   1460     bind = llvm::ELF::STB_GLOBAL;
   1461   }
   1462 
   1463   if (config().codeGenType() != LinkerConfig::Object &&
   1464       (pSymbol.visibility() == llvm::ELF::STV_INTERNAL ||
   1465       pSymbol.visibility() == llvm::ELF::STV_HIDDEN))
   1466     bind = llvm::ELF::STB_LOCAL;
   1467 
   1468   uint32_t type = pSymbol.resolveInfo()->type();
   1469   // if the IndirectFunc symbol (i.e., STT_GNU_IFUNC) is from dynobj, change
   1470   // its type to Function
   1471   if (type == ResolveInfo::IndirectFunc && pSymbol.isDyn())
   1472     type = ResolveInfo::Function;
   1473   return (type | (bind << 4));
   1474 }
   1475 
   1476 /// getSymbolValue - this function is called after layout()
   1477 uint64_t GNULDBackend::getSymbolValue(const LDSymbol& pSymbol) const
   1478 {
   1479   if (pSymbol.isDyn())
   1480     return 0x0;
   1481 
   1482   return pSymbol.value();
   1483 }
   1484 
   1485 /// getSymbolShndx - this function is called after layout()
   1486 uint64_t
   1487 GNULDBackend::getSymbolShndx(const LDSymbol& pSymbol) const
   1488 {
   1489   if (pSymbol.resolveInfo()->isAbsolute())
   1490     return llvm::ELF::SHN_ABS;
   1491   if (pSymbol.resolveInfo()->isCommon())
   1492     return llvm::ELF::SHN_COMMON;
   1493   if (pSymbol.resolveInfo()->isUndef() || pSymbol.isDyn())
   1494     return llvm::ELF::SHN_UNDEF;
   1495 
   1496   if (pSymbol.resolveInfo()->isLocal() &&
   1497       LinkerConfig::Object != config().codeGenType()) {
   1498     switch (pSymbol.type()) {
   1499       case ResolveInfo::NoType:
   1500       case ResolveInfo::File:
   1501         return llvm::ELF::SHN_ABS;
   1502     }
   1503   }
   1504 
   1505   if (pSymbol.resolveInfo()->isDefine() && !pSymbol.hasFragRef())
   1506     return llvm::ELF::SHN_ABS;
   1507 
   1508   assert(pSymbol.hasFragRef() && "symbols must have fragment reference to get its index");
   1509   return pSymbol.fragRef()->frag()->getParent()->getSection().index();
   1510 }
   1511 
   1512 /// getSymbolIdx - called by emitRelocation to get the ouput symbol table index
   1513 size_t GNULDBackend::getSymbolIdx(const LDSymbol* pSymbol) const
   1514 {
   1515    HashTableType::iterator entry = m_pSymIndexMap->find(const_cast<LDSymbol *>(pSymbol));
   1516    assert(entry != m_pSymIndexMap->end() && "symbol not found in the symbol table");
   1517    return entry.getEntry()->value();
   1518 }
   1519 
   1520 /// isTemporary - Whether pSymbol is a local label.
   1521 bool GNULDBackend::isTemporary(const LDSymbol& pSymbol) const
   1522 {
   1523   if (ResolveInfo::Local != pSymbol.binding())
   1524     return false;
   1525 
   1526   if (pSymbol.nameSize() < 2)
   1527     return false;
   1528 
   1529   const char* name = pSymbol.name();
   1530   if ('.' == name[0] && 'L' == name[1])
   1531     return true;
   1532 
   1533   // UnixWare 2.1 cc generate DWARF debugging symbols with `..' prefix.
   1534   // @ref Google gold linker, target.cc:39 @@ Target::do_is_local_label_name()
   1535   if (name[0] == '.' && name[1] == '.')
   1536     return true;
   1537 
   1538   // Work arround for gcc's bug
   1539   // gcc sometimes generate symbols with '_.L_' prefix.
   1540   // @ref Google gold linker, target.cc:39 @@ Target::do_is_local_label_name()
   1541   if (pSymbol.nameSize() < 4)
   1542     return false;
   1543 
   1544   if (name[0] == '_' && name[1] == '.' && name[2] == 'L' && name[3] == '_')
   1545     return true;
   1546 
   1547   return false;
   1548 }
   1549 
   1550 /// allocateCommonSymbols - allocate common symbols in the corresponding
   1551 /// sections. This is executed at pre-layout stage.
   1552 /// @refer Google gold linker: common.cc: 214
   1553 bool
   1554 GNULDBackend::allocateCommonSymbols(Module& pModule)
   1555 {
   1556   SymbolCategory& symbol_list = pModule.getSymbolTable();
   1557 
   1558   if (symbol_list.emptyCommons() && symbol_list.emptyFiles() &&
   1559       symbol_list.emptyLocals() && symbol_list.emptyLocalDyns())
   1560     return true;
   1561 
   1562   SymbolCategory::iterator com_sym, com_end;
   1563 
   1564   // FIXME: If the order of common symbols is defined, then sort common symbols
   1565   // std::sort(com_sym, com_end, some kind of order);
   1566 
   1567   // get corresponding BSS LDSection
   1568   ELFFileFormat* file_format = getOutputFormat();
   1569   LDSection& bss_sect = file_format->getBSS();
   1570   LDSection& tbss_sect = file_format->getTBSS();
   1571 
   1572   // get or create corresponding BSS SectionData
   1573   SectionData* bss_sect_data = NULL;
   1574   if (bss_sect.hasSectionData())
   1575     bss_sect_data = bss_sect.getSectionData();
   1576   else
   1577     bss_sect_data = IRBuilder::CreateSectionData(bss_sect);
   1578 
   1579   SectionData* tbss_sect_data = NULL;
   1580   if (tbss_sect.hasSectionData())
   1581     tbss_sect_data = tbss_sect.getSectionData();
   1582   else
   1583     tbss_sect_data = IRBuilder::CreateSectionData(tbss_sect);
   1584 
   1585   // remember original BSS size
   1586   uint64_t bss_offset  = bss_sect.size();
   1587   uint64_t tbss_offset = tbss_sect.size();
   1588 
   1589   // allocate all local common symbols
   1590   com_end = symbol_list.localEnd();
   1591 
   1592   for (com_sym = symbol_list.localBegin(); com_sym != com_end; ++com_sym) {
   1593     if (ResolveInfo::Common == (*com_sym)->desc()) {
   1594       // We have to reset the description of the symbol here. When doing
   1595       // incremental linking, the output relocatable object may have common
   1596       // symbols. Therefore, we can not treat common symbols as normal symbols
   1597       // when emitting the regular name pools. We must change the symbols'
   1598       // description here.
   1599       (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define);
   1600       Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size());
   1601       (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0));
   1602 
   1603       if (ResolveInfo::ThreadLocal == (*com_sym)->type()) {
   1604         // allocate TLS common symbol in tbss section
   1605         tbss_offset += ObjectBuilder::AppendFragment(*frag,
   1606                                                      *tbss_sect_data,
   1607                                                      (*com_sym)->value());
   1608       }
   1609       else {
   1610         bss_offset += ObjectBuilder::AppendFragment(*frag,
   1611                                                     *bss_sect_data,
   1612                                                     (*com_sym)->value());
   1613       }
   1614     }
   1615   }
   1616 
   1617   // allocate all global common symbols
   1618   com_end = symbol_list.commonEnd();
   1619   for (com_sym = symbol_list.commonBegin(); com_sym != com_end; ++com_sym) {
   1620     // We have to reset the description of the symbol here. When doing
   1621     // incremental linking, the output relocatable object may have common
   1622     // symbols. Therefore, we can not treat common symbols as normal symbols
   1623     // when emitting the regular name pools. We must change the symbols'
   1624     // description here.
   1625     (*com_sym)->resolveInfo()->setDesc(ResolveInfo::Define);
   1626     Fragment* frag = new FillFragment(0x0, 1, (*com_sym)->size());
   1627     (*com_sym)->setFragmentRef(FragmentRef::Create(*frag, 0));
   1628 
   1629     if (ResolveInfo::ThreadLocal == (*com_sym)->type()) {
   1630       // allocate TLS common symbol in tbss section
   1631       tbss_offset += ObjectBuilder::AppendFragment(*frag,
   1632                                                    *tbss_sect_data,
   1633                                                    (*com_sym)->value());
   1634     }
   1635     else {
   1636       bss_offset += ObjectBuilder::AppendFragment(*frag,
   1637                                                   *bss_sect_data,
   1638                                                   (*com_sym)->value());
   1639     }
   1640   }
   1641 
   1642   bss_sect.setSize(bss_offset);
   1643   tbss_sect.setSize(tbss_offset);
   1644   symbol_list.changeCommonsToGlobal();
   1645   return true;
   1646 }
   1647 
   1648 /// updateSectionFlags - update pTo's flags when merging pFrom
   1649 /// update the output section flags based on input section flags.
   1650 /// @ref The Google gold linker:
   1651 ///      output.cc: 2809: Output_section::update_flags_for_input_section
   1652 bool GNULDBackend::updateSectionFlags(LDSection& pTo, const LDSection& pFrom)
   1653 {
   1654   // union the flags from input
   1655   uint32_t flags = pTo.flag();
   1656   flags |= (pFrom.flag() &
   1657               (llvm::ELF::SHF_WRITE |
   1658                llvm::ELF::SHF_ALLOC |
   1659                llvm::ELF::SHF_EXECINSTR));
   1660 
   1661   // if there is an input section is not SHF_MERGE, clean this flag
   1662   if (0 == (pFrom.flag() & llvm::ELF::SHF_MERGE))
   1663     flags &= ~llvm::ELF::SHF_MERGE;
   1664 
   1665   // if there is an input section is not SHF_STRINGS, clean this flag
   1666   if (0 == (pFrom.flag() & llvm::ELF::SHF_STRINGS))
   1667     flags &= ~llvm::ELF::SHF_STRINGS;
   1668 
   1669   pTo.setFlag(flags);
   1670   return true;
   1671 }
   1672 
   1673 /// createProgramHdrs - base on output sections to create the program headers
   1674 void GNULDBackend::createProgramHdrs(Module& pModule)
   1675 {
   1676   ELFFileFormat *file_format = getOutputFormat();
   1677 
   1678   // make PT_PHDR
   1679   m_ELFSegmentTable.produce(llvm::ELF::PT_PHDR);
   1680 
   1681   // make PT_INTERP
   1682   if (file_format->hasInterp()) {
   1683     ELFSegment* interp_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_INTERP);
   1684     interp_seg->addSection(&file_format->getInterp());
   1685   }
   1686 
   1687   uint32_t cur_flag, prev_flag = getSegmentFlag(0);
   1688   ELFSegment* load_seg = NULL;
   1689   // make possible PT_LOAD segments
   1690   LinkerScript::AddressMap::iterator addrEnd
   1691                                       = pModule.getScript().addressMap().end();
   1692   Module::iterator sect, sect_end = pModule.end();
   1693   for (sect = pModule.begin(); sect != sect_end; ++sect) {
   1694 
   1695     if (0 == ((*sect)->flag() & llvm::ELF::SHF_ALLOC) &&
   1696         LDFileFormat::Null != (*sect)->kind())
   1697       continue;
   1698 
   1699     cur_flag = getSegmentFlag((*sect)->flag());
   1700     bool createPT_LOAD = false;
   1701     if (LDFileFormat::Null == (*sect)->kind()) {
   1702       // 1. create text segment
   1703       createPT_LOAD = true;
   1704     }
   1705     else if (!config().options().omagic() &&
   1706              (prev_flag & llvm::ELF::PF_W) ^ (cur_flag & llvm::ELF::PF_W)) {
   1707       // 2. create data segment if w/o omagic set
   1708       createPT_LOAD = true;
   1709     }
   1710     else if ((*sect)->kind() == LDFileFormat::BSS &&
   1711              load_seg->isDataSegment() &&
   1712              addrEnd != pModule.getScript().addressMap().find(".bss")) {
   1713       // 3. create bss segment if w/ -Tbss and there is a data segment
   1714       createPT_LOAD = true;
   1715     }
   1716     else {
   1717       if ((*sect != &(file_format->getText())) &&
   1718           (*sect != &(file_format->getData())) &&
   1719           (*sect != &(file_format->getBSS())) &&
   1720           (addrEnd != pModule.getScript().addressMap().find((*sect)->name())))
   1721         // 4. create PT_LOAD for sections in address map except for text, data,
   1722         // and bss
   1723         createPT_LOAD = true;
   1724     }
   1725 
   1726     if (createPT_LOAD) {
   1727       // create new PT_LOAD segment
   1728       load_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_LOAD, cur_flag);
   1729       if (!config().options().nmagic() && !config().options().omagic())
   1730         load_seg->setAlign(abiPageSize());
   1731     }
   1732 
   1733     assert(NULL != load_seg);
   1734     load_seg->addSection((*sect));
   1735     if (cur_flag != prev_flag)
   1736       load_seg->updateFlag(cur_flag);
   1737 
   1738     prev_flag = cur_flag;
   1739   }
   1740 
   1741   // make PT_DYNAMIC
   1742   if (file_format->hasDynamic()) {
   1743     ELFSegment* dyn_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_DYNAMIC,
   1744                                                     llvm::ELF::PF_R |
   1745                                                     llvm::ELF::PF_W);
   1746     dyn_seg->addSection(&file_format->getDynamic());
   1747   }
   1748 
   1749   if (config().options().hasRelro()) {
   1750     // make PT_GNU_RELRO
   1751     ELFSegment* relro_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_RELRO);
   1752     for (ELFSegmentFactory::iterator seg = elfSegmentTable().begin(),
   1753          segEnd = elfSegmentTable().end(); seg != segEnd; ++seg) {
   1754       if (llvm::ELF::PT_LOAD != (*seg).type())
   1755         continue;
   1756 
   1757       for (ELFSegment::sect_iterator sect = (*seg).begin(),
   1758              sectEnd = (*seg).end(); sect != sectEnd; ++sect) {
   1759         unsigned int order = getSectionOrder(**sect);
   1760         if (SHO_RELRO_LOCAL == order ||
   1761             SHO_RELRO == order ||
   1762             SHO_RELRO_LAST == order) {
   1763           relro_seg->addSection(*sect);
   1764         }
   1765       }
   1766     }
   1767   }
   1768 
   1769   // make PT_GNU_EH_FRAME
   1770   if (file_format->hasEhFrameHdr()) {
   1771     ELFSegment* eh_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_EH_FRAME);
   1772     eh_seg->addSection(&file_format->getEhFrameHdr());
   1773   }
   1774 
   1775   // make PT_TLS
   1776   if (file_format->hasTData() || file_format->hasTBSS()) {
   1777     ELFSegment* tls_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_TLS);
   1778     if (file_format->hasTData())
   1779       tls_seg->addSection(&file_format->getTData());
   1780     if (file_format->hasTBSS())
   1781       tls_seg->addSection(&file_format->getTBSS());
   1782   }
   1783 
   1784   // make PT_GNU_STACK
   1785   if (file_format->hasStackNote()) {
   1786     m_ELFSegmentTable.produce(llvm::ELF::PT_GNU_STACK,
   1787                               llvm::ELF::PF_R |
   1788                               llvm::ELF::PF_W |
   1789                               getSegmentFlag(file_format->getStackNote().flag()));
   1790   }
   1791 
   1792   // make PT_NOTE
   1793   ELFSegment *note_seg = NULL;
   1794   prev_flag = getSegmentFlag(0);
   1795   for (sect = pModule.begin(); sect != sect_end; ++sect) {
   1796     if ((*sect)->kind() != LDFileFormat::Note ||
   1797         ((*sect)->flag() & llvm::ELF::SHF_ALLOC) == 0)
   1798       continue;
   1799 
   1800     cur_flag = getSegmentFlag((*sect)->flag());
   1801     // we have different section orders for read-only and writable notes, so
   1802     // create 2 segments if needed.
   1803     if (note_seg == NULL ||
   1804         (cur_flag & llvm::ELF::PF_W) != (prev_flag & llvm::ELF::PF_W))
   1805       note_seg = m_ELFSegmentTable.produce(llvm::ELF::PT_NOTE, cur_flag);
   1806 
   1807     note_seg->addSection(*sect);
   1808     prev_flag = cur_flag;
   1809   }
   1810 
   1811   // create target dependent segments
   1812   doCreateProgramHdrs(pModule);
   1813 }
   1814 
   1815 /// setupProgramHdrs - set up the attributes of segments
   1816 void GNULDBackend::setupProgramHdrs(const LinkerScript& pScript)
   1817 {
   1818   // update segment info
   1819   uint64_t seg_start_addr = getSegmentStartAddr(pScript);
   1820   ELFSegmentFactory::iterator seg, seg_end = m_ELFSegmentTable.end();
   1821   for (seg = m_ELFSegmentTable.begin(); seg != seg_end; ++seg) {
   1822     ELFSegment& segment = *seg;
   1823 
   1824     // update PT_PHDR
   1825     if (llvm::ELF::PT_PHDR == segment.type()) {
   1826       uint64_t offset = 0, phdr_size = 0;
   1827       if (config().targets().is32Bits()) {
   1828         offset = sizeof(llvm::ELF::Elf32_Ehdr);
   1829         phdr_size = sizeof(llvm::ELF::Elf32_Phdr);
   1830       }
   1831       else {
   1832         offset = sizeof(llvm::ELF::Elf64_Ehdr);
   1833         phdr_size = sizeof(llvm::ELF::Elf64_Phdr);
   1834       }
   1835       segment.setOffset(offset);
   1836       segment.setVaddr(seg_start_addr + offset);
   1837       segment.setPaddr(segment.vaddr());
   1838       segment.setFilesz(numOfSegments() * phdr_size);
   1839       segment.setMemsz(numOfSegments() * phdr_size);
   1840       segment.setAlign(config().targets().bitclass() / 8);
   1841       continue;
   1842     }
   1843 
   1844     // bypass if there is no section in this segment (e.g., PT_GNU_STACK)
   1845     if (segment.numOfSections() == 0)
   1846       continue;
   1847 
   1848     segment.setOffset(segment.front()->offset());
   1849     if (llvm::ELF::PT_LOAD == segment.type() &&
   1850         LDFileFormat::Null == segment.front()->kind())
   1851       segment.setVaddr(seg_start_addr);
   1852     else
   1853       segment.setVaddr(segment.front()->addr());
   1854     segment.setPaddr(segment.vaddr());
   1855 
   1856     const LDSection* last_sect = segment.back();
   1857     assert(NULL != last_sect);
   1858     uint64_t file_size = last_sect->offset() - segment.offset();
   1859     if (LDFileFormat::BSS != last_sect->kind())
   1860       file_size += last_sect->size();
   1861     segment.setFilesz(file_size);
   1862 
   1863     segment.setMemsz(last_sect->addr() - segment.vaddr() + last_sect->size());
   1864   }
   1865 }
   1866 
   1867 /// setupGNUStackInfo - setup the section flag of .note.GNU-stack in output
   1868 /// @ref gold linker: layout.cc:2608
   1869 void GNULDBackend::setupGNUStackInfo(Module& pModule)
   1870 {
   1871   uint32_t flag = 0x0;
   1872   if (config().options().hasStackSet()) {
   1873     // 1. check the command line option (-z execstack or -z noexecstack)
   1874     if (config().options().hasExecStack())
   1875       flag = llvm::ELF::SHF_EXECINSTR;
   1876   }
   1877   else {
   1878     // 2. check the stack info from the input objects
   1879     // FIXME: since we alway emit .note.GNU-stack in output now, we may be able
   1880     // to check this from the output .note.GNU-stack directly after section
   1881     // merging is done
   1882     size_t object_count = 0, stack_note_count = 0;
   1883     Module::const_obj_iterator obj, objEnd = pModule.obj_end();
   1884     for (obj = pModule.obj_begin(); obj != objEnd; ++obj) {
   1885       ++object_count;
   1886       const LDSection* sect = (*obj)->context()->getSection(".note.GNU-stack");
   1887       if (NULL != sect) {
   1888         ++stack_note_count;
   1889         // 2.1 found a stack note that is set as executable
   1890         if (0 != (llvm::ELF::SHF_EXECINSTR & sect->flag())) {
   1891           flag = llvm::ELF::SHF_EXECINSTR;
   1892           break;
   1893         }
   1894       }
   1895     }
   1896 
   1897     // 2.2 there are no stack note sections in all input objects
   1898     if (0 == stack_note_count)
   1899       return;
   1900 
   1901     // 2.3 a special case. Use the target default to decide if the stack should
   1902     //     be executable
   1903     if (llvm::ELF::SHF_EXECINSTR != flag && object_count != stack_note_count)
   1904       if (m_pInfo->isDefaultExecStack())
   1905         flag = llvm::ELF::SHF_EXECINSTR;
   1906   }
   1907 
   1908   if (getOutputFormat()->hasStackNote()) {
   1909     getOutputFormat()->getStackNote().setFlag(flag);
   1910   }
   1911 }
   1912 
   1913 /// setupRelro - setup the offset constraint of PT_RELRO
   1914 void GNULDBackend::setupRelro(Module& pModule)
   1915 {
   1916   assert(config().options().hasRelro());
   1917   // if -z relro is given, we need to adjust sections' offset again, and let
   1918   // PT_GNU_RELRO end on a common page boundary
   1919 
   1920   Module::iterator sect = pModule.begin();
   1921   for (Module::iterator sect_end = pModule.end(); sect != sect_end; ++sect) {
   1922     // find the first non-relro section
   1923     if (getSectionOrder(**sect) > SHO_RELRO_LAST)
   1924       break;
   1925   }
   1926 
   1927   // align the first non-relro section to page boundary
   1928   uint64_t offset = (*sect)->offset();
   1929   alignAddress(offset, commonPageSize());
   1930   (*sect)->setOffset(offset);
   1931 
   1932   // It seems that compiler think .got and .got.plt are continuous (w/o any
   1933   // padding between). If .got is the last section in PT_RELRO and it's not
   1934   // continuous to its next section (i.e. .got.plt), we need to add padding
   1935   // in front of .got instead.
   1936   // FIXME: Maybe we can handle this in a more general way.
   1937   LDSection& got = getOutputFormat()->getGOT();
   1938   if ((getSectionOrder(got) == SHO_RELRO_LAST) &&
   1939       (got.offset() + got.size() != offset)) {
   1940     got.setOffset(offset - got.size());
   1941   }
   1942 
   1943   // set up remaining section's offset
   1944   setOutputSectionOffset(pModule, ++sect, pModule.end());
   1945 }
   1946 
   1947 /// setOutputSectionOffset - helper function to set a group of output sections'
   1948 /// offset, and set pSectBegin to pStartOffset if pStartOffset is not -1U.
   1949 void GNULDBackend::setOutputSectionOffset(Module& pModule,
   1950                                           Module::iterator pSectBegin,
   1951                                           Module::iterator pSectEnd,
   1952                                           uint64_t pStartOffset)
   1953 {
   1954   if (pSectBegin == pModule.end())
   1955     return;
   1956 
   1957   assert(pSectEnd == pModule.end() ||
   1958          (pSectEnd != pModule.end() &&
   1959           (*pSectBegin)->index() <= (*pSectEnd)->index()));
   1960 
   1961   if (pStartOffset != -1U) {
   1962     (*pSectBegin)->setOffset(pStartOffset);
   1963     ++pSectBegin;
   1964   }
   1965 
   1966   // set up the "cur" and "prev" iterator
   1967   Module::iterator cur = pSectBegin;
   1968   Module::iterator prev = pSectBegin;
   1969   if (cur != pModule.begin())
   1970     --prev;
   1971   else
   1972     ++cur;
   1973 
   1974   for (; cur != pSectEnd; ++cur, ++prev) {
   1975     uint64_t offset = 0x0;
   1976     switch ((*prev)->kind()) {
   1977       case LDFileFormat::Null:
   1978         offset = sectionStartOffset();
   1979         break;
   1980       case LDFileFormat::BSS:
   1981         offset = (*prev)->offset();
   1982         break;
   1983       default:
   1984         offset = (*prev)->offset() + (*prev)->size();
   1985         break;
   1986     }
   1987 
   1988     alignAddress(offset, (*cur)->align());
   1989     (*cur)->setOffset(offset);
   1990   }
   1991 }
   1992 
   1993 /// setOutputSectionOffset - helper function to set output sections' address
   1994 void GNULDBackend::setOutputSectionAddress(Module& pModule,
   1995                                            Module::iterator pSectBegin,
   1996                                            Module::iterator pSectEnd)
   1997 {
   1998   if (pSectBegin == pModule.end())
   1999     return;
   2000 
   2001   assert(pSectEnd == pModule.end() ||
   2002          (pSectEnd != pModule.end() &&
   2003           (*pSectBegin)->index() <= (*pSectEnd)->index()));
   2004 
   2005   const LinkerScript& script = pModule.getScript();
   2006   uint64_t seg_start_addr = getSegmentStartAddr(script);
   2007   for (ELFSegmentFactory::iterator seg = elfSegmentTable().begin(),
   2008          segEnd = elfSegmentTable().end(), prev = elfSegmentTable().end();
   2009        seg != segEnd; prev = seg, ++seg) {
   2010     if (llvm::ELF::PT_LOAD != (*seg).type())
   2011       continue;
   2012 
   2013     uint64_t start_addr = 0x0;
   2014     LinkerScript::AddressMap::const_iterator mapping;
   2015     if ((*seg).front()->kind() == LDFileFormat::Null)
   2016       mapping = script.addressMap().find(".text");
   2017     else if ((*seg).isDataSegment())
   2018       mapping = script.addressMap().find(".data");
   2019     else if ((*seg).isBssSegment())
   2020       mapping = script.addressMap().find(".bss");
   2021     else
   2022       mapping = script.addressMap().find((*seg).front()->name());
   2023 
   2024     if (mapping != script.addressMap().end()) {
   2025       // use address mapping in script options
   2026       start_addr = mapping.getEntry()->value();
   2027     }
   2028     else {
   2029       if ((*seg).front()->kind() == LDFileFormat::Null) {
   2030         // 1st PT_LOAD
   2031         start_addr = seg_start_addr;
   2032       }
   2033       else if ((*prev).front()->kind() == LDFileFormat::Null) {
   2034         // prev segment is 1st PT_LOAD
   2035         start_addr = seg_start_addr + (*seg).front()->offset();
   2036       }
   2037       else {
   2038         // Others
   2039         start_addr = (*prev).front()->addr() + (*seg).front()->offset();
   2040       }
   2041       // Try to align p_vaddr at page boundary if not in script options.
   2042       // To do so will add more padding in file, but can save one page
   2043       // at runtime.
   2044       alignAddress(start_addr, (*seg).align());
   2045     }
   2046 
   2047     // in p75,http://www.sco.com/developers/devspecs/gabi41.pdf
   2048     // p_align: As "Program Loading" describes in this chapter of the
   2049     // processor supplement, loadable process segments must have congruent
   2050     // values for p_vaddr and p_offset, modulo the page size.
   2051     if ((start_addr & ((*seg).align() - 1)) !=
   2052         ((*seg).front()->offset() & ((*seg).align() - 1))) {
   2053       uint64_t padding = (*seg).align() +
   2054                          (start_addr & ((*seg).align() - 1)) -
   2055                          ((*seg).front()->offset() & ((*seg).align() - 1));
   2056       setOutputSectionOffset(pModule,
   2057                              pModule.begin() + (*seg).front()->index(),
   2058                              pModule.end(),
   2059                              (*seg).front()->offset() + padding);
   2060       if (config().options().hasRelro())
   2061         setupRelro(pModule);
   2062     }
   2063 
   2064     for (ELFSegment::sect_iterator sect = (*seg).begin(),
   2065            sectEnd = (*seg).end(); sect != sectEnd; ++sect) {
   2066       if ((*sect)->index() < (*pSectBegin)->index())
   2067         continue;
   2068 
   2069       if (LDFileFormat::Null == (*sect)->kind())
   2070         continue;
   2071 
   2072       if (sect == pSectEnd)
   2073         return;
   2074 
   2075       if (sect != (*seg).begin())
   2076         (*sect)->setAddr(start_addr + (*sect)->offset() -
   2077                          (*seg).front()->offset());
   2078       else
   2079         (*sect)->setAddr(start_addr);
   2080     }
   2081   }
   2082 }
   2083 
   2084 /// layout - layout method
   2085 void GNULDBackend::layout(Module& pModule)
   2086 {
   2087   std::vector<SHOEntry> output_list;
   2088   // 1. determine what sections will go into final output, and push the needed
   2089   // sections into output_list for later processing
   2090   for (Module::iterator it = pModule.begin(), ie = pModule.end(); it != ie;
   2091        ++it) {
   2092     switch ((*it)->kind()) {
   2093     // take NULL and StackNote directly
   2094     case LDFileFormat::Null:
   2095     case LDFileFormat::StackNote:
   2096       output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
   2097       break;
   2098     // ignore if section size is 0
   2099     case LDFileFormat::EhFrame:
   2100       if (((*it)->size() != 0) ||
   2101           ((*it)->hasEhFrame() &&
   2102            config().codeGenType() == LinkerConfig::Object))
   2103         output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
   2104       break;
   2105     case LDFileFormat::Relocation:
   2106       if (((*it)->size() != 0) ||
   2107           ((*it)->hasRelocData() &&
   2108            config().codeGenType() == LinkerConfig::Object))
   2109         output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
   2110       break;
   2111     case LDFileFormat::Regular:
   2112     case LDFileFormat::Target:
   2113     case LDFileFormat::MetaData:
   2114     case LDFileFormat::BSS:
   2115     case LDFileFormat::Debug:
   2116     case LDFileFormat::GCCExceptTable:
   2117     case LDFileFormat::Note:
   2118     case LDFileFormat::NamePool:
   2119     case LDFileFormat::EhFrameHdr:
   2120       if (((*it)->size() != 0) ||
   2121           ((*it)->hasSectionData() &&
   2122            config().codeGenType() == LinkerConfig::Object))
   2123         output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
   2124       break;
   2125     case LDFileFormat::Group:
   2126       if (LinkerConfig::Object == config().codeGenType()) {
   2127         //TODO: support incremental linking
   2128         ;
   2129       }
   2130       break;
   2131     case LDFileFormat::Version:
   2132       if (0 != (*it)->size()) {
   2133         output_list.push_back(std::make_pair(*it, getSectionOrder(**it)));
   2134         warning(diag::warn_unsupported_symbolic_versioning) << (*it)->name();
   2135       }
   2136       break;
   2137     default:
   2138       if (0 != (*it)->size()) {
   2139         error(diag::err_unsupported_section) << (*it)->name() << (*it)->kind();
   2140       }
   2141       break;
   2142     }
   2143   } // end of for
   2144 
   2145   // 2. sort output section orders
   2146   std::stable_sort(output_list.begin(), output_list.end(), SHOCompare());
   2147 
   2148   // 3. update output sections in Module
   2149   pModule.getSectionTable().clear();
   2150   for(size_t index = 0; index < output_list.size(); ++index) {
   2151     (output_list[index].first)->setIndex(index);
   2152     pModule.getSectionTable().push_back(output_list[index].first);
   2153   }
   2154 
   2155   // 4. create program headers
   2156   if (LinkerConfig::Object != config().codeGenType()) {
   2157     createProgramHdrs(pModule);
   2158   }
   2159 
   2160   // 5. set output section offset
   2161   setOutputSectionOffset(pModule, pModule.begin(), pModule.end(), 0x0);
   2162 }
   2163 
   2164 /// preLayout - Backend can do any needed modification before layout
   2165 void GNULDBackend::preLayout(Module& pModule, IRBuilder& pBuilder)
   2166 {
   2167   // prelayout target first
   2168   doPreLayout(pBuilder);
   2169 
   2170   if (LinkerConfig::Object != config().codeGenType() &&
   2171       config().options().hasEhFrameHdr() && getOutputFormat()->hasEhFrame()) {
   2172     // init EhFrameHdr and size the output section
   2173     ELFFileFormat* format = getOutputFormat();
   2174     m_pEhFrameHdr = new EhFrameHdr(format->getEhFrameHdr(),
   2175                                    format->getEhFrame());
   2176     m_pEhFrameHdr->sizeOutput();
   2177   }
   2178 
   2179   // change .tbss and .tdata section symbol from Local to LocalDyn category
   2180   if (NULL != f_pTDATA)
   2181     pModule.getSymbolTable().changeToDynamic(*f_pTDATA);
   2182 
   2183   if (NULL != f_pTBSS)
   2184     pModule.getSymbolTable().changeToDynamic(*f_pTBSS);
   2185 
   2186   // To merge input's relocation sections into output's relocation sections.
   2187   //
   2188   // If we are generating relocatables (-r), move input relocation sections
   2189   // to corresponding output relocation sections.
   2190   if (LinkerConfig::Object == config().codeGenType()) {
   2191     Module::obj_iterator input, inEnd = pModule.obj_end();
   2192     for (input = pModule.obj_begin(); input != inEnd; ++input) {
   2193       LDContext::sect_iterator rs, rsEnd = (*input)->context()->relocSectEnd();
   2194       for (rs = (*input)->context()->relocSectBegin(); rs != rsEnd; ++rs) {
   2195 
   2196         // get the output relocation LDSection with identical name.
   2197         LDSection* output_sect = pModule.getSection((*rs)->name());
   2198         if (NULL == output_sect) {
   2199           output_sect = LDSection::Create((*rs)->name(),
   2200                                           (*rs)->kind(),
   2201                                           (*rs)->type(),
   2202                                           (*rs)->flag());
   2203 
   2204           output_sect->setAlign((*rs)->align());
   2205           pModule.getSectionTable().push_back(output_sect);
   2206         }
   2207 
   2208         // set output relocation section link
   2209         const LDSection* input_link = (*rs)->getLink();
   2210         assert(NULL != input_link && "Illegal input relocation section.");
   2211 
   2212         // get the linked output section
   2213         LDSection* output_link = pModule.getSection(input_link->name());
   2214         assert(NULL != output_link);
   2215 
   2216         output_sect->setLink(output_link);
   2217 
   2218         // get output relcoationData, create one if not exist
   2219         if (!output_sect->hasRelocData())
   2220           IRBuilder::CreateRelocData(*output_sect);
   2221 
   2222         RelocData* out_reloc_data = output_sect->getRelocData();
   2223 
   2224         // move relocations from input's to output's RelcoationData
   2225         RelocData::RelocationListType& out_list =
   2226                                              out_reloc_data->getRelocationList();
   2227         RelocData::RelocationListType& in_list =
   2228                                       (*rs)->getRelocData()->getRelocationList();
   2229         out_list.splice(out_list.end(), in_list);
   2230 
   2231         // size output
   2232         if (llvm::ELF::SHT_REL == output_sect->type())
   2233           output_sect->setSize(out_reloc_data->size() * getRelEntrySize());
   2234         else if (llvm::ELF::SHT_RELA == output_sect->type())
   2235           output_sect->setSize(out_reloc_data->size() * getRelaEntrySize());
   2236         else {
   2237           fatal(diag::unknown_reloc_section_type) << output_sect->type()
   2238                                                   << output_sect->name();
   2239         }
   2240       } // end of for each relocation section
   2241     } // end of for each input
   2242   } // end of if
   2243 
   2244   // set up the section flag of .note.GNU-stack section
   2245   setupGNUStackInfo(pModule);
   2246 }
   2247 
   2248 /// postLayout - Backend can do any needed modification after layout
   2249 void GNULDBackend::postLayout(Module& pModule, IRBuilder& pBuilder)
   2250 {
   2251   // 1. set up section address and segment attributes
   2252   if (LinkerConfig::Object != config().codeGenType()) {
   2253     if (config().options().hasRelro()) {
   2254       // 1.1 set up the offset constraint of PT_RELRO
   2255       setupRelro(pModule);
   2256     }
   2257 
   2258     // 1.2 set up the output sections' address
   2259     setOutputSectionAddress(pModule, pModule.begin(), pModule.end());
   2260 
   2261     // 1.3 do relaxation
   2262     relax(pModule, pBuilder);
   2263 
   2264     // 1.4 set up the attributes of program headers
   2265     setupProgramHdrs(pModule.getScript());
   2266   }
   2267 
   2268   // 2. target specific post layout
   2269   doPostLayout(pModule, pBuilder);
   2270 }
   2271 
   2272 void GNULDBackend::postProcessing(MemoryArea& pOutput)
   2273 {
   2274   if (LinkerConfig::Object != config().codeGenType() &&
   2275       config().options().hasEhFrameHdr() && getOutputFormat()->hasEhFrame()) {
   2276     // emit eh_frame_hdr
   2277     m_pEhFrameHdr->emitOutput<32>(pOutput);
   2278   }
   2279 }
   2280 
   2281 /// getHashBucketCount - calculate hash bucket count.
   2282 /// @ref Google gold linker, dynobj.cc:791
   2283 unsigned GNULDBackend::getHashBucketCount(unsigned pNumOfSymbols,
   2284                                           bool pIsGNUStyle)
   2285 {
   2286   // @ref Google gold, dynobj.cc:loc 791
   2287   static const unsigned int buckets[] =
   2288   {
   2289     1, 3, 17, 37, 67, 97, 131, 197, 263, 521, 1031, 2053, 4099, 8209,
   2290     16411, 32771, 65537, 131101, 262147
   2291   };
   2292   const unsigned buckets_count = sizeof buckets / sizeof buckets[0];
   2293 
   2294   unsigned int result = 1;
   2295   for (unsigned i = 0; i < buckets_count; ++i) {
   2296     if (pNumOfSymbols < buckets[i])
   2297       break;
   2298     result = buckets[i];
   2299   }
   2300 
   2301   if (pIsGNUStyle && result < 2)
   2302     result = 2;
   2303 
   2304   return result;
   2305 }
   2306 
   2307 /// getGNUHashMaskbitslog2 - calculate the number of mask bits in log2
   2308 /// @ref binutils gold, dynobj.cc:1165
   2309 unsigned GNULDBackend::getGNUHashMaskbitslog2(unsigned pNumOfSymbols) const
   2310 {
   2311   uint32_t maskbitslog2 = 1;
   2312   for (uint32_t x = pNumOfSymbols >> 1; x != 0; x >>=1)
   2313     ++maskbitslog2;
   2314 
   2315   if (maskbitslog2 < 3)
   2316     maskbitslog2 = 5;
   2317   else if (((1U << (maskbitslog2 - 2)) & pNumOfSymbols) != 0)
   2318     maskbitslog2 += 3;
   2319   else
   2320     maskbitslog2 += 2;
   2321 
   2322   if (config().targets().bitclass() == 64 && maskbitslog2 == 5)
   2323     maskbitslog2 = 6;
   2324 
   2325   return maskbitslog2;
   2326 }
   2327 
   2328 /// isDynamicSymbol
   2329 /// @ref Google gold linker: symtab.cc:311
   2330 bool GNULDBackend::isDynamicSymbol(const LDSymbol& pSymbol)
   2331 {
   2332   // If a local symbol is in the LDContext's symbol table, it's a real local
   2333   // symbol. We should not add it
   2334   if (pSymbol.binding() == ResolveInfo::Local)
   2335     return false;
   2336 
   2337   // If we are building shared object, and the visibility is external, we
   2338   // need to add it.
   2339   if (LinkerConfig::DynObj == config().codeGenType() ||
   2340       LinkerConfig::Exec   == config().codeGenType() ||
   2341       LinkerConfig::Binary == config().codeGenType()) {
   2342     if (pSymbol.resolveInfo()->visibility() == ResolveInfo::Default ||
   2343         pSymbol.resolveInfo()->visibility() == ResolveInfo::Protected)
   2344       return true;
   2345   }
   2346   return false;
   2347 }
   2348 
   2349 /// isDynamicSymbol
   2350 /// @ref Google gold linker: symtab.cc:311
   2351 bool GNULDBackend::isDynamicSymbol(const ResolveInfo& pResolveInfo)
   2352 {
   2353   // If a local symbol is in the LDContext's symbol table, it's a real local
   2354   // symbol. We should not add it
   2355   if (pResolveInfo.binding() == ResolveInfo::Local)
   2356     return false;
   2357 
   2358   // If we are building shared object, and the visibility is external, we
   2359   // need to add it.
   2360   if (LinkerConfig::DynObj == config().codeGenType() ||
   2361       LinkerConfig::Exec   == config().codeGenType() ||
   2362       LinkerConfig::Binary == config().codeGenType()) {
   2363     if (pResolveInfo.visibility() == ResolveInfo::Default ||
   2364         pResolveInfo.visibility() == ResolveInfo::Protected)
   2365       return true;
   2366   }
   2367   return false;
   2368 }
   2369 
   2370 /// commonPageSize - the common page size of the target machine.
   2371 /// @ref gold linker: target.h:135
   2372 uint64_t GNULDBackend::commonPageSize() const
   2373 {
   2374   if (config().options().commPageSize() > 0)
   2375     return std::min(config().options().commPageSize(), abiPageSize());
   2376   else
   2377     return std::min(m_pInfo->commonPageSize(), abiPageSize());
   2378 }
   2379 
   2380 /// abiPageSize - the abi page size of the target machine.
   2381 /// @ref gold linker: target.h:125
   2382 uint64_t GNULDBackend::abiPageSize() const
   2383 {
   2384   if (config().options().maxPageSize() > 0)
   2385     return config().options().maxPageSize();
   2386   else
   2387     return m_pInfo->abiPageSize();
   2388 }
   2389 
   2390 /// isSymbolPreemtible - whether the symbol can be preemted by other
   2391 /// link unit
   2392 /// @ref Google gold linker, symtab.h:551
   2393 bool GNULDBackend::isSymbolPreemptible(const ResolveInfo& pSym) const
   2394 {
   2395   if (pSym.other() != ResolveInfo::Default)
   2396     return false;
   2397 
   2398   // This is because the codeGenType of pie is DynObj. And gold linker check
   2399   // the "shared" option instead.
   2400   if (config().options().isPIE())
   2401     return false;
   2402 
   2403   if (LinkerConfig::DynObj != config().codeGenType())
   2404     return false;
   2405 
   2406   if (config().options().Bsymbolic())
   2407     return false;
   2408 
   2409   // A local defined symbol should be non-preemptible.
   2410   // This issue is found when linking libstdc++ on freebsd. A R_386_GOT32
   2411   // relocation refers to a local defined symbol, and we should generate a
   2412   // relative dynamic relocation when applying the relocation.
   2413   if (pSym.isDefine() && pSym.binding() == ResolveInfo::Local)
   2414     return false;
   2415 
   2416   return true;
   2417 }
   2418 
   2419 /// symbolNeedsDynRel - return whether the symbol needs a dynamic relocation
   2420 /// @ref Google gold linker, symtab.h:645
   2421 bool GNULDBackend::symbolNeedsDynRel(const ResolveInfo& pSym,
   2422                                      bool pSymHasPLT,
   2423                                      bool isAbsReloc) const
   2424 {
   2425   // an undefined reference in the executables should be statically
   2426   // resolved to 0 and no need a dynamic relocation
   2427   if (pSym.isUndef() &&
   2428       !pSym.isDyn() &&
   2429       (LinkerConfig::Exec   == config().codeGenType() ||
   2430        LinkerConfig::Binary == config().codeGenType()))
   2431     return false;
   2432 
   2433   // An absolute symbol can be resolved directly if it is either local
   2434   // or we are linking statically. Otherwise it can still be overridden
   2435   // at runtime.
   2436   if (pSym.isAbsolute() &&
   2437       (pSym.binding() == ResolveInfo::Local || config().isCodeStatic()))
   2438     return false;
   2439   if (config().isCodeIndep() && isAbsReloc)
   2440     return true;
   2441   if (pSymHasPLT && ResolveInfo::Function == pSym.type())
   2442     return false;
   2443   if (!config().isCodeIndep() && pSymHasPLT)
   2444     return false;
   2445   if (pSym.isDyn() || pSym.isUndef() ||
   2446       isSymbolPreemptible(pSym))
   2447     return true;
   2448 
   2449   return false;
   2450 }
   2451 
   2452 /// symbolNeedsPLT - return whether the symbol needs a PLT entry
   2453 /// @ref Google gold linker, symtab.h:596
   2454 bool GNULDBackend::symbolNeedsPLT(const ResolveInfo& pSym) const
   2455 {
   2456   if (pSym.isUndef() &&
   2457       !pSym.isDyn() &&
   2458       LinkerConfig::DynObj != config().codeGenType())
   2459     return false;
   2460 
   2461   // An IndirectFunc symbol (i.e., STT_GNU_IFUNC) always needs a plt entry
   2462   if (pSym.type() == ResolveInfo::IndirectFunc)
   2463     return true;
   2464 
   2465   if (pSym.type() != ResolveInfo::Function)
   2466     return false;
   2467 
   2468   if (config().isCodeStatic())
   2469     return false;
   2470 
   2471   if (config().options().isPIE())
   2472     return false;
   2473 
   2474   return (pSym.isDyn() ||
   2475           pSym.isUndef() ||
   2476           isSymbolPreemptible(pSym));
   2477 }
   2478 
   2479 /// symbolHasFinalValue - return true if the symbol's value can be decided at
   2480 /// link time
   2481 /// @ref Google gold linker, Symbol::final_value_is_known
   2482 bool GNULDBackend::symbolFinalValueIsKnown(const ResolveInfo& pSym) const
   2483 {
   2484   // if the output is pic code or if not executables, symbols' value may change
   2485   // at runtime
   2486   // FIXME: CodeIndep() || LinkerConfig::Relocatable == CodeGenType
   2487   if (config().isCodeIndep() ||
   2488       (LinkerConfig::Exec != config().codeGenType() &&
   2489        LinkerConfig::Binary != config().codeGenType()))
   2490     return false;
   2491 
   2492   // if the symbol is from dynamic object, then its value is unknown
   2493   if (pSym.isDyn())
   2494     return false;
   2495 
   2496   // if the symbol is not in dynamic object and is not undefined, then its value
   2497   // is known
   2498   if (!pSym.isUndef())
   2499     return true;
   2500 
   2501   // if the symbol is undefined and not in dynamic objects, for example, a weak
   2502   // undefined symbol, then whether the symbol's final value can be known
   2503   // depends on whrther we're doing static link
   2504   return config().isCodeStatic();
   2505 }
   2506 
   2507 /// symbolNeedsCopyReloc - return whether the symbol needs a copy relocation
   2508 bool GNULDBackend::symbolNeedsCopyReloc(const Relocation& pReloc,
   2509                                         const ResolveInfo& pSym) const
   2510 {
   2511   // only the reference from dynamic executable to non-function symbol in
   2512   // the dynamic objects may need copy relocation
   2513   if (config().isCodeIndep() ||
   2514       !pSym.isDyn() ||
   2515       pSym.type() == ResolveInfo::Function ||
   2516       pSym.size() == 0)
   2517     return false;
   2518 
   2519   // check if the option -z nocopyreloc is given
   2520   if (config().options().hasNoCopyReloc())
   2521     return false;
   2522 
   2523   // TODO: Is this check necessary?
   2524   // if relocation target place is readonly, a copy relocation is needed
   2525   uint32_t flag = pReloc.targetRef().frag()->getParent()->getSection().flag();
   2526   if (0 == (flag & llvm::ELF::SHF_WRITE))
   2527     return true;
   2528 
   2529   return false;
   2530 }
   2531 
   2532 LDSymbol& GNULDBackend::getTDATASymbol()
   2533 {
   2534   assert(NULL != f_pTDATA);
   2535   return *f_pTDATA;
   2536 }
   2537 
   2538 const LDSymbol& GNULDBackend::getTDATASymbol() const
   2539 {
   2540   assert(NULL != f_pTDATA);
   2541   return *f_pTDATA;
   2542 }
   2543 
   2544 LDSymbol& GNULDBackend::getTBSSSymbol()
   2545 {
   2546   assert(NULL != f_pTBSS);
   2547   return *f_pTBSS;
   2548 }
   2549 
   2550 const LDSymbol& GNULDBackend::getTBSSSymbol() const
   2551 {
   2552   assert(NULL != f_pTBSS);
   2553   return *f_pTBSS;
   2554 }
   2555 
   2556 void GNULDBackend::checkAndSetHasTextRel(const LDSection& pSection)
   2557 {
   2558   if (m_bHasTextRel)
   2559     return;
   2560 
   2561   // if the target section of the dynamic relocation is ALLOCATE but is not
   2562   // writable, than we should set DF_TEXTREL
   2563   const uint32_t flag = pSection.flag();
   2564   if (0 == (flag & llvm::ELF::SHF_WRITE) && (flag & llvm::ELF::SHF_ALLOC))
   2565     m_bHasTextRel = true;
   2566 
   2567   return;
   2568 }
   2569 
   2570 /// initBRIslandFactory - initialize the branch island factory for relaxation
   2571 bool GNULDBackend::initBRIslandFactory()
   2572 {
   2573   if (NULL == m_pBRIslandFactory) {
   2574     m_pBRIslandFactory = new BranchIslandFactory(maxBranchOffset());
   2575   }
   2576   return true;
   2577 }
   2578 
   2579 /// initStubFactory - initialize the stub factory for relaxation
   2580 bool GNULDBackend::initStubFactory()
   2581 {
   2582   if (NULL == m_pStubFactory) {
   2583     m_pStubFactory = new StubFactory();
   2584   }
   2585   return true;
   2586 }
   2587 
   2588 bool GNULDBackend::relax(Module& pModule, IRBuilder& pBuilder)
   2589 {
   2590   if (!mayRelax())
   2591     return true;
   2592 
   2593   bool finished = true;
   2594   do {
   2595     if (doRelax(pModule, pBuilder, finished)) {
   2596       // If the sections (e.g., .text) are relaxed, the layout is also changed
   2597       // We need to do the following:
   2598 
   2599       // 1. set up the offset
   2600       setOutputSectionOffset(pModule, pModule.begin(), pModule.end());
   2601 
   2602       // 2. set up the offset constraint of PT_RELRO
   2603       if (config().options().hasRelro())
   2604         setupRelro(pModule);
   2605 
   2606       // 3. set up the output sections' address
   2607       setOutputSectionAddress(pModule, pModule.begin(), pModule.end());
   2608     }
   2609   } while (!finished);
   2610 
   2611   return true;
   2612 }
   2613 
   2614 bool GNULDBackend::DynsymCompare::needGNUHash(const LDSymbol& X) const
   2615 {
   2616   // FIXME: in bfd and gold linker, an undefined symbol might be hashed
   2617   // when the ouput is not PIC, if the symbol is referred by a non pc-relative
   2618   // reloc, and its value is set to the addr of the plt entry.
   2619   return !X.resolveInfo()->isUndef() && !X.isDyn();
   2620 }
   2621 
   2622 bool GNULDBackend::DynsymCompare::operator()(const LDSymbol* X,
   2623                                              const LDSymbol* Y) const
   2624 {
   2625   return !needGNUHash(*X) && needGNUHash(*Y);
   2626 }
   2627 
   2628