Skip to content

Commit d11ae8a

Browse files
committed
[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
1 parent 5285209 commit d11ae8a

File tree

3 files changed

+18
-2
lines changed

3 files changed

+18
-2
lines changed

lib/CodeGen/BackendUtil.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,8 @@ static llvm::CodeModel::Model getCodeModel(const CodeGenOptions &CodeGenOpts) {
323323
}
324324

325325
static llvm::Reloc::Model getRelocModel(const CodeGenOptions &CodeGenOpts) {
326-
// Keep this synced with the equivalent code in tools/driver/cc1as_main.cpp.
326+
// Keep this synced with the equivalent code in
327+
// lib/Frontend/CompilerInvocation.cpp
327328
llvm::Optional<llvm::Reloc::Model> RM;
328329
RM = llvm::StringSwitch<llvm::Reloc::Model>(CodeGenOpts.RelocationModel)
329330
.Case("static", llvm::Reloc::Static)

lib/Frontend/CompilerInvocation.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,17 @@ static StringRef getCodeModel(ArgList &Args, DiagnosticsEngine &Diags) {
330330
return "default";
331331
}
332332

333+
static StringRef getRelocModel(ArgList &Args, DiagnosticsEngine &Diags) {
334+
if (Arg *A = Args.getLastArg(OPT_mrelocation_model)) {
335+
StringRef Value = A->getValue();
336+
if (Value == "static" || Value == "pic" || Value == "ropi" ||
337+
Value == "rwpi" || Value == "ropi-rwpi" || Value == "dynamic-no-pic")
338+
return Value;
339+
Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Value;
340+
}
341+
return "pic";
342+
}
343+
333344
/// \brief Create a new Regex instance out of the string value in \p RpassArg.
334345
/// It returns a pointer to the newly generated Regex instance.
335346
static std::shared_ptr<llvm::Regex>
@@ -615,7 +626,7 @@ static bool ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, InputKind IK,
615626
Args.hasArg(OPT_cl_unsafe_math_optimizations) ||
616627
Args.hasArg(OPT_cl_fast_relaxed_math);
617628
Opts.UnwindTables = Args.hasArg(OPT_munwind_tables);
618-
Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic");
629+
Opts.RelocationModel = getRelocModel(Args, Diags);
619630
Opts.ThreadModel = Args.getLastArgValue(OPT_mthread_model, "posix");
620631
if (Opts.ThreadModel != "posix" && Opts.ThreadModel != "single")
621632
Diags.Report(diag::err_drv_invalid_value)

test/Driver/reloc-model.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: not %clang -cc1 -mrelocation-model tinkywinky \
2+
// RUN: -emit-llvm %s 2>&1 | FileCheck -check-prefix CHECK-INVALID %s
3+
4+
// CHECK-INVALID: error: invalid value 'tinkywinky' in '-mrelocation-model tinkywinky'

0 commit comments

Comments
 (0)