Print this page
patch fix-lint
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdisasm/common/dis_i386.c
+++ new/usr/src/lib/libdisasm/common/dis_i386.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
26 + * Copyright 2014 Nexenta Systems, Inc. All rights reserved.
26 27 */
27 28
28 29 #include <libdisasm.h>
29 -#include <stdlib.h>
30 -#include <stdio.h>
31 30
32 31 #include "dis_tables.h"
33 32 #include "libdisasm_impl.h"
34 33
35 34 typedef struct dis_handle_i386 {
36 35 int dhx_mode;
37 36 dis86_t dhx_dis;
38 37 uint64_t dhx_end;
39 38 } dis_handle_i386_t;
40 39
41 40 /*
42 41 * Returns true if we are near the end of a function. This is a cheap hack at
43 42 * detecting NULL padding between functions. If we're within a few bytes of the
44 43 * next function, or past the start, then return true.
45 44 */
46 45 static int
47 46 check_func(void *data)
48 47 {
49 48 dis_handle_t *dhp = data;
50 49 uint64_t start;
51 50 size_t len;
52 51
53 52 if (dhp->dh_lookup(dhp->dh_data, dhp->dh_addr, NULL, 0, &start, &len)
54 53 != 0)
55 54 return (0);
56 55
57 56 if (start < dhp->dh_addr)
58 57 return (dhp->dh_addr > start + len - 0x10);
59 58
60 59 return (1);
61 60 }
62 61
63 62 static int
64 63 get_byte(void *data)
65 64 {
66 65 uchar_t byte;
67 66 dis_handle_t *dhp = data;
68 67
69 68 if (dhp->dh_read(dhp->dh_data, dhp->dh_addr, &byte, sizeof (byte)) !=
70 69 sizeof (byte))
71 70 return (-1);
72 71
73 72 dhp->dh_addr++;
74 73
75 74 return ((int)byte);
76 75 }
77 76
78 77 static int
79 78 do_lookup(void *data, uint64_t addr, char *buf, size_t buflen)
80 79 {
81 80 dis_handle_t *dhp = data;
82 81
83 82 return (dhp->dh_lookup(dhp->dh_data, addr, buf, buflen, NULL, NULL));
84 83 }
85 84
86 85 static void
87 86 dis_i386_handle_detach(dis_handle_t *dhp)
88 87 {
89 88 dis_free(dhp->dh_arch_private, sizeof (dis_handle_i386_t));
90 89 dhp->dh_arch_private = NULL;
91 90 }
92 91
93 92 static int
94 93 dis_i386_handle_attach(dis_handle_t *dhp)
95 94 {
96 95 dis_handle_i386_t *dhx;
97 96
98 97 /*
99 98 * Validate architecture flags
100 99 */
101 100 if (dhp->dh_flags & ~(DIS_X86_SIZE16 | DIS_X86_SIZE32 | DIS_X86_SIZE64 |
102 101 DIS_OCTAL | DIS_NOIMMSYM)) {
103 102 (void) dis_seterrno(E_DIS_INVALFLAG);
104 103 return (-1);
105 104 }
106 105
107 106 /*
108 107 * Create and initialize the internal structure
109 108 */
110 109 if ((dhx = dis_zalloc(sizeof (dis_handle_i386_t))) == NULL) {
111 110 (void) dis_seterrno(E_DIS_NOMEM);
112 111 return (-1);
113 112 }
114 113 dhp->dh_arch_private = dhx;
115 114
116 115 /*
117 116 * Initialize x86-specific architecture structure
118 117 */
↓ open down ↓ |
78 lines elided |
↑ open up ↑ |
119 118 if (dhp->dh_flags & DIS_X86_SIZE16)
120 119 dhx->dhx_mode = SIZE16;
121 120 else if (dhp->dh_flags & DIS_X86_SIZE64)
122 121 dhx->dhx_mode = SIZE64;
123 122 else
124 123 dhx->dhx_mode = SIZE32;
125 124
126 125 if (dhp->dh_flags & DIS_OCTAL)
127 126 dhx->dhx_dis.d86_flags = DIS_F_OCTAL;
128 127
129 - dhx->dhx_dis.d86_sprintf_func = snprintf;
128 + dhx->dhx_dis.d86_sprintf_func = dis_snprintf;
130 129 dhx->dhx_dis.d86_get_byte = get_byte;
131 130 dhx->dhx_dis.d86_sym_lookup = do_lookup;
132 131 dhx->dhx_dis.d86_check_func = check_func;
133 132
134 133 dhx->dhx_dis.d86_data = dhp;
135 134
136 135 return (0);
137 136 }
138 137
139 138 static int
140 139 dis_i386_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf,
141 140 size_t buflen)
142 141 {
143 142 dis_handle_i386_t *dhx = dhp->dh_arch_private;
144 143 dhp->dh_addr = addr;
145 144
146 145 /* DIS_NOIMMSYM might not be set until now, so update */
147 146 if (dhp->dh_flags & DIS_NOIMMSYM)
148 147 dhx->dhx_dis.d86_flags |= DIS_F_NOIMMSYM;
149 148 else
150 149 dhx->dhx_dis.d86_flags &= ~DIS_F_NOIMMSYM;
151 150
152 151 if (dtrace_disx86(&dhx->dhx_dis, dhx->dhx_mode) != 0)
153 152 return (-1);
154 153
155 154 if (buf != NULL)
156 155 dtrace_disx86_str(&dhx->dhx_dis, dhx->dhx_mode, addr, buf,
157 156 buflen);
158 157
159 158 return (0);
160 159 }
161 160
162 161 /* ARGSUSED */
163 162 static int
164 163 dis_i386_max_instrlen(dis_handle_t *dhp)
165 164 {
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
166 165 return (15);
167 166 }
168 167
169 168 /* ARGSUSED */
170 169 static int
171 170 dis_i386_min_instrlen(dis_handle_t *dhp)
172 171 {
173 172 return (1);
174 173 }
175 174
176 -#define MIN(a, b) ((a) < (b) ? (a) : (b))
177 -
178 175 /*
179 176 * Return the previous instruction. On x86, we have no choice except to
180 177 * disassemble everything from the start of the symbol, and stop when we have
181 178 * reached our instruction address. If we're not in the middle of a known
182 179 * symbol, then we return the same address to indicate failure.
183 180 */
184 181 static uint64_t
185 182 dis_i386_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
186 183 {
187 184 uint64_t *hist, addr, start;
188 185 int cur, nseen;
189 186 uint64_t res = pc;
190 187
191 188 if (n <= 0)
192 189 return (pc);
193 190
194 191 if (dhp->dh_lookup(dhp->dh_data, pc, NULL, 0, &start, NULL) != 0 ||
195 192 start == pc)
196 193 return (res);
197 194
198 195 hist = dis_zalloc(sizeof (uint64_t) * n);
199 196
200 197 for (cur = 0, nseen = 0, addr = start; addr < pc; addr = dhp->dh_addr) {
201 198 hist[cur] = addr;
202 199 cur = (cur + 1) % n;
203 200 nseen++;
204 201
205 202 /* if we cannot make forward progress, give up */
206 203 if (dis_disassemble(dhp, addr, NULL, 0) != 0)
207 204 goto done;
208 205 }
209 206
210 207 if (addr != pc) {
211 208 /*
212 209 * We scanned past %pc, but didn't find an instruction that
213 210 * started at %pc. This means that either the caller specified
214 211 * an invalid address, or we ran into something other than code
215 212 * during our scan. Virtually any combination of bytes can be
216 213 * construed as a valid Intel instruction, so any non-code bytes
217 214 * we encounter will have thrown off the scan.
218 215 */
219 216 goto done;
220 217 }
221 218
222 219 res = hist[(cur + n - MIN(n, nseen)) % n];
223 220
224 221 done:
225 222 dis_free(hist, sizeof (uint64_t) * n);
226 223 return (res);
227 224 }
228 225
229 226 static int
230 227 dis_i386_supports_flags(int flags)
231 228 {
232 229 int archflags = flags & DIS_ARCH_MASK;
233 230
234 231 if (archflags == DIS_X86_SIZE16 || archflags == DIS_X86_SIZE32 ||
235 232 archflags == DIS_X86_SIZE64)
236 233 return (1);
237 234
238 235 return (0);
239 236 }
240 237
241 238 static int
242 239 dis_i386_instrlen(dis_handle_t *dhp, uint64_t pc)
243 240 {
244 241 if (dis_disassemble(dhp, pc, NULL, 0) != 0)
245 242 return (-1);
246 243
247 244 return (dhp->dh_addr - pc);
248 245 }
249 246
250 247 dis_arch_t dis_arch_i386 = {
251 248 dis_i386_supports_flags,
252 249 dis_i386_handle_attach,
253 250 dis_i386_handle_detach,
254 251 dis_i386_disassemble,
255 252 dis_i386_previnstr,
256 253 dis_i386_min_instrlen,
257 254 dis_i386_max_instrlen,
258 255 dis_i386_instrlen,
259 256 };
↓ open down ↓ |
72 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX