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