From 6104ebc9b6f1805a827c0a2acae8978a3fa33603 Mon Sep 17 00:00:00 2001 From: aiju Date: Thu, 24 Aug 2017 13:02:27 +0000 Subject: [PATCH] libmach: support disassembling from memory --- sys/include/mach.h | 1 + sys/src/libmach/access.c | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/sys/include/mach.h b/sys/include/mach.h index 51397fcb8..78c3ab062 100644 --- a/sys/include/mach.h +++ b/sys/include/mach.h @@ -130,6 +130,7 @@ struct Map { struct segment { /* per-segment map */ char *name; /* the segment name */ int fd; /* file descriptor */ + long (*read)(int, void *, long, vlong); int inuse; /* in use - not in use */ int cache; /* should cache reads? */ uvlong b; /* base */ diff --git a/sys/src/libmach/access.c b/sys/src/libmach/access.c index 6d8ed1f3e..ff84d777e 100644 --- a/sys/src/libmach/access.c +++ b/sys/src/libmach/access.c @@ -42,7 +42,7 @@ get8(Map *map, uvlong addr, uvlong *x) 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; return 1; } @@ -60,7 +60,7 @@ get4(Map *map, uvlong addr, ulong *x) 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; return 1; } @@ -78,7 +78,7 @@ get2(Map *map, uvlong addr, ushort *x) 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; return 1; } @@ -98,7 +98,7 @@ get1(Map *map, uvlong addr, uchar *x, int size) 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; while (cp < (uchar*)(&addr+1) && size-- > 0) *x++ = *cp++; @@ -171,6 +171,9 @@ spread(struct segment *s, void *buf, int n, uvlong off) char a[8192]; uvlong off; } cache; + + if(s->read != nil) + return s->read(s->fd, buf, n, off); if(s->cache){ base = off&~(sizeof cache.a-1); @@ -204,7 +207,7 @@ mget(Map *map, uvlong addr, void *buf, int size) s = reloc(map, addr, (vlong*)&off); if (!s) return -1; - if (s->fd < 0) { + if (s->fd < 0 && s->read == nil) { werrstr("unreadable map"); return -1; }