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