blob: 726e08e4faca19ccf749ea670479f690cfc81920 [file] [log] [blame]
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001//===- 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 Changaffc1502012-07-16 14:28:23 +080014#include <mcld/Support/Path.h>
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070015#include <mcld/Support/RealPath.h>
Zonr Changaffc1502012-07-16 14:28:23 +080016#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 Liao5460a1f2012-03-16 22:41:16 -070022#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 Changaffc1502012-07-16 14:28:23 +080036#include <llvm/Support/Signals.h>
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070037#include <llvm/Support/TargetRegistry.h>
38#include <llvm/Support/TargetSelect.h>
39#include <llvm/Support/ToolOutputFile.h>
Zonr Changaffc1502012-07-16 14:28:23 +080040#include <llvm/Support/Process.h>
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070041#include <llvm/Target/TargetData.h>
42#include <llvm/Target/TargetMachine.h>
43
44using namespace llvm;
45
46#ifdef ENABLE_UNITTEST
47#include <gtest.h>
48
49static cl::opt<bool>
50UnitTest("unittest", cl::desc("do unit test") );
51
52int unit_test( int argc, char* argv[] )
53{
54 testing::InitGoogleTest( &argc, argv );
55 return RUN_ALL_TESTS();
56}
57
58#endif
59
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070060// 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 Liao5460a1f2012-03-16 22:41:16 -070064// Determine optimization level.
65static cl::opt<char>
66OptLevel("O",
67 cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
68 "(default = '-O2')"),
69 cl::Prefix,
70 cl::ZeroOrMore,
71 cl::init(' '));
72
73static cl::opt<std::string>
74TargetTriple("mtriple", cl::desc("Override target triple for module"));
75
76static cl::opt<std::string>
77MArch("march", cl::desc("Architecture to generate code for (see --version)"));
78
79static cl::opt<std::string>
80MCPU("mcpu",
81 cl::desc("Target a specific cpu type (-mcpu=help for details)"),
82 cl::value_desc("cpu-name"),
83 cl::init(""));
84
85static cl::list<std::string>
86MAttrs("mattr",
87 cl::CommaSeparated,
88 cl::desc("Target specific attributes (-mattr=help for details)"),
89 cl::value_desc("a1,+a2,-a3,..."));
90
Shih-wei Liao5460a1f2012-03-16 22:41:16 -070091static cl::opt<llvm::CodeModel::Model>
92CMModel("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 Liao5460a1f2012-03-16 22:41:16 -0700107cl::opt<bool> NoVerify("disable-verify", cl::Hidden,
108 cl::desc("Do not verify input module"));
109
110static cl::opt<bool>
111EnableFPMAD("enable-fp-mad",
112 cl::desc("Enable less precise MAD instructions to be generated"),
113 cl::init(false));
114
115static cl::opt<bool>
116PrintCode("print-machineinstrs",
117 cl::desc("Print generated machine code"),
118 cl::init(false));
119
120static cl::opt<bool>
121DisableFPElim("disable-fp-elim",
122 cl::desc("Disable frame pointer elimination optimization"),
123 cl::init(false));
124
125static cl::opt<bool>
126DisableFPElimNonLeaf("disable-non-leaf-fp-elim",
127 cl::desc("Disable frame pointer elimination optimization for non-leaf funcs"),
128 cl::init(false));
129
130static cl::opt<bool>
131DisableExcessPrecision("disable-excess-fp-precision",
132 cl::desc("Disable optimizations that may increase FP precision"),
133 cl::init(false));
134
135static cl::opt<bool>
136EnableUnsafeFPMath("enable-unsafe-fp-math",
137 cl::desc("Enable optimizations that may decrease FP precision"),
138 cl::init(false));
139
140static cl::opt<bool>
141EnableNoInfsFPMath("enable-no-infs-fp-math",
142 cl::desc("Enable FP math optimizations that assume no +-Infs"),
143 cl::init(false));
144
145static cl::opt<bool>
146EnableNoNaNsFPMath("enable-no-nans-fp-math",
147 cl::desc("Enable FP math optimizations that assume no NaNs"),
148 cl::init(false));
149
150static cl::opt<bool>
151EnableHonorSignDependentRoundingFPMath("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
156static cl::opt<bool>
157GenerateSoftFloatCalls("soft-float",
158 cl::desc("Generate software floating point library calls"),
159 cl::init(false));
160
161static cl::opt<llvm::FloatABI::ABIType>
162FloatABIForCalls("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
174static cl::opt<bool>
175DontPlaceZerosInBSS("nozero-initialized-in-bss",
176 cl::desc("Don't place zero-initialized symbols into bss section"),
177 cl::init(false));
178
179static cl::opt<bool>
180EnableJITExceptionHandling("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
190static cl::opt<bool>
191EmitJitDebugInfo("jit-emit-debug",
192 cl::desc("Emit debug information to debugger"),
193 cl::init(EMIT_DEBUG));
194#undef EMIT_DEBUG
195
196static cl::opt<bool>
197EmitJitDebugInfoToDisk("jit-emit-debug-to-disk",
198 cl::Hidden,
199 cl::desc("Emit debug info objfiles to disk"),
200 cl::init(false));
201
202static cl::opt<bool>
203EnableGuaranteedTailCallOpt("tailcallopt",
204 cl::desc("Turn fastcc calls into tail calls by (potentially) changing ABI."),
205 cl::init(false));
206
207static cl::opt<unsigned>
208OverrideStackAlignment("stack-alignment",
209 cl::desc("Override default stack alignment"),
210 cl::init(0));
211
212static cl::opt<bool>
213EnableRealignStack("realign-stack",
214 cl::desc("Realign stack if needed"),
215 cl::init(true));
216
217static cl::opt<bool>
218DisableSwitchTables(cl::Hidden, "disable-jump-tables",
219 cl::desc("Do not generate jump tables."),
220 cl::init(false));
221
222static cl::opt<std::string>
223TrapFuncName("trap-func", cl::Hidden,
224 cl::desc("Emit a call to trap function rather than a trap instruction"),
225 cl::init(""));
226
227static cl::opt<bool>
228SegmentedStacks("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
243static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> >
Zonr Changaffc1502012-07-16 14:28:23 +0800244ArgBitcodeFilename("dB",
245 cl::desc("set default bitcode"),
246 cl::value_desc("bitcode"));
247
248static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> >
249ArgOutputFilename("o",
250 cl::desc("Output filename"),
251 cl::value_desc("filename"));
252
253static cl::alias
254AliasOutputFilename("output",
255 cl::desc("alias for -o"),
256 cl::aliasopt(ArgOutputFilename));
257
258static cl::opt<mcld::sys::fs::Path, false, llvm::cl::parser<mcld::sys::fs::Path> >
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700259ArgSysRoot("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
264static cl::list<mcld::MCLDDirectory, bool, llvm::cl::parser<mcld::MCLDDirectory> >
265ArgSearchDirList("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
271static cl::alias
272ArgSearchDirListAlias("library-path",
273 cl::desc("alias for -L"),
274 cl::aliasopt(ArgSearchDirList));
275
276static cl::opt<bool>
277ArgTrace("t",
278 cl::desc("Print the names of the input files as ld processes them."));
279
280static cl::alias
281ArgTraceAlias("trace",
282 cl::desc("alias for -t"),
283 cl::aliasopt(ArgTrace));
284
Zonr Changaffc1502012-07-16 14:28:23 +0800285static cl::opt<int>
286ArgVerbose("verbose",
287 cl::init(-1),
288 cl::desc("Display the version number for ld and list the linker emulations supported."));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700289
Zonr Changaffc1502012-07-16 14:28:23 +0800290static cl::opt<bool>
291ArgVersion("V",
292 cl::init(false),
293 cl::desc("Display the version number for MCLinker."));
294
295static cl::opt<int>
296ArgMaxErrorNum("error-limit",
297 cl::init(-1),
298 cl::desc("limits the maximum number of erros."));
299
300static cl::opt<int>
301ArgMaxWarnNum("warning-limit",
302 cl::init(-1),
303 cl::desc("limits the maximum number of warnings."));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700304
305static cl::opt<std::string>
306ArgEntry("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
311static cl::alias
312ArgEntryAlias("entry",
313 cl::desc("alias for -e"),
314 cl::aliasopt(ArgEntry));
315
316static cl::opt<bool>
317ArgBsymbolic("Bsymbolic",
318 cl::desc("Bind references within the shared library."),
319 cl::init(false));
320
Zonr Changaffc1502012-07-16 14:28:23 +0800321static cl::opt<bool>
322ArgBgroup("Bgroup",
323 cl::desc("Info the dynamic linker to perform lookups only inside the group."),
324 cl::init(false));
325
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700326static cl::opt<std::string>
327ArgSOName("soname",
328 cl::desc("Set internal name of shared library"),
329 cl::value_desc("name"));
330
Zonr Changaffc1502012-07-16 14:28:23 +0800331static cl::opt<bool>
332ArgNoUndefined("no-undefined",
333 cl::desc("Do not allow unresolved references"),
334 cl::init(false));
335
336static cl::opt<bool>
337ArgAllowMulDefs("allow-multiple-definition",
338 cl::desc("Allow multiple definition"),
339 cl::init(false));
340
341static cl::opt<bool>
342ArgEhFrameHdr("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
346static cl::list<mcld::ZOption, bool, llvm::cl::parser<mcld::ZOption> >
347ArgZOptionList("z",
348 cl::ZeroOrMore,
349 cl::desc("The -z options for GNU ld compatibility."),
350 cl::value_desc("keyword"),
351 cl::Prefix);
352
353cl::opt<mcld::CodeGenFileType>
354ArgFileType("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
369static cl::opt<bool>
370ArgShared("shared",
371 cl::desc("Create a shared library."),
372 cl::init(false));
373
374static cl::alias
375ArgSharedAlias("Bshareable",
376 cl::desc("alias for -shared"),
377 cl::aliasopt(ArgShared));
378
379static cl::opt<bool>
380ArgPIE("pie",
381 cl::desc("Emit a position-independent executable file"),
382 cl::init(false));
383
384static cl::opt<Reloc::Model>
385ArgRelocModel("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
399static cl::opt<bool>
400ArgFPIC("fPIC",
401 cl::desc("Set relocation model to pic. The same as -relocation-model=pic."),
402 cl::init(false));
403
404static cl::opt<std::string>
405ArgDyld("dynamic-linker",
406 cl::desc("Set the name of the dynamic linker."),
407 cl::value_desc("Program"));
408
409namespace color {
410enum Color {
411 Never,
412 Always,
413 Auto
414};
415} // namespace of color
416
417static cl::opt<color::Color>
418ArgColor("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 Liao5460a1f2012-03-16 22:41:16 -0700431//===----------------------------------------------------------------------===//
432// Inputs
433static cl::list<mcld::sys::fs::Path>
434ArgInputObjectFiles(cl::Positional,
435 cl::desc("[input object files]"),
436 cl::ZeroOrMore);
437
438static cl::list<std::string>
439ArgNameSpecList("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
445static cl::alias
446ArgNameSpecListAlias("library",
447 cl::desc("alias for -l"),
448 cl::aliasopt(ArgNameSpecList));
449
450static cl::list<bool>
451ArgStartGroupList("start-group",
452 cl::ValueDisallowed,
453 cl::desc("start to record a group of archives"));
454
455static cl::alias
456ArgStartGroupListAlias("(",
457 cl::desc("alias for --start-group"),
458 cl::aliasopt(ArgStartGroupList));
459
460static cl::list<bool>
461ArgEndGroupList("end-group",
462 cl::ValueDisallowed,
463 cl::desc("stop recording a group of archives"));
464
465static cl::alias
466ArgEndGroupListAlias(")",
467 cl::desc("alias for --end-group"),
468 cl::aliasopt(ArgEndGroupList));
469
470//===----------------------------------------------------------------------===//
471// Attributes of Inputs
472static cl::list<bool>
473ArgWholeArchiveList("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
477static cl::list<bool>
478ArgNoWholeArchiveList("no-whole-archive",
479 cl::ValueDisallowed,
480 cl::desc("Turn off the effect of the --whole-archive option for subsequent archive files."));
481
482static cl::list<bool>
483ArgAsNeededList("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
487static cl::list<bool>
488ArgNoAsNeededList("no-as-needed",
489 cl::ValueDisallowed,
490 cl::desc("Turn off the effect of the --as-needed option for subsequent dynamic libraries"));
491
492static cl::list<bool>
493ArgAddNeededList("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
497static cl::list<bool>
498ArgNoAddNeededList("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
502static cl::list<bool>
503ArgBDynamicList("Bdynamic",
504 cl::ValueDisallowed,
505 cl::desc("Link against dynamic library"));
506
507static cl::alias
508ArgBDynamicListAlias1("dy",
509 cl::desc("alias for --Bdynamic"),
510 cl::aliasopt(ArgBDynamicList));
511
512static cl::alias
513ArgBDynamicListAlias2("call_shared",
514 cl::desc("alias for --Bdynamic"),
515 cl::aliasopt(ArgBDynamicList));
516
517static cl::list<bool>
518ArgBStaticList("Bstatic",
519 cl::ValueDisallowed,
520 cl::desc("Link against static library"));
521
522static cl::alias
523ArgBStaticListAlias1("dn",
524 cl::desc("alias for --Bstatic"),
525 cl::aliasopt(ArgBStaticList));
526
527static cl::alias
528ArgBStaticListAlias2("static",
529 cl::desc("alias for --Bstatic"),
530 cl::aliasopt(ArgBStaticList));
531
532static cl::alias
533ArgBStaticListAlias3("non_shared",
534 cl::desc("alias for --Bstatic"),
535 cl::aliasopt(ArgBStaticList));
536
537//===----------------------------------------------------------------------===//
538// Scripting Options
Zonr Changaffc1502012-07-16 14:28:23 +0800539static cl::list<std::string>
540ArgWrapList("wrap",
541 cl::ZeroOrMore,
542 cl::desc("Use a wrap function fo symbol."),
543 cl::value_desc("symbol"));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700544
Zonr Changaffc1502012-07-16 14:28:23 +0800545static cl::list<std::string>
546ArgPortList("portable",
547 cl::ZeroOrMore,
548 cl::desc("Use a portable function fo symbol."),
549 cl::value_desc("symbol"));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700550
551//===----------------------------------------------------------------------===//
552/// non-member functions
553
Zonr Changaffc1502012-07-16 14:28:23 +0800554/// GetOutputStream - get the output stream.
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700555static tool_output_file *GetOutputStream(const char* pTargetName,
Zonr Changaffc1502012-07-16 14:28:23 +0800556 Triple::OSType pOSType,
557 mcld::CodeGenFileType pFileType,
558 const mcld::sys::fs::Path& pInputFilename,
559 mcld::sys::fs::Path& pOutputFilename)
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700560{
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700561 if (pOutputFilename.empty()) {
Zonr Changaffc1502012-07-16 14:28:23 +0800562 if (0 == pInputFilename.native().compare("-"))
563 pOutputFilename.assign("-");
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700564 else {
Zonr Changaffc1502012-07-16 14:28:23 +0800565 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 Liao5460a1f2012-03-16 22:41:16 -0700571
Zonr Changaffc1502012-07-16 14:28:23 +0800572 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 Liao5460a1f2012-03-16 22:41:16 -0700598 else
Zonr Changaffc1502012-07-16 14:28:23 +0800599 pOutputFilename.assign(pInputFilename.stem().native());
600 pOutputFilename.native() += ".dll";
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700601 }
602 else
Zonr Changaffc1502012-07-16 14:28:23 +0800603 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 Liao5460a1f2012-03-16 22:41:16 -0700615 else
Zonr Changaffc1502012-07-16 14:28:23 +0800616 pOutputFilename.assign("a.out");
617 }
618 break;
619
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700620 case mcld::CGFT_NULLFile:
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700621 break;
622 default:
Zonr Changaffc1502012-07-16 14:28:23 +0800623 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 Liao5460a1f2012-03-16 22:41:16 -0700627
628 // Decide if we need "binary" output.
Zonr Changaffc1502012-07-16 14:28:23 +0800629 unsigned int fd_flags = 0x0;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700630 switch (pFileType) {
631 default: assert(0 && "Unknown file type");
632 case mcld::CGFT_ASMFile:
633 break;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700634 case mcld::CGFT_OBJFile:
635 case mcld::CGFT_DSOFile:
636 case mcld::CGFT_EXEFile:
637 case mcld::CGFT_NULLFile:
Zonr Changaffc1502012-07-16 14:28:23 +0800638 fd_flags |= raw_fd_ostream::F_Binary;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700639 break;
640 }
641
642 // Open the file.
Zonr Changaffc1502012-07-16 14:28:23 +0800643 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 Liao5460a1f2012-03-16 22:41:16 -0700652 }
653
Zonr Changaffc1502012-07-16 14:28:23 +0800654 return result_output;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700655}
656
Zonr Changaffc1502012-07-16 14:28:23 +0800657static bool ShouldColorize()
658{
659 const char* term = getenv("TERM");
660 return term && (0 != strcmp(term, "dumb"));
661}
662
663static bool ProcessLinkerOptionsFromCommand(mcld::MCLDInfo& pLDInfo) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700664 // ----- Set up General Options ----- //
665 // set up soname
Zonr Changaffc1502012-07-16 14:28:23 +0800666 pLDInfo.output().setSOName(ArgSOName);
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700667
668 // set up sysroot
669 if (!ArgSysRoot.empty()) {
670 if (exists(ArgSysRoot) && is_directory(ArgSysRoot))
Zonr Changaffc1502012-07-16 14:28:23 +0800671 pLDInfo.options().setSysroot(ArgSysRoot);
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700672 }
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 Changaffc1502012-07-16 14:28:23 +0800679 sd->setSysroot(pLDInfo.options().sysroot());
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700680 if (exists(sd->path()) && is_directory(sd->path())) {
Zonr Changaffc1502012-07-16 14:28:23 +0800681 pLDInfo.options().directories().add(*sd);
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700682 }
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 Changaffc1502012-07-16 14:28:23 +0800691 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 Liao5460a1f2012-03-16 22:41:16 -0700703
Zonr Changaffc1502012-07-16 14:28:23 +0800704 // 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
782static bool ProcessLinkerInputsFromCommand(mcld::SectLinkerOption &pOption) {
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700783 // ----- 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 Changaffc1502012-07-16 14:28:23 +0800892 return true;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700893}
894
895int main( int argc, char* argv[] )
896{
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700897 LLVMContext &Context = getGlobalContext();
898 llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
Shih-wei Liao67e37f12012-07-27 03:50:34 -0700899 cl::ParseCommandLineOptions(argc, argv, "MCLinker\n");
900
901#ifdef ENABLE_UNITTEST
902 if (UnitTest) {
903 return unit_test( argc, argv );
904 }
905#endif
Zonr Changaffc1502012-07-16 14:28:23 +0800906
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700907 // Initialize targets first, so that --version shows registered targets.
908 InitializeAllTargets();
909 InitializeAllAsmPrinters();
910 InitializeAllAsmParsers();
911 InitializeAllTargetMCs();
912 mcld::InitializeAllTargets();
913 mcld::InitializeAllLinkers();
Zonr Changaffc1502012-07-16 14:28:23 +0800914 mcld::InitializeAllDiagnostics();
915
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700916 // Load the module to be compiled...
917 std::auto_ptr<Module> M;
918
Zonr Changaffc1502012-07-16 14:28:23 +0800919 // -shared
920 if (true == ArgShared) {
921 ArgFileType = mcld::CGFT_DSOFile;
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700922 }
923
Zonr Changaffc1502012-07-16 14:28:23 +0800924 // -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 Liao5460a1f2012-03-16 22:41:16 -0700945 SMDiagnostic Err;
Zonr Changaffc1502012-07-16 14:28:23 +0800946 M.reset(ParseIRFile(ArgBitcodeFilename.native(), Err, Context));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700947
948 if (M.get() == 0) {
949 Err.print(argv[0], errs());
950 errs() << "** Failed to to the given bitcode/llvm asm file '"
Zonr Changaffc1502012-07-16 14:28:23 +0800951 << ArgBitcodeFilename.native() << "'. **\n";
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700952 return 1;
953 }
Zonr Changaffc1502012-07-16 14:28:23 +0800954 }
955 else {
956 // If here, output must be dynamic shared object (mcld::CGFT_DSOFile) and
957 // executable file (mcld::CGFT_EXEFile).
Shih-wei Liao5460a1f2012-03-16 22:41:16 -0700958 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 Changaffc1502012-07-16 14:28:23 +08001008 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 Liao5460a1f2012-03-16 22:41:16 -07001013 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 Changaffc1502012-07-16 14:28:23 +08001038 // set -fPIC
1039 if (ArgFPIC)
1040 ArgRelocModel = Reloc::PIC_;
1041
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001042 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 Changaffc1502012-07-16 14:28:23 +08001070 ArgRelocModel, CMModel, OLvl));
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001071 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 Changaffc1502012-07-16 14:28:23 +08001077 // Set up mcld::outs() and mcld::errs()
1078 InitializeOStreams(TheTargetMachine.getLDInfo());
1079
1080 // Set up MsgHandler
1081 OwningPtr<mcld::DiagnosticLineInfo>
Shih-wei Liao67e37f12012-07-27 03:50:34 -07001082 diag_line_info(TheTarget->createDiagnosticLineInfo(*TheTarget,
Zonr Changaffc1502012-07-16 14:28:23 +08001083 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 Liao5460a1f2012-03-16 22:41:16 -07001093 // 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 Changaffc1502012-07-16 14:28:23 +08001097 ArgFileType,
1098 ArgBitcodeFilename,
1099 ArgOutputFilename));
1100 if (!Out) {
1101 // FIXME: show some error message pls.
1102 return 1;
1103 }
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001104
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 Changaffc1502012-07-16 14:28:23 +08001121 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 Liao5460a1f2012-03-16 22:41:16 -07001127 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 Changaffc1502012-07-16 14:28:23 +08001137 ArgOutputFilename.native(),
1138 ArgFileType,
Shih-wei Liao5460a1f2012-03-16 22:41:16 -07001139 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 Changaffc1502012-07-16 14:28:23 +08001159 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 Liao5460a1f2012-03-16 22:41:16 -07001169 return 0;
1170}
1171