annotate src/utils/hsdis/hsdis.c @ 51271:b6e0bfe4a6ec

8208541: non-ASCII characters in hsdis UPL text Summary: replace Unicode left/right double quote pair with normal ASCII double quotes Reviewed-by: jrose
author dbuck
date Wed, 01 Aug 2018 01:40:44 -0400
parents 59b0d8afc831
children 5064f30ac015
rev   line source
jrose@347 1 /*
dbuck@46748 2 * Copyright (c) 2008, 2017, Oracle and/or its affiliates. All rights reserved.
jrose@347 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jrose@347 4 *
dbuck@51202 5 * The Universal Permissive License (UPL), Version 1.0
jrose@347 6 *
dbuck@51202 7 * Subject to the condition set forth below, permission is hereby granted to
dbuck@51202 8 * any person obtaining a copy of this software, associated documentation
dbuck@51202 9 * and/or data (collectively the "Software"), free of charge and under any
dbuck@51202 10 * and all copyright rights in the Software, and any and all patent rights
dbuck@51202 11 * owned or freely licensable by each licensor hereunder covering either (i)
dbuck@51202 12 * the unmodified Software as contributed to or provided by such licensor,
dbuck@51202 13 * or (ii) the Larger Works (as defined below), to deal in both
jrose@347 14 *
dbuck@51202 15 * (a) the Software, and
dbuck@51202 16 *
dbuck@51202 17 * (b) any piece of software and/or hardware listed in the lrgrwrks.txt file
dbuck@51271 18 * if one is included with the Software (each a "Larger Work" to which the
dbuck@51202 19 * Software is contributed by such licensors),
dbuck@51202 20 *
dbuck@51202 21 * without restriction, including without limitation the rights to copy,
dbuck@51202 22 * create derivative works of, display, perform, and distribute the Software
dbuck@51202 23 * and make, use, sell, offer for sale, import, export, have made, and have
dbuck@51202 24 * sold the Software and the Larger Work(s), and to sublicense the foregoing
dbuck@51202 25 * rights on either these or other terms.
dbuck@51202 26 *
dbuck@51202 27 * This license is subject to the following condition:
dbuck@51202 28 *
dbuck@51202 29 * The above copyright notice and either this complete permission notice or
dbuck@51202 30 * at a minimum a reference to the UPL must be included in all copies or
dbuck@51202 31 * substantial portions of the Software.
dbuck@51202 32 *
dbuck@51202 33 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
dbuck@51202 34 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
dbuck@51202 35 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
dbuck@51202 36 * NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
dbuck@51202 37 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
dbuck@51202 38 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
dbuck@51202 39 * USE OR OTHER DEALINGS IN THE SOFTWARE.
jrose@347 40 *
trims@5547 41 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 42 * or visit www.oracle.com if you need additional information or have any
trims@5547 43 * questions.
jrose@347 44 *
jrose@347 45 */
jrose@347 46
jrose@347 47 /* hsdis.c -- dump a range of addresses as native instructions
jrose@347 48 This implements the plugin protocol required by the
jrose@347 49 HotSpot PrintAssembly option.
jrose@347 50 */
jrose@347 51
anoll@17618 52 #include <config.h> /* required by bfd.h */
jrose@347 53 #include <libiberty.h>
jrose@347 54 #include <bfd.h>
jrose@347 55 #include <dis-asm.h>
never@2564 56 #include <inttypes.h>
minqi@13873 57 #include <string.h>
minqi@13873 58 #include <errno.h>
minqi@13873 59 #include "hsdis.h"
jrose@347 60
jrose@347 61 #ifndef bool
jrose@347 62 #define bool int
jrose@347 63 #define true 1
jrose@347 64 #define false 0
jrose@347 65 #endif /*bool*/
jrose@347 66
jrose@347 67 /* short names for stuff in hsdis.h */
jrose@347 68 typedef decode_instructions_event_callback_ftype event_callback_t;
jrose@347 69 typedef decode_instructions_printf_callback_ftype printf_callback_t;
jrose@347 70
jrose@347 71 /* disassemble_info.application_data object */
jrose@347 72 struct hsdis_app_data {
minqi@13873 73 /* virtual address of data */
minqi@13873 74 uintptr_t start_va, end_va;
minqi@13873 75 /* the instructions to be decoded */
minqi@13873 76 unsigned char* buffer;
minqi@13873 77 uintptr_t length;
jrose@347 78 event_callback_t event_callback; void* event_stream;
jrose@347 79 printf_callback_t printf_callback; void* printf_stream;
jrose@347 80 bool losing;
minqi@13873 81 bool do_newline;
jrose@347 82
jrose@347 83 /* the architecture being disassembled */
jrose@347 84 const char* arch_name;
jrose@347 85 const bfd_arch_info_type* arch_info;
jrose@347 86
jrose@347 87 /* the disassembler we are going to use: */
jrose@347 88 disassembler_ftype dfn;
jrose@347 89 struct disassemble_info dinfo; /* the actual struct! */
jrose@347 90
jrose@347 91 char mach_option[64];
jrose@347 92 char insn_options[256];
jrose@347 93 };
jrose@347 94
minqi@13873 95 static void* decode(struct hsdis_app_data* app_data, const char* options);
minqi@13873 96
jrose@347 97 #define DECL_APP_DATA(dinfo) \
jrose@347 98 struct hsdis_app_data* app_data = (struct hsdis_app_data*) (dinfo)->application_data
jrose@347 99
jrose@347 100 #define DECL_EVENT_CALLBACK(app_data) \
jrose@347 101 event_callback_t event_callback = (app_data)->event_callback; \
jrose@347 102 void* event_stream = (app_data)->event_stream
jrose@347 103
jrose@347 104 #define DECL_PRINTF_CALLBACK(app_data) \
jrose@347 105 printf_callback_t printf_callback = (app_data)->printf_callback; \
jrose@347 106 void* printf_stream = (app_data)->printf_stream
jrose@347 107
jrose@347 108
jrose@347 109 static void print_help(struct hsdis_app_data* app_data,
jrose@347 110 const char* msg, const char* arg);
jrose@347 111 static void setup_app_data(struct hsdis_app_data* app_data,
jrose@347 112 const char* options);
jrose@347 113 static const char* format_insn_close(const char* close,
jrose@347 114 disassemble_info* dinfo,
jrose@347 115 char* buf, size_t bufsize);
jrose@347 116
jrose@347 117 void*
jrose@347 118 #ifdef DLL_ENTRY
jrose@347 119 DLL_ENTRY
jrose@347 120 #endif
minqi@13873 121 decode_instructions_virtual(uintptr_t start_va, uintptr_t end_va,
minqi@13873 122 unsigned char* buffer, uintptr_t length,
minqi@13873 123 event_callback_t event_callback_arg, void* event_stream_arg,
minqi@13873 124 printf_callback_t printf_callback_arg, void* printf_stream_arg,
minqi@14384 125 const char* options, int newline) {
minqi@13873 126 struct hsdis_app_data app_data;
minqi@13873 127 memset(&app_data, 0, sizeof(app_data));
minqi@13873 128 app_data.start_va = start_va;
minqi@13873 129 app_data.end_va = end_va;
minqi@13873 130 app_data.buffer = buffer;
minqi@13873 131 app_data.length = length;
minqi@13873 132 app_data.event_callback = event_callback_arg;
minqi@13873 133 app_data.event_stream = event_stream_arg;
minqi@13873 134 app_data.printf_callback = printf_callback_arg;
minqi@13873 135 app_data.printf_stream = printf_stream_arg;
minqi@14384 136 app_data.do_newline = newline == 0 ? false : true;
minqi@13873 137
minqi@13873 138 return decode(&app_data, options);
minqi@13873 139 }
minqi@13873 140
minqi@13873 141 /* This is the compatability interface for older version of hotspot */
minqi@13873 142 void*
minqi@13873 143 #ifdef DLL_ENTRY
minqi@13873 144 DLL_ENTRY
minqi@13873 145 #endif
jrose@347 146 decode_instructions(void* start_pv, void* end_pv,
jrose@347 147 event_callback_t event_callback_arg, void* event_stream_arg,
jrose@347 148 printf_callback_t printf_callback_arg, void* printf_stream_arg,
jrose@347 149 const char* options) {
vlivanov@35542 150 return decode_instructions_virtual((uintptr_t)start_pv,
vlivanov@35542 151 (uintptr_t)end_pv,
vlivanov@35542 152 (unsigned char*)start_pv,
vlivanov@35542 153 (uintptr_t)end_pv - (uintptr_t)start_pv,
vlivanov@35542 154 event_callback_arg,
vlivanov@35542 155 event_stream_arg,
vlivanov@35542 156 printf_callback_arg,
vlivanov@35542 157 printf_stream_arg,
vlivanov@35542 158 options, false);
minqi@13873 159 }
jrose@347 160
minqi@13873 161 static void* decode(struct hsdis_app_data* app_data, const char* options) {
minqi@13873 162 setup_app_data(app_data, options);
jrose@347 163 char buf[128];
jrose@347 164
jrose@347 165 {
jrose@347 166 /* now reload everything from app_data: */
minqi@13873 167 DECL_EVENT_CALLBACK(app_data);
minqi@13873 168 DECL_PRINTF_CALLBACK(app_data);
minqi@13873 169 uintptr_t start = app_data->start_va;
minqi@13873 170 uintptr_t end = app_data->end_va;
jrose@347 171 uintptr_t p = start;
jrose@347 172
jrose@347 173 (*event_callback)(event_stream, "insns", (void*)start);
jrose@347 174
jrose@347 175 (*event_callback)(event_stream, "mach name='%s'",
minqi@13873 176 (void*) app_data->arch_info->printable_name);
minqi@13873 177 if (app_data->dinfo.bytes_per_line != 0) {
jrose@347 178 (*event_callback)(event_stream, "format bytes-per-line='%p'/",
minqi@13873 179 (void*)(intptr_t) app_data->dinfo.bytes_per_line);
jrose@347 180 }
jrose@347 181
minqi@13873 182 while (p < end && !app_data->losing) {
jrose@347 183 (*event_callback)(event_stream, "insn", (void*) p);
jrose@347 184
jrose@347 185 /* reset certain state, so we can read it with confidence */
minqi@13873 186 app_data->dinfo.insn_info_valid = 0;
minqi@13873 187 app_data->dinfo.branch_delay_insns = 0;
minqi@13873 188 app_data->dinfo.data_size = 0;
minqi@13873 189 app_data->dinfo.insn_type = 0;
jrose@347 190
minqi@13873 191 int size = (*app_data->dfn)((bfd_vma) p, &app_data->dinfo);
jrose@347 192
jrose@347 193 if (size > 0) p += size;
minqi@13873 194 else app_data->losing = true;
jrose@347 195
minqi@13873 196 if (!app_data->losing) {
minqi@13873 197 const char* insn_close = format_insn_close("/insn", &app_data->dinfo,
minqi@13873 198 buf, sizeof(buf));
minqi@14384 199 (*event_callback)(event_stream, insn_close, (void*) p);
jrose@347 200
minqi@13873 201 if (app_data->do_newline) {
minqi@13873 202 /* follow each complete insn by a nice newline */
minqi@13873 203 (*printf_callback)(printf_stream, "\n");
minqi@13873 204 }
minqi@13873 205 }
jrose@347 206 }
jrose@347 207
minqi@14384 208 if (app_data->losing) (*event_callback)(event_stream, "/insns", (void*) p);
jrose@347 209 return (void*) p;
jrose@347 210 }
jrose@347 211 }
jrose@347 212
jrose@347 213 /* take the address of the function, for luck, and also test the typedef: */
minqi@14384 214 const decode_func_vtype decode_func_virtual_address = &decode_instructions_virtual;
minqi@14384 215 const decode_func_stype decode_func_address = &decode_instructions;
jrose@347 216
jrose@347 217 static const char* format_insn_close(const char* close,
jrose@347 218 disassemble_info* dinfo,
jrose@347 219 char* buf, size_t bufsize) {
jrose@347 220 if (!dinfo->insn_info_valid)
jrose@347 221 return close;
jrose@347 222 enum dis_insn_type itype = dinfo->insn_type;
jrose@347 223 int dsize = dinfo->data_size, delays = dinfo->branch_delay_insns;
jrose@347 224 if ((itype == dis_nonbranch && (dsize | delays) == 0)
jrose@347 225 || (strlen(close) + 3*20 > bufsize))
jrose@347 226 return close;
jrose@347 227
jrose@347 228 const char* type = "unknown";
jrose@347 229 switch (itype) {
jrose@347 230 case dis_nonbranch: type = NULL; break;
jrose@347 231 case dis_branch: type = "branch"; break;
jrose@347 232 case dis_condbranch: type = "condbranch"; break;
jrose@347 233 case dis_jsr: type = "jsr"; break;
jrose@347 234 case dis_condjsr: type = "condjsr"; break;
jrose@347 235 case dis_dref: type = "dref"; break;
jrose@347 236 case dis_dref2: type = "dref2"; break;
vlivanov@35542 237 case dis_noninsn: type = "noninsn"; break;
jrose@347 238 }
jrose@347 239
jrose@347 240 strcpy(buf, close);
jrose@347 241 char* p = buf;
jrose@347 242 if (type) sprintf(p += strlen(p), " type='%s'", type);
jrose@347 243 if (dsize) sprintf(p += strlen(p), " dsize='%d'", dsize);
jrose@347 244 if (delays) sprintf(p += strlen(p), " delay='%d'", delays);
jrose@347 245 return buf;
jrose@347 246 }
jrose@347 247
jrose@347 248 /* handler functions */
jrose@347 249
jrose@347 250 static int
jrose@347 251 hsdis_read_memory_func(bfd_vma memaddr,
jrose@347 252 bfd_byte* myaddr,
jrose@347 253 unsigned int length,
jrose@347 254 struct disassemble_info* dinfo) {
jrose@347 255 DECL_APP_DATA(dinfo);
minqi@13873 256 /* convert the virtual address memaddr into an address within memory buffer */
minqi@13873 257 uintptr_t offset = ((uintptr_t) memaddr) - app_data->start_va;
minqi@13873 258 if (offset + length > app_data->length) {
jrose@347 259 /* read is out of bounds */
jrose@347 260 return EIO;
jrose@347 261 } else {
minqi@13873 262 memcpy(myaddr, (bfd_byte*) (app_data->buffer + offset), length);
jrose@347 263 return 0;
jrose@347 264 }
jrose@347 265 }
jrose@347 266
jrose@347 267 static void
jrose@347 268 hsdis_print_address_func(bfd_vma vma, struct disassemble_info* dinfo) {
jrose@347 269 /* the actual value to print: */
jrose@347 270 void* addr_value = (void*) (uintptr_t) vma;
jrose@347 271 DECL_APP_DATA(dinfo);
jrose@347 272 DECL_EVENT_CALLBACK(app_data);
jrose@347 273
jrose@347 274 /* issue the event: */
jrose@347 275 void* result =
jrose@347 276 (*event_callback)(event_stream, "addr/", addr_value);
jrose@347 277 if (result == NULL) {
jrose@347 278 /* event declined */
jrose@347 279 generic_print_address(vma, dinfo);
jrose@347 280 }
jrose@347 281 }
jrose@347 282
jrose@347 283
jrose@347 284 /* configuration */
jrose@347 285
jrose@347 286 static void set_optional_callbacks(struct hsdis_app_data* app_data);
jrose@347 287 static void parse_caller_options(struct hsdis_app_data* app_data,
jrose@347 288 const char* caller_options);
jrose@347 289 static const char* native_arch_name();
jrose@347 290 static enum bfd_endian native_endian();
jrose@347 291 static const bfd_arch_info_type* find_arch_info(const char* arch_nane);
jrose@347 292 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
jrose@347 293 /* to avoid malloc: */
jrose@347 294 bfd* empty_bfd, bfd_target* empty_xvec);
jrose@347 295 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
jrose@347 296 void *stream,
jrose@347 297 fprintf_ftype fprintf_func,
jrose@347 298 bfd* bfd,
jrose@347 299 char* disassembler_options);
jrose@347 300 static void parse_fake_insn(disassembler_ftype dfn,
jrose@347 301 struct disassemble_info* dinfo);
jrose@347 302
jrose@347 303 static void setup_app_data(struct hsdis_app_data* app_data,
jrose@347 304 const char* caller_options) {
jrose@347 305 /* Make reasonable defaults for null callbacks.
jrose@347 306 A non-null stream for a null callback is assumed to be a FILE* for output.
jrose@347 307 Events are rendered as XML.
jrose@347 308 */
jrose@347 309 set_optional_callbacks(app_data);
jrose@347 310
jrose@347 311 /* Look into caller_options for anything interesting. */
jrose@347 312 if (caller_options != NULL)
jrose@347 313 parse_caller_options(app_data, caller_options);
jrose@347 314
jrose@347 315 /* Discover which architecture we are going to disassemble. */
jrose@347 316 app_data->arch_name = &app_data->mach_option[0];
jrose@347 317 if (app_data->arch_name[0] == '\0')
jrose@347 318 app_data->arch_name = native_arch_name();
jrose@347 319 app_data->arch_info = find_arch_info(app_data->arch_name);
jrose@347 320
jrose@347 321 /* Make a fake bfd to hold the arch. and byteorder info. */
jrose@347 322 struct {
jrose@347 323 bfd_target empty_xvec;
jrose@347 324 bfd empty_bfd;
jrose@347 325 } buf;
jrose@347 326 bfd* native_bfd = get_native_bfd(app_data->arch_info,
jrose@347 327 /* to avoid malloc: */
jrose@347 328 &buf.empty_bfd, &buf.empty_xvec);
jrose@347 329 init_disassemble_info_from_bfd(&app_data->dinfo,
jrose@347 330 app_data->printf_stream,
jrose@347 331 app_data->printf_callback,
jrose@347 332 native_bfd,
simonis@22822 333 /* On PowerPC we get warnings, if we pass empty options */
simonis@22822 334 (caller_options == NULL) ? NULL : app_data->insn_options);
jrose@347 335
jrose@347 336 /* Finish linking together the various callback blocks. */
jrose@347 337 app_data->dinfo.application_data = (void*) app_data;
jrose@347 338 app_data->dfn = disassembler(native_bfd);
jrose@347 339 app_data->dinfo.print_address_func = hsdis_print_address_func;
jrose@347 340 app_data->dinfo.read_memory_func = hsdis_read_memory_func;
jrose@347 341
jrose@347 342 if (app_data->dfn == NULL) {
jrose@347 343 const char* bad = app_data->arch_name;
jrose@347 344 static bool complained;
jrose@347 345 if (bad == &app_data->mach_option[0])
jrose@347 346 print_help(app_data, "bad mach=%s", bad);
jrose@347 347 else if (!complained)
jrose@347 348 print_help(app_data, "bad native mach=%s; please port hsdis to this platform", bad);
jrose@347 349 complained = true;
jrose@347 350 /* must bail out */
jrose@347 351 app_data->losing = true;
jrose@347 352 return;
jrose@347 353 }
jrose@347 354
jrose@347 355 parse_fake_insn(app_data->dfn, &app_data->dinfo);
jrose@347 356 }
jrose@347 357
jrose@347 358
jrose@347 359 /* ignore all events, return a null */
jrose@347 360 static void* null_event_callback(void* ignore_stream, const char* ignore_event, void* arg) {
jrose@347 361 return NULL;
jrose@347 362 }
jrose@347 363
jrose@347 364 /* print all events as XML markup */
jrose@347 365 static void* xml_event_callback(void* stream, const char* event, void* arg) {
jrose@347 366 FILE* fp = (FILE*) stream;
jrose@347 367 #define NS_PFX "dis:"
jrose@347 368 if (event[0] != '/') {
jrose@347 369 /* issue the tag, with or without a formatted argument */
jrose@347 370 fprintf(fp, "<"NS_PFX);
jrose@347 371 fprintf(fp, event, arg);
jrose@347 372 fprintf(fp, ">");
jrose@347 373 } else {
jrose@347 374 ++event; /* skip slash */
jrose@347 375 const char* argp = strchr(event, ' ');
jrose@347 376 if (argp == NULL) {
jrose@347 377 /* no arguments; just issue the closing tag */
jrose@347 378 fprintf(fp, "</"NS_PFX"%s>", event);
jrose@347 379 } else {
jrose@347 380 /* split out the closing attributes as <dis:foo_done attr='val'/> */
jrose@347 381 int event_prefix = (argp - event);
jrose@347 382 fprintf(fp, "<"NS_PFX"%.*s_done", event_prefix, event);
jrose@347 383 fprintf(fp, argp, arg);
jrose@347 384 fprintf(fp, "/></"NS_PFX"%.*s>", event_prefix, event);
jrose@347 385 }
jrose@347 386 }
jrose@347 387 return NULL;
jrose@347 388 }
jrose@347 389
jrose@347 390 static void set_optional_callbacks(struct hsdis_app_data* app_data) {
jrose@347 391 if (app_data->printf_callback == NULL) {
jrose@347 392 int (*fprintf_callback)(FILE*, const char*, ...) = &fprintf;
jrose@347 393 FILE* fprintf_stream = stdout;
jrose@347 394 app_data->printf_callback = (printf_callback_t) fprintf_callback;
jrose@347 395 if (app_data->printf_stream == NULL)
jrose@347 396 app_data->printf_stream = (void*) fprintf_stream;
jrose@347 397 }
jrose@347 398 if (app_data->event_callback == NULL) {
jrose@347 399 if (app_data->event_stream == NULL)
jrose@347 400 app_data->event_callback = &null_event_callback;
jrose@347 401 else
jrose@347 402 app_data->event_callback = &xml_event_callback;
jrose@347 403 }
jrose@347 404
jrose@347 405 }
jrose@347 406
jrose@347 407 static void parse_caller_options(struct hsdis_app_data* app_data, const char* caller_options) {
jrose@347 408 char* iop_base = app_data->insn_options;
jrose@347 409 char* iop_limit = iop_base + sizeof(app_data->insn_options) - 1;
jrose@347 410 char* iop = iop_base;
jrose@347 411 const char* p;
jrose@347 412 for (p = caller_options; p != NULL; ) {
jrose@347 413 const char* q = strchr(p, ',');
jrose@347 414 size_t plen = (q == NULL) ? strlen(p) : ((q++) - p);
jrose@347 415 if (plen == 4 && strncmp(p, "help", plen) == 0) {
jrose@347 416 print_help(app_data, NULL, NULL);
jrose@347 417 } else if (plen >= 5 && strncmp(p, "mach=", 5) == 0) {
jrose@347 418 char* mach_option = app_data->mach_option;
jrose@347 419 size_t mach_size = sizeof(app_data->mach_option);
jrose@347 420 mach_size -= 1; /*leave room for the null*/
jrose@347 421 if (plen > mach_size) plen = mach_size;
jrose@347 422 strncpy(mach_option, p, plen);
jrose@347 423 mach_option[plen] = '\0';
kvn@11782 424 } else if (plen > 6 && strncmp(p, "hsdis-", 6) == 0) {
jrose@347 425 // do not pass these to the next level
jrose@347 426 } else {
jrose@347 427 /* just copy it; {i386,sparc}-dis.c might like to see it */
jrose@347 428 if (iop > iop_base && iop < iop_limit) (*iop++) = ',';
jrose@347 429 if (iop + plen > iop_limit)
jrose@347 430 plen = iop_limit - iop;
jrose@347 431 strncpy(iop, p, plen);
jrose@347 432 iop += plen;
jrose@347 433 }
jrose@347 434 p = q;
jrose@347 435 }
stuefe@30281 436 *iop = '\0';
jrose@347 437 }
jrose@347 438
jrose@347 439 static void print_help(struct hsdis_app_data* app_data,
jrose@347 440 const char* msg, const char* arg) {
jrose@347 441 DECL_PRINTF_CALLBACK(app_data);
jrose@347 442 if (msg != NULL) {
jrose@347 443 (*printf_callback)(printf_stream, "hsdis: ");
jrose@347 444 (*printf_callback)(printf_stream, msg, arg);
jrose@347 445 (*printf_callback)(printf_stream, "\n");
jrose@347 446 }
jrose@347 447 (*printf_callback)(printf_stream, "hsdis output options:\n");
jrose@347 448 if (printf_callback == (printf_callback_t) &fprintf)
jrose@347 449 disassembler_usage((FILE*) printf_stream);
jrose@347 450 else
jrose@347 451 disassembler_usage(stderr); /* better than nothing */
jrose@347 452 (*printf_callback)(printf_stream, " mach=<arch> select disassembly mode\n");
jrose@347 453 #if defined(LIBARCH_i386) || defined(LIBARCH_amd64)
jrose@347 454 (*printf_callback)(printf_stream, " mach=i386 select 32-bit mode\n");
jrose@347 455 (*printf_callback)(printf_stream, " mach=x86-64 select 64-bit mode\n");
jrose@347 456 (*printf_callback)(printf_stream, " suffix always print instruction suffix\n");
jrose@347 457 #endif
jrose@347 458 (*printf_callback)(printf_stream, " help print this message\n");
jrose@347 459 }
jrose@347 460
jrose@347 461
jrose@347 462 /* low-level bfd and arch stuff that binutils doesn't do for us */
jrose@347 463
jrose@347 464 static const bfd_arch_info_type* find_arch_info(const char* arch_name) {
jrose@347 465 const bfd_arch_info_type* arch_info = bfd_scan_arch(arch_name);
jrose@347 466 if (arch_info == NULL) {
jrose@347 467 extern const bfd_arch_info_type bfd_default_arch_struct;
jrose@347 468 arch_info = &bfd_default_arch_struct;
jrose@347 469 }
jrose@347 470 return arch_info;
jrose@347 471 }
jrose@347 472
jrose@347 473 static const char* native_arch_name() {
never@2564 474 const char* res = NULL;
never@2564 475 #ifdef LIBARCH_i386
minqi@13873 476 res = "i386";
never@2564 477 #endif
jrose@347 478 #ifdef LIBARCH_amd64
minqi@13873 479 res = "i386:x86-64";
jrose@347 480 #endif
jrose@347 481 #ifdef LIBARCH_sparc
minqi@13873 482 res = "sparc:v8plusb";
jrose@347 483 #endif
jrose@347 484 #ifdef LIBARCH_sparcv9
minqi@13873 485 res = "sparc:v9b";
jrose@347 486 #endif
asmundak@35075 487 #if defined(LIBARCH_ppc64) || defined(LIBARCH_ppc64le)
simonis@22822 488 res = "powerpc:common64";
simonis@22822 489 #endif
dbuck@46748 490 #ifdef LIBARCH_arm
dbuck@46748 491 res = "arm";
dbuck@46748 492 #endif
aph@29191 493 #ifdef LIBARCH_aarch64
aph@29191 494 res = "aarch64";
aph@29191 495 #endif
goetz@42062 496 #ifdef LIBARCH_s390x
goetz@42062 497 res = "s390:64-bit";
goetz@42062 498 #endif
jrose@347 499 if (res == NULL)
never@2564 500 res = "architecture not set in Makefile!";
jrose@347 501 return res;
jrose@347 502 }
jrose@347 503
jrose@347 504 static enum bfd_endian native_endian() {
jrose@347 505 int32_t endian_test = 'x';
jrose@347 506 if (*(const char*) &endian_test == 'x')
jrose@347 507 return BFD_ENDIAN_LITTLE;
jrose@347 508 else
jrose@347 509 return BFD_ENDIAN_BIG;
jrose@347 510 }
jrose@347 511
jrose@347 512 static bfd* get_native_bfd(const bfd_arch_info_type* arch_info,
jrose@347 513 bfd* empty_bfd, bfd_target* empty_xvec) {
jrose@347 514 memset(empty_bfd, 0, sizeof(*empty_bfd));
jrose@347 515 memset(empty_xvec, 0, sizeof(*empty_xvec));
jrose@347 516 empty_xvec->flavour = bfd_target_unknown_flavour;
jrose@347 517 empty_xvec->byteorder = native_endian();
jrose@347 518 empty_bfd->xvec = empty_xvec;
jrose@347 519 empty_bfd->arch_info = arch_info;
jrose@347 520 return empty_bfd;
jrose@347 521 }
jrose@347 522
jrose@347 523 static int read_zero_data_only(bfd_vma ignore_p,
jrose@347 524 bfd_byte* myaddr, unsigned int length,
jrose@347 525 struct disassemble_info *ignore_info) {
jrose@347 526 memset(myaddr, 0, length);
jrose@347 527 return 0;
jrose@347 528 }
jrose@347 529 static int print_to_dev_null(void* ignore_stream, const char* ignore_format, ...) {
jrose@347 530 return 0;
jrose@347 531 }
jrose@347 532
jrose@347 533 /* Prime the pump by running the selected disassembler on a null input.
jrose@347 534 This forces the machine-specific disassembler to divulge invariant
jrose@347 535 information like bytes_per_line.
jrose@347 536 */
jrose@347 537 static void parse_fake_insn(disassembler_ftype dfn,
jrose@347 538 struct disassemble_info* dinfo) {
jrose@347 539 typedef int (*read_memory_ftype)
jrose@347 540 (bfd_vma memaddr, bfd_byte *myaddr, unsigned int length,
jrose@347 541 struct disassemble_info *info);
jrose@347 542 read_memory_ftype read_memory_func = dinfo->read_memory_func;
jrose@347 543 fprintf_ftype fprintf_func = dinfo->fprintf_func;
jrose@347 544
jrose@347 545 dinfo->read_memory_func = &read_zero_data_only;
jrose@347 546 dinfo->fprintf_func = &print_to_dev_null;
jrose@347 547 (*dfn)(0, dinfo);
jrose@347 548
minqi@13873 549 /* put it back */
jrose@347 550 dinfo->read_memory_func = read_memory_func;
jrose@347 551 dinfo->fprintf_func = fprintf_func;
jrose@347 552 }
jrose@347 553
jrose@347 554 static void init_disassemble_info_from_bfd(struct disassemble_info* dinfo,
jrose@347 555 void *stream,
jrose@347 556 fprintf_ftype fprintf_func,
jrose@347 557 bfd* abfd,
jrose@347 558 char* disassembler_options) {
jrose@347 559 init_disassemble_info(dinfo, stream, fprintf_func);
jrose@347 560
jrose@347 561 dinfo->flavour = bfd_get_flavour(abfd);
jrose@347 562 dinfo->arch = bfd_get_arch(abfd);
jrose@347 563 dinfo->mach = bfd_get_mach(abfd);
jrose@347 564 dinfo->disassembler_options = disassembler_options;
jrose@347 565 dinfo->octets_per_byte = bfd_octets_per_byte (abfd);
jrose@347 566 dinfo->skip_zeroes = sizeof(void*) * 2;
jrose@347 567 dinfo->skip_zeroes_at_end = sizeof(void*)-1;
jrose@347 568 dinfo->disassembler_needs_relocs = FALSE;
jrose@347 569
jrose@347 570 if (bfd_big_endian(abfd))
jrose@347 571 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_BIG;
jrose@347 572 else if (bfd_little_endian(abfd))
jrose@347 573 dinfo->display_endian = dinfo->endian = BFD_ENDIAN_LITTLE;
jrose@347 574 else
jrose@347 575 dinfo->endian = native_endian();
jrose@347 576
jrose@347 577 disassemble_init_for_target(dinfo);
jrose@347 578 }