xref: /plan9/sys/src/libmach/access.c (revision 9b943567965ba040fd275927fbe088656eb8ce4f)
1 /*
2  * functions to read and write an executable or file image
3  */
4 
5 #include <u.h>
6 #include <libc.h>
7 #include <bio.h>
8 #include <mach.h>
9 
10 static	int	mget(Map*, ulong, char*, int);
11 static	int	mput(Map*, ulong, char*, int);
12 static	struct	segment*	reloc(Map*, ulong, long*);
13 
14 /*
15  * routines to get/put various types
16  */
17 
18 int
19 get8(Map *map, ulong addr, vlong *x)
20 {
21 	if (!map) {
22 		werrstr("get8: invalid map");
23 		return -1;
24 	}
25 
26 	if (map->nsegs == 1 && map->seg[0].fd < 0) {
27 		*x = (vlong)addr;
28 		return 1;
29 	}
30 	if (mget(map, addr, (char *)x, 8) < 0)
31 		return -1;
32 	*x = machdata->swav(*x);
33 	return (1);
34 }
35 
36 int
37 get4(Map *map, ulong addr, long *x)
38 {
39 	if (!map) {
40 		werrstr("get4: invalid map");
41 		return -1;
42 	}
43 
44 	if (map->nsegs == 1 && map->seg[0].fd < 0) {
45 		*x = addr;
46 		return 1;
47 	}
48 	if (mget(map, addr, (char *)x, 4) < 0)
49 		return -1;
50 	*x = machdata->swal(*x);
51 	return (1);
52 }
53 
54 int
55 get2(Map *map, ulong addr, ushort *x)
56 {
57 	if (!map) {
58 		werrstr("get2: invalid map");
59 		return -1;
60 	}
61 
62 	if (map->nsegs == 1 && map->seg[0].fd < 0) {
63 		*x = addr;
64 		return 1;
65 	}
66 	if (mget(map, addr, (char *)x, 2) < 0)
67 		return -1;
68 	*x = machdata->swab(*x);
69 	return (1);
70 }
71 
72 int
73 get1(Map *map, ulong addr, uchar *x, int size)
74 {
75 	uchar *cp;
76 
77 	if (!map) {
78 		werrstr("get1: invalid map");
79 		return -1;
80 	}
81 
82 	if (map->nsegs == 1 && map->seg[0].fd < 0) {
83 		cp = (uchar*)&addr;
84 		while (cp < (uchar*)(&addr+1) && size-- > 0)
85 			*x++ = *cp++;
86 		while (size-- > 0)
87 			*x++ = 0;
88 	} else
89 		return mget(map, addr, (char*)x, size);
90 	return 1;
91 }
92 
93 int
94 put8(Map *map, ulong addr, vlong v)
95 {
96 	if (!map) {
97 		werrstr("put8: invalid map");
98 		return -1;
99 	}
100 	v = machdata->swav(v);
101 	return mput(map, addr, (char *)&v, 8);
102 }
103 
104 int
105 put4(Map *map, ulong addr, long v)
106 {
107 	if (!map) {
108 		werrstr("put4: invalid map");
109 		return -1;
110 	}
111 	v = machdata->swal(v);
112 	return mput(map, addr, (char *)&v, 4);
113 }
114 
115 int
116 put2(Map *map, ulong addr, ushort v)
117 {
118 	if (!map) {
119 		werrstr("put2: invalid map");
120 		return -1;
121 	}
122 	v = machdata->swab(v);
123 	return mput(map, addr, (char *)&v, 2);
124 }
125 
126 int
127 put1(Map *map, ulong addr, uchar *v, int size)
128 {
129 	if (!map) {
130 		werrstr("put1: invalid map");
131 		return -1;
132 	}
133 	return mput(map, addr, (char *)v, size);
134 }
135 
136 static int
137 spread(struct segment *s, char *buf, int n, ulong off)
138 {
139 	ulong base;
140 
141 	static struct {
142 		struct segment *s;
143 		char a[8192];
144 		ulong off;
145 	} cache;
146 
147 	if(s->cache){
148 		base = off&~(sizeof cache.a-1);
149 		if(cache.s != s || cache.off != base){
150 			cache.off = ~0;
151 			if(seek(s->fd, base, 0) >= 0
152 			&& readn(s->fd, cache.a, sizeof cache.a) == sizeof cache.a){
153 				cache.s = s;
154 				cache.off = base;
155 			}
156 		}
157 		if(cache.s == s && cache.off == base){
158 			off &= sizeof cache.a-1;
159 			if(off+n > sizeof cache.a)
160 				n = sizeof cache.a - off;
161 			memmove(buf, cache.a+off, n);
162 			return n;
163 		}
164 	}
165 
166 	return pread(s->fd, buf, n, off);
167 }
168 
169 static int
170 mget(Map *map, ulong addr, char *buf, int size)
171 {
172 	long off;
173 	uvlong voff;
174 	int i, j, k;
175 	struct segment *s;
176 
177 	s = reloc(map, addr, &off);
178 	if (!s)
179 		return -1;
180 	if (s->fd < 0) {
181 		werrstr("unreadable map");
182 		return -1;
183 	}
184 	voff = (ulong)off;
185 	for (i = j = 0; i < 2; i++) {	/* in case read crosses page */
186 		k = spread(s, buf, size-j, voff+j);
187 		if (k < 0) {
188 			werrstr("can't read address %lux: %r", addr);
189 			return -1;
190 		}
191 		j += k;
192 		if (j == size)
193 			return j;
194 	}
195 	werrstr("partial read at address %lux", addr);
196 	return -1;
197 }
198 
199 static int
200 mput(Map *map, ulong addr, char *buf, int size)
201 {
202 	long off;
203 	vlong voff;
204 	int i, j, k;
205 	struct segment *s;
206 
207 	s = reloc(map, addr, &off);
208 	if (!s)
209 		return -1;
210 	if (s->fd < 0) {
211 		werrstr("unwritable map");
212 		return -1;
213 	}
214 
215 	voff = (ulong)off;
216 	seek(s->fd, voff, 0);
217 	for (i = j = 0; i < 2; i++) {	/* in case read crosses page */
218 		k = write(s->fd, buf, size-j);
219 		if (k < 0) {
220 			werrstr("can't write address %lux: %r", addr);
221 			return -1;
222 		}
223 		j += k;
224 		if (j == size)
225 			return j;
226 	}
227 	werrstr("partial write at address %lux", addr);
228 	return -1;
229 }
230 
231 /*
232  *	convert address to file offset; returns nonzero if ok
233  */
234 static struct segment*
235 reloc(Map *map, ulong addr, long *offp)
236 {
237 	int i;
238 
239 	for (i = 0; i < map->nsegs; i++) {
240 		if (map->seg[i].inuse)
241 		if (map->seg[i].b <= addr && addr < map->seg[i].e) {
242 			*offp = addr + map->seg[i].f - map->seg[i].b;
243 			return &map->seg[i];
244 		}
245 	}
246 	werrstr("can't translate address %lux", addr);
247 	return 0;
248 }
249