libmach: support disassembling from memory

This commit is contained in:
aiju 2017-08-24 13:02:27 +00:00
parent 721160290f
commit 6104ebc9b6
2 changed files with 9 additions and 5 deletions

View file

@ -130,6 +130,7 @@ struct Map {
struct segment { /* per-segment map */ struct segment { /* per-segment map */
char *name; /* the segment name */ char *name; /* the segment name */
int fd; /* file descriptor */ int fd; /* file descriptor */
long (*read)(int, void *, long, vlong);
int inuse; /* in use - not in use */ int inuse; /* in use - not in use */
int cache; /* should cache reads? */ int cache; /* should cache reads? */
uvlong b; /* base */ uvlong b; /* base */

View file

@ -42,7 +42,7 @@ get8(Map *map, uvlong addr, uvlong *x)
return -1; return -1;
} }
if (map->nsegs == 1 && map->seg[0].fd < 0) { if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
*x = addr; *x = addr;
return 1; return 1;
} }
@ -60,7 +60,7 @@ get4(Map *map, uvlong addr, ulong *x)
return -1; return -1;
} }
if (map->nsegs == 1 && map->seg[0].fd < 0) { if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
*x = addr; *x = addr;
return 1; return 1;
} }
@ -78,7 +78,7 @@ get2(Map *map, uvlong addr, ushort *x)
return -1; return -1;
} }
if (map->nsegs == 1 && map->seg[0].fd < 0) { if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
*x = addr; *x = addr;
return 1; return 1;
} }
@ -98,7 +98,7 @@ get1(Map *map, uvlong addr, uchar *x, int size)
return -1; return -1;
} }
if (map->nsegs == 1 && map->seg[0].fd < 0) { if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
cp = (uchar*)&addr; cp = (uchar*)&addr;
while (cp < (uchar*)(&addr+1) && size-- > 0) while (cp < (uchar*)(&addr+1) && size-- > 0)
*x++ = *cp++; *x++ = *cp++;
@ -172,6 +172,9 @@ spread(struct segment *s, void *buf, int n, uvlong off)
uvlong off; uvlong off;
} cache; } cache;
if(s->read != nil)
return s->read(s->fd, buf, n, off);
if(s->cache){ if(s->cache){
base = off&~(sizeof cache.a-1); base = off&~(sizeof cache.a-1);
if(cache.s != s || cache.off != base){ if(cache.s != s || cache.off != base){
@ -204,7 +207,7 @@ mget(Map *map, uvlong addr, void *buf, int size)
s = reloc(map, addr, (vlong*)&off); s = reloc(map, addr, (vlong*)&off);
if (!s) if (!s)
return -1; return -1;
if (s->fd < 0) { if (s->fd < 0 && s->read == nil) {
werrstr("unreadable map"); werrstr("unreadable map");
return -1; return -1;
} }