From 358faec6ab268e4614494668706281f174e1ebb7 Mon Sep 17 00:00:00 2001 From: Mandeep Singh Grang Date: Thu, 1 Dec 2016 18:42:16 +0000 Subject: [PATCH] [clang] Implement support for -defsym assembler option Summary: Adds support for -Wa,-defsym,abc=1 option. Related llvm patch: https://reviews.llvm.org/D26214 Reviewers: rafael, t.p.northover, colinl, echristo, compnerd, rengolin Subscribers: mehdi_amini Tags: #clang-c Differential Revision: https://reviews.llvm.org/D26213 llvm-svn: 288397 --- clang/include/clang/Driver/CC1Options.td | 2 ++ clang/lib/Driver/Tools.cpp | 3 +++ clang/test/Driver/defsym.s | 22 ++++++++++++++++++++++ clang/tools/driver/cc1as_main.cpp | 12 ++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 clang/test/Driver/defsym.s diff --git a/clang/include/clang/Driver/CC1Options.td b/clang/include/clang/Driver/CC1Options.td index 5f4512d..f203d78 100644 --- a/clang/include/clang/Driver/CC1Options.td +++ b/clang/include/clang/Driver/CC1Options.td @@ -743,4 +743,6 @@ def show_inst : Flag<["-"], "show-inst">, def dwarf_debug_producer : Separate<["-"], "dwarf-debug-producer">, HelpText<"The string to embed in the Dwarf debug AT_producer record.">; +def defsym : Separate<["-"], "defsym">, + HelpText<"Define a value for a symbol">; } // let Flags = [CC1AsOption] diff --git a/clang/lib/Driver/Tools.cpp b/clang/lib/Driver/Tools.cpp index 757c0ce..40e6dd8 100644 --- a/clang/lib/Driver/Tools.cpp +++ b/clang/lib/Driver/Tools.cpp @@ -3115,6 +3115,9 @@ static void CollectArgsForIntegratedAssembler(Compilation &C, } else if (Value.startswith("-mcpu") || Value.startswith("-mfpu") || Value.startswith("-mhwdiv") || Value.startswith("-march")) { // Do nothing, we'll validate it later. + } else if (Value == "-defsym") { + CmdArgs.push_back(Value.data()); + TakeNextArg = true; } else { D.Diag(diag::err_drv_unsupported_option_argument) << A->getOption().getName() << Value; diff --git a/clang/test/Driver/defsym.s b/clang/test/Driver/defsym.s new file mode 100644 index 0000000..8623e29 --- /dev/null +++ b/clang/test/Driver/defsym.s @@ -0,0 +1,22 @@ +// RUN: %clang -### -c -integrated-as %s \ +// RUN: -Wa,-defsym,abc=5 -Wa,-defsym,xyz=0xa \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM1 + +// RUN: %clang -### -c -no-integrated-as %s \ +// RUN: -Wa,-defsym,abc=5 -Wa,-defsym,xyz=0xa \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM1 + +// CHECK-DEFSYM1: "-defsym" +// CHECK-DEFSYM1: "abc=5" +// CHECK-DEFSYM1: "-defsym" +// CHECK-DEFSYM1: "xyz=0xa" + +// RUN: not %clang -c -integrated-as %s \ +// RUN: -Wa,-defsym,abc= \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR1 +// CHECK-DEFSYM-ERR1: error: defsym must be of the form: sym=value: abc= + +// RUN: not %clang -c -integrated-as %s \ +// RUN: -Wa,-defsym,abc=1a2b3c \ +// RUN: 2>&1 | FileCheck %s --check-prefix=CHECK-DEFSYM-ERR2 +// CHECK-DEFSYM-ERR2: error: Value is not an integer: 1a2b3c diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp index 6b818a9..c936154 100644 --- a/clang/tools/driver/cc1as_main.cpp +++ b/clang/tools/driver/cc1as_main.cpp @@ -77,6 +77,9 @@ struct AssemblerInvocation { /// be a list of strings starting with '+' or '-'. std::vector Features; + /// The list of symbol definitions. + std::vector SymbolDefs; + /// @} /// @name Language Options /// @{ @@ -252,6 +255,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts, Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); Opts.IncrementalLinkerCompatible = Args.hasArg(OPT_mincremental_linker_compatible); + Opts.SymbolDefs = Args.getAllArgValues(OPT_defsym); return Success; } @@ -420,6 +424,14 @@ static bool ExecuteAssembler(AssemblerInvocation &Opts, if (!TAP) Failed = Diags.Report(diag::err_target_unknown_triple) << Opts.Triple; + // Set values for symbols, if any. + for (auto &S : Opts.SymbolDefs) { + if (Ctx.setSymbolValue(Parser->getStreamer(), S)) { + Failed = true; + break; + } + } + if (!Failed) { Parser->setTargetParser(*TAP.get()); Failed = Parser->Run(Opts.NoInitialTextSection); -- 2.7.4