Skip to content

Commit eb549da

Browse files
authored
[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: #104542
1 parent 83fc989 commit eb549da

File tree

8 files changed

+84
-2
lines changed

8 files changed

+84
-2
lines changed

clang/include/clang/Basic/CodeGenOptions.def

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ VALUE_CODEGENOPT(Name, Bits, Default)
3737

3838
CODEGENOPT(DisableIntegratedAS, 1, 0) ///< -no-integrated-as
3939
CODEGENOPT(Crel, 1, 0) ///< -Wa,--crel
40+
CODEGENOPT(ImplicitMapSyms, 1, 0) ///< -Wa,-mmapsyms=implicit
4041
CODEGENOPT(AsmVerbose , 1, 0) ///< -dA, -fverbose-asm.
4142
CODEGENOPT(PreserveAsmComments, 1, 1) ///< -dA, -fno-preserve-as-comments.
4243
CODEGENOPT(AssumeSaneOperatorNew , 1, 1) ///< implicit __attribute__((malloc)) operator new

clang/include/clang/Driver/Options.td

+6
Original file line numberDiff line numberDiff line change
@@ -7142,6 +7142,12 @@ def massembler_fatal_warnings : Flag<["-"], "massembler-fatal-warnings">,
71427142
def crel : Flag<["--"], "crel">,
71437143
HelpText<"Enable CREL relocation format (ELF only)">,
71447144
MarshallingInfoFlag<CodeGenOpts<"Crel">>;
7145+
def mmapsyms_implicit : Flag<["-"], "mmapsyms=implicit">,
7146+
HelpText<"Allow mapping symbol at section beginning to be implicit, "
7147+
"lowering number of mapping symbols at the expense of some "
7148+
"portability. Recommended for projects that can build all their "
7149+
"object files using this option">,
7150+
MarshallingInfoFlag<CodeGenOpts<"ImplicitMapSyms">>;
71457151
def mrelax_relocations_no : Flag<["-"], "mrelax-relocations=no">,
71467152
HelpText<"Disable x86 relax relocations">,
71477153
MarshallingInfoNegativeFlag<CodeGenOpts<"X86RelaxRelocations">>;

clang/lib/CodeGen/BackendUtil.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ static bool initTargetOptions(DiagnosticsEngine &Diags,
471471
Options.MCOptions.Dwarf64 = CodeGenOpts.Dwarf64;
472472
Options.MCOptions.PreserveAsmComments = CodeGenOpts.PreserveAsmComments;
473473
Options.MCOptions.Crel = CodeGenOpts.Crel;
474+
Options.MCOptions.ImplicitMapSyms = CodeGenOpts.ImplicitMapSyms;
474475
Options.MCOptions.X86RelaxRelocations = CodeGenOpts.X86RelaxRelocations;
475476
Options.MCOptions.CompressDebugSections =
476477
CodeGenOpts.getCompressDebugSections();

clang/lib/Driver/ToolChains/Clang.cpp

+12
Original file line numberDiff line numberDiff line change
@@ -2554,6 +2554,7 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
25542554
const llvm::Triple &Triple = C.getDefaultToolChain().getTriple();
25552555
bool IsELF = Triple.isOSBinFormatELF();
25562556
bool Crel = false, ExperimentalCrel = false;
2557+
bool ImplicitMapSyms = false;
25572558
bool UseRelaxRelocations = C.getDefaultToolChain().useRelaxRelocations();
25582559
bool UseNoExecStack = false;
25592560
bool Msa = false;
@@ -2642,6 +2643,15 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
26422643
// recognize but skip over here.
26432644
continue;
26442645
break;
2646+
case llvm::Triple::aarch64:
2647+
case llvm::Triple::aarch64_be:
2648+
case llvm::Triple::aarch64_32:
2649+
if (Equal.first == "-mmapsyms") {
2650+
ImplicitMapSyms = Equal.second == "implicit";
2651+
checkArg(IsELF, {"default", "implicit"});
2652+
continue;
2653+
}
2654+
break;
26452655
case llvm::Triple::mips:
26462656
case llvm::Triple::mipsel:
26472657
case llvm::Triple::mips64:
@@ -2786,6 +2796,8 @@ static void CollectArgsForIntegratedAssembler(Compilation &C,
27862796
<< "-Wa,--crel" << D.getTargetTriple();
27872797
}
27882798
}
2799+
if (ImplicitMapSyms)
2800+
CmdArgs.push_back("-mmapsyms=implicit");
27892801
if (Msa)
27902802
CmdArgs.push_back("-mmsa");
27912803
if (!UseRelaxRelocations)

clang/lib/Driver/ToolChains/CommonArgs.cpp

+22-2
Original file line numberDiff line numberDiff line change
@@ -1143,10 +1143,27 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11431143
addMachineOutlinerArgs(D, Args, CmdArgs, ToolChain.getEffectiveTriple(),
11441144
/*IsLTO=*/true, PluginOptPrefix);
11451145

1146+
bool IsELF = Triple.isOSBinFormatELF();
11461147
bool Crel = false;
1148+
bool ImplicitMapSyms = false;
11471149
for (const Arg *A : Args.filtered(options::OPT_Wa_COMMA)) {
11481150
for (StringRef V : A->getValues()) {
1149-
if (V == "--crel")
1151+
auto Equal = V.split('=');
1152+
auto checkArg = [&](bool ValidTarget,
1153+
std::initializer_list<const char *> Set) {
1154+
if (!ValidTarget) {
1155+
D.Diag(diag::err_drv_unsupported_opt_for_target)
1156+
<< (Twine("-Wa,") + Equal.first + "=").str()
1157+
<< Triple.getTriple();
1158+
} else if (!llvm::is_contained(Set, Equal.second)) {
1159+
D.Diag(diag::err_drv_unsupported_option_argument)
1160+
<< (Twine("-Wa,") + Equal.first + "=").str() << Equal.second;
1161+
}
1162+
};
1163+
if (Equal.first == "-mmapsyms") {
1164+
ImplicitMapSyms = Equal.second == "implicit";
1165+
checkArg(IsELF && Triple.isAArch64(), {"default", "implicit"});
1166+
} else if (V == "--crel")
11501167
Crel = true;
11511168
else if (V == "--no-crel")
11521169
Crel = false;
@@ -1156,13 +1173,16 @@ void tools::addLTOOptions(const ToolChain &ToolChain, const ArgList &Args,
11561173
}
11571174
}
11581175
if (Crel) {
1159-
if (Triple.isOSBinFormatELF() && !Triple.isMIPS()) {
1176+
if (IsELF && !Triple.isMIPS()) {
11601177
CmdArgs.push_back(Args.MakeArgString(Twine(PluginOptPrefix) + "-crel"));
11611178
} else {
11621179
D.Diag(diag::err_drv_unsupported_opt_for_target)
11631180
<< "-Wa,--crel" << D.getTargetTriple();
11641181
}
11651182
}
1183+
if (ImplicitMapSyms)
1184+
CmdArgs.push_back(
1185+
Args.MakeArgString(Twine(PluginOptPrefix) + "-implicit-mapsyms"));
11661186
}
11671187

11681188
void tools::addOpenMPRuntimeLibraryPath(const ToolChain &TC,

clang/test/Driver/mmapsyms.c

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/// Alternative mapping symbol scheme for AArch64.
2+
// RUN: %clang -### -c --target=aarch64 -Wa,-mmapsyms=implicit %s -Werror 2>&1 | FileCheck %s
3+
// RUN: %clang -### -c --target=aarch64_be -Wa,-mmapsyms=implicit %s -Werror 2>&1 | FileCheck %s
4+
// RUN: %clang -### -c --target=aarch64 -Wa,-mmapsyms=implicit,-mmapsyms=default %s -Werror 2>&1 | FileCheck %s --check-prefix=NO
5+
// RUN: not %clang -### -c --target=arm64-apple-darwin -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=ERR
6+
// RUN: not %clang -### -c --target=x86_64 -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=ERR2
7+
8+
// RUN: %clang -### -c --target=aarch64 -Werror -Wa,-mmapsyms=implicit -x assembler %s -Werror 2>&1 | FileCheck %s --check-prefix=ASM
9+
// RUN: not %clang -### -c --target=x86_64 -Wa,-mmapsyms=implicit -x assembler %s 2>&1 | FileCheck %s --check-prefix=ERR2
10+
11+
// CHECK: "-cc1" {{.*}}"-mmapsyms=implicit"
12+
// NO: "-cc1"
13+
// NO-NOT: "-mmapsyms=implicit"
14+
// ASM: "-cc1as" {{.*}}"-mmapsyms=implicit"
15+
// ERR: error: unsupported option '-Wa,-mmapsyms=' for target 'arm64-apple-darwin'
16+
// ERR2: error: unsupported argument '-mmapsyms=implicit' to option '-Wa,'
17+
18+
/// Check LTO.
19+
// RUN: %clang -### --target=aarch64-linux -Werror -flto -Wa,-mmapsyms=implicit %s 2>&1 | FileCheck %s --check-prefix=LTO
20+
// RUN: %clang -### --target=aarch64-linux -Werror -flto -Wa,-mmapsyms=implicit -Wa,-mmapsyms=default %s 2>&1 | FileCheck %s --check-prefix=LTO-NO
21+
22+
// LTO: "-plugin-opt=-implicit-mapsyms"
23+
// LTO-NO-NOT: "-plugin-opt=-implicit-mapsyms"
24+
25+
// RUN: touch %t.o
26+
// RUN: not %clang -### --target=x86_64-unknown-linux -flto -Wa,-mmapsyms=implicit %t.o 2>&1 | FileCheck %s --check-prefix=LTO-ERR
27+
28+
// LTO-ERR: error: unsupported option '-Wa,-mmapsyms=' for target 'x86_64-unknown-linux'

clang/test/Misc/cc1as-mmapsyms.c

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// REQUIRES: aarch64-registered-target
2+
// RUN: %clang -cc1as -triple aarch64 %s -filetype obj -mmapsyms=implicit -o %t.o
3+
// RUN: llvm-readelf -s %t.o | FileCheck %s
4+
5+
// CHECK: Symbol table '.symtab' contains 1 entries:
6+
nop
7+
8+
.data
9+
.quad 0

clang/tools/driver/cc1as_main.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,8 @@ struct AssemblerInvocation {
164164

165165
LLVM_PREFERRED_TYPE(bool)
166166
unsigned Crel : 1;
167+
LLVM_PREFERRED_TYPE(bool)
168+
unsigned ImplicitMapsyms : 1;
167169

168170
LLVM_PREFERRED_TYPE(bool)
169171
unsigned X86RelaxRelocations : 1;
@@ -211,6 +213,7 @@ struct AssemblerInvocation {
211213
EmitDwarfUnwind = EmitDwarfUnwindType::Default;
212214
EmitCompactUnwindNonCanonical = false;
213215
Crel = false;
216+
ImplicitMapsyms = 0;
214217
X86RelaxRelocations = 0;
215218
X86Sse2Avx = 0;
216219
}
@@ -382,6 +385,7 @@ bool AssemblerInvocation::CreateFromArgs(AssemblerInvocation &Opts,
382385
Opts.EmitCompactUnwindNonCanonical =
383386
Args.hasArg(OPT_femit_compact_unwind_non_canonical);
384387
Opts.Crel = Args.hasArg(OPT_crel);
388+
Opts.ImplicitMapsyms = Args.hasArg(OPT_mmapsyms_implicit);
385389
Opts.X86RelaxRelocations = !Args.hasArg(OPT_mrelax_relocations_no);
386390
Opts.X86Sse2Avx = Args.hasArg(OPT_msse2avx);
387391

@@ -442,6 +446,7 @@ static bool ExecuteAssemblerImpl(AssemblerInvocation &Opts,
442446
MCOptions.EmitCompactUnwindNonCanonical = Opts.EmitCompactUnwindNonCanonical;
443447
MCOptions.MCSaveTempLabels = Opts.SaveTemporaryLabels;
444448
MCOptions.Crel = Opts.Crel;
449+
MCOptions.ImplicitMapSyms = Opts.ImplicitMapsyms;
445450
MCOptions.X86RelaxRelocations = Opts.X86RelaxRelocations;
446451
MCOptions.X86Sse2Avx = Opts.X86Sse2Avx;
447452
MCOptions.CompressDebugSections = Opts.CompressDebugSections;

0 commit comments

Comments
 (0)