Skip to content

Commit 04a1a34

Browse files
authored
[Driver] Add -Wa, options --crel and --allow-experimental-crel
The two options are discussed in a few comments around #91280 (comment) * -Wa,--crel: error "-Wa,--allow-experimental-crel must be specified to use -Wa,--crel..." * -Wa,--allow-experimental-crel: no-op * -Wa,--crel,--allow-experimental-crel: enable CREL in the integrated assembler (#91280) MIPS's little-endian n64 ABI messed up the `r_info` field in relocations. While this could be fixed with CREL, my intention is to avoid complication in assembler/linker. The implementation simply doesn't allow CREL for MIPS. Link: https://discourse.llvm.org/t/rfc-crel-a-compact-relocation-format-for-elf/77600 Pull Request: #97378
1 parent b586498 commit 04a1a34

File tree

9 files changed

+91
-0
lines changed

9 files changed

+91
-0
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ VALUE_CODEGENOPT(Name, Bits, Default)
3636
#endif
3737

3838
CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
39+
CODEGENOPT(Crel, 1, 0) ///< -Wa,--crel
3940
CODEGENOPT(RelaxELFRelocations, 1, 1) ///< -Wa,-mrelax-relocations={yes,no}
4041
CODEGENOPT(AsmVerbose , 1, 0) ///< -dA, -fverbose-asm.
4142
CODEGENOPT(PreserveAsmComments, 1, 1) ///< -dA, -fno-preserve-as-comments.

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -801,6 +801,10 @@ def warn_drv_missing_multilib : Warning<
801801
def note_drv_available_multilibs : Note<
802802
"available multilibs are:%0">;
803803

804+
def err_drv_experimental_crel : Error<
805+
"-Wa,--allow-experimental-crel must be specified to use -Wa,--crel. "
806+
"CREL is experimental and uses a non-standard section type code">;
807+
804808
def warn_android_unversioned_fallback : Warning<
805809
"using unversioned Android target directory %0 for target %1; unversioned "
806810
"directories will not be used in Clang 19 -- provide a versioned directory "

clang/include/clang/Driver/Options.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7027,6 +7027,9 @@ def massembler_no_warn : Flag<["-"], "massembler-no-warn">,
70277027
def massembler_fatal_warnings : Flag<["-"], "massembler-fatal-warnings">,
70287028
HelpText<"Make assembler warnings fatal">,
70297029
MarshallingInfoFlag<CodeGenOpts<"FatalWarnings">>;
7030+
def crel : Flag<["--"], "crel">,
7031+
HelpText<"Enable CREL relocation format (ELF only)">,
7032+
MarshallingInfoFlag<CodeGenOpts<"Crel">>;
70307033
def mrelax_relocations_no : Flag<["-"], "mrelax-relocations=no">,
70317034
HelpText<"Disable x86 relax relocations">,
70327035
MarshallingInfoNegativeFlag<CodeGenOpts<"RelaxELFRelocations">>;

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
470470
Options.MCOptions.AsmVerbose = CodeGenOpts.AsmVerbose;
471471
Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
472472
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
473+
Options.MCOptions.Crel = CodeGenOpts.Crel;
473474
Options.MCOptions.X86RelaxRelocations = CodeGenOpts.RelaxELFRelocations;
474475
Options.MCOptions.CompressDebugSections =
475476
CodeGenOpts.getCompressDebugSections();

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2500,6 +2500,8 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
25002500
// arg after parsing the '-I' arg.
25012501
bool TakeNextArg = false;
25022502

2503+
const llvm::Triple &Triple = C.getDefaultToolChain().getTriple();
2504+
bool Crel = false, ExperimentalCrel = false;
25032505
bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
25042506
bool UseNoExecStack = false;
25052507
const char *MipsTargetFeature = nullptr;
@@ -2623,6 +2625,12 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
26232625
Value == "-nocompress-debug-sections" ||
26242626
Value == "--nocompress-debug-sections") {
26252627
CmdArgs.push_back(Value.data());
2628+
} else if (Value == "--crel") {
2629+
Crel = true;
2630+
} else if (Value == "--no-crel") {
2631+
Crel = false;
2632+
} else if (Value == "--allow-experimental-crel") {
2633+
ExperimentalCrel = true;
26262634
} else if (Value == "-mrelax-relocations=yes" ||
26272635
Value == "--mrelax-relocations=yes") {
26282636
UseRelaxRelocations = true;
@@ -2688,6 +2696,16 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
26882696
}
26892697
if (ImplicitIt.size())
26902698
AddARMImplicitITArgs(Args, CmdArgs, ImplicitIt);
2699+
if (Crel) {
2700+
if (!ExperimentalCrel)
2701+
D.Diag(diag::err_drv_experimental_crel);
2702+
if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) {
2703+
CmdArgs.push_back("--crel");
2704+
} else {
2705+
D.Diag(diag::err_drv_unsupported_opt_for_target)
2706+
<< "-Wa,--crel" << D.getTargetTriple();
2707+
}
2708+
}
26912709
if (!UseRelaxRelocations)
26922710
CmdArgs.push_back("-mrelax-relocations=no");
26932711
if (UseNoExecStack)

clang/lib/Driver/ToolChains/CommonArgs.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,6 +1133,27 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11331133

11341134
addMachineOutlinerArgs(D, Args, CmdArgs, ToolChain.getEffectiveTriple(),
11351135
/*IsLTO=*/true, PluginOptPrefix);
1136+
1137+
for (const Arg *A : Args.filtered(options::OPT_Wa_COMMA)) {
1138+
bool Crel = false;
1139+
for (StringRef V : A->getValues()) {
1140+
if (V == "--crel")
1141+
Crel = true;
1142+
else if (V == "--no-crel")
1143+
Crel = false;
1144+
else
1145+
continue;
1146+
A->claim();
1147+
}
1148+
if (Crel) {
1149+
if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) {
1150+
CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + "-crel"));
1151+
} else {
1152+
D.Diag(diag::err_drv_unsupported_opt_for_target)
1153+
<< "-Wa,--crel" << D.getTargetTriple();
1154+
}
1155+
}
1156+
}
11361157
}
11371158

11381159
/// Adds the '-lcgpu' and '-lmgpu' libraries to the compilation to include the

clang/test/Driver/crel.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// RUN: not %clang -### -c --target=x86_64 -Wa,--crel %s 2>&1 | FileCheck %s --check-prefix=NOEXP
2+
3+
// NOEXP: error: -Wa,--allow-experimental-crel must be specified to use -Wa,--crel. CREL is experimental and uses a non-standard section type code
4+
5+
// RUN: %clang -### -c --target=x86_64 -Wa,--crel,--allow-experimental-crel %s -Werror 2>&1 | FileCheck %s
6+
// RUN: %clang -### -c --target=x86_64 -Wa,--crel,--no-crel,--allow-experimental-crel %s -Werror 2>&1 | FileCheck %s --check-prefix=NO
7+
// RUN: %clang -### -c --target=x86_64 -Wa,--allow-experimental-crel %s -Werror 2>&1 | FileCheck %s --check-prefix=NO
8+
// RUN: not %clang -### -c --target=arm64-apple-darwin -Wa,--crel,--allow-experimental-crel %s 2>&1 | FileCheck %s --check-prefix=ERR
9+
// RUN: not %clang -### -c --target=mips64 -Wa,--crel,--allow-experimental-crel %s 2>&1 | FileCheck %s --check-prefix=ERR
10+
11+
// RUN: %clang -### -c --target=aarch64 -Werror -Wa,--crel,--allow-experimental-crel -x assembler %s -Werror 2>&1 | FileCheck %s --check-prefix=ASM
12+
// RUN: not %clang -### -c --target=mips64 -Wa,--crel,--allow-experimental-crel -x assembler %s 2>&1 | FileCheck %s --check-prefix=ERR
13+
14+
// CHECK: "-cc1" {{.*}}"--crel"
15+
// NO: "-cc1"
16+
// NO-NOT: "--crel"
17+
// ASM: "-cc1as" {{.*}}"--crel"
18+
// ERR: error: unsupported option '-Wa,--crel' for target '{{.*}}'
19+
20+
/// The --allow-experimental-crel error check is exempted for -fno-integrated-as.
21+
// RUN: %clang -### -c --target=aarch64 -fno-integrated-as -Wa,--crel %s -Werror 2>&1 | FileCheck %s --check-prefix=GAS
22+
23+
// GAS: "--crel"
24+
25+
/// The --allow-experimental-crel error check doesn't apply to LTO.
26+
// RUN: %clang -### --target=x86_64-linux -Werror -flto -Wa,--crel %s 2>&1 | FileCheck %s --check-prefix=LTO
27+
28+
// LTO: "-plugin-opt=-crel"
29+
30+
// RUN: touch %t.o
31+
// RUN: not %clang -### --target=mips64-linux-gnu -flto -Wa,--crel %t.o 2>&1 | FileCheck %s --check-prefix=ERR

clang/test/Misc/cc1as-crel.s

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// REQUIRES: x86-registered-target
2+
// RUN: %clang -cc1as -triple x86_64 %s -filetype obj --crel -o %t.o
3+
// RUN: llvm-readelf -S %t.o | FileCheck %s
4+
5+
// CHECK: .crel.text CREL
6+
call foo

clang/tools/driver/cc1as_main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ struct AssemblerInvocation {
164164
LLVM_PREFERRED_TYPE(bool)
165165
unsigned EmitCompactUnwindNonCanonical : 1;
166166

167+
LLVM_PREFERRED_TYPE(bool)
168+
unsigned Crel : 1;
169+
167170
/// The name of the relocation model to use.
168171
std::string RelocationModel;
169172

@@ -204,6 +207,7 @@ struct AssemblerInvocation {
204207
EmbedBitcode = 0;
205208
EmitDwarfUnwind = EmitDwarfUnwindType::Default;
206209
EmitCompactUnwindNonCanonical = false;
210+
Crel = false;
207211
}
208212

209213
static bool CreateFromArgs(AssemblerInvocation &Res,
@@ -373,6 +377,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
373377

374378
Opts.EmitCompactUnwindNonCanonical =
375379
Args.hasArg(OPT_femit_compact_unwind_non_canonical);
380+
Opts.Crel = Args.hasArg(OPT_crel);
376381

377382
Opts.AsSecureLogFile = Args.getLastArgValue(OPT_as_secure_log_file);
378383

@@ -430,6 +435,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
430435
MCOptions.EmitDwarfUnwind = Opts.EmitDwarfUnwind;
431436
MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
432437
MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;
438+
MCOptions.Crel = Opts.Crel;
433439
MCOptions.X86RelaxRelocations = Opts.RelaxELFRelocations;
434440
MCOptions.CompressDebugSections = Opts.CompressDebugSections;
435441
MCOptions.AsSecureLogFile = Opts.AsSecureLogFile;

0 commit comments

Comments
 (0)