eb847bc866
The x86_64 build was failing because clone.S had a call to __thread_entry which was being added to a different intermediate .a on the way to making libc.so, and the linker couldn't guarantee statically that such a relocation would be possible. ld: error: out/target/product/generic_x86_64/obj/STATIC_LIBRARIES/libc_common_intermediates/libc_common.a(clone.o): requires dynamic R_X86_64_PC32 reloc against '__thread_entry' which may overflow at runtime; recompile with -fPIC This patch addresses that by ensuring that the caller and callee end up in the same intermediate .a. While I'm here, I've tried to clean up some of the mess that led to this situation too. In particular, this removes libc/private/ from the default include path (except for the DNS code), and splits out the DNS code into its own library (since it's a weird special case of upstream NetBSD code that's diverged so heavily it's unlikely ever to get back in sync). There's more cleanup of the DNS situation possible, but this is definitely a step in the right direction, and it's more than enough to get x86_64 building cleanly. Change-Id: I00425a7245b7a2573df16cc38798187d0729e7c4
182 lines
6 KiB
C++
182 lines
6 KiB
C++
/*
|
|
* Copyright (C) 2012 The Android Open Source Project
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
* * Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in
|
|
* the documentation and/or other materials provided with the
|
|
* distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*/
|
|
|
|
#include "debug_stacktrace.h"
|
|
|
|
#include <dlfcn.h>
|
|
#include <inttypes.h>
|
|
#include <unistd.h>
|
|
#include <unwind.h>
|
|
#include <sys/types.h>
|
|
|
|
#include "debug_mapinfo.h"
|
|
#include "private/libc_logging.h"
|
|
|
|
/* depends how the system includes define this */
|
|
#ifdef HAVE_UNWIND_CONTEXT_STRUCT
|
|
typedef struct _Unwind_Context __unwind_context;
|
|
#else
|
|
typedef _Unwind_Context __unwind_context;
|
|
#endif
|
|
|
|
static mapinfo_t* gMapInfo = NULL;
|
|
static void* gDemangler;
|
|
typedef char* (*DemanglerFn)(const char*, char*, size_t*, int*);
|
|
static DemanglerFn gDemanglerFn = NULL;
|
|
|
|
__LIBC_HIDDEN__ void backtrace_startup() {
|
|
gMapInfo = mapinfo_create(getpid());
|
|
gDemangler = dlopen("libgccdemangle.so", RTLD_NOW);
|
|
if (gDemangler != NULL) {
|
|
void* sym = dlsym(gDemangler, "__cxa_demangle");
|
|
gDemanglerFn = reinterpret_cast<DemanglerFn>(sym);
|
|
}
|
|
}
|
|
|
|
__LIBC_HIDDEN__ void backtrace_shutdown() {
|
|
mapinfo_destroy(gMapInfo);
|
|
dlclose(gDemangler);
|
|
}
|
|
|
|
static char* demangle(const char* symbol) {
|
|
if (gDemanglerFn == NULL) {
|
|
return NULL;
|
|
}
|
|
return (*gDemanglerFn)(symbol, NULL, NULL, NULL);
|
|
}
|
|
|
|
struct stack_crawl_state_t {
|
|
uintptr_t* frames;
|
|
size_t frame_count;
|
|
size_t max_depth;
|
|
bool have_skipped_self;
|
|
|
|
stack_crawl_state_t(uintptr_t* frames, size_t max_depth)
|
|
: frames(frames), frame_count(0), max_depth(max_depth), have_skipped_self(false) {
|
|
}
|
|
};
|
|
|
|
#if defined(__arm__) && !defined(_Unwind_GetIP)
|
|
// Older versions of Clang don't provide a definition of _Unwind_GetIP(), so
|
|
// we include an appropriate version of our own. Once we have updated to
|
|
// Clang 3.4, this code can be removed.
|
|
static __inline__
|
|
uintptr_t _Unwind_GetIP(struct _Unwind_Context *__context) {
|
|
uintptr_t __ip = 0;
|
|
_Unwind_VRS_Get(__context, _UVRSC_CORE, 15, _UVRSD_UINT32, &__ip);
|
|
return __ip & ~0x1;
|
|
}
|
|
#endif
|
|
|
|
static _Unwind_Reason_Code trace_function(__unwind_context* context, void* arg) {
|
|
stack_crawl_state_t* state = static_cast<stack_crawl_state_t*>(arg);
|
|
|
|
uintptr_t ip = _Unwind_GetIP(context);
|
|
|
|
// The first stack frame is get_backtrace itself. Skip it.
|
|
if (ip != 0 && !state->have_skipped_self) {
|
|
state->have_skipped_self = true;
|
|
return _URC_NO_REASON;
|
|
}
|
|
|
|
#ifdef __arm__
|
|
/*
|
|
* The instruction pointer is pointing at the instruction after the bl(x), and
|
|
* the _Unwind_Backtrace routine already masks the Thumb mode indicator (LSB
|
|
* in PC). So we need to do a quick check here to find out if the previous
|
|
* instruction is a Thumb-mode BLX(2). If so subtract 2 otherwise 4 from PC.
|
|
*/
|
|
if (ip != 0) {
|
|
short* ptr = reinterpret_cast<short*>(ip);
|
|
// Thumb BLX(2)
|
|
if ((*(ptr-1) & 0xff80) == 0x4780) {
|
|
ip -= 2;
|
|
} else {
|
|
ip -= 4;
|
|
}
|
|
}
|
|
#endif
|
|
|
|
state->frames[state->frame_count++] = ip;
|
|
return (state->frame_count >= state->max_depth) ? _URC_END_OF_STACK : _URC_NO_REASON;
|
|
}
|
|
|
|
__LIBC_HIDDEN__ int get_backtrace(uintptr_t* frames, size_t max_depth) {
|
|
stack_crawl_state_t state(frames, max_depth);
|
|
_Unwind_Backtrace(trace_function, &state);
|
|
return state.frame_count;
|
|
}
|
|
|
|
__LIBC_HIDDEN__ void log_backtrace(uintptr_t* frames, size_t frame_count) {
|
|
uintptr_t self_bt[16];
|
|
if (frames == NULL) {
|
|
frame_count = get_backtrace(self_bt, 16);
|
|
frames = self_bt;
|
|
}
|
|
|
|
__libc_format_log(ANDROID_LOG_ERROR, "libc",
|
|
"*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***\n");
|
|
|
|
for (size_t i = 0 ; i < frame_count; ++i) {
|
|
uintptr_t offset = 0;
|
|
const char* symbol = NULL;
|
|
|
|
Dl_info info;
|
|
if (dladdr((void*) frames[i], &info) != 0) {
|
|
offset = reinterpret_cast<uintptr_t>(info.dli_saddr);
|
|
symbol = info.dli_sname;
|
|
}
|
|
|
|
uintptr_t rel_pc;
|
|
const mapinfo_t* mi = (gMapInfo != NULL) ? mapinfo_find(gMapInfo, frames[i], &rel_pc) : NULL;
|
|
const char* soname = (mi != NULL) ? mi->name : info.dli_fname;
|
|
if (soname == NULL) {
|
|
soname = "<unknown>";
|
|
}
|
|
if (symbol != NULL) {
|
|
// TODO: we might need a flag to say whether it's safe to allocate (demangling allocates).
|
|
char* demangled_symbol = demangle(symbol);
|
|
const char* best_name = (demangled_symbol != NULL) ? demangled_symbol : symbol;
|
|
|
|
__libc_format_log(ANDROID_LOG_ERROR, "libc",
|
|
" #%02zd pc %0*" PRIxPTR " %s (%s+%" PRIuPTR ")",
|
|
i,
|
|
static_cast<int>(2 * sizeof(void*)), rel_pc,
|
|
soname,
|
|
best_name, frames[i] - offset);
|
|
|
|
free(demangled_symbol);
|
|
} else {
|
|
__libc_format_log(ANDROID_LOG_ERROR, "libc",
|
|
" #%02zd pc %0*" PRIxPTR " %s",
|
|
i,
|
|
static_cast<int>(2 * sizeof(void*)), rel_pc,
|
|
soname);
|
|
}
|
|
}
|
|
}
|