From bf697b0293cc0272a33036fca0fb53e1bdcb0a93 Mon Sep 17 00:00:00 2001 From: Martin Nordholts Date: Thu, 23 Oct 2025 06:03:12 +0200 Subject: [PATCH] hwasan: Fix relocation errors by adjusting `NewGV` alias address calculation We don't want to apply `PointerTagShift` to the alias address of `NewGV` that replaces the `GV` we are instrumenting, because that makes the address huge and results in relocation errors such as: hwaddress.7rcbfp3g-cgu.0:(.text.main+0x7c): \ relocation truncated to fit: R_AARCH64_ADR_PREL_PG_HI21 against `.data.rel.ro..L.hwasan' unless optimizations happens get rid of the faulty calculation. --- llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp index 832592e7663b2..e12ca0404ee9c 100644 --- a/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp +++ b/llvm/lib/Transforms/Instrumentation/HWAddressSanitizer.cpp @@ -1763,9 +1763,7 @@ void HWAddressSanitizer::instrumentGlobal(GlobalVariable *GV, uint8_t Tag) { } Constant *Aliasee = ConstantExpr::getIntToPtr( - ConstantExpr::getAdd( - ConstantExpr::getPtrToInt(NewGV, Int64Ty), - ConstantInt::get(Int64Ty, uint64_t(Tag) << PointerTagShift)), + ConstantExpr::getPtrToInt(NewGV, Int64Ty), GV->getType()); auto *Alias = GlobalAlias::create(GV->getValueType(), GV->getAddressSpace(), GV->getLinkage(), "", Aliasee, &M);