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)
54 pc2file(ulong pc, char *file, uint nfile, ulong *line)
58 for(p=fhdrlist; p; p=p->next)
59 if(p->pc2file && p->pc2file(p, pc-p->base, file, nfile, line) >= 0)
61 werrstr("no source file for 0x%lux", pc);
66 pc2line(ulong pc, ulong *line)
68 char tmp[10]; /* just in case */
69 return pc2file(pc, tmp, sizeof tmp, line);
73 file2pc(char *file, ulong line, ulong *addr)
77 for(p=fhdrlist; p; p=p->next)
78 if(p->file2pc && p->file2pc(p, file, line, addr) >= 0){
82 werrstr("no instructions at %s:%lud", file, line);
87 line2pc(ulong basepc, ulong line, ulong *pc)
91 for(p=fhdrlist; p; p=p->next)
92 if(p->line2pc && p->line2pc(p, basepc-p->base, line, pc) >= 0){
96 werrstr("no instructions on line %lud", line);
101 fnbound(ulong pc, ulong *bounds)
107 for(p=fhdrlist; p; p=p->next){
108 l = locaddr(pc - p->base);
109 if((s = ffindsym(p, l, CANY)) != nil){
110 if(s->loc.type != LADDR){
111 werrstr("function %s has weird location %L", s->name, s->loc);
114 bounds[0] = s->loc.addr + p->base;
115 if(s->hiloc.type != LADDR){
116 werrstr("can't find upper bound for function %s", s->name);
119 bounds[1] = s->hiloc.addr + p->base;
123 werrstr("no function contains 0x%lux", pc);
128 fileline(ulong pc, char *a, uint n)
132 if(pc2file(pc, a, n, &line) < 0)
134 seprint(a+strlen(a), a+n, ":%lud", line);
139 flookupsym(Fhdr *fhdr, char *name)
153 i = strcmp(name, t->name);
161 m += a - fhdr->byname;
163 assert(strcmp(name, a[m]->name) == 0);
164 while(m > 0 && strcmp(name, a[m-1]->name) == 0)
173 lookupsym(char *fn, char *var, Symbol *s)
183 for(p=fhdrlist; p; p=p->next)
184 if((t=flookupsym(p, nam)) != nil){
185 relocsym(&s1, t, p->base);
191 return lookuplsym(&s1, var, s);
196 werrstr("unknown symbol %s%s%s", fn ? fn : "",
197 fn && var ? ":" : "", var ? var : "");
202 findexsym(Fhdr *fp, uint i, Symbol *s)
206 relocsym(s, &fp->sym[i], fp->base);
211 indexsym(uint ndx, Symbol *s)
216 for(p=fhdrlist; p; p=p->next){
221 relocsym(s, &p->sym[ndx], p->base);
229 ffindsym(Fhdr *fhdr, Loc loc, uint class)
237 if(a == nil || n <= 0)
241 * We have a list of possibly duplicate locations in a.
242 * We want to find the largest index i such that
243 * a[i] <= loc. This cannot be done with a simple
244 * binary search. Instead we binary search to find
245 * where the location should be.
251 cmp = loccmp(&loc, &a[i].loc);
252 if(cmp < 0) /* loc < a[i].loc */
254 if(cmp > 0) /* loc > a[i].loc */
260 /* found position where value would go, but not there -- go back one */
267 * might be in a run of all-the-same -- go back to beginning of run.
268 * if runs were long, could binary search for a[i].loc instead.
270 while(i > 0 && loccmp(&a[i-1].loc, &a[i].loc) == 0)
274 if(t->hiloc.type && loccmp(&loc, &t->hiloc) >= 0)
276 if(class != CANY && class != t->class)
282 findsym(Loc loc, uint class, Symbol *s)
293 for(p=fhdrlist; p; p=p->next){
295 l.addr = loc.addr - p->base;
296 if((t = ffindsym(p, l, CANY)) != nil){
297 d = l.addr - t->loc.addr;
298 if(0 <= d && d < 4096)
299 if(best == nil || d < bestd){
307 if(class != CANY && class != best->class)
309 relocsym(s, best, bestp->base);
313 werrstr("could not find symbol at %L", loc);
318 lookuplsym(Symbol *s1, char *name, Symbol *s2)
323 if(p->lookuplsym && p->lookuplsym(p, s1, name, s2) >= 0){
324 relocsym(s2, s2, p->base);
331 indexlsym(Symbol *s1, uint ndx, Symbol *s2)
336 if(p->indexlsym && p->indexlsym(p, s1, ndx, s2) >= 0){
337 relocsym(s2, s2, p->base);
344 findlsym(Symbol *s1, Loc loc, Symbol *s2)
349 if(p->findlsym && p->findlsym(p, s1, loc, s2) >= 0){
350 relocsym(s2, s2, p->base);
357 unwindframe(Map *map, Regs *regs, ulong *next)
361 for(p=fhdrlist; p; p=p->next)
362 if(p->unwind && p->unwind(p, map, regs, next) >= 0)
364 if(mach->unwind && mach->unwind(map, regs, next) >= 0)
370 symoff(char *a, uint n, ulong addr, uint class)
377 if(findsym(l, class, &s) < 0 || addr-s.loc.addr >= 4096){
378 snprint(a, n, "%lux", addr);
381 if(addr != s.loc.addr)
382 snprint(a, n, "%s+%ld", s.name, addr-s.loc.addr);
384 snprint(a, n, "%s", s.name);
388 /* location, class, name */
390 byloccmp(const void *va, const void *vb)
397 i = loccmp(&a->loc, &b->loc);
400 i = a->class - b->class;
403 return strcmp(a->name, b->name);
406 /* name, location, class */
408 bynamecmp(const void *va, const void *vb)
415 i = strcmp(a->name, b->name);
418 i = loccmp(&a->loc, &b->loc);
421 return a->class - b->class;
430 if(hdr->syminit == nil){
431 werrstr("no debugging symbols");
434 if(hdr->syminit(hdr) < 0)
437 qsort(hdr->sym, hdr->nsym, sizeof(hdr->sym[0]), byloccmp);
438 es = hdr->sym+hdr->nsym;
439 for(r=w=hdr->sym; r<es; r++){
441 && strcmp((w-1)->name, r->name) ==0
442 && loccmp(&(w-1)->loc, &r->loc) == 0){
447 hdr->nsym = w - hdr->sym;
449 hdr->byname = malloc(hdr->nsym*sizeof(hdr->byname[0]));
450 if(hdr->byname == nil){
451 fprint(2, "could not allocate table to sort by location\n");
453 for(i=0; i<hdr->nsym; i++)
454 hdr->byname[i] = &hdr->sym[i];
455 qsort(hdr->byname, hdr->nsym, sizeof(hdr->byname[0]), bynamecmp);
461 addsym(Fhdr *fp, Symbol *sym)
465 if(fp->nsym%128 == 0){
466 s = realloc(fp->sym, (fp->nsym+128)*sizeof(fp->sym[0]));
472 fprint(2, "sym %s %c %L\n", sym->name, sym->type, sym->loc);
474 s = &fp->sym[fp->nsym++];