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