Home | History | Annotate | Download | only in Target
      1 //===- ELFEmulation.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/ELFEmulation.h>
     10 #include <mcld/LinkerScript.h>
     11 #include <mcld/LinkerConfig.h>
     12 #include <mcld/Script/InputSectDesc.h>
     13 
     14 #include <llvm/Support/Host.h>
     15 
     16 using namespace mcld;
     17 
     18 struct NameMap {
     19   const char* from; ///< the prefix of the input string. (match FROM*)
     20   const char* to;   ///< the output string.
     21   InputSectDesc::KeepPolicy policy; /// mark whether the input is kept in GC
     22 };
     23 
     24 static const NameMap map[] =
     25 {
     26   {".text*", ".text", InputSectDesc::NoKeep},
     27   {".rodata*", ".rodata", InputSectDesc::NoKeep},
     28   {".data.rel.ro.local*", ".data.rel.ro.local", InputSectDesc::NoKeep},
     29   {".data.rel.ro*", ".data.rel.ro", InputSectDesc::NoKeep},
     30   {".data*", ".data", InputSectDesc::NoKeep},
     31   {".bss*", ".bss", InputSectDesc::NoKeep},
     32   {".tdata*", ".tdata", InputSectDesc::NoKeep},
     33   {".tbss*", ".tbss", InputSectDesc::NoKeep},
     34   {".init", ".init", InputSectDesc::Keep},
     35   {".fini", ".fini", InputSectDesc::Keep},
     36   {".preinit_array*", ".preinit_array", InputSectDesc::Keep},
     37   {".init_array*", ".init_array", InputSectDesc::Keep},
     38   {".fini_array*", ".fini_array", InputSectDesc::Keep},
     39   // TODO: Support DT_INIT_ARRAY for all constructors?
     40   {".ctors*", ".ctors", InputSectDesc::Keep},
     41   {".dtors*", ".dtors", InputSectDesc::Keep},
     42   {".jcr", ".jcr", InputSectDesc::Keep},
     43   // FIXME: in GNU ld, if we are creating a shared object .sdata2 and .sbss2
     44   // sections would be handled differently.
     45   {".sdata2*", ".sdata", InputSectDesc::NoKeep},
     46   {".sbss2*", ".sbss", InputSectDesc::NoKeep},
     47   {".sdata*", ".sdata", InputSectDesc::NoKeep},
     48   {".sbss*", ".sbss", InputSectDesc::NoKeep},
     49   {".lrodata*", ".lrodata", InputSectDesc::NoKeep},
     50   {".ldata*", ".ldata", InputSectDesc::NoKeep},
     51   {".lbss*", ".lbss", InputSectDesc::NoKeep},
     52   {".gcc_except_table*", ".gcc_except_table", InputSectDesc::Keep},
     53   {".gnu.linkonce.d.rel.ro.local*", ".data.rel.ro.local", InputSectDesc::NoKeep},
     54   {".gnu.linkonce.d.rel.ro*", ".data.rel.ro", InputSectDesc::NoKeep},
     55   {".gnu.linkonce.r*", ".rodata", InputSectDesc::NoKeep},
     56   {".gnu.linkonce.d*", ".data", InputSectDesc::NoKeep},
     57   {".gnu.linkonce.b*", ".bss", InputSectDesc::NoKeep},
     58   {".gnu.linkonce.sb2*", ".sbss", InputSectDesc::NoKeep},
     59   {".gnu.linkonce.sb*", ".sbss", InputSectDesc::NoKeep},
     60   {".gnu.linkonce.s2*", ".sdata", InputSectDesc::NoKeep},
     61   {".gnu.linkonce.s*", ".sdata", InputSectDesc::NoKeep},
     62   {".gnu.linkonce.wi*", ".debug_info", InputSectDesc::NoKeep},
     63   {".gnu.linkonce.td*", ".tdata", InputSectDesc::NoKeep},
     64   {".gnu.linkonce.tb*", ".tbss", InputSectDesc::NoKeep},
     65   {".gnu.linkonce.t*", ".text", InputSectDesc::NoKeep},
     66   {".gnu.linkonce.lr*", ".lrodata", InputSectDesc::NoKeep},
     67   {".gnu.linkonce.lb*", ".lbss", InputSectDesc::NoKeep},
     68   {".gnu.linkonce.l*", ".ldata", InputSectDesc::NoKeep},
     69 };
     70 
     71 bool mcld::MCLDEmulateELF(LinkerScript& pScript, LinkerConfig& pConfig)
     72 // FIXME: LinkerConfig& pConfig should be constant
     73 {
     74   // set up section map
     75   if (pConfig.options().getScriptList().empty() &&
     76       pConfig.codeGenType() != LinkerConfig::Object) {
     77     const unsigned int map_size =  (sizeof(map) / sizeof(map[0]) );
     78     for (unsigned int i = 0; i < map_size; ++i) {
     79       std::pair<SectionMap::mapping, bool> res =
     80         pScript.sectionMap().insert(map[i].from, map[i].to, map[i].policy);
     81       if (!res.second)
     82         return false;
     83     }
     84   } else {
     85     // FIXME: this is the hack to help assignment processing in current
     86     // implementation.
     87     pScript.sectionMap().insert("", "");
     88   }
     89 
     90   if (!pConfig.options().nostdlib()) {
     91     // TODO: check if user sets the default search path instead via -Y option
     92     // set up default search path
     93     switch (pConfig.targets().triple().getOS()) {
     94       case llvm::Triple::NetBSD:
     95         pScript.directories().insert("=/usr/lib");
     96         break;
     97       case llvm::Triple::MinGW32:
     98         pScript.directories().insert("=/mingw/lib");
     99         break;
    100       default:
    101         pScript.directories().insert("=/lib");
    102         pScript.directories().insert("=/usr/lib");
    103         break;
    104     }
    105   }
    106   return true;
    107 }
    108 
    109