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