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