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 | //===----------------------------------------------------------------------===// |
| 9 | #include <mcld/Target/TargetMachine.h> |
| 10 | #include <mcld/Support/TargetSelect.h> |
| 11 | #include <mcld/Support/TargetRegistry.h> |
| 12 | #include <mcld/Support/CommandLine.h> |
| 13 | #include <mcld/Support/DerivedPositionDependentOptions.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 14 | #include <mcld/Support/Path.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 15 | #include <mcld/Support/RealPath.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 16 | #include <mcld/Support/MsgHandling.h> |
| 17 | #include <mcld/Support/FileSystem.h> |
| 18 | #include <mcld/Support/raw_ostream.h> |
| 19 | #include <mcld/LD/DiagnosticLineInfo.h> |
| 20 | #include <mcld/LD/TextDiagnosticPrinter.h> |
| 21 | #include <mcld/MC/MCLDInfo.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 22 | #include <mcld/CodeGen/SectLinkerOption.h> |
| 23 | |
| 24 | #include <llvm/Module.h> |
| 25 | #include <llvm/PassManager.h> |
| 26 | #include <llvm/Pass.h> |
| 27 | #include <llvm/ADT/Triple.h> |
| 28 | #include <llvm/LLVMContext.h> |
| 29 | #include <llvm/MC/SubtargetFeature.h> |
| 30 | #include <llvm/Support/CommandLine.h> |
| 31 | #include <llvm/Support/Debug.h> |
| 32 | #include <llvm/Support/FormattedStream.h> |
| 33 | #include <llvm/Support/Host.h> |
| 34 | #include <llvm/Support/IRReader.h> |
| 35 | #include <llvm/Support/ManagedStatic.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 36 | #include <llvm/Support/Signals.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 37 | #include <llvm/Support/TargetRegistry.h> |
| 38 | #include <llvm/Support/TargetSelect.h> |
| 39 | #include <llvm/Support/ToolOutputFile.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 40 | #include <llvm/Support/Process.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 41 | #include <llvm/Target/TargetData.h> |
| 42 | #include <llvm/Target/TargetMachine.h> |
| 43 | |
| 44 | using namespace llvm; |
| 45 | |
| 46 | #ifdef ENABLE_UNITTEST |
| 47 | #include <gtest.h> |
| 48 | |
| 49 | static cl::opt<bool> |
| 50 | UnitTest("unittest", cl::desc("do unit test") ); |
| 51 | |
| 52 | int unit_test( int argc, char* argv[] ) |
| 53 | { |
| 54 | testing::InitGoogleTest( &argc, argv ); |
| 55 | return RUN_ALL_TESTS(); |
| 56 | } |
| 57 | |
| 58 | #endif |
| 59 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 60 | // General options for llc. Other pass-specific options are specified |
| 61 | // within the corresponding llc passes, and target-specific options |
| 62 | // and back-end code generation options are specified with the target machine. |
| 63 | // |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 64 | // Determine optimization level. |
| 65 | static cl::opt<char> |
| 66 | OptLevel("O", |
| 67 | cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] " |
| 68 | "(default = '-O2')"), |
| 69 | cl::Prefix, |
| 70 | cl::ZeroOrMore, |
| 71 | cl::init(' ')); |
| 72 | |
| 73 | static cl::opt<std::string> |
| 74 | TargetTriple("mtriple", cl::desc("Override target triple for module")); |
| 75 | |
| 76 | static cl::opt<std::string> |
| 77 | MArch("march", cl::desc("Architecture to generate code for (see --version)")); |
| 78 | |
| 79 | static cl::opt<std::string> |
| 80 | MCPU("mcpu", |
| 81 | cl::desc("Target a specific cpu type (-mcpu=help for details)"), |
| 82 | cl::value_desc("cpu-name"), |
| 83 | cl::init("")); |
| 84 | |
| 85 | static cl::list<std::string> |
| 86 | MAttrs("mattr", |
| 87 | cl::CommaSeparated, |
| 88 | cl::desc("Target specific attributes (-mattr=help for details)"), |
| 89 | cl::value_desc("a1,+a2,-a3,...")); |
| 90 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 91 | static cl::opt<llvm::CodeModel::Model> |
| 92 | CMModel("code-model", |
| 93 | cl::desc("Choose code model"), |
| 94 | cl::init(CodeModel::Default), |
| 95 | cl::values(clEnumValN(CodeModel::Default, "default", |
| 96 | "Target default code model"), |
| 97 | clEnumValN(CodeModel::Small, "small", |
| 98 | "Small code model"), |
| 99 | clEnumValN(CodeModel::Kernel, "kernel", |
| 100 | "Kernel code model"), |
| 101 | clEnumValN(CodeModel::Medium, "medium", |
| 102 | "Medium code model"), |
| 103 | clEnumValN(CodeModel::Large, "large", |
| 104 | "Large code model"), |
| 105 | clEnumValEnd)); |
| 106 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 107 | cl::opt<bool> NoVerify("disable-verify", cl::Hidden, |
| 108 | cl::desc("Do not verify input module")); |
| 109 | |
| 110 | static cl::opt<bool> |
| 111 | EnableFPMAD("enable-fp-mad", |
| 112 | cl::desc("Enable less precise MAD instructions to be generated"), |
| 113 | cl::init(false)); |
| 114 | |
| 115 | static cl::opt<bool> |
| 116 | PrintCode("print-machineinstrs", |
| 117 | cl::desc("Print generated machine code"), |
| 118 | cl::init(false)); |
| 119 | |
| 120 | static cl::opt<bool> |
| 121 | DisableFPElim("disable-fp-elim", |
| 122 | cl::desc("Disable frame pointer elimination optimization"), |
| 123 | cl::init(false)); |
| 124 | |
| 125 | static cl::opt<bool> |
| 126 | DisableFPElimNonLeaf("disable-non-leaf-fp-elim", |
| 127 | cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"), |
| 128 | cl::init(false)); |
| 129 | |
| 130 | static cl::opt<bool> |
| 131 | DisableExcessPrecision("disable-excess-fp-precision", |
| 132 | cl::desc("Disable optimizations that may increase FP precision"), |
| 133 | cl::init(false)); |
| 134 | |
| 135 | static cl::opt<bool> |
| 136 | EnableUnsafeFPMath("enable-unsafe-fp-math", |
| 137 | cl::desc("Enable optimizations that may decrease FP precision"), |
| 138 | cl::init(false)); |
| 139 | |
| 140 | static cl::opt<bool> |
| 141 | EnableNoInfsFPMath("enable-no-infs-fp-math", |
| 142 | cl::desc("Enable FP math optimizations that assume no +-Infs"), |
| 143 | cl::init(false)); |
| 144 | |
| 145 | static cl::opt<bool> |
| 146 | EnableNoNaNsFPMath("enable-no-nans-fp-math", |
| 147 | cl::desc("Enable FP math optimizations that assume no NaNs"), |
| 148 | cl::init(false)); |
| 149 | |
| 150 | static cl::opt<bool> |
| 151 | EnableHonorSignDependentRoundingFPMath("enable-sign-dependent-rounding-fp-math", |
| 152 | cl::Hidden, |
| 153 | cl::desc("Force codegen to assume rounding mode can change dynamically"), |
| 154 | cl::init(false)); |
| 155 | |
| 156 | static cl::opt<bool> |
| 157 | GenerateSoftFloatCalls("soft-float", |
| 158 | cl::desc("Generate software floating point library calls"), |
| 159 | cl::init(false)); |
| 160 | |
| 161 | static cl::opt<llvm::FloatABI::ABIType> |
| 162 | FloatABIForCalls("float-abi", |
| 163 | cl::desc("Choose float ABI type"), |
| 164 | cl::init(FloatABI::Default), |
| 165 | cl::values( |
| 166 | clEnumValN(FloatABI::Default, "default", |
| 167 | "Target default float ABI type"), |
| 168 | clEnumValN(FloatABI::Soft, "soft", |
| 169 | "Soft float ABI (implied by -soft-float)"), |
| 170 | clEnumValN(FloatABI::Hard, "hard", |
| 171 | "Hard float ABI (uses FP registers)"), |
| 172 | clEnumValEnd)); |
| 173 | |
| 174 | static cl::opt<bool> |
| 175 | DontPlaceZerosInBSS("nozero-initialized-in-bss", |
| 176 | cl::desc("Don't place zero-initialized symbols into bss section"), |
| 177 | cl::init(false)); |
| 178 | |
| 179 | static cl::opt<bool> |
| 180 | EnableJITExceptionHandling("jit-enable-eh", |
| 181 | cl::desc("Emit exception handling information"), |
| 182 | cl::init(false)); |
| 183 | |
| 184 | // In debug builds, make this default to true. |
| 185 | #ifdef NDEBUG |
| 186 | #define EMIT_DEBUG false |
| 187 | #else |
| 188 | #define EMIT_DEBUG true |
| 189 | #endif |
| 190 | static cl::opt<bool> |
| 191 | EmitJitDebugInfo("jit-emit-debug", |
| 192 | cl::desc("Emit debug information to debugger"), |
| 193 | cl::init(EMIT_DEBUG)); |
| 194 | #undef EMIT_DEBUG |
| 195 | |
| 196 | static cl::opt<bool> |
| 197 | EmitJitDebugInfoToDisk("jit-emit-debug-to-disk", |
| 198 | cl::Hidden, |
| 199 | cl::desc("Emit debug info objfiles to disk"), |
| 200 | cl::init(false)); |
| 201 | |
| 202 | static cl::opt<bool> |
| 203 | EnableGuaranteedTailCallOpt("tailcallopt", |
| 204 | cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."), |
| 205 | cl::init(false)); |
| 206 | |
| 207 | static cl::opt<unsigned> |
| 208 | OverrideStackAlignment("stack-alignment", |
| 209 | cl::desc("Override default stack alignment"), |
| 210 | cl::init(0)); |
| 211 | |
| 212 | static cl::opt<bool> |
| 213 | EnableRealignStack("realign-stack", |
| 214 | cl::desc("Realign stack if needed"), |
| 215 | cl::init(true)); |
| 216 | |
| 217 | static cl::opt<bool> |
| 218 | DisableSwitchTables(cl::Hidden, "disable-jump-tables", |
| 219 | cl::desc("Do not generate jump tables."), |
| 220 | cl::init(false)); |
| 221 | |
| 222 | static cl::opt<std::string> |
| 223 | TrapFuncName("trap-func", cl::Hidden, |
| 224 | cl::desc("Emit a call to trap function rather than a trap instruction"), |
| 225 | cl::init("")); |
| 226 | |
| 227 | static cl::opt<bool> |
| 228 | SegmentedStacks("segmented-stacks", |
| 229 | cl::desc("Use segmented stacks if possible."), |
| 230 | cl::init(false)); |
| 231 | |
| 232 | //===----------------------------------------------------------------------===// |
| 233 | // Command Line Options |
| 234 | // There are four kinds of command line options: |
| 235 | // 1. input, (may be a file, such as -m and /tmp/XXXX.o.) |
| 236 | // 2. attribute of inputs, (describing the attributes of inputs, such as |
| 237 | // --as-needed and --whole-archive. usually be positional.) |
| 238 | // 3. scripting options, (represent a subset of link scripting language, such |
| 239 | // as --defsym.) |
| 240 | // 4. and general options. (the rest of options) |
| 241 | //===----------------------------------------------------------------------===// |
| 242 | // General Options |
| 243 | 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] | 244 | ArgBitcodeFilename("dB", |
| 245 | cl::desc("set default bitcode"), |
| 246 | cl::value_desc("bitcode")); |
| 247 | |
| 248 | static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> > |
| 249 | ArgOutputFilename("o", |
| 250 | cl::desc("Output filename"), |
| 251 | cl::value_desc("filename")); |
| 252 | |
| 253 | static cl::alias |
| 254 | AliasOutputFilename("output", |
| 255 | cl::desc("alias for -o"), |
| 256 | cl::aliasopt(ArgOutputFilename)); |
| 257 | |
| 258 | 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] | 259 | ArgSysRoot("sysroot", |
| 260 | cl::desc("Use directory as the location of the sysroot, overriding the configure-time default."), |
| 261 | cl::value_desc("directory"), |
| 262 | cl::ValueRequired); |
| 263 | |
| 264 | static cl::list<mcld::MCLDDirectory, bool, llvm::cl::parser<mcld::MCLDDirectory> > |
| 265 | ArgSearchDirList("L", |
| 266 | cl::ZeroOrMore, |
| 267 | cl::desc("Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts."), |
| 268 | cl::value_desc("searchdir"), |
| 269 | cl::Prefix); |
| 270 | |
| 271 | static cl::alias |
| 272 | ArgSearchDirListAlias("library-path", |
| 273 | cl::desc("alias for -L"), |
| 274 | cl::aliasopt(ArgSearchDirList)); |
| 275 | |
| 276 | static cl::opt<bool> |
| 277 | ArgTrace("t", |
| 278 | cl::desc("Print the names of the input files as ld processes them.")); |
| 279 | |
| 280 | static cl::alias |
| 281 | ArgTraceAlias("trace", |
| 282 | cl::desc("alias for -t"), |
| 283 | cl::aliasopt(ArgTrace)); |
| 284 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 285 | static cl::opt<int> |
| 286 | ArgVerbose("verbose", |
| 287 | cl::init(-1), |
| 288 | 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] | 289 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 290 | static cl::opt<bool> |
| 291 | ArgVersion("V", |
| 292 | cl::init(false), |
| 293 | cl::desc("Display the version number for MCLinker.")); |
| 294 | |
| 295 | static cl::opt<int> |
| 296 | ArgMaxErrorNum("error-limit", |
| 297 | cl::init(-1), |
| 298 | cl::desc("limits the maximum number of erros.")); |
| 299 | |
| 300 | static cl::opt<int> |
| 301 | ArgMaxWarnNum("warning-limit", |
| 302 | cl::init(-1), |
| 303 | cl::desc("limits the maximum number of warnings.")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 304 | |
| 305 | static cl::opt<std::string> |
| 306 | ArgEntry("e", |
| 307 | cl::desc("Use entry as the explicit symbol for beginning execution of your program."), |
| 308 | cl::value_desc("entry"), |
| 309 | cl::ValueRequired); |
| 310 | |
| 311 | static cl::alias |
| 312 | ArgEntryAlias("entry", |
| 313 | cl::desc("alias for -e"), |
| 314 | cl::aliasopt(ArgEntry)); |
| 315 | |
| 316 | static cl::opt<bool> |
| 317 | ArgBsymbolic("Bsymbolic", |
| 318 | cl::desc("Bind references within the shared library."), |
| 319 | cl::init(false)); |
| 320 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 321 | static cl::opt<bool> |
| 322 | ArgBgroup("Bgroup", |
| 323 | cl::desc("Info the dynamic linker to perform lookups only inside the group."), |
| 324 | cl::init(false)); |
| 325 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 326 | static cl::opt<std::string> |
| 327 | ArgSOName("soname", |
| 328 | cl::desc("Set internal name of shared library"), |
| 329 | cl::value_desc("name")); |
| 330 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 331 | static cl::opt<bool> |
| 332 | ArgNoUndefined("no-undefined", |
| 333 | cl::desc("Do not allow unresolved references"), |
| 334 | cl::init(false)); |
| 335 | |
| 336 | static cl::opt<bool> |
| 337 | ArgAllowMulDefs("allow-multiple-definition", |
| 338 | cl::desc("Allow multiple definition"), |
| 339 | cl::init(false)); |
| 340 | |
| 341 | static cl::opt<bool> |
| 342 | ArgEhFrameHdr("eh-frame-hdr", |
| 343 | cl::desc("Request creation of \".eh_frame_hdr\" section and ELF \"PT_GNU_EH_FRAME\" segment header."), |
| 344 | cl::init(false)); |
| 345 | |
| 346 | static cl::list<mcld::ZOption, bool, llvm::cl::parser<mcld::ZOption> > |
| 347 | ArgZOptionList("z", |
| 348 | cl::ZeroOrMore, |
| 349 | cl::desc("The -z options for GNU ld compatibility."), |
| 350 | cl::value_desc("keyword"), |
| 351 | cl::Prefix); |
| 352 | |
| 353 | cl::opt<mcld::CodeGenFileType> |
| 354 | ArgFileType("filetype", cl::init(mcld::CGFT_EXEFile), |
| 355 | cl::desc("Choose a file type (not all types are supported by all targets):"), |
| 356 | cl::values( |
| 357 | clEnumValN(mcld::CGFT_ASMFile, "asm", |
| 358 | "Emit an assembly ('.s') file"), |
| 359 | clEnumValN(mcld::CGFT_OBJFile, "obj", |
| 360 | "Emit a relocatable object ('.o') file"), |
| 361 | clEnumValN(mcld::CGFT_DSOFile, "dso", |
| 362 | "Emit an dynamic shared object ('.so') file"), |
| 363 | clEnumValN(mcld::CGFT_EXEFile, "exe", |
| 364 | "Emit a executable ('.exe') file"), |
| 365 | clEnumValN(mcld::CGFT_NULLFile, "null", |
| 366 | "Emit nothing, for performance testing"), |
| 367 | clEnumValEnd)); |
| 368 | |
| 369 | static cl::opt<bool> |
| 370 | ArgShared("shared", |
| 371 | cl::desc("Create a shared library."), |
| 372 | cl::init(false)); |
| 373 | |
| 374 | static cl::alias |
| 375 | ArgSharedAlias("Bshareable", |
| 376 | cl::desc("alias for -shared"), |
| 377 | cl::aliasopt(ArgShared)); |
| 378 | |
| 379 | static cl::opt<bool> |
| 380 | ArgPIE("pie", |
| 381 | cl::desc("Emit a position-independent executable file"), |
| 382 | cl::init(false)); |
| 383 | |
| 384 | static cl::opt<Reloc::Model> |
| 385 | ArgRelocModel("relocation-model", |
| 386 | cl::desc("Choose relocation model"), |
| 387 | cl::init(Reloc::Default), |
| 388 | cl::values( |
| 389 | clEnumValN(Reloc::Default, "default", |
| 390 | "Target default relocation model"), |
| 391 | clEnumValN(Reloc::Static, "static", |
| 392 | "Non-relocatable code"), |
| 393 | clEnumValN(Reloc::PIC_, "pic", |
| 394 | "Fully relocatable, position independent code"), |
| 395 | clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", |
| 396 | "Relocatable external references, non-relocatable code"), |
| 397 | clEnumValEnd)); |
| 398 | |
| 399 | static cl::opt<bool> |
| 400 | ArgFPIC("fPIC", |
| 401 | cl::desc("Set relocation model to pic. The same as -relocation-model=pic."), |
| 402 | cl::init(false)); |
| 403 | |
| 404 | static cl::opt<std::string> |
| 405 | ArgDyld("dynamic-linker", |
| 406 | cl::desc("Set the name of the dynamic linker."), |
| 407 | cl::value_desc("Program")); |
| 408 | |
| 409 | namespace color { |
| 410 | enum Color { |
| 411 | Never, |
| 412 | Always, |
| 413 | Auto |
| 414 | }; |
| 415 | } // namespace of color |
| 416 | |
| 417 | static cl::opt<color::Color> |
| 418 | ArgColor("color", |
| 419 | cl::value_desc("WHEN"), |
| 420 | cl::desc("Surround the result strings with the marker"), |
| 421 | cl::init(color::Auto), |
| 422 | cl::values( |
| 423 | clEnumValN(color::Never, "never", |
| 424 | "do not surround result strings"), |
| 425 | clEnumValN(color::Always, "always", |
| 426 | "always surround result strings, even the output is a plain file"), |
| 427 | clEnumValN(color::Auto, "auto", |
| 428 | "surround result strings only if the output is a tty"), |
| 429 | clEnumValEnd)); |
| 430 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 431 | //===----------------------------------------------------------------------===// |
| 432 | // Inputs |
| 433 | static cl::list<mcld::sys::fs::Path> |
| 434 | ArgInputObjectFiles(cl::Positional, |
| 435 | cl::desc("[input object files]"), |
| 436 | cl::ZeroOrMore); |
| 437 | |
| 438 | static cl::list<std::string> |
| 439 | ArgNameSpecList("l", |
| 440 | cl::ZeroOrMore, |
| 441 | cl::desc("Add the archive or object file specified by namespec to the list of files to link."), |
| 442 | cl::value_desc("namespec"), |
| 443 | cl::Prefix); |
| 444 | |
| 445 | static cl::alias |
| 446 | ArgNameSpecListAlias("library", |
| 447 | cl::desc("alias for -l"), |
| 448 | cl::aliasopt(ArgNameSpecList)); |
| 449 | |
| 450 | static cl::list<bool> |
| 451 | ArgStartGroupList("start-group", |
| 452 | cl::ValueDisallowed, |
| 453 | cl::desc("start to record a group of archives")); |
| 454 | |
| 455 | static cl::alias |
| 456 | ArgStartGroupListAlias("(", |
| 457 | cl::desc("alias for --start-group"), |
| 458 | cl::aliasopt(ArgStartGroupList)); |
| 459 | |
| 460 | static cl::list<bool> |
| 461 | ArgEndGroupList("end-group", |
| 462 | cl::ValueDisallowed, |
| 463 | cl::desc("stop recording a group of archives")); |
| 464 | |
| 465 | static cl::alias |
| 466 | ArgEndGroupListAlias(")", |
| 467 | cl::desc("alias for --end-group"), |
| 468 | cl::aliasopt(ArgEndGroupList)); |
| 469 | |
| 470 | //===----------------------------------------------------------------------===// |
| 471 | // Attributes of Inputs |
| 472 | static cl::list<bool> |
| 473 | ArgWholeArchiveList("whole-archive", |
| 474 | cl::ValueDisallowed, |
| 475 | cl::desc("For each archive mentioned on the command line after the --whole-archive option, include all object files in the archive.")); |
| 476 | |
| 477 | static cl::list<bool> |
| 478 | ArgNoWholeArchiveList("no-whole-archive", |
| 479 | cl::ValueDisallowed, |
| 480 | cl::desc("Turn off the effect of the --whole-archive option for subsequent archive files.")); |
| 481 | |
| 482 | static cl::list<bool> |
| 483 | ArgAsNeededList("as-needed", |
| 484 | cl::ValueDisallowed, |
| 485 | cl::desc("This option affects ELF DT_NEEDED tags for dynamic libraries mentioned on the command line after the --as-needed option.")); |
| 486 | |
| 487 | static cl::list<bool> |
| 488 | ArgNoAsNeededList("no-as-needed", |
| 489 | cl::ValueDisallowed, |
| 490 | cl::desc("Turn off the effect of the --as-needed option for subsequent dynamic libraries")); |
| 491 | |
| 492 | static cl::list<bool> |
| 493 | ArgAddNeededList("add-needed", |
| 494 | cl::ValueDisallowed, |
| 495 | cl::desc("--add-needed causes DT_NEEDED tags are always emitted for those libraries from DT_NEEDED tags. This is the default behavior.")); |
| 496 | |
| 497 | static cl::list<bool> |
| 498 | ArgNoAddNeededList("no-add-needed", |
| 499 | cl::ValueDisallowed, |
| 500 | cl::desc("--no-add-needed causes DT_NEEDED tags will never be emitted for those libraries from DT_NEEDED tags")); |
| 501 | |
| 502 | static cl::list<bool> |
| 503 | ArgBDynamicList("Bdynamic", |
| 504 | cl::ValueDisallowed, |
| 505 | cl::desc("Link against dynamic library")); |
| 506 | |
| 507 | static cl::alias |
| 508 | ArgBDynamicListAlias1("dy", |
| 509 | cl::desc("alias for --Bdynamic"), |
| 510 | cl::aliasopt(ArgBDynamicList)); |
| 511 | |
| 512 | static cl::alias |
| 513 | ArgBDynamicListAlias2("call_shared", |
| 514 | cl::desc("alias for --Bdynamic"), |
| 515 | cl::aliasopt(ArgBDynamicList)); |
| 516 | |
| 517 | static cl::list<bool> |
| 518 | ArgBStaticList("Bstatic", |
| 519 | cl::ValueDisallowed, |
| 520 | cl::desc("Link against static library")); |
| 521 | |
| 522 | static cl::alias |
| 523 | ArgBStaticListAlias1("dn", |
| 524 | cl::desc("alias for --Bstatic"), |
| 525 | cl::aliasopt(ArgBStaticList)); |
| 526 | |
| 527 | static cl::alias |
| 528 | ArgBStaticListAlias2("static", |
| 529 | cl::desc("alias for --Bstatic"), |
| 530 | cl::aliasopt(ArgBStaticList)); |
| 531 | |
| 532 | static cl::alias |
| 533 | ArgBStaticListAlias3("non_shared", |
| 534 | cl::desc("alias for --Bstatic"), |
| 535 | cl::aliasopt(ArgBStaticList)); |
| 536 | |
| 537 | //===----------------------------------------------------------------------===// |
| 538 | // Scripting Options |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 539 | static cl::list<std::string> |
| 540 | ArgWrapList("wrap", |
| 541 | cl::ZeroOrMore, |
| 542 | cl::desc("Use a wrap function fo symbol."), |
| 543 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 544 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 545 | static cl::list<std::string> |
| 546 | ArgPortList("portable", |
| 547 | cl::ZeroOrMore, |
| 548 | cl::desc("Use a portable function fo symbol."), |
| 549 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 550 | |
| 551 | //===----------------------------------------------------------------------===// |
| 552 | /// non-member functions |
| 553 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 554 | /// GetOutputStream - get the output stream. |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 555 | static tool_output_file *GetOutputStream(const char* pTargetName, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 556 | Triple::OSType pOSType, |
| 557 | mcld::CodeGenFileType pFileType, |
| 558 | const mcld::sys::fs::Path& pInputFilename, |
| 559 | mcld::sys::fs::Path& pOutputFilename) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 560 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 561 | if (pOutputFilename.empty()) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 562 | if (0 == pInputFilename.native().compare("-")) |
| 563 | pOutputFilename.assign("-"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 564 | else { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 565 | switch(pFileType) { |
| 566 | case mcld::CGFT_ASMFile: { |
| 567 | if (0 == pInputFilename.native().compare("-")) |
| 568 | pOutputFilename.assign("_out"); |
| 569 | else |
| 570 | pOutputFilename.assign(pInputFilename.stem().native()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 571 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 572 | if (0 == strcmp(pTargetName, "c")) |
| 573 | pOutputFilename.native() += ".cbe.c"; |
| 574 | else if (0 == strcmp(pTargetName, "cpp")) |
| 575 | pOutputFilename.native() += ".cpp"; |
| 576 | else |
| 577 | pOutputFilename.native() += ".s"; |
| 578 | } |
| 579 | break; |
| 580 | |
| 581 | case mcld::CGFT_OBJFile: { |
| 582 | if (0 == pInputFilename.native().compare("-")) |
| 583 | pOutputFilename.assign("_out"); |
| 584 | else |
| 585 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 586 | |
| 587 | if (pOSType == Triple::Win32) |
| 588 | pOutputFilename.native() += ".obj"; |
| 589 | else |
| 590 | pOutputFilename.native() += ".o"; |
| 591 | } |
| 592 | break; |
| 593 | |
| 594 | case mcld::CGFT_DSOFile: { |
| 595 | if (Triple::Win32 == pOSType) { |
| 596 | if (0 == pInputFilename.native().compare("-")) |
| 597 | pOutputFilename.assign("_out"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 598 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 599 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 600 | pOutputFilename.native() += ".dll"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 601 | } |
| 602 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 603 | pOutputFilename.assign("a.out"); |
| 604 | } |
| 605 | break; |
| 606 | |
| 607 | case mcld::CGFT_EXEFile: { |
| 608 | if (Triple::Win32 == pOSType) { |
| 609 | if (0 == pInputFilename.native().compare("-")) |
| 610 | pOutputFilename.assign("_out"); |
| 611 | else |
| 612 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 613 | pOutputFilename.native() += ".exe"; |
| 614 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 615 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 616 | pOutputFilename.assign("a.out"); |
| 617 | } |
| 618 | break; |
| 619 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 620 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 621 | break; |
| 622 | default: |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 623 | llvm::report_fatal_error("Unknown output file type.\n"); |
| 624 | } // end of switch |
| 625 | } // end of ! pInputFilename == "-" |
| 626 | } // end of if empty pOutputFilename |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 627 | |
| 628 | // Decide if we need "binary" output. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 629 | unsigned int fd_flags = 0x0; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 630 | switch (pFileType) { |
| 631 | default: assert(0 && "Unknown file type"); |
| 632 | case mcld::CGFT_ASMFile: |
| 633 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 634 | case mcld::CGFT_OBJFile: |
| 635 | case mcld::CGFT_DSOFile: |
| 636 | case mcld::CGFT_EXEFile: |
| 637 | case mcld::CGFT_NULLFile: |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 638 | fd_flags |= raw_fd_ostream::F_Binary; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 639 | break; |
| 640 | } |
| 641 | |
| 642 | // Open the file. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 643 | std::string err_mesg; |
| 644 | tool_output_file *result_output = |
| 645 | new tool_output_file(pOutputFilename.c_str(), |
| 646 | err_mesg, |
| 647 | fd_flags); |
| 648 | if (!err_mesg.empty()) { |
| 649 | errs() << err_mesg << '\n'; |
| 650 | delete result_output; |
| 651 | return NULL; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 652 | } |
| 653 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 654 | return result_output; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 657 | static bool ShouldColorize() |
| 658 | { |
| 659 | const char* term = getenv("TERM"); |
| 660 | return term && (0 != strcmp(term, "dumb")); |
| 661 | } |
| 662 | |
| 663 | static bool ProcessLinkerOptionsFromCommand(mcld::MCLDInfo& pLDInfo) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 664 | // ----- Set up General Options ----- // |
| 665 | // set up soname |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 666 | pLDInfo.output().setSOName(ArgSOName); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 667 | |
| 668 | // set up sysroot |
| 669 | if (!ArgSysRoot.empty()) { |
| 670 | if (exists(ArgSysRoot) && is_directory(ArgSysRoot)) |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 671 | pLDInfo.options().setSysroot(ArgSysRoot); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 672 | } |
| 673 | |
| 674 | // add all search directories |
| 675 | cl::list<mcld::MCLDDirectory>::iterator sd; |
| 676 | cl::list<mcld::MCLDDirectory>::iterator sdEnd = ArgSearchDirList.end(); |
| 677 | for (sd=ArgSearchDirList.begin(); sd!=sdEnd; ++sd) { |
| 678 | if (sd->isInSysroot()) |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 679 | sd->setSysroot(pLDInfo.options().sysroot()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 680 | if (exists(sd->path()) && is_directory(sd->path())) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 681 | pLDInfo.options().directories().add(*sd); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 682 | } |
| 683 | else { |
| 684 | // FIXME: need a warning function |
| 685 | errs() << "WARNING: can not open search directory `-L" |
| 686 | << sd->name() |
| 687 | << "'.\n"; |
| 688 | } |
| 689 | } |
| 690 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 691 | pLDInfo.options().setPIE(ArgPIE); |
| 692 | pLDInfo.options().setTrace(ArgTrace); |
| 693 | pLDInfo.options().setVerbose(ArgVerbose); |
| 694 | pLDInfo.options().setMaxErrorNum(ArgMaxErrorNum); |
| 695 | pLDInfo.options().setMaxWarnNum(ArgMaxWarnNum); |
| 696 | pLDInfo.options().setEntry(ArgEntry); |
| 697 | pLDInfo.options().setBsymbolic(ArgBsymbolic); |
| 698 | pLDInfo.options().setBgroup(ArgBgroup); |
| 699 | pLDInfo.options().setDyld(ArgDyld); |
| 700 | pLDInfo.options().setNoUndefined(ArgNoUndefined); |
| 701 | pLDInfo.options().setMulDefs(ArgAllowMulDefs); |
| 702 | pLDInfo.options().setEhFrameHdr(ArgEhFrameHdr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 703 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 704 | // set up rename map, for --wrap |
| 705 | cl::list<std::string>::iterator wname; |
| 706 | cl::list<std::string>::iterator wnameEnd = ArgWrapList.end(); |
| 707 | for (wname = ArgWrapList.begin(); wname != wnameEnd; ++wname) { |
| 708 | bool exist = false; |
| 709 | |
| 710 | // add wname -> __wrap_wname |
| 711 | mcld::StringEntry<llvm::StringRef>* to_wrap = |
| 712 | pLDInfo.scripts().renameMap().insert(*wname, exist); |
| 713 | |
| 714 | std::string to_wrap_str = "__wrap_" + *wname; |
| 715 | to_wrap->setValue(to_wrap_str); |
| 716 | |
| 717 | if (exist) |
| 718 | mcld::warning(mcld::diag::rewrap) << *wname << to_wrap_str; |
| 719 | |
| 720 | // add __real_wname -> wname |
| 721 | std::string from_real_str = "__real_" + *wname; |
| 722 | mcld::StringEntry<llvm::StringRef>* from_real = |
| 723 | pLDInfo.scripts().renameMap().insert(from_real_str, exist); |
| 724 | from_real->setValue(*wname); |
| 725 | if (exist) |
| 726 | mcld::warning(mcld::diag::rewrap) << *wname << from_real_str; |
| 727 | } // end of for |
| 728 | |
| 729 | // set up rename map, for --portable |
| 730 | cl::list<std::string>::iterator pname; |
| 731 | cl::list<std::string>::iterator pnameEnd = ArgPortList.end(); |
| 732 | for (pname = ArgPortList.begin(); pname != pnameEnd; ++pname) { |
| 733 | bool exist = false; |
| 734 | |
| 735 | // add pname -> pname_portable |
| 736 | mcld::StringEntry<llvm::StringRef>* to_port = |
| 737 | pLDInfo.scripts().renameMap().insert(*pname, exist); |
| 738 | |
| 739 | std::string to_port_str = *pname + "_portable"; |
| 740 | to_port->setValue(to_port_str); |
| 741 | |
| 742 | if (exist) |
| 743 | mcld::warning(mcld::diag::rewrap) << *pname << to_port_str; |
| 744 | |
| 745 | // add __real_pname -> pname |
| 746 | std::string from_real_str = "__real_" + *pname; |
| 747 | mcld::StringEntry<llvm::StringRef>* from_real = |
| 748 | pLDInfo.scripts().renameMap().insert(from_real_str, exist); |
| 749 | |
| 750 | from_real->setValue(*pname); |
| 751 | if (exist) |
| 752 | mcld::warning(mcld::diag::rewrap) << *pname << from_real_str; |
| 753 | } // end of for |
| 754 | |
| 755 | // set up colorize |
| 756 | switch (ArgColor) { |
| 757 | case color::Never: |
| 758 | pLDInfo.options().setColor(false); |
| 759 | break; |
| 760 | case color::Always: |
| 761 | pLDInfo.options().setColor(true); |
| 762 | break; |
| 763 | case color::Auto: |
| 764 | bool color_option = ShouldColorize() && |
| 765 | llvm::sys::Process::FileDescriptorIsDisplayed(STDOUT_FILENO); |
| 766 | pLDInfo.options().setColor(color_option); |
| 767 | break; |
| 768 | } |
| 769 | |
| 770 | // add -z options |
| 771 | cl::list<mcld::ZOption>::iterator zOpt; |
| 772 | cl::list<mcld::ZOption>::iterator zOptEnd = ArgZOptionList.end(); |
| 773 | for (zOpt = ArgZOptionList.begin(); zOpt != zOptEnd; ++zOpt) { |
| 774 | pLDInfo.options().addZOption(*zOpt); |
| 775 | } |
| 776 | |
| 777 | // ----- Set up Script Options ----- // |
| 778 | |
| 779 | return true; |
| 780 | } |
| 781 | |
| 782 | static bool ProcessLinkerInputsFromCommand(mcld::SectLinkerOption &pOption) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 783 | // ----- Set up Inputs ----- // |
| 784 | // add all start-group |
| 785 | cl::list<bool>::iterator sg; |
| 786 | cl::list<bool>::iterator sgEnd = ArgStartGroupList.end(); |
| 787 | for (sg=ArgStartGroupList.begin(); sg!=sgEnd; ++sg) { |
| 788 | // calculate position |
| 789 | pOption.appendOption(new mcld::StartGroupOption( |
| 790 | ArgStartGroupList.getPosition(sg-ArgStartGroupList.begin()))); |
| 791 | } |
| 792 | |
| 793 | // add all end-group |
| 794 | cl::list<bool>::iterator eg; |
| 795 | cl::list<bool>::iterator egEnd = ArgEndGroupList.end(); |
| 796 | for (eg=ArgEndGroupList.begin(); eg!=egEnd; ++eg) { |
| 797 | // calculate position |
| 798 | pOption.appendOption(new mcld::EndGroupOption( |
| 799 | ArgEndGroupList.getPosition(eg-ArgEndGroupList.begin()))); |
| 800 | } |
| 801 | |
| 802 | // add all namespecs |
| 803 | cl::list<std::string>::iterator ns; |
| 804 | cl::list<std::string>::iterator nsEnd = ArgNameSpecList.end(); |
| 805 | for (ns=ArgNameSpecList.begin(); ns!=nsEnd; ++ns) { |
| 806 | // calculate position |
| 807 | pOption.appendOption(new mcld::NamespecOption( |
| 808 | ArgNameSpecList.getPosition(ns-ArgNameSpecList.begin()), |
| 809 | *ns)); |
| 810 | } |
| 811 | |
| 812 | // add all object files |
| 813 | cl::list<mcld::sys::fs::Path>::iterator obj; |
| 814 | cl::list<mcld::sys::fs::Path>::iterator objEnd = ArgInputObjectFiles.end(); |
| 815 | for (obj=ArgInputObjectFiles.begin(); obj!=objEnd; ++obj) { |
| 816 | // calculate position |
| 817 | pOption.appendOption(new mcld::InputFileOption( |
| 818 | ArgInputObjectFiles.getPosition(obj-ArgInputObjectFiles.begin()), |
| 819 | *obj)); |
| 820 | } |
| 821 | |
| 822 | // ----- Set up Attributes of Inputs ----- // |
| 823 | // --whole-archive |
| 824 | cl::list<bool>::iterator attr = ArgWholeArchiveList.begin(); |
| 825 | cl::list<bool>::iterator attrEnd = ArgWholeArchiveList.end(); |
| 826 | for (; attr!=attrEnd; ++attr) { |
| 827 | pOption.appendOption(new mcld::WholeArchiveOption( |
| 828 | ArgWholeArchiveList.getPosition(attr-ArgWholeArchiveList.begin()))); |
| 829 | } |
| 830 | |
| 831 | // --no-whole-archive |
| 832 | attr = ArgNoWholeArchiveList.begin(); |
| 833 | attrEnd = ArgNoWholeArchiveList.end(); |
| 834 | for (; attr!=attrEnd; ++attr) { |
| 835 | pOption.appendOption(new mcld::NoWholeArchiveOption( |
| 836 | ArgNoWholeArchiveList.getPosition(attr-ArgNoWholeArchiveList.begin()))); |
| 837 | } |
| 838 | |
| 839 | // --as-needed |
| 840 | attr = ArgAsNeededList.begin(); |
| 841 | attrEnd = ArgAsNeededList.end(); |
| 842 | while(attr != attrEnd) { |
| 843 | pOption.appendOption(new mcld::AsNeededOption( |
| 844 | ArgAsNeededList.getPosition(attr-ArgAsNeededList.begin()))); |
| 845 | ++attr; |
| 846 | } |
| 847 | |
| 848 | // --no-as-needed |
| 849 | attr = ArgNoAsNeededList.begin(); |
| 850 | attrEnd = ArgNoAsNeededList.end(); |
| 851 | while(attr != attrEnd) { |
| 852 | pOption.appendOption(new mcld::NoAsNeededOption( |
| 853 | ArgNoAsNeededList.getPosition(attr-ArgNoAsNeededList.begin()))); |
| 854 | ++attr; |
| 855 | } |
| 856 | |
| 857 | // --add-needed |
| 858 | attr = ArgAddNeededList.begin(); |
| 859 | attrEnd = ArgAddNeededList.end(); |
| 860 | while(attr != attrEnd) { |
| 861 | pOption.appendOption(new mcld::AddNeededOption( |
| 862 | ArgAddNeededList.getPosition(attr-ArgAddNeededList.begin()))); |
| 863 | ++attr; |
| 864 | } |
| 865 | |
| 866 | // --no-add-needed |
| 867 | attr = ArgNoAddNeededList.begin(); |
| 868 | attrEnd = ArgNoAddNeededList.end(); |
| 869 | while(attr != attrEnd) { |
| 870 | pOption.appendOption(new mcld::NoAddNeededOption( |
| 871 | ArgNoAddNeededList.getPosition(attr-ArgNoAddNeededList.begin()))); |
| 872 | ++attr; |
| 873 | } |
| 874 | |
| 875 | // -Bdynamic |
| 876 | attr = ArgBDynamicList.begin(); |
| 877 | attrEnd = ArgBDynamicList.end(); |
| 878 | while(attr != attrEnd) { |
| 879 | pOption.appendOption(new mcld::BDynamicOption( |
| 880 | ArgBDynamicList.getPosition(attr-ArgBDynamicList.begin()))); |
| 881 | } |
| 882 | |
| 883 | // -Bstatic |
| 884 | attr = ArgBStaticList.begin(); |
| 885 | attrEnd = ArgBStaticList.end(); |
| 886 | while(attr != attrEnd) { |
| 887 | pOption.appendOption(new mcld::BStaticOption( |
| 888 | ArgBStaticList.getPosition(attr-ArgBStaticList.begin()))); |
| 889 | ++attr; |
| 890 | } |
| 891 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 892 | return true; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | int main( int argc, char* argv[] ) |
| 896 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 897 | LLVMContext &Context = getGlobalContext(); |
| 898 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame^] | 899 | cl::ParseCommandLineOptions(argc, argv, "MCLinker\n"); |
| 900 | |
| 901 | #ifdef ENABLE_UNITTEST |
| 902 | if (UnitTest) { |
| 903 | return unit_test( argc, argv ); |
| 904 | } |
| 905 | #endif |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 906 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 907 | // Initialize targets first, so that --version shows registered targets. |
| 908 | InitializeAllTargets(); |
| 909 | InitializeAllAsmPrinters(); |
| 910 | InitializeAllAsmParsers(); |
| 911 | InitializeAllTargetMCs(); |
| 912 | mcld::InitializeAllTargets(); |
| 913 | mcld::InitializeAllLinkers(); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 914 | mcld::InitializeAllDiagnostics(); |
| 915 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 916 | // Load the module to be compiled... |
| 917 | std::auto_ptr<Module> M; |
| 918 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 919 | // -shared |
| 920 | if (true == ArgShared) { |
| 921 | ArgFileType = mcld::CGFT_DSOFile; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 924 | // -V |
| 925 | if (ArgVersion) { |
| 926 | outs() << "MCLinker - "; |
| 927 | outs() << mcld::MCLDInfo::version(); |
| 928 | outs() << "\n"; |
| 929 | } |
| 930 | |
| 931 | if (ArgBitcodeFilename.empty() && |
| 932 | (mcld::CGFT_DSOFile != ArgFileType && |
| 933 | mcld::CGFT_EXEFile != ArgFileType)) { |
| 934 | // If the file is not given, forcefully read from stdin |
| 935 | if (ArgVerbose >= 0) { |
| 936 | errs() << "** The bitcode/llvm asm file is not given. Read from stdin.\n" |
| 937 | << "** Specify input bitcode/llvm asm file by\n\n" |
| 938 | << " llvm-mcld -dB [the bitcode/llvm asm]\n\n"; |
| 939 | } |
| 940 | |
| 941 | ArgBitcodeFilename.assign("-"); |
| 942 | } |
| 943 | |
| 944 | if (!ArgBitcodeFilename.empty()) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 945 | SMDiagnostic Err; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 946 | M.reset(ParseIRFile(ArgBitcodeFilename.native(), Err, Context)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 947 | |
| 948 | if (M.get() == 0) { |
| 949 | Err.print(argv[0], errs()); |
| 950 | errs() << "** Failed to to the given bitcode/llvm asm file '" |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 951 | << ArgBitcodeFilename.native() << "'. **\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 952 | return 1; |
| 953 | } |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 954 | } |
| 955 | else { |
| 956 | // If here, output must be dynamic shared object (mcld::CGFT_DSOFile) and |
| 957 | // executable file (mcld::CGFT_EXEFile). |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 958 | M.reset(new Module("Empty Module", Context)); |
| 959 | } |
| 960 | Module &mod = *M.get(); |
| 961 | |
| 962 | // If we are supposed to override the target triple, do so now. |
| 963 | Triple TheTriple; |
| 964 | if (!TargetTriple.empty()) { |
| 965 | TheTriple.setTriple(TargetTriple); |
| 966 | mod.setTargetTriple(TargetTriple); |
| 967 | } |
| 968 | |
| 969 | // User doesn't specify the triple from command. |
| 970 | if (TheTriple.getTriple().empty()) { |
| 971 | // Try to get one from the input Module. |
| 972 | const std::string &TripleStr = mod.getTargetTriple(); |
| 973 | |
| 974 | if (TripleStr.empty()) |
| 975 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 976 | else |
| 977 | TheTriple.setTriple(TripleStr); |
| 978 | } |
| 979 | |
| 980 | // Allocate target machine. First, check whether the user has explicitly |
| 981 | // specified an architecture to compile for. If so we have to look it up by |
| 982 | // name, because it might be a backend that has no mapping to a target triple. |
| 983 | const mcld::Target *TheTarget = 0; |
| 984 | if (!MArch.empty()) { |
| 985 | for (mcld::TargetRegistry::iterator it = mcld::TargetRegistry::begin(), |
| 986 | ie = mcld::TargetRegistry::end(); it != ie; ++it) { |
| 987 | if (MArch == (*it)->get()->getName()) { |
| 988 | TheTarget = *it; |
| 989 | break; |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | if (!TheTarget) { |
| 994 | errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; |
| 995 | return 1; |
| 996 | } |
| 997 | |
| 998 | // Adjust the triple to match (if known), otherwise stick with the |
| 999 | // module/host triple. |
| 1000 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); |
| 1001 | if (Type != Triple::UnknownArch) |
| 1002 | TheTriple.setArch(Type); |
| 1003 | } |
| 1004 | else { |
| 1005 | std::string Err; |
| 1006 | TheTarget = mcld::TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); |
| 1007 | if (TheTarget == 0) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1008 | errs() << "error: auto-selecting target `" << TheTriple.getTriple() |
| 1009 | << "'\n" |
| 1010 | << "Please use the -march option to explicitly select a target.\n" |
| 1011 | << "Example:\n" |
| 1012 | << " $ " << argv[0] << " -march=arm\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1013 | return 1; |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | // Package up features to be passed to target/subtarget |
| 1018 | std::string FeaturesStr; |
| 1019 | if (MAttrs.size()) { |
| 1020 | SubtargetFeatures Features; |
| 1021 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 1022 | Features.AddFeature(MAttrs[i]); |
| 1023 | FeaturesStr = Features.getString(); |
| 1024 | } |
| 1025 | |
| 1026 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 1027 | switch (OptLevel) { |
| 1028 | default: |
| 1029 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 1030 | return 1; |
| 1031 | case ' ': break; |
| 1032 | case '0': OLvl = CodeGenOpt::None; break; |
| 1033 | case '1': OLvl = CodeGenOpt::Less; break; |
| 1034 | case '2': OLvl = CodeGenOpt::Default; break; |
| 1035 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 1036 | } |
| 1037 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1038 | // set -fPIC |
| 1039 | if (ArgFPIC) |
| 1040 | ArgRelocModel = Reloc::PIC_; |
| 1041 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1042 | TargetOptions Options; |
| 1043 | Options.LessPreciseFPMADOption = EnableFPMAD; |
| 1044 | Options.PrintMachineCode = PrintCode; |
| 1045 | Options.NoFramePointerElim = DisableFPElim; |
| 1046 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
| 1047 | Options.NoExcessFPPrecision = DisableExcessPrecision; |
| 1048 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 1049 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 1050 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 1051 | Options.HonorSignDependentRoundingFPMathOption = |
| 1052 | EnableHonorSignDependentRoundingFPMath; |
| 1053 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 1054 | if (FloatABIForCalls != FloatABI::Default) |
| 1055 | Options.FloatABIType = FloatABIForCalls; |
| 1056 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 1057 | Options.JITExceptionHandling = EnableJITExceptionHandling; |
| 1058 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 1059 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
| 1060 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 1061 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 1062 | Options.RealignStack = EnableRealignStack; |
| 1063 | Options.DisableJumpTables = DisableSwitchTables; |
| 1064 | Options.TrapFuncName = TrapFuncName; |
| 1065 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 1066 | |
| 1067 | std::auto_ptr<mcld::LLVMTargetMachine> target_machine( |
| 1068 | TheTarget->createTargetMachine(TheTriple.getTriple(), |
| 1069 | MCPU, FeaturesStr, Options, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1070 | ArgRelocModel, CMModel, OLvl)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1071 | assert(target_machine.get() && "Could not allocate target machine!"); |
| 1072 | mcld::LLVMTargetMachine &TheTargetMachine = *target_machine.get(); |
| 1073 | |
| 1074 | TheTargetMachine.getTM().setMCUseLoc(false); |
| 1075 | TheTargetMachine.getTM().setMCUseCFI(false); |
| 1076 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1077 | // Set up mcld::outs() and mcld::errs() |
| 1078 | InitializeOStreams(TheTargetMachine.getLDInfo()); |
| 1079 | |
| 1080 | // Set up MsgHandler |
| 1081 | OwningPtr<mcld::DiagnosticLineInfo> |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame^] | 1082 | diag_line_info(TheTarget->createDiagnosticLineInfo(*TheTarget, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1083 | TheTriple.getTriple())); |
| 1084 | OwningPtr<mcld::DiagnosticPrinter> |
| 1085 | diag_printer(new mcld::TextDiagnosticPrinter(mcld::errs(), |
| 1086 | TheTargetMachine.getLDInfo())); |
| 1087 | |
| 1088 | mcld::InitializeDiagnosticEngine(TheTargetMachine.getLDInfo(), |
| 1089 | diag_line_info.take(), |
| 1090 | diag_printer.get()); |
| 1091 | |
| 1092 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1093 | // Figure out where we are going to send the output... |
| 1094 | OwningPtr<tool_output_file> |
| 1095 | Out(GetOutputStream(TheTarget->get()->getName(), |
| 1096 | TheTriple.getOS(), |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1097 | ArgFileType, |
| 1098 | ArgBitcodeFilename, |
| 1099 | ArgOutputFilename)); |
| 1100 | if (!Out) { |
| 1101 | // FIXME: show some error message pls. |
| 1102 | return 1; |
| 1103 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1104 | |
| 1105 | // Build up all of the passes that we want to do to the module. |
| 1106 | PassManager PM; |
| 1107 | |
| 1108 | // Add the target data from the target machine, if it exists, or the module. |
| 1109 | if (const TargetData *TD = TheTargetMachine.getTM().getTargetData()) |
| 1110 | PM.add(new TargetData(*TD)); |
| 1111 | else |
| 1112 | PM.add(new TargetData(&mod)); |
| 1113 | |
| 1114 | // Override default to generate verbose assembly. |
| 1115 | TheTargetMachine.getTM().setAsmVerbosityDefault(true); |
| 1116 | |
| 1117 | // Process the linker input from the command line |
| 1118 | mcld::SectLinkerOption *LinkerOpt = |
| 1119 | new mcld::SectLinkerOption(TheTargetMachine.getLDInfo()); |
| 1120 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1121 | if (!ProcessLinkerOptionsFromCommand(TheTargetMachine.getLDInfo())) { |
| 1122 | errs() << argv[0] << ": failed to process linker options from command line!\n"; |
| 1123 | return 1; |
| 1124 | } |
| 1125 | |
| 1126 | if (!ProcessLinkerInputsFromCommand(*LinkerOpt)) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1127 | errs() << argv[0] << ": failed to process inputs from command line!\n"; |
| 1128 | return 1; |
| 1129 | } |
| 1130 | |
| 1131 | { |
| 1132 | formatted_raw_ostream FOS(Out->os()); |
| 1133 | |
| 1134 | // Ask the target to add backend passes as necessary. |
| 1135 | if( TheTargetMachine.addPassesToEmitFile(PM, |
| 1136 | FOS, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1137 | ArgOutputFilename.native(), |
| 1138 | ArgFileType, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1139 | OLvl, |
| 1140 | LinkerOpt, |
| 1141 | NoVerify)) { |
| 1142 | errs() << argv[0] << ": target does not support generation of this" |
| 1143 | << " file type!\n"; |
| 1144 | return 1; |
| 1145 | } |
| 1146 | |
| 1147 | // Before executing passes, print the final values of the LLVM options. |
| 1148 | cl::PrintOptionValues(); |
| 1149 | |
| 1150 | PM.run(mod); |
| 1151 | } |
| 1152 | |
| 1153 | // Declare success. |
| 1154 | Out->keep(); |
| 1155 | |
| 1156 | // clean up |
| 1157 | delete LinkerOpt; |
| 1158 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1159 | if (0 != diag_printer->getNumErrors()) { |
| 1160 | // If we reached here, we are failing ungracefully. Run the interrupt handlers |
| 1161 | // to make sure any special cleanups get done, in particular that we remove |
| 1162 | // files registered with RemoveFileOnSignal. |
| 1163 | llvm::sys::RunInterruptHandlers(); |
| 1164 | diag_printer->finish(); |
| 1165 | exit(1); |
| 1166 | } |
| 1167 | |
| 1168 | diag_printer->finish(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1169 | return 0; |
| 1170 | } |
| 1171 | |