Move the abort message to libc_shared_globals
__libc_shared_globals() is available in dynamic modules as soon as
relocation has finished (i.e. after ifuncs run). Before ifuncs have run,
the android_set_abort_message() function already doesn't work because it
calls public APIs via the PLT. (If this matters, we can use a static
bool variable to enable android_set_abort_message after libc
initialization).
__libc_shared_globals() is hidden, so it's available in the linker
immediately (i.e. before relocation). TLS memory (e.g. errno) currently
isn't accessible until after relocation, but a later patch fixes that.
Bug: none
Test: bionic unit tests
Change-Id: Ied4433758ed2da9ee404c6158e319cf502d05a53
diff --git a/libc/bionic/android_set_abort_message.cpp b/libc/bionic/android_set_abort_message.cpp
index 58f24cf..56e6771 100644
--- a/libc/bionic/android_set_abort_message.cpp
+++ b/libc/bionic/android_set_abort_message.cpp
@@ -35,10 +35,9 @@
#include <sys/mman.h>
#include "private/bionic_defs.h"
+#include "private/bionic_globals.h"
#include "private/ScopedPthreadMutexLocker.h"
-static pthread_mutex_t g_abort_msg_lock = PTHREAD_MUTEX_INITIALIZER;
-
struct abort_msg_t {
size_t size;
char msg[0];
@@ -56,8 +55,6 @@
"The in-memory layout of magic_abort_msg_t is not consistent with what automated "
"tools expect.");
-abort_msg_t** __abort_message_ptr; // Accessible to __libc_init_common.
-
[[clang::optnone]]
static void fill_abort_message_magic(magic_abort_msg_t* new_magic_abort_message) {
// 128-bit magic for the abort message. Chosen by fair dice roll.
@@ -71,14 +68,9 @@
__BIONIC_WEAK_FOR_NATIVE_BRIDGE
void android_set_abort_message(const char* msg) {
- ScopedPthreadMutexLocker locker(&g_abort_msg_lock);
+ ScopedPthreadMutexLocker locker(&__libc_shared_globals()->abort_msg_lock);
- if (__abort_message_ptr == nullptr) {
- // We must have crashed _very_ early.
- return;
- }
-
- if (*__abort_message_ptr != nullptr) {
+ if (__libc_shared_globals()->abort_msg != nullptr) {
// We already have an abort message.
// Assume that the first crash is the one most worth reporting.
return;
@@ -94,5 +86,5 @@
fill_abort_message_magic(new_magic_abort_message);
new_magic_abort_message->msg.size = size;
strcpy(new_magic_abort_message->msg.msg, msg);
- *__abort_message_ptr = &new_magic_abort_message->msg;
+ __libc_shared_globals()->abort_msg = &new_magic_abort_message->msg;
}