src/share/tools/hsdis/hsdis.c

Fri, 03 Apr 2020 14:14:26 +0100

author
aph
date
Fri, 03 Apr 2020 14:14:26 +0100
changeset 9920
3a3803a0c789
parent 9493
882a55369341
child 9931
fd44df5e3bc3
permissions
-rw-r--r--

8076475: Misuses of strncpy/strncat
Summary: Various small fixes around strncpy and strncat
Reviewed-by: andrew

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

mercurial