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 | |
| 25 | #include <llvm/Module.h> |
| 26 | #include <llvm/PassManager.h> |
| 27 | #include <llvm/Pass.h> |
| 28 | #include <llvm/ADT/Triple.h> |
| 29 | #include <llvm/LLVMContext.h> |
| 30 | #include <llvm/MC/SubtargetFeature.h> |
| 31 | #include <llvm/Support/CommandLine.h> |
| 32 | #include <llvm/Support/Debug.h> |
| 33 | #include <llvm/Support/FormattedStream.h> |
| 34 | #include <llvm/Support/Host.h> |
| 35 | #include <llvm/Support/IRReader.h> |
| 36 | #include <llvm/Support/ManagedStatic.h> |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 37 | #include <llvm/Support/Signals.h> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 38 | #include <llvm/Support/TargetRegistry.h> |
| 39 | #include <llvm/Support/TargetSelect.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 | |
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 | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 286 | static cl::list<std::string> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 287 | ArgSearchDirList("L", |
| 288 | cl::ZeroOrMore, |
| 289 | cl::desc("Add path searchdir to the list of paths that ld will search for archive libraries and ld control scripts."), |
| 290 | cl::value_desc("searchdir"), |
| 291 | cl::Prefix); |
| 292 | |
| 293 | static cl::alias |
| 294 | ArgSearchDirListAlias("library-path", |
| 295 | cl::desc("alias for -L"), |
| 296 | cl::aliasopt(ArgSearchDirList)); |
| 297 | |
| 298 | static cl::opt<bool> |
| 299 | ArgTrace("t", |
| 300 | cl::desc("Print the names of the input files as ld processes them.")); |
| 301 | |
| 302 | static cl::alias |
| 303 | ArgTraceAlias("trace", |
| 304 | cl::desc("alias for -t"), |
| 305 | cl::aliasopt(ArgTrace)); |
| 306 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 307 | static cl::opt<int> |
| 308 | ArgVerbose("verbose", |
| 309 | cl::init(-1), |
| 310 | cl::desc("Display the version number for ld and list the linker emulations supported.")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 311 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 312 | static cl::opt<bool> |
| 313 | ArgVersion("V", |
| 314 | cl::init(false), |
| 315 | cl::desc("Display the version number for MCLinker.")); |
| 316 | |
| 317 | static cl::opt<int> |
| 318 | ArgMaxErrorNum("error-limit", |
| 319 | cl::init(-1), |
| 320 | cl::desc("limits the maximum number of erros.")); |
| 321 | |
| 322 | static cl::opt<int> |
| 323 | ArgMaxWarnNum("warning-limit", |
| 324 | cl::init(-1), |
| 325 | cl::desc("limits the maximum number of warnings.")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 326 | |
| 327 | static cl::opt<std::string> |
| 328 | ArgEntry("e", |
| 329 | cl::desc("Use entry as the explicit symbol for beginning execution of your program."), |
| 330 | cl::value_desc("entry"), |
| 331 | cl::ValueRequired); |
| 332 | |
| 333 | static cl::alias |
| 334 | ArgEntryAlias("entry", |
| 335 | cl::desc("alias for -e"), |
| 336 | cl::aliasopt(ArgEntry)); |
| 337 | |
| 338 | static cl::opt<bool> |
| 339 | ArgBsymbolic("Bsymbolic", |
| 340 | cl::desc("Bind references within the shared library."), |
| 341 | cl::init(false)); |
| 342 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 343 | static cl::opt<bool> |
| 344 | ArgBgroup("Bgroup", |
| 345 | cl::desc("Info the dynamic linker to perform lookups only inside the group."), |
| 346 | cl::init(false)); |
| 347 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 348 | static cl::opt<std::string> |
| 349 | ArgSOName("soname", |
| 350 | cl::desc("Set internal name of shared library"), |
| 351 | cl::value_desc("name")); |
| 352 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 353 | static cl::opt<bool> |
| 354 | ArgNoUndefined("no-undefined", |
| 355 | cl::desc("Do not allow unresolved references"), |
| 356 | cl::init(false)); |
| 357 | |
| 358 | static cl::opt<bool> |
| 359 | ArgAllowMulDefs("allow-multiple-definition", |
| 360 | cl::desc("Allow multiple definition"), |
| 361 | cl::init(false)); |
| 362 | |
| 363 | static cl::opt<bool> |
| 364 | ArgEhFrameHdr("eh-frame-hdr", |
| 365 | cl::desc("Request creation of \".eh_frame_hdr\" section and ELF \"PT_GNU_EH_FRAME\" segment header."), |
| 366 | cl::init(false)); |
| 367 | |
| 368 | static cl::list<mcld::ZOption, bool, llvm::cl::parser<mcld::ZOption> > |
| 369 | ArgZOptionList("z", |
| 370 | cl::ZeroOrMore, |
| 371 | cl::desc("The -z options for GNU ld compatibility."), |
| 372 | cl::value_desc("keyword"), |
| 373 | cl::Prefix); |
| 374 | |
| 375 | cl::opt<mcld::CodeGenFileType> |
| 376 | ArgFileType("filetype", cl::init(mcld::CGFT_EXEFile), |
| 377 | cl::desc("Choose a file type (not all types are supported by all targets):"), |
| 378 | cl::values( |
| 379 | clEnumValN(mcld::CGFT_ASMFile, "asm", |
| 380 | "Emit an assembly ('.s') file"), |
| 381 | clEnumValN(mcld::CGFT_OBJFile, "obj", |
| 382 | "Emit a relocatable object ('.o') file"), |
| 383 | clEnumValN(mcld::CGFT_DSOFile, "dso", |
| 384 | "Emit an dynamic shared object ('.so') file"), |
| 385 | clEnumValN(mcld::CGFT_EXEFile, "exe", |
| 386 | "Emit a executable ('.exe') file"), |
| 387 | clEnumValN(mcld::CGFT_NULLFile, "null", |
| 388 | "Emit nothing, for performance testing"), |
| 389 | clEnumValEnd)); |
| 390 | |
| 391 | static cl::opt<bool> |
| 392 | ArgShared("shared", |
| 393 | cl::desc("Create a shared library."), |
| 394 | cl::init(false)); |
| 395 | |
| 396 | static cl::alias |
| 397 | ArgSharedAlias("Bshareable", |
| 398 | cl::desc("alias for -shared"), |
| 399 | cl::aliasopt(ArgShared)); |
| 400 | |
| 401 | static cl::opt<bool> |
| 402 | ArgPIE("pie", |
| 403 | cl::desc("Emit a position-independent executable file"), |
| 404 | cl::init(false)); |
| 405 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 406 | static cl::opt<bool> |
| 407 | ArgRelocatable("relocatable", |
| 408 | cl::desc("Generate relocatable output"), |
| 409 | cl::init(false)); |
| 410 | |
| 411 | static cl::alias |
| 412 | ArgRelocatableAlias("r", |
| 413 | cl::desc("alias for --relocatable"), |
| 414 | cl::aliasopt(ArgRelocatable)); |
| 415 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 416 | static cl::opt<Reloc::Model> |
| 417 | ArgRelocModel("relocation-model", |
| 418 | cl::desc("Choose relocation model"), |
| 419 | cl::init(Reloc::Default), |
| 420 | cl::values( |
| 421 | clEnumValN(Reloc::Default, "default", |
| 422 | "Target default relocation model"), |
| 423 | clEnumValN(Reloc::Static, "static", |
| 424 | "Non-relocatable code"), |
| 425 | clEnumValN(Reloc::PIC_, "pic", |
| 426 | "Fully relocatable, position independent code"), |
| 427 | clEnumValN(Reloc::DynamicNoPIC, "dynamic-no-pic", |
| 428 | "Relocatable external references, non-relocatable code"), |
| 429 | clEnumValEnd)); |
| 430 | |
| 431 | static cl::opt<bool> |
| 432 | ArgFPIC("fPIC", |
| 433 | cl::desc("Set relocation model to pic. The same as -relocation-model=pic."), |
| 434 | cl::init(false)); |
| 435 | |
| 436 | static cl::opt<std::string> |
| 437 | ArgDyld("dynamic-linker", |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 438 | cl::ZeroOrMore, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 439 | cl::desc("Set the name of the dynamic linker."), |
| 440 | cl::value_desc("Program")); |
| 441 | |
| 442 | namespace color { |
| 443 | enum Color { |
| 444 | Never, |
| 445 | Always, |
| 446 | Auto |
| 447 | }; |
| 448 | } // namespace of color |
| 449 | |
| 450 | static cl::opt<color::Color> |
| 451 | ArgColor("color", |
| 452 | cl::value_desc("WHEN"), |
| 453 | cl::desc("Surround the result strings with the marker"), |
| 454 | cl::init(color::Auto), |
| 455 | cl::values( |
| 456 | clEnumValN(color::Never, "never", |
| 457 | "do not surround result strings"), |
| 458 | clEnumValN(color::Always, "always", |
| 459 | "always surround result strings, even the output is a plain file"), |
| 460 | clEnumValN(color::Auto, "auto", |
| 461 | "surround result strings only if the output is a tty"), |
| 462 | clEnumValEnd)); |
| 463 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 464 | /// @{ |
| 465 | /// @name FIXME: begin of unsupported options |
| 466 | /// @} |
| 467 | static cl::opt<bool> |
| 468 | ArgGCSections("gc-sections", |
| 469 | cl::desc("Enable garbage collection of unused input sections."), |
| 470 | cl::init(false)); |
| 471 | |
| 472 | static cl::opt<bool> |
| 473 | ArgNoGCSections("no-gc-sections", |
| 474 | cl::desc("disable garbage collection of unused input sections."), |
| 475 | cl::init(false)); |
| 476 | |
| 477 | namespace icf { |
| 478 | enum Mode { |
| 479 | None, |
| 480 | All, |
| 481 | Safe |
| 482 | }; |
| 483 | } // namespace of icf |
| 484 | |
| 485 | static cl::opt<icf::Mode> |
| 486 | ArgICF("icf", |
| 487 | cl::desc("Identical Code Folding"), |
| 488 | cl::init(icf::None), |
| 489 | cl::values( |
| 490 | clEnumValN(icf::None, "none", |
| 491 | "do not perform cold folding"), |
| 492 | clEnumValN(icf::All, "all", |
| 493 | "always preform cold folding"), |
| 494 | clEnumValN(icf::Safe, "safe", |
| 495 | "Folds ctors, dtors and functions whose pointers are definitely not taken."), |
| 496 | clEnumValEnd)); |
| 497 | |
| 498 | // FIXME: add this to target options? |
| 499 | static cl::opt<bool> |
| 500 | ArgFIXCA8("fix-cortex-a8", |
| 501 | cl::desc("Enable Cortex-A8 Thumb-2 branch erratum fix"), |
| 502 | cl::init(false)); |
| 503 | |
| 504 | static cl::opt<bool> |
| 505 | ArgDiscardLocals("discard-locals", |
| 506 | cl::desc("Delete all temporary local symbols."), |
| 507 | cl::init(false)); |
| 508 | |
| 509 | static cl::alias |
| 510 | ArgDiscardLocalsAlias("X", |
| 511 | cl::desc("alias for --discard-locals"), |
| 512 | cl::aliasopt(ArgDiscardLocals)); |
| 513 | |
| 514 | static cl::opt<bool> |
| 515 | ArgDiscardAll("discard-all", |
| 516 | cl::desc("Delete all local symbols."), |
| 517 | cl::init(false)); |
| 518 | |
| 519 | static cl::alias |
| 520 | ArgDiscardAllAlias("x", |
| 521 | cl::desc("alias for --discard-all"), |
| 522 | cl::aliasopt(ArgDiscardAll)); |
| 523 | |
| 524 | static cl::opt<bool> |
| 525 | ArgNMagic("nmagic", |
| 526 | cl::desc("Do not page align data"), |
| 527 | cl::init(false)); |
| 528 | |
| 529 | static cl::alias |
| 530 | ArgNMagicAlias("n", |
| 531 | cl::desc("alias for --nmagic"), |
| 532 | cl::aliasopt(ArgNMagic)); |
| 533 | |
| 534 | static cl::opt<bool> |
| 535 | ArgOMagic("omagic", |
| 536 | cl::desc("Do not page align data, do not make text readonly"), |
| 537 | cl::init(false)); |
| 538 | |
| 539 | static cl::alias |
| 540 | ArgOMagicAlias("N", |
| 541 | cl::desc("alias for --omagic"), |
| 542 | cl::aliasopt(ArgOMagic)); |
| 543 | |
| 544 | static cl::opt<bool> |
| 545 | ArgStripDebug("strip-debug", |
| 546 | cl::desc("Omit debugger symbol information from the output file."), |
| 547 | cl::init(false)); |
| 548 | |
| 549 | static cl::alias |
| 550 | ArgStripDebugAlias("S", |
| 551 | cl::desc("alias for --strip-debug"), |
| 552 | cl::aliasopt(ArgStripDebug)); |
| 553 | |
| 554 | static cl::opt<bool> |
| 555 | ArgExportDynamic("export-dynamic", |
| 556 | cl::desc("Export all dynamic symbols"), |
| 557 | cl::init(false)); |
| 558 | |
| 559 | static cl::alias |
| 560 | ArgExportDynamicAlias("E", |
| 561 | cl::desc("alias for --export-dynamic"), |
| 562 | cl::aliasopt(ArgExportDynamic)); |
| 563 | |
| 564 | static cl::opt<std::string> |
| 565 | ArgEmulation("m", |
| 566 | cl::desc("Set GNU linker emulation"), |
| 567 | cl::value_desc("emulation")); |
| 568 | |
| 569 | static cl::opt<std::string> |
| 570 | ArgRuntimePath("rpath", |
| 571 | cl::desc("Add a directory to the runtime library search path"), |
| 572 | cl::value_desc("dir")); |
| 573 | |
| 574 | static cl::opt<std::string> |
| 575 | ArgRuntimePathLink("rpath-link", |
| 576 | cl::desc("Add a directory to the link time library search path"), |
| 577 | cl::value_desc("dir")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 578 | |
| 579 | static cl::list<std::string> |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 580 | ArgExcludeLIBS("exclude-libs", |
| 581 | cl::CommaSeparated, |
| 582 | cl::desc("Exclude libraries from automatic export"), |
| 583 | cl::value_desc("lib1,lib2,...")); |
| 584 | |
| 585 | static cl::opt<std::string> |
| 586 | ArgBuildID("build-id", |
| 587 | cl::desc("Request creation of \".note.gnu.build-id\" ELF note section."), |
| 588 | cl::value_desc("style")); |
| 589 | |
| 590 | static cl::opt<std::string> |
| 591 | ArgForceUndefined("u", |
| 592 | cl::desc("Force symbol to be undefined in the output file"), |
| 593 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 594 | |
| 595 | static cl::alias |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 596 | ArgForceUndefinedAlias("undefined", |
| 597 | cl::desc("alias for -u"), |
| 598 | cl::aliasopt(ArgForceUndefined)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 599 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 600 | /// @{ |
| 601 | /// @name FIXME: end of unsupported options |
| 602 | /// @} |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 603 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 604 | static cl::opt<bool> |
| 605 | ArgWarnSharedTextrel("warn-shared-textrel", |
| 606 | cl::desc("Warn if adding DT_TEXTREL in a shared object."), |
| 607 | cl::init(false)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 608 | |
| 609 | //===----------------------------------------------------------------------===// |
| 610 | // Scripting Options |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 611 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 612 | static cl::list<std::string> |
| 613 | ArgWrapList("wrap", |
| 614 | cl::ZeroOrMore, |
| 615 | cl::desc("Use a wrap function fo symbol."), |
| 616 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 617 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 618 | static cl::list<std::string> |
| 619 | ArgPortList("portable", |
| 620 | cl::ZeroOrMore, |
| 621 | cl::desc("Use a portable function fo symbol."), |
| 622 | cl::value_desc("symbol")); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 623 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 624 | static cl::list<std::string> |
| 625 | ArgAddressMapList("section-start", |
| 626 | cl::ZeroOrMore, |
| 627 | cl::desc("Locate a output section at the given absolute address"), |
| 628 | cl::value_desc("Set address of section"), |
| 629 | cl::Prefix); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 630 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 631 | static cl::opt<unsigned long long> |
| 632 | ArgBssSegAddr("Tbss", |
| 633 | cl::desc("Set the address of the bss segment"), |
| 634 | cl::init(-1U)); |
| 635 | |
| 636 | static cl::opt<unsigned long long> |
| 637 | ArgDataSegAddr("Tdata", |
| 638 | cl::desc("Set the address of the data segment"), |
| 639 | cl::init(-1U)); |
| 640 | |
| 641 | static cl::opt<unsigned long long> |
| 642 | ArgTextSegAddr("Ttext", |
| 643 | cl::desc("Set the address of the text segment"), |
| 644 | cl::init(-1U)); |
| 645 | |
| 646 | //===----------------------------------------------------------------------===// |
| 647 | // non-member functions |
| 648 | //===----------------------------------------------------------------------===// |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 649 | /// GetOutputStream - get the output stream. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 650 | static mcld::ToolOutputFile *GetOutputStream(const char* pTargetName, |
| 651 | Triple::OSType pOSType, |
| 652 | mcld::CodeGenFileType pFileType, |
| 653 | const mcld::sys::fs::Path& pInputFilename, |
| 654 | mcld::sys::fs::Path& pOutputFilename) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 655 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 656 | if (pOutputFilename.empty()) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 657 | if (0 == pInputFilename.native().compare("-")) |
| 658 | pOutputFilename.assign("-"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 659 | else { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 660 | switch(pFileType) { |
| 661 | case mcld::CGFT_ASMFile: { |
| 662 | if (0 == pInputFilename.native().compare("-")) |
| 663 | pOutputFilename.assign("_out"); |
| 664 | else |
| 665 | pOutputFilename.assign(pInputFilename.stem().native()); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 666 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 667 | if (0 == strcmp(pTargetName, "c")) |
| 668 | pOutputFilename.native() += ".cbe.c"; |
| 669 | else if (0 == strcmp(pTargetName, "cpp")) |
| 670 | pOutputFilename.native() += ".cpp"; |
| 671 | else |
| 672 | pOutputFilename.native() += ".s"; |
| 673 | } |
| 674 | break; |
| 675 | |
| 676 | case mcld::CGFT_OBJFile: { |
| 677 | if (0 == pInputFilename.native().compare("-")) |
| 678 | pOutputFilename.assign("_out"); |
| 679 | else |
| 680 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 681 | |
| 682 | if (pOSType == Triple::Win32) |
| 683 | pOutputFilename.native() += ".obj"; |
| 684 | else |
| 685 | pOutputFilename.native() += ".o"; |
| 686 | } |
| 687 | break; |
| 688 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 689 | case mcld::CGFT_PARTIAL: { |
| 690 | if (Triple::Win32 == pOSType) { |
| 691 | if (0 == pInputFilename.native().compare("-")) |
| 692 | pOutputFilename.assign("_out"); |
| 693 | else |
| 694 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 695 | pOutputFilename.native() += ".obj"; |
| 696 | } |
| 697 | else |
| 698 | pOutputFilename.assign("a.out"); |
| 699 | } |
| 700 | break; |
| 701 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 702 | case mcld::CGFT_DSOFile: { |
| 703 | if (Triple::Win32 == pOSType) { |
| 704 | if (0 == pInputFilename.native().compare("-")) |
| 705 | pOutputFilename.assign("_out"); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 706 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 707 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 708 | pOutputFilename.native() += ".dll"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 709 | } |
| 710 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 711 | pOutputFilename.assign("a.out"); |
| 712 | } |
| 713 | break; |
| 714 | |
| 715 | case mcld::CGFT_EXEFile: { |
| 716 | if (Triple::Win32 == pOSType) { |
| 717 | if (0 == pInputFilename.native().compare("-")) |
| 718 | pOutputFilename.assign("_out"); |
| 719 | else |
| 720 | pOutputFilename.assign(pInputFilename.stem().native()); |
| 721 | pOutputFilename.native() += ".exe"; |
| 722 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 723 | else |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 724 | pOutputFilename.assign("a.out"); |
| 725 | } |
| 726 | break; |
| 727 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 728 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 729 | break; |
| 730 | default: |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 731 | llvm::report_fatal_error("Unknown output file type.\n"); |
| 732 | } // end of switch |
| 733 | } // end of ! pInputFilename == "-" |
| 734 | } // end of if empty pOutputFilename |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 735 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 736 | mcld::FileHandle::Permission permission; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 737 | switch (pFileType) { |
| 738 | default: assert(0 && "Unknown file type"); |
| 739 | case mcld::CGFT_ASMFile: |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 740 | case mcld::CGFT_OBJFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 741 | case mcld::CGFT_PARTIAL: |
| 742 | permission = 0644; |
| 743 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 744 | case mcld::CGFT_DSOFile: |
| 745 | case mcld::CGFT_EXEFile: |
| 746 | case mcld::CGFT_NULLFile: |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 747 | permission = 0755; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 748 | break; |
| 749 | } |
| 750 | |
| 751 | // Open the file. |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 752 | mcld::ToolOutputFile* result_output = |
| 753 | new mcld::ToolOutputFile(pOutputFilename, |
| 754 | mcld::FileHandle::ReadWrite | |
| 755 | mcld::FileHandle::Create | |
| 756 | mcld::FileHandle::Truncate, |
| 757 | permission); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 758 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 759 | return result_output; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 762 | static bool ShouldColorize() |
| 763 | { |
| 764 | const char* term = getenv("TERM"); |
| 765 | return term && (0 != strcmp(term, "dumb")); |
| 766 | } |
| 767 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 768 | static bool ProcessLinkerOptionsFromCommand(mcld::LinkerConfig& pConfig) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 769 | // ----- Set up General Options ----- // |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 770 | // set up colorize |
| 771 | switch (ArgColor) { |
| 772 | case color::Never: |
| 773 | pConfig.options().setColor(false); |
| 774 | break; |
| 775 | case color::Always: |
| 776 | pConfig.options().setColor(true); |
| 777 | break; |
| 778 | case color::Auto: |
| 779 | bool color_option = ShouldColorize() && |
| 780 | llvm::sys::Process::FileDescriptorIsDisplayed(STDOUT_FILENO); |
| 781 | pConfig.options().setColor(color_option); |
| 782 | break; |
| 783 | } |
| 784 | |
| 785 | mcld::outs().setColor(pConfig.options().color()); |
| 786 | mcld::errs().setColor(pConfig.options().color()); |
| 787 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 788 | // set up soname |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 789 | pConfig.options().setSOName(ArgSOName); |
| 790 | |
| 791 | // -shared or -pie |
| 792 | if (true == ArgShared || true == ArgPIE) { |
| 793 | ArgFileType = mcld::CGFT_DSOFile; |
| 794 | } |
| 795 | else if (true == ArgRelocatable) { |
| 796 | ArgFileType = mcld::CGFT_PARTIAL; |
| 797 | } |
| 798 | |
| 799 | // -V |
| 800 | if (ArgVersion) { |
| 801 | mcld::outs() << "MCLinker - " |
| 802 | << mcld::LinkerConfig::version() |
| 803 | << "\n"; |
| 804 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 805 | |
| 806 | // set up sysroot |
| 807 | if (!ArgSysRoot.empty()) { |
| 808 | if (exists(ArgSysRoot) && is_directory(ArgSysRoot)) |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 809 | pConfig.options().setSysroot(ArgSysRoot); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // add all search directories |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 813 | cl::list<std::string>::iterator sd; |
| 814 | cl::list<std::string>::iterator sdEnd = ArgSearchDirList.end(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 815 | for (sd=ArgSearchDirList.begin(); sd!=sdEnd; ++sd) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 816 | if (!pConfig.options().directories().insert(*sd)) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 817 | // FIXME: need a warning function |
| 818 | errs() << "WARNING: can not open search directory `-L" |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 819 | << *sd |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 820 | << "'.\n"; |
| 821 | } |
| 822 | } |
| 823 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 824 | pConfig.options().setPIE(ArgPIE); |
| 825 | pConfig.options().setTrace(ArgTrace); |
| 826 | pConfig.options().setVerbose(ArgVerbose); |
| 827 | pConfig.options().setMaxErrorNum(ArgMaxErrorNum); |
| 828 | pConfig.options().setMaxWarnNum(ArgMaxWarnNum); |
| 829 | pConfig.options().setEntry(ArgEntry); |
| 830 | pConfig.options().setBsymbolic(ArgBsymbolic); |
| 831 | pConfig.options().setBgroup(ArgBgroup); |
| 832 | pConfig.options().setDyld(ArgDyld); |
| 833 | pConfig.options().setNoUndefined(ArgNoUndefined); |
| 834 | pConfig.options().setMulDefs(ArgAllowMulDefs); |
| 835 | pConfig.options().setEhFrameHdr(ArgEhFrameHdr); |
| 836 | pConfig.options().setNMagic(ArgNMagic); |
| 837 | pConfig.options().setOMagic(ArgOMagic); |
| 838 | pConfig.options().setStripDebug(ArgStripDebug); |
| 839 | pConfig.options().setExportDynamic(ArgExportDynamic); |
| 840 | pConfig.options().setWarnSharedTextrel(ArgWarnSharedTextrel); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 841 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 842 | // set up rename map, for --wrap |
| 843 | cl::list<std::string>::iterator wname; |
| 844 | cl::list<std::string>::iterator wnameEnd = ArgWrapList.end(); |
| 845 | for (wname = ArgWrapList.begin(); wname != wnameEnd; ++wname) { |
| 846 | bool exist = false; |
| 847 | |
| 848 | // add wname -> __wrap_wname |
| 849 | mcld::StringEntry<llvm::StringRef>* to_wrap = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 850 | pConfig.scripts().renameMap().insert(*wname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 851 | |
| 852 | std::string to_wrap_str = "__wrap_" + *wname; |
| 853 | to_wrap->setValue(to_wrap_str); |
| 854 | |
| 855 | if (exist) |
| 856 | mcld::warning(mcld::diag::rewrap) << *wname << to_wrap_str; |
| 857 | |
| 858 | // add __real_wname -> wname |
| 859 | std::string from_real_str = "__real_" + *wname; |
| 860 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 861 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 862 | from_real->setValue(*wname); |
| 863 | if (exist) |
| 864 | mcld::warning(mcld::diag::rewrap) << *wname << from_real_str; |
| 865 | } // end of for |
| 866 | |
| 867 | // set up rename map, for --portable |
| 868 | cl::list<std::string>::iterator pname; |
| 869 | cl::list<std::string>::iterator pnameEnd = ArgPortList.end(); |
| 870 | for (pname = ArgPortList.begin(); pname != pnameEnd; ++pname) { |
| 871 | bool exist = false; |
| 872 | |
| 873 | // add pname -> pname_portable |
| 874 | mcld::StringEntry<llvm::StringRef>* to_port = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 875 | pConfig.scripts().renameMap().insert(*pname, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 876 | |
| 877 | std::string to_port_str = *pname + "_portable"; |
| 878 | to_port->setValue(to_port_str); |
| 879 | |
| 880 | if (exist) |
| 881 | mcld::warning(mcld::diag::rewrap) << *pname << to_port_str; |
| 882 | |
| 883 | // add __real_pname -> pname |
| 884 | std::string from_real_str = "__real_" + *pname; |
| 885 | mcld::StringEntry<llvm::StringRef>* from_real = |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 886 | pConfig.scripts().renameMap().insert(from_real_str, exist); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 887 | |
| 888 | from_real->setValue(*pname); |
| 889 | if (exist) |
| 890 | mcld::warning(mcld::diag::rewrap) << *pname << from_real_str; |
| 891 | } // end of for |
| 892 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 893 | // add -z options |
| 894 | cl::list<mcld::ZOption>::iterator zOpt; |
| 895 | cl::list<mcld::ZOption>::iterator zOptEnd = ArgZOptionList.end(); |
| 896 | for (zOpt = ArgZOptionList.begin(); zOpt != zOptEnd; ++zOpt) { |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 897 | pConfig.options().addZOption(*zOpt); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 898 | } |
| 899 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 900 | if (ArgGCSections) { |
| 901 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgGCSections.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 902 | } |
| 903 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 904 | // set up icf mode |
| 905 | switch (ArgICF) { |
| 906 | case icf::None: |
| 907 | break; |
| 908 | case icf::All: |
| 909 | case icf::Safe: |
| 910 | default: |
| 911 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgICF.ArgStr; |
| 912 | break; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 913 | } |
| 914 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 915 | if (ArgFIXCA8) { |
| 916 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgFIXCA8.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 917 | } |
| 918 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 919 | if (ArgDiscardLocals) { |
| 920 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgDiscardLocals.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 921 | } |
| 922 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 923 | if (ArgDiscardAll) { |
| 924 | mcld::warning(mcld::diag::warn_unsupported_option) << ArgDiscardAll.ArgStr; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 927 | // add address mappings |
| 928 | // -Ttext |
| 929 | if (-1U != ArgTextSegAddr) { |
| 930 | bool exist = false; |
| 931 | mcld::StringEntry<uint64_t>* text_mapping = |
| 932 | pConfig.scripts().addressMap().insert(".text", exist); |
| 933 | text_mapping->setValue(ArgTextSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 934 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 935 | // -Tdata |
| 936 | if (-1U != ArgDataSegAddr) { |
| 937 | bool exist = false; |
| 938 | mcld::StringEntry<uint64_t>* data_mapping = |
| 939 | pConfig.scripts().addressMap().insert(".data", exist); |
| 940 | data_mapping->setValue(ArgDataSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 941 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 942 | // -Tbss |
| 943 | if (-1U != ArgBssSegAddr) { |
| 944 | bool exist = false; |
| 945 | mcld::StringEntry<uint64_t>* bss_mapping = |
| 946 | pConfig.scripts().addressMap().insert(".bss", exist); |
| 947 | bss_mapping->setValue(ArgBssSegAddr); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 948 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 949 | // --section-start SECTION=ADDRESS |
| 950 | for (cl::list<std::string>::iterator |
| 951 | it = ArgAddressMapList.begin(), ie = ArgAddressMapList.end(); |
| 952 | it != ie; ++it) { |
| 953 | // FIXME: Add a cl::parser |
| 954 | size_t pos = (*it).find_last_of('='); |
| 955 | llvm::StringRef script(*it); |
| 956 | uint64_t address = 0x0; |
| 957 | script.substr(pos + 1).getAsInteger(0, address); |
| 958 | bool exist = false; |
| 959 | mcld::StringEntry<uint64_t>* addr_mapping = |
| 960 | pConfig.scripts().addressMap().insert(script.substr(0, pos), exist); |
| 961 | addr_mapping->setValue(address); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 962 | } |
| 963 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 964 | return true; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 965 | } |
| 966 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 967 | int main(int argc, char* argv[]) |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 968 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 969 | LLVMContext &Context = getGlobalContext(); |
| 970 | llvm_shutdown_obj Y; // Call llvm_shutdown() on exit. |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame] | 971 | cl::ParseCommandLineOptions(argc, argv, "MCLinker\n"); |
| 972 | |
| 973 | #ifdef ENABLE_UNITTEST |
| 974 | if (UnitTest) { |
| 975 | return unit_test( argc, argv ); |
| 976 | } |
| 977 | #endif |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 978 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 979 | // Initialize targets first, so that --version shows registered targets. |
| 980 | InitializeAllTargets(); |
| 981 | InitializeAllAsmPrinters(); |
| 982 | InitializeAllAsmParsers(); |
| 983 | InitializeAllTargetMCs(); |
| 984 | mcld::InitializeAllTargets(); |
| 985 | mcld::InitializeAllLinkers(); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 986 | mcld::InitializeAllEmulations(); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 987 | mcld::InitializeAllDiagnostics(); |
| 988 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 989 | // Load the module to be compiled... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 990 | std::auto_ptr<llvm::Module> M; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 991 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 992 | // Load the module to be linked... |
| 993 | mcld::Module LDIRModule; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 994 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 995 | mcld::LinkerConfig LDConfig; |
| 996 | |
| 997 | // Process the linker input from the command line |
| 998 | if (!ProcessLinkerOptionsFromCommand(LDConfig)) { |
| 999 | errs() << argv[0] << ": failed to process linker options from command line!\n"; |
| 1000 | return 1; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1001 | } |
| 1002 | |
| 1003 | if (ArgBitcodeFilename.empty() && |
| 1004 | (mcld::CGFT_DSOFile != ArgFileType && |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1005 | mcld::CGFT_EXEFile != ArgFileType && |
| 1006 | mcld::CGFT_PARTIAL != ArgFileType)) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1007 | // If the file is not given, forcefully read from stdin |
| 1008 | if (ArgVerbose >= 0) { |
| 1009 | errs() << "** The bitcode/llvm asm file is not given. Read from stdin.\n" |
| 1010 | << "** Specify input bitcode/llvm asm file by\n\n" |
| 1011 | << " llvm-mcld -dB [the bitcode/llvm asm]\n\n"; |
| 1012 | } |
| 1013 | |
| 1014 | ArgBitcodeFilename.assign("-"); |
| 1015 | } |
| 1016 | |
| 1017 | if (!ArgBitcodeFilename.empty()) { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1018 | SMDiagnostic Err; |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1019 | M.reset(ParseIRFile(ArgBitcodeFilename.native(), Err, Context)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1020 | |
| 1021 | if (M.get() == 0) { |
| 1022 | Err.print(argv[0], errs()); |
| 1023 | errs() << "** Failed to to the given bitcode/llvm asm file '" |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1024 | << ArgBitcodeFilename.native() << "'. **\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1025 | return 1; |
| 1026 | } |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1027 | } |
| 1028 | else { |
| 1029 | // If here, output must be dynamic shared object (mcld::CGFT_DSOFile) and |
| 1030 | // executable file (mcld::CGFT_EXEFile). |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1031 | M.reset(new Module("Empty Module", Context)); |
| 1032 | } |
| 1033 | Module &mod = *M.get(); |
| 1034 | |
| 1035 | // If we are supposed to override the target triple, do so now. |
| 1036 | Triple TheTriple; |
| 1037 | if (!TargetTriple.empty()) { |
| 1038 | TheTriple.setTriple(TargetTriple); |
| 1039 | mod.setTargetTriple(TargetTriple); |
| 1040 | } |
| 1041 | |
| 1042 | // User doesn't specify the triple from command. |
| 1043 | if (TheTriple.getTriple().empty()) { |
| 1044 | // Try to get one from the input Module. |
| 1045 | const std::string &TripleStr = mod.getTargetTriple(); |
| 1046 | |
| 1047 | if (TripleStr.empty()) |
| 1048 | TheTriple.setTriple(sys::getDefaultTargetTriple()); |
| 1049 | else |
| 1050 | TheTriple.setTriple(TripleStr); |
| 1051 | } |
| 1052 | |
| 1053 | // Allocate target machine. First, check whether the user has explicitly |
| 1054 | // specified an architecture to compile for. If so we have to look it up by |
| 1055 | // name, because it might be a backend that has no mapping to a target triple. |
| 1056 | const mcld::Target *TheTarget = 0; |
| 1057 | if (!MArch.empty()) { |
| 1058 | for (mcld::TargetRegistry::iterator it = mcld::TargetRegistry::begin(), |
| 1059 | ie = mcld::TargetRegistry::end(); it != ie; ++it) { |
| 1060 | if (MArch == (*it)->get()->getName()) { |
| 1061 | TheTarget = *it; |
| 1062 | break; |
| 1063 | } |
| 1064 | } |
| 1065 | |
| 1066 | if (!TheTarget) { |
| 1067 | errs() << argv[0] << ": error: invalid target '" << MArch << "'.\n"; |
| 1068 | return 1; |
| 1069 | } |
| 1070 | |
| 1071 | // Adjust the triple to match (if known), otherwise stick with the |
| 1072 | // module/host triple. |
| 1073 | Triple::ArchType Type = Triple::getArchTypeForLLVMName(MArch); |
| 1074 | if (Type != Triple::UnknownArch) |
| 1075 | TheTriple.setArch(Type); |
| 1076 | } |
| 1077 | else { |
| 1078 | std::string Err; |
| 1079 | TheTarget = mcld::TargetRegistry::lookupTarget(TheTriple.getTriple(), Err); |
| 1080 | if (TheTarget == 0) { |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1081 | errs() << "error: auto-selecting target `" << TheTriple.getTriple() |
| 1082 | << "'\n" |
| 1083 | << "Please use the -march option to explicitly select a target.\n" |
| 1084 | << "Example:\n" |
| 1085 | << " $ " << argv[0] << " -march=arm\n"; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1086 | return 1; |
| 1087 | } |
| 1088 | } |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1089 | // Set up mcld::LinkerConfig |
| 1090 | LDConfig.setTriple(TheTriple); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1091 | |
| 1092 | // Package up features to be passed to target/subtarget |
| 1093 | std::string FeaturesStr; |
| 1094 | if (MAttrs.size()) { |
| 1095 | SubtargetFeatures Features; |
| 1096 | for (unsigned i = 0; i != MAttrs.size(); ++i) |
| 1097 | Features.AddFeature(MAttrs[i]); |
| 1098 | FeaturesStr = Features.getString(); |
| 1099 | } |
| 1100 | |
| 1101 | CodeGenOpt::Level OLvl = CodeGenOpt::Default; |
| 1102 | switch (OptLevel) { |
| 1103 | default: |
| 1104 | errs() << argv[0] << ": invalid optimization level.\n"; |
| 1105 | return 1; |
| 1106 | case ' ': break; |
| 1107 | case '0': OLvl = CodeGenOpt::None; break; |
| 1108 | case '1': OLvl = CodeGenOpt::Less; break; |
| 1109 | case '2': OLvl = CodeGenOpt::Default; break; |
| 1110 | case '3': OLvl = CodeGenOpt::Aggressive; break; |
| 1111 | } |
| 1112 | |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1113 | // set -fPIC |
| 1114 | if (ArgFPIC) |
| 1115 | ArgRelocModel = Reloc::PIC_; |
| 1116 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1117 | TargetOptions Options; |
| 1118 | Options.LessPreciseFPMADOption = EnableFPMAD; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1119 | Options.NoFramePointerElim = DisableFPElim; |
| 1120 | Options.NoFramePointerElimNonLeaf = DisableFPElimNonLeaf; |
Shih-wei Liao | cedee4b | 2012-08-02 23:13:03 -0700 | [diff] [blame] | 1121 | Options.AllowFPOpFusion = FuseFPOps; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1122 | Options.UnsafeFPMath = EnableUnsafeFPMath; |
| 1123 | Options.NoInfsFPMath = EnableNoInfsFPMath; |
| 1124 | Options.NoNaNsFPMath = EnableNoNaNsFPMath; |
| 1125 | Options.HonorSignDependentRoundingFPMathOption = |
| 1126 | EnableHonorSignDependentRoundingFPMath; |
| 1127 | Options.UseSoftFloat = GenerateSoftFloatCalls; |
| 1128 | if (FloatABIForCalls != FloatABI::Default) |
| 1129 | Options.FloatABIType = FloatABIForCalls; |
| 1130 | Options.NoZerosInBSS = DontPlaceZerosInBSS; |
| 1131 | Options.JITExceptionHandling = EnableJITExceptionHandling; |
| 1132 | Options.JITEmitDebugInfo = EmitJitDebugInfo; |
| 1133 | Options.JITEmitDebugInfoToDisk = EmitJitDebugInfoToDisk; |
| 1134 | Options.GuaranteedTailCallOpt = EnableGuaranteedTailCallOpt; |
| 1135 | Options.StackAlignmentOverride = OverrideStackAlignment; |
| 1136 | Options.RealignStack = EnableRealignStack; |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1137 | Options.TrapFuncName = TrapFuncName; |
| 1138 | Options.EnableSegmentedStacks = SegmentedStacks; |
| 1139 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1140 | std::auto_ptr<mcld::MCLDTargetMachine> target_machine( |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1141 | TheTarget->createTargetMachine(TheTriple.getTriple(), |
| 1142 | MCPU, FeaturesStr, Options, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1143 | ArgRelocModel, CMModel, OLvl)); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1144 | assert(target_machine.get() && "Could not allocate target machine!"); |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1145 | mcld::MCLDTargetMachine &TheTargetMachine = *target_machine.get(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1146 | |
| 1147 | TheTargetMachine.getTM().setMCUseLoc(false); |
| 1148 | TheTargetMachine.getTM().setMCUseCFI(false); |
| 1149 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1150 | // FIXME: Move the initialization of LineInfo to mcld::Linker when we |
| 1151 | // finish LineInfo's implementation. |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1152 | OwningPtr<mcld::DiagnosticLineInfo> |
Shih-wei Liao | 67e37f1 | 2012-07-27 03:50:34 -0700 | [diff] [blame] | 1153 | diag_line_info(TheTarget->createDiagnosticLineInfo(*TheTarget, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1154 | TheTriple.getTriple())); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1155 | |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1156 | mcld::getDiagnosticEngine().setLineInfo(*diag_line_info.take()); |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1157 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1158 | // Figure out where we are going to send the output... |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1159 | OwningPtr<mcld::ToolOutputFile> |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1160 | Out(GetOutputStream(TheTarget->get()->getName(), |
| 1161 | TheTriple.getOS(), |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1162 | ArgFileType, |
| 1163 | ArgBitcodeFilename, |
| 1164 | ArgOutputFilename)); |
| 1165 | if (!Out) { |
| 1166 | // FIXME: show some error message pls. |
| 1167 | return 1; |
| 1168 | } |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1169 | |
| 1170 | // Build up all of the passes that we want to do to the module. |
| 1171 | PassManager PM; |
| 1172 | |
| 1173 | // Add the target data from the target machine, if it exists, or the module. |
| 1174 | if (const TargetData *TD = TheTargetMachine.getTM().getTargetData()) |
| 1175 | PM.add(new TargetData(*TD)); |
| 1176 | else |
| 1177 | PM.add(new TargetData(&mod)); |
| 1178 | |
| 1179 | // Override default to generate verbose assembly. |
| 1180 | TheTargetMachine.getTM().setAsmVerbosityDefault(true); |
| 1181 | |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1182 | { |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1183 | // Ask the target to add backend passes as necessary. |
| 1184 | if( TheTargetMachine.addPassesToEmitFile(PM, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1185 | *Out, |
Zonr Chang | affc150 | 2012-07-16 14:28:23 +0800 | [diff] [blame] | 1186 | ArgFileType, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1187 | OLvl, |
Shih-wei Liao | 22add6f | 2012-12-15 17:21:00 -0800 | [diff] [blame^] | 1188 | LDIRModule, |
| 1189 | LDConfig, |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1190 | NoVerify)) { |
| 1191 | errs() << argv[0] << ": target does not support generation of this" |
| 1192 | << " file type!\n"; |
| 1193 | return 1; |
| 1194 | } |
| 1195 | |
| 1196 | // Before executing passes, print the final values of the LLVM options. |
| 1197 | cl::PrintOptionValues(); |
| 1198 | |
| 1199 | PM.run(mod); |
| 1200 | } |
| 1201 | |
| 1202 | // Declare success. |
| 1203 | Out->keep(); |
Shih-wei Liao | 5460a1f | 2012-03-16 22:41:16 -0700 | [diff] [blame] | 1204 | return 0; |
| 1205 | } |
| 1206 | |