Skip to content

Commit cf5df40

Browse files
committed
Revert "[AddressSanitizer] Don't use weak linkage for __{start,stop}_asan_globals"
This reverts commit d76e62f. Reverting since this can lead to linker errors: ``` ld.lld: error: undefined hidden symbol: __start_asan_globals ``` when using --gc-sections. The linker can discard __start_asan_globals once there are no more `asan_globals` sections left, which can lead to this error if we have external linkages to them.
1 parent cf2274b commit cf5df40

File tree

2 files changed

+11
-31
lines changed

2 files changed

+11
-31
lines changed

llvm/lib/Transforms/Instrumentation/AddressSanitizer.cpp

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,23 +2103,10 @@ void ModuleAddressSanitizer::InstrumentGlobalsELF(
21032103
SetComdatForGlobalMetadata(G, Metadata, UniqueModuleId);
21042104
}
21052105

2106-
// This should never be called when there are no globals, by the logic that
2107-
// computes the UniqueModuleId string, which is "" when there are no globals.
2108-
// It's important that this path is only used when there are actually some
2109-
// globals, because that means that there will certainly be a live
2110-
// `asan_globals` input section at link time and thus `__start_asan_globals`
2111-
// and `__stop_asan_globals` symbols will definitely be defined at link time.
2112-
// This means there's no need for the references to them to be weak, which
2113-
// enables better code generation because ExternalWeakLinkage implies
2114-
// isInterposable() and thus requires GOT indirection for PIC. Since these
2115-
// are known-defined hidden/dso_local symbols, direct PIC accesses without
2116-
// dynamic relocation are always sufficient.
2117-
assert(!MetadataGlobals.empty());
2118-
assert(!UniqueModuleId.empty());
2119-
21202106
// Update llvm.compiler.used, adding the new metadata globals. This is
21212107
// needed so that during LTO these variables stay alive.
2122-
appendToCompilerUsed(M, MetadataGlobals);
2108+
if (!MetadataGlobals.empty())
2109+
appendToCompilerUsed(M, MetadataGlobals);
21232110

21242111
// RegisteredFlag serves two purposes. First, we can pass it to dladdr()
21252112
// to look up the loaded image that contains it. Second, we can store in it
@@ -2132,18 +2119,15 @@ void ModuleAddressSanitizer::InstrumentGlobalsELF(
21322119
ConstantInt::get(IntptrTy, 0), kAsanGlobalsRegisteredFlagName);
21332120
RegisteredFlag->setVisibility(GlobalVariable::HiddenVisibility);
21342121

2135-
// Create start and stop symbols. These are known to be defined by
2136-
// the linker, see comment above.
2137-
auto MakeStartStopGV = [&](const char *Prefix) {
2138-
GlobalVariable *StartStop =
2139-
new GlobalVariable(M, IntptrTy, false, GlobalVariable::ExternalLinkage,
2140-
nullptr, Prefix + getGlobalMetadataSection());
2141-
StartStop->setVisibility(GlobalVariable::HiddenVisibility);
2142-
assert(StartStop->isImplicitDSOLocal());
2143-
return StartStop;
2144-
};
2145-
GlobalVariable *StartELFMetadata = MakeStartStopGV("__start_");
2146-
GlobalVariable *StopELFMetadata = MakeStartStopGV("__stop_");
2122+
// Create start and stop symbols.
2123+
GlobalVariable *StartELFMetadata = new GlobalVariable(
2124+
M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2125+
"__start_" + getGlobalMetadataSection());
2126+
StartELFMetadata->setVisibility(GlobalVariable::HiddenVisibility);
2127+
GlobalVariable *StopELFMetadata = new GlobalVariable(
2128+
M, IntptrTy, false, GlobalVariable::ExternalWeakLinkage, nullptr,
2129+
"__stop_" + getGlobalMetadataSection());
2130+
StopELFMetadata->setVisibility(GlobalVariable::HiddenVisibility);
21472131

21482132
// Create a call to register the globals with the runtime.
21492133
IRB.CreateCall(AsanRegisterElfGlobals,

llvm/test/Instrumentation/AddressSanitizer/global_metadata.ll

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@ target triple = "x86_64-unknown-linux-gnu"
2828
; during LTO.
2929
; CHECK: @llvm.compiler.used {{.*}} @__asan_global_global {{.*}} section "llvm.metadata"
3030

31-
; Check that start and stop symbols will be accessed as dso_local.
32-
; CHECK: @__start_asan_globals = external hidden global i64
33-
; CHECK: @__stop_asan_globals = external hidden global i64
34-
3531
; Check that location descriptors and global names were passed into __asan_register_globals:
3632
; CHECK: call void @__asan_register_elf_globals(i64 ptrtoint (i64* @___asan_globals_registered to i64), i64 ptrtoint (i64* @__start_asan_globals to i64), i64 ptrtoint (i64* @__stop_asan_globals to i64))
3733

0 commit comments

Comments
 (0)