From d11ae8abf6a042aa838045a82696022fdfe353e5 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Sat, 1 Apr 2017 21:07:07 +0000 Subject: [PATCH] [Driver] Don't crash on invalid values of -mrelocation-model=. This is handled in a similar way we handle invalid -mcode-model. PR: 31840 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@299315 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/BackendUtil.cpp | 3 ++- lib/Frontend/CompilerInvocation.cpp | 13 ++++++++++++- test/Driver/reloc-model.c | 4 ++++ 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 test/Driver/reloc-model.c diff --git a/lib/CodeGen/BackendUtil.cpp b/lib/CodeGen/BackendUtil.cpp index 874fd14094..855d6795b9 100644 --- a/lib/CodeGen/BackendUtil.cpp +++ b/lib/CodeGen/BackendUtil.cpp @@ -323,7 +323,8 @@ static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) { } static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) { - // Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp. + // Keep this synced with the equivalent code in + // lib/Frontend/CompilerInvocation.cpp llvm::Optional RM; RM = llvm::StringSwitch(CodeGenOpts.RelocationModel) .Case("static", llvm::Reloc::Static) diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index a9d805b7db..9138450181 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -330,6 +330,17 @@ static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) { return "default"; } +static StringRef getRelocModel(ArgList &Args, DiagnosticsEngine &Diags) { + if (Arg *A = Args.getLastArg(OPT_mrelocation_model)) { + StringRef Value = A->getValue(); + if (Value == "static" || Value == "pic" || Value == "ropi" || + Value == "rwpi" || Value == "ropi-rwpi" || Value == "dynamic-no-pic") + return Value; + Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value; + } + return "pic"; +} + /// \brief Create a new Regex instance out of the string value in \p RpassArg. /// It returns a pointer to the newly generated Regex instance. static std::shared_ptr @@ -615,7 +626,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK, Args.hasArg(OPT_cl_unsafe_math_optimizations) || Args.hasArg(OPT_cl_fast_relaxed_math); Opts.UnwindTables = Args.hasArg(OPT_munwind_tables); - Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); + Opts.RelocationModel = getRelocModel(Args, Diags); Opts.ThreadModel = Args.getLastArgValue(OPT_mthread_model, "posix"); if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single") Diags.Report(diag::err_drv_invalid_value) diff --git a/test/Driver/reloc-model.c b/test/Driver/reloc-model.c new file mode 100644 index 0000000000..5d08eee89b --- /dev/null +++ b/test/Driver/reloc-model.c @@ -0,0 +1,4 @@ +// RUN: not %clang -cc1 -mrelocation-model tinkywinky \ +// RUN: -emit-llvm %s 2>&1 | FileCheck -check-prefix CHECK-INVALID %s + +// CHECK-INVALID: error: invalid value 'tinkywinky' in '-mrelocation-model tinkywinky' -- 2.40.0