[clang] eb549da - [Driver] Add -Wa, options -mmapsyms={default, implicit}
via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 22 09:20:56 PDT 2024
Author: Fangrui Song
Date: 2024-08-22T09:20:53-07:00
New Revision: eb549da9e5c1e626edb14ba9ce43e46ad3d088af
URL: https://github.com/llvm/llvm-project/commit/eb549da9e5c1e626edb14ba9ce43e46ad3d088af
DIFF: https://github.com/llvm/llvm-project/commit/eb549da9e5c1e626edb14ba9ce43e46ad3d088af.diff
LOG: [Driver] Add -Wa, options -mmapsyms={default,implicit}
-Wa,-mmapsyms=implicit enables the alternative mapping symbol scheme
discussed at #99718.
While not conforming to the current aaelf64 ABI, the option is
invaluable for those with full control over their toolchain, no reliance
on weird relocatable files, and a strong focus on minimizing both
relocatable and executable sizes.
The option is discouraged when portability of the relocatable objects is
a concern.
https://maskray.me/blog/2024-07-21-mapping-symbols-rethinking-for-efficiency
elaborates the risk.
Pull Request: https://github.com/llvm/llvm-project/pull/104542
Added:
clang/test/Driver/mmapsyms.c
clang/test/Misc/cc1as-mmapsyms.c
Modified:
clang/include/clang/Basic/CodeGenOptions.def
clang/include/clang/Driver/Options.td
clang/lib/CodeGen/BackendUtil.cpp
clang/lib/Driver/ToolChains/Clang.cpp
clang/lib/Driver/ToolChains/CommonArgs.cpp
clang/tools/driver/cc1as_main.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def
index ecea476abe3232..b600198998d85b 100644
--- a/clang/include/clang/Basic/CodeGenOptions.def
+++ b/clang/include/clang/Basic/CodeGenOptions.def
@@ -37,6 +37,7 @@ VALUE_CODEGENOPT(Name, Bits, Default)
CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
CODEGENOPT(Crel, 1, 0) ///< -Wa,--crel
+CODEGENOPT(ImplicitMapSyms, 1, 0) ///< -Wa,-mmapsyms=implicit
CODEGENOPT(AsmVerbose , 1, 0) ///< -dA, -fverbose-asm.
CODEGENOPT(PreserveAsmComments, 1, 1) ///< -dA, -fno-preserve-as-comments.
CODEGENOPT(AssumeSaneOperatorNew , 1, 1) ///< implicit __attribute__((malloc)) operator new
diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td
index 5d8791727d2109..7a3c699a6a8e88 100644
--- a/clang/include/clang/Driver/Options.td
+++ b/clang/include/clang/Driver/Options.td
@@ -7142,6 +7142,12 @@ def massembler_fatal_warnings : Flag<["-"], "massembler-fatal-warnings">,
def crel : Flag<["--"], "crel">,
HelpText<"Enable CREL relocation format (ELF only)">,
MarshallingInfoFlag<CodeGenOpts<"Crel">>;
+def mmapsyms_implicit : Flag<["-"], "mmapsyms=implicit">,
+ HelpText<"Allow mapping symbol at section beginning to be implicit, "
+ "lowering number of mapping symbols at the expense of some "
+ "portability. Recommended for projects that can build all their "
+ "object files using this option">,
+ MarshallingInfoFlag<CodeGenOpts<"ImplicitMapSyms">>;
def mrelax_relocations_no : Flag<["-"], "mrelax-relocations=no">,
HelpText<"Disable x86 relax relocations">,
MarshallingInfoNegativeFlag<CodeGenOpts<"X86RelaxRelocations">>;
diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp
index 34c08818dbb9ad..fdd89edd72e109 100644
--- a/clang/lib/CodeGen/BackendUtil.cpp
+++ b/clang/lib/CodeGen/BackendUtil.cpp
@@ -471,6 +471,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
Options.MCOptions.Crel = CodeGenOpts.Crel;
+ Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms;
Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations;
Options.MCOptions.CompressDebugSections =
CodeGenOpts.getCompressDebugSections();
diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 53fdc29948508e..9f1d57f43b6565 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -2554,6 +2554,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
const llvm::Triple &Triple = C.getDefaultToolChain().getTriple();
bool IsELF = Triple.isOSBinFormatELF();
bool Crel = false, ExperimentalCrel = false;
+ bool ImplicitMapSyms = false;
bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
bool UseNoExecStack = false;
bool Msa = false;
@@ -2642,6 +2643,15 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
// recognize but skip over here.
continue;
break;
+ case llvm::Triple::aarch64:
+ case llvm::Triple::aarch64_be:
+ case llvm::Triple::aarch64_32:
+ if (Equal.first == "-mmapsyms") {
+ ImplicitMapSyms = Equal.second == "implicit";
+ checkArg(IsELF, {"default", "implicit"});
+ continue;
+ }
+ break;
case llvm::Triple::mips:
case llvm::Triple::mipsel:
case llvm::Triple::mips64:
@@ -2786,6 +2796,8 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
<< "-Wa,--crel" << D.getTargetTriple();
}
}
+ if (ImplicitMapSyms)
+ CmdArgs.push_back("-mmapsyms=implicit");
if (Msa)
CmdArgs.push_back("-mmsa");
if (!UseRelaxRelocations)
diff --git a/clang/lib/Driver/ToolChains/CommonArgs.cpp b/clang/lib/Driver/ToolChains/CommonArgs.cpp
index 320d2901da06ed..0738ed18f54078 100644
--- a/clang/lib/Driver/ToolChains/CommonArgs.cpp
+++ b/clang/lib/Driver/ToolChains/CommonArgs.cpp
@@ -1143,10 +1143,27 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
addMachineOutlinerArgs(D, Args, CmdArgs, ToolChain.getEffectiveTriple(),
/*IsLTO=*/true, PluginOptPrefix);
+ bool IsELF = Triple.isOSBinFormatELF();
bool Crel = false;
+ bool ImplicitMapSyms = false;
for (const Arg *A : Args.filtered(options::OPT_Wa_COMMA)) {
for (StringRef V : A->getValues()) {
- if (V == "--crel")
+ auto Equal = V.split('=');
+ auto checkArg = [&](bool ValidTarget,
+ std::initializer_list<const char *> Set) {
+ if (!ValidTarget) {
+ D.Diag(diag::err_drv_unsupported_opt_for_target)
+ << (Twine("-Wa,") + Equal.first + "=").str()
+ << Triple.getTriple();
+ } else if (!llvm::is_contained(Set, Equal.second)) {
+ D.Diag(diag::err_drv_unsupported_option_argument)
+ << (Twine("-Wa,") + Equal.first + "=").str() << Equal.second;
+ }
+ };
+ if (Equal.first == "-mmapsyms") {
+ ImplicitMapSyms = Equal.second == "implicit";
+ checkArg(IsELF && Triple.isAArch64(), {"default", "implicit"});
+ } else if (V == "--crel")
Crel = true;
else if (V == "--no-crel")
Crel = false;
@@ -1156,13 +1173,16 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
}
}
if (Crel) {
- if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) {
+ if (IsELF && !Triple.isMIPS()) {
CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + "-crel"));
} else {
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< "-Wa,--crel" << D.getTargetTriple();
}
}
+ if (ImplicitMapSyms)
+ CmdArgs.push_back(
+ Args.MakeArgString(Twine(PluginOptPrefix) + "-implicit-mapsyms"));
}
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,
diff --git a/clang/test/Driver/mmapsyms.c b/clang/test/Driver/mmapsyms.c
new file mode 100644
index 00000000000000..3535af3cf1cdda
--- /dev/null
+++ b/clang/test/Driver/mmapsyms.c
@@ -0,0 +1,28 @@
+/// Alternative mapping symbol scheme for AArch64.
+// RUN: %clang -### -c --target=aarch64 -Wa,-mmapsyms=implicit %s -Werror 2>&1 | FileCheck %s
+// RUN: %clang -### -c --target=aarch64_be -Wa,-mmapsyms=implicit %s -Werror 2>&1 | FileCheck %s
+// RUN: %clang -### -c --target=aarch64 -Wa,-mmapsyms=implicit,-mmapsyms=default %s -Werror 2>&1 | FileCheck %s --check-prefix=NO
+// RUN: not %clang -### -c --target=arm64-apple-darwin -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=ERR
+// RUN: not %clang -### -c --target=x86_64 -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=ERR2
+
+// RUN: %clang -### -c --target=aarch64 -Werror -Wa,-mmapsyms=implicit -x assembler %s -Werror 2>&1 | FileCheck %s --check-prefix=ASM
+// RUN: not %clang -### -c --target=x86_64 -Wa,-mmapsyms=implicit -x assembler %s 2>&1 | FileCheck %s --check-prefix=ERR2
+
+// CHECK: "-cc1" {{.*}}"-mmapsyms=implicit"
+// NO: "-cc1"
+// NO-NOT: "-mmapsyms=implicit"
+// ASM: "-cc1as" {{.*}}"-mmapsyms=implicit"
+// ERR: error: unsupported option '-Wa,-mmapsyms=' for target 'arm64-apple-darwin'
+// ERR2: error: unsupported argument '-mmapsyms=implicit' to option '-Wa,'
+
+/// Check LTO.
+// RUN: %clang -### --target=aarch64-linux -Werror -flto -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=LTO
+// RUN: %clang -### --target=aarch64-linux -Werror -flto -Wa,-mmapsyms=implicit -Wa,-mmapsyms=default %s 2>&1 | FileCheck %s --check-prefix=LTO-NO
+
+// LTO: "-plugin-opt=-implicit-mapsyms"
+// LTO-NO-NOT: "-plugin-opt=-implicit-mapsyms"
+
+// RUN: touch %t.o
+// RUN: not %clang -### --target=x86_64-unknown-linux -flto -Wa,-mmapsyms=implicit %t.o 2>&1 | FileCheck %s --check-prefix=LTO-ERR
+
+// LTO-ERR: error: unsupported option '-Wa,-mmapsyms=' for target 'x86_64-unknown-linux'
diff --git a/clang/test/Misc/cc1as-mmapsyms.c b/clang/test/Misc/cc1as-mmapsyms.c
new file mode 100644
index 00000000000000..550281903c216e
--- /dev/null
+++ b/clang/test/Misc/cc1as-mmapsyms.c
@@ -0,0 +1,9 @@
+// REQUIRES: aarch64-registered-target
+// RUN: %clang -cc1as -triple aarch64 %s -filetype obj -mmapsyms=implicit -o %t.o
+// RUN: llvm-readelf -s %t.o | FileCheck %s
+
+// CHECK: Symbol table '.symtab' contains 1 entries:
+nop
+
+.data
+.quad 0
diff --git a/clang/tools/driver/cc1as_main.cpp b/clang/tools/driver/cc1as_main.cpp
index 070cf8b44e8eb6..7fe97cc6e6ace1 100644
--- a/clang/tools/driver/cc1as_main.cpp
+++ b/clang/tools/driver/cc1as_main.cpp
@@ -164,6 +164,8 @@ struct AssemblerInvocation {
LLVM_PREFERRED_TYPE(bool)
unsigned Crel : 1;
+ LLVM_PREFERRED_TYPE(bool)
+ unsigned ImplicitMapsyms : 1;
LLVM_PREFERRED_TYPE(bool)
unsigned X86RelaxRelocations : 1;
@@ -211,6 +213,7 @@ struct AssemblerInvocation {
EmitDwarfUnwind = EmitDwarfUnwindType::Default;
EmitCompactUnwindNonCanonical = false;
Crel = false;
+ ImplicitMapsyms = 0;
X86RelaxRelocations = 0;
X86Sse2Avx = 0;
}
@@ -382,6 +385,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
Opts.EmitCompactUnwindNonCanonical =
Args.hasArg(OPT_femit_compact_unwind_non_canonical);
Opts.Crel = Args.hasArg(OPT_crel);
+ Opts.ImplicitMapsyms = Args.hasArg(OPT_mmapsyms_implicit);
Opts.X86RelaxRelocations = !Args.hasArg(OPT_mrelax_relocations_no);
Opts.X86Sse2Avx = Args.hasArg(OPT_msse2avx);
@@ -442,6 +446,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;
MCOptions.Crel = Opts.Crel;
+ MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms;
MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations;
MCOptions.X86Sse2Avx = Opts.X86Sse2Avx;
MCOptions.CompressDebugSections = Opts.CompressDebugSections;
More information about the cfe-commits
mailing list