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 | |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 464 | static cl::opt<bool> |
| 465 | ArgDiscardLocals("discard-locals", |
| 466 | cl::desc("Delete all temporary local symbols."), |
| 467 | cl::init(false)); |
| 468 | |
| 469 | static cl::alias |
| 470 | ArgDiscardLocalsAlias("X", |
| 471 | cl::desc("alias for --discard-locals"), |
| 472 | cl::aliasopt(ArgDiscardLocals)); |
| 473 | |
| 474 | static cl::opt<bool> |
| 475 | ArgDiscardAll("discard-all", |
| 476 | cl::desc("Delete all local symbols."), |
| 477 | cl::init(false)); |
| 478 | |
| 479 | static cl::alias |
| 480 | ArgDiscardAllAlias("x", |
| 481 | cl::desc("alias for --discard-all"), |
| 482 | cl::aliasopt(ArgDiscardAll)); |
| 483 | |
| 484 | static cl::opt<bool> |
| 485 | ArgStripDebug("strip-debug", |
| 486 | cl::desc("Omit debugger symbol information from the output file."), |
| 487 | cl::init(false)); |
| 488 | |
| 489 | static cl::alias |
| 490 | ArgStripDebugAlias("S", |
| 491 | cl::desc("alias for --strip-debug"), |
| 492 | cl::aliasopt(ArgStripDebug)); |
| 493 | |
| 494 | static cl::opt<bool> |
| 495 | ArgStripAll("strip-all", |
| 496 | cl::desc("Omit all symbol information from the output file."), |
| 497 | cl::init(false)); |
| 498 | |
| 499 | static cl::alias |
| 500 | ArgStripAllAlias("s", |
| 501 | cl::desc("alias for --strip-all"), |
| 502 | cl::aliasopt(ArgStripAll)); |
| 503 | |
| 504 | static cl::opt<bool> |
| 505 | ArgNMagic("nmagic", |
| 506 | cl::desc("Do not page align data"), |
| 507 | cl::init(false)); |
| 508 | |
| 509 | static cl::alias |
| 510 | ArgNMagicAlias("n", |
| 511 | cl::desc("alias for --nmagic"), |
| 512 | cl::aliasopt(ArgNMagic)); |
| 513 | |
| 514 | static cl::opt<bool> |
| 515 | ArgOMagic("omagic", |
| 516 | cl::desc("Do not page align data, do not make text readonly"), |
| 517 | cl::init(false)); |
| 518 | |
| 519 | static cl::alias |
| 520 | ArgOMagicAlias("N", |
| 521 | cl::desc("alias for --omagic"), |
| 522 | cl::aliasopt(ArgOMagic)); |
| 523 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 524 | /// @{ |
| 525 | /// @name FIXME: begin of unsupported options |
| 526 | /// @} |
| 527 | static cl::opt<bool> |
| 528 | ArgGCSections("gc-sections", |
| 529 | cl::desc("Enable garbage collection of unused input sections."), |
| 530 | cl::init(false)); |
| 531 | |
| 532 | static cl::opt<bool> |
| 533 | ArgNoGCSections("no-gc-sections", |
| 534 | cl::desc("disable garbage collection of unused input sections."), |
| 535 | cl::init(false)); |
| 536 | |
| 537 | namespace icf { |
| 538 | enum Mode { |
| 539 | None, |
| 540 | All, |
| 541 | Safe |
| 542 | }; |
| 543 | } // namespace of icf |
| 544 | |
| 545 | static cl::opt<icf::Mode> |
| 546 | ArgICF("icf", |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 547 | cl::ZeroOrMore, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 548 | cl::desc("Identical Code Folding"), |
| 549 | cl::init(icf::None), |
| 550 | cl::values( |
| 551 | clEnumValN(icf::None, "none", |
| 552 | "do not perform cold folding"), |
| 553 | clEnumValN(icf::All, "all", |
| 554 | "always preform cold folding"), |
| 555 | clEnumValN(icf::Safe, "safe", |
| 556 | "Folds ctors, dtors and functions whose pointers are definitely not taken."), |
| 557 | clEnumValEnd)); |
| 558 | |
| 559 | // FIXME: add this to target options? |
| 560 | static cl::opt<bool> |
| 561 | ArgFIXCA8("fix-cortex-a8", |
| 562 | cl::desc("Enable Cortex-A8 Thumb-2 branch erratum fix"), |
| 563 | cl::init(false)); |
| 564 | |
| 565 | static cl::opt<bool> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 566 | ArgExportDynamic("export-dynamic", |
| 567 | cl::desc("Export all dynamic symbols"), |
| 568 | cl::init(false)); |
| 569 | |
| 570 | static cl::alias |
| 571 | ArgExportDynamicAlias("E", |
| 572 | cl::desc("alias for --export-dynamic"), |
| 573 | cl::aliasopt(ArgExportDynamic)); |
| 574 | |
| 575 | static cl::opt<std::string> |
| 576 | ArgEmulation("m", |
| 577 | cl::desc("Set GNU linker emulation"), |
| 578 | cl::value_desc("emulation")); |
| 579 | |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 580 | static cl::list<std::string, bool, llvm::cl::SearchDirParser> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 581 | ArgRuntimePathLink("rpath-link", |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 582 | cl::ZeroOrMore, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 583 | cl::desc("Add a directory to the link time library search path"), |
| 584 | cl::value_desc("dir")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 585 | |
| 586 | static cl::list<std::string> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 587 | ArgExcludeLIBS("exclude-libs", |
| 588 | cl::CommaSeparated, |
| 589 | cl::desc("Exclude libraries from automatic export"), |
| 590 | cl::value_desc("lib1,lib2,...")); |
| 591 | |
| 592 | static cl::opt<std::string> |
| 593 | ArgBuildID("build-id", |
| 594 | cl::desc("Request creation of \".note.gnu.build-id\" ELF note section."), |
| 595 | cl::value_desc("style")); |
| 596 | |
| 597 | static cl::opt<std::string> |
| 598 | ArgForceUndefined("u", |
| 599 | cl::desc("Force symbol to be undefined in the output file"), |
| 600 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 601 | |
| 602 | static cl::alias |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 603 | ArgForceUndefinedAlias("undefined", |
| 604 | cl::desc("alias for -u"), |
| 605 | cl::aliasopt(ArgForceUndefined)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 606 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 607 | static cl::opt<std::string> |
| 608 | ArgVersionScript("version-script", |
| 609 | cl::desc("Version script."), |
| 610 | cl::value_desc("Version script")); |
| 611 | |
| 612 | static cl::opt<bool> |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 613 | ArgWarnCommon("warn-common", |
| 614 | cl::desc("warn common symbol"), |
| 615 | cl::init(false)); |
| 616 | |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 617 | static cl::opt<mcld::GeneralOptions::HashStyle> |
| 618 | ArgHashStyle("hash-style", cl::init(mcld::GeneralOptions::SystemV), |
| 619 | cl::desc("Set the type of linker's hash table(s)."), |
| 620 | cl::values( |
| 621 | clEnumValN(mcld::GeneralOptions::SystemV, "sysv", |
| 622 | "classic ELF .hash section"), |
| 623 | clEnumValN(mcld::GeneralOptions::GNU, "gnu", |
| 624 | "new style GNU .gnu.hash section"), |
| 625 | clEnumValN(mcld::GeneralOptions::Both, "both", |
| 626 | "both the classic ELF and new style GNU hash tables"), |
| 627 | clEnumValEnd)); |
| 628 | |
| 629 | static cl::opt<std::string> |
| 630 | ArgFilter("F", |
| 631 | cl::desc("Filter for shared object symbol table"), |
| 632 | cl::value_desc("name")); |
| 633 | |
| 634 | static cl::alias |
| 635 | ArgFilterAlias("filter", |
| 636 | cl::desc("alias for -F"), |
| 637 | cl::aliasopt(ArgFilterAlias)); |
| 638 | |
| 639 | static cl::list<std::string> |
| 640 | ArgAuxiliary("f", |
| 641 | cl::ZeroOrMore, |
| 642 | cl::desc("Auxiliary filter for shared object symbol table"), |
| 643 | cl::value_desc("name")); |
| 644 | |
| 645 | static cl::alias |
| 646 | ArgAuxiliaryAlias("auxiliary", |
| 647 | cl::desc("alias for -f"), |
| 648 | cl::aliasopt(ArgAuxiliary)); |
| 649 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 650 | static cl::opt<bool> |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 651 | ArgEB("EB", |
| 652 | cl::desc("Link big-endian objects. This affects the default output format."), |
| 653 | cl::init(false)); |
| 654 | |
| 655 | static cl::opt<bool> |
| 656 | ArgEL("EL", |
| 657 | cl::desc("Link little-endian objects. This affects the default output format."), |
| 658 | cl::init(false)); |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 659 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 660 | /// @{ |
| 661 | /// @name FIXME: end of unsupported options |
| 662 | /// @} |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 663 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 664 | static cl::opt<bool> |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 665 | ArgNoStdlib("nostdlib", |
| 666 | cl::desc("Only search lib dirs explicitly specified on cmdline"), |
| 667 | cl::init(false)); |
| 668 | |
| 669 | static cl::list<std::string, bool, llvm::cl::SearchDirParser> |
| 670 | ArgRuntimePath("rpath", |
| 671 | cl::ZeroOrMore, |
| 672 | cl::desc("Add a directory to the runtime library search path"), |
| 673 | cl::value_desc("dir")); |
| 674 | |
| 675 | static cl::alias |
| 676 | ArgRuntimePathAlias("R", |
| 677 | cl::desc("alias for --rpath"), |
| 678 | cl::aliasopt(ArgRuntimePath), cl::Prefix); |
| 679 | |
| 680 | static cl::opt<bool> |
| 681 | ArgEnableNewDTags("enable-new-dtags", |
| 682 | cl::desc("Enable use of DT_RUNPATH and DT_FLAGS"), |
| 683 | cl::init(false)); |
| 684 | |
| 685 | class FalseParser : public cl::parser<bool> { |
| 686 | const char *ArgStr; |
| 687 | public: |
| 688 | |
| 689 | // parse - Return true on error. |
| 690 | bool parse(cl::Option &O, StringRef ArgName, StringRef Arg, bool &Val) { |
| 691 | if (cl::parser<bool>::parse(O, ArgName, Arg, Val)) |
| 692 | return false; |
| 693 | Val = false; |
| 694 | return false; |
| 695 | } |
| 696 | }; |
| 697 | |
| 698 | static bool ArgFatalWarnings; |
| 699 | |
| 700 | static cl::opt<bool, true, FalseParser> |
| 701 | ArgNoFatalWarnings("no-fatal-warnings", |
| 702 | cl::location(ArgFatalWarnings), |
| 703 | cl::desc("do not turn warnings into errors"), |
| 704 | cl::init(false), |
| 705 | cl::ValueDisallowed); |
| 706 | |
| 707 | static cl::opt<bool, true> |
| 708 | ArgFatalWarnings_("fatal-warnings", |
| 709 | cl::location(ArgFatalWarnings), |
| 710 | cl::desc("turn all warnings into errors"), |
| 711 | cl::init(false), |
| 712 | cl::ValueDisallowed); |
| 713 | |
| 714 | static cl::opt<bool> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 715 | ArgWarnSharedTextrel("warn-shared-textrel", |
| 716 | cl::desc("Warn if adding DT_TEXTREL in a shared object."), |
| 717 | cl::init(false)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 718 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 719 | namespace format { |
| 720 | enum Format { |
| 721 | Binary, |
| 722 | Unknown // decided by triple |
| 723 | }; |
| 724 | } // namespace of format |
| 725 | |
| 726 | static cl::opt<format::Format> |
| 727 | ArgFormat("b", |
| 728 | cl::value_desc("Format"), |
| 729 | cl::desc("set input format"), |
| 730 | cl::init(format::Unknown), |
| 731 | cl::values( |
| 732 | clEnumValN(format::Binary, "binary", |
| 733 | "read in binary machine code."), |
| 734 | clEnumValEnd)); |
| 735 | |
| 736 | static cl::alias |
| 737 | ArgFormatAlias("format", |
| 738 | cl::desc("alias for -b"), |
| 739 | cl::aliasopt(ArgFormat)); |
| 740 | |
| 741 | static cl::opt<format::Format> |
| 742 | ArgOFormat("oformat", |
| 743 | cl::value_desc("Format"), |
| 744 | cl::desc("set output format"), |
| 745 | cl::init(format::Unknown), |
| 746 | cl::values( |
| 747 | clEnumValN(format::Binary, "binary", |
| 748 | "generate binary machine code."), |
| 749 | clEnumValEnd)); |
| 750 | |
| 751 | static cl::opt<bool> |
| 752 | ArgDefineCommon("d", |
| 753 | cl::ZeroOrMore, |
| 754 | cl::desc("Define common symbol"), |
| 755 | cl::init(false)); |
| 756 | |
| 757 | static cl::alias |
| 758 | ArgDefineCommonAlias1("dc", |
| 759 | cl::desc("alias for -d"), |
| 760 | cl::aliasopt(ArgDefineCommon)); |
| 761 | |
| 762 | static cl::alias |
| 763 | ArgDefineCommonAlias2("dp", |
| 764 | cl::desc("alias for -d"), |
| 765 | cl::aliasopt(ArgDefineCommon)); |
| 766 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 767 | //===----------------------------------------------------------------------===// |
| 768 | // Scripting Options |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 769 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 770 | static cl::list<std::string> |
| 771 | ArgWrapList("wrap", |
| 772 | cl::ZeroOrMore, |
| 773 | cl::desc("Use a wrap function fo symbol."), |
| 774 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 775 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 776 | static cl::list<std::string> |
| 777 | ArgPortList("portable", |
| 778 | cl::ZeroOrMore, |
| 779 | cl::desc("Use a portable function fo symbol."), |
| 780 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 781 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 782 | static cl::list<std::string> |
| 783 | ArgAddressMapList("section-start", |
| 784 | cl::ZeroOrMore, |
| 785 | cl::desc("Locate a output section at the given absolute address"), |
| 786 | cl::value_desc("Set address of section"), |
| 787 | cl::Prefix); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 788 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 789 | static cl::opt<unsigned long long> |
| 790 | ArgBssSegAddr("Tbss", |
| 791 | cl::desc("Set the address of the bss segment"), |
| 792 | cl::init(-1U)); |
| 793 | |
| 794 | static cl::opt<unsigned long long> |
| 795 | ArgDataSegAddr("Tdata", |
| 796 | cl::desc("Set the address of the data segment"), |
| 797 | cl::init(-1U)); |
| 798 | |
| 799 | static cl::opt<unsigned long long> |
| 800 | ArgTextSegAddr("Ttext", |
| 801 | cl::desc("Set the address of the text segment"), |
| 802 | cl::init(-1U)); |
| 803 | |
| 804 | //===----------------------------------------------------------------------===// |
| 805 | // non-member functions |
| 806 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 807 | /// GetOutputStream - get the output stream. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 808 | static mcld::ToolOutputFile *GetOutputStream(const char* pTargetName, |
| 809 | Triple::OSType pOSType, |
| 810 | mcld::CodeGenFileType pFileType, |
| 811 | const mcld::sys::fs::Path& pInputFilename, |
| 812 | mcld::sys::fs::Path& pOutputFilename) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 813 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 814 | if (pOutputFilename.empty()) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 815 | if (0 == pInputFilename.native().compare("-")) |
| 816 | pOutputFilename.assign("-"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 817 | else { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 818 | switch(pFileType) { |
| 819 | case mcld::CGFT_ASMFile: { |
| 820 | if (0 == pInputFilename.native().compare("-")) |
| 821 | pOutputFilename.assign("_out"); |
| 822 | else |
| 823 | pOutputFilename.assign(pInputFilename.stem().native()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 824 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 825 | if (0 == strcmp(pTargetName, "c")) |
| 826 | pOutputFilename.native() += ".cbe.c"; |
| 827 | else if (0 == strcmp(pTargetName, "cpp")) |
| 828 | pOutputFilename.native() += ".cpp"; |
| 829 | else |
| 830 | pOutputFilename.native() += ".s"; |
| 831 | } |
| 832 | break; |
| 833 | |
| 834 | case mcld::CGFT_OBJFile: { |
| 835 | if (0 == pInputFilename.native().compare("-")) |
| 836 | pOutputFilename.assign("_out"); |
| 837 | else |
| 838 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 839 | |
| 840 | if (pOSType == Triple::Win32) |
| 841 | pOutputFilename.native() += ".obj"; |
| 842 | else |
| 843 | pOutputFilename.native() += ".o"; |
| 844 | } |
| 845 | break; |
| 846 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 847 | case mcld::CGFT_PARTIAL: { |
| 848 | if (Triple::Win32 == pOSType) { |
| 849 | if (0 == pInputFilename.native().compare("-")) |
| 850 | pOutputFilename.assign("_out"); |
| 851 | else |
| 852 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 853 | pOutputFilename.native() += ".obj"; |
| 854 | } |
| 855 | else |
| 856 | pOutputFilename.assign("a.out"); |
| 857 | } |
| 858 | break; |
| 859 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 860 | case mcld::CGFT_DSOFile: { |
| 861 | if (Triple::Win32 == pOSType) { |
| 862 | if (0 == pInputFilename.native().compare("-")) |
| 863 | pOutputFilename.assign("_out"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 864 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 865 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 866 | pOutputFilename.native() += ".dll"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 867 | } |
| 868 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 869 | pOutputFilename.assign("a.out"); |
| 870 | } |
| 871 | break; |
| 872 | |
| 873 | case mcld::CGFT_EXEFile: { |
| 874 | if (Triple::Win32 == pOSType) { |
| 875 | if (0 == pInputFilename.native().compare("-")) |
| 876 | pOutputFilename.assign("_out"); |
| 877 | else |
| 878 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 879 | pOutputFilename.native() += ".exe"; |
| 880 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 881 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 882 | pOutputFilename.assign("a.out"); |
| 883 | } |
| 884 | break; |
| 885 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 886 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 887 | break; |
| 888 | default: |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 889 | llvm::report_fatal_error("Unknown output file type.\n"); |
| 890 | } // end of switch |
| 891 | } // end of ! pInputFilename == "-" |
| 892 | } // end of if empty pOutputFilename |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 893 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 894 | mcld::FileHandle::Permission permission; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 895 | switch (pFileType) { |
| 896 | default: assert(0 && "Unknown file type"); |
| 897 | case mcld::CGFT_ASMFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 898 | case mcld::CGFT_OBJFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 899 | case mcld::CGFT_PARTIAL: |
| 900 | permission = 0644; |
| 901 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 902 | case mcld::CGFT_DSOFile: |
| 903 | case mcld::CGFT_EXEFile: |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 904 | case mcld::CGFT_BINARY: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 905 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 906 | permission = 0755; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 907 | break; |
| 908 | } |
| 909 | |
| 910 | // Open the file. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 911 | mcld::ToolOutputFile* result_output = |
| 912 | new mcld::ToolOutputFile(pOutputFilename, |
| 913 | mcld::FileHandle::ReadWrite | |
| 914 | mcld::FileHandle::Create | |
| 915 | mcld::FileHandle::Truncate, |
| 916 | permission); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 917 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 918 | return result_output; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 919 | } |
| 920 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 921 | /// ParseProgName - Parse program name |
| 922 | /// This function simplifies cross-compiling by reading triple from the program |
| 923 | /// name. For example, if the program name is `arm-linux-eabi-ld.mcld', we can |
| 924 | /// get the triple is arm-linux-eabi by the program name. |
| 925 | static void ParseProgName(const char *progname) |
| 926 | { |
| 927 | static const char *suffixes[] = { |
| 928 | "ld", |
| 929 | "ld.mcld", |
| 930 | }; |
| 931 | |
| 932 | std::string ProgName(mcld::sys::fs::Path(progname).stem().native()); |
| 933 | |
| 934 | for (size_t i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { |
| 935 | if (ProgName == suffixes[i]) |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | StringRef ProgNameRef(ProgName); |
| 940 | StringRef Prefix; |
| 941 | |
| 942 | for (size_t i = 0; i < sizeof(suffixes) / sizeof(suffixes[0]); ++i) { |
| 943 | if (!ProgNameRef.endswith(suffixes[i])) |
| 944 | continue; |
| 945 | |
| 946 | StringRef::size_type LastComponent = ProgNameRef.rfind('-', |
| 947 | ProgNameRef.size() - strlen(suffixes[i])); |
| 948 | if (LastComponent == StringRef::npos) |
| 949 | continue; |
| 950 | StringRef Prefix = ProgNameRef.slice(0, LastComponent); |
| 951 | std::string IgnoredError; |
| 952 | if (!llvm::TargetRegistry::lookupTarget(Prefix, IgnoredError)) |
| 953 | continue; |
| 954 | TargetTriple = Prefix.str(); |
| 955 | return; |
| 956 | } |
| 957 | } |
| 958 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 959 | static bool ShouldColorize() |
| 960 | { |
| 961 | const char* term = getenv("TERM"); |
| 962 | return term && (0 != strcmp(term, "dumb")); |
| 963 | } |
| 964 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 965 | static bool ProcessLinkerOptionsFromCommand(mcld::LinkerConfig& pConfig) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 966 | // ----- Set up General Options ----- // |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 967 | // set up colorize |
| 968 | switch (ArgColor) { |
| 969 | case color::Never: |
| 970 | pConfig.options().setColor(false); |
| 971 | break; |
| 972 | case color::Always: |
| 973 | pConfig.options().setColor(true); |
| 974 | break; |
| 975 | case color::Auto: |
| 976 | bool color_option = ShouldColorize() && |
| 977 | llvm::sys::Process::FileDescriptorIsDisplayed(STDOUT_FILENO); |
| 978 | pConfig.options().setColor(color_option); |
| 979 | break; |
| 980 | } |
| 981 | |
| 982 | mcld::outs().setColor(pConfig.options().color()); |
| 983 | mcld::errs().setColor(pConfig.options().color()); |
| 984 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 985 | // set up soname |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 986 | pConfig.options().setSOName(ArgSOName); |
| 987 | |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 988 | // add all rpath entries |
| 989 | cl::list<std::string>::iterator rp; |
| 990 | cl::list<std::string>::iterator rpEnd = ArgRuntimePath.end(); |
| 991 | for (rp = ArgRuntimePath.begin(); rp != rpEnd; ++rp) { |
| 992 | pConfig.options().getRpathList().push_back(*rp); |
| 993 | } |
| 994 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 995 | // --fatal-warnings |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 996 | // pConfig.options().setFatalWarnings(ArgFatalWarnings); |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 997 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 998 | // -shared or -pie |
| 999 | if (true == ArgShared || true == ArgPIE) { |
| 1000 | ArgFileType = mcld::CGFT_DSOFile; |
| 1001 | } |
| 1002 | else if (true == ArgRelocatable) { |
| 1003 | ArgFileType = mcld::CGFT_PARTIAL; |
| 1004 | } |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1005 | else if (format::Binary == ArgOFormat) { |
| 1006 | ArgFileType = mcld::CGFT_BINARY; |
| 1007 | } |
| 1008 | |
| 1009 | // -b [input-format], --format=[input-format] |
| 1010 | if (format::Binary == ArgFormat) |
| 1011 | pConfig.options().setBinaryInput(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1012 | |
| 1013 | // -V |
| 1014 | if (ArgVersion) { |
| 1015 | mcld::outs() << "MCLinker - " |
| 1016 | << mcld::LinkerConfig::version() |
| 1017 | << "\n"; |
| 1018 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1019 | |
| 1020 | // set up sysroot |
| 1021 | if (!ArgSysRoot.empty()) { |
| 1022 | if (exists(ArgSysRoot) && is_directory(ArgSysRoot)) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1023 | pConfig.options().setSysroot(ArgSysRoot); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | // add all search directories |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1027 | cl::list<std::string>::iterator sd; |
| 1028 | cl::list<std::string>::iterator sdEnd = ArgSearchDirList.end(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1029 | for (sd=ArgSearchDirList.begin(); sd!=sdEnd; ++sd) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1030 | if (!pConfig.options().directories().insert(*sd)) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1031 | // FIXME: need a warning function |
| 1032 | errs() << "WARNING: can not open search directory `-L" |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1033 | << *sd |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1034 | << "'.\n"; |
| 1035 | } |
| 1036 | } |
| 1037 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1038 | pConfig.options().setPIE(ArgPIE); |
| 1039 | pConfig.options().setTrace(ArgTrace); |
| 1040 | pConfig.options().setVerbose(ArgVerbose); |
| 1041 | pConfig.options().setMaxErrorNum(ArgMaxErrorNum); |
| 1042 | pConfig.options().setMaxWarnNum(ArgMaxWarnNum); |
| 1043 | pConfig.options().setEntry(ArgEntry); |
| 1044 | pConfig.options().setBsymbolic(ArgBsymbolic); |
| 1045 | pConfig.options().setBgroup(ArgBgroup); |
| 1046 | pConfig.options().setDyld(ArgDyld); |
| 1047 | pConfig.options().setNoUndefined(ArgNoUndefined); |
| 1048 | pConfig.options().setMulDefs(ArgAllowMulDefs); |
| 1049 | pConfig.options().setEhFrameHdr(ArgEhFrameHdr); |
| 1050 | pConfig.options().setNMagic(ArgNMagic); |
| 1051 | pConfig.options().setOMagic(ArgOMagic); |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 1052 | pConfig.options().setStripDebug(ArgStripDebug || ArgStripAll); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1053 | pConfig.options().setExportDynamic(ArgExportDynamic); |
| 1054 | pConfig.options().setWarnSharedTextrel(ArgWarnSharedTextrel); |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1055 | pConfig.options().setDefineCommon(ArgDefineCommon); |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 1056 | pConfig.options().setNewDTags(ArgEnableNewDTags); |
| 1057 | pConfig.options().setHashStyle(ArgHashStyle); |
| 1058 | pConfig.options().setNoStdlib(ArgNoStdlib); |
| 1059 | |
| 1060 | if (ArgStripAll) |
| 1061 | pConfig.options().setStripSymbols(mcld::GeneralOptions::StripAllSymbols); |
| 1062 | else if (ArgDiscardAll) |
| 1063 | pConfig.options().setStripSymbols(mcld::GeneralOptions::StripLocals); |
| 1064 | else if (ArgDiscardLocals) |
| 1065 | pConfig.options().setStripSymbols(mcld::GeneralOptions::StripTemporaries); |
| 1066 | else |
| 1067 | pConfig.options().setStripSymbols(mcld::GeneralOptions::KeepAllSymbols); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1068 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1069 | // set up rename map, for --wrap |
| 1070 | cl::list<std::string>::iterator wname; |
| 1071 | cl::list<std::string>::iterator wnameEnd = ArgWrapList.end(); |
| 1072 | for (wname = ArgWrapList.begin(); wname != wnameEnd; ++wname) { |
| 1073 | bool exist = false; |
| 1074 | |
| 1075 | // add wname -> __wrap_wname |
| 1076 | mcld::StringEntry<llvm::StringRef>* to_wrap = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1077 | pConfig.scripts().renameMap().insert(*wname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1078 | |
| 1079 | std::string to_wrap_str = "__wrap_" + *wname; |
| 1080 | to_wrap->setValue(to_wrap_str); |
| 1081 | |
| 1082 | if (exist) |
| 1083 | mcld::warning(mcld::diag::rewrap) << *wname << to_wrap_str; |
| 1084 | |
| 1085 | // add __real_wname -> wname |
| 1086 | std::string from_real_str = "__real_" + *wname; |
| 1087 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1088 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1089 | from_real->setValue(*wname); |
| 1090 | if (exist) |
| 1091 | mcld::warning(mcld::diag::rewrap) << *wname << from_real_str; |
| 1092 | } // end of for |
| 1093 | |
| 1094 | // set up rename map, for --portable |
| 1095 | cl::list<std::string>::iterator pname; |
| 1096 | cl::list<std::string>::iterator pnameEnd = ArgPortList.end(); |
| 1097 | for (pname = ArgPortList.begin(); pname != pnameEnd; ++pname) { |
| 1098 | bool exist = false; |
| 1099 | |
| 1100 | // add pname -> pname_portable |
| 1101 | mcld::StringEntry<llvm::StringRef>* to_port = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1102 | pConfig.scripts().renameMap().insert(*pname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1103 | |
| 1104 | std::string to_port_str = *pname + "_portable"; |
| 1105 | to_port->setValue(to_port_str); |
| 1106 | |
| 1107 | if (exist) |
| 1108 | mcld::warning(mcld::diag::rewrap) << *pname << to_port_str; |
| 1109 | |
| 1110 | // add __real_pname -> pname |
| 1111 | std::string from_real_str = "__real_" + *pname; |
| 1112 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1113 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1114 | |
| 1115 | from_real->setValue(*pname); |
| 1116 | if (exist) |
| 1117 | mcld::warning(mcld::diag::rewrap) << *pname << from_real_str; |
| 1118 | } // end of for |
| 1119 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1120 | // add -z options |
| 1121 | cl::list<mcld::ZOption>::iterator zOpt; |
| 1122 | cl::list<mcld::ZOption>::iterator zOptEnd = ArgZOptionList.end(); |
| 1123 | for (zOpt = ArgZOptionList.begin(); zOpt != zOptEnd; ++zOpt) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1124 | pConfig.options().addZOption(*zOpt); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1125 | } |
| 1126 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1127 | if (ArgGCSections) { |
| 1128 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgGCSections.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1129 | } |
| 1130 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1131 | // set up icf mode |
| 1132 | switch (ArgICF) { |
| 1133 | case icf::None: |
| 1134 | break; |
| 1135 | case icf::All: |
| 1136 | case icf::Safe: |
| 1137 | default: |
| 1138 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgICF.ArgStr; |
| 1139 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1140 | } |
| 1141 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1142 | if (ArgFIXCA8) { |
| 1143 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgFIXCA8.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1144 | } |
| 1145 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1146 | // add address mappings |
| 1147 | // -Ttext |
| 1148 | if (-1U != ArgTextSegAddr) { |
| 1149 | bool exist = false; |
| 1150 | mcld::StringEntry<uint64_t>* text_mapping = |
| 1151 | pConfig.scripts().addressMap().insert(".text", exist); |
| 1152 | text_mapping->setValue(ArgTextSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1153 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1154 | // -Tdata |
| 1155 | if (-1U != ArgDataSegAddr) { |
| 1156 | bool exist = false; |
| 1157 | mcld::StringEntry<uint64_t>* data_mapping = |
| 1158 | pConfig.scripts().addressMap().insert(".data", exist); |
| 1159 | data_mapping->setValue(ArgDataSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1160 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1161 | // -Tbss |
| 1162 | if (-1U != ArgBssSegAddr) { |
| 1163 | bool exist = false; |
| 1164 | mcld::StringEntry<uint64_t>* bss_mapping = |
| 1165 | pConfig.scripts().addressMap().insert(".bss", exist); |
| 1166 | bss_mapping->setValue(ArgBssSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1167 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1168 | // --section-start SECTION=ADDRESS |
| 1169 | for (cl::list<std::string>::iterator |
| 1170 | it = ArgAddressMapList.begin(), ie = ArgAddressMapList.end(); |
| 1171 | it != ie; ++it) { |
| 1172 | // FIXME: Add a cl::parser |
| 1173 | size_t pos = (*it).find_last_of('='); |
| 1174 | llvm::StringRef script(*it); |
| 1175 | uint64_t address = 0x0; |
| 1176 | script.substr(pos + 1).getAsInteger(0, address); |
| 1177 | bool exist = false; |
| 1178 | mcld::StringEntry<uint64_t>* addr_mapping = |
| 1179 | pConfig.scripts().addressMap().insert(script.substr(0, pos), exist); |
| 1180 | addr_mapping->setValue(address); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1181 | } |
| 1182 | |
Stephen Hines | 6f75755 | 2013-03-04 19:51:03 -0800 | [diff] [blame^] | 1183 | // set up filter/aux filter for shared object |
| 1184 | pConfig.options().setFilter(ArgFilter); |
| 1185 | |
| 1186 | cl::list<std::string>::iterator aux; |
| 1187 | cl::list<std::string>::iterator auxEnd = ArgAuxiliary.end(); |
| 1188 | for (aux = ArgAuxiliary.begin(); aux != auxEnd; ++aux) |
| 1189 | pConfig.options().getAuxiliaryList().push_back(*aux); |
| 1190 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1191 | return true; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1192 | } |
| 1193 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1194 | int main(int argc, char* argv[]) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1195 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1196 | LLVMContext &Context = getGlobalContext(); |
| 1197 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1198 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1199 | // Initialize targets first, so that --version shows registered targets. |
| 1200 | InitializeAllTargets(); |
| 1201 | InitializeAllAsmPrinters(); |
| 1202 | InitializeAllAsmParsers(); |
| 1203 | InitializeAllTargetMCs(); |
| 1204 | mcld::InitializeAllTargets(); |
| 1205 | mcld::InitializeAllLinkers(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1206 | mcld::InitializeAllEmulations(); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1207 | mcld::InitializeAllDiagnostics(); |
| 1208 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1209 | ParseProgName(argv[0]); |
| 1210 | cl::ParseCommandLineOptions(argc, argv, "MCLinker\n"); |
| 1211 | |
| 1212 | #ifdef ENABLE_UNITTEST |
| 1213 | if (UnitTest) { |
| 1214 | return unit_test( argc, argv ); |
| 1215 | } |
| 1216 | #endif |
| 1217 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1218 | // Load the module to be compiled... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1219 | std::auto_ptr<llvm::Module> M; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1220 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1221 | // Load the module to be linked... |
| 1222 | mcld::Module LDIRModule; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1223 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1224 | mcld::LinkerConfig LDConfig; |
| 1225 | |
| 1226 | // Process the linker input from the command line |
| 1227 | if (!ProcessLinkerOptionsFromCommand(LDConfig)) { |
| 1228 | errs() << argv[0] << ": failed to process linker options from command line!\n"; |
| 1229 | return 1; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | if (ArgBitcodeFilename.empty() && |
| 1233 | (mcld::CGFT_DSOFile != ArgFileType && |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1234 | mcld::CGFT_EXEFile != ArgFileType && |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1235 | mcld::CGFT_PARTIAL != ArgFileType && |
| 1236 | mcld::CGFT_BINARY != ArgFileType)) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1237 | // If the file is not given, forcefully read from stdin |
| 1238 | if (ArgVerbose >= 0) { |
| 1239 | errs() << "** The bitcode/llvm asm file is not given. Read from stdin.\n" |
| 1240 | << "** Specify input bitcode/llvm asm file by\n\n" |
| 1241 | << " llvm-mcld -dB [the bitcode/llvm asm]\n\n"; |
| 1242 | } |
| 1243 | |
| 1244 | ArgBitcodeFilename.assign("-"); |
| 1245 | } |
| 1246 | |
| 1247 | if (!ArgBitcodeFilename.empty()) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1248 | SMDiagnostic Err; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1249 | M.reset(ParseIRFile(ArgBitcodeFilename.native(), Err, Context)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1250 | |
| 1251 | if (M.get() == 0) { |
| 1252 | Err.print(argv[0], errs()); |
| 1253 | errs() << "** Failed to to the given bitcode/llvm asm file '" |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1254 | << ArgBitcodeFilename.native() << "'. **\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1255 | return 1; |
| 1256 | } |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1257 | } |
| 1258 | else { |
| 1259 | // If here, output must be dynamic shared object (mcld::CGFT_DSOFile) and |
| 1260 | // executable file (mcld::CGFT_EXEFile). |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1261 | M.reset(new Module("Empty Module", Context)); |
| 1262 | } |
| 1263 | Module &mod = *M.get(); |
| 1264 | |
| 1265 | // If we are supposed to override the target triple, do so now. |
| 1266 | Triple TheTriple; |
| 1267 | if (!TargetTriple.empty()) { |
| 1268 | TheTriple.setTriple(TargetTriple); |
| 1269 | mod.setTargetTriple(TargetTriple); |
| 1270 | } |
| 1271 | |
| 1272 | // User doesn't specify the triple from command. |
| 1273 | if (TheTriple.getTriple().empty()) { |
| 1274 | // Try to get one from the input Module. |
| 1275 | const std::string &TripleStr = mod.getTargetTriple(); |
| 1276 | |
| 1277 | if (TripleStr.empty()) |
| 1278 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 1279 | else |
| 1280 | TheTriple.setTriple(TripleStr); |
| 1281 | } |
| 1282 | |
| 1283 | // Allocate target machine. First, check whether the user has explicitly |
| 1284 | // specified an architecture to compile for. If so we have to look it up by |
| 1285 | // name, because it might be a backend that has no mapping to a target triple. |
| 1286 | const mcld::Target *TheTarget = 0; |
| 1287 | if (!MArch.empty()) { |
| 1288 | for (mcld::TargetRegistry::iterator it = mcld::TargetRegistry::begin(), |
| 1289 | ie = mcld::TargetRegistry::end(); it != ie; ++it) { |
| 1290 | if (MArch == (*it)->get()->getName()) { |
| 1291 | TheTarget = *it; |
| 1292 | break; |
| 1293 | } |
| 1294 | } |
| 1295 | |
| 1296 | if (!TheTarget) { |
| 1297 | errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; |
| 1298 | return 1; |
| 1299 | } |
| 1300 | |
| 1301 | // Adjust the triple to match (if known), otherwise stick with the |
| 1302 | // module/host triple. |
| 1303 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); |
| 1304 | if (Type != Triple::UnknownArch) |
| 1305 | TheTriple.setArch(Type); |
| 1306 | } |
| 1307 | else { |
| 1308 | std::string Err; |
| 1309 | TheTarget = mcld::TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); |
| 1310 | if (TheTarget == 0) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1311 | errs() << "error: auto-selecting target `" << TheTriple.getTriple() |
| 1312 | << "'\n" |
| 1313 | << "Please use the -march option to explicitly select a target.\n" |
| 1314 | << "Example:\n" |
| 1315 | << " $ " << argv[0] << " -march=arm\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1316 | return 1; |
| 1317 | } |
| 1318 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1319 | // Set up mcld::LinkerConfig |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1320 | LDConfig.targets().setTriple(TheTriple); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1321 | |
| 1322 | // Package up features to be passed to target/subtarget |
| 1323 | std::string FeaturesStr; |
| 1324 | if (MAttrs.size()) { |
| 1325 | SubtargetFeatures Features; |
| 1326 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 1327 | Features.AddFeature(MAttrs[i]); |
| 1328 | FeaturesStr = Features.getString(); |
| 1329 | } |
| 1330 | |
| 1331 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 1332 | switch (OptLevel) { |
| 1333 | default: |
| 1334 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 1335 | return 1; |
| 1336 | case ' ': break; |
| 1337 | case '0': OLvl = CodeGenOpt::None; break; |
| 1338 | case '1': OLvl = CodeGenOpt::Less; break; |
| 1339 | case '2': OLvl = CodeGenOpt::Default; break; |
| 1340 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 1341 | } |
| 1342 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1343 | // set -fPIC |
| 1344 | if (ArgFPIC) |
| 1345 | ArgRelocModel = Reloc::PIC_; |
| 1346 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1347 | TargetOptions Options; |
| 1348 | Options.LessPreciseFPMADOption = EnableFPMAD; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1349 | Options.NoFramePointerElim = DisableFPElim; |
| 1350 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
Shih-wei Liao | cedee4b | 2012-08-02 23:13:03 -0700 | [diff] [blame] | 1351 | Options.AllowFPOpFusion = FuseFPOps; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1352 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 1353 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 1354 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 1355 | Options.HonorSignDependentRoundingFPMathOption = |
| 1356 | EnableHonorSignDependentRoundingFPMath; |
| 1357 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 1358 | if (FloatABIForCalls != FloatABI::Default) |
| 1359 | Options.FloatABIType = FloatABIForCalls; |
| 1360 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 1361 | Options.JITExceptionHandling = EnableJITExceptionHandling; |
| 1362 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 1363 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
| 1364 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 1365 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 1366 | Options.RealignStack = EnableRealignStack; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1367 | Options.TrapFuncName = TrapFuncName; |
| 1368 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 1369 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1370 | std::auto_ptr<mcld::MCLDTargetMachine> target_machine( |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1371 | TheTarget->createTargetMachine(TheTriple.getTriple(), |
| 1372 | MCPU, FeaturesStr, Options, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1373 | ArgRelocModel, CMModel, OLvl)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1374 | assert(target_machine.get() && "Could not allocate target machine!"); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1375 | mcld::MCLDTargetMachine &TheTargetMachine = *target_machine.get(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1376 | |
| 1377 | TheTargetMachine.getTM().setMCUseLoc(false); |
| 1378 | TheTargetMachine.getTM().setMCUseCFI(false); |
| 1379 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1380 | // FIXME: Move the initialization of LineInfo to mcld::Linker when we |
| 1381 | // finish LineInfo's implementation. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1382 | OwningPtr<mcld::DiagnosticLineInfo> |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame] | 1383 | diag_line_info(TheTarget->createDiagnosticLineInfo(*TheTarget, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1384 | TheTriple.getTriple())); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1385 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1386 | mcld::getDiagnosticEngine().setLineInfo(*diag_line_info.take()); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1387 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1388 | // Figure out where we are going to send the output... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1389 | OwningPtr<mcld::ToolOutputFile> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1390 | Out(GetOutputStream(TheTarget->get()->getName(), |
| 1391 | TheTriple.getOS(), |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1392 | ArgFileType, |
| 1393 | ArgBitcodeFilename, |
| 1394 | ArgOutputFilename)); |
| 1395 | if (!Out) { |
| 1396 | // FIXME: show some error message pls. |
| 1397 | return 1; |
| 1398 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1399 | |
| 1400 | // Build up all of the passes that we want to do to the module. |
| 1401 | PassManager PM; |
| 1402 | |
Shih-wei Liao | 86036a3 | 2013-01-14 19:16:03 -0800 | [diff] [blame] | 1403 | // Add the data layout from the target machine, if it exists, or the module. |
| 1404 | if (const DataLayout *DL = TheTargetMachine.getTM().getDataLayout()) |
| 1405 | PM.add(new DataLayout(*DL)); |
| 1406 | else |
| 1407 | PM.add(new DataLayout(&mod)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1408 | |
| 1409 | // Override default to generate verbose assembly. |
| 1410 | TheTargetMachine.getTM().setAsmVerbosityDefault(true); |
| 1411 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1412 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1413 | // Ask the target to add backend passes as necessary. |
| 1414 | if( TheTargetMachine.addPassesToEmitFile(PM, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1415 | *Out, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1416 | ArgFileType, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1417 | OLvl, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame] | 1418 | LDIRModule, |
| 1419 | LDConfig, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1420 | NoVerify)) { |
| 1421 | errs() << argv[0] << ": target does not support generation of this" |
| 1422 | << " file type!\n"; |
| 1423 | return 1; |
| 1424 | } |
| 1425 | |
| 1426 | // Before executing passes, print the final values of the LLVM options. |
| 1427 | cl::PrintOptionValues(); |
| 1428 | |
| 1429 | PM.run(mod); |
| 1430 | } |
| 1431 | |
Shih-wei Liao | d0fbbb2 | 2013-01-03 06:23:31 -0800 | [diff] [blame] | 1432 | if (mcld::getDiagnosticEngine().getPrinter()->getNumErrors()) |
| 1433 | return 1; |
| 1434 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1435 | // Declare success. |
| 1436 | Out->keep(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |