src/share/tools/hsdis/hsdis.c

Fri, 26 Jul 2013 10:12:15 +0200

author
simonis
date
Fri, 26 Jul 2013 10:12:15 +0200
changeset 6456
c9f0adfb4a8b
parent 5152
ec922e5c545a
child 6503
a9becfeecd1b
permissions
-rw-r--r--

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

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

mercurial