Skip to content

[lld] [MTE] Allow android note for static executables. #77078

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 1 commit into from
Jan 8, 2024
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
3 changes: 1 addition & 2 deletions lld/ELF/Arch/AArch64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1025,8 +1025,7 @@ addTaggedSymbolReferences(InputSectionBase &sec,
// symbols should also be built with tagging. But, to handle these cases, we
// demote the symbol to be untagged.
void lld::elf::createTaggedSymbols(const SmallVector<ELFFileBase *, 0> &files) {
assert(config->emachine == EM_AARCH64 &&
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE);
assert(hasMemtag());

// First, collect all symbols that are marked as tagged, and count how many
// times they're marked as tagged.
Expand Down
2 changes: 1 addition & 1 deletion lld/ELF/SyntheticSections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1450,7 +1450,7 @@ DynamicSection<ELFT>::computeContents() {
if (config->zPacPlt)
addInt(DT_AARCH64_PAC_PLT, 0);

if (config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE) {
if (hasMemtag()) {
addInt(DT_AARCH64_MEMTAG_MODE, config->androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
addInt(DT_AARCH64_MEMTAG_HEAP, config->androidMemtagHeap);
addInt(DT_AARCH64_MEMTAG_STACK, config->androidMemtagStack);
Expand Down
16 changes: 11 additions & 5 deletions lld/ELF/Writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -291,15 +291,19 @@ static void demoteSymbolsAndComputeIsPreemptible() {
}
}

bool elf::hasMemtag() {
return config->emachine == EM_AARCH64 &&
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
}

// Fully static executables don't support MTE globals at this point in time, as
// we currently rely on:
// - A dynamic loader to process relocations, and
// - Dynamic entries.
// This restriction could be removed in future by re-using some of the ideas
// that ifuncs use in fully static executables.
bool elf::canHaveMemtagGlobals() {
return config->emachine == EM_AARCH64 &&
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE &&
return hasMemtag() &&
(config->relocatable || config->shared || needsInterpSection());
}

Expand Down Expand Up @@ -397,11 +401,13 @@ template <class ELFT> void elf::createSyntheticSections() {
std::make_unique<SymbolTableSection<ELFT>>(*part.dynStrTab);
part.dynamic = std::make_unique<DynamicSection<ELFT>>();

if (canHaveMemtagGlobals()) {
if (hasMemtag()) {
part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>();
add(*part.memtagAndroidNote);
part.memtagDescriptors = std::make_unique<MemtagDescriptors>();
add(*part.memtagDescriptors);
if (canHaveMemtagGlobals()) {
part.memtagDescriptors = std::make_unique<MemtagDescriptors>();
Copy link
Contributor

Choose a reason for hiding this comment

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

driveby, potentially in another CL: can we rename MemtagDescriptors to MemtagGlobalDescriptors?

add(*part.memtagDescriptors);
}
}

if (config->androidPackDynRelocs)
Expand Down
1 change: 1 addition & 0 deletions lld/ELF/Writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ bool isMipsN32Abi(const InputFile *f);
bool isMicroMips();
bool isMipsR6();

bool hasMemtag();
bool canHaveMemtagGlobals();
} // namespace lld::elf

Expand Down
12 changes: 12 additions & 0 deletions lld/test/ELF/aarch64-memtag-android-abi.s
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@
# BAD-MODE: error: unknown --android-memtag-mode value: "asymm", should be one of
# BAD-MODE: {async, sync, none}

# RUN: ld.lld -static --android-memtag-mode=sync --android-memtag-heap \
# RUN: --android-memtag-stack %t.o -o %t
# RUN: llvm-readelf --memtag %t | FileCheck %s --check-prefixes=STATIC

# STATIC: Memtag Dynamic Entries:
# STATIC-NEXT: < none found >
# STATIC: Memtag Android Note:
# STATIC-NEXT: Tagging Mode: SYNC
# STATIC-NEXT: Heap: Enabled
# STATIC-NEXT: Stack: Enabled


.globl _start
_start:
ret