Skip to content

Commit a831a21

Browse files
authored
[lld] [MTE] Allow android note for static executables. (#77078)
Florian pointed out that we're accidentally eliding the Android note for static executables, as it's guarded behind the "can have memtag globals" conditional. Of course, memtag globals are unsupported for static executables, but we should still allow static binaries to produce the Android note (as that's the only way they get MTE).
1 parent 27f5479 commit a831a21

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

lld/ELF/Arch/AArch64.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,8 +1025,7 @@ addTaggedSymbolReferences(InputSectionBase &sec,
10251025
// symbols should also be built with tagging. But, to handle these cases, we
10261026
// demote the symbol to be untagged.
10271027
void lld::elf::createTaggedSymbols(const SmallVector<ELFFileBase *, 0> &files) {
1028-
assert(config->emachine == EM_AARCH64 &&
1029-
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE);
1028+
assert(hasMemtag());
10301029

10311030
// First, collect all symbols that are marked as tagged, and count how many
10321031
// times they're marked as tagged.

lld/ELF/SyntheticSections.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ DynamicSection<ELFT>::computeContents() {
14501450
if (config->zPacPlt)
14511451
addInt(DT_AARCH64_PAC_PLT, 0);
14521452

1453-
if (config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE) {
1453+
if (hasMemtag()) {
14541454
addInt(DT_AARCH64_MEMTAG_MODE, config->androidMemtagMode == NT_MEMTAG_LEVEL_ASYNC);
14551455
addInt(DT_AARCH64_MEMTAG_HEAP, config->androidMemtagHeap);
14561456
addInt(DT_AARCH64_MEMTAG_STACK, config->androidMemtagStack);

lld/ELF/Writer.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,19 @@ static void demoteSymbolsAndComputeIsPreemptible() {
291291
}
292292
}
293293

294+
bool elf::hasMemtag() {
295+
return config->emachine == EM_AARCH64 &&
296+
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE;
297+
}
298+
294299
// Fully static executables don't support MTE globals at this point in time, as
295300
// we currently rely on:
296301
// - A dynamic loader to process relocations, and
297302
// - Dynamic entries.
298303
// This restriction could be removed in future by re-using some of the ideas
299304
// that ifuncs use in fully static executables.
300305
bool elf::canHaveMemtagGlobals() {
301-
return config->emachine == EM_AARCH64 &&
302-
config->androidMemtagMode != ELF::NT_MEMTAG_LEVEL_NONE &&
306+
return hasMemtag() &&
303307
(config->relocatable || config->shared || needsInterpSection());
304308
}
305309

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

400-
if (canHaveMemtagGlobals()) {
404+
if (hasMemtag()) {
401405
part.memtagAndroidNote = std::make_unique<MemtagAndroidNote>();
402406
add(*part.memtagAndroidNote);
403-
part.memtagDescriptors = std::make_unique<MemtagDescriptors>();
404-
add(*part.memtagDescriptors);
407+
if (canHaveMemtagGlobals()) {
408+
part.memtagDescriptors = std::make_unique<MemtagDescriptors>();
409+
add(*part.memtagDescriptors);
410+
}
405411
}
406412

407413
if (config->androidPackDynRelocs)

lld/ELF/Writer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ bool isMipsN32Abi(const InputFile *f);
5757
bool isMicroMips();
5858
bool isMipsR6();
5959

60+
bool hasMemtag();
6061
bool canHaveMemtagGlobals();
6162
} // namespace lld::elf
6263

lld/test/ELF/aarch64-memtag-android-abi.s

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,18 @@
5656
# BAD-MODE: error: unknown --android-memtag-mode value: "asymm", should be one of
5757
# BAD-MODE: {async, sync, none}
5858

59+
# RUN: ld.lld -static --android-memtag-mode=sync --android-memtag-heap \
60+
# RUN: --android-memtag-stack %t.o -o %t
61+
# RUN: llvm-readelf --memtag %t | FileCheck %s --check-prefixes=STATIC
62+
63+
# STATIC: Memtag Dynamic Entries:
64+
# STATIC-NEXT: < none found >
65+
# STATIC: Memtag Android Note:
66+
# STATIC-NEXT: Tagging Mode: SYNC
67+
# STATIC-NEXT: Heap: Enabled
68+
# STATIC-NEXT: Stack: Enabled
69+
70+
5971
.globl _start
6072
_start:
6173
ret

0 commit comments

Comments
 (0)