Skip to content

[Driver] Add -Wa, options -mmapsyms={default,implicit} #104542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/include/clang/Basic/CodeGenOptions.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions clang/include/clang/Driver/Options.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx. Added help message for clang -cc1 and clang -cc1as

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">>;
Expand Down
1 change: 1 addition & 0 deletions clang/lib/CodeGen/BackendUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
12 changes: 12 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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)
Expand Down
24 changes: 22 additions & 2 deletions clang/lib/Driver/ToolChains/CommonArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down
28 changes: 28 additions & 0 deletions clang/test/Driver/mmapsyms.c
Original file line number Diff line number Diff line change
@@ -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'
9 changes: 9 additions & 0 deletions clang/test/Misc/cc1as-mmapsyms.c
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions clang/tools/driver/cc1as_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -211,6 +213,7 @@ struct AssemblerInvocation {
EmitDwarfUnwind = EmitDwarfUnwindType::Default;
EmitCompactUnwindNonCanonical = false;
Crel = false;
ImplicitMapsyms = 0;
X86RelaxRelocations = 0;
X86Sse2Avx = 0;
}
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
Expand Down
Loading