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(ulong 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(ulong pc, ulong *line)
86 char tmp[10]; /* just in case */
87 return pc2file(pc, tmp, sizeof tmp, line);
91 file2pc(char *file, ulong line, ulong *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(ulong basepc, ulong line, ulong *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(ulong pc, ulong *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(ulong 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 lookupsym(char *fn, char *var, Symbol *s)
201 for(p=fhdrlist; p; p=p->next)
202 if((t=flookupsym(p, nam)) != nil){
203 relocsym(&s1, t, p->base);
209 return lookuplsym(&s1, var, s);
214 werrstr("unknown symbol %s%s%s", fn ? fn : "",
215 fn && var ? ":" : "", var ? var : "");
220 findexsym(Fhdr *fp, uint i, Symbol *s)
224 relocsym(s, &fp->sym[i], fp->base);
229 indexsym(uint ndx, Symbol *s)
234 for(p=fhdrlist; p; p=p->next){
239 relocsym(s, &p->sym[ndx], p->base);
247 ffindsym(Fhdr *fhdr, Loc loc, uint class)
255 if(a == nil || n <= 0)
259 * We have a list of possibly duplicate locations in a.
260 * We want to find the largest index i such that
261 * a[i] <= loc. This cannot be done with a simple
262 * binary search. Instead we binary search to find
263 * where the location should be.
269 cmp = loccmp(&loc, &a[i].loc);
270 if(cmp < 0) /* loc < a[i].loc */
272 if(cmp > 0) /* loc > a[i].loc */
278 /* found position where value would go, but not there -- go back one */
285 * might be in a run of all-the-same -- go back to beginning of run.
286 * if runs were long, could binary search for a[i].loc instead.
288 while(i > 0 && loccmp(&a[i-1].loc, &a[i].loc) == 0)
292 if(t->hiloc.type && loccmp(&loc, &t->hiloc) >= 0)
294 if(class != CANY && class != t->class)
300 findsym(Loc loc, uint class, Symbol *s)
311 for(p=fhdrlist; p; p=p->next){
313 l.addr = loc.addr - p->base;
314 if((t = ffindsym(p, l, CANY)) != nil){
315 d = l.addr - t->loc.addr;
316 if(0 <= d && d < 4096)
317 if(best == nil || d < bestd){
325 if(class != CANY && class != best->class)
327 relocsym(s, best, bestp->base);
331 werrstr("could not find symbol at %L", loc);
336 lookuplsym(Symbol *s1, char *name, Symbol *s2)
341 if(p->lookuplsym && p->lookuplsym(p, s1, name, s2) >= 0){
342 relocsym(s2, s2, p->base);
349 indexlsym(Symbol *s1, uint ndx, Symbol *s2)
354 if(p->indexlsym && p->indexlsym(p, s1, ndx, s2) >= 0){
355 relocsym(s2, s2, p->base);
362 findlsym(Symbol *s1, Loc loc, Symbol *s2)
367 if(p->findlsym && p->findlsym(p, s1, loc, s2) >= 0){
368 relocsym(s2, s2, p->base);
375 unwindframe(Map *map, Regs *regs, ulong *next, Symbol *sym)
379 for(p=fhdrlist; p; p=p->next)
380 if(p->unwind && p->unwind(p, map, regs, next, sym) >= 0)
382 if(mach->unwind && mach->unwind(map, regs, next, sym) >= 0)
388 symoff(char *a, uint n, ulong addr, uint class)
395 if(findsym(l, class, &s) < 0 || addr-s.loc.addr >= 4096){
396 snprint(a, n, "%#lux", addr);
399 if(addr != s.loc.addr)
400 snprint(a, n, "%s+%#lx", s.name, addr-s.loc.addr);
402 snprint(a, n, "%s", s.name);
406 /* location, class, name */
408 byloccmp(const void *va, const void *vb)
415 i = loccmp(&a->loc, &b->loc);
418 i = a->class - b->class;
421 return strcmp(a->name, b->name);
424 /* name, location, class */
426 bynamecmp(const void *va, const void *vb)
433 i = strcmp(a->name, b->name);
436 i = loccmp(&a->loc, &b->loc);
439 return a->class - b->class;
448 if(hdr->syminit == 0){
449 werrstr("no debugging symbols");
452 if(hdr->syminit(hdr) < 0)
455 qsort(hdr->sym, hdr->nsym, sizeof(hdr->sym[0]), byloccmp);
456 es = hdr->sym+hdr->nsym;
457 for(r=w=hdr->sym; r<es; r++){
459 && strcmp((w-1)->name, r->name) ==0
460 && loccmp(&(w-1)->loc, &r->loc) == 0){
465 hdr->nsym = w - hdr->sym;
467 hdr->byname = malloc(hdr->nsym*sizeof(hdr->byname[0]));
468 if(hdr->byname == nil){
469 fprint(2, "could not allocate table to sort by location\n");
471 for(i=0; i<hdr->nsym; i++)
472 hdr->byname[i] = &hdr->sym[i];
473 qsort(hdr->byname, hdr->nsym, sizeof(hdr->byname[0]), bynamecmp);
492 _addsym(Fhdr *fp, Symbol *sym)
496 if(fp->nsym%128 == 0){
497 s = realloc(fp->sym, (fp->nsym+128)*sizeof(fp->sym[0]));
503 fprint(2, "sym %s %c %L\n", sym->name, sym->type, sym->loc);
505 s = &fp->sym[fp->nsym++];