Fri, 26 Jul 2013 10:12:15 +0200
8019926: PPC64 (part 106): Make hsdis build and work on Linux/PPC64
Summary: Make hsdis work on Linux/PPC64 and AIX/PPC64
Reviewed-by: kvn
1 /*
2 * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
25 /* hsdis.c -- dump a range of addresses as native instructions
26 This implements the plugin protocol required by the
27 HotSpot PrintAssembly option.
28 */
30 #include <config.h> /* required by bfd.h */
31 #include <libiberty.h>
32 #include <bfd.h>
33 #include <dis-asm.h>
34 #include <inttypes.h>
35 #include <string.h>
36 #include <errno.h>
37 #include "hsdis.h"
39 #ifndef bool
40 #define bool int
41 #define true 1
42 #define false 0
43 #endif /*bool*/
45 /* short names for stuff in hsdis.h */
46 typedef decode_instructions_event_callback_ftype event_callback_t;
47 typedef decode_instructions_printf_callback_ftype printf_callback_t;
49 /* disassemble_info.application_data object */
50 struct hsdis_app_data {
51 /* virtual address of data */
52 uintptr_t start_va, end_va;
53 /* the instructions to be decoded */
54 unsigned char* buffer;
55 uintptr_t length;
56 event_callback_t event_callback; void* event_stream;
57 printf_callback_t printf_callback; void* printf_stream;
58 bool losing;
59 bool do_newline;
61 /* the architecture being disassembled */
62 const char* arch_name;
63 const bfd_arch_info_type* arch_info;
65 /* the disassembler we are going to use: */
66 disassembler_ftype dfn;
67 struct disassemble_info dinfo; /* the actual struct! */
69 char mach_option[64];
70 char insn_options[256];
71 };
73 static void* decode(struct hsdis_app_data* app_data, const char* options);
75 #define DECL_APP_DATA(dinfo) \
76 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
78 #define DECL_EVENT_CALLBACK(app_data) \
79 event_callback_t event_callback = (app_data)->event_callback; \
80 void* event_stream = (app_data)->event_stream
82 #define DECL_PRINTF_CALLBACK(app_data) \
83 printf_callback_t printf_callback = (app_data)->printf_callback; \
84 void* printf_stream = (app_data)->printf_stream
87 static void print_help(struct hsdis_app_data* app_data,
88 const char* msg, const char* arg);
89 static void setup_app_data(struct hsdis_app_data* app_data,
90 const char* options);
91 static const char* format_insn_close(const char* close,
92 disassemble_info* dinfo,
93 char* buf, size_t bufsize);
95 void*
96 #ifdef DLL_ENTRY
97 DLL_ENTRY
98 #endif
99 decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
100 unsigned char* buffer, uintptr_t length,
101 event_callback_t event_callback_arg, void* event_stream_arg,
102 printf_callback_t printf_callback_arg, void* printf_stream_arg,
103 const char* options, int newline) {
104 struct hsdis_app_data app_data;
105 memset(&app_data, 0, sizeof(app_data));
106 app_data.start_va = start_va;
107 app_data.end_va = end_va;
108 app_data.buffer = buffer;
109 app_data.length = length;
110 app_data.event_callback = event_callback_arg;
111 app_data.event_stream = event_stream_arg;
112 app_data.printf_callback = printf_callback_arg;
113 app_data.printf_stream = printf_stream_arg;
114 app_data.do_newline = newline == 0 ? false : true;
116 return decode(&app_data, options);
117 }
119 /* This is the compatability interface for older version of hotspot */
120 void*
121 #ifdef DLL_ENTRY
122 DLL_ENTRY
123 #endif
124 decode_instructions(void* start_pv, void* end_pv,
125 event_callback_t event_callback_arg, void* event_stream_arg,
126 printf_callback_t printf_callback_arg, void* printf_stream_arg,
127 const char* options) {
128 decode_instructions_virtual((uintptr_t)start_pv,
129 (uintptr_t)end_pv,
130 (unsigned char*)start_pv,
131 (uintptr_t)end_pv - (uintptr_t)start_pv,
132 event_callback_arg,
133 event_stream_arg,
134 printf_callback_arg,
135 printf_stream_arg,
136 options, false);
137 }
139 static void* decode(struct hsdis_app_data* app_data, const char* options) {
140 setup_app_data(app_data, options);
141 char buf[128];
143 {
144 /* now reload everything from app_data: */
145 DECL_EVENT_CALLBACK(app_data);
146 DECL_PRINTF_CALLBACK(app_data);
147 uintptr_t start = app_data->start_va;
148 uintptr_t end = app_data->end_va;
149 uintptr_t p = start;
151 (*event_callback)(event_stream, "insns", (void*)start);
153 (*event_callback)(event_stream, "mach name='%s'",
154 (void*) app_data->arch_info->printable_name);
155 if (app_data->dinfo.bytes_per_line != 0) {
156 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
157 (void*)(intptr_t) app_data->dinfo.bytes_per_line);
158 }
160 while (p < end && !app_data->losing) {
161 (*event_callback)(event_stream, "insn", (void*) p);
163 /* reset certain state, so we can read it with confidence */
164 app_data->dinfo.insn_info_valid = 0;
165 app_data->dinfo.branch_delay_insns = 0;
166 app_data->dinfo.data_size = 0;
167 app_data->dinfo.insn_type = 0;
169 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
171 if (size > 0) p += size;
172 else app_data->losing = true;
174 if (!app_data->losing) {
175 const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
176 buf, sizeof(buf));
177 (*event_callback)(event_stream, insn_close, (void*) p);
179 if (app_data->do_newline) {
180 /* follow each complete insn by a nice newline */
181 (*printf_callback)(printf_stream, "\n");
182 }
183 }
184 }
186 if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
187 return (void*) p;
188 }
189 }
191 /* take the address of the function, for luck, and also test the typedef: */
192 const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
193 const decode_func_stype decode_func_address = &decode_instructions;
195 static const char* format_insn_close(const char* close,
196 disassemble_info* dinfo,
197 char* buf, size_t bufsize) {
198 if (!dinfo->insn_info_valid)
199 return close;
200 enum dis_insn_type itype = dinfo->insn_type;
201 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
202 if ((itype == dis_nonbranch && (dsize | delays) == 0)
203 || (strlen(close) + 3*20 > bufsize))
204 return close;
206 const char* type = "unknown";
207 switch (itype) {
208 case dis_nonbranch: type = NULL; break;
209 case dis_branch: type = "branch"; break;
210 case dis_condbranch: type = "condbranch"; break;
211 case dis_jsr: type = "jsr"; break;
212 case dis_condjsr: type = "condjsr"; break;
213 case dis_dref: type = "dref"; break;
214 case dis_dref2: type = "dref2"; break;
215 }
217 strcpy(buf, close);
218 char* p = buf;
219 if (type) sprintf(p += strlen(p), " type='%s'", type);
220 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
221 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
222 return buf;
223 }
225 /* handler functions */
227 static int
228 hsdis_read_memory_func(bfd_vma memaddr,
229 bfd_byte* myaddr,
230 unsigned int length,
231 struct disassemble_info* dinfo) {
232 DECL_APP_DATA(dinfo);
233 /* convert the virtual address memaddr into an address within memory buffer */
234 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
235 if (offset + length > app_data->length) {
236 /* read is out of bounds */
237 return EIO;
238 } else {
239 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
240 return 0;
241 }
242 }
244 static void
245 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
246 /* the actual value to print: */
247 void* addr_value = (void*) (uintptr_t) vma;
248 DECL_APP_DATA(dinfo);
249 DECL_EVENT_CALLBACK(app_data);
251 /* issue the event: */
252 void* result =
253 (*event_callback)(event_stream, "addr/", addr_value);
254 if (result == NULL) {
255 /* event declined */
256 generic_print_address(vma, dinfo);
257 }
258 }
261 /* configuration */
263 static void set_optional_callbacks(struct hsdis_app_data* app_data);
264 static void parse_caller_options(struct hsdis_app_data* app_data,
265 const char* caller_options);
266 static const char* native_arch_name();
267 static enum bfd_endian native_endian();
268 static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
269 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
270 /* to avoid malloc: */
271 bfd* empty_bfd, bfd_target* empty_xvec);
272 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
273 void *stream,
274 fprintf_ftype fprintf_func,
275 bfd* bfd,
276 char* disassembler_options);
277 static void parse_fake_insn(disassembler_ftype dfn,
278 struct disassemble_info* dinfo);
280 static void setup_app_data(struct hsdis_app_data* app_data,
281 const char* caller_options) {
282 /* Make reasonable defaults for null callbacks.
283 A non-null stream for a null callback is assumed to be a FILE* for output.
284 Events are rendered as XML.
285 */
286 set_optional_callbacks(app_data);
288 /* Look into caller_options for anything interesting. */
289 if (caller_options != NULL)
290 parse_caller_options(app_data, caller_options);
292 /* Discover which architecture we are going to disassemble. */
293 app_data->arch_name = &app_data->mach_option[0];
294 if (app_data->arch_name[0] == '\0')
295 app_data->arch_name = native_arch_name();
296 app_data->arch_info = find_arch_info(app_data->arch_name);
298 /* Make a fake bfd to hold the arch. and byteorder info. */
299 struct {
300 bfd_target empty_xvec;
301 bfd empty_bfd;
302 } buf;
303 bfd* native_bfd = get_native_bfd(app_data->arch_info,
304 /* to avoid malloc: */
305 &buf.empty_bfd, &buf.empty_xvec);
306 init_disassemble_info_from_bfd(&app_data->dinfo,
307 app_data->printf_stream,
308 app_data->printf_callback,
309 native_bfd,
310 /* On PowerPC we get warnings, if we pass empty options */
311 (caller_options == NULL) ? NULL : app_data->insn_options);
313 /* Finish linking together the various callback blocks. */
314 app_data->dinfo.application_data = (void*) app_data;
315 app_data->dfn = disassembler(native_bfd);
316 app_data->dinfo.print_address_func = hsdis_print_address_func;
317 app_data->dinfo.read_memory_func = hsdis_read_memory_func;
319 if (app_data->dfn == NULL) {
320 const char* bad = app_data->arch_name;
321 static bool complained;
322 if (bad == &app_data->mach_option[0])
323 print_help(app_data, "bad mach=%s", bad);
324 else if (!complained)
325 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
326 complained = true;
327 /* must bail out */
328 app_data->losing = true;
329 return;
330 }
332 parse_fake_insn(app_data->dfn, &app_data->dinfo);
333 }
336 /* ignore all events, return a null */
337 static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
338 return NULL;
339 }
341 /* print all events as XML markup */
342 static void* xml_event_callback(void* stream, const char* event, void* arg) {
343 FILE* fp = (FILE*) stream;
344 #define NS_PFX "dis:"
345 if (event[0] != '/') {
346 /* issue the tag, with or without a formatted argument */
347 fprintf(fp, "<"NS_PFX);
348 fprintf(fp, event, arg);
349 fprintf(fp, ">");
350 } else {
351 ++event; /* skip slash */
352 const char* argp = strchr(event, ' ');
353 if (argp == NULL) {
354 /* no arguments; just issue the closing tag */
355 fprintf(fp, "</"NS_PFX"%s>", event);
356 } else {
357 /* split out the closing attributes as <dis:foo_done attr='val'/> */
358 int event_prefix = (argp - event);
359 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
360 fprintf(fp, argp, arg);
361 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
362 }
363 }
364 return NULL;
365 }
367 static void set_optional_callbacks(struct hsdis_app_data* app_data) {
368 if (app_data->printf_callback == NULL) {
369 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
370 FILE* fprintf_stream = stdout;
371 app_data->printf_callback = (printf_callback_t) fprintf_callback;
372 if (app_data->printf_stream == NULL)
373 app_data->printf_stream = (void*) fprintf_stream;
374 }
375 if (app_data->event_callback == NULL) {
376 if (app_data->event_stream == NULL)
377 app_data->event_callback = &null_event_callback;
378 else
379 app_data->event_callback = &xml_event_callback;
380 }
382 }
384 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
385 char* iop_base = app_data->insn_options;
386 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
387 char* iop = iop_base;
388 const char* p;
389 for (p = caller_options; p != NULL; ) {
390 const char* q = strchr(p, ',');
391 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
392 if (plen == 4 && strncmp(p, "help", plen) == 0) {
393 print_help(app_data, NULL, NULL);
394 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
395 char* mach_option = app_data->mach_option;
396 size_t mach_size = sizeof(app_data->mach_option);
397 mach_size -= 1; /*leave room for the null*/
398 if (plen > mach_size) plen = mach_size;
399 strncpy(mach_option, p, plen);
400 mach_option[plen] = '\0';
401 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
402 // do not pass these to the next level
403 } else {
404 /* just copy it; {i386,sparc}-dis.c might like to see it */
405 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
406 if (iop + plen > iop_limit)
407 plen = iop_limit - iop;
408 strncpy(iop, p, plen);
409 iop += plen;
410 }
411 p = q;
412 }
413 }
415 static void print_help(struct hsdis_app_data* app_data,
416 const char* msg, const char* arg) {
417 DECL_PRINTF_CALLBACK(app_data);
418 if (msg != NULL) {
419 (*printf_callback)(printf_stream, "hsdis: ");
420 (*printf_callback)(printf_stream, msg, arg);
421 (*printf_callback)(printf_stream, "\n");
422 }
423 (*printf_callback)(printf_stream, "hsdis output options:\n");
424 if (printf_callback == (printf_callback_t) &fprintf)
425 disassembler_usage((FILE*) printf_stream);
426 else
427 disassembler_usage(stderr); /* better than nothing */
428 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
429 #if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
430 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
431 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
432 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
433 #endif
434 (*printf_callback)(printf_stream, " help print this message\n");
435 }
438 /* low-level bfd and arch stuff that binutils doesn't do for us */
440 static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
441 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
442 if (arch_info == NULL) {
443 extern const bfd_arch_info_type bfd_default_arch_struct;
444 arch_info = &bfd_default_arch_struct;
445 }
446 return arch_info;
447 }
449 static const char* native_arch_name() {
450 const char* res = NULL;
451 #ifdef LIBARCH_i386
452 res = "i386";
453 #endif
454 #ifdef LIBARCH_amd64
455 res = "i386:x86-64";
456 #endif
457 #ifdef LIBARCH_sparc
458 res = "sparc:v8plusb";
459 #endif
460 #ifdef LIBARCH_sparcv9
461 res = "sparc:v9b";
462 #endif
463 #ifdef LIBARCH_ppc64
464 res = "powerpc:common64";
465 #endif
466 if (res == NULL)
467 res = "architecture not set in Makefile!";
468 return res;
469 }
471 static enum bfd_endian native_endian() {
472 int32_t endian_test = 'x';
473 if (*(const char*) &endian_test == 'x')
474 return BFD_ENDIAN_LITTLE;
475 else
476 return BFD_ENDIAN_BIG;
477 }
479 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
480 bfd* empty_bfd, bfd_target* empty_xvec) {
481 memset(empty_bfd, 0, sizeof(*empty_bfd));
482 memset(empty_xvec, 0, sizeof(*empty_xvec));
483 empty_xvec->flavour = bfd_target_unknown_flavour;
484 empty_xvec->byteorder = native_endian();
485 empty_bfd->xvec = empty_xvec;
486 empty_bfd->arch_info = arch_info;
487 return empty_bfd;
488 }
490 static int read_zero_data_only(bfd_vma ignore_p,
491 bfd_byte* myaddr, unsigned int length,
492 struct disassemble_info *ignore_info) {
493 memset(myaddr, 0, length);
494 return 0;
495 }
496 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
497 return 0;
498 }
500 /* Prime the pump by running the selected disassembler on a null input.
501 This forces the machine-specific disassembler to divulge invariant
502 information like bytes_per_line.
503 */
504 static void parse_fake_insn(disassembler_ftype dfn,
505 struct disassemble_info* dinfo) {
506 typedef int (*read_memory_ftype)
507 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
508 struct disassemble_info *info);
509 read_memory_ftype read_memory_func = dinfo->read_memory_func;
510 fprintf_ftype fprintf_func = dinfo->fprintf_func;
512 dinfo->read_memory_func = &read_zero_data_only;
513 dinfo->fprintf_func = &print_to_dev_null;
514 (*dfn)(0, dinfo);
516 /* put it back */
517 dinfo->read_memory_func = read_memory_func;
518 dinfo->fprintf_func = fprintf_func;
519 }
521 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
522 void *stream,
523 fprintf_ftype fprintf_func,
524 bfd* abfd,
525 char* disassembler_options) {
526 init_disassemble_info(dinfo, stream, fprintf_func);
528 dinfo->flavour = bfd_get_flavour(abfd);
529 dinfo->arch = bfd_get_arch(abfd);
530 dinfo->mach = bfd_get_mach(abfd);
531 dinfo->disassembler_options = disassembler_options;
532 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
533 dinfo->skip_zeroes = sizeof(void*) * 2;
534 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
535 dinfo->disassembler_needs_relocs = FALSE;
537 if (bfd_big_endian(abfd))
538 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
539 else if (bfd_little_endian(abfd))
540 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
541 else
542 dinfo->endian = native_endian();
544 disassemble_init_for_target(dinfo);
545 }