#include <errno.h>
#include <inttypes.h>
#include <stdbool.h>
#include <stdlib.h>
#include "debug.h"
#include "dwarf-aux.h"
#include "dwarf-regs.h"
#include "strbuf.h"
#include "string2.h"
const char *cu_find_realpath(Dwarf_Die *cu_die, const char *fname)
{
Dwarf_Files *files;
size_t nfiles, i;
const char *src = NULL;
int ret;
if (!fname)
return NULL;
ret = dwarf_getsrcfiles(cu_die, &files, &nfiles);
if (ret != 0)
return NULL;
for (i = 0; i < nfiles; i++) {
src = dwarf_filesrc(files, i, NULL, NULL);
if (strtailcmp(src, fname) == 0)
break;
}
if (i == nfiles)
return NULL;
return src;
}
const char *cu_get_comp_dir(Dwarf_Die *cu_die)
{
Dwarf_Attribute attr;
if (dwarf_attr(cu_die, DW_AT_comp_dir, &attr) == NULL)
return NULL;
return dwarf_formstring(&attr);
}
static Dwarf_Line *cu_getsrc_die(Dwarf_Die *cu_die, Dwarf_Addr addr)
{
Dwarf_Addr laddr;
Dwarf_Lines *lines;
Dwarf_Line *line;
size_t nlines, l, u, n;
bool flag;
if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0 ||
nlines == 0)
return NULL;
l = 0; u = nlines - 1;
while (l < u) {
n = u - (u - l) / 2;
line = dwarf_onesrcline(lines, n);
if (!line || dwarf_lineaddr(line, &laddr) != 0)
return NULL;
if (addr < laddr)
u = n - 1;
else
l = n;
}
do {
line = dwarf_onesrcline(lines, --l);
if (!line || dwarf_lineaddr(line, &laddr) != 0)
return NULL;
} while (laddr == addr);
l++;
do {
line = dwarf_onesrcline(lines, l++);
if (!line || dwarf_lineaddr(line, &laddr) != 0 ||
dwarf_linebeginstatement(line, &flag) != 0)
return NULL;
if (laddr > addr)
return NULL;
} while (!flag);
return line;
}
int cu_find_lineinfo(Dwarf_Die *cu_die, Dwarf_Addr addr,
const char **fname, int *lineno)
{
Dwarf_Line *line;
Dwarf_Die die_mem;
Dwarf_Addr faddr;
if (die_find_realfunc(cu_die, addr, &die_mem)
&& die_entrypc(&die_mem, &faddr) == 0 &&
faddr == addr) {
*fname = die_get_decl_file(&die_mem);
dwarf_decl_line(&die_mem, lineno);
goto out;
}
line = cu_getsrc_die(cu_die, addr);
if (line && dwarf_lineno(line, lineno) == 0) {
*fname = dwarf_linesrc(line, NULL, NULL);
if (!*fname)
*lineno = 0;
}
out:
return (*lineno && *fname) ? *lineno : -ENOENT;
}
static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data);
int cu_walk_functions_at(Dwarf_Die *cu_die, Dwarf_Addr addr,
int (*callback)(Dwarf_Die *, void *), void *data)
{
Dwarf_Die die_mem;
Dwarf_Die *sc_die;
int ret = -ENOENT;
for (sc_die = die_find_realfunc(cu_die, addr, &die_mem);
sc_die != NULL;
sc_die = die_find_child(sc_die, __die_find_inline_cb, &addr,
&die_mem)) {
ret = callback(sc_die, data);
if (ret)
break;
}
return ret;
}
const char *die_get_linkage_name(Dwarf_Die *dw_die)
{
Dwarf_Attribute attr;
if (dwarf_attr_integrate(dw_die, DW_AT_linkage_name, &attr) == NULL)
return NULL;
return dwarf_formstring(&attr);
}
bool die_compare_name(Dwarf_Die *dw_die, const char *tname)
{
const char *name;
name = dwarf_diename(dw_die);
return name ? (strcmp(tname, name) == 0) : false;
}
bool die_match_name(Dwarf_Die *dw_die, const char *glob)
{
const char *name;
name = dwarf_diename(dw_die);
if (name && strglobmatch(name, glob))
return true;
name = die_get_linkage_name(dw_die);
if (name && strglobmatch(name, glob))
return true;
return false;
}
int die_get_call_lineno(Dwarf_Die *in_die)
{
Dwarf_Attribute attr;
Dwarf_Word ret;
if (!dwarf_attr(in_die, DW_AT_call_line, &attr))
return -ENOENT;
dwarf_formudata(&attr, &ret);
return (int)ret;
}
Dwarf_Die *die_get_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
{
Dwarf_Attribute attr;
if (dwarf_attr_integrate(vr_die, DW_AT_type, &attr) &&
dwarf_formref_die(&attr, die_mem))
return die_mem;
else
return NULL;
}
Dwarf_Die *__die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
{
int tag;
do {
vr_die = die_get_type(vr_die, die_mem);
if (!vr_die)
break;
tag = dwarf_tag(vr_die);
} while (tag == DW_TAG_const_type ||
tag == DW_TAG_restrict_type ||
tag == DW_TAG_volatile_type ||
tag == DW_TAG_shared_type);
return vr_die;
}
Dwarf_Die *die_get_real_type(Dwarf_Die *vr_die, Dwarf_Die *die_mem)
{
do {
vr_die = __die_get_real_type(vr_die, die_mem);
} while (vr_die && dwarf_tag(vr_die) == DW_TAG_typedef);
return vr_die;
}
static int die_get_attr_udata(Dwarf_Die *tp_die, unsigned int attr_name,
Dwarf_Word *result)
{
Dwarf_Attribute attr;
if (dwarf_attr_integrate(tp_die, attr_name, &attr) == NULL ||
dwarf_formudata(&attr, result) != 0)
return -ENOENT;
return 0;
}
bool die_is_signed_type(Dwarf_Die *tp_die)
{
Dwarf_Word ret;
if (die_get_attr_udata(tp_die, DW_AT_encoding, &ret))
return false;
return (ret == DW_ATE_signed_char || ret == DW_ATE_signed ||
ret == DW_ATE_signed_fixed);
}
bool die_is_func_def(Dwarf_Die *dw_die)
{
Dwarf_Attribute attr;
Dwarf_Addr addr = 0;
if (dwarf_tag(dw_die) != DW_TAG_subprogram)
return false;
if (dwarf_attr(dw_die, DW_AT_declaration, &attr))
return false;
if (!dwarf_attr(dw_die, DW_AT_inline, &attr) &&
die_entrypc(dw_die, &addr) < 0)
return false;
return true;
}
int die_entrypc(Dwarf_Die *dw_die, Dwarf_Addr *addr)
{
Dwarf_Addr base, end;
Dwarf_Attribute attr;
if (!addr)
return -EINVAL;
if (dwarf_entrypc(dw_die, addr) == 0)
return 0;
if (!dwarf_attr(dw_die, DW_AT_ranges, &attr))
return -ENOENT;
return dwarf_ranges(dw_die, 0, &base, addr, &end) < 0 ? -ENOENT : 0;
}
bool die_is_func_instance(Dwarf_Die *dw_die)
{
Dwarf_Addr tmp;
Dwarf_Attribute attr_mem;
int tag = dwarf_tag(dw_die);
if (tag != DW_TAG_subprogram &&
tag != DW_TAG_inlined_subroutine)
return false;
return dwarf_entrypc(dw_die, &tmp) == 0 ||
dwarf_attr(dw_die, DW_AT_ranges, &attr_mem) != NULL;
}
int die_get_data_member_location(Dwarf_Die *mb_die, Dwarf_Word *offs)
{
Dwarf_Attribute attr;
Dwarf_Op *expr;
size_t nexpr;
int ret;
if (dwarf_attr(mb_die, DW_AT_data_member_location, &attr) == NULL)
return -ENOENT;
if (dwarf_formudata(&attr, offs) != 0) {
ret = dwarf_getlocation(&attr, &expr, &nexpr);
if (ret < 0 || nexpr == 0)
return -ENOENT;
if (expr[0].atom != DW_OP_plus_uconst || nexpr != 1) {
pr_debug("Unable to get offset:Unexpected OP %x (%zd)\n",
expr[0].atom, nexpr);
return -ENOTSUP;
}
*offs = (Dwarf_Word)expr[0].number;
}
return 0;
}
static int die_get_call_fileno(Dwarf_Die *in_die)
{
Dwarf_Word idx;
if (die_get_attr_udata(in_die, DW_AT_call_file, &idx) == 0)
return (int)idx;
else
return -ENOENT;
}
static int die_get_decl_fileno(Dwarf_Die *pdie)
{
Dwarf_Word idx;
if (die_get_attr_udata(pdie, DW_AT_decl_file, &idx) == 0)
return (int)idx;
else
return -ENOENT;
}
static const char *die_get_file_name(Dwarf_Die *dw_die, int idx)
{
Dwarf_Die cu_die;
Dwarf_Files *files;
Dwarf_Attribute attr_mem;
if (idx < 0 || !dwarf_attr_integrate(dw_die, DW_AT_decl_file, &attr_mem) ||
!dwarf_cu_die(attr_mem.cu, &cu_die, NULL, NULL, NULL, NULL, NULL, NULL) ||
dwarf_getsrcfiles(&cu_die, &files, NULL) != 0)
return NULL;
return dwarf_filesrc(files, idx, NULL, NULL);
}
const char *die_get_call_file(Dwarf_Die *in_die)
{
return die_get_file_name(in_die, die_get_call_fileno(in_die));
}
const char *die_get_decl_file(Dwarf_Die *dw_die)
{
return die_get_file_name(dw_die, die_get_decl_fileno(dw_die));
}
Dwarf_Die *die_find_child(Dwarf_Die *rt_die,
int (*callback)(Dwarf_Die *, void *),
void *data, Dwarf_Die *die_mem)
{
Dwarf_Die child_die;
int ret;
ret = dwarf_child(rt_die, die_mem);
if (ret != 0)
return NULL;
do {
ret = callback(die_mem, data);
if (ret == DIE_FIND_CB_END)
return die_mem;
if ((ret & DIE_FIND_CB_CHILD) &&
die_find_child(die_mem, callback, data, &child_die)) {
memcpy(die_mem, &child_die, sizeof(Dwarf_Die));
return die_mem;
}
} while ((ret & DIE_FIND_CB_SIBLING) &&
dwarf_siblingof(die_mem, die_mem) == 0);
return NULL;
}
struct __addr_die_search_param {
Dwarf_Addr addr;
Dwarf_Die *die_mem;
};
static int __die_search_func_tail_cb(Dwarf_Die *fn_die, void *data)
{
struct __addr_die_search_param *ad = data;
Dwarf_Addr addr = 0;
if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
!dwarf_highpc(fn_die, &addr) &&
addr == ad->addr) {
memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
return DWARF_CB_ABORT;
}
return DWARF_CB_OK;
}
Dwarf_Die *die_find_tailfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
Dwarf_Die *die_mem)
{
struct __addr_die_search_param ad;
ad.addr = addr;
ad.die_mem = die_mem;
if (!dwarf_getfuncs(cu_die, __die_search_func_tail_cb, &ad, 0))
return NULL;
else
return die_mem;
}
static int __die_search_func_cb(Dwarf_Die *fn_die, void *data)
{
struct __addr_die_search_param *ad = data;
if (dwarf_tag(fn_die) == DW_TAG_subprogram &&
dwarf_haspc(fn_die, ad->addr)) {
memcpy(ad->die_mem, fn_die, sizeof(Dwarf_Die));
return DWARF_CB_ABORT;
}
return DWARF_CB_OK;
}
Dwarf_Die *die_find_realfunc(Dwarf_Die *cu_die, Dwarf_Addr addr,
Dwarf_Die *die_mem)
{
struct __addr_die_search_param ad;
ad.addr = addr;
ad.die_mem = die_mem;
if (!dwarf_getfuncs(cu_die, __die_search_func_cb, &ad, 0))
return NULL;
else
return die_mem;
}
static int __die_find_inline_cb(Dwarf_Die *die_mem, void *data)
{
Dwarf_Addr *addr = data;
if (dwarf_tag(die_mem) == DW_TAG_inlined_subroutine &&
dwarf_haspc(die_mem, *addr))
return DIE_FIND_CB_END;
return DIE_FIND_CB_CONTINUE;
}
Dwarf_Die *die_find_top_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
Dwarf_Die *die_mem)
{
return die_find_child(sp_die, __die_find_inline_cb, &addr, die_mem);
}
Dwarf_Die *die_find_inlinefunc(Dwarf_Die *sp_die, Dwarf_Addr addr,
Dwarf_Die *die_mem)
{
Dwarf_Die tmp_die;
sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr, &tmp_die);
if (!sp_die)
return NULL;
while (sp_die) {
memcpy(die_mem, sp_die, sizeof(Dwarf_Die));
sp_die = die_find_child(sp_die, __die_find_inline_cb, &addr,
&tmp_die);
}
return die_mem;
}
static int __die_find_func_rettype_cb(Dwarf_Die *die_mem, void *data)
{
const char *func_name;
if (dwarf_tag(die_mem) != DW_TAG_subprogram)
return DIE_FIND_CB_SIBLING;
func_name = dwarf_diename(die_mem);
if (func_name && !strcmp(func_name, data))
return DIE_FIND_CB_END;
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_find_func_rettype(Dwarf_Die *cu_die, const char *name,
Dwarf_Die *die_mem)
{
Dwarf_Die tmp_die;
cu_die = die_find_child(cu_die, __die_find_func_rettype_cb,
(void *)name, &tmp_die);
if (!cu_die)
return NULL;
if (die_get_real_type(&tmp_die, die_mem) == NULL)
return NULL;
return die_mem;
}
struct __instance_walk_param {
void *addr;
int (*callback)(Dwarf_Die *, void *);
void *data;
int retval;
};
static int __die_walk_instances_cb(Dwarf_Die *inst, void *data)
{
struct __instance_walk_param *iwp = data;
Dwarf_Attribute attr_mem;
Dwarf_Die origin_mem;
Dwarf_Attribute *attr;
Dwarf_Die *origin;
int tmp;
if (!die_is_func_instance(inst))
return DIE_FIND_CB_CONTINUE;
attr = dwarf_attr(inst, DW_AT_abstract_origin, &attr_mem);
if (attr == NULL)
return DIE_FIND_CB_CONTINUE;
origin = dwarf_formref_die(attr, &origin_mem);
if (origin == NULL || origin->addr != iwp->addr)
return DIE_FIND_CB_CONTINUE;
if (dwarf_tag(inst) == DW_TAG_inlined_subroutine) {
dwarf_decl_line(origin, &tmp);
if (die_get_call_lineno(inst) == tmp) {
tmp = die_get_decl_fileno(origin);
if (die_get_call_fileno(inst) == tmp)
return DIE_FIND_CB_CONTINUE;
}
}
iwp->retval = iwp->callback(inst, iwp->data);
return (iwp->retval) ? DIE_FIND_CB_END : DIE_FIND_CB_CONTINUE;
}
int die_walk_instances(Dwarf_Die *or_die, int (*callback)(Dwarf_Die *, void *),
void *data)
{
Dwarf_Die cu_die;
Dwarf_Die die_mem;
struct __instance_walk_param iwp = {
.addr = or_die->addr,
.callback = callback,
.data = data,
.retval = -ENOENT,
};
if (dwarf_diecu(or_die, &cu_die, NULL, NULL) == NULL)
return -ENOENT;
die_find_child(&cu_die, __die_walk_instances_cb, &iwp, &die_mem);
return iwp.retval;
}
struct __line_walk_param {
bool recursive;
line_walk_callback_t callback;
void *data;
int retval;
};
static int __die_walk_funclines_cb(Dwarf_Die *in_die, void *data)
{
struct __line_walk_param *lw = data;
Dwarf_Addr addr = 0;
const char *fname;
int lineno;
if (dwarf_tag(in_die) == DW_TAG_inlined_subroutine) {
fname = die_get_call_file(in_die);
lineno = die_get_call_lineno(in_die);
if (fname && lineno > 0 && die_entrypc(in_die, &addr) == 0) {
lw->retval = lw->callback(fname, lineno, addr, lw->data);
if (lw->retval != 0)
return DIE_FIND_CB_END;
}
if (!lw->recursive)
return DIE_FIND_CB_SIBLING;
}
if (addr) {
fname = die_get_decl_file(in_die);
if (fname && dwarf_decl_line(in_die, &lineno) == 0) {
lw->retval = lw->callback(fname, lineno, addr, lw->data);
if (lw->retval != 0)
return DIE_FIND_CB_END;
}
}
return DIE_FIND_CB_CONTINUE;
}
static int __die_walk_funclines(Dwarf_Die *sp_die, bool recursive,
line_walk_callback_t callback, void *data)
{
struct __line_walk_param lw = {
.recursive = recursive,
.callback = callback,
.data = data,
.retval = 0,
};
Dwarf_Die die_mem;
Dwarf_Addr addr;
const char *fname;
int lineno;
fname = die_get_decl_file(sp_die);
if (fname && dwarf_decl_line(sp_die, &lineno) == 0 &&
die_entrypc(sp_die, &addr) == 0) {
lw.retval = callback(fname, lineno, addr, data);
if (lw.retval != 0)
goto done;
}
die_find_child(sp_die, __die_walk_funclines_cb, &lw, &die_mem);
done:
return lw.retval;
}
static int __die_walk_culines_cb(Dwarf_Die *sp_die, void *data)
{
struct __line_walk_param *lw = data;
lw->retval = __die_walk_funclines(sp_die, true, lw->callback, lw->data);
if (lw->retval != 0)
return DWARF_CB_ABORT;
return DWARF_CB_OK;
}
int die_walk_lines(Dwarf_Die *rt_die, line_walk_callback_t callback, void *data)
{
Dwarf_Lines *lines;
Dwarf_Line *line;
Dwarf_Addr addr;
const char *fname, *decf = NULL, *inf = NULL;
int lineno, ret = 0;
int decl = 0, inl;
Dwarf_Die die_mem, *cu_die;
size_t nlines, i;
bool flag;
if (dwarf_tag(rt_die) != DW_TAG_compile_unit) {
cu_die = dwarf_diecu(rt_die, &die_mem, NULL, NULL);
dwarf_decl_line(rt_die, &decl);
decf = die_get_decl_file(rt_die);
if (!decf) {
pr_debug2("Failed to get the declared file name of %s\n",
dwarf_diename(rt_die));
return -EINVAL;
}
} else
cu_die = rt_die;
if (!cu_die) {
pr_debug2("Failed to get CU from given DIE.\n");
return -EINVAL;
}
if (dwarf_getsrclines(cu_die, &lines, &nlines) != 0) {
pr_debug2("Failed to get source lines on this CU.\n");
return -ENOENT;
}
pr_debug2("Get %zd lines from this CU\n", nlines);
for (i = 0; i < nlines; i++) {
line = dwarf_onesrcline(lines, i);
if (line == NULL ||
dwarf_lineno(line, &lineno) != 0 ||
dwarf_lineaddr(line, &addr) != 0) {
pr_debug2("Failed to get line info. "
"Possible error in debuginfo.\n");
continue;
}
if (dwarf_lineendsequence(line, &flag) != 0 || flag)
continue;
if (dwarf_linebeginstatement(line, &flag) != 0 || !flag)
continue;
if (rt_die != cu_die) {
if (!dwarf_haspc(rt_die, addr))
continue;
if (die_find_inlinefunc(rt_die, addr, &die_mem)) {
inf = die_get_call_file(&die_mem);
if ((inf && !strcmp(inf, decf)) &&
die_get_call_lineno(&die_mem) == lineno)
goto found;
dwarf_decl_line(&die_mem, &inl);
if (inl != decl ||
decf != die_get_decl_file(&die_mem))
continue;
}
}
found:
fname = dwarf_linesrc(line, NULL, NULL);
ret = callback(fname, lineno, addr, data);
if (ret != 0)
return ret;
}
if (rt_die != cu_die)
ret = __die_walk_funclines(rt_die, false, callback, data);
else {
struct __line_walk_param param = {
.callback = callback,
.data = data,
.retval = 0,
};
dwarf_getfuncs(cu_die, __die_walk_culines_cb, ¶m, 0);
ret = param.retval;
}
return ret;
}
struct __find_variable_param {
const char *name;
Dwarf_Addr addr;
};
static int __die_find_variable_cb(Dwarf_Die *die_mem, void *data)
{
struct __find_variable_param *fvp = data;
Dwarf_Attribute attr;
int tag;
tag = dwarf_tag(die_mem);
if ((tag == DW_TAG_formal_parameter ||
tag == DW_TAG_variable) &&
die_compare_name(die_mem, fvp->name) &&
(dwarf_attr(die_mem, DW_AT_external, &attr) ||
dwarf_attr(die_mem, DW_AT_location, &attr) ||
dwarf_attr(die_mem, DW_AT_const_value, &attr)))
return DIE_FIND_CB_END;
if (dwarf_haspc(die_mem, fvp->addr))
return DIE_FIND_CB_CONTINUE;
else
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_find_variable_at(Dwarf_Die *sp_die, const char *name,
Dwarf_Addr addr, Dwarf_Die *die_mem)
{
struct __find_variable_param fvp = { .name = name, .addr = addr};
return die_find_child(sp_die, __die_find_variable_cb, (void *)&fvp,
die_mem);
}
static int __die_find_member_cb(Dwarf_Die *die_mem, void *data)
{
const char *name = data;
if (dwarf_tag(die_mem) == DW_TAG_member) {
if (die_compare_name(die_mem, name))
return DIE_FIND_CB_END;
else if (!dwarf_diename(die_mem)) {
Dwarf_Die type_die, tmp_die;
if (die_get_type(die_mem, &type_die) &&
die_find_member(&type_die, name, &tmp_die))
return DIE_FIND_CB_END;
}
}
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_find_member(Dwarf_Die *st_die, const char *name,
Dwarf_Die *die_mem)
{
return die_find_child(st_die, __die_find_member_cb, (void *)name,
die_mem);
}
int die_get_typename_from_type(Dwarf_Die *type_die, struct strbuf *buf)
{
int tag, ret;
const char *tmp = "";
tag = dwarf_tag(type_die);
if (tag == DW_TAG_pointer_type)
tmp = "*";
else if (tag == DW_TAG_array_type)
tmp = "[]";
else if (tag == DW_TAG_subroutine_type) {
return strbuf_add(buf, "(function_type)", 15);
} else {
const char *name = dwarf_diename(type_die);
if (tag == DW_TAG_union_type)
tmp = "union ";
else if (tag == DW_TAG_structure_type)
tmp = "struct ";
else if (tag == DW_TAG_enumeration_type)
tmp = "enum ";
else if (name == NULL)
return -ENOENT;
return strbuf_addf(buf, "%s%s", tmp, name ?: "");
}
ret = die_get_typename(type_die, buf);
if (ret < 0) {
if (tag == DW_TAG_pointer_type && ret == -ENOENT)
return strbuf_addf(buf, "void*");
return ret;
}
return strbuf_addstr(buf, tmp);
}
int die_get_typename(Dwarf_Die *vr_die, struct strbuf *buf)
{
Dwarf_Die type;
if (__die_get_real_type(vr_die, &type) == NULL)
return -ENOENT;
return die_get_typename_from_type(&type, buf);
}
int die_get_varname(Dwarf_Die *vr_die, struct strbuf *buf)
{
int ret;
ret = die_get_typename(vr_die, buf);
if (ret < 0) {
pr_debug("Failed to get type, make it unknown.\n");
ret = strbuf_add(buf, "(unknown_type)", 14);
}
return ret < 0 ? ret : strbuf_addf(buf, "\t%s", dwarf_diename(vr_die));
}
static int reg_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
return op->atom - DW_OP_reg0;
case DW_OP_breg0 ... DW_OP_breg31:
return op->atom - DW_OP_breg0;
case DW_OP_regx:
case DW_OP_bregx:
return op->number;
case DW_OP_fbreg:
return DWARF_REG_FB;
default:
break;
}
return -1;
}
static int offset_from_dwarf_op(Dwarf_Op *op)
{
switch (op->atom) {
case DW_OP_reg0 ... DW_OP_reg31:
case DW_OP_regx:
return 0;
case DW_OP_breg0 ... DW_OP_breg31:
case DW_OP_fbreg:
return op->number;
case DW_OP_bregx:
return op->number2;
default:
break;
}
return -1;
}
static bool check_allowed_ops(Dwarf_Op *ops, size_t nops)
{
ops++;
nops--;
while (nops) {
switch (ops->atom) {
case DW_OP_stack_value:
case DW_OP_deref_size:
case DW_OP_deref:
case DW_OP_piece:
break;
default:
return false;
}
ops++;
nops--;
}
return true;
}
static int die_get_var_innermost_scope(Dwarf_Die *sp_die, Dwarf_Die *vr_die,
struct strbuf *buf)
{
Dwarf_Die *scopes;
int count;
size_t offset = 0;
Dwarf_Addr base;
Dwarf_Addr start, end;
Dwarf_Addr entry;
int ret;
bool first = true;
const char *name;
ret = die_entrypc(sp_die, &entry);
if (ret)
return ret;
name = dwarf_diename(sp_die);
if (!name)
return -ENOENT;
count = dwarf_getscopes_die(vr_die, &scopes);
if (count <= 1) {
ret = -EINVAL;
goto out;
}
while ((offset = dwarf_ranges(&scopes[1], offset, &base,
&start, &end)) > 0) {
start -= entry;
end -= entry;
if (first) {
ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
name, start, end);
first = false;
} else {
ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
start, end);
}
if (ret < 0)
goto out;
}
if (!first)
ret = strbuf_add(buf, "]>", 2);
out:
free(scopes);
return ret;
}
int die_get_var_range(Dwarf_Die *sp_die, Dwarf_Die *vr_die, struct strbuf *buf)
{
int ret = 0;
Dwarf_Addr base;
Dwarf_Addr start, end;
Dwarf_Addr entry;
Dwarf_Op *op;
size_t nops;
size_t offset = 0;
Dwarf_Attribute attr;
bool first = true;
const char *name;
ret = die_entrypc(sp_die, &entry);
if (ret)
return ret;
name = dwarf_diename(sp_die);
if (!name)
return -ENOENT;
if (dwarf_attr(vr_die, DW_AT_location, &attr) == NULL)
return -EINVAL;
while ((offset = dwarf_getlocations(&attr, offset, &base,
&start, &end, &op, &nops)) > 0) {
if (start == 0) {
ret = die_get_var_innermost_scope(sp_die, vr_die, buf);
goto out;
}
start -= entry;
end -= entry;
if (first) {
ret = strbuf_addf(buf, "@<%s+[%" PRIu64 "-%" PRIu64,
name, start, end);
first = false;
} else {
ret = strbuf_addf(buf, ",%" PRIu64 "-%" PRIu64,
start, end);
}
if (ret < 0)
goto out;
}
if (!first)
ret = strbuf_add(buf, "]>", 2);
out:
return ret;
}
struct find_var_data {
Dwarf_Addr pc;
Dwarf_Addr addr;
unsigned reg;
int offset;
bool is_fbreg;
};
#define DWARF_OP_DIRECT_REGS 32
static bool match_var_offset(Dwarf_Die *die_mem, struct find_var_data *data,
s64 addr_offset, s64 addr_type, bool is_pointer)
{
Dwarf_Die type_die;
Dwarf_Word size;
s64 offset = addr_offset - addr_type;
if (offset == 0) {
data->offset = 0;
return true;
}
if (offset < 0)
return false;
if (die_get_real_type(die_mem, &type_die) == NULL)
return false;
if (is_pointer && dwarf_tag(&type_die) == DW_TAG_pointer_type) {
if (die_get_real_type(&type_die, &type_die) == NULL)
return false;
}
if (dwarf_aggregate_size(&type_die, &size) < 0)
return false;
if ((u64)offset >= size)
return false;
data->offset = offset;
return true;
}
static bool is_breg_access_indirect(Dwarf_Op *ops, size_t nops)
{
if (nops == 1)
return false;
if (nops == 2 && ops[1].atom == DW_OP_stack_value)
return true;
if (nops == 3 && (ops[1].atom == DW_OP_deref ||
ops[1].atom == DW_OP_deref_size) &&
ops[2].atom == DW_OP_stack_value)
return false;
return false;
}
static int __die_find_var_reg_cb(Dwarf_Die *die_mem, void *arg)
{
struct find_var_data *data = arg;
int tag = dwarf_tag(die_mem);
ptrdiff_t off = 0;
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
if (tag != DW_TAG_variable && tag != DW_TAG_formal_parameter)
return DIE_FIND_CB_SIBLING;
if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL)
return DIE_FIND_CB_SIBLING;
while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
if (end <= data->pc)
continue;
if (start > data->pc)
break;
if (data->is_fbreg && ops->atom == DW_OP_fbreg &&
check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->offset, ops->number,
is_breg_access_indirect(ops, nops)))
return DIE_FIND_CB_END;
if (data->reg < DWARF_OP_DIRECT_REGS) {
if (ops->atom == (DW_OP_reg0 + data->reg) &&
check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->offset, 0,
true))
return DIE_FIND_CB_END;
if (ops->atom == (DW_OP_breg0 + data->reg) &&
check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->offset, ops->number,
is_breg_access_indirect(ops, nops)))
return DIE_FIND_CB_END;
} else {
if (ops->atom == DW_OP_regx && ops->number == data->reg &&
check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->offset, 0,
true))
return DIE_FIND_CB_END;
if (ops->atom == DW_OP_bregx && data->reg == ops->number &&
check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->offset, ops->number2,
is_breg_access_indirect(ops, nops)))
return DIE_FIND_CB_END;
}
}
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_find_variable_by_reg(Dwarf_Die *sc_die, Dwarf_Addr pc, int reg,
int *poffset, bool is_fbreg,
Dwarf_Die *die_mem)
{
struct find_var_data data = {
.pc = pc,
.reg = reg,
.offset = *poffset,
.is_fbreg = is_fbreg,
};
Dwarf_Die *result;
result = die_find_child(sc_die, __die_find_var_reg_cb, &data, die_mem);
if (result)
*poffset = data.offset;
return result;
}
static int __die_find_var_addr_cb(Dwarf_Die *die_mem, void *arg)
{
struct find_var_data *data = arg;
int tag = dwarf_tag(die_mem);
ptrdiff_t off = 0;
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
if (tag != DW_TAG_variable)
return DIE_FIND_CB_SIBLING;
if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL)
return DIE_FIND_CB_SIBLING;
while ((off = dwarf_getlocations(&attr, off, &base, &start, &end, &ops, &nops)) > 0) {
if (ops->atom != DW_OP_addr)
continue;
if (check_allowed_ops(ops, nops) &&
match_var_offset(die_mem, data, data->addr, ops->number,
false))
return DIE_FIND_CB_END;
}
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_find_variable_by_addr(Dwarf_Die *sc_die, Dwarf_Addr addr,
Dwarf_Die *die_mem, int *offset)
{
struct find_var_data data = {
.addr = addr,
};
Dwarf_Die *result;
result = die_find_child(sc_die, __die_find_var_addr_cb, &data, die_mem);
if (result)
*offset = data.offset;
return result;
}
static int __die_collect_vars_cb(Dwarf_Die *die_mem, void *arg)
{
struct die_var_type **var_types = arg;
Dwarf_Die type_die;
int tag = dwarf_tag(die_mem);
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
struct die_var_type *vt;
if (tag != DW_TAG_variable && tag != DW_TAG_formal_parameter)
return DIE_FIND_CB_SIBLING;
if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL)
return DIE_FIND_CB_SIBLING;
if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <= 0)
return DIE_FIND_CB_SIBLING;
if (!check_allowed_ops(ops, nops))
return DIE_FIND_CB_SIBLING;
if (__die_get_real_type(die_mem, &type_die) == NULL)
return DIE_FIND_CB_SIBLING;
vt = malloc(sizeof(*vt));
if (vt == NULL)
return DIE_FIND_CB_END;
vt->is_reg_var_addr = false;
if (((ops->atom >= DW_OP_breg0 && ops->atom <= DW_OP_breg31) ||
ops->atom == DW_OP_bregx || ops->atom == DW_OP_fbreg) &&
!is_breg_access_indirect(ops, nops))
vt->is_reg_var_addr = true;
vt->die_off = dwarf_dieoffset(&type_die);
vt->addr = start;
vt->reg = reg_from_dwarf_op(ops);
vt->offset = offset_from_dwarf_op(ops);
vt->next = *var_types;
*var_types = vt;
return DIE_FIND_CB_SIBLING;
}
void die_collect_vars(Dwarf_Die *sc_die, struct die_var_type **var_types)
{
Dwarf_Die die_mem;
die_find_child(sc_die, __die_collect_vars_cb, (void *)var_types, &die_mem);
}
static int __die_collect_global_vars_cb(Dwarf_Die *die_mem, void *arg)
{
struct die_var_type **var_types = arg;
Dwarf_Die type_die;
int tag = dwarf_tag(die_mem);
Dwarf_Attribute attr;
Dwarf_Addr base, start, end;
Dwarf_Op *ops;
size_t nops;
struct die_var_type *vt;
if (tag != DW_TAG_variable)
return DIE_FIND_CB_SIBLING;
if (dwarf_attr(die_mem, DW_AT_location, &attr) == NULL)
return DIE_FIND_CB_SIBLING;
if (dwarf_getlocations(&attr, 0, &base, &start, &end, &ops, &nops) <= 0)
return DIE_FIND_CB_SIBLING;
if (ops->atom != DW_OP_addr)
return DIE_FIND_CB_SIBLING;
if (!check_allowed_ops(ops, nops))
return DIE_FIND_CB_SIBLING;
if (die_get_real_type(die_mem, &type_die) == NULL)
return DIE_FIND_CB_SIBLING;
vt = malloc(sizeof(*vt));
if (vt == NULL)
return DIE_FIND_CB_END;
vt->die_off = dwarf_dieoffset(&type_die);
vt->addr = ops->number;
vt->reg = -1;
vt->offset = 0;
vt->next = *var_types;
*var_types = vt;
return DIE_FIND_CB_SIBLING;
}
void die_collect_global_vars(Dwarf_Die *cu_die, struct die_var_type **var_types)
{
Dwarf_Die die_mem;
die_find_child(cu_die, __die_collect_global_vars_cb, (void *)var_types, &die_mem);
}
int die_get_cfa(Dwarf *dwarf, u64 pc, int *preg, int *poffset)
{
Dwarf_CFI *cfi;
Dwarf_Frame *frame = NULL;
Dwarf_Op *ops = NULL;
size_t nops;
cfi = dwarf_getcfi(dwarf);
if (cfi == NULL)
return -1;
if (!dwarf_cfi_addrframe(cfi, pc, &frame) &&
!dwarf_frame_cfa(frame, &ops, &nops) &&
check_allowed_ops(ops, nops)) {
*preg = reg_from_dwarf_op(ops);
*poffset = offset_from_dwarf_op(ops);
return 0;
}
return -1;
}
static bool die_has_loclist(Dwarf_Die *vr_die)
{
Dwarf_Attribute loc;
int tag = dwarf_tag(vr_die);
if (tag != DW_TAG_formal_parameter &&
tag != DW_TAG_variable)
return false;
return (dwarf_attr_integrate(vr_die, DW_AT_location, &loc) &&
dwarf_whatform(&loc) == DW_FORM_sec_offset);
}
bool die_is_optimized_target(Dwarf_Die *cu_die)
{
Dwarf_Die tmp_die;
if (die_has_loclist(cu_die))
return true;
if (!dwarf_child(cu_die, &tmp_die) &&
die_is_optimized_target(&tmp_die))
return true;
if (!dwarf_siblingof(cu_die, &tmp_die) &&
die_is_optimized_target(&tmp_die))
return true;
return false;
}
static bool die_search_idx(Dwarf_Lines *lines, unsigned long nr_lines,
Dwarf_Addr addr, unsigned long *idx)
{
unsigned long i;
Dwarf_Addr tmp;
for (i = 0; i < nr_lines; i++) {
if (dwarf_lineaddr(dwarf_onesrcline(lines, i), &tmp))
return false;
if (tmp == addr) {
*idx = i;
return true;
}
}
return false;
}
static bool die_get_postprologue_addr(unsigned long entrypc_idx,
Dwarf_Lines *lines,
unsigned long nr_lines,
Dwarf_Addr highpc,
Dwarf_Addr *postprologue_addr)
{
unsigned long i;
int entrypc_lno, lno;
Dwarf_Line *line;
Dwarf_Addr addr;
bool p_end;
line = dwarf_onesrcline(lines, entrypc_idx);
if (dwarf_lineno(line, &entrypc_lno))
return false;
for (i = entrypc_idx; i < nr_lines; i++) {
line = dwarf_onesrcline(lines, i);
if (dwarf_lineaddr(line, &addr) ||
dwarf_lineno(line, &lno) ||
dwarf_lineprologueend(line, &p_end))
return false;
if (addr >= highpc)
break;
if (p_end)
break;
if (lno != entrypc_lno)
break;
if (i != entrypc_idx)
break;
}
dwarf_lineaddr(line, postprologue_addr);
if (*postprologue_addr >= highpc)
dwarf_lineaddr(dwarf_onesrcline(lines, i - 1),
postprologue_addr);
return true;
}
void die_skip_prologue(Dwarf_Die *sp_die, Dwarf_Die *cu_die,
Dwarf_Addr *entrypc)
{
size_t nr_lines = 0;
unsigned long entrypc_idx = 0;
Dwarf_Lines *lines = NULL;
Dwarf_Addr postprologue_addr;
Dwarf_Addr highpc;
if (dwarf_highpc(sp_die, &highpc))
return;
if (dwarf_getsrclines(cu_die, &lines, &nr_lines))
return;
if (!die_search_idx(lines, nr_lines, *entrypc, &entrypc_idx))
return;
if (!die_get_postprologue_addr(entrypc_idx, lines, nr_lines,
highpc, &postprologue_addr))
return;
*entrypc = postprologue_addr;
}
struct find_scope_data {
Dwarf_Addr pc;
int nr;
Dwarf_Die *scopes;
};
static int __die_find_scope_cb(Dwarf_Die *die_mem, void *arg)
{
struct find_scope_data *data = arg;
int tag = dwarf_tag(die_mem);
if (dwarf_haspc(die_mem, data->pc)) {
Dwarf_Die *tmp;
tmp = realloc(data->scopes, (data->nr + 1) * sizeof(*tmp));
if (tmp == NULL)
return DIE_FIND_CB_END;
memcpy(tmp + data->nr, die_mem, sizeof(*die_mem));
data->scopes = tmp;
data->nr++;
return DIE_FIND_CB_CHILD;
}
if (tag == DW_TAG_namespace)
return DIE_FIND_CB_CONTINUE;
return DIE_FIND_CB_SIBLING;
}
int die_get_scopes(Dwarf_Die *cu_die, Dwarf_Addr pc, Dwarf_Die **scopes)
{
struct find_scope_data data = {
.pc = pc,
};
Dwarf_Die die_mem;
die_find_child(cu_die, __die_find_scope_cb, &data, &die_mem);
*scopes = data.scopes;
return data.nr;
}
static int __die_find_member_offset_cb(Dwarf_Die *die_mem, void *arg)
{
Dwarf_Die type_die;
Dwarf_Word size, loc;
Dwarf_Word offset = (long)arg;
int tag = dwarf_tag(die_mem);
if (tag != DW_TAG_member)
return DIE_FIND_CB_SIBLING;
if (die_get_data_member_location(die_mem, &loc) < 0) {
Dwarf_Attribute attr;
if (dwarf_attr_integrate(die_mem, DW_AT_data_bit_offset, &attr) &&
dwarf_formudata(&attr, &loc) == 0)
loc /= 8;
else
loc = 0;
}
if (offset == loc)
return DIE_FIND_CB_END;
if (die_get_real_type(die_mem, &type_die) == NULL) {
return DIE_FIND_CB_SIBLING;
}
if (dwarf_aggregate_size(&type_die, &size) < 0)
size = 0;
if (loc < offset && offset < (loc + size))
return DIE_FIND_CB_END;
return DIE_FIND_CB_SIBLING;
}
Dwarf_Die *die_get_member_type(Dwarf_Die *type_die, int offset,
Dwarf_Die *die_mem)
{
Dwarf_Die *member;
Dwarf_Die mb_type;
int tag;
tag = dwarf_tag(type_die);
if (tag != DW_TAG_structure_type && tag != DW_TAG_union_type) {
Dwarf_Word size;
if (dwarf_aggregate_size(type_die, &size) < 0)
size = 0;
if ((unsigned)offset >= size)
return NULL;
*die_mem = *type_die;
return die_mem;
}
mb_type = *type_die;
while (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) {
member = die_find_child(&mb_type, __die_find_member_offset_cb,
(void *)(long)offset, die_mem);
if (member == NULL)
return NULL;
if (die_get_real_type(member, &mb_type) == NULL)
return NULL;
tag = dwarf_tag(&mb_type);
if (tag == DW_TAG_structure_type || tag == DW_TAG_union_type) {
Dwarf_Word loc;
if (die_get_data_member_location(member, &loc) == 0)
offset -= loc;
}
}
*die_mem = mb_type;
return die_mem;
}
Dwarf_Die *die_deref_ptr_type(Dwarf_Die *ptr_die, int offset,
Dwarf_Die *die_mem)
{
Dwarf_Die type_die;
if (dwarf_tag(ptr_die) != DW_TAG_pointer_type)
return NULL;
if (die_get_real_type(ptr_die, &type_die) == NULL)
return NULL;
return die_get_member_type(&type_die, offset, die_mem);
}