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