2004-08-17 17:34:28 +02:00
|
|
|
/* libunwind - a platform-independent unwind library
|
|
|
|
Copyright (C) 2002 Hewlett-Packard Co
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
Copyright (C) 2007 David Mosberger-Tang
|
|
|
|
Contributed by David Mosberger-Tang <dmosberger@gmail.com>
|
2004-08-17 17:34:28 +02:00
|
|
|
|
|
|
|
This file is part of libunwind.
|
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be
|
|
|
|
included in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
|
2010-04-20 16:45:18 +02:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
|
|
|
|
2004-08-17 17:34:28 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "unwind_i.h"
|
|
|
|
|
|
|
|
#ifdef UNW_REMOTE_ONLY
|
|
|
|
|
|
|
|
/* unw_local_addr_space is a NULL pointer in this case. */
|
|
|
|
PROTECTED unw_addr_space_t unw_local_addr_space;
|
|
|
|
|
|
|
|
#else /* !UNW_REMOTE_ONLY */
|
|
|
|
|
|
|
|
static struct unw_addr_space local_addr_space;
|
|
|
|
|
|
|
|
PROTECTED unw_addr_space_t unw_local_addr_space = &local_addr_space;
|
|
|
|
|
|
|
|
# ifdef UNW_LOCAL_ONLY
|
|
|
|
|
|
|
|
HIDDEN void *
|
|
|
|
tdep_uc_addr (ucontext_t *uc, int reg)
|
|
|
|
{
|
2010-04-05 21:42:23 +02:00
|
|
|
return x86_r_uc_addr (uc, reg);
|
2004-08-17 17:34:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# endif /* UNW_LOCAL_ONLY */
|
|
|
|
|
|
|
|
HIDDEN unw_dyn_info_list_t _U_dyn_info_list;
|
|
|
|
|
|
|
|
/* XXX fix me: there is currently no way to locate the dyn-info list
|
|
|
|
by a remote unwinder. On ia64, this is done via a special
|
|
|
|
unwind-table entry. Perhaps something similar can be done with
|
|
|
|
DWARF2 unwind info. */
|
|
|
|
|
|
|
|
static void
|
|
|
|
put_unwind_info (unw_addr_space_t as, unw_proc_info_t *proc_info, void *arg)
|
|
|
|
{
|
|
|
|
/* it's a no-op */
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_dyn_info_list_addr (unw_addr_space_t as, unw_word_t *dyn_info_list_addr,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
*dyn_info_list_addr = (unw_word_t) &_U_dyn_info_list;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-03-16 19:06:26 +01:00
|
|
|
#define PAGE_SIZE 4096
|
|
|
|
#define PAGE_START(a) ((a) & ~(PAGE_SIZE-1))
|
|
|
|
|
|
|
|
/* Cache of already validated addresses */
|
|
|
|
#define NLGA 4
|
|
|
|
static unw_word_t last_good_addr[NLGA];
|
|
|
|
static int lga_victim;
|
|
|
|
|
|
|
|
static int
|
|
|
|
validate_mem (unw_word_t addr)
|
|
|
|
{
|
|
|
|
int i, victim;
|
2010-04-20 16:45:18 +02:00
|
|
|
#ifdef HAVE_MINCORE
|
2010-04-21 14:18:02 +02:00
|
|
|
char mvec[2]; /* Unaligned access may cross page boundary */
|
2010-04-20 16:45:18 +02:00
|
|
|
#endif
|
2009-03-16 19:06:26 +01:00
|
|
|
|
|
|
|
addr = PAGE_START(addr);
|
|
|
|
|
2009-12-01 22:59:45 +01:00
|
|
|
if (addr == 0)
|
|
|
|
return -1;
|
|
|
|
|
2009-03-16 19:06:26 +01:00
|
|
|
for (i = 0; i < NLGA; i++)
|
|
|
|
{
|
|
|
|
if (last_good_addr[i] && (addr == last_good_addr[i]))
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-04-20 16:45:18 +02:00
|
|
|
#ifdef HAVE_MINCORE
|
2010-04-21 14:33:37 +02:00
|
|
|
if (mincore ((void *) addr, sizeof (unw_word_t), mvec) == -1)
|
2010-04-20 16:45:18 +02:00
|
|
|
#else
|
2010-04-21 14:33:37 +02:00
|
|
|
if (msync ((void *) addr, sizeof (unw_word_t), MS_ASYNC) == -1)
|
2010-04-20 16:45:18 +02:00
|
|
|
#endif
|
2009-03-16 19:06:26 +01:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
victim = lga_victim;
|
|
|
|
for (i = 0; i < NLGA; i++) {
|
|
|
|
if (!last_good_addr[victim]) {
|
|
|
|
last_good_addr[victim++] = addr;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
victim = (victim + 1) % NLGA;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* All slots full. Evict the victim. */
|
|
|
|
last_good_addr[victim] = addr;
|
|
|
|
victim = (victim + 1) % NLGA;
|
|
|
|
lga_victim = victim;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2004-08-17 17:34:28 +02:00
|
|
|
static int
|
|
|
|
access_mem (unw_addr_space_t as, unw_word_t addr, unw_word_t *val, int write,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
if (write)
|
|
|
|
{
|
|
|
|
Debug (16, "mem[%x] <- %x\n", addr, *val);
|
|
|
|
*(unw_word_t *) addr = *val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-03-16 19:06:26 +01:00
|
|
|
/* validate address */
|
|
|
|
const struct cursor *c = (const struct cursor *)arg;
|
|
|
|
if (c && c->validate && validate_mem(addr))
|
|
|
|
return -1;
|
2004-08-17 17:34:28 +02:00
|
|
|
*val = *(unw_word_t *) addr;
|
|
|
|
Debug (16, "mem[%x] -> %x\n", addr, *val);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
access_reg (unw_addr_space_t as, unw_regnum_t reg, unw_word_t *val, int write,
|
|
|
|
void *arg)
|
|
|
|
{
|
|
|
|
unw_word_t *addr;
|
2009-03-16 19:06:26 +01:00
|
|
|
ucontext_t *uc = ((struct cursor *)arg)->uc;
|
2004-08-17 17:34:28 +02:00
|
|
|
|
|
|
|
if (unw_is_fpreg (reg))
|
|
|
|
goto badreg;
|
|
|
|
|
2010-04-05 21:42:23 +02:00
|
|
|
if (!(addr = x86_r_uc_addr (uc, reg)))
|
2004-08-17 17:34:28 +02:00
|
|
|
goto badreg;
|
|
|
|
|
|
|
|
if (write)
|
|
|
|
{
|
|
|
|
*(unw_word_t *) addr = *val;
|
|
|
|
Debug (12, "%s <- %x\n", unw_regname (reg), *val);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*val = *(unw_word_t *) addr;
|
|
|
|
Debug (12, "%s -> %x\n", unw_regname (reg), *val);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
badreg:
|
|
|
|
Debug (1, "bad register number %u\n", reg);
|
|
|
|
return -UNW_EBADREG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
access_fpreg (unw_addr_space_t as, unw_regnum_t reg, unw_fpreg_t *val,
|
|
|
|
int write, void *arg)
|
|
|
|
{
|
2009-03-16 19:06:26 +01:00
|
|
|
ucontext_t *uc = ((struct cursor *)arg)->uc;
|
2004-08-17 17:34:28 +02:00
|
|
|
unw_fpreg_t *addr;
|
|
|
|
|
|
|
|
if (!unw_is_fpreg (reg))
|
|
|
|
goto badreg;
|
|
|
|
|
2010-04-05 21:42:23 +02:00
|
|
|
if (!(addr = x86_r_uc_addr (uc, reg)))
|
2004-08-17 17:34:28 +02:00
|
|
|
goto badreg;
|
|
|
|
|
|
|
|
if (write)
|
|
|
|
{
|
|
|
|
Debug (12, "%s <- %08lx.%08lx.%08lx\n", unw_regname (reg),
|
|
|
|
((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
|
|
|
|
*(unw_fpreg_t *) addr = *val;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*val = *(unw_fpreg_t *) addr;
|
|
|
|
Debug (12, "%s -> %08lx.%08lx.%08lx\n", unw_regname (reg),
|
|
|
|
((long *)val)[0], ((long *)val)[1], ((long *)val)[2]);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
badreg:
|
|
|
|
Debug (1, "bad register number %u\n", reg);
|
|
|
|
/* attempt to access a non-preserved register */
|
|
|
|
return -UNW_EBADREG;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_static_proc_name (unw_addr_space_t as, unw_word_t ip,
|
|
|
|
char *buf, size_t buf_len, unw_word_t *offp,
|
|
|
|
void *arg)
|
|
|
|
{
|
Introduce a tdep_get_func_addr_hook() in the ELF lookup_symbol()
routine and add address-space argument. This is needed because on
PPC64, a the function-name symbol refers to a function descriptor
(unlike, for example, on ia64, where the @fptr() operator is needed to
refer to a function descriptor). Thus, in order to look up the name
of a function, we need to dereference the function descriptor. To
make matters more "interesting", the function descriptors are normally
resolved by the dynamic linker, so we can't get their values from the
ELF file. Instead, we have to read them from the running image, hence
the need for the address-space argument.
2007-08-22 21:02:09 +02:00
|
|
|
return _Uelf32_get_proc_name (as, getpid (), ip, buf, buf_len, offp);
|
2004-08-17 17:34:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
HIDDEN void
|
|
|
|
x86_local_addr_space_init (void)
|
|
|
|
{
|
|
|
|
memset (&local_addr_space, 0, sizeof (local_addr_space));
|
|
|
|
local_addr_space.caching_policy = UNW_CACHE_GLOBAL;
|
|
|
|
local_addr_space.acc.find_proc_info = dwarf_find_proc_info;
|
|
|
|
local_addr_space.acc.put_unwind_info = put_unwind_info;
|
|
|
|
local_addr_space.acc.get_dyn_info_list_addr = get_dyn_info_list_addr;
|
|
|
|
local_addr_space.acc.access_mem = access_mem;
|
|
|
|
local_addr_space.acc.access_reg = access_reg;
|
|
|
|
local_addr_space.acc.access_fpreg = access_fpreg;
|
|
|
|
local_addr_space.acc.resume = x86_local_resume;
|
|
|
|
local_addr_space.acc.get_proc_name = get_static_proc_name;
|
|
|
|
unw_flush_cache (&local_addr_space, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* !UNW_REMOTE_ONLY */
|