Esempio n. 1
0
int
dwarflookupsubname(Dwarf *d, DwarfSym *parent, char *name, DwarfSym *s)
{
    *s = *parent;
    while(dwarfnextsymat(d, parent, s))
        if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
            return 0;
    werrstr("symbol '%s' not found", name);
    return -1;
}
Esempio n. 2
0
int
dwarflookupnameinunit(Dwarf *d, ulong unit, char *name, DwarfSym *s)
{
    DwarfSym compunit = { };
    if(dwarfenumunit(d, unit, &compunit) < 0)
        return -1;
    while(dwarfnextsymat(d, &compunit, s) == 0) {
        werrstr("got %s looking for %s\n", s->attrs.name, name);
        if(s->attrs.name && strcmp(s->attrs.name, name) == 0)
            return 0;
    }
    werrstr("symbol '%s' not found", name);
    return -1;
}
Esempio n. 3
0
int
dwarflookupfn(Dwarf *d, ulong unit, ulong pc, DwarfSym *s)
{
    DwarfSym compunit = { };
    if(dwarfenumunit(d, unit, &compunit) < 0)
        return -1;
    while(dwarfnextsymat(d, &compunit, s) == 0){
        if(s->attrs.tag != TagSubprogram)
            continue;
        if(s->attrs.lowpc <= pc && pc < s->attrs.highpc)
            return 0;
    }
    werrstr("fn containing pc 0x%lux not found", pc);
    return -1;
}
Esempio n. 4
0
int
dwarfseeksym(Dwarf *d, ulong unit, ulong off, DwarfSym *s)
{
    DwarfSym compunit = { };
    if(dwarfenumunit(d, unit, &compunit) < 0)
        return -1;
    werrstr("dwarfseeksym: unit %x off %x\n", unit, off);
    s->b.d = d;
    s->b.p = d->info.data + unit + off;
    s->b.ep = compunit.b.ep;
    if(dwarfnextsymat(d, &compunit, s) == -1)
        return -1;
    werrstr("dwarfseeksym: unit %x off %x, tag %x", unit, off, s->attrs.tag);
    return 0;
}
Esempio n. 5
0
int
dwarflookupchildtag(Dwarf *d, DwarfSym *parent, ulong tag, DwarfSym *s)
{
    int rsym = dwarfnextsymat(d, parent, s);
    while (rsym == 0 && s->attrs.tag != tag) {
        if (s->attrs.haskids) {
            DwarfSym p = *s;
            int csym = dwarflookupchildtag(d, &p, tag, s);
            if (csym == 0) {
                return csym;
            }
        }
        rsym = dwarfnextsym(d, s);
    }
    return rsym;
}
Esempio n. 6
0
int
dwarfnextsym(Dwarf *d, DwarfSym *s)
{
    ulong num;
    DwarfAbbrev *a;

    werrstr("sym at %x (left %x)\n", s->b.p - d->info.data, s->b.ep - s->b.p);

    num = dwarfget128(&s->b);
    werrstr("abbrev num %x\n", num);
    s->num = num;
    if(num == 0){
        return -1;
    }

    a = dwarfgetabbrev(d, s->aoff, num);
    werrstr("a %p\n", a);
    if(a == nil){
        werrstr("getabbrev %x %x for %x", s->aoff, num, s->unit);
        return -1;
    }

    if(parseattrs(d, &s->b, s->attrs.tag, s->unit, a, &s->attrs) < 0) {
        return -1;
    }

    if (s->attrs.haskids) {
        DwarfSym childSkip = { };
        s->childoff = s->b.p - d->info.data;
        werrstr("Set childoff at %x\n", s->childoff);
        int r = dwarfnextsymat(d, s, &childSkip);
        while (r == 0) {
            r = dwarfnextsym(d, &childSkip);
        }
        s->b = childSkip.b;
    } else {
        s->childoff = 0;
    }
    return 0;
}
Esempio n. 7
0
BOOLEAN
RosSymAggregate(PROSSYM_INFO RosSymInfo, PCHAR Type, PROSSYM_AGGREGATE Aggregate)
{
    char *tchar;
    ulong unit, typeoff = 0;
    DwarfSym type = { };
    // Get the first unit
    if (dwarfaddrtounit(RosSymInfo, RosSymInfo->pe->codestart + RosSymInfo->pe->imagebase, &unit) == -1)
        return FALSE;

    if (Type[0] == '#') {
        for (tchar = Type + 1; *tchar; tchar++) {
            typeoff *= 10;
            typeoff += *tchar - '0';
        }
        if (dwarfseeksym(RosSymInfo, unit, typeoff, &type) == -1)
            return FALSE;
    } else if (dwarflookupnameinunit(RosSymInfo, unit, Type, &type) != 0 ||
        (type.attrs.tag != TagStructType && type.attrs.tag != TagUnionType))
        return FALSE;
    
    DwarfSym element = { }, inner = { };
    int count = 0;
    
    werrstr("type %s (want %s) type %x\n", type.attrs.name, Type, type.attrs.type);
    
    if (type.attrs.have.type) {
        if (dwarfseeksym(RosSymInfo, unit, type.attrs.type, &inner) == -1)
            return FALSE;
        type = inner;
    }
    
    werrstr("finding members %d\n", type.attrs.haskids);
    while (dwarfnextsymat(RosSymInfo, &type, &element) != -1) {
        if (element.attrs.have.name)
            werrstr("%x %s\n", element.attrs.tag, element.attrs.name);
        if (element.attrs.tag == TagMember) count++;
    }
    
    werrstr("%d members\n", count);
    
    if (!count) return FALSE;
    memset(&element, 0, sizeof(element));
    Aggregate->NumElements = count;
    Aggregate->Elements = malloc(sizeof(ROSSYM_AGGREGATE_MEMBER) * count);
    count = 0;
    werrstr("Enumerating %s\n", Type);
    while (dwarfnextsymat(RosSymInfo, &type, &element) != -1) {
        memset(&Aggregate->Elements[count], 0, sizeof(*Aggregate->Elements));
        if (element.attrs.tag == TagMember) {
            if (element.attrs.have.name) {
                Aggregate->Elements[count].Name = malloc(strlen(element.attrs.name) + 1);
                strcpy(Aggregate->Elements[count].Name, element.attrs.name);
            }
            Aggregate->Elements[count].TypeId = element.attrs.type;
            // Seek our range in loc
            DwarfBuf locbuf;
            DwarfBuf instream = { };
            
            locbuf.d = RosSymInfo;
            locbuf.addrsize = RosSymInfo->addrsize;

            if (element.attrs.have.datamemberloc) {
                instream = locbuf;
                instream.p = element.attrs.datamemberloc.b.data;
                instream.ep = element.attrs.datamemberloc.b.data + element.attrs.datamemberloc.b.len;
                werrstr("datamemberloc type %x %p:%x\n", 
                        element.attrs.have.datamemberloc,
                        element.attrs.datamemberloc.b.data, element.attrs.datamemberloc.b.len);
            }

            if (dwarfgetarg(RosSymInfo, element.attrs.name, &instream, 0, NULL, &Aggregate->Elements[count].BaseOffset) == -1)
                Aggregate->Elements[count].BaseOffset = -1;
            werrstr("tag %x name %s base %x type %x\n", 
                    element.attrs.tag, element.attrs.name, 
                    Aggregate->Elements[count].BaseOffset,
                    Aggregate->Elements[count].TypeId);
            count++;
        }
    }
    for (count = 0; count < Aggregate->NumElements; count++) {
        memset(&type, 0, sizeof(type));
        memset(&inner, 0, sizeof(inner));
        werrstr("seeking type %x (%s) from %s\n", 
                Aggregate->Elements[count].TypeId,
                Aggregate->Elements[count].Type,
                Aggregate->Elements[count].Name);
        dwarfseeksym(RosSymInfo, unit, Aggregate->Elements[count].TypeId, &type);
        while (type.attrs.have.type && type.attrs.tag != TagPointerType) {
            if (dwarfseeksym(RosSymInfo, unit, type.attrs.type, &inner) == -1)
                return FALSE;
            type = inner;
        }
        //dwarfdumpsym(RosSymInfo, &type);
        if (type.attrs.have.name) {
            Aggregate->Elements[count].Type = malloc(strlen(type.attrs.name) + 1);
            strcpy(Aggregate->Elements[count].Type, type.attrs.name);
        } else {
            char strbuf[128] = {'#'}, *bufptr = strbuf + 1;
            ulong idcopy = Aggregate->Elements[count].TypeId;
            ulong mult = 1;
            while (mult * 10 < idcopy) mult *= 10;
            while (mult > 0) {
                *bufptr++ = '0' + ((idcopy / mult) % 10);
                mult /= 10;
            }
            Aggregate->Elements[count].Type = malloc(strlen(strbuf) + 1);
            strcpy(Aggregate->Elements[count].Type, strbuf);
        }
        if (type.attrs.tag == TagPointerType)
            Aggregate->Elements[count].Size = RosSymInfo->addrsize;
        else
            Aggregate->Elements[count].Size = type.attrs.bytesize;
        if (type.attrs.have.bitsize)
            Aggregate->Elements[count].Bits = type.attrs.bitsize;
        if (type.attrs.have.bitoffset)
            Aggregate->Elements[count].FirstBit = type.attrs.bitoffset;
    }
    return TRUE;
}
Esempio n. 8
0
int
dwarfpctoline(Dwarf *d, DwarfSym *proc, ulong pc, char **file, char **function, ulong *line)
{
	char *cdir;
    uchar *prog, *opcount, *end, *dirs;
    ulong off, unit, len, vers, x, start, lastline;
    int i, first, firstline, op, a, l, quantum, isstmt, linebase, linerange, opcodebase, nf;
    char *files, *s;
    DwarfBuf b;
    DwarfSym sym;
    State emit, cur, reset;
    char **f, **newf;

    f = nil;
    memset(proc, 0, sizeof(*proc));

    int runit = dwarfaddrtounit(d, pc, &unit);
    if (runit < 0)
        return -1;
    int rtag = dwarflookuptag(d, unit, TagCompileUnit, &sym);
    if (rtag < 0)
        return -1;

    if(!sym.attrs.have.stmtlist){
        werrstr("no line mapping information for 0x%x", pc);
        return -1;
    }
    off = sym.attrs.stmtlist;
    if(off >= d->line.len){
        werrstr("bad stmtlist");
        goto bad;
    }

    if(trace) werrstr("unit 0x%x stmtlist 0x%x", unit, sym.attrs.stmtlist);

    memset(&b, 0, sizeof b);
    b.d = d;
    b.p = d->line.data + off;
    b.ep = b.p + d->line.len;
    b.addrsize = sym.b.addrsize;	/* should i get this from somewhere else? */

    len = dwarfget4(&b);
    if(b.p==nil || b.p+len > b.ep || b.p+len < b.p){
        werrstr("bad len");
        goto bad;
    }

    b.ep = b.p+len;
    vers = dwarfget2(&b);
    if(vers != 2){
        werrstr("bad dwarf version 0x%x", vers);
        return -1;
    }

    len = dwarfget4(&b);
    if(b.p==nil || b.p+len > b.ep || b.p+len < b.p){
        werrstr("another bad len");
        goto bad;
    }
    prog = b.p+len;

    quantum = dwarfget1(&b);
    isstmt = dwarfget1(&b);
    linebase = (schar)dwarfget1(&b);
    linerange = (schar)dwarfget1(&b);
    opcodebase = dwarfget1(&b);

    opcount = b.p-1;
    dwarfgetnref(&b, opcodebase-1);
    if(b.p == nil){
        werrstr("bad opcode chart");
        goto bad;
    }

    /* just skip the files and dirs for now; we'll come back */
    dirs = b.p;
    while (b.p && *b.p)
        dwarfgetstring(&b);
    dwarfget1(&b);

    files = (char*)b.p;
    while(b.p!=nil && *b.p!=0){
        dwarfgetstring(&b);
        dwarfget128(&b);
        dwarfget128(&b);
        dwarfget128(&b);
    }
    dwarfget1(&b);

    /* move on to the program */
    if(b.p == nil || b.p > prog){
        werrstr("bad header");
        goto bad;
    }
    b.p = prog;

    reset.addr = 0;
    reset.file = 1;
    reset.line = 1;
    reset.column = 0;
    reset.flags = isstmt ? Isstmt : 0;
    reset.isa = 0;

    cur = reset;
    emit = reset;
    nf = 0;
    start = 0;
    if(trace) werrstr("program @ %lu ... %.*H opbase = %d", b.p - d->line.data, b.ep-b.p, b.p, opcodebase);
    first = 1;
    while(b.p != nil){
        firstline = 0;
        op = dwarfget1(&b);
        if(trace) werrstr("\tline %lu, addr 0x%x, op %d %.10H", cur.line, cur.addr, op, b.p);
        if(op >= opcodebase){
            a = (op - opcodebase) / linerange;
            l = (op - opcodebase) % linerange + linebase;
            cur.line += l;
            cur.addr += a * quantum;
            if(trace) werrstr(" +%d,%d", a, l);
        emit:
            if(first){
                if(cur.addr > pc){
                    werrstr("found wrong line mapping 0x%x for pc 0x%x", cur.addr, pc);
                    /* This is an overzealous check.  gcc can produce discontiguous ranges
                       and reorder statements, so it's possible for a future line to start
                       ahead of pc and still find a matching one. */
                    /*goto out;*/
                    firstline = 1;
                }
                first = 0;
                start = cur.addr;
            }
            if(cur.addr > pc && !firstline)
                break;
            if(b.p == nil){
                werrstr("buffer underflow in line mapping");
                goto out;
            }
            emit = cur;
            if(emit.flags & EndSequence){
                werrstr("found wrong line mapping 0x%x-0x%x for pc 0x%x", start, cur.addr, pc);
                goto out;
            }
            cur.flags &= ~(BasicDwarfBlock|PrologueEnd|EpilogueBegin);
        }else{
            switch(op){
            case 0:	/* extended op code */
                if(trace) werrstr(" ext");
                len = dwarfget128(&b);
                end = b.p+len;
                if(b.p == nil || end > b.ep || end < b.p || len < 1)
                    goto bad;
                switch(dwarfget1(&b)){
                case 1:	/* end sequence */
                    if(trace) werrstr(" end");
                    cur.flags |= EndSequence;
                    goto emit;
                case 2:	/* set address */
                    cur.addr = dwarfgetaddr(&b);
                    if(trace) werrstr(" set pc 0x%x", cur.addr);
                    break;
                case 3:	/* define file */
                    newf = malloc(nf+1*sizeof(f[0]));
                    if (newf)
                        RtlMoveMemory(newf, f, nf*sizeof(f[0]));
                    if(newf == nil)
                        goto out;
					free(f);
                    f = newf;
					f[nf++] = s = dwarfgetstring(&b);
					DPRINT1("str %s", s);
                    dwarfget128(&b);
                    dwarfget128(&b);
                    dwarfget128(&b);
                    if(trace) werrstr(" def file %s", s);
                    break;
                }
                if(b.p == nil || b.p > end)
                    goto bad;
                b.p = end;
                break;
            case 1:	/* emit */
                if(trace) werrstr(" emit");
                goto emit;
            case 2:	/* advance pc */
                a = dwarfget128(&b);
                if(trace) werrstr(" advance pc + %lu", a*quantum);
                cur.addr += a * quantum;
                break;
            case 3:	/* advance line */
                l = dwarfget128s(&b);
                if(trace) werrstr(" advance line + %ld", l);
                cur.line += l;
                break;
            case 4:	/* set file */
                if(trace) werrstr(" set file");
                cur.file = dwarfget128s(&b);
                break;
            case 5:	/* set column */
                if(trace) werrstr(" set column");
                cur.column = dwarfget128(&b);
                break;
            case 6:	/* negate stmt */
                if(trace) werrstr(" negate stmt");
                cur.flags ^= Isstmt;
                break;
            case 7:	/* set basic block */
                if(trace) werrstr(" set basic block");
                cur.flags |= BasicDwarfBlock;
                break;
            case 8:	/* const add pc */
                a = (255 - opcodebase) / linerange * quantum;
                if(trace) werrstr(" const add pc + %d", a);
                cur.addr += a;
                break;
            case 9:	/* fixed advance pc */
                a = dwarfget2(&b);
                if(trace) werrstr(" fixed advance pc + %d", a);
                cur.addr += a;
                break;
            case 10:	/* set prologue end */
                if(trace) werrstr(" set prologue end");
                cur.flags |= PrologueEnd;
                break;
            case 11:	/* set epilogue begin */
                if(trace) werrstr(" set epilogue begin");
                cur.flags |= EpilogueBegin;
                break;
            case 12:	/* set isa */
                if(trace) werrstr(" set isa");
                cur.isa = dwarfget128(&b);
                break;
            default:	/* something new - skip it */
                if(trace) werrstr(" unknown %d", opcount[op]);
                for(i=0; i<opcount[op]; i++)
                    dwarfget128(&b);
                break;
            }
        }
    }
    if(b.p == nil)
        goto bad;

    /* finally!  the data we seek is in "emit" */

    if(emit.file == 0){
        werrstr("invalid file index in mapping data");
        goto out;
    }
    if(line)
        *line = emit.line;

    /* skip over first emit.file-2 guys */
    b.p = (uchar*)files;
    for(i=emit.file-1; i > 0 && b.p!=nil && *b.p!=0; i--){
        dwarfgetstring(&b);
        dwarfget128(&b);
        dwarfget128(&b);
        dwarfget128(&b);
    }
    if(b.p == nil){
        werrstr("problem parsing file data second time (cannot happen)");
        goto bad;
    }
    if(*b.p == 0){
        if(i >= nf){
            werrstr("bad file index in mapping data");
            goto bad;
        }
        b.p = (uchar*)f[i];
    }
    s = dwarfgetstring(&b);
	*file = s;
    i = dwarfget128(&b);		/* directory */
    x = dwarfget128(&b);
    x = dwarfget128(&b);

    /* fetch dir name */
	cdir = sym.attrs.have.compdir ? sym.attrs.compdir : 0;

	char *dwarfdir;
	dwarfdir = nil;
	b.p = dirs;
	for (x = 1; b.p && *b.p; x++) {
		dwarfdir = dwarfgetstring(&b);
		if (x == i) break;
	}

	if (!cdir && dwarfdir)
		cdir = dwarfdir;
	
	char *filefull = malloc(strlen(cdir) + strlen(*file) + 2);
	strcpy(filefull, cdir);
	strcat(filefull, "/");
	strcat(filefull, *file);
	*file = filefull;
	
    *function = nil;
    lastline = 0;
	
    runit = dwarfaddrtounit(d, pc, &unit);
    if (runit == 0) {
        DwarfSym compunit = { };
        int renum = dwarfenumunit(d, unit, &compunit);
        if (renum < 0)
            return -1;
        renum = dwarfnextsymat(d, &compunit, proc);
        while (renum == 0) {
            if (proc->attrs.tag == TagSubprogram && 
				proc->attrs.have.name)
			{
                if (proc->attrs.lowpc <= pc && proc->attrs.highpc > pc) {
                    *function = malloc(strlen(proc->attrs.name)+1);
					strcpy(*function, proc->attrs.name);
                    goto done;
				}
			}
            renum = dwarfnextsym(d, proc);
        }
    }

    // Next search by declaration
    runit = dwarfaddrtounit(d, pc, &unit);
    if (runit == 0) {
        DwarfSym compunit = { };
        int renum = dwarfenumunit(d, unit, &compunit);
        if (renum < 0)
            return -1;
        renum = dwarfnextsymat(d, &compunit, proc);
        while (renum == 0) {
            if (proc->attrs.tag == TagSubprogram && 
				proc->attrs.have.name &&
				proc->attrs.declfile == emit.file)
			{
                if (proc->attrs.declline <= *line &&
                    proc->attrs.declline > lastline) {
                    free(*function);
                    *function = malloc(strlen(proc->attrs.name)+1);
					strcpy(*function, proc->attrs.name);
                    goto done;
				}
                lastline = proc->attrs.declline;
			}
            renum = dwarfnextsym(d, proc);
        }
    }

    /* free at last, free at last */
done:
    free(f);
    return 0;
bad:
    werrstr("corrupted line mapping for 0x%x", pc);
out:
    free(f);
    return -1;
}