Print this page
3317 dis(1) should support cross-target disassembly

Split Close
Expand all
Collapse all
          --- old/usr/src/lib/libdisasm/common/libdisasm.c
          +++ new/usr/src/lib/libdisasm/common/libdisasm.c
↓ open down ↓ 14 lines elided ↑ open up ↑
  15   15   * If applicable, add the following below this CDDL HEADER, with the
  16   16   * fields enclosed by brackets "[]" replaced with your own identifying
  17   17   * information: Portions Copyright [yyyy] [name of copyright owner]
  18   18   *
  19   19   * CDDL HEADER END
  20   20   */
  21   21  
  22   22  /*
  23   23   * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
  24   24   * Use is subject to license terms.
       25 + * Copyright 2012 Joshua M. Clulow <josh@sysmgr.org>
  25   26   */
  26   27  
  27      -#pragma ident   "%Z%%M% %I%     %E% SMI"
  28      -
  29   28  #include <libdisasm.h>
  30   29  #include <stdlib.h>
  31   30  #ifdef DIS_STANDALONE
  32   31  #include <mdb/mdb_modapi.h>
  33   32  #endif
  34   33  
       34 +#include "libdisasm_impl.h"
       35 +
  35   36  static int _dis_errno;
  36   37  
  37   38  /*
       39 + * If we're building the standalone library, then we only want to
       40 + * include support for disassembly of the native architecture.
       41 + * The regular shared library should include support for all
       42 + * architectures.
       43 + */
       44 +#if !defined(DIS_STANDALONE) || defined(__i386) || defined(__amd64)
       45 +extern dis_arch_t dis_arch_i386;
       46 +#endif
       47 +#if !defined(DIS_STANDALONE) || defined(__sparc)
       48 +extern dis_arch_t dis_arch_sparc;
       49 +#endif
       50 +
       51 +static dis_arch_t *dis_archs[] = {
       52 +#if !defined(DIS_STANDALONE) || defined(__i386) || defined(__amd64)
       53 +        &dis_arch_i386,
       54 +#endif
       55 +#if !defined(DIS_STANDALONE) || defined(__sparc)
       56 +        &dis_arch_sparc,
       57 +#endif
       58 +        NULL
       59 +};
       60 +
       61 +/*
  38   62   * For the standalone library, we need to link against mdb's malloc/free.
  39   63   * Otherwise, use the standard malloc/free.
  40   64   */
  41   65  #ifdef DIS_STANDALONE
  42   66  void *
  43   67  dis_zalloc(size_t bytes)
  44   68  {
  45   69          return (mdb_zalloc(bytes, UM_SLEEP));
  46   70  }
  47   71  
↓ open down ↓ 31 lines elided ↑ open up ↑
  79  103  }
  80  104  
  81  105  const char *
  82  106  dis_strerror(int error)
  83  107  {
  84  108          switch (error) {
  85  109          case E_DIS_NOMEM:
  86  110                  return ("out of memory");
  87  111          case E_DIS_INVALFLAG:
  88  112                  return ("invalid flags for this architecture");
      113 +        case E_DIS_UNSUPARCH:
      114 +                return ("unsupported machine architecture");
  89  115          default:
  90  116                  return ("unknown error");
  91  117          }
  92  118  }
      119 +
      120 +void
      121 +dis_set_data(dis_handle_t *dhp, void *data)
      122 +{
      123 +        dhp->dh_data = data;
      124 +}
      125 +
      126 +void
      127 +dis_flags_set(dis_handle_t *dhp, int f)
      128 +{
      129 +        dhp->dh_flags |= f;
      130 +}
      131 +
      132 +void
      133 +dis_flags_clear(dis_handle_t *dhp, int f)
      134 +{
      135 +        dhp->dh_flags &= ~f;
      136 +}
      137 +
      138 +void
      139 +dis_handle_destroy(dis_handle_t *dhp)
      140 +{
      141 +        dhp->dh_arch->da_handle_detach(dhp);
      142 +        dis_free(dhp, sizeof (dis_handle_t));
      143 +}
      144 +
      145 +dis_handle_t *
      146 +dis_handle_create(int flags, void *data, dis_lookup_f lookup_func,
      147 +    dis_read_f read_func)
      148 +{
      149 +        dis_handle_t *dhp;
      150 +        dis_arch_t *arch = NULL;
      151 +        int i;
      152 +
      153 +        /* Select an architecture based on flags */
      154 +        for (i = 0; dis_archs[i] != NULL; i++) {
      155 +                if (dis_archs[i]->da_supports_flags(flags)) {
      156 +                        arch = dis_archs[i];
      157 +                        break;
      158 +                }
      159 +        }
      160 +        if (arch == NULL) {
      161 +                (void) dis_seterrno(E_DIS_UNSUPARCH);
      162 +                return (NULL);
      163 +        }
      164 +
      165 +        if ((dhp = dis_zalloc(sizeof (dis_handle_t))) == NULL) {
      166 +                (void) dis_seterrno(E_DIS_NOMEM);
      167 +                return (NULL);
      168 +        }
      169 +        dhp->dh_arch = arch;
      170 +        dhp->dh_lookup = lookup_func;
      171 +        dhp->dh_read = read_func;
      172 +        dhp->dh_flags = flags;
      173 +        dhp->dh_data = data;
      174 +
      175 +        /*
      176 +         * Allow the architecture-specific code to allocate
      177 +         * its private data.
      178 +         */
      179 +        if (arch->da_handle_attach(dhp) != 0) {
      180 +                dis_free(dhp, sizeof (dis_handle_t));
      181 +                /* dis errno already set */
      182 +                return (NULL);
      183 +        }
      184 +
      185 +        return (dhp);
      186 +}
      187 +
      188 +int
      189 +dis_disassemble(dis_handle_t *dhp, uint64_t addr, char *buf, size_t buflen)
      190 +{
      191 +        return (dhp->dh_arch->da_disassemble(dhp, addr, buf, buflen));
      192 +}
      193 +
      194 +uint64_t
      195 +dis_previnstr(dis_handle_t *dhp, uint64_t pc, int n)
      196 +{
      197 +        return (dhp->dh_arch->da_previnstr(dhp, pc, n));
      198 +}
      199 +
      200 +int
      201 +dis_min_instrlen(dis_handle_t *dhp)
      202 +{
      203 +        return (dhp->dh_arch->da_min_instrlen(dhp));
      204 +}
      205 +
      206 +int
      207 +dis_max_instrlen(dis_handle_t *dhp)
      208 +{
      209 +        return (dhp->dh_arch->da_max_instrlen(dhp));
      210 +}
      211 +
      212 +int
      213 +dis_instrlen(dis_handle_t *dhp, uint64_t pc)
      214 +{
      215 +        return (dhp->dh_arch->da_instrlen(dhp, pc));
      216 +}
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX