2 * functions to read and write an executable or file image
10 static int mget(Map*, uvlong, void*, int);
11 static int mput(Map*, uvlong, void*, int);
12 static struct segment* reloc(Map*, uvlong, vlong*);
15 * routines to get/put various types
18 geta(Map *map, uvlong addr, uvlong *x)
23 if (mach->szaddr == 8){
24 if (get8(map, addr, &vl) < 0)
30 if (get4(map, addr, &l) < 0)
38 get8(Map *map, uvlong addr, uvlong *x)
41 werrstr("get8: invalid map");
45 if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
49 if (mget(map, addr, x, 8) < 0)
51 *x = machdata->swav(*x);
56 get4(Map *map, uvlong addr, ulong *x)
59 werrstr("get4: invalid map");
63 if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
67 if (mget(map, addr, x, 4) < 0)
69 *x = machdata->swal(*x);
74 get2(Map *map, uvlong addr, ushort *x)
77 werrstr("get2: invalid map");
81 if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
85 if (mget(map, addr, x, 2) < 0)
87 *x = machdata->swab(*x);
92 get1(Map *map, uvlong addr, uchar *x, int size)
97 werrstr("get1: invalid map");
101 if (map->nsegs == 1 && map->seg[0].fd < 0 && map->seg[0].read == nil) {
103 while (cp < (uchar*)(&addr+1) && size-- > 0)
108 return mget(map, addr, x, size);
113 puta(Map *map, uvlong addr, uvlong v)
115 if (mach->szaddr == 8)
116 return put8(map, addr, v);
118 return put4(map, addr, v);
122 put8(Map *map, uvlong addr, uvlong v)
125 werrstr("put8: invalid map");
128 v = machdata->swav(v);
129 return mput(map, addr, &v, 8);
133 put4(Map *map, uvlong addr, ulong v)
136 werrstr("put4: invalid map");
139 v = machdata->swal(v);
140 return mput(map, addr, &v, 4);
144 put2(Map *map, uvlong addr, ushort v)
147 werrstr("put2: invalid map");
150 v = machdata->swab(v);
151 return mput(map, addr, &v, 2);
155 put1(Map *map, uvlong addr, uchar *v, int size)
158 werrstr("put1: invalid map");
161 return mput(map, addr, v, size);
165 spread(struct segment *s, void *buf, int n, uvlong off)
176 return s->read(s->fd, buf, n, off);
179 base = off&~(sizeof cache.a-1);
180 if(cache.s != s || cache.off != base){
182 if(seek(s->fd, base, 0) >= 0
183 && readn(s->fd, cache.a, sizeof cache.a) == sizeof cache.a){
188 if(cache.s == s && cache.off == base){
189 off &= sizeof cache.a-1;
190 if(off+n > sizeof cache.a)
191 n = sizeof cache.a - off;
192 memmove(buf, cache.a+off, n);
197 return pread(s->fd, buf, n, off);
201 mget(Map *map, uvlong addr, void *buf, int size)
207 s = reloc(map, addr, (vlong*)&off);
210 if (s->fd < 0 && s->read == nil) {
211 werrstr("unreadable map");
214 for (i = j = 0; i < 2; i++) { /* in case read crosses page */
215 k = spread(s, (void*)((uchar *)buf+j), size-j, off+j);
217 werrstr("can't read address %llux: %r", addr);
224 werrstr("partial read at address %llux (size %d j %d)", addr, size, j);
229 mput(Map *map, uvlong addr, void *buf, int size)
235 s = reloc(map, addr, &off);
239 werrstr("unwritable map");
244 for (i = j = 0; i < 2; i++) { /* in case read crosses page */
245 k = write(s->fd, buf, size-j);
247 werrstr("can't write address %llux: %r", addr);
254 werrstr("partial write at address %llux", addr);
259 * convert address to file offset; returns nonzero if ok
261 static struct segment*
262 reloc(Map *map, uvlong addr, vlong *offp)
266 for (i = 0; i < map->nsegs; i++) {
267 if (map->seg[i].inuse)
268 if (map->seg[i].b <= addr && addr < map->seg[i].e) {
269 addr += map->seg[i].f - map->seg[i].b;
272 * avoid negative file offsets for kernel
273 * addresses by clearing the sign bit.
274 * devproc sign extends back to 64 bit.
283 werrstr("can't translate address %llux", addr);