12 relocsym(Symbol *dst, Symbol *src, ulong base)
16 if(dst->loc.type == LADDR)
17 dst->loc.addr += base;
18 if(dst->hiloc.type == LADDR)
19 dst->hiloc.addr += base;
43 for(p=fhdrlist; p && p->next!=h; p=p->next)
61 for(p=fhdrlist; p; p=p->next){
62 plen = strlen(p->filename);
64 if(strcmp(p->filename+plen-len, name) == 0)
65 if(plen == len || p->filename[plen-len-1] == '/')
72 pc2file(u64int pc, char *file, uint nfile, ulong *line)
76 for(p=fhdrlist; p; p=p->next)
77 if(p->pc2file && p->pc2file(p, pc-p->base, file, nfile, line) >= 0)
79 werrstr("no source file for 0x%lux", pc);
84 pc2line(u64int pc, ulong *line)
86 char tmp[10]; /* just in case */
87 return pc2file(pc, tmp, sizeof tmp, line);
91 file2pc(char *file, ulong line, u64int *addr)
95 for(p=fhdrlist; p; p=p->next)
96 if(p->file2pc && p->file2pc(p, file, line, addr) >= 0){
100 werrstr("no instructions at %s:%lud", file, line);
105 line2pc(u64int basepc, ulong line, u64int *pc)
109 for(p=fhdrlist; p; p=p->next)
110 if(p->line2pc && p->line2pc(p, basepc-p->base, line, pc) >= 0){
114 werrstr("no instructions on line %lud", line);
119 fnbound(u64int pc, u64int *bounds)
125 for(p=fhdrlist; p; p=p->next){
126 l = locaddr(pc - p->base);
127 if((s = ffindsym(p, l, CANY)) != nil){
128 if(s->loc.type != LADDR){
129 werrstr("function %s has weird location %L", s->name, s->loc);
132 bounds[0] = s->loc.addr + p->base;
133 if(s->hiloc.type != LADDR){
134 werrstr("can't find upper bound for function %s", s->name);
137 bounds[1] = s->hiloc.addr + p->base;
141 werrstr("no function contains 0x%lux", pc);
146 fileline(u64int pc, char *a, uint n)
150 if(pc2file(pc, a, n, &line) < 0)
152 seprint(a+strlen(a), a+n, ":%lud", line);
157 flookupsym(Fhdr *fhdr, char *name)
171 i = strcmp(name, t->name);
179 m += a - fhdr->byname;
181 assert(strcmp(name, a[m]->name) == 0);
182 while(m > 0 && strcmp(name, a[m-1]->name) == 0)
191 flookupsymx(Fhdr *fhdr, char *name)
205 i = strcmp(name, t->xname);
213 m += a - fhdr->byxname;
215 assert(strcmp(name, a[m]->xname) == 0);
216 while(m > 0 && strcmp(name, a[m-1]->xname) == 0)
225 lookupsym(char *fn, char *var, Symbol *s)
235 for(p=fhdrlist; p; p=p->next)
236 if((t=flookupsym(p, nam)) != nil
237 || (t=flookupsymx(p, nam)) != nil){
238 relocsym(&s1, t, p->base);
245 return lookuplsym(&s1, var, s);
250 werrstr("unknown symbol %s%s%s", fn ? fn : "",
251 fn && var ? ":" : "", var ? var : "");
256 findexsym(Fhdr *fp, uint i, Symbol *s)
260 relocsym(s, &fp->sym[i], fp->base);
265 indexsym(uint ndx, Symbol *s)
270 for(p=fhdrlist; p; p=p->next){
275 relocsym(s, &p->sym[ndx], p->base);
283 ffindsym(Fhdr *fhdr, Loc loc, uint class)
291 if(a == nil || n <= 0)
295 * We have a list of possibly duplicate locations in a.
296 * We want to find the largest index i such that
297 * a[i] <= loc. This cannot be done with a simple
298 * binary search. Instead we binary search to find
299 * where the location should be.
305 cmp = loccmp(&loc, &a[i].loc);
306 if(cmp < 0) /* loc < a[i].loc */
308 if(cmp > 0) /* loc > a[i].loc */
314 /* found position where value would go, but not there -- go back one */
321 * might be in a run of all-the-same -- go back to beginning of run.
322 * if runs were long, could binary search for a[i].loc instead.
324 while(i > 0 && loccmp(&a[i-1].loc, &a[i].loc) == 0)
328 if(t->hiloc.type && loccmp(&loc, &t->hiloc) >= 0)
330 if(class != CANY && class != t->class)
336 findsym(Loc loc, uint class, Symbol *s)
347 for(p=fhdrlist; p; p=p->next){
349 l.addr = loc.addr - p->base;
350 if((t = ffindsym(p, l, CANY)) != nil){
351 d = l.addr - t->loc.addr;
352 if(0 <= d && d < 4096)
353 if(best == nil || d < bestd){
361 if(class != CANY && class != best->class)
363 relocsym(s, best, bestp->base);
367 werrstr("could not find symbol at %L", loc);
372 lookuplsym(Symbol *s1, char *name, Symbol *s2)
377 if(p->lookuplsym && p->lookuplsym(p, s1, name, s2) >= 0){
378 relocsym(s2, s2, p->base);
385 indexlsym(Symbol *s1, uint ndx, Symbol *s2)
390 if(p->indexlsym && p->indexlsym(p, s1, ndx, s2) >= 0){
391 relocsym(s2, s2, p->base);
398 findlsym(Symbol *s1, Loc loc, Symbol *s2)
403 if(p->findlsym && p->findlsym(p, s1, loc, s2) >= 0){
404 relocsym(s2, s2, p->base);
411 unwindframe(Map *map, Regs *regs, u64int *next, Symbol *sym)
415 for(p=fhdrlist; p; p=p->next)
416 if(p->unwind && p->unwind(p, map, regs, next, sym) >= 0)
418 if(mach->unwind && mach->unwind(map, regs, next, sym) >= 0)
424 symoff(char *a, uint n, u64int addr, uint class)
431 if(findsym(l, class, &s) < 0 || addr-s.loc.addr >= 4096){
432 snprint(a, n, "%#lux", addr);
435 if(addr != s.loc.addr)
436 snprint(a, n, "%s+%#lx", s.name, addr-s.loc.addr);
438 snprint(a, n, "%s", s.name);
442 /* location, class, name */
444 byloccmp(const void *va, const void *vb)
451 i = loccmp(&a->loc, &b->loc);
454 i = a->class - b->class;
457 return strcmp(a->name, b->name);
460 /* name, location, class */
462 byxnamecmp(const void *va, const void *vb)
469 i = strcmp(a->xname, b->xname);
472 i = strcmp(a->name, b->name);
475 i = loccmp(&a->loc, &b->loc);
478 return a->class - b->class;
481 /* name, location, class */
483 bynamecmp(const void *va, const void *vb)
490 i = strcmp(a->name, b->name);
493 i = loccmp(&a->loc, &b->loc);
496 return a->class - b->class;
505 if(hdr->syminit == 0){
506 werrstr("no debugging symbols");
509 if(hdr->syminit(hdr) < 0)
512 qsort(hdr->sym, hdr->nsym, sizeof(hdr->sym[0]), byloccmp);
513 es = hdr->sym+hdr->nsym;
514 for(r=w=hdr->sym; r<es; r++){
516 && strcmp((w-1)->name, r->name) ==0
517 && loccmp(&(w-1)->loc, &r->loc) == 0){
522 hdr->nsym = w - hdr->sym;
524 hdr->byname = malloc(hdr->nsym*sizeof(hdr->byname[0]));
525 if(hdr->byname == nil){
526 fprint(2, "could not allocate table to sort by name\n");
528 for(i=0; i<hdr->nsym; i++)
529 hdr->byname[i] = &hdr->sym[i];
530 qsort(hdr->byname, hdr->nsym, sizeof(hdr->byname[0]), bynamecmp);
533 hdr->byxname = malloc(hdr->nsym*sizeof(hdr->byxname[0]));
534 if(hdr->byxname == nil){
535 fprint(2, "could not allocate table to sort by xname\n");
537 for(i=0; i<hdr->nsym; i++)
538 hdr->byxname[i] = &hdr->sym[i];
539 qsort(hdr->byxname, hdr->nsym, sizeof(hdr->byxname[0]), byxnamecmp);
558 _addsym(Fhdr *fp, Symbol *sym)
561 static char buf[65536];
564 if(fp->nsym%128 == 0){
565 s = realloc(fp->sym, (fp->nsym+128)*sizeof(fp->sym[0]));
571 fprint(2, "sym %s %c %L\n", sym->name, sym->type, sym->loc);
573 t = demangle(sym->name, buf, 1);
580 s = &fp->sym[fp->nsym++];