Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1 | //===- llvm-mcld.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 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 9 | #include <mcld/Module.h> |
| 10 | #include <mcld/LinkerConfig.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 11 | #include <mcld/Target/TargetMachine.h> |
| 12 | #include <mcld/Support/TargetSelect.h> |
| 13 | #include <mcld/Support/TargetRegistry.h> |
| 14 | #include <mcld/Support/CommandLine.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 15 | #include <mcld/Support/Path.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 16 | #include <mcld/Support/RealPath.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 17 | #include <mcld/Support/MsgHandling.h> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 18 | #include <mcld/Support/FileHandle.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 19 | #include <mcld/Support/FileSystem.h> |
| 20 | #include <mcld/Support/raw_ostream.h> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 21 | #include <mcld/Support/ToolOutputFile.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 22 | #include <mcld/LD/DiagnosticLineInfo.h> |
| 23 | #include <mcld/LD/TextDiagnosticPrinter.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 24 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 25 | #include <llvm/PassManager.h> |
| 26 | #include <llvm/Pass.h> |
Shih-wei Liao | 86036a3 | 2013-01-14 19:16:03 -0800 | [diff] [blame^] | 27 | #include <llvm/IR/Module.h> |
| 28 | #include <llvm/IR/DataLayout.h> |
| 29 | #include <llvm/IR/LLVMContext.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 30 | #include <llvm/ADT/Triple.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 31 | #include <llvm/MC/SubtargetFeature.h> |
| 32 | #include <llvm/Support/CommandLine.h> |
| 33 | #include <llvm/Support/Debug.h> |
| 34 | #include <llvm/Support/FormattedStream.h> |
| 35 | #include <llvm/Support/Host.h> |
| 36 | #include <llvm/Support/IRReader.h> |
| 37 | #include <llvm/Support/ManagedStatic.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 38 | #include <llvm/Support/Signals.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 39 | #include <llvm/Support/TargetRegistry.h> |
| 40 | #include <llvm/Support/TargetSelect.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 41 | #include <llvm/Support/Process.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 42 | #include <llvm/Target/TargetMachine.h> |
| 43 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 44 | #if defined(HAVE_UNISTD_H) |
| 45 | # include <unistd.h> |
| 46 | #endif |
| 47 | |
| 48 | #if defined(_MSC_VER) |
| 49 | #include <io.h> |
| 50 | #ifndef STDIN_FILENO |
| 51 | # define STDIN_FILENO 0 |
| 52 | #endif |
| 53 | #ifndef STDOUT_FILENO |
| 54 | # define STDOUT_FILENO 1 |
| 55 | #endif |
| 56 | #ifndef STDERR_FILENO |
| 57 | # define STDERR_FILENO 2 |
| 58 | #endif |
| 59 | #endif |
| 60 | |
| 61 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 62 | using namespace llvm; |
| 63 | |
| 64 | #ifdef ENABLE_UNITTEST |
| 65 | #include <gtest.h> |
| 66 | |
| 67 | static cl::opt<bool> |
| 68 | UnitTest("unittest", cl::desc("do unit test") ); |
| 69 | |
| 70 | int unit_test( int argc, char* argv[] ) |
| 71 | { |
| 72 | testing::InitGoogleTest( &argc, argv ); |
| 73 | return RUN_ALL_TESTS(); |
| 74 | } |
| 75 | |
| 76 | #endif |
| 77 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 78 | // General options for llc. Other pass-specific options are specified |
| 79 | // within the corresponding llc passes, and target-specific options |
| 80 | // and back-end code generation options are specified with the target machine. |
| 81 | // |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 82 | // Determine optimization level. |
| 83 | static cl::opt<char> |
| 84 | OptLevel("O", |
| 85 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 86 | "(default = '-O2')"), |
| 87 | cl::Prefix, |
| 88 | cl::ZeroOrMore, |
| 89 | cl::init(' ')); |
| 90 | |
| 91 | static cl::opt<std::string> |
| 92 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 93 | |
| 94 | static cl::opt<std::string> |
| 95 | MArch("march", cl::desc("Architecture to generate code for (see --version)")); |
| 96 | |
| 97 | static cl::opt<std::string> |
| 98 | MCPU("mcpu", |
| 99 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 100 | cl::value_desc("cpu-name"), |
| 101 | cl::init("")); |
| 102 | |
| 103 | static cl::list<std::string> |
| 104 | MAttrs("mattr", |
| 105 | cl::CommaSeparated, |
| 106 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 107 | cl::value_desc("a1,+a2,-a3,...")); |
| 108 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 109 | static cl::opt<llvm::CodeModel::Model> |
| 110 | CMModel("code-model", |
| 111 | cl::desc("Choose code model"), |
| 112 | cl::init(CodeModel::Default), |
| 113 | cl::values(clEnumValN(CodeModel::Default, "default", |
| 114 | "Target default code model"), |
| 115 | clEnumValN(CodeModel::Small, "small", |
| 116 | "Small code model"), |
| 117 | clEnumValN(CodeModel::Kernel, "kernel", |
| 118 | "Kernel code model"), |
| 119 | clEnumValN(CodeModel::Medium, "medium", |
| 120 | "Medium code model"), |
| 121 | clEnumValN(CodeModel::Large, "large", |
| 122 | "Large code model"), |
| 123 | clEnumValEnd)); |
| 124 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 125 | cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
| 126 | cl::desc("Do not verify input module")); |
| 127 | |
| 128 | static cl::opt<bool> |
| 129 | EnableFPMAD("enable-fp-mad", |
| 130 | cl::desc("Enable less precise MAD instructions to be generated"), |
| 131 | cl::init(false)); |
| 132 | |
| 133 | static cl::opt<bool> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 134 | DisableFPElim("disable-fp-elim", |
| 135 | cl::desc("Disable frame pointer elimination optimization"), |
| 136 | cl::init(false)); |
| 137 | |
| 138 | static cl::opt<bool> |
| 139 | DisableFPElimNonLeaf("disable-non-leaf-fp-elim", |
| 140 | cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"), |
| 141 | cl::init(false)); |
| 142 | |
Shih-wei Liao | cedee4b | 2012-08-02 23:13:03 -0700 | [diff] [blame] | 143 | static cl::opt<llvm::FPOpFusion::FPOpFusionMode> |
| 144 | FuseFPOps("fuse-fp-ops", |
| 145 | cl::desc("Enable aggresive formation of fused FP ops"), |
| 146 | cl::init(FPOpFusion::Standard), |
| 147 | cl::values( |
| 148 | clEnumValN(FPOpFusion::Fast, "fast", |
| 149 | "Fuse FP ops whenever profitable"), |
| 150 | clEnumValN(FPOpFusion::Standard, "standard", |
| 151 | "Only fuse 'blessed' FP ops."), |
| 152 | clEnumValN(FPOpFusion::Strict, "strict", |
| 153 | "Only fuse FP ops when the result won't be effected."), |
| 154 | clEnumValEnd)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 155 | |
| 156 | static cl::opt<bool> |
| 157 | EnableUnsafeFPMath("enable-unsafe-fp-math", |
| 158 | cl::desc("Enable optimizations that may decrease FP precision"), |
| 159 | cl::init(false)); |
| 160 | |
| 161 | static cl::opt<bool> |
| 162 | EnableNoInfsFPMath("enable-no-infs-fp-math", |
| 163 | cl::desc("Enable FP math optimizations that assume no +-Infs"), |
| 164 | cl::init(false)); |
| 165 | |
| 166 | static cl::opt<bool> |
| 167 | EnableNoNaNsFPMath("enable-no-nans-fp-math", |
| 168 | cl::desc("Enable FP math optimizations that assume no NaNs"), |
| 169 | cl::init(false)); |
| 170 | |
| 171 | static cl::opt<bool> |
| 172 | EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math", |
| 173 | cl::Hidden, |
| 174 | cl::desc("Force codegen to assume rounding mode can change dynamically"), |
| 175 | cl::init(false)); |
| 176 | |
| 177 | static cl::opt<bool> |
| 178 | GenerateSoftFloatCalls("soft-float", |
| 179 | cl::desc("Generate software floating point library calls"), |
| 180 | cl::init(false)); |
| 181 | |
| 182 | static cl::opt<llvm::FloatABI::ABIType> |
| 183 | FloatABIForCalls("float-abi", |
| 184 | cl::desc("Choose float ABI type"), |
| 185 | cl::init(FloatABI::Default), |
| 186 | cl::values( |
| 187 | clEnumValN(FloatABI::Default, "default", |
| 188 | "Target default float ABI type"), |
| 189 | clEnumValN(FloatABI::Soft, "soft", |
| 190 | "Soft float ABI (implied by -soft-float)"), |
| 191 | clEnumValN(FloatABI::Hard, "hard", |
| 192 | "Hard float ABI (uses FP registers)"), |
| 193 | clEnumValEnd)); |
| 194 | |
| 195 | static cl::opt<bool> |
| 196 | DontPlaceZerosInBSS("nozero-initialized-in-bss", |
| 197 | cl::desc("Don't place zero-initialized symbols into bss section"), |
| 198 | cl::init(false)); |
| 199 | |
| 200 | static cl::opt<bool> |
| 201 | EnableJITExceptionHandling("jit-enable-eh", |
| 202 | cl::desc("Emit exception handling information"), |
| 203 | cl::init(false)); |
| 204 | |
| 205 | // In debug builds, make this default to true. |
| 206 | #ifdef NDEBUG |
| 207 | #define EMIT_DEBUG false |
| 208 | #else |
| 209 | #define EMIT_DEBUG true |
| 210 | #endif |
| 211 | static cl::opt<bool> |
| 212 | EmitJitDebugInfo("jit-emit-debug", |
| 213 | cl::desc("Emit debug information to debugger"), |
| 214 | cl::init(EMIT_DEBUG)); |
| 215 | #undef EMIT_DEBUG |
| 216 | |
| 217 | static cl::opt<bool> |
| 218 | EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", |
| 219 | cl::Hidden, |
| 220 | cl::desc("Emit debug info objfiles to disk"), |
| 221 | cl::init(false)); |
| 222 | |
| 223 | static cl::opt<bool> |
| 224 | EnableGuaranteedTailCallOpt("tailcallopt", |
| 225 | cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), |
| 226 | cl::init(false)); |
| 227 | |
| 228 | static cl::opt<unsigned> |
| 229 | OverrideStackAlignment("stack-alignment", |
| 230 | cl::desc("Override default stack alignment"), |
| 231 | cl::init(0)); |
| 232 | |
| 233 | static cl::opt<bool> |
| 234 | EnableRealignStack("realign-stack", |
| 235 | cl::desc("Realign stack if needed"), |
| 236 | cl::init(true)); |
| 237 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 238 | static cl::opt<std::string> |
| 239 | TrapFuncName("trap-func", cl::Hidden, |
| 240 | cl::desc("Emit a call to trap function rather than a trap instruction"), |
| 241 | cl::init("")); |
| 242 | |
| 243 | static cl::opt<bool> |
| 244 | SegmentedStacks("segmented-stacks", |
| 245 | cl::desc("Use segmented stacks if possible."), |
| 246 | cl::init(false)); |
| 247 | |
| 248 | //===----------------------------------------------------------------------===// |
| 249 | // Command Line Options |
| 250 | // There are four kinds of command line options: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 251 | // 1. Bitcode option. Used to represent a bitcode. |
| 252 | // 2. Attribute options. Attributes describes the input file after them. For |
| 253 | // example, --as-needed affects the input file after this option. Attribute |
| 254 | // options are not attributes. Attribute options are the options that is |
| 255 | // used to define a legal attribute. |
| 256 | // 3. Scripting options, Used to represent a subset of link scripting |
| 257 | // language, such as --defsym. |
| 258 | // 4. General options. (the rest of options) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 259 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 260 | // Bitcode Options |
| 261 | //===----------------------------------------------------------------------===// |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 262 | static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> > |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 263 | ArgBitcodeFilename("dB", |
| 264 | cl::desc("set default bitcode"), |
| 265 | cl::value_desc("bitcode")); |
| 266 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 267 | //===----------------------------------------------------------------------===// |
| 268 | // General Options |
| 269 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 270 | static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> > |
| 271 | ArgOutputFilename("o", |
| 272 | cl::desc("Output filename"), |
| 273 | cl::value_desc("filename")); |
| 274 | |
| 275 | static cl::alias |
| 276 | AliasOutputFilename("output", |
| 277 | cl::desc("alias for -o"), |
| 278 | cl::aliasopt(ArgOutputFilename)); |
| 279 | |
| 280 | static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> > |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 281 | ArgSysRoot("sysroot", |
| 282 | cl::desc("Use directory as the location of the sysroot, overriding the configure-time default."), |
| 283 | cl::value_desc("directory"), |
| 284 | cl::ValueRequired); |
| 285 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 286 | static cl::list<std::string, bool, llvm::cl::SearchDirParser> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 287 | ArgSearchDirList("L", |
| 288 | cl::ZeroOrMore, |
| 289 | cl::desc("Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts."), |
| 290 | cl::value_desc("searchdir"), |
| 291 | cl::Prefix); |
| 292 | |
| 293 | static cl::alias |
| 294 | ArgSearchDirListAlias("library-path", |
| 295 | cl::desc("alias for -L"), |
| 296 | cl::aliasopt(ArgSearchDirList)); |
| 297 | |
| 298 | static cl::opt<bool> |
| 299 | ArgTrace("t", |
| 300 | cl::desc("Print the names of the input files as ld processes them.")); |
| 301 | |
| 302 | static cl::alias |
| 303 | ArgTraceAlias("trace", |
| 304 | cl::desc("alias for -t"), |
| 305 | cl::aliasopt(ArgTrace)); |
| 306 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 307 | static cl::opt<int> |
| 308 | ArgVerbose("verbose", |
| 309 | cl::init(-1), |
| 310 | cl::desc("Display the version number for ld and list the linker emulations supported.")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 311 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 312 | static cl::opt<bool> |
| 313 | ArgVersion("V", |
| 314 | cl::init(false), |
| 315 | cl::desc("Display the version number for MCLinker.")); |
| 316 | |
| 317 | static cl::opt<int> |
| 318 | ArgMaxErrorNum("error-limit", |
| 319 | cl::init(-1), |
| 320 | cl::desc("limits the maximum number of erros.")); |
| 321 | |
| 322 | static cl::opt<int> |
| 323 | ArgMaxWarnNum("warning-limit", |
| 324 | cl::init(-1), |
| 325 | cl::desc("limits the maximum number of warnings.")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 326 | |
| 327 | static cl::opt<std::string> |
| 328 | ArgEntry("e", |
| 329 | cl::desc("Use entry as the explicit symbol for beginning execution of your program."), |
| 330 | cl::value_desc("entry"), |
| 331 | cl::ValueRequired); |
| 332 | |
| 333 | static cl::alias |
| 334 | ArgEntryAlias("entry", |
| 335 | cl::desc("alias for -e"), |
| 336 | cl::aliasopt(ArgEntry)); |
| 337 | |
| 338 | static cl::opt<bool> |
| 339 | ArgBsymbolic("Bsymbolic", |
| 340 | cl::desc("Bind references within the shared library."), |
| 341 | cl::init(false)); |
| 342 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 343 | static cl::opt<bool> |
| 344 | ArgBgroup("Bgroup", |
| 345 | cl::desc("Info the dynamic linker to perform lookups only inside the group."), |
| 346 | cl::init(false)); |
| 347 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 348 | static cl::opt<std::string> |
| 349 | ArgSOName("soname", |
| 350 | cl::desc("Set internal name of shared library"), |
| 351 | cl::value_desc("name")); |
| 352 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 353 | static cl::opt<bool> |
| 354 | ArgNoUndefined("no-undefined", |
| 355 | cl::desc("Do not allow unresolved references"), |
| 356 | cl::init(false)); |
| 357 | |
| 358 | static cl::opt<bool> |
| 359 | ArgAllowMulDefs("allow-multiple-definition", |
| 360 | cl::desc("Allow multiple definition"), |
| 361 | cl::init(false)); |
| 362 | |
| 363 | static cl::opt<bool> |
| 364 | ArgEhFrameHdr("eh-frame-hdr", |
| 365 | cl::desc("Request creation of \".eh_frame_hdr\" section and ELF \"PT_GNU_EH_FRAME\" segment header."), |
| 366 | cl::init(false)); |
| 367 | |
| 368 | static cl::list<mcld::ZOption, bool, llvm::cl::parser<mcld::ZOption> > |
| 369 | ArgZOptionList("z", |
| 370 | cl::ZeroOrMore, |
| 371 | cl::desc("The -z options for GNU ld compatibility."), |
| 372 | cl::value_desc("keyword"), |
| 373 | cl::Prefix); |
| 374 | |
| 375 | cl::opt<mcld::CodeGenFileType> |
| 376 | ArgFileType("filetype", cl::init(mcld::CGFT_EXEFile), |
| 377 | cl::desc("Choose a file type (not all types are supported by all targets):"), |
| 378 | cl::values( |
| 379 | clEnumValN(mcld::CGFT_ASMFile, "asm", |
| 380 | "Emit an assembly ('.s') file"), |
| 381 | clEnumValN(mcld::CGFT_OBJFile, "obj", |
| 382 | "Emit a relocatable object ('.o') file"), |
| 383 | clEnumValN(mcld::CGFT_DSOFile, "dso", |
| 384 | "Emit an dynamic shared object ('.so') file"), |
| 385 | clEnumValN(mcld::CGFT_EXEFile, "exe", |
| 386 | "Emit a executable ('.exe') file"), |
| 387 | clEnumValN(mcld::CGFT_NULLFile, "null", |
| 388 | "Emit nothing, for performance testing"), |
| 389 | clEnumValEnd)); |
| 390 | |
| 391 | static cl::opt<bool> |
| 392 | ArgShared("shared", |
| 393 | cl::desc("Create a shared library."), |
| 394 | cl::init(false)); |
| 395 | |
| 396 | static cl::alias |
| 397 | ArgSharedAlias("Bshareable", |
| 398 | cl::desc("alias for -shared"), |
| 399 | cl::aliasopt(ArgShared)); |
| 400 | |
| 401 | static cl::opt<bool> |
| 402 | ArgPIE("pie", |
| 403 | cl::desc("Emit a position-independent executable file"), |
| 404 | cl::init(false)); |
| 405 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 406 | static cl::opt<bool> |
| 407 | ArgRelocatable("relocatable", |
| 408 | cl::desc("Generate relocatable output"), |
| 409 | cl::init(false)); |
| 410 | |
| 411 | static cl::alias |
| 412 | ArgRelocatableAlias("r", |
| 413 | cl::desc("alias for --relocatable"), |
| 414 | cl::aliasopt(ArgRelocatable)); |
| 415 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 416 | static cl::opt<Reloc::Model> |
| 417 | ArgRelocModel("relocation-model", |
| 418 | cl::desc("Choose relocation model"), |
| 419 | cl::init(Reloc::Default), |
| 420 | cl::values( |
| 421 | clEnumValN(Reloc::Default, "default", |
| 422 | "Target default relocation model"), |
| 423 | clEnumValN(Reloc::Static, "static", |
| 424 | "Non-relocatable code"), |
| 425 | clEnumValN(Reloc::PIC_, "pic", |
| 426 | "Fully relocatable, position independent code"), |
| 427 | clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", |
| 428 | "Relocatable external references, non-relocatable code"), |
| 429 | clEnumValEnd)); |
| 430 | |
| 431 | static cl::opt<bool> |
| 432 | ArgFPIC("fPIC", |
| 433 | cl::desc("Set relocation model to pic. The same as -relocation-model=pic."), |
| 434 | cl::init(false)); |
| 435 | |
| 436 | static cl::opt<std::string> |
| 437 | ArgDyld("dynamic-linker", |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 438 | cl::ZeroOrMore, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 439 | cl::desc("Set the name of the dynamic linker."), |
| 440 | cl::value_desc("Program")); |
| 441 | |
| 442 | namespace color { |
| 443 | enum Color { |
| 444 | Never, |
| 445 | Always, |
| 446 | Auto |
| 447 | }; |
| 448 | } // namespace of color |
| 449 | |
| 450 | static cl::opt<color::Color> |
| 451 | ArgColor("color", |
| 452 | cl::value_desc("WHEN"), |
| 453 | cl::desc("Surround the result strings with the marker"), |
| 454 | cl::init(color::Auto), |
| 455 | cl::values( |
| 456 | clEnumValN(color::Never, "never", |
| 457 | "do not surround result strings"), |
| 458 | clEnumValN(color::Always, "always", |
| 459 | "always surround result strings, even the output is a plain file"), |
| 460 | clEnumValN(color::Auto, "auto", |
| 461 | "surround result strings only if the output is a tty"), |
| 462 | clEnumValEnd)); |
| 463 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 464 | /// @{ |
| 465 | /// @name FIXME: begin of unsupported options |
| 466 | /// @} |
| 467 | static cl::opt<bool> |
| 468 | ArgGCSections("gc-sections", |
| 469 | cl::desc("Enable garbage collection of unused input sections."), |
| 470 | cl::init(false)); |
| 471 | |
| 472 | static cl::opt<bool> |
| 473 | ArgNoGCSections("no-gc-sections", |
| 474 | cl::desc("disable garbage collection of unused input sections."), |
| 475 | cl::init(false)); |
| 476 | |
| 477 | namespace icf { |
| 478 | enum Mode { |
| 479 | None, |
| 480 | All, |
| 481 | Safe |
| 482 | }; |
| 483 | } // namespace of icf |
| 484 | |
| 485 | static cl::opt<icf::Mode> |
| 486 | ArgICF("icf", |
| 487 | cl::desc("Identical Code Folding"), |
| 488 | cl::init(icf::None), |
| 489 | cl::values( |
| 490 | clEnumValN(icf::None, "none", |
| 491 | "do not perform cold folding"), |
| 492 | clEnumValN(icf::All, "all", |
| 493 | "always preform cold folding"), |
| 494 | clEnumValN(icf::Safe, "safe", |
| 495 | "Folds ctors, dtors and functions whose pointers are definitely not taken."), |
| 496 | clEnumValEnd)); |
| 497 | |
| 498 | // FIXME: add this to target options? |
| 499 | static cl::opt<bool> |
| 500 | ArgFIXCA8("fix-cortex-a8", |
| 501 | cl::desc("Enable Cortex-A8 Thumb-2 branch erratum fix"), |
| 502 | cl::init(false)); |
| 503 | |
| 504 | static cl::opt<bool> |
| 505 | ArgDiscardLocals("discard-locals", |
| 506 | cl::desc("Delete all temporary local symbols."), |
| 507 | cl::init(false)); |
| 508 | |
| 509 | static cl::alias |
| 510 | ArgDiscardLocalsAlias("X", |
| 511 | cl::desc("alias for --discard-locals"), |
| 512 | cl::aliasopt(ArgDiscardLocals)); |
| 513 | |
| 514 | static cl::opt<bool> |
| 515 | ArgDiscardAll("discard-all", |
| 516 | cl::desc("Delete all local symbols."), |
| 517 | cl::init(false)); |
| 518 | |
| 519 | static cl::alias |
| 520 | ArgDiscardAllAlias("x", |
| 521 | cl::desc("alias for --discard-all"), |
| 522 | cl::aliasopt(ArgDiscardAll)); |
| 523 | |
| 524 | static cl::opt<bool> |
| 525 | ArgNMagic("nmagic", |
| 526 | cl::desc("Do not page align data"), |
| 527 | cl::init(false)); |
| 528 | |
| 529 | static cl::alias |
| 530 | ArgNMagicAlias("n", |
| 531 | cl::desc("alias for --nmagic"), |
| 532 | cl::aliasopt(ArgNMagic)); |
| 533 | |
| 534 | static cl::opt<bool> |
| 535 | ArgOMagic("omagic", |
| 536 | cl::desc("Do not page align data, do not make text readonly"), |
| 537 | cl::init(false)); |
| 538 | |
| 539 | static cl::alias |
| 540 | ArgOMagicAlias("N", |
| 541 | cl::desc("alias for --omagic"), |
| 542 | cl::aliasopt(ArgOMagic)); |
| 543 | |
| 544 | static cl::opt<bool> |
| 545 | ArgStripDebug("strip-debug", |
| 546 | cl::desc("Omit debugger symbol information from the output file."), |
| 547 | cl::init(false)); |
| 548 | |
| 549 | static cl::alias |
| 550 | ArgStripDebugAlias("S", |
| 551 | cl::desc("alias for --strip-debug"), |
| 552 | cl::aliasopt(ArgStripDebug)); |
| 553 | |
| 554 | static cl::opt<bool> |
| 555 | ArgExportDynamic("export-dynamic", |
| 556 | cl::desc("Export all dynamic symbols"), |
| 557 | cl::init(false)); |
| 558 | |
| 559 | static cl::alias |
| 560 | ArgExportDynamicAlias("E", |
| 561 | cl::desc("alias for --export-dynamic"), |
| 562 | cl::aliasopt(ArgExportDynamic)); |
| 563 | |
| 564 | static cl::opt<std::string> |
| 565 | ArgEmulation("m", |
| 566 | cl::desc("Set GNU linker emulation"), |
| 567 | cl::value_desc("emulation")); |
| 568 | |
| 569 | static cl::opt<std::string> |
| 570 | ArgRuntimePath("rpath", |
| 571 | cl::desc("Add a directory to the runtime library search path"), |
| 572 | cl::value_desc("dir")); |
| 573 | |
| 574 | static cl::opt<std::string> |
| 575 | ArgRuntimePathLink("rpath-link", |
| 576 | cl::desc("Add a directory to the link time library search path"), |
| 577 | cl::value_desc("dir")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 578 | |
| 579 | static cl::list<std::string> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 580 | ArgExcludeLIBS("exclude-libs", |
| 581 | cl::CommaSeparated, |
| 582 | cl::desc("Exclude libraries from automatic export"), |
| 583 | cl::value_desc("lib1,lib2,...")); |
| 584 | |
| 585 | static cl::opt<std::string> |
| 586 | ArgBuildID("build-id", |
| 587 | cl::desc("Request creation of \".note.gnu.build-id\" ELF note section."), |
| 588 | cl::value_desc("style")); |
| 589 | |
| 590 | static cl::opt<std::string> |
| 591 | ArgForceUndefined("u", |
| 592 | cl::desc("Force symbol to be undefined in the output file"), |
| 593 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 594 | |
| 595 | static cl::alias |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 596 | ArgForceUndefinedAlias("undefined", |
| 597 | cl::desc("alias for -u"), |
| 598 | cl::aliasopt(ArgForceUndefined)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 599 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 600 | static cl::opt<std::string> |
| 601 | ArgVersionScript("version-script", |
| 602 | cl::desc("Version script."), |
| 603 | cl::value_desc("Version script")); |
| 604 | |
| 605 | static cl::opt<bool> |
| 606 | ArgNoStdLib("nostdlib", |
| 607 | cl::desc("Only search lib dirs explicitly specified on cmdline"), |
| 608 | cl::init(false)); |
| 609 | |
| 610 | static cl::opt<bool> |
| 611 | ArgWarnCommon("warn-common", |
| 612 | cl::desc("warn common symbol"), |
| 613 | cl::init(false)); |
| 614 | |
| 615 | static cl::opt<bool> |
| 616 | ArgFatalWarnings("fatal-warnings", |
| 617 | cl::desc("turn all warnings into errors"), |
| 618 | cl::init(false)); |
| 619 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 620 | /// @{ |
| 621 | /// @name FIXME: end of unsupported options |
| 622 | /// @} |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 623 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 624 | static cl::opt<bool> |
| 625 | ArgWarnSharedTextrel("warn-shared-textrel", |
| 626 | cl::desc("Warn if adding DT_TEXTREL in a shared object."), |
| 627 | cl::init(false)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 628 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 629 | namespace format { |
| 630 | enum Format { |
| 631 | Binary, |
| 632 | Unknown // decided by triple |
| 633 | }; |
| 634 | } // namespace of format |
| 635 | |
| 636 | static cl::opt<format::Format> |
| 637 | ArgFormat("b", |
| 638 | cl::value_desc("Format"), |
| 639 | cl::desc("set input format"), |
| 640 | cl::init(format::Unknown), |
| 641 | cl::values( |
| 642 | clEnumValN(format::Binary, "binary", |
| 643 | "read in binary machine code."), |
| 644 | clEnumValEnd)); |
| 645 | |
| 646 | static cl::alias |
| 647 | ArgFormatAlias("format", |
| 648 | cl::desc("alias for -b"), |
| 649 | cl::aliasopt(ArgFormat)); |
| 650 | |
| 651 | static cl::opt<format::Format> |
| 652 | ArgOFormat("oformat", |
| 653 | cl::value_desc("Format"), |
| 654 | cl::desc("set output format"), |
| 655 | cl::init(format::Unknown), |
| 656 | cl::values( |
| 657 | clEnumValN(format::Binary, "binary", |
| 658 | "generate binary machine code."), |
| 659 | clEnumValEnd)); |
| 660 | |
| 661 | static cl::opt<bool> |
| 662 | ArgDefineCommon("d", |
| 663 | cl::ZeroOrMore, |
| 664 | cl::desc("Define common symbol"), |
| 665 | cl::init(false)); |
| 666 | |
| 667 | static cl::alias |
| 668 | ArgDefineCommonAlias1("dc", |
| 669 | cl::desc("alias for -d"), |
| 670 | cl::aliasopt(ArgDefineCommon)); |
| 671 | |
| 672 | static cl::alias |
| 673 | ArgDefineCommonAlias2("dp", |
| 674 | cl::desc("alias for -d"), |
| 675 | cl::aliasopt(ArgDefineCommon)); |
| 676 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 677 | //===----------------------------------------------------------------------===// |
| 678 | // Scripting Options |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 679 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 680 | static cl::list<std::string> |
| 681 | ArgWrapList("wrap", |
| 682 | cl::ZeroOrMore, |
| 683 | cl::desc("Use a wrap function fo symbol."), |
| 684 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 685 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 686 | static cl::list<std::string> |
| 687 | ArgPortList("portable", |
| 688 | cl::ZeroOrMore, |
| 689 | cl::desc("Use a portable function fo symbol."), |
| 690 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 691 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 692 | static cl::list<std::string> |
| 693 | ArgAddressMapList("section-start", |
| 694 | cl::ZeroOrMore, |
| 695 | cl::desc("Locate a output section at the given absolute address"), |
| 696 | cl::value_desc("Set address of section"), |
| 697 | cl::Prefix); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 698 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 699 | static cl::opt<unsigned long long> |
| 700 | ArgBssSegAddr("Tbss", |
| 701 | cl::desc("Set the address of the bss segment"), |
| 702 | cl::init(-1U)); |
| 703 | |
| 704 | static cl::opt<unsigned long long> |
| 705 | ArgDataSegAddr("Tdata", |
| 706 | cl::desc("Set the address of the data segment"), |
| 707 | cl::init(-1U)); |
| 708 | |
| 709 | static cl::opt<unsigned long long> |
| 710 | ArgTextSegAddr("Ttext", |
| 711 | cl::desc("Set the address of the text segment"), |
| 712 | cl::init(-1U)); |
| 713 | |
| 714 | //===----------------------------------------------------------------------===// |
| 715 | // non-member functions |
| 716 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 717 | /// GetOutputStream - get the output stream. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 718 | static mcld::ToolOutputFile *GetOutputStream(const char* pTargetName, |
| 719 | Triple::OSType pOSType, |
| 720 | mcld::CodeGenFileType pFileType, |
| 721 | const mcld::sys::fs::Path& pInputFilename, |
| 722 | mcld::sys::fs::Path& pOutputFilename) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 723 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 724 | if (pOutputFilename.empty()) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 725 | if (0 == pInputFilename.native().compare("-")) |
| 726 | pOutputFilename.assign("-"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 727 | else { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 728 | switch(pFileType) { |
| 729 | case mcld::CGFT_ASMFile: { |
| 730 | if (0 == pInputFilename.native().compare("-")) |
| 731 | pOutputFilename.assign("_out"); |
| 732 | else |
| 733 | pOutputFilename.assign(pInputFilename.stem().native()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 734 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 735 | if (0 == strcmp(pTargetName, "c")) |
| 736 | pOutputFilename.native() += ".cbe.c"; |
| 737 | else if (0 == strcmp(pTargetName, "cpp")) |
| 738 | pOutputFilename.native() += ".cpp"; |
| 739 | else |
| 740 | pOutputFilename.native() += ".s"; |
| 741 | } |
| 742 | break; |
| 743 | |
| 744 | case mcld::CGFT_OBJFile: { |
| 745 | if (0 == pInputFilename.native().compare("-")) |
| 746 | pOutputFilename.assign("_out"); |
| 747 | else |
| 748 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 749 | |
| 750 | if (pOSType == Triple::Win32) |
| 751 | pOutputFilename.native() += ".obj"; |
| 752 | else |
| 753 | pOutputFilename.native() += ".o"; |
| 754 | } |
| 755 | break; |
| 756 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 757 | case mcld::CGFT_PARTIAL: { |
| 758 | if (Triple::Win32 == pOSType) { |
| 759 | if (0 == pInputFilename.native().compare("-")) |
| 760 | pOutputFilename.assign("_out"); |
| 761 | else |
| 762 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 763 | pOutputFilename.native() += ".obj"; |
| 764 | } |
| 765 | else |
| 766 | pOutputFilename.assign("a.out"); |
| 767 | } |
| 768 | break; |
| 769 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 770 | case mcld::CGFT_DSOFile: { |
| 771 | if (Triple::Win32 == pOSType) { |
| 772 | if (0 == pInputFilename.native().compare("-")) |
| 773 | pOutputFilename.assign("_out"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 774 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 775 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 776 | pOutputFilename.native() += ".dll"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 777 | } |
| 778 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 779 | pOutputFilename.assign("a.out"); |
| 780 | } |
| 781 | break; |
| 782 | |
| 783 | case mcld::CGFT_EXEFile: { |
| 784 | if (Triple::Win32 == pOSType) { |
| 785 | if (0 == pInputFilename.native().compare("-")) |
| 786 | pOutputFilename.assign("_out"); |
| 787 | else |
| 788 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 789 | pOutputFilename.native() += ".exe"; |
| 790 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 791 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 792 | pOutputFilename.assign("a.out"); |
| 793 | } |
| 794 | break; |
| 795 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 796 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 797 | break; |
| 798 | default: |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 799 | llvm::report_fatal_error("Unknown output file type.\n"); |
| 800 | } // end of switch |
| 801 | } // end of ! pInputFilename == "-" |
| 802 | } // end of if empty pOutputFilename |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 803 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 804 | mcld::FileHandle::Permission permission; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 805 | switch (pFileType) { |
| 806 | default: assert(0 && "Unknown file type"); |
| 807 | case mcld::CGFT_ASMFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 808 | case mcld::CGFT_OBJFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 809 | case mcld::CGFT_PARTIAL: |
| 810 | permission = 0644; |
| 811 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 812 | case mcld::CGFT_DSOFile: |
| 813 | case mcld::CGFT_EXEFile: |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 814 | case mcld::CGFT_BINARY: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 815 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 816 | permission = 0755; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 817 | break; |
| 818 | } |
| 819 | |
| 820 | // Open the file. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 821 | mcld::ToolOutputFile* result_output = |
| 822 | new mcld::ToolOutputFile(pOutputFilename, |
| 823 | mcld::FileHandle::ReadWrite | |
| 824 | mcld::FileHandle::Create | |
| 825 | mcld::FileHandle::Truncate, |
| 826 | permission); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 827 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 828 | return result_output; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 829 | } |
| 830 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 831 | /// ParseProgName - Parse program name |
| 832 | /// This function simplifies cross-compiling by reading triple from the program |
| 833 | /// name. For example, if the program name is `arm-linux-eabi-ld.mcld', we can |
| 834 | /// get the triple is arm-linux-eabi by the program name. |
| 835 | static void ParseProgName(const char *progname) |
| 836 | { |
| 837 | static const char *suffixes[] = { |
| 838 | "ld", |
| 839 | "ld.mcld", |
| 840 | }; |
| 841 | |
| 842 | std::string ProgName(mcld::sys::fs::Path(progname).stem().native()); |
| 843 | |
| 844 | for (size_t i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { |
| 845 | if (ProgName == suffixes[i]) |
| 846 | return; |
| 847 | } |
| 848 | |
| 849 | StringRef ProgNameRef(ProgName); |
| 850 | StringRef Prefix; |
| 851 | |
| 852 | for (size_t i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { |
| 853 | if (!ProgNameRef.endswith(suffixes[i])) |
| 854 | continue; |
| 855 | |
| 856 | StringRef::size_type LastComponent = ProgNameRef.rfind('-', |
| 857 | ProgNameRef.size() - strlen(suffixes[i])); |
| 858 | if (LastComponent == StringRef::npos) |
| 859 | continue; |
| 860 | StringRef Prefix = ProgNameRef.slice(0, LastComponent); |
| 861 | std::string IgnoredError; |
| 862 | if (!llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) |
| 863 | continue; |
| 864 | TargetTriple = Prefix.str(); |
| 865 | return; |
| 866 | } |
| 867 | } |
| 868 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 869 | static bool ShouldColorize() |
| 870 | { |
| 871 | const char* term = getenv("TERM"); |
| 872 | return term && (0 != strcmp(term, "dumb")); |
| 873 | } |
| 874 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 875 | static bool ProcessLinkerOptionsFromCommand(mcld::LinkerConfig& pConfig) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 876 | // ----- Set up General Options ----- // |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 877 | // set up colorize |
| 878 | switch (ArgColor) { |
| 879 | case color::Never: |
| 880 | pConfig.options().setColor(false); |
| 881 | break; |
| 882 | case color::Always: |
| 883 | pConfig.options().setColor(true); |
| 884 | break; |
| 885 | case color::Auto: |
| 886 | bool color_option = ShouldColorize() && |
| 887 | llvm::sys::Process::FileDescriptorIsDisplayed(STDOUT_FILENO); |
| 888 | pConfig.options().setColor(color_option); |
| 889 | break; |
| 890 | } |
| 891 | |
| 892 | mcld::outs().setColor(pConfig.options().color()); |
| 893 | mcld::errs().setColor(pConfig.options().color()); |
| 894 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 895 | // set up soname |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 896 | pConfig.options().setSOName(ArgSOName); |
| 897 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 898 | // --fatal-warnings |
| 899 | pConfig.options().setFatalWarnings(ArgFatalWarnings); |
| 900 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 901 | // -shared or -pie |
| 902 | if (true == ArgShared || true == ArgPIE) { |
| 903 | ArgFileType = mcld::CGFT_DSOFile; |
| 904 | } |
| 905 | else if (true == ArgRelocatable) { |
| 906 | ArgFileType = mcld::CGFT_PARTIAL; |
| 907 | } |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 908 | else if (format::Binary == ArgOFormat) { |
| 909 | ArgFileType = mcld::CGFT_BINARY; |
| 910 | } |
| 911 | |
| 912 | // -b [input-format], --format=[input-format] |
| 913 | if (format::Binary == ArgFormat) |
| 914 | pConfig.options().setBinaryInput(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 915 | |
| 916 | // -V |
| 917 | if (ArgVersion) { |
| 918 | mcld::outs() << "MCLinker - " |
| 919 | << mcld::LinkerConfig::version() |
| 920 | << "\n"; |
| 921 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 922 | |
| 923 | // set up sysroot |
| 924 | if (!ArgSysRoot.empty()) { |
| 925 | if (exists(ArgSysRoot) && is_directory(ArgSysRoot)) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 926 | pConfig.options().setSysroot(ArgSysRoot); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 927 | } |
| 928 | |
| 929 | // add all search directories |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 930 | cl::list<std::string>::iterator sd; |
| 931 | cl::list<std::string>::iterator sdEnd = ArgSearchDirList.end(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 932 | for (sd=ArgSearchDirList.begin(); sd!=sdEnd; ++sd) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 933 | if (!pConfig.options().directories().insert(*sd)) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 934 | // FIXME: need a warning function |
| 935 | errs() << "WARNING: can not open search directory `-L" |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 936 | << *sd |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 937 | << "'.\n"; |
| 938 | } |
| 939 | } |
| 940 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 941 | pConfig.options().setPIE(ArgPIE); |
| 942 | pConfig.options().setTrace(ArgTrace); |
| 943 | pConfig.options().setVerbose(ArgVerbose); |
| 944 | pConfig.options().setMaxErrorNum(ArgMaxErrorNum); |
| 945 | pConfig.options().setMaxWarnNum(ArgMaxWarnNum); |
| 946 | pConfig.options().setEntry(ArgEntry); |
| 947 | pConfig.options().setBsymbolic(ArgBsymbolic); |
| 948 | pConfig.options().setBgroup(ArgBgroup); |
| 949 | pConfig.options().setDyld(ArgDyld); |
| 950 | pConfig.options().setNoUndefined(ArgNoUndefined); |
| 951 | pConfig.options().setMulDefs(ArgAllowMulDefs); |
| 952 | pConfig.options().setEhFrameHdr(ArgEhFrameHdr); |
| 953 | pConfig.options().setNMagic(ArgNMagic); |
| 954 | pConfig.options().setOMagic(ArgOMagic); |
| 955 | pConfig.options().setStripDebug(ArgStripDebug); |
| 956 | pConfig.options().setExportDynamic(ArgExportDynamic); |
| 957 | pConfig.options().setWarnSharedTextrel(ArgWarnSharedTextrel); |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 958 | pConfig.options().setDefineCommon(ArgDefineCommon); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 959 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 960 | // set up rename map, for --wrap |
| 961 | cl::list<std::string>::iterator wname; |
| 962 | cl::list<std::string>::iterator wnameEnd = ArgWrapList.end(); |
| 963 | for (wname = ArgWrapList.begin(); wname != wnameEnd; ++wname) { |
| 964 | bool exist = false; |
| 965 | |
| 966 | // add wname -> __wrap_wname |
| 967 | mcld::StringEntry<llvm::StringRef>* to_wrap = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 968 | pConfig.scripts().renameMap().insert(*wname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 969 | |
| 970 | std::string to_wrap_str = "__wrap_" + *wname; |
| 971 | to_wrap->setValue(to_wrap_str); |
| 972 | |
| 973 | if (exist) |
| 974 | mcld::warning(mcld::diag::rewrap) << *wname << to_wrap_str; |
| 975 | |
| 976 | // add __real_wname -> wname |
| 977 | std::string from_real_str = "__real_" + *wname; |
| 978 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 979 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 980 | from_real->setValue(*wname); |
| 981 | if (exist) |
| 982 | mcld::warning(mcld::diag::rewrap) << *wname << from_real_str; |
| 983 | } // end of for |
| 984 | |
| 985 | // set up rename map, for --portable |
| 986 | cl::list<std::string>::iterator pname; |
| 987 | cl::list<std::string>::iterator pnameEnd = ArgPortList.end(); |
| 988 | for (pname = ArgPortList.begin(); pname != pnameEnd; ++pname) { |
| 989 | bool exist = false; |
| 990 | |
| 991 | // add pname -> pname_portable |
| 992 | mcld::StringEntry<llvm::StringRef>* to_port = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 993 | pConfig.scripts().renameMap().insert(*pname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 994 | |
| 995 | std::string to_port_str = *pname + "_portable"; |
| 996 | to_port->setValue(to_port_str); |
| 997 | |
| 998 | if (exist) |
| 999 | mcld::warning(mcld::diag::rewrap) << *pname << to_port_str; |
| 1000 | |
| 1001 | // add __real_pname -> pname |
| 1002 | std::string from_real_str = "__real_" + *pname; |
| 1003 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1004 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1005 | |
| 1006 | from_real->setValue(*pname); |
| 1007 | if (exist) |
| 1008 | mcld::warning(mcld::diag::rewrap) << *pname << from_real_str; |
| 1009 | } // end of for |
| 1010 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1011 | // add -z options |
| 1012 | cl::list<mcld::ZOption>::iterator zOpt; |
| 1013 | cl::list<mcld::ZOption>::iterator zOptEnd = ArgZOptionList.end(); |
| 1014 | for (zOpt = ArgZOptionList.begin(); zOpt != zOptEnd; ++zOpt) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1015 | pConfig.options().addZOption(*zOpt); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1016 | } |
| 1017 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1018 | if (ArgGCSections) { |
| 1019 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgGCSections.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1022 | // set up icf mode |
| 1023 | switch (ArgICF) { |
| 1024 | case icf::None: |
| 1025 | break; |
| 1026 | case icf::All: |
| 1027 | case icf::Safe: |
| 1028 | default: |
| 1029 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgICF.ArgStr; |
| 1030 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1031 | } |
| 1032 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1033 | if (ArgFIXCA8) { |
| 1034 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgFIXCA8.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1035 | } |
| 1036 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1037 | if (ArgDiscardLocals) { |
| 1038 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgDiscardLocals.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1039 | } |
| 1040 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1041 | if (ArgDiscardAll) { |
| 1042 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgDiscardAll.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1045 | // add address mappings |
| 1046 | // -Ttext |
| 1047 | if (-1U != ArgTextSegAddr) { |
| 1048 | bool exist = false; |
| 1049 | mcld::StringEntry<uint64_t>* text_mapping = |
| 1050 | pConfig.scripts().addressMap().insert(".text", exist); |
| 1051 | text_mapping->setValue(ArgTextSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1052 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1053 | // -Tdata |
| 1054 | if (-1U != ArgDataSegAddr) { |
| 1055 | bool exist = false; |
| 1056 | mcld::StringEntry<uint64_t>* data_mapping = |
| 1057 | pConfig.scripts().addressMap().insert(".data", exist); |
| 1058 | data_mapping->setValue(ArgDataSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1059 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1060 | // -Tbss |
| 1061 | if (-1U != ArgBssSegAddr) { |
| 1062 | bool exist = false; |
| 1063 | mcld::StringEntry<uint64_t>* bss_mapping = |
| 1064 | pConfig.scripts().addressMap().insert(".bss", exist); |
| 1065 | bss_mapping->setValue(ArgBssSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1066 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1067 | // --section-start SECTION=ADDRESS |
| 1068 | for (cl::list<std::string>::iterator |
| 1069 | it = ArgAddressMapList.begin(), ie = ArgAddressMapList.end(); |
| 1070 | it != ie; ++it) { |
| 1071 | // FIXME: Add a cl::parser |
| 1072 | size_t pos = (*it).find_last_of('='); |
| 1073 | llvm::StringRef script(*it); |
| 1074 | uint64_t address = 0x0; |
| 1075 | script.substr(pos + 1).getAsInteger(0, address); |
| 1076 | bool exist = false; |
| 1077 | mcld::StringEntry<uint64_t>* addr_mapping = |
| 1078 | pConfig.scripts().addressMap().insert(script.substr(0, pos), exist); |
| 1079 | addr_mapping->setValue(address); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1082 | return true; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1083 | } |
| 1084 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1085 | int main(int argc, char* argv[]) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1086 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1087 | LLVMContext &Context = getGlobalContext(); |
| 1088 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1089 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1090 | // Initialize targets first, so that --version shows registered targets. |
| 1091 | InitializeAllTargets(); |
| 1092 | InitializeAllAsmPrinters(); |
| 1093 | InitializeAllAsmParsers(); |
| 1094 | InitializeAllTargetMCs(); |
| 1095 | mcld::InitializeAllTargets(); |
| 1096 | mcld::InitializeAllLinkers(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1097 | mcld::InitializeAllEmulations(); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1098 | mcld::InitializeAllDiagnostics(); |
| 1099 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1100 | ParseProgName(argv[0]); |
| 1101 | cl::ParseCommandLineOptions(argc, argv, "MCLinker\n"); |
| 1102 | |
| 1103 | #ifdef ENABLE_UNITTEST |
| 1104 | if (UnitTest) { |
| 1105 | return unit_test( argc, argv ); |
| 1106 | } |
| 1107 | #endif |
| 1108 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1109 | // Load the module to be compiled... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1110 | std::auto_ptr<llvm::Module> M; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1111 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1112 | // Load the module to be linked... |
| 1113 | mcld::Module LDIRModule; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1114 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1115 | mcld::LinkerConfig LDConfig; |
| 1116 | |
| 1117 | // Process the linker input from the command line |
| 1118 | if (!ProcessLinkerOptionsFromCommand(LDConfig)) { |
| 1119 | errs() << argv[0] << ": failed to process linker options from command line!\n"; |
| 1120 | return 1; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | if (ArgBitcodeFilename.empty() && |
| 1124 | (mcld::CGFT_DSOFile != ArgFileType && |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1125 | mcld::CGFT_EXEFile != ArgFileType && |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1126 | mcld::CGFT_PARTIAL != ArgFileType && |
| 1127 | mcld::CGFT_BINARY != ArgFileType)) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1128 | // If the file is not given, forcefully read from stdin |
| 1129 | if (ArgVerbose >= 0) { |
| 1130 | errs() << "** The bitcode/llvm asm file is not given. Read from stdin.\n" |
| 1131 | << "** Specify input bitcode/llvm asm file by\n\n" |
| 1132 | << " llvm-mcld -dB [the bitcode/llvm asm]\n\n"; |
| 1133 | } |
| 1134 | |
| 1135 | ArgBitcodeFilename.assign("-"); |
| 1136 | } |
| 1137 | |
| 1138 | if (!ArgBitcodeFilename.empty()) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1139 | SMDiagnostic Err; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1140 | M.reset(ParseIRFile(ArgBitcodeFilename.native(), Err, Context)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1141 | |
| 1142 | if (M.get() == 0) { |
| 1143 | Err.print(argv[0], errs()); |
| 1144 | errs() << "** Failed to to the given bitcode/llvm asm file '" |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1145 | << ArgBitcodeFilename.native() << "'. **\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1146 | return 1; |
| 1147 | } |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1148 | } |
| 1149 | else { |
| 1150 | // If here, output must be dynamic shared object (mcld::CGFT_DSOFile) and |
| 1151 | // executable file (mcld::CGFT_EXEFile). |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1152 | M.reset(new Module("Empty Module", Context)); |
| 1153 | } |
| 1154 | Module &mod = *M.get(); |
| 1155 | |
| 1156 | // If we are supposed to override the target triple, do so now. |
| 1157 | Triple TheTriple; |
| 1158 | if (!TargetTriple.empty()) { |
| 1159 | TheTriple.setTriple(TargetTriple); |
| 1160 | mod.setTargetTriple(TargetTriple); |
| 1161 | } |
| 1162 | |
| 1163 | // User doesn't specify the triple from command. |
| 1164 | if (TheTriple.getTriple().empty()) { |
| 1165 | // Try to get one from the input Module. |
| 1166 | const std::string &TripleStr = mod.getTargetTriple(); |
| 1167 | |
| 1168 | if (TripleStr.empty()) |
| 1169 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 1170 | else |
| 1171 | TheTriple.setTriple(TripleStr); |
| 1172 | } |
| 1173 | |
| 1174 | // Allocate target machine. First, check whether the user has explicitly |
| 1175 | // specified an architecture to compile for. If so we have to look it up by |
| 1176 | // name, because it might be a backend that has no mapping to a target triple. |
| 1177 | const mcld::Target *TheTarget = 0; |
| 1178 | if (!MArch.empty()) { |
| 1179 | for (mcld::TargetRegistry::iterator it = mcld::TargetRegistry::begin(), |
| 1180 | ie = mcld::TargetRegistry::end(); it != ie; ++it) { |
| 1181 | if (MArch == (*it)->get()->getName()) { |
| 1182 | TheTarget = *it; |
| 1183 | break; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | if (!TheTarget) { |
| 1188 | errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; |
| 1189 | return 1; |
| 1190 | } |
| 1191 | |
| 1192 | // Adjust the triple to match (if known), otherwise stick with the |
| 1193 | // module/host triple. |
| 1194 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); |
| 1195 | if (Type != Triple::UnknownArch) |
| 1196 | TheTriple.setArch(Type); |
| 1197 | } |
| 1198 | else { |
| 1199 | std::string Err; |
| 1200 | TheTarget = mcld::TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); |
| 1201 | if (TheTarget == 0) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1202 | errs() << "error: auto-selecting target `" << TheTriple.getTriple() |
| 1203 | << "'\n" |
| 1204 | << "Please use the -march option to explicitly select a target.\n" |
| 1205 | << "Example:\n" |
| 1206 | << " $ " << argv[0] << " -march=arm\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1207 | return 1; |
| 1208 | } |
| 1209 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1210 | // Set up mcld::LinkerConfig |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1211 | LDConfig.targets().setTriple(TheTriple); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1212 | |
| 1213 | // Package up features to be passed to target/subtarget |
| 1214 | std::string FeaturesStr; |
| 1215 | if (MAttrs.size()) { |
| 1216 | SubtargetFeatures Features; |
| 1217 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 1218 | Features.AddFeature(MAttrs[i]); |
| 1219 | FeaturesStr = Features.getString(); |
| 1220 | } |
| 1221 | |
| 1222 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 1223 | switch (OptLevel) { |
| 1224 | default: |
| 1225 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 1226 | return 1; |
| 1227 | case ' ': break; |
| 1228 | case '0': OLvl = CodeGenOpt::None; break; |
| 1229 | case '1': OLvl = CodeGenOpt::Less; break; |
| 1230 | case '2': OLvl = CodeGenOpt::Default; break; |
| 1231 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 1232 | } |
| 1233 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1234 | // set -fPIC |
| 1235 | if (ArgFPIC) |
| 1236 | ArgRelocModel = Reloc::PIC_; |
| 1237 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1238 | TargetOptions Options; |
| 1239 | Options.LessPreciseFPMADOption = EnableFPMAD; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1240 | Options.NoFramePointerElim = DisableFPElim; |
| 1241 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
Shih-wei Liao | cedee4b | 2012-08-02 23:13:03 -0700 | [diff] [blame] | 1242 | Options.AllowFPOpFusion = FuseFPOps; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1243 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 1244 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 1245 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 1246 | Options.HonorSignDependentRoundingFPMathOption = |
| 1247 | EnableHonorSignDependentRoundingFPMath; |
| 1248 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 1249 | if (FloatABIForCalls != FloatABI::Default) |
| 1250 | Options.FloatABIType = FloatABIForCalls; |
| 1251 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 1252 | Options.JITExceptionHandling = EnableJITExceptionHandling; |
| 1253 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 1254 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
| 1255 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 1256 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 1257 | Options.RealignStack = EnableRealignStack; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1258 | Options.TrapFuncName = TrapFuncName; |
| 1259 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 1260 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1261 | std::auto_ptr<mcld::MCLDTargetMachine> target_machine( |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1262 | TheTarget->createTargetMachine(TheTriple.getTriple(), |
| 1263 | MCPU, FeaturesStr, Options, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1264 | ArgRelocModel, CMModel, OLvl)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1265 | assert(target_machine.get() && "Could not allocate target machine!"); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1266 | mcld::MCLDTargetMachine &TheTargetMachine = *target_machine.get(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1267 | |
| 1268 | TheTargetMachine.getTM().setMCUseLoc(false); |
| 1269 | TheTargetMachine.getTM().setMCUseCFI(false); |
| 1270 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1271 | // FIXME: Move the initialization of LineInfo to mcld::Linker when we |
| 1272 | // finish LineInfo's implementation. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1273 | OwningPtr<mcld::DiagnosticLineInfo> |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame] | 1274 | diag_line_info(TheTarget->createDiagnosticLineInfo(*TheTarget, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1275 | TheTriple.getTriple())); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1276 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1277 | mcld::getDiagnosticEngine().setLineInfo(*diag_line_info.take()); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1278 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1279 | // Figure out where we are going to send the output... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1280 | OwningPtr<mcld::ToolOutputFile> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1281 | Out(GetOutputStream(TheTarget->get()->getName(), |
| 1282 | TheTriple.getOS(), |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1283 | ArgFileType, |
| 1284 | ArgBitcodeFilename, |
| 1285 | ArgOutputFilename)); |
| 1286 | if (!Out) { |
| 1287 | // FIXME: show some error message pls. |
| 1288 | return 1; |
| 1289 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1290 | |
| 1291 | // Build up all of the passes that we want to do to the module. |
| 1292 | PassManager PM; |
| 1293 | |
Shih-wei Liao | 86036a3 | 2013-01-14 19:16:03 -0800 | [diff] [blame^] | 1294 | // Add the data layout from the target machine, if it exists, or the module. |
| 1295 | if (const DataLayout *DL = TheTargetMachine.getTM().getDataLayout()) |
| 1296 | PM.add(new DataLayout(*DL)); |
| 1297 | else |
| 1298 | PM.add(new DataLayout(&mod)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1299 | |
| 1300 | // Override default to generate verbose assembly. |
| 1301 | TheTargetMachine.getTM().setAsmVerbosityDefault(true); |
| 1302 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1303 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1304 | // Ask the target to add backend passes as necessary. |
| 1305 | if( TheTargetMachine.addPassesToEmitFile(PM, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1306 | *Out, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1307 | ArgFileType, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1308 | OLvl, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1309 | LDIRModule, |
| 1310 | LDConfig, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1311 | NoVerify)) { |
| 1312 | errs() << argv[0] << ": target does not support generation of this" |
| 1313 | << " file type!\n"; |
| 1314 | return 1; |
| 1315 | } |
| 1316 | |
| 1317 | // Before executing passes, print the final values of the LLVM options. |
| 1318 | cl::PrintOptionValues(); |
| 1319 | |
| 1320 | PM.run(mod); |
| 1321 | } |
| 1322 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1323 | if (mcld::getDiagnosticEngine().getPrinter()->getNumErrors()) |
| 1324 | return 1; |
| 1325 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1326 | // Declare success. |
| 1327 | Out->keep(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1328 | return 0; |
| 1329 | } |
| 1330 | |