src/share/tools/hsdis/hsdis.c

Fri, 02 Nov 2012 13:30:47 -0700

author
minqi
date
Fri, 02 Nov 2012 13:30:47 -0700
changeset 4244
3d701c802d01
parent 4093
5a98bf7d847b
child 5152
ec922e5c545a
permissions
-rw-r--r--

8000489: older builds of hsdis don't work anymore after 6879063
Summary: The old function not defined properly, need a definition for export in dll. Also changes made to let new jvm work with old hsdis.
Reviewed-by: jrose, sspitsyn, kmo
Contributed-by: yumin.qi@oracle.com

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

mercurial